Fix Corrupted JPEGs Made by the Samsung Galaxy S2 on Windows/Mac!
When I was organizing some of the pictures which I took with my Samsung Galaxy S2, I came across one file which failed to open. And then I investigated upon this issue and came to know that sometimes Samsung Galaxy S2 creates corrupt JPEGs.
Problem
The Standard (linux) picture viewer application with tell you that It fails to open the file. Even you convert the images it tells you the same thing:
$ convert testimage.jpg out.jpgconvert: Corrupt JPEG data: 1072 extraneous bytes before marker 0xd8 `test.jpg’ @ warning/jpeg.c/EmitMessage/231.convert: Invalid JPEG file structure: two SOI markers `testimage.jpg’ @ error/jpeg.c/EmitMessage/236.convert: missing an image filename `out.jpg’ @ error/convert.c/ConvertImageCommand/3011.
Here the valuable information is:
1072 extraneous bytes before marker 0xd8 and the two SOI markers
EXIF standard
After searching for this error message I came to know about the hex editor. My corrupt file stats was
FF D8 FF E1 00 0E 45 78 69 66 00 00 49 49 2A 00 …
Here you can see that the JPEG file FF D8 is followed by some EXIF information (FF E1). With the use of EXIF specification (PDF) the start of the application segment 1 (APP1):.
Offset (Hex) | Name | Code (Hex) |
0000 | SOI (Start Of Image) Marker | FFD8 |
0002 | APP1 Marker | FFE1 |
0004 | APP1 Length | xxxx |
0006 | Identifier | 4578 6966 00 (“Exif”00) |
000B | Pad | 00 |
000C | APP1 Body |
FFD8 is thus the SOI marker and the error message says that the file contains two of them which are bad. After searching I got FFD8 another occurrence at 0x442 = 1090. It is also said that it has 1072 extraneous bytes before marker 0xd8 which is a bit smaller that APP1 header and next to SOI marker. So what has happen is the SOI wrong.
Valid JPEG file
To see what exactly was wrong I opened another JPEG which worked and it was only taken few minutes before the corrupt file. Comparing them by fast switching between the console tabs you may notice two things.
- The FFD8 can be found on the same position on both the files, which is not a problem.
- first difference is of APP1 length.
- This difference is a bit huge!
Look for:
Corrupt file:
FF D8 FF E1 00 0E 45 78 69 66 00 00 49 49 2A 00 …
Valid file:
FF D8 FF E1 E0 42 45 78 69 66 00 00 49 49 2A 00 …
The length of the APP1 segment on the corrupt file is only 0xE = 14? This is very small.
If you increase the length of the corrupted file randomly then you will see the error message will give you, but it will be more like hypotheses.
Let’s see what is at the end of APP1 segment in the valid file:
0xE042: FD CF FF D9 FF DB 00 84
0xE044 which is more than 0xE042 the SOI marker which is before the APP1 segment says FFD9 and the EXIF specification will tell about the EOI (End Of Image) marker followed by FFDB which is DQT (Define Quantization Table) marker. Everything is there where it should be.
Overflow
Now about the corrupt file, when I searched for FFD9FFDB and found that it was at 0x10010. Were you able to see it earlier? For this minus the two bytes for the SOI marker the length of the APP1 segment would be 0x1000E which will not able to get stored at the two bytes. Thus the lower part of 0x000E can be stored on the APP1 segment header. An example of an integer overflow is in huge amount.
But the EXIF specification is quite clear:
The APP1 consists of APP1 marker, Exif identifier code, and the information of the attribute. The size of the APP1 includes all the elements that shall exceed the 64 Kbytes specification in JPEG standard.
Solution
The APP1 segment contains of thumbnail at the end. After recalculating and storing properly by image processing applications, thus you need to try shorting the data there under 64 Kbytes. After removing the 20 bytes directly before the FFD9FFDB it gave rise to new APP1 segment length of 0x1000E – 0x14 = 0xFFFA, which can be stored in the new length at 0x0004.
It will work. The JPEG can now be opened again without any error. You will even get any error of thumbnails.
Apart from going through such a massive method you can take help of third party repair tool. JPEG File Repair Tool for Windows/Mac helps to repair header and rebuild the internal structure of many types of files. After fixing the inaccessibility error it helps you in getting access to those files and let you to view, edit the images efficiently. It repairs all the corrupt *.JPEG, *.JPG files in lossless mode. This repair is performed at the block level without re-encoding the files, thus it helps to retain the original JPEG quality. Both the main and thumbnail images are rebuilt and EXIF information is preserved.
How to use JPEG File Repair Tool:
Step 1. The main interface of JPEG File Repair Tool provides “Extract Thumbnail” and “Repair JPEG” choices. To extract only the thumbnails of the JPEG files use “Extract Thumbnail” option whereas to repair your JPEG files use “Repair JPEG”. In order to proceed, click on the appropriate button.
Step 2. Then you have to add the files which are to be repaired by clicking on the “Add File” button, you can also add multiple files. From the left panel select the file and click on “Remove from the list” to remove the files from the “list of JPEG files” which images are perfect and in order to repair all the files click on the button “Select All” this would allow you to select all the added JPEG files and by selecting “Unselect All” would un-select all the listed files. Then to initialize the repairing process, click on the “Repair File”.
Step 3. After scanning and repairing the corrupted or lost JPEG files. Then on the left pane it would display all the repaired files. You can see the list of JPEG images in thumbnails, if you have selected the “repair JPEG files”. In order to see the preview in the main interface window you have to select the file from the left pane. After seeing the preview you can click on the “Save” button to save the files on the system.
Step 4. On choosing the “Extract the thumbnails” option from the main interface, you can add the JPEG files by selecting the “Add File” button. Now you can extract the desired files from the thumbnails of the JPEG files.
Step 5. In order to extract the thumbnails of the JPEG images then this software would only display the thumbnail image in order to preview the file. Then click “Save” button to save the thumbnail images of JPEG files to your desired location.
Step 6. After saving the repaired file, it would display a pop-up message as shown above. You can go to the container folder by clicking on it.
579 Total Views 1 Views Today