JPG-Repair Introduction

In this page I’ll try describe the methods available in JPG-Repair to repair JPEG photos. Repairing files with corrupt headers is easiest. It is a matter of placing the corrupt files + a sample file in one folder and clicking the repair button.

Automatic repair has it’s limits and that’s why JPG-Repair allows you to patch/edit the actual JPEG data to correct visual errors. Editing the JPEG data stream requires more skill, patience and a bit of luck.

Most repairs can be done by simply selecting the folder that contains the corrupt files (and if needed a reference or sample file) and clicking ‘Repair’.

Method

To a PC a file is nothing but a bunch of binary data. The most accessible form for humans to ‘read’ that data is in HEX code. Below you see a hex dump of the file we’ll use as an example on this page.

Our JPEG file in HEX code

When I started my attempts to repair corrupt JPEG files it was this hex editor I used. I edited, added or removed bytes. This is also called patching. I then used a photo viewer to watch the effects.

Essentially JPG-Repair uses the exact same methods I used back then. The difference is that it hides the HEX code and instead provides you with visual feedback when adding / removing bytes to / from the JPEG data stream.

JPG-Repair offers tools to alter the HEX code inside a JPEG file. This is different than what a normal photo editor does. A normal editor ‘decodes’ the JPEG file, then allows you to edit or change it (apply filters, crop, flip etc.) and then encodes the file again. You change the image on the canvas, and then that end result is saved. In effect you end up with a completely new file. It will completely change the hex code.

JPG-Repair on the other hand never decodes the file. After each change you make, it simply dumps that to the raw HEX code, and then only decodes so it can show you a preview of the altered file. The same as I did with the hex editor and then opening the file in a photo viewer to watch the effects. For the largest part the hex code of the file remains the same.

So, the UI of JPG-Repair is basically nothing else than an overlay for the raw hex data inside a JPEG file.

JPEG structures

JPG-repair allows to patch intact headers on files with corrupt headers. You can also edit the raw data that makes up the actual image. a JPEG file is made up of several sections. Each section is preceded by a JPEG Marker which defines what the following section is all about. Each marker defines the size of the section so that the next marker can be found. If any of those markers is corrupt, the file becomes corrupt. If the raw data that makes up the actual image (JPEG encoded data) becomes corrupt, your file may partially ‘render’: Part of the picture shows, while the rest is distorted or absent (Grey block, colored block, striped block).

A single wrong byte value can cause the rest of the image to turn to a solid grey or colored block

A single wrong byte value can cause the rest of the image to turn to a solid grey or colored block

JPG-Repair allows you to repair the section preceding the raw image data (sometimes called the header) using an intact JPEG file. It can also repair some errors in the raw JPEG data and allows you to patch (alter) this raw data.

Repairing photos with JPG-Repair Toolkit

JPEG repair offers a number of tools to diagnose, repair and extract images in and from corrupt JPEG and RAW photo files

Please do not let the different buttons and options intimidate you. For most simple repairs you only need the section of the left the window. In general JPEG corruption is one of two (or both): A corrupt header or bit corruption in the actual image data. Typically the first photo is what you get when the header is corrupt. In the second example, the image actually can be opened but ‘distorts’ at some point due to bit errors in the JPEG data.

Typical case of a corrupt header: Windows can not show a preview.

Typical case of a corrupt header: Windows can not show a preview.

Corrupt data in JPEG data. Windows may even show an intact preview, but main picture will look something like this

 

1 Repair corrupt JPEGs and RAW files

Note: technically RAW files are not repaired. All JPG-Repair does is scan the corrupt RAW photo for embedded JPEG data > validate that data > save the data to a JPEG file.

Most repairs can be done by simply selecting the folder that contains the corrupt files (and if needed a reference or sample file) and clicking ‘Repair’.

You need a sample file when repairing JPEGs with corrupted headers only. For corrupt RAW photos you do not need a sample. If the header of a JPEG is corrupt it is likely you will not be able to open it at all.A sample file is a JPEG shot with the same camera using the same settings. For JPG-Repair to recognize it it needs to be renamed to sample.jpg.

Steps to repair corrupt headers:

  1. Browse to folder containing the corrupt file(s) + sample if needed.
  2. Select the corrupt file(s). No need to actually select the sample.
  3. Only if you selected RAW files (NEF, CR2 etc.) you need to select a minimum resolution + tick option ‘assume header okay’.
  4. Click Repair.

For RAW files select a minimum resolution probably > 1.9 (MP) to skip lower resolution thumbnails and medium sized previews. Many RAW photo formats contain a full size JPEG. If this setting is set to a too low value, JPG-Repair may simply extract the medium sized preview. If too high it will skip every JPEG it comes across.

JPG-Repair will get to work and save files using the following format: OriginalName-1.JPG. For each file it is able to repair it will show a preview and certain details such as resolution below the preview.

If JPEGs are not repaired or JPG-Repair reports ‘an error occurred processing this image’, the image may be beyond repair. Refer to the paragraph in interpreting the byte histogram and entropy (3) to determine if a file can be repaired or not.

The free trial version will save only scaled down images with the BMP extension!

Repairing errors in JPEG data stream

The basic operations are removing data (bytes) and adding bytes. Removing bytes in general causes image data to shift left, adding bytes shift all data from the point we’re patching to the right.

Step 1 is always removing corrupt data. In case the image partially displays this is caused by invalid markers. JPG-Repair does this automatically when you load an image and click repair.

