Potrzebuję wyświetlić BMP 640x480x256. Ładuję się i wyświetla ( chodzi o piksele ) prawidłowo, wyjątkiem jest paleta którą jak bym nie ustawił, zawsze wygląda nieprawidłowo.
Próbowałem ją ustawiać za pomocą funkcji z SDL'owej biblioteki SDL_SetPalette ( jak i również SDL_SetColor ) pobierając składowe kolorów z powierzchni do której został wczytany obrazek, oraz napisałem też własną funkcję która bezpośrednio z pliku BMP czyta paletę. W każdym przypadku objawem są dokładnie takie same nieprawidłowe kolory. Jak prawidłowo ustawić paletę 8-bitową w SDLu?
Oczywiście obrazek ładowany w programach graficznych wyświetla się prawidłowo, więc to nie z nim jest problem.
#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 ); // to tez probowane, ale efekty bez zmian
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;
}