# Determining the resulting pixel value before creating a compressed JPEG image

Is it possible to determine the resulting pixel values and modify them before the compressed jpeg image is actually created?

For example, I'm at the stage where I already have the quantized DCT coefficients of the original image and I want to change all pixels of the resulting image with a (255, 255, 0) value to (255, 254, 0).

What I was thinking is, if this is possible I would need to:

1. Get the equivalent DCT coefficient of original image and do quantization
2. Determine the resulting pixel values and do desired changes
3. Obtain the new DCT coefficients that will result to it
4. Proceed to Huffman encoding

Can the following be done during Huffman encoding?

1. Get the equivalent DCT coefficient from YCbCr and do quantizations
2. Do Huffman encoding and modify the desired pixel
• In your example you show a color of (255, 255, 0) but each DCT block will correspond to a different part of the color. You would have three blocks of DCT coefficients to work with in this case. Also, are your colors in RGB colorspace? If so, JPEG is typically in the Y'CbCr colorspace and you'll have to convert between the two as well. Feb 7, 2012 at 19:38
• The only method I've heard of is trial and error, which can, and may need to done on a macroblock basis. Compress, decompress, compare against the desired result, then vary the input macroblock, including pixels other than the target, and try again. Feb 8, 2012 at 2:44

To give you an upfront answer your question you want to determine the $pixel-values$ from $DCT-Coefficients$ probably there is nothing on earth better than doing IDCT itself!

However, leaving aside some vagueness i can re-phrase your question:

If i have an algorithm x - that is defined to work on Pixels, here is what i do:
DCT-Coeffs ->  IDCT  -> pixels  Algorithm_X  -> pixels  DCT  -> DCT-Coeffs
(ignoring other stuff that is peripheral).

The question is: is there a short cut to this brute force where i am obviously undoing quite some stuff done in first stage?

What you are referring to is what is generally known as Compressed Domain Processing.

DCT-Coeffs ->  Compressed_Domain_Algorithm_X  -> DCT-Coeffs

Basically, all compressed domain algorithm exploits the linearity of DCT. For example

$$Y = \alpha * X_1 + \beta * X_2$$ then,
$$DCT(Y) = \alpha * DCT(X_1) + \beta * DCT(X_2)$$ where $\alpha$ $\beta$ are scalars.

There are many things possible, and while many NOT. It is too vast to answer the set of all compressed domain algorithms which are possible, but i would recommend key readings that will set a great stage for you.

Specifics: If you are using libjpeg you can actually get quantized DCT co-efficients directly. However, most above literature refers un-quantized DCT co-efficients.