The way this works is that JPEG loads the raw data of a file into memory. It then tries to render this raw data and show what current state looks like. JPG-Repair writes the raw data + any modifications you make (add or remove bytes) to a copy of this modified data. Clicking the save button saves the current state including modifications.

Loading file: File > Array_1.

Patch 1, add 10 bytes at byte address 1000: (Array_1 + changes) > Array_2 > Render image from data in Array_2.

Patch 2, add 50 bytes at byte address 1000: Flush Array_2 > (Array_1 + changes) > Array_2 > Render image from data in Array_2.

So, modifications in Patch 1 are lost during Patch 2.

If you do not save, no changes are kept. Assume you remove 100 bytes from an x byte address. You then increase the number of bytes to remove to 200. This does not mean we now have removed 300 bytes! No, we experiment with removing 200 bytes rather than 100 and get visual feedback.

Or, we remove 100 bytes from byte address 100000. We then change the byte address to 100200. This not not mean we now removed 200 bytes; 100 from 100000 and 100 from 100200! It means we have removed 100 bytes from 100200.

It is often possible to repair corruption in actual JPEG image data by removing the offending bytes. We call this ‘patching’. If a corrupt byte violates the JPEG standard (invalid marker) JPG-Repair will remove that data automatically. No sample file is required.

Steps to repair / patch JPEG data bit stream:

  1. Browse to folder containing the corrupt file.
  2. Select the corrupt file.
  3. Tick option ‘assume header okay’.
  4. Click Repair.

Three things can happen now:

  1. JPG-Repair detects ‘bad bytes’, removes offending bytes and image looks good.
  2. JPG-Repair detects ‘bad bytes’, repair those but image or colors still ‘shift’. If so, up the value for ‘remove bytes’ and click apply. JPG-Repair will add/remove bytes from the bad byte location. Experiment with higher values and see if image improves. Bad data tends to clutter.

  3. No bad bytes but still a distorted image.

If the latter:

Time for a little adventure! Finding the right spot requires patience and some luck. Do not be afraid to try things, the original corrupt file is not altered in any way. You can not make anything worse.

Due to encoding and compression of image data,there is no direct correlation between any pixel in an JPEG image and a corresponding byte. So, main challenge is to find the byte(s) corresponding with the image corruption.

  1. Click on image in the area of corruption. Increase value for ‘remove bytes’ to 48 – 96 and click apply (v button). You have now introduced new corruption. 
  2. If new corruption is below the original corruption use the up key to travel up. Click apply. Repeat until your corruption is close to original corruption. The goal is to overlap the old with the new corruption. Use <<, >> and <, > to move closer.
  3. Once at the correct location start increasing value for ‘remove bytes’ until colors below and above corruption match as closely as possible.
  4. Use copy button to remember the byte address.
  5. Click save button.

Up/Down are large changes of byte address. << and >> medium and value can be adjusted using the slider below the buttons. < and > travel one byte only.

Use magnifier to zoom in on details:

What the start of the jpeg corruption typically looks like

What the start of the corruption typically looks like

 

 

 

 

 

 

To shift image back again:

Copy/Paste byte address

 

 

 

  1. Select the file you have just saved and click ‘repair’
  2. Paste byte address that you saved earlier.
  3. Select ‘add bytes’ and increase value.
  4. Click apply and view
  5. Repeat until image shifted back

Condition of a file

Not all JPEG files can be repaired. In fact I see that a lot unfortunately. JPG-Repair does several analysis on each file it processes that may help you determine why a file can not be repaired.

JPEG data has certain properties due to the fact that it is encoded and compressed. For example, all byte values are used/present in a JPEG, unlike for example a text document. Also, due to compression the file data has high entropy (chaotic, unpredictable).
Assume following uncompressed data: AAAAAABBAACAAAABCCCAAAA = 23 characters. Let’s try compress this using the simplest method I can come up with right now: 6A2B2AC4AB3C4A = 14 characters. Instead of writing out 6 A’s, we simply indicate by 6A that uncompressed data contains AAAAAA. Followed by 2 * B, 2 * A, C, 4 * A etc.. When we compare the compressed and the uncompressed data we see uncompressed data is less random and less predictable. Compressed data appears more random. Cutting corners here, but you could call that higher entropy. And this is sort of what we see in JPEG files as well.

JPG-Repair shows entropy and a byte histogram

Entropy in a JPEG file is somewhere around 7.6 – 7.9 bits per byte. A tad higher? Then the data is probably encrypted which is what I see a lot with JPEGs copied from Android devices such as smart-phones. In that case the file can not be repaired. The byte histogram shows you occurrence of bytes with specific values. Above shows a quite normal JPEG. Note that the zero value (blue bar on the left) is highest, this is common for JPEG files. Below are examples of files that are beyond repair:

 Only lower half of byte values used, probably other file format. So, this NOT mean only half the file is filled. There are simply only byte values found in range 0 – 128.

 File is filled with zeros, see blue bar on left. This file can of course not be repaired. Often caused by fake memory card.

 File is filled with a pattern, see bar at right, value 255 (equals FF in hex). Can be symptom of files being saved to a fake memory card.

 
It is also possible to simply inspect the file visually, click the right button under tools to open a hex viewer.
Only half the image because data is missing

Only half the image because data is missing. At some point if file there are only zeros.