[SDL] 8bpp BMP seg faults

Ryan C. Gordon icculus at linuxgames.com
Sun Jun 17 01:08:18 PDT 2001


> 	    /* Generate The Texture */
> 	    glTexImage2D( GL_TEXTURE_2D, 0, 3, TextureImage[0]->w,
> 			  TextureImage[0]->h, 0, GL_RGB,
> 			  GL_UNSIGNED_BYTE, TextureImage[0]->pixels );
> 
> After that last line is where it bombs. Any suggestions? Thanks!

You are giving OpenGL texture data where each byte represents a pixel
(8-bit), but telling it to read three bytes for every pixel
(GL_RGB). glTexImage2D() is reading past the end of the surface's pixel
data, and probably touching memory your application doesn't own.

If you're loading an 8-bit bitmap, you should use GL_COLOR_INDEX (not
GL_RGB) and set up a pixel map with glPixelMapfv().

That's a pain, though.

Alternately, after loading the 8-bit bitmap, convert the SDL surface to 24
bit:

/* this is completely untested. */
SDL_Surface *bmp = SDL_LoadBMP("data/crate.bmp");

if ( (bmp != NULL) && (bmp->format.BitsPerPixel != 24) ) 
{
    SDL_Surface *tmp;
    SDL_PixelFormat fmt;

    memcpy(fmt, bmp->fmt, sizeof (SDL_PixelFormat));
    fmt->palette = NULL;
    fmt->BitsPerPixel = 24;
    fmt->BytesPerPixel = 3;
    /* set anything else... */

    tmp = SDL_ConvertSurface(bmp, &fmt, SDL_SWSURFACE);
    assert(tmp);
    free(bmp->pixels);
    SDL_FreeSurface(bmp);
    bmp = tmp;
}

/* your GL code goes here, now with a surface guaranteed to be 24-bit, so
you can use GL_RGB. */

Hhmmm...that's also a pain.  :)

Talk amongst yourselves.

--ryan.






More information about the SDL mailing list