OSX: Segmentation violation caused by Screen('MakeTexture')

Hi all,

I've run into a segmentation violation problem while making textures
under OSX using Screen('MakeTexture'...). This happens when I try to
make textures from image arrays - some images work, others don't. Two
examples are attached: 'goat.jpg' always works, 'oval.jpg' virtually
always produces a segmentation violation and sometimes crashes Matlab
entirely. An example script illustrating the problem follows below, as
does the error message that Matlab outputs when things go wrong.

I would be very grateful for any hints!

Best regards,
Johannes


Here is the script that illustrates the problem. Run it from the same
folder as the two images attached to this message.
% ***************************************************************
clear all;
AssertOpenGL;
screens=Screen('Screens');
screenNumber=max(screens);
frameRate=Screen('FrameRate',screenNumber);
if(frameRate==0)
frameRate=60;
end
% Making the goat texture - this works
try
goat = imread('goat.jpg');
goattex = Screen('MakeTexture', screenNumber, goat);
disp('The goat texture worked.');
catch
error('The goat texture didn''t work.');
end
% Making the oval texture - this often fails
try
oval = imread('oval.jpg');
ovaltex = Screen('MakeTexture', screenNumber, oval);
disp('The oval texture worked.');
catch
error('The oval texture didn''t work.');
end
% ***************************************************************


Here is Matlab's error message:
------------------------------------------------------------------------
Segmentation violation detected at Mon Nov 15 14:41:09 2004
------------------------------------------------------------------------

Configuration:
MATLAB Version: 6.5.0.185000 (R13)
Operating System: Darwin 7.4.0 Darwin Kernel Version 7.4.0: Wed May
12 16:58:24 PDT 2004; root:xnu/xnu-517.7.7.obj~7/RELEASE_PPC Power
Macintosh
Window System: The XFree86 Project, Inc (40300000), display :0.0
Current Visual: 0x22 (class 4, depth 24)
Virtual Machine: Java 1.3.1 with "Apple Computer, Inc." Java
HotSpot(TM) Client VM
(mixed mode)



Segmentation violation occurred within signal handler.
Unable to complete stack trace (stack was probably corrupted)

This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for
information
on debugging MEX-files.

If it is an official MathWorks function, please
follow these steps in reporting this problem to The MathWorks so
that we have the best chance of correcting it:

1. Send this crash report to segv@... for automated
analysis.
For your convenience, this information has been recorded in:
/Users/johannes/matlab_crash_dump.2278

2. Also, if the problem is reproducible, send the crash report to
support@... along with:
- A specific list of steps that will reproduce the problem
- Any M, MEX, MDL or other files required to reproduce the
problem
- Any error messages displayed prior to this crash report
A technical support engineer will contact you with further
information.

Thank you for your assistance. Please save your workspace and restart
MATLAB before continuing your work.
Johannes:

Your images do not get posted (they changed Yahoo groups), but I bet this is a symptom
of the MakeTexture bug I wrote about here several weeks ago. Basically it is an interaction
of the version of OpenGL as implemented both in your particular video card and in OS X.
Newer versions of OpenGL allow non-square textures, but older versions did not.
Theoretically all should be okay with the release of OS X 10.4, but that is some time from
now and I still wonder if an older version of OpenGL on a video card might take
precedence and result in the same problem. What I found was that I could not make
anything but square textures on some cards, but other cards allow rectangular textures.

To work around this, I wrote impad (see below). It basically reads an image from a file
(alter if you need to calculate your image), then pads the image to square with the
background gray level you specify.

Best, Mike

PS Would be nice for PTB to run a test that reported the version of OpenGL and let the user
know which functions are affected.

%%%%% START %%%%%

function img = impad(iFile, bg)
% img = impad(iFile, bg)
%
% impad reads in an image from a file and pads it to be
% square using the specified background graylevel
%
% version = 0.2
%
% HISTORY
%
% 10/7/04 mjt wrote it
% 10/9/04 mjt sped up image copy and creation

try
in = imread(iFile);
dim = size(in); % dimensions of image
depth = max(size(dim)); % grayscale (2) or color image (3)
if (length(dim) < 3)
dim(3) = 1;
end

if (dim(1) == dim(2))
img = uint8(zeros(dim(1),dim(2),dim(3)));
img = in;
else
pad = max(dim(1:2));
img = repmat(uint8(bg),[pad pad dim(3)]);

