JPEG Visual corruption due to bad header

By | May 9, 2019

JPEG visual corruption

In general there are two kinds of symptoms that indicate a JPEG image is corrupted:

  1. Photo can not be viewed at all; Windows Explorer fails to display thumbnails
  2. JPEG can be opened but there is visual corruption in one form or the other

In case 1 it is safe to assume at least the header of the file is damaged. Usually if there is visual corruption then there is corrupted data within actual image data (rather than in header).

However, recently I got a number of cases where the photos were visually corrupted although there appeared to be no corrupt data within the JPEG bitstream: No invalid JPEG markers were present and the JPEG decoder returns no errors.

Coincidentally I found some very similarly corrupted JPEGs on my own hard drive. This is the type of visual corruption I am talking about:

JPEG visual corruption

JPEG visual corruption – no corrupt data in JPEG bitstream

Even if the cause would be in the JPEG bitstream, due to the number of shifts in the image data, patching it would be close to impossible. Because it appears the image data is shifting at a very regular interval.

Regular Interval

It then occurred to me that this regular interval could be due to ‘some variable’ that is vital during the decoding of the RAW JPEG data ‘being off’.

JPEG is encoded and decoded in blocks that are typically 8×8, 16×8 or 16×16 pixels called MCUs (minimum coded unit). Once a block is decoded the decoder moves to the next block. Then at some point the decoder needs to drop a line or the resulting image would be either 8 or 16 pixels in height. Of course the image width is the most logical variable for determining if the end the line is reached (total width in pixels / MCU width in pixels).

When doing header repair and picking a wrong size resolution reference file you could also see this type of corruption.

Example

Assume a JPEG of 64 x 64 pixels and MCU size 8×8 pixels. The decoder (built into your image viewer or photo editor) first needs to decode 8 MCUs and then drop a line. Then it decodes the next 8 MCUs etc..

If for some freak reason the image dimensions would be incorrectly recorded in the header then the decoder would drop a line too early or too late. Which would in turn cause visual corruption: ‘shift’. In the first Table image dimensions are correctly 64×64 pixels. In second table Image dimensions are 56×64 so decoder drops a line one MCU early causing image data shift.

Table 1. JPEG dimensions 64×64 pixels, each block (MCU) 8×8 pixels

 

Table 2. JPEG dimensions 56×64 pixels, each block (MCU) 8×8 pixels

How to repair this visual corruption?

Assuming all image data is actually present and no other errors are present steps to repair include:

  1. Find correct JPEG image dimensions
  2. Patch the JPEG header to reflect correct dimensions so the decoder will use them

JPEG-Repair Toolkit allows overriding dimensions since version 2.1. Both width and height are adjustable so you can experiment to find the correct values and store them once the correct values are known.

Procedure:

  • Run JPEG-Repair > Patch > Select file > Click Repair.
  • In the File Properties section check ‘Override’.
  • Use up/down arrows next to Width and Height properties to experiment. Values are always a plural of 8.
  • Click Apply to test values
  • Click Save button (next to Apply) to save the JPEG

Experimenting becomes a lot easier if you know valid dimensions for your camera type. The user guide often shows you exact values for size settings L(arge), M(edium) and S(mall).

 

Leave a Reply

Your email address will not be published. Required fields are marked *