dW

Sep 15th 2011

Quantizing PNG Color Channels

I’ve covered color quantization before when I wrote about PNG optimization and quantization. That article only covered quantization of 8-bit images, but color reduction of higher bit depth images which use direct color is quite possible. PNG images can take up quite a bit of space because while the format is definitely compressed the image format isn’t lossy. Adding an alpha channel only creates even larger files. PNG quantization usually is used to turn 32-bit PNG images (24-bit color, 8-bit alpha channel) into 16-bit PNG images (8-bit color, 8-bit alpha channel). Sometimes images aren’t well-suited for that much color reduction, so many of these large 32-bit PNG images wind up being used when quantizing the color channels themselves can yield smaller sizes without sacrificing too much of the image’s appearance in the process. I’m focusing on PNG images here, and while these processes definitely work for JPEG images the results are more widely mixed because of what the lossy compression does to the image; it doesn’t mean attempting to do this on JPEG images is a waste of time, after all.

Direct Color

Images that are higher than 8-bit typically use a direct color system which means the colors are directly encoded instead of mapped to a palette. These images are made up of separate channels containing tints for each primary color: red, green, and blue. When combined the colors are mixed to produce the image itself. In a 24-bit image each channel contains 256 colors, signifying they’re each 8-bit. 8×3=24. To produce a 12-bit image each channel would have to contain 16 colors (4-bit). 4×3=12. 16-bit is a bit different because the red and blue channels each contain 32 colors (5-bit) while the green channel contains 64 colors (6-bit). 5+6+5=16. The green channel contains more colors because humans are more visually sensitive to green light. Given a good raster graphics application like Adobe Photoshop, it’s possible to manually edit these channels separately.

Indexing Channels

Crop of Blimey!: first half 24-bit, second half 16-bit dithered

The image above is a cropping of my digital painting, Blimey!. The first half of the image above is 24-bit and the second half is 16-bit; The 16-bit half was created by copying each channel to a separate file, converting each to an indexed image with the colors necessary for each channel using error diffusion while preserving colors, and then lastly copying the image data to the channels. On most images this process only creates larger files because it makes PNG’s predictive compression algorithm less effective; adding additional noise only increases the file size. The original image as a 1900x1200 24-bit PNG is around 3.85 MB. As a 16-bit PNG at the same resolution using this process just outlined is around 3.94 MB — around a 2% increase in size.

Crop of Blimey!: first half 24-bit, second half 16-bit no dithering

The first half of the image above is 24-bit and the second half is 16-bit as before, but when quantizing the color channels diffusion was turned off. This simple change produces an image which is around 3.83 MB — almost a half a percentage point smaller in file size. This looks to be splitting hairs, but I’m mentioning it because not all images are alike; one method which produces good results on one image might not for another, and this method given another file might produce superior results.

There are other indexing methods available such as pattern dithering, and there are others not available in Photoshop which are explained in great detail in Wikipedia’s article on Dithering which may be available in other applications if someone else is feeling experimental or indeed peckish.

Posterizing Channels

Crop of Blimey!: first half 24-bit, second half 16-bit posterized

There’s yet another method which is available for quantizing direct color images, and that’s to posterize each channel separately. Like before the first half of the image is 24-bit and the other half is 16-bit except with each channel posterized instead of indexed. Posterizing lends itself well to PNG’s compression algorithm because it tends to create larger areas of flat color. The file yielded was around 2.53 MB — an astounding 34% decrease in size. Color preservation is thrown out the window using this process because of how it works, so when color needs to match up with something else it’d be a good idea to check to see if the composite image after channel posterization is acceptable. Additionally, because of the fact that bit depths like 16-bit don’t have the same bit depth per channel greys are affected by being skewed in one color direction; in the case of 16-bit greys are bent purple. To get around this problem the channels can be posterized to a level bit depth like 15-bit (5-bit per channel) or 18-bit (6-bit per channel) as an equal posterization on each channel would prevent the color’s warping. In the example above, however, this process is perfectly suited; the original is hardly noticeable from the quantized one.

Quantization Photoshop Actions

Opera Mac OS X

These processes can be quite monotonous, so they naturally lend themselves well to automation. I’ve created some Photoshop actions which make these processes effortless. My prebuilt actions don’t cover every possible bit depth and method for quantizing color channels, but they’re easily adaptable. They can also in fact be mixed and matched to produce different results.

Download

I’m offering this up for download without any warranty at all, and I’m not going to accept any responsibility for any damages it might cause to your computer. Installing Photoshop actions isn’t to my knowledge capable of causing harm to a computer. The compressed archive contains a license which allows the user to do whatever he/she wishes to do with it as long as attribution and a copy of the license is provided with it. This software requires Adobe Photoshop and a computer capable of running it to install.

Quantization Photoshop Actions (2.19 KB) — After unarchiving double-click on the quantization.atn file to install.