3
votes

So I am trying to write a simple bmp to jpeg image converter program. As you may know there are 3 major steps involved in jpeg compression, DCT, Quantization and entropy encoding. I want to write the values computed at each of these intermediate stages to a file.

Now my question is after I've converted each pixel's RGB value to YUV format and stored it into 3 separate 2-D arrays, is the DCT computed on each of these arrays? Thus giving me 3 different arrays with DCT applied on them? If that is correct then the next step would be to quantize each of the arrays corresponding to Y, U, and V. Then entropy encoding must be applied to each of these 2-D arrays.

How are these 3 arrays combined/written to a file so they can be readable as .jpeg files?

1

1 Answers

3
votes

First of all I suggest you to download the jpeg specifications.

You have to compute the DCT indipendently for each color channel: so for the YUV color space you have to compute one DCT for each Y 8x8 block, one for the U and one for the V channel.

Most of the times the U and V channels are subsampled: this mean that there are 2 or 4 Y blocks for each U and V.

After the DCT has been applied, then you can quantize each DCT: different channels usually need different quantization tables (the jpeg specifications suggest the correct tables).

The result of the quantization is then encoded using the Huffman algorithm: you can dump to the jpeg stream each color channel in interleaved (one to 4 Y blocks are followed by 1 U and 1 V block) or linear mode (first all the Y blocks, then all the U and then V).

The jpeg stream may contain RST tags which re-synchronize the decoding in the case of loss of bytes.

But you really need to have the jpeg specs to complete this task.