JPEG ENCODER & DECODER




Download the source code

ALGORITHM - ENCODER

Divide the image into 8*8 blocks and do the following for each block
  1. Shift the block
  2. Perform a DCT on the block
  3. Quantize the block
  4. Subtract the last DC coefficient from the current DC coefficient
  5. Zigzag the block
  6. Zero run length encode the block
  7. Break down the non-zero coefficients into variable-length binary numbers & their lengths
  8. Entropy encode the run lengths & binary number lengths
  9. Write the entropy encoded information & binary numbers to the output


ALGORITHM - DECODER

  1. Read bit-by-bit  from the file to get the  DC coefficient, non-zero AC coefficients and the run lengths.
  2. Get the  DC coefficient and the 63 AC coefficients.
  3. Then we dezigzag the block.
  4. Add the last DC coefficient to the current DC coefficient
  5. Next we dequantize the block
  6. Next we perform the Inverse Discrete Cosine Transform .
  7. Finally, we shift the block by adding 128 to each value in the block
Merge all the blocks to get the final block .



RESULTS

Original Image
Standard JPEG Compression
JPEG Compression
JPEG Compression (Lossless)
Original BMP image : 54582 bytes

Standard JPEG : 10529 bytes
Compression Ratio : 1 : 5.2

Compressed Image : 5226 bytes
Compression Ratio : 1 : 10.5

Compressed Image : 34102 bytes
Compression Ratio : 1 : 1.6

Original BMP image : 53874 bytes

Standard JPEG : 11553 bytes
Compression Ratio : 1: 4.7

Compressed Image :  6218 bytes
Compression Ratio : 1 : 8.7

Compressed Image :  42505 bytes
Compression Ratio : 1 : 1.27

Original BMP image : 88918 bytes

Standard JPEG : 20623 bytes
Compression Ratio : 1 : 4.3

Compressed Image : 10658 bytes
Compression Ratio : 1 : 8.3

Compressed Image :  64405 bytes
Compression Ratio : 1 : 1.38

Original BMP image :  38830 bytes

Standard JPEG : 10834 bytes
Compression Ratio : 1 : 3.58

Compressed Image :  5029 bytes
Compression Ratio : 1 : 7.72

Compressed Image :  36572 bytes
Compression Ratio : 1 : 1.06

Original BMP image : 62326 bytes


Standard JPEG : 10881 bytes
Compression Ratio : 1 : 5.7

Compressed Image :  6055 bytes
Compression Ratio : 1 : 10.29

Compressed Image : 27322 bytes
Compression Ratio : 1 : 2.28