

Table lookup approach using the 216-color palette
8bit color palette converter full#
Let’s check our old standby, the Big Buck Bunny logo image:ĭistance approach using the full 256-color QuickTime default paletteĭistance approach using the 216-color palette However, there still seems to be a problem. Thus, using the distance approach with the full default table provides for a little more accuracy. One problem is that the regulation QuickTime palette actually has 40 more entries above and beyond the typical 216-entry color cube (rounding out the grand total of 256 colors). Now, ideally, swapping out one algorithm for another in my SMC encoder should yield identical results. So I was pretty pleased with myself for coming up with that. The palette index is 5, which maps to color (0xFF, 0xFF, 0x00). Finally, the index into the palette table is given by:įor example, the pixel (0xFE, 0xFE, 0x01) would yield ri, gi, and bi values of 0, 0, and 5. Each of the R, G, and B components of an input pixel are used to index into this table and derive 3 indices ri, gi, and bi. I created a table which maps indices 0.215 to values from 5.0. Thus, if an input RGB pixel has a red color closest to 0xFF, it must map to one of those first 36 entries. The first 36 palette entries in the table all have a red component of 0xFF. The first (0th) entry in the table is (FF, FF, FF), followed by (FF, FF, CC), (FF, FF, 99), and on down to (FF, FF, 00) when the green component gets knocked down and step and the next color is (FF, CC, FF). This happens to be identical to the web-safe color palette.

If you mix and match these for red, green, and blue values, you come up with 6 * 6 * 6 = 216 different colors. There’s a pattern there– all of the RGB entries are comprised of combinations of 6 values - 0x00, 0x33, 0圆6, 0x99, 0xCC, and 0xFF.

I think this is the approach that FFmpeg used to use, but I went and derived it for myself after studying the default QuickTime palette table. As you might imagine, this can be a bit time-consuming. That means for each pixel in an image, check the pixel against 256 palette entries (early termination is possible if an acceptable threshold is met).
8bit color palette converter how to#
How to perform the matching? Find the palette entry that is closest to a given input pixel, where “closest” is the minimum distance as computed by the usual distance formula (square root of the sum of the squares of the diffs of all the components). The path of least resistance was to match the pixels in the input image to the default 256-color palette that QuickTime assumes (and is hardcoded into FFmpeg/Libav). When I started writing my SMC video encoder, I needed to convert RGB (from PNG files) to PAL8 colorspace. Still, there’s no telling if the old system would have computed palettes correctly for QuickTime files. I suspect it got removed during some swscale refactoring. Somewhere along the line, FFmpeg and Libav lost the ability to do this. Indeed, FFmpeg used to be able to, at least at the time I wrote this post about ZMBV and was unhappy with FFmpeg’s default results. Ideally, FFmpeg / Libav should be able to handle this automatically. How do you take a 24-bit RGB image and convert it to an 8-bit paletted image for the purpose of compression using a codec that requires 8-bit input images? Seems simple enough and that’s what I’m tackling in this post.
