Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is re-encoding JPEG images an idempotent operation?

I am aware that JPEG compression is lossy. I have 2 questions:

Given an operation T:
1. Take a JPEG-80 image
2. Decode it to a byte buffer
3. Encode given byte buffer as JPEG-80

Is T an idempotent operation in terms of visual quality? Or will the quality of the image keep degrading as I repeat T? Does the same hold true for the JPEG-XR codec?

Thank you!

Edit: Since there have been conflicting answers, it would be great if you could provide references!

like image 383
Sau Avatar asked Feb 12 '13 21:02

Sau


People also ask

What encoding is used for JPG?

In a JPEG we store the DCT (Discrete Cosine Transform) information using Huffman encoding.

Is JPEG An encoding?

JPEG, the group of people, created JPEG, a standard for digital image compression, in 1992. Anyone who's ever used the internet has probably seen a JPEG-encoded image. It is by far the most ubiquitous way of encoding, sending and storing images.


1 Answers

It's not guaranteed, but it may happen. Especially if you repeat the encode -> decode -> encode -> decode process enough times, it will eventually settle on a fixpoint and stop losing quality further (as long as you stick to the same quality and same encoder).

JPEG encoding is done in several steps:

  1. RGB to YUV conversion
  2. DCT (change into frequency domain)
  3. Quantization (throwing away bits of the DCT)
  4. Lossless compression

And decoding is the same process backwards.

Steps 1 and 2 have rounding errors (especially in speed-optimized encoders using integer math), so for idempotent re-encoding you need to be lucky to get encoding and decoding rounding errors to be small or cancel each other out.

The step 3, which is the major lossy step, is actually idempotent. If your decoded pixels convert to similar-enough DCT it will quantize to the same data again!

JPEG XR also uses YUV, so it may suffer some rounding errors, but OTOH instead of DCT it uses a different transform that can be computed without rounding errors, so it should be easier to round-trip JPEG-XR than other formats.

like image 169
Kornel Avatar answered Jan 04 '23 05:01

Kornel