SDL_Image related issues

Started by
2 comments, last by pinknation 15 years, 1 month ago
I am loading a bitmap from memory.. it seems to have 1 BytePerPixel, which I assume is referencing to some palette. If I were to save the file, and view it in Photoshop/Misc Image Viewing tools, it would appear fine(with color); however because it is 1 BytePerPixel I end up with a gray-scale image(due to each byte being an index to some palette) Is there some way to obtain the correct colors in cases like this? I found SDL_SetPalette.. there are no SDL_GetPalette so I cannot manually do it.. I need a work around, thanks! Note: Some bitmaps appear correctly.. but that is because they have 3-4 BytesPerPixel. FIXED: Uint32 rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff; #else rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000; #endif SDL_Surface *converted = SDL_CreateRGBSurface(0, Surface->w, Surface->h, 32, rmask, gmask, bmask, amask); SDL_LockSurface(Surface); for(int i = 0; i < (Surface->w * Surface->h); ++i) { SDL_Color color = Surface->format->palette->colors[*((Uint8 *)Surface->pixels + i)]; *((Uint32*)converted->pixels + i) = SDL_MapRGB(converted->format, color.r, color.g, color.b); } SDL_UnlockSurface(Surface); SDL_FreeSurface(Surface); Surface = converted; If anyone is interested. [Edited by - pinknation on March 8, 2009 1:41:15 AM]
Advertisement
SDL can do pixel format conversions so you don't have to do them manually. Have you tried SDL_BlitSurface and just blitted the 8bit surface onto the 32bit one? Or even SDL_ConvertSurface will do if you need more flexibility.

0xa0000000
Nevermind. I read your post again, and I don't understand your problem. You've obviously found the palette member of the SDL_PixelFormat type.
Yes, it has been resolved... Though, can anyone tell me how I might turn this into less/faster code?

Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface *converted = SDL_CreateRGBSurface(0, Surface->w, Surface->h, 32, rmask, gmask, bmask, amask);

SDL_LockSurface(Surface);
for(int i = 0; i < (Surface->w * Surface->h); ++i)
{
SDL_Color color = Surface->format->palette->colors[*((Uint8 *)Surface->pixels + i)];
*((Uint32*)converted->pixels + i) = SDL_MapRGB(converted->format, color.r, color.g, color.b);
}
SDL_UnlockSurface(Surface);

SDL_FreeSurface(Surface);
Surface = converted;


I also have a 'MakeTransparent' method which turns an pixel(255,0,255) to a transparent white pixel(255, 255, 255, 0) = 32bits;

Any way I might improve it? Currently it just loops through each pixel, switching Magneta colors with Transparent White colors.

This topic is closed to new replies.

Advertisement