Compressed Image File Formats: JPEG, PNG, GIF, XBM, BMP

Author: John Miano
This Month Stack Overflow 13


by anonymous   2019-07-21

You have two choices with the huffman coding.

  1. Use a pre-canned huffman table.
  2. Make two passes over the data where the first pass generates the huffman tables and the second pass encode.

You cannot have a different dictionary for each MCU.

You say you have the run length encoded values. You huffman encode those and write to the output stream.


You need to be sure that the matlab huffman endocoder is JPEG-compatible. There are different ways to huffman encode.

You need to write the bits from the encoder to the JPEG stream. This means you need a bit level I/O routine. PLUS you need to convert FF values in the compressed data into FF00 values in the JPEG stream.

I suggest getting a copy of

to show how the encoding is done.

by anonymous   2019-07-21

If you have to do it yourself, you need to read the book and have a copy of the JPEG standard.

by anonymous   2019-07-21

What you are saying has no chance of happening. JPEG files have one of several file structures that share a common compressed data format.

A text file is going have lines of characters separated by , , or sequences that will just screw up a compressed JPEG stream.

If you want to understand how JPEG works, you should get this book:

by anonymous   2019-01-13

This book explains the process of decoding for programmers:

The entire process is way to complicated to fit within the space of a SO answer. PNG uses two different methods of compression: LZ and Huffman encoding.

by anonymous   2019-01-13

JPEG is somewhat unique in that it involves a series of compression steps. There are two that provide the most opportunities for reducing the size of the image.

The first is sampling. In JPEG one usually converts from RGB to YCbCR. In RGB, each component is equal in value. In YCbCr, the Y component is much more important than the Cb and Cr components. If you sample the later at 4 to 1, a 4x4 block of pixels gets reduced from 16+16+16 to 16+1+1. Just by sampling you have reduced the size of the data to be compressed by nearly 1/3.

The other is quantization. You take the sampled pixel values, divide them into 8x8 blocks and perform the Discrete Cosine transform on them. In 8bpp this takes 8x8 8-bit data and converts it to 8x8 16 bit data (inverse compression at that point).

The DCT process tends to produce larger values in the upper right corner and smaller values (close to zero) towards the lower left corner. The upper right coefficients are more valuable than the lower left coefficients.

The 16-bit values are then "quantized" (division in plain english).

The compression process defines an 8x8 quantization matrix. Divide the corresponding entry in the DCT coefficients by the value in the quantization matrix. Because this is integer division, the small values will go to zero. Long runs of zero values are combined using run-length compression. The more consecutive zeros you get, the better the compression.

Generally, the quantization values are much higher at the lower left than in the upper right. You try to force these DCT coefficients to be zero unless they are very large.

This is where much of the loss (not all of it though) comes from in JPEG.

The trade off is to get as many zeros as you can without noticeably degrading the image.

The choice of quantization matrices is the major factor in compression. Most JPEG libraries present a "quality" setting to the user. This translates into the selection of a quantization matrices in the encoder. If someone could devise better quantization matrices, you could get better compression.

This book explains the JPEG process in plain English:

by anonymous   2018-07-19

The encoding process depends upon whether you have a sequential scan or a progressive scan. The details of the encoding process are too complicated to fit within an answer here.

I highly recommend this book:,204,203,200_QL70_&dpSrc=srch

It is the only source I know of that explains JPEG end-to-end in plain English.

by anonymous   2018-03-18

JPEG is extremely complex. The only source I know of that explains this for programmers is:

by anonymous   2017-11-20

This is the only book that actually explains the process for programmers without academic mumbo-jumbo

by anonymous   2017-11-13
by anonymous   2017-10-08