2
$\begingroup$

I have been reading the guide from:

I eventually reached the point where it states:

Finally, we want to convert the DCT DC value to an RGB value. Assuming that the gain of the DCT transform is 4, we divide the values by 4 to get block1 = -128, block2 = +127.

I have to admit, I do not understand where this magic value '4' is coming from. With the help of djpeg and jpegtran, I was able to craft two different JPEG files:

  1. white512.jpg (enter image description here) is a JPEG where the DC value is +512,
  2. white338.jpg (enter image description here) is a JPEG where the DC value is +338

Using a simple check, we can confirm they both decompress to the same white ppm file:

% djpeg -outfile 338.ppm white338.jpg
% djpeg -outfile 512.ppm white512.jpg 
% md5sum 338.ppm 512.ppm
18f695bae266c6a24506936dab1dfc23  338.ppm
18f695bae266c6a24506936dab1dfc23  512.ppm

Could someone please tell me:

  1. Where is this value '4' value coming from ?
  2. How come both the DC value 338 & 512 expand to the same white value (0xFF) ?

For reference, the JPEG files uploaded on imgur have been edited in my back. In particular the upload process strips the APP0 marker. To get the exact same image as I have on disk, one can apply back the default jpegtran settings:

% jpegtran -outfile white512.jpg -optimize jTKco.jpg
% jpegtran -outfile white338.jpg -optimize WqThz.jpg
% md5sum white512.jpg white338.jpg
695527adb6b269214e38e45157edb256  white512.jpg
051434f11491bdf1c1be38ad8f69f5be  white338.jpg
$\endgroup$
1
$\begingroup$

Here is the small experiment I was able to conduct on my Debian system. I crafted two small $8 \times 8$ JPEG files again:

  • white: (white_1016.jpg)
  • black: (black_minus1024.jpg)

I used the following commands:

% convert -size 8x8 -depth 8 xc:white white.pgm
% convert -size 8x8 -depth 8 xc:black black.pgm

then:

% cjpeg -sample 1x1 -quality 100 -grayscale -outfile white.jpg white.pgm
% cjpeg -sample 1x1 -quality 100 -grayscale -outfile black.jpg black.pgm

Note that in this case, it is a single component JPEG (no color space transform):

% file white.jpg
white.jpg: JPEG image data, JFIF standard 1.01, aspect ratio, density 1x1, segment length 16, baseline, precision 8, 8x8, components 1

Upon inspection, one can check that the DC coefficient stored in white.jpg is 1016, while the DC coefficient stored in black.jpg is -1024. Which can also be represented as:

$$\begin{bmatrix} 1016&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0\\ 0&0&0&0&0&0&0&0 \end{bmatrix}$$

One can also check that the quantization table is simply a a bunch of ones:

% djpeg -verbose -verbose -outfile dummy.ppm white.jpg
[...]
Define Quantization Table 0  precision 0
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1
           1    1    1    1    1    1    1    1

So taking the entry-for-entry product with the quantization matrix from above results simply in the same matrix.

Going back to the equations on wikipedia page:

$$f_{x,y} = \frac{1}{4} \sum_{u=0}^7 \sum_{v=0}^7 \alpha(u) \alpha(v) F_{u,v} \cos \left[\frac{(2x+1)u\pi}{16} \right] \cos \left[\frac{(2y+1)v\pi}{16} \right]$$

Simply gives on our case (no rounding error):

$$f_{x,y} = \frac{1}{4}\left(\frac{1}{\sqrt{2}\sqrt{2}}1016\times 1+63\times 0\right) = 127$$

So adding 128 to each entries lead to:

$$\left[ \begin{array}{rrrrrrrr} 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \\ 255 & 255 & 255 & 255 & 255 & 255 & 255 & 255 \end{array} \right]$$

Repeating the above for the black.jpg image with DC value -1024 gives:

$$f_{x,y} = \frac{1}{4}\left(\frac{1}{\sqrt{2}\sqrt{2}}\times -1024\times 1+63\times 0\right) = -128$$

Which is what we expected.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.