Search code examples
imagejpegemgucvsteganographyimage-conversion

A code for step by step JPEG compression


The JPEG compression steps are as follows:

Raw image data -> forward DCT -> Quantization -> Entropy encoding -> JPEG image

There are numbers of converters and APIs out there and the converting process is a single API call. I was unable to find a step by step code. My question is where can I find a code for each individual step, or can I perform these individual steps one by one and produce a standard JPEG image? I am using EmguCV for my image steganography project.


Solution

  • where can I find a code for each individual step

    If a C library might be a candidate for you, you should have a look at jpec a lightweight JPEG encoder written in C - note that it only supports grayscale images.

    The main encoding function (jpec_enc_run) is easy to read and provides each above step via internal functions:

    /* open = write JPEG headers */
    jpec_enc_open(e);
    
    while (jpec_enc_next_block(e)) {
      /* compute the DCT for the current 8x8 block */
      jpec_enc_block_dct(e);
    
      /* quantize the DCT coefficients for the current block */
      jpec_enc_block_quant(e);
    
      /* re-order the quantized coefficients with the zig-zag pattern */
      jpec_enc_block_zz(e);
    
      /* perform entropy coding of the current block and write to the global buffer*/
      e->hskel->encode_block(e->hskel->opq, &e->block, e->buf);
    }
    
    /* close = write JPEG end of image marker */
    jpec_enc_close(e);
    

    can I perform these individual steps one by one and produce a standard JPEG image

    This is not available out-of-the box with jpec, but you should be able to modify it for that purpose quite easily (by exposing and/or adapting the low-level internal functions).