Texture Compression with Luminance + Chrominance (YUV, or YCbCr)

Hello. I'm Hirayama from the Technology Department.
This post is a sequel to my earlier article on indexed color images and article on 16-bit color images. Today I'll introduce a technique that converts RGB into "luminance" and "chrominance" and then compresses it. Its main characteristics are:
- Runs on GLES2
- Fairly good image quality
- 12 bits per pixel when opaque (effectively a 3/8 compression ratio), 20 bits per pixel with alpha support (a 5/8 compression ratio)
- Compression is fast and doesn't require any particular tool
- Bilinear filtering works without any weird shader tricks
- Like indexed color, it needs a dedicated component and shader, which is a pain
And the conclusion hasn't changed: isn't ETC2 good enough?
It's more or less the same thing as ChromaPack, an existing public implementation for Unity, and differs only slightly in points like:
- If the original texture is a power of two (POT: Power Of Two), it stays one
- Handling of the alpha channel
As usual, the source is published on GitHub. I've also put a working version up in WebGL so you can compare the image quality right away.
The option of cutting resolution
Indexed color compression reduced size by "cutting the number of colors and replacing them with table lookups," and 16-bit conversion by "cutting the number of bits assigned to each color." Neither touched the number of pixels, that is, the resolution.
But reducing the number of pixels, that is, lowering the resolution, also cuts the size, so of course that deserves consideration too.
In fact, in real projects, before debating formats, the first thing to check is whether the resolution is appropriate. Just because the machine it runs on has a resolution of 1920x1080 doesn't mean your images have to be 1920x1080, and rendering below the hardware resolution used to be common practice too. (The PS3 era is the one that sticks in my memory. Plenty of articles are still around in which developers admit they lowered the resolution for lack of performance, like this one and this one.)
In our Tokyo Prison, the standard resolution was 1136x640, and most images were made at a size that displays at 1:1 at that resolution. On a 1920x1080 device they get scaled up. We decided that preparing more detailed images than that, just to show them on a small smartphone screen, would hurt download time, storage use, memory use, processing load, development cost, and so on.
So cutting resolution matters, but simply cutting it is a business decision, not a technical one. In this article I'll talk about cutting resolution by technical means.
The idea of changing the resolution per color
Human vision is uneven.
For example, as far as red, green and blue go, human vision is sensitive to green and insensitive to blue. In the RGB565 compression format I mentioned in the previous article, red and blue get 32 levels, but green gets 64. That's to account for the characteristics of human vision. So let's apply the same idea to resolution. How about keeping only green at the original resolution and dropping red and blue to half?

On the left is the original image, in the middle is this experiment, and on the right is the image simply halved vertically and horizontally.
Green is showing up along the edges. It's unusable. That's hardly surprising, but compare it with how blurry the simply halved one on the right is. Considering it's been cut to half the original size (24 bits → 12 bits in RGB), doesn't it seem to hold on to its overall sharpness?
The YUV approach
A smarter take on this approach is YUV (also called YCbCr).
YUV converts RGB into luminance Y and chrominance U and V. Human vision is sensitive to brightness, that is, luminance, so Y keeps the original resolution. On the other hand, it's insensitive to changes in color, the chrominance, so U and V have their resolution lowered. Most video is compressed this way, and for still images, JPEG uses this technique. It has a long and respectable pedigree.
Implementation
Encoding
First, mix R, G and B just right to create the luminance Y, which represents brightness.
Various specific recipes for just right have been proposed, but here I used a long-established method called ITU-R BT.601.
static void ToYuv(ref Color32 pixel)
{
const float yr = 0.299f;
const float yb = 0.114f;
const float yg = 1f - yr - yb;
float y = (yr * pixel.r) + (yg * pixel.g) + (yb * pixel.b);
We make the luminance Y by mixing 29.9% red, 11.4% blue, and the rest, 58.7%, green. This reflects the fact that the human eye is most sensitive to green, then red, and insensitive to blue. Y ranges from 0 to 255.
Next comes chrominance. The chrominance values U and V are calculated as "blue - luminance" and "red - luminance"
respectively, multiplied by a suitable factor so they fall in the range -(255/2) to (255/2), and finally (255/2)
is added to make a value from 0 to 255.
const float uScale = 0.5f / (1f - yb);
const float vScale = 0.5f / (1f - yr);
float u = (pixel.b - y) * uScale;
float v = (pixel.r - y) * vScale;
u += 255f / 2f;
v += 255f / 2f;
}
Let's take U as an example. (b-y) gives the chrominance, which is multiplied by a value called uScale so that it
spans (-255/2) to (255/2).
How do we calculate uScale? Think of, say, "a very blue color." Let b be 255, and r and g be 0. Very blue indeed. Then y
is 255*yb and b is 255, so (b-y) is 255*(1-yb). We want this to become 255/2, so uScale should be
(255/2) / (255*(1-yb)). The 255 is in both the numerator and the denominator, so it cancels, giving 0.5/(1-yb), which matches
the code.
Then add 255/2 to bring it into the range 0 to 255, and you're done. To store it in a Color32, round it and cast it
to byte. You can round by "adding 0.5 and then truncating to an integer."
By the way, the formulas you often see on the web,
Y = 0.299R + 0.587G + 0.114B
U = -0.169R - 0.331G + 0.500B
V = 0.500R - 0.419G - 0.081B
are what you get if you precompute the constants that multiply r, g and b. It's probably slightly faster, but there are as many as 9 constants, and I don't want bugs from copying one wrong, so here I wrote it in a way that needs only 2 constants.
Encoding result
Here are the resulting split textures.