% faster code courtesy of Frans Cornelissen and Daw-An Wu
startX = 1 + floor(abs(pad - dim(1)) / 2);
endX = startX + dim(1) - 1 ;
% xsize = endX - startX;

startY = 1 + floor(abs(pad - dim(2)) / 2);
endY = startY + dim(2) - 1;
% ysize = endY - startY;

img(startX:endX,startY:endY,:) = in(:,:,:);

end

catch
% This "catch" section executes in case of an error in the "try" section
% above. Importantly, it closes the onscreen window if its open.
ShowCursor;
Screen('CloseAll');
rethrow(lasterror);
clear all
end % try..catch..

%%%%% END %%%%%





--- In psychtoolbox@yahoogroups.com, Johannes Haushofer <haushof@f...> wrote:
> Hi all,
>
> I've run into a segmentation violation problem while making textures
> under OSX using Screen('MakeTexture'...). This happens when I try to
> make textures from image arrays - some images work, others don't. Two
> examples are attached: 'goat.jpg' always works, 'oval.jpg' virtually
> always produces a segmentation violation and sometimes crashes Matlab
> entirely. An example script illustrating the problem follows below, as
> does the error message that Matlab outputs when things go wrong.
>
> I would be very grateful for any hints!
>
> Best regards,
> Johannes
>
>
> Here is the script that illustrates the problem. Run it from the same
> folder as the two images attached to this message.
> % ***************************************************************
> clear all;
> AssertOpenGL;
> screens=Screen('Screens');
> screenNumber=max(screens);
> frameRate=Screen('FrameRate',screenNumber);
> if(frameRate==0)
> frameRate=60;
> end
> % Making the goat texture - this works
> try
> goat = imread('goat.jpg');
> goattex = Screen('MakeTexture', screenNumber, goat);
> disp('The goat texture worked.');
> catch
> error('The goat texture didn''t work.');
> end
> % Making the oval texture - this often fails
> try
> oval = imread('oval.jpg');
> ovaltex = Screen('MakeTexture', screenNumber, oval);
> disp('The oval texture worked.');
> catch
> error('The oval texture didn''t work.');
> end
> % ***************************************************************
>
>
> Here is Matlab's error message:
> ------------------------------------------------------------------------
> Segmentation violation detected at Mon Nov 15 14:41:09 2004
> ------------------------------------------------------------------------
>
> Configuration:
> MATLAB Version: 6.5.0.185000 (R13)
> Operating System: Darwin 7.4.0 Darwin Kernel Version 7.4.0: Wed May
> 12 16:58:24 PDT 2004; root:xnu/xnu-517.7.7.obj~7/RELEASE_PPC Power
> Macintosh
> Window System: The XFree86 Project, Inc (40300000), display :0.0
> Current Visual: 0x22 (class 4, depth 24)
> Virtual Machine: Java 1.3.1 with "Apple Computer, Inc." Java
> HotSpot(TM) Client VM
> (mixed mode)
>
>
>
> Segmentation violation occurred within signal handler.
> Unable to complete stack trace (stack was probably corrupted)
>
> This error was detected while a MEX-file was running. If the MEX-file
> is not an official MathWorks function, please examine its source code
> for errors. Please consult the External Interfaces Guide for
> information
> on debugging MEX-files.
>
> If it is an official MathWorks function, please
> follow these steps in reporting this problem to The MathWorks so
> that we have the best chance of correcting it:
>
> 1. Send this crash report to segv@m... for automated
> analysis.
> For your convenience, this information has been recorded in:
> /Users/johannes/matlab_crash_dump.2278
>
> 2. Also, if the problem is reproducible, send the crash report to
> support@m... along with:
> - A specific list of steps that will reproduce the problem
> - Any M, MEX, MDL or other files required to reproduce the
> problem
> - Any error messages displayed prior to this crash report
> A technical support engineer will contact you with further
> information.
>
> Thank you for your assistance. Please save your workspace and restart
> MATLAB before continuing your work.
Ahha!!! This explains the square texture requirement I ran into... When square x=y, so
things work out okay. At this point I assume the next release of PTB will correct this bug -
I can then verify that my non-square textures run with use of impad (which will then be
retired).

-m


