BMP palette is 'messy'


#1

I need to display 640x480x256 BMP. It loads through SDL_LoadBMP and renders to the screen fine, but I am not able to set the palette properly, the colors are messy. I even tried to write my own function to get the palette directly from the bmp file, but the colors are exactly the same mess. How to properly set the palette for 8bit BMP file?

#include "SDL/SDL.h"
#include <stdio.h>

struct rgb 
{
	unsigned char b, g, r, z;	
};	

struct rgb old_palette[256];

SDL_Surface *image = NULL;
SDL_Surface *screen = NULL;

int set_palette_sdl ()
{
	SDL_PixelFormat *fmt;
	SDL_Color colors[256];
	int index;	

	fmt = image->format;
	if ( fmt->BitsPerPixel != 8 ) 
		printf ( "Not an 8-bit surface.\n" );
	else
		printf ( "8-bit surface.\n" );
	SDL_LockSurface ( image );
	for ( index = 0; index < 256; index++ )
	{
		colors [ index ] = fmt->palette->colors[index];
//		fprintf ( p_file_out, "Pixel Color-> Red: %d, Green: %d, Blue: %d. Index: %d\n", colors[index].r, colors[index].g, colors[index].b, index );
	}
	
	SDL_UnlockSurface ( image );

	SDL_SetPalette ( screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256 );
	return 0;
	
}

int set_palette_directly_from_bmp ()
{
	struct rgb palette;
	SDL_Color colors[256];
	int i;
	FILE *p_file;

	p_file = fopen ( "hello.bmp", "rb" );
	
	if (!p_file)
	{
		printf ( "Problem opening file\n" );
		return 1;
	}
	
	fseek ( p_file, 54, SEEK_SET );
	
	for ( i=0 ; i < 256; i++ )
	{
		fread ( &palette, sizeof ( struct rgb ), 1, p_file );
		colors[i].r = palette.r;
		colors[i].g = palette.g;
		colors[i].b = palette.b;	
	}
	
//	SDL_SetPalette ( screen, SDL_LOGPAL|SDL_PHYSPAL, colors, 0, 256 ); // tried this also, but the result is the same
	SDL_SetColors ( screen, colors, 0, 256 );
	return 0;
}

int main( int argc, char* args[] )
{
	int i;
		
	SDL_Init( SDL_INIT_EVERYTHING );
	screen = SDL_SetVideoMode( 640, 480, 8, SDL_SWSURFACE|SDL_HWPALETTE );
	image = SDL_LoadBMP( "hello.bmp" );
	
	SDL_BlitSurface( image, NULL, screen, NULL );
	SDL_Flip( screen );
	
//	set_palette_directly_from_bmp();	
	set_palette_sdl();
	
	SDL_Delay ( 1500 );
	SDL_FreeSurface( image );
	SDL_Quit();
	return 0;
}