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’.
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.
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.
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).
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
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.
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.
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.
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.
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.