--- In psychtoolbox@yahoogroups.com, Jochen Laubrock <laubrock@r...> wrote:
>
> Hi,
>
> although I did not have the time to analyze this in detail, I think
> there might be some typos in ScreenMakeTexture.c. First, I could also
> reproduce the crash. When I modified the source, by replacing
> for(iy=0;iy<xSize;iy++){
> with
> for(iy=0;iy<ySize;iy++){
> in all "for (iy=0;..){}" loops,
> and by replacing line 167
> if(isImageMatrixDoubles && numMatrixPlanes==4){
> with
> if(isImageMatrixBytes && numMatrixPlanes==4){
> , cleaned and recompiled the Screen.app target and copied the resulting
> Screen.mexmac to the Psychtoolbox/PsychBasic folder, your test data
> gave the following result;
>
> The goat texture worked.
> The oval texture worked.
>
> hth, jochen
>
> Am 16.11.2004 um 06:57 schrieb Johannes Haushofer:
>
> > <goat.jpg><oval.jpg>Hi all,
> >
> > I've run into a segmentation violation problem while making textures
> > under OSX using Screen('MakeTexture'...). This happens when I try to
> > make textures from image arrays - some images work, others don't. Two
> > examples are attached: 'goat.jpg' always works, 'oval.jpg' virtually
> > always produces a segmentation violation and sometimes crashes Matlab
> > entirely. An example script illustrating the problem follows below, as
> > does the error message that Matlab outputs when things go wrong.
> >
> > I would be very grateful for any hints!
> >
> > Best regards,
> > Johannes
> >
> >
> > Here is the script that illustrates the problem. Run it from the same
> > folder as the two images attached to this message.
> > % ***************************************************************
> > clear all;
> > AssertOpenGL;
> > screens=Screen('Screens');
> > screenNumber=max(screens);
> > frameRate=Screen('FrameRate',screenNumber);
> > if(frameRate==0)
> > frameRate=60;
> > end
> > % Making the goat texture - this works
> > try
> > goat = imread('goat.jpg');
> > goattex = Screen('MakeTexture', screenNumber, goat);
> > disp('The goat texture worked.');
> > catch
> > error('The goat texture didn''t work.');
> > end
> > % Making the oval texture - this often fails
> > try
> > oval = imread('oval.jpg');
> > ovaltex = Screen('MakeTexture', screenNumber, oval);
> > disp('The oval texture worked.');
> > catch
> > error('The oval texture didn''t work.');
> > end
> > % ***************************************************************
> >
> >
> > Here is Matlab's error message:
> > -----------------------------------------------------------------------
> > -
> > Segmentation violation detected at Mon Nov 15 14:41:09 2004
> > -----------------------------------------------------------------------
> > -
> >
> > Configuration:
> > MATLAB Version: 6.5.0.185000 (R13)
> > Operating System: Darwin 7.4.0 Darwin Kernel Version 7.4.0: Wed May
> > 12 16:58:24 PDT 2004; root:xnu/xnu-517.7.7.obj~7/RELEASE_PPC Power
> > Macintosh
> > Window System: The XFree86 Project, Inc (40300000), display :0.0
> > Current Visual: 0x22 (class 4, depth 24)
> > Virtual Machine: Java 1.3.1 with "Apple Computer, Inc." Java
> > HotSpot(TM) Client VM
> > (mixed mode)
> >
> >
> >
> > Segmentation violation occurred within signal handler.
> > Unable to complete stack trace (stack was probably corrupted)
> >
> > This error was detected while a MEX-file was running. If the MEX-file
> > is not an official MathWorks function, please examine its source code
> > for errors. Please consult the External Interfaces Guide for
> > information
> > on debugging MEX-files.
> >
> > If it is an official MathWorks function, please
> > follow these steps in reporting this problem to The MathWorks so
> > that we have the best chance of correcting it:
> >
> > 1. Send this crash report to segv@m... for automated
> > analysis.
> > For your convenience, this information has been recorded in:
> > /Users/johannes/matlab_crash_dump.2278
> >
> > 2. Also, if the problem is reproducible, send the crash report to
> > support@m... along with:
> > - A specific list of steps that will reproduce the problem
> > - Any M, MEX, MDL or other files required to reproduce the
> > problem
> > - Any error messages displayed prior to this crash report
> > A technical support engineer will contact you with further
> > information.
> >
> > Thank you for your assistance. Please save your workspace and restart
> > MATLAB before continuing your work.