SDL Parachute Deployed when accessing surface->pixels

BobbyWatson
Unregistered
 
Post: #1
It's been a while since I've actually touched any SDL/OpenGL stuff, so I'm sorry if my problem is a very simple one (and I'm guessing that it is).

Here it is : I'm currently trying to load a texture (from a PNG file, though I've tried with a GIF and a BMP, with the same results), but every time I try to access my variable SDL_Surface* textureSurface->pixels, I get the following message in the Run Log :

Fatal signal: Bus Error (SDL Parachute Deployed)

Please note that I'm also using PHYSFS for loading files from inside a zip archive (which, according to the log, seems to be working fine).

1. I open the file with a PHYSFS function in "read" mode.
2. I read the whole file in memory.
3. I create an SDL_RWops using the copy of the file currently in RAM.
4. I check to make sure that I have a valid SDL_RWops, and then I use IMG_Load_RW to create and SDL_Surface from the stored memory. I check with IMG_GetError, and there is none.
5. I lock the texture surface.
6. Using SDL_Surface->format->BitsPerPixel, I determine the pixel format I have to pass to gluBuild2DMipmaps.
7. I can get the following information :

width = 512, height = 128, bitsPerPixel = 32

from the SDL_Surface without any problem.
8. The problem happens when I try to read something from SDL_Surface->pixels, I always get the Fatal signal Bus Error.

If it helps, here's the code :

Code:
if ((textureSurface = IMG_Load_RW(rw, 1)) != NULL)
{
        SDL_LockSurface(textureSurface);
                    
    //Il faut maintenant determiner le format de pixel de l'image
    switch(textureSurface->format->BitsPerPixel)
    {
        case 32 :    pixelFormat = GL_RGBA;
                imageType = GL_UNSIGNED_BYTE;
                internalPixelFormat = GL_RGBA8;
                break;
           case 24 :    pixelFormat = GL_RGB;
                imageType = GL_UNSIGNED_BYTE;
                internalPixelFormat = GL_RGB8;
                break;
        case 16 :    pixelFormat = GL_RGBA;
                imageType = GL_UNSIGNED_SHORT_5_5_5_1;
                internalPixelFormat = GL_RGB5_A1;
                break;
        default :    pixelFormat = GL_LUMINANCE;
                imageType = GL_UNSIGNED_BYTE;
                internalPixelFormat = GL_LUMINANCE8;
                break;
    }
                    
printf("width = %i, height = %i, bitsPerPixel = %i\n", textureSurface->w, textureSurface->h, textureSurface->format->BitsPerPixel);
                    
glGenTextures(1, &textureID);
                    
glBindTexture(GL_TEXTURE_2D, textureID);
                    
if (gluBuild2DMipmaps(GL_TEXTURE_2D, internalPixelFormat, textureSurface->w, textureSurface->h, pixelFormat, imageType, textureSurface->pixels))
    isValid = false;
                    
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                    
SDL_UnlockSurface(textureSurface);
                    
SDL_FreeSurface(textureSurface);

}

Does anyone have an idea of where the problem might be? I'm out of ideas...
Quote this message in a reply
Apprentice
Posts: 8
Joined: 2004.07
Post: #2
I'm not sure, but it wouldn't be too strange if you cannot acces the data you just deallocated, now would it?
Try to comment out SDL_FreeSurface(textureSurface);
Quote this message in a reply
BobbyWatson
Unregistered
 
Post: #3
The code doesn't get to SDL_FreeSurface, it crashes at gluBuild2DMipmaps, 3 lines before freeing the surface.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #4
Try peeking at textureSurface->pixels using a known image to make sure SDL has loaded the bits you expect.

i.e.
Code:
unsigned int *pix = (unsigned int *)textureSurface->pixels;
printf("first few pixels of image: %08x %08x %08x %08x", pix[0], pix[1], pix[2], pix[3]);

Maybe it has discarded the alpha channel or something.
Quote this message in a reply
BobbyWatson
Unregistered
 
Post: #5
It gives me the following :

width = 512, height = 128, bitsPerPixel = 32
Fatal signal: Bus Error (SDL Parachute Deployed)
first few pixels of image: 00000000 00000000 00000000 00000000

Instead of 0, 1, 2 and 3 as pixels, I tried some random values (since most of the picture is transparent) between 0 and 65535 (since the picture is 512 by 128 pixels) and I got this :

width = 512, height = 128, bitsPerPixel = 32
Fatal signal: Bus Error (SDL Parachute Deployed)
first few pixels of image: 00000000 fdfbfbff 00000000 1514144d
Executable “Hector's Quest” has exited with status 246.
Quote this message in a reply
Sage
Posts: 1,232
Joined: 2002.10
Post: #6
Try it with a known image. Make a bitmap where the top left (bottom left, whichever) pixels are red, green, transparent, etc so you can compare against known values.
Quote this message in a reply
BobbyWatson
Unregistered
 
Post: #7
I created a BMP image with a red pixel at 0, a green at 1 and a blue at 2, with the rest all black, and here's what I got :

width = 32, height = 32, bitsPerPixel = 24
Fatal signal: Bus Error (SDL Parachute Deployed)
first few pixels of image: 0000ff00 ff00ff00 00000000 00000000
Executable “Hector's Quest” has exited with status 246.

I'm not even sure if this is okay. Does SDL keep the same pixel order (BMP's are BGR and not RGB, if memory serves right), but it looks okay to me (considering that the pixel format is 3-byte and not 4-byte, no alpha channel). First pixel : 0000ff, second 00ff00, third ff0000, fourth 000000...

I also tried using SDL_LoadBMP instead of SDL_image, and the results are the same.
Quote this message in a reply
BobbyWatson
Unregistered
 
Post: #8
Okay, stop thinking about it, I just found the source of the problem. It didn't come from SDL, but rather from the fact that I was calling glGenTextures before having an OpenGL context.

I'm really sorry about wasting everyone's time...
Quote this message in a reply
Moderator
Posts: 434
Joined: 2002.09
Post: #9
It wasn't a waste of time. The discussion helped you, and it may help others in the future.

Measure twice, cut once, curse three or four times.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  texture no square surface kendric 7 4,233 Mar 20, 2009 05:26 PM
Last Post: kendric
  Best way to readback float pixels? kelvin 8 5,144 Feb 27, 2008 10:23 PM
Last Post: kelvin
  surface rendering from data points mc1961 6 4,689 Nov 15, 2007 03:37 PM
Last Post: TomorrowPlusX
  Getting to an NSWindow's pixels ThemsAllTook 4 5,271 Jun 5, 2007 08:05 PM
Last Post: sammyjojo
  Trouble turning an SDL surface into an OpenGL texture Joseph Duchesne 4 5,509 May 22, 2007 05:14 PM
Last Post: Joseph Duchesne