writing 8-bit png with a transparent color from C++ in Windows

1.4k views Asked by At

I've got some code to generate false-color synthetic images from side scan sonar data that I want to load into Google Earth as overlays. I need to be space efficient, so 8-bit PNG files seemed to be the way to go. I can use 1 color table entry for background and 255 for my false color table.

I saw previous answers that recommended the CImage class in the ATL library. So I look up the MSDN documentation here (http://msdn.microsoft.com/en-us/library/5eb252d0(v=vs.100).aspx)

It looks straight forward enough so I coded something like this:

CImage Cimg;
Cimg.Create(width,height,8,0);

RGBQUAD ColorTable[256];
for(i=0;i<256;i++)
    // Fill table.
Cimg.SetTransparentColor(long(255));
Cimg.SetColorTable(0,256,Ctable);

// fill the pixel values.

Cimg.Save("TestFile.png",Gdiplus::ImageFormatPNG);

Everything works more or less as expected, but color 255 is just another color. It's not transparent at all.

I know this is not an issue with GoogleEarth, because I can load the file into Paint Shop Pro and manually set the background clear, and then everything works great.

I've tried setting the .rgbReserved byte in the RGBQUAD table to all zeros, 255s and a ramp, thinking it may be interpreted as the Alpha value, but that had no effect.

I also tried stepping through the implementation of CImage::Save() where I determined it's using Gdiplus::Bitmap::Save() to do the write with the "Built In PNG Encoder". I thought maybe there were some encoder parameters which weren't getting set right, but this MSDN page suggests there are zero parameters for the PNG encoder.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms533846(v=vs.85).aspx

So, um, I guess my question is "HELP!" :-)

More specifically, does anyone know if any of the MSDN native functions will let me generate 8-bit PNGs with a palate I can define and at least one transparent color? A simple fix would be great, but I'm annoyed enough with CImage now that I'm willing to abandon it and try something else.

Alternatively, does anyone know if there is a command line utility that could be used to fix the broken background color?

Thanks!

0

There are 0 answers