# AIC - Color Conversion

For color images, the Advanced Image Coding codec converts the source image from the RGB color space to the YCbCr color space. This color space separates the luminance and chrominance in the image. The luminance of the image will be stored in the Y-channel. The Cb and Cr channels hold the chrominance values in blue and red directions respectively.

As an example, look at the Lena image and its RGB channels:

 RGB Image Red Channel Green Channel Blue Channel

As you can see, all three channels have roughly the same amount of contrast and "information", which doesn't make them good candidates for compression. The green channel looks the most natural and real. This is because our eyes are most sensitive to greens.

De purpose of the YCbCr color space in the context of compression, is that the chrominance channels usually contain much less information than the luminance channel, and are thus better compressible. The following example shows the same image, separated into its YCbCr channels:

 YCbCr Image Y Channel Cb Channel Cr Channel

The Y-channel is the perfect representation of the image in grayscale. As you can see, the Cb and Cr channels have much less contrast, which means that they can be compressed much better.

The following formulae are used to convert an RGB pixel to an YCbCr pixel:

 Y = 0.299R + 0.587G + 0.114B Cb = -0.169R - 0.331G + 0.5B Cr = 0.5R - 0.419G - 0.081B

The green component has the highest impact on the Y-channel, as our eyes are most sensitive to greens, followed by the red and blue components. When R, G and B are in the range of 0...255 (as is the case for 24 bit images), Y will also be in the range 0...255 and Cb and Cr will have ranges -128...127. Usually, the value 128 is added to the Cb and Cr channels to fit them into an unsigned byte data type.

The decoder uses the following formulae to convert these values back to RGB:

 R = Y + 1.402Cr G = Y - 0.344Cb - 0.714Cr B = Y + 1.772Cb

Note that the JPEG and H.264 standards usually sub sample the Cb and Cr channels to half their original resolution. This way, these channels contain even less information and can be compressed further. In the AIC codec, no sub sampling is used, since block prediction and binary arithmetic coding makes sub sampling less necessary and only has a negative impact on the image quality. With AIC, higher quality/compression ratios are possible when no sub sampling is used.

The color conversion code in AIC is based on the IJG JPEG code.

The next stage in the encoding process is block prediction. Only the Y channel is used to choose a prediction mode. The Cb and Cr channels use the same prediction mode as for the Y channel.