On the left is Y, on the right are U and V. The original is 275x108; y is left as is, while u and v are halved and joined together.
You could split them into 3 textures, but that means more files, which is a nuisance, so I stuck u and v together. ChromaPack packs all of y, u and v into one, but as a result the texture is 1.5 times the original width, which breaks things if you want to keep the texture size a power of two. I love power-of-two textures, so I implemented it this way (some hardware used to draw power-of-two textures faster; I haven't checked whether that's still true today). If you want the texture to repeat, it's better to split u and v as well.
Decoding
Once the textures are ready, it's time for the rendering side. We convert back to RGB in the shader.
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uvY = TRANSFORM_TEX(v.uv, _MainTex);
o.uvU = float2(o.uvY.x * 0.5, o.uvY.y);
o.uvV = float2(o.uvU.x + 0.5, o.uvU.y);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half3 yuv;
yuv.x = tex2D(_MainTex, i.uvY).a;
yuv.y = tex2D(_UvTex, i.uvU).a;
yuv.z = tex2D(_UvTex, i.uvV).a;
half4 rgba;
rgba.xyz = yuvToRgb(yuv);
rgba.a = 1.0;
return rgba;
}
half3 yuvToRgb(half3 yuv)
{
const float wr = 0.299;
const float wb = 0.114;
const float uScale = 0.5 / (1.0 - wb);
const float vScale = 0.5 / (1.0 - wr);
const float wg = 1.0 - wr - wb;
half3 rgb;
rgb.b = ((yuv.g - 0.5) / uScale) + yuv.r;
rgb.r = ((yuv.b - 0.5) / vScale) + yuv.r;
rgb.g = (yuv.r - (yr * rgb.r) - (yb * rgb.b)) / yg;
return saturate(rgb);
}
The vertex shader generates the texture coordinates for Y, U and V. Doing it in the vertex shader means less calculation than in the fragment shader, and I've seen hardware that slows down dramatically when texture coordinates are calculated in the fragment shader.
The fragment shader calls tex2D three separate times, for Y, U and V, using the texture coordinates it received, and converts YUV back into RGB with yuvToRgb().
What yuvToRgb does is simply invert the transform applied at import time. In the shader the range is 0 to 1 rather
than 0 to 255, so the constant that was (255/2) earlier has become 0.5 and so on, but basically it's just the
inverse transform, retracing the calculation backward. Also, bilinear filtering can produce colors that weren't in the
original, so the result may fall outside 0 to 1; saturate clamps it back into that range.
Here too, if you precompute the constants that multiply y, u and v, you can write it like this:
rgb.r = yuv.r + (1.402 * yuv.b) - 0.701;
rgb.g = yuv.r - (0.344 * yuv.g) - (0.714 * yuv.b) + 0.529;
rgb.b = yuv.r + (1.772 * yuv.g) - 0.886;
This is probably faster.
Results
Now for the result. It's the image from the top of the article.

The middle one is compressed, but I can't tell it apart from the uncompressed one on the left. If that halves the size, isn't that a pretty good deal?
For comparison, I've also put one compressed with dithered 565 on the right. It's quite clean too, and unless you zoom in you can hardly tell the difference. If you can't tell the difference, smaller is better.
Weaknesses of YUV
With that image, compression didn't degrade it much, and it looks quite usable. But does it really have no weaknesses?
Of course it does. Take a look at the image below.

It's an image with four areas of vivid colors. On the left is the original image, on the right the compressed one.
The edges of the letters look fishy. I don't remember adding an outline. The border between red and green looks off too. Color is stored at half resolution, so where the color changes abruptly, strange colors appear. Unlike recent formats such as BC7 and ASTC, it compresses everything the same way, so it can't give special treatment to areas like these.
The alpha problem
Note that YUV only deals with the RGB part; the alpha channel is a separate matter. ChromaPack, too, supports only 1 bit of alpha: "transparent or not."
However, if the simple approach of "putting the alpha channel in a separate texture" is good enough, supporting it is easy. I included that in this implementation as well.

On the left is the RGBA32 original image. In the middle, the original's RGB has been converted to YUV, A has been split off unchanged into a separate texture, and the two are combined in the shader. On the right is the image reduced to 4 bits per channel (ARGB16).
It may come in handy in cases like "simple 16-bit conversion is unacceptable, but plain RGBA32 is too big."
Summary
Converting to YUV gives 12 bits per pixel, a middling compression ratio. The importer, component and shader all have to be custom, so it's as much of a pain as indexed color was.
But compared with indexed color, it's better in the following ways.
- Bilinear filtering doesn't get expensive
- No dedicated shader is needed for bilinear filtering
- Quality doesn't depend on how good the color-reduction software is
With indexed color, bilinear filtering had to be done by hand in the shader. This time, you get bilinear just by turning on the texture's bilinear filter setting. You don't need a separate shader for bilinear either.
And the quality is mostly good; unless an image specifically hits its weak spots, you can use it without worrying. No special software is needed either, and just leaving it to the importer gives decent quality.
It only halves the size compared with 24-bit RGB, but most GPUs inflate 24-bit textures to 32 bits in memory, so in terms of memory consumption it's 3/8, which isn't so bad. And if you need an alpha channel, you can add one.
All in all, it suits cases where "you don't want to agonize over quality, but smaller beats not smaller, it's clear where it'll be used, and a separate component is acceptable."
But I know what you're thinking: isn't ETC2 good enough?