Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does JPEG encoding an 8x8 checkered image produce artifacts?

I have the following 64x64 image, which is composed of randomly-colored 8x8 squares.

enter image description here

Now, I push this image through PHP's imagejpg() function, which does 75% quality by default, and i receive this this result, with visible artifacts:

enter image description here

enter image description here

As I understand JPEG, the DCT is applied to individual 8x8 squares, which are all solid colors. The chroma sub-subsampling also has far more than enough resolution to provide the exact color, so that shouldn't have an effect either.

How come I'm seeing artifacts inside the 8x8 squares? Is it possible that 16x16 DCT is being used? Are the 8x8 squares being chosen in random locations, and not from the corners? Is there some other possible reason that would cause my squares to not perfectly line up with the 8x8 DCT partitioning?

Thanks!

like image 245
David Davidson Avatar asked Oct 28 '25 08:10

David Davidson


1 Answers

I believe I have figured it out. The DCT is not applied to 8x8 blocks on the raw image, but is applied separately to each of the Y-Cb-Cr color channels. The Y channel is not sub-sampled, but the Cb and Cr are sub-sampled by half (4:2:0).

That means that a solid 8x8 square will only have 4 horizontal Cb samples, and 4 vertical Cb samples. This is is not 8x8. So when the DCT is partitioning the Cr and Cb channels, an 8x8 section will be taking Cb and Cr samples that belong to adjacent squares.

I've increased the size of each square to 16x16, and the artifacts have disappeared.

I'm still seeing small off-by-one values, but I'm guessing this can be attributed to round-off error in the algorithm or similar.

like image 125
David Davidson Avatar answered Oct 29 '25 23:10

David Davidson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!