Repair corrupt JPEG bit stream – Invalid JPEG marker

<< Back to JPG-Repair

Repair an invalid JPEG marker in bit stream using JPG-Repair

JPEG Header, Image Data and JPEG markers

A JPEG file basically consists of a header or some meta information and the actual image data. A header describes how the image data is encoded and compressed. So, the image data is encoded and compressed and as such it is impossible to determine what byte corresponds to what pixel in a JPEG without decoding the image data. Changing, adding or removing a single byte affects all data following that byte.

That being said, the effects of this corruption can be small to invisible or may ruin an entire picture In case of an invalid JPEG marker inside the image bit stream, part of the picture is probably cut off. Opening and saving such a picture in a photo editor makes the corruption permanent: When the editor opens the file, the image stream is decompressed and decoded. When saving the file, it is compressed and encoded. At this point changing the corrupt byte back to it’s original value is no longer possible. It is no longer even there due to the way compression works.

Symptoms of an invalid JPEG Marker

In general a JPEG with corruption limited to the image bit stream can be opened using any image viewer or editor. Corruption shows as:

  • Color bands, from one point (or at multiple points) on, colors may look ‘off’. See above JPEG glitching example.
  • Shift in image data. Vertical lines are no longer aligned. Image data drops off at end of image and reappears at the beginning.
  • No image data after a certain point, just a grayish square. So in other words, the image is only partially displayed. This is the case when an invalid JPEG marker is present inside the compressed/encoded image data.
No more image after a certain point may indicate corruption of JPEG image bit stream

No more image after a certain point may indicate corruption of JPEG image bit stream

Patching the JPEG image bit stream

The best method to mitigate this type of corruption is by editing the image bit stream directly. Editing binary data (files, disks) on this level is also called ‘patching’. It usually requires a hex editor.

There are folks that purposely corrupt JPEG files using a hex editor to create art. It is called JPEG glitching or moshing. Basically a hex editor is used to modify, add or remove arbitrary bytes, save the file and view the result in an image viewer. The same process can be used to repair a corrupt JPEG file.

JPG-Repair includes tools to patch the image bit stream without the use of a hex editor.

A purposely corrupted JPEG .. Art or?

A purposely corrupted JPEG .. Art or? Image source: http://datamoshing.com/tag/jpeg/

Limitations

Using the method of patching the JPEG mage bit stream, significant improvements to the JPEG image can be achieved. Often however the result is not 100% and additional work may be required using a photo editor such as Paint.NET or whatever photo editor you prefer.

Changing this corrupt JPEG to a somewhat presentable image required removing corrupt bytes which caused image shift > then to re-open the file with 'edit processed file' enabled to start adding bytes again.

Changing this corrupt JPEG to a somewhat presentable image required removing corrupt bytes which caused image shift > then to re-open the file with ‘edit processed file’ enabled to start adding bytes again.

 

Requirements

JPG-Repair always requires an intact reference or sample file. In most cases even the file with the corrupt image bit stream will do. Simply copy it and rename it to ‘sample.jpg’.

The corrupt file should be unmodified! Once you have loaded it into a photo editor and saved it again, the file can no longer be repaired.

The ‘editor’

The biggest challenge is identifying the offending bytes. Since the image data is compressed we basically work with a ‘binary blob’ where we can not tie specific bytes to specific pixels in the corrupt JPEG. So, the method used is trial and error. JPG-repair offers some tools to make this easier.

Always work on a copy of the corrupt JPEG!

  • Invalid JPEG markers in image bit stream: JPG-Repair can identify invalid markers and other odd looking data. In many cases, this is where the trouble it located. By deleting those invalid byte values and the surrounding area, often corrupt data can be eliminated and the JPEG image improved. 9 out of 10 times I am able to pinpoint the offending bytes this way.
  • It is easy to apply a change an review the result (resulting image). You can observe where in the image effects are visible and by using trial and error you can zoom in on the corruption.
  • By clicking the image JPG-Repair gives an estimate byte address.

The ‘editor’ or ‘patcher’ allows you to add, remove or change bytes.

JPG-repair offers several tools to patch a JPEG image bit stream

JPG-repair offers several tools to patch a JPEG image bit stream

After you open a file, JPG-Repair keeps a copy of the file as it’s opened in memory, this your starting point each time you change your modification (add, remove). Modifications you make are done on a 2nd copy of the file in memory. This 2nd copy is immediately saved to the file you opened as well.

If you try a new value for ‘add bytes’ for example, it will apply this to starting point! So, this new modification is not ‘added’ to the one you did previously. Instead it is applied to file as you initially opened it. This allows you to experiment with different values.

So, if you add 64 bytes to the image bit stream, and do that again, it does not mean you have no added 128 bytes. To add 128 bytes you will actually have to set the value for add/remove bytes to 128.

If you want modifications to ‘stick’, you need to reload the file (select the file > click Repair). In addition, you need to check the ‘open processed file’ checkbox.

Procedure

Place a copy of the corrupt file and a sample in the same folder > Select the corrupt file and click repair.

Always work on a copy of the corrupt JPEG!

Often JPG-Repair will automatically detect ‘bad’ bytes. For example, these can be invalid markers or areas that are affected by bad sectors on the source medium. Again, often these bad or corrupt bytes tend to group together. By eliminating the group of offending bytes the JPEG image can be improved.

If as a result of the corruption the image appears to shift to the right from one point, remove bytes. Experiment with different values by using the up/down arrows. If the image does not shift in the desired direction or not enough, increase the value. If you over-shoot, slightly decrease. At this point use the slider to set 1 byte jumps. Once the image shift is corrected, continue to apply small changes to see if an improvement in color shift can be achieved.

An example repair 1

Example of a corrupt JPEG as I received it from a customer:

Corrupt JPEG, invalid jpeg marker

Actual example I received from customer.

After loading the JPEG into JPG-Repair and clicking repair, it immediately becomes clear the actual image data is still present. The fact that it now shows is due to JPG-Repair modifying invalid JPEG markers in the image bit stream. JPG-Repair will indicate it has repaired invalid bytes by putting ‘bad bytes detected’ under the right lower corner if the image preview.

JPEG repair detected bad bytes. In this case an invalid JPEG marker which prevented the decoding of image data from a certain point on.

JPEG repair detected bad bytes. In this case an invalid JPEG marker which prevented the decoding of image data from a certain point on.

As you can see the image data is there however the colors are off and it shifts to the right. To shift the lower portion of the image to the left we need to remove bytes.

Check the ‘Remove bytes’ radio button > increase byte value > and click Apply. Increase the value to continue shifting the shifted portion in the right direction.

By removing bytes, the image starts shifting back (left)

By removing bytes, the image starts shifting back (left)

Keep going until we reach the point where image data is aligned. By experimenting with single byte adjustments try adjusting the color. In this example I tried 32, 48, 64, until I reached 112 which shifted bytes too far.

Using the slider next to the add/remove bytes option I set the number of bytes to jump to 1 so I can experiment with smaller increases/decreases. Going from 112 to 106 I reached a satisfactory result.

The JPEG image is now aligned

The JPEG image is now aligned

The image is automatically saved to the file you opened each time you click apply (registered version only).

Experiment with different values until satisfactory result is achieved

This image requires some more processing in Paint.NET. I used the the curves tool and clone stamping. This is the result of a quick and dirty edit (there is room for improvement). From the corrupt image to final result took about 2 minutes. Invest more time and you will achieve better results.

The procedure is also explained in this video:

Check my Youtube Channel for more examples.