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’.
|Get started quickly with these videos|
|Corrupt header (no thumbnail, will not open)||Corrupt RAW photo (CR2, NEF etc.)||Part of image grey, distorted area|
Describes the underlying method used by JPG-repair.
How to recognize different types of JPEG corruption.
How to repair corrupted JPEG headers. Corrupted headers result in different photo viewers and editor refusing to open the photo at all.
How to get use-able data from corrupted RAW photos such as Cr2 (Canon) and NEF (Nikon).
Editing RAW JPEG data with visual feedback to repair color errors and image shift and also grey blocks.
Unfortunately, often the question is now how a photo can be repaired, but if it can be repaired. JPG-Repair Toolkit offers some tools to diagnose corrupt JPEG photos.
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.
JPG-Repair is not a photo editor!
So, the UI of JPG-Repair is basically nothing else than an overlay for the raw hex data inside a JPEG file. This is where JPG-Repair is totally different from ordinary photo editors:
Normal editor: Open file > decompress and decode file > display decoded data > allow modification of decoded data > to save, encode and compress data. This process always includes loss of image quality due to encoding algorithms. Even if you do not change the image, information is lost by re-encoding.
JPG-repair: Open file > edit raw data > show preview of raw data > save raw data. If no changes are made by JPG-Repair, image data is exactly the way it was as before you opened the file. As no data is re-encoded, the process is lossless. Of course if you use JPG-Repair to remove say 180 corrupted bytes, the file that is saved is equal to the original file minus the 200 corrupted bytes.
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.
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.
There are three things you can do with the ‘patcher’ or the editor:
- Remove bytes
- Add or insert bytes
- Change the value of a byte
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. Removing bytes allows you to weed out corrupted data that is causing the image to distort. Using this method it is often possible to repair color errors (where colors are off after a certain point in the image) and correct shift.
Step 1 is always removing corrupt data. In case the image partially displays this is caused by invalid markers. These are bytes that happen to correspond with what the JPEG specification defines as markers. JPG-Repair repairs those automatically when you load an image and click repair. Often there is not just this one invalid marker, but it is surrounded by more corrupted bytes. And often these corrupt bytes tend to clutter. So we need to get rid of all those bad bytes.
Using JPEG-Repair you cane experiment with removing, adding and changing bytes while it provides you with visual feedback.
You are either adding, removing or changing bytes. In general you start removing bytes until colors above and below the corrupted area match. You then save the file, and re-open the last saved file to start adding bytes to correct shift. If this is to vague, I strongly suggest to watch the above videos (video on the right).
If you remove 200 bytes and click apply, JPG-Repair shows you the ‘original’ image with the 200 bytes removed. You decide to try 300 bytes and click apply. JPG-Repair now shows the original image with 300 bytes removed from a certain byte address (and not 500: it is not like you first removed 200 and then an additional 300). So, each time you click apply it is a new experiment.
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.