2008.11.21 21:45 "Re: [Tiff] Re: RGB to palette images", by Kevin Myers
You cannot do it in one pass because the entries in a palettized image are indices into the palette, which you have not built unless you have seen all the entries. In the special case that you know all the possible values ahead of time, you could do it in one pass. You could also do it in one pass if you knew in advance the mapping you wanted to use between the input values and the output values, eg for a USGS topo map, there are only a fixed number of possible colors and you can be sure that only a specific set of RGB values will appear in the data stream. In that case, build you palette table at the top of the code, and convert RGB triplets to palette indices on the fly.
But, while scanning an image, if I come across a new unique color (a color that hasn't already been added to the palette), then I do know the index into the palette, because I know that this is the nth new color that I have added to the palette, right? So, I still think that this could be done in a single pass, building the palette on the fly, if desired, but maybe I am overlooking something.