This topic should have been PNG Image Transparency.
My DT2PNG program was already capable of producing an 8bit PNG image from a 32bit image as input. It uses Neuquant32 which is an extension of Anthony Dekker's original Neuquant Algorithm. As the name implies Neuquant32 can handle 32bit data so each color in the Quantized Color Palette is composed of 4 bytes, ARGB.
My challenge, and it is truly a challenge, is to write new code for DT2PNG to produce a 'tRNS' Data Chunk so the output 8bit PNG will show Simple Transparency. In fact, it will look very similar to the original 32bit image.
It's a long, difficult process to produce the tRNS Data. We start by looping through all 256 colors in the Neuquant Std Palette which is simply called 'map'. It is a unsigned char [256] [4] so it supports ARGB data. We have to find the transparent colors for the byte array 'trans' as well as 'num_trans' so we know the count of trans bytes which we will need later. We already have a Std Palette so we need a 'Remap' function to create a 'tRNS Palette' rearranged so that all the colors with transparent values less than 255 come first. All other values have alpha = 255 which means they are opaque. Sounds easy doesn't it?
Here's the code so far. For(x=0; x<n_colors; x++) {
If(map[ x ][ 0] == 255, remap[ x] = high_index++;
Else, remap[ x ] = low_index++; }
In this case low_index holds num_trans which is the total count of transparent colors. The tRNS Chunk only contains transparent values not opaque values, hence why we separate high & low index values.
In the next step we can gather values for the 'trans' byte array. For(x=0; x<n_colors; x++) { trans[ remap[ x ] = map[ x] [ 0 ]; }
After that I use remap[ x] in a similar way to get all the colors for the 'tRNS Colormap'. The next step is to find all the tRNS index values now that the colormap has changed. But my remap function doesn't work with Neuquant's inxsearch function so I have to do it differently. I have 2 Colormaps : Std Colormap & tRNS Colormap. I also have the Original Index values from inxsearch. So I need two new functions FindColor using Std Colormap & Std Index value. It returns the Original RGB color from the Original Palette. FindIndex uses the tRNS Colormap & Original Color from FindColor to return the new tRNS Index value. We do that for every index value. Then store the indices in the Datatype. After storing the tRNS Colormap & tRNS Index values we use PNG Datatype to save the 8bit PNG.
But then the fun part. We have to insert the tRNS Chunk.
So much fun!