Will Perone

Lost in trying to find documentation on how to load Microsoft .PAL palette files into an SDL palette for your 1337 palettized sprite game? I know I was; there is literally no information at all on the net on how to do this! So I figured it out...
// given: const char *filename; and SDL_Palette palette;

std::ifstream file(filename, std::ios::in|std::ios::binary);
if (file.fail()) 
{
	// do error for file and bail out
}
file.seekg(4+4+4+4+4 + 2, std::ios::beg); // skip unrelevant stuff

// read in number of colors in the palette
unsigned short numcolors;
file.read((char *)&numcolors, 2); // need to read into unsigned short and assign because ncolors is an unsigned int!
palette.ncolors= numcolors;

if (!palette.ncolors) palette.ncolors= 256; // sanity check
palette.colors= new SDL_Color[palette.ncolors];	

// read in the colors
file.read((char *)palette.colors, 4*palette.ncolors);		

file.close();	
It's that easy, the unrelevant stuff I skip is:
'RIFF' (4 bytes)
file length - 8 (4 bytes)
' PAL' (4 bytes)
'data' (4 bytes)
data size (filelength - 20) (4 bytes)
version number (always 00 03) (2 bytes)
Now setting the palette for a palettized SDL_Surface is real easy:
SDL_SetColors(surface, palette.colors, 0, palette.ncolors);
So now you can load your .BMP's and have your .PAL's too... This functionality should have been built into SDL imho. You might try to be sneaky and realize that SDL_Surface stores a pointer to it's SDL_Palette, so why not just allocate a SDL_Palette pointer and set the surface palette to point to it? Because when you call SDL_FreeSurface it's gonna try to delete the palette and then you are gonna try to delete the palette again and BOOM so watch out for this trap.
I wrote an addon for SDL that loads .PAL files into SDL_Palettes just like the SDL_LoadBMP does for surfaces. It can be found here.
10 Comments
Videogamer555 2011/09/14 Contact Me0 0
Photoshop can save palettes in this format. I dumped a simple palette and here's what I got when looking at a hex editor. Can you help me understand each part of this file?

52 49 46 46 28 00 00 00 50 41 4C 20 64 61 74 61 1C 00 00 00 00 03 06 00 00 00 00 00 FF 00 00 00 00 FF 00 00 00 00 FF 00 FF FF 00 00 FF FF FF 00

Will 2011/09/140 0
I described the first 22 bytes of the file above which will get you to the '00 03' in your hex string. Then after that each 4 bytes is a RGB color code.
Adib 2013/03/31 Contact Me0 0
Will, you mean each 4 bytes is a RGBA color code, the fourth byte being alpha, right?
Will 2013/04/09 Contact Me1 13
Normally yes the 4th byte would be the alpha but it doesn't make any sense in context of reading in a palette file so the 4th byte is just empty. They just have 4 byte alignment to make reading it in easier from 32 bit boundaries.
Adib 2013/04/09 Contact Me2 22
Sweet! Thanks.
Joao 2014/02/23 Contact Me1 0
This was super helpful info, thanks!
I did a quick HEX read and after the version nr (00 03) I get two more bytes before starting the RGBA info and I figured out those are the number of colors in the table. Say, if you have 5 colors, it will be 05 00.
THEN the RGBA info begins.
Using Photoshop CS6 to save the PAL file.. not sure if that matters.
Will 2014/02/230 51
Interesting, there must be a discrepancy in the format that Photoshop writes. The PAL format I was working off of was from 10 years ago so I don't know if it has changed since then.
Joao 2014/02/24 Contact Me2 26031
Created a quick project in C# for this. Sharing it here in case other people land on this page through a Google search like I did. Couldn't find documentation on this format anywhere else!

https://code.google.com/p/pal-reader/

Special attention to the PALReader class
https://code.google.com/p/pal-reader/source/browse/trunk/pal-reader/PAL%20Reader/PalReader.cs

Hope this helps!
Callum 2014/08/142 2
.PAL format is very easy to understand thanks to this document! Skip the first 22 bytes then the next 2 bytes are the number of colors, then comes the actual colors. Each color is 4 bytes long and are encoded as RGBA.
Aybe 2014/08/28 Contact Me0 0
Bull$h!# ... :D the 4th byte is a flag, http://www.aelius.com/njh/wavemetatools/doc/riffmci.pdf.

<- for private contact