Why can lossless JPEG rotation create a vertical line on the edge?

Asked 7/16/2014

1 views

2 answers

0

I rotate JPEGs in a script with exiftran -a -i mypic.jpg. On some images, especially photos from my Galaxy Note 3, the rotated result shows a thin vertical line along the right edge.

A viewer also warns that physical rotation may cause small distortions because the image dimensions are not multiples of 8. mogrify -auto-orient seems to behave better.

Why does this happen with some JPEGs, and why can a “lossless” JPEG rotation still produce an edge artifact?

Originally by Photography Stack Exchange contributor. Source · Licensed CC BY-SA 4.0

Photography Stack Exchange contributor

12y ago

2 Answers

2

The JPEG algorithm uses a combination of lossy and lossless steps. The algorithm breaks up an image into 8x8 blocks, known as Minimum Coding Units (MCU). If an image has dimensions that are not divisible by 8, MCUs along the right and bottom edges are padded. Each MCU is then processed through FFT/DCT (Fast Fourier Transform/Discrete Cosine Transform) and quantized. Quantization is the main lossy step. Then a series of lossless steps are performed, including lossless compression, are performed.

Lossless operations can be performed on JPEG by undoing the final lossless steps and performing lossless operations on the MCUs, such as rearranging them. If image dimensions are not divisible by 8 (or 16 if chroma subsampling is enabled), artifacts may appear along the edges because padded blocks will be along the wrong edge and the blocks that need padding won't be.

By default, exiftran discards these blocks. However, these blocks can be retained, and artifacts along the edges appear, if exiftran is called with the -np flag.

I suspect most digital cameras and cell phones produce images with dimensions that will not cause this problem because it makes optimal use of available resources to do so. This is the case for devices I have checked, made by Apple, Canon, FujiFilm, Lenovo, and Samsung.

Originally by user75526. Source · Licensed CC BY-SA 4.0

user75526

8y ago

0

AI Answer

Generated from our catalog & community — verify before relying on it.

JPEG stores image data in small blocks called MCUs, typically 8×8 pixels, or effectively 16-pixel boundaries with some chroma subsampling modes. A “lossless” rotator such as exiftran usually works by rearranging those compressed blocks without fully decoding and re-encoding the image.

If the JPEG’s width or height is not an exact multiple of the required block size, the rightmost and/or bottom edge uses padded partial blocks. When those blocks are rotated losslessly, the padded edge data can end up visible as a thin line or small artifact on the new edge.

So the rotation is lossless for the JPEG blocks, but not every image dimension allows a perfect edge-preserving transform.

That is why some programs warn about dimensions not being multiples of 8, and why phone JPEGs can show this issue. A tool like mogrify -auto-orient may look better because it likely decodes and re-encodes the image instead of only rearranging JPEG blocks, which avoids the block-edge artifact at the cost of recompression.

UniqueBot

AI

12y ago

Your Answer