dc coeffs value for a black region

15 views
Skip to first unread message

David Miguel Susano Pinto

unread,
Sep 28, 2023, 1:25:04 PM9/28/23
to libjpeg-turbo User Discussion/Support
Hi

I'm trying to paint a region black by modifying that region
coefficients.  I am aware of jpegtran and its `-wipe` option and
that's been a big help to understand the internals.  However, that
greys out the region and I'm trying to blacken it.

Assuming YCbCr, where with the [-128 127] range black is:

    ycbcr([-128 0 0])

I was expecting that zeroing all coefficients of all components and
setting the DC of the first component to:

    -CENTERJSAMPLE / comp_info.quant_table[0].quantval[0]

was the way to go but that's not it (I just get a slightly darker
grey).

I can't figure out what should I set the DC coefficients for black.
Can someone help?

Many thanks
David

David Pinto

unread,
Sep 29, 2023, 8:59:30 PM9/29/23
to libjpeg-t...@googlegroups.com
On Thu, 28 Sept 2023 at 17:25, David Miguel Susano Pinto
<caran...@gmail.com> wrote:
> [...]
> Assuming YCbCr, where with the [-128 127] range black is:
>
> ycbcr([-128 0 0])
>
> I was expecting that zeroing all coefficients of all components and
> setting the DC of the first component to:
>
> -CENTERJSAMPLE / comp_info.quant_table[0].quantval[0]
>
> was the way to go but that's not it (I just get a slightly darker
> grey).
>

I found the following on jdct.h:

/*
[...]
* The DCT inputs are expected to be signed (range +-CENTERJSAMPLE).
* The DCT outputs are returned scaled up by a factor of 8; they therefore
* have a range of +-8K for 8-bit data, +-128K for 12-bit data. This
[...]
*/

and now, setting the DC of the first component to:

(-CENTERJSAMPLE * 8) / comp_info.quant_table[0].quantval[0]

seems to works fine but this hardcoded 8 seems a bit odd and I'm
wondering if it's specified somewhere else.

Many thanks
David

DRC

unread,
Oct 18, 2023, 4:14:59 PM10/18/23
to libjpeg-t...@googlegroups.com
The scaling in question is only for the intermediate results of the
forward DCT, and the coefficients are subsequently descaled during
quantization.  DCT coefficients are normally in the range of -1024 to
1023 for 8-bit precision, so it is expected that the luminance DC
coefficient should be -1024 for pure black when there is no quantization
(quality = 100.)  However, the libjpeg forward DCT implementation
actually produces a luminance DC coefficient of -8192 for pure black,
and that value is subsequently descaled by 8 when it is quantized.

As to why the DCT coefficients are in the range of -1024 to 1023 for
8-bit precision, bear in mind that DCT coefficients are an expression of
the frequencies within an 8x8 block of samples.  (A sample is, in this
case, the Y, Cb, or Cr value for one pixel.) For the DC coefficient, the
more general 2D DCT formula (refer to
https://dsp.stackexchange.com/questions/60801/dc-coefficient-of-jpeg-block)
reduces to the sum of all 64 sample values divided by 8.  So the
luminance DC coefficient for an 8x8 block of pure black would be -128 *
64 / 8, which is the same as -128 * 8.

DRC
Reply all
Reply to author
Forward
0 new messages