I'm trying to convert images from a nonstandard format to PNG using the pngimage library. The original format uses a 256-color palette, just like PNG does, so it should be simple. And I can get the image to convert and look exactly like the original. There's only one problem.

The way my program is set up, it checks the image for palette entry #0, and defines that as the mask color. When I'm translating the image to PNG, the original palette has to be preserved in its original order, but I can't find any way to make that work.

My original code tried loading the original image's palette into an array, setting all the pixels of the image, and then using the Windows API function:
windows.SetPaletteEntries(png.Palette, 0, 256, thePalette[0]);
This produced an image that looked correct, but the palette #0 entry was wrong, which screws up masking. It's as if the palette was never set.

Next, I tried calling SetPaletteEntries first, then setting all the image pixels at the end. This produced proper masking, but the colors of the final image were bizarrely munged. Each pixel of the same color in the original came out the same color in the converted version, but they weren't the same two colors, so apparently the palette references were correct but the entries themselves had been corrupted.

Then I tried setting the palette manually. I wrote this:
[pascal] for I := 0 to 255 do
with TChunkPLTE(png.Chunks.ItemFromClass(TChunkPLTE)).I tem[i] do
begin
rgbRed := thePalette[i].peRed;
rgbBlue := thePalette[i].peBlue;
rgbGreen := thePalette[i].peGreen;
end;
[/pascal]
and tried placing it first before and then after the routine that sets the pixels. Both gave me the same results as my original code: Correct image, incorrect palette #0.

I'm about at my wits' end here. Does anyone know how to explicitly set up a the palette entries in a TPngObject and have it come out looking properly AND with the palette in the right order?