Sometimes when I save certain images from particular imageboards, I cannot preview or open those on Mac OS. They work fine on Windows, but I cannot even edit those on Mac OS using graphic editors. If you ever saw this infinitely loading preview, then you know what I am talking about:

If video doesn’t play in your browser, you can download it here.

So far my solution was to re-save the picture on Windows using GIMP or something else, but as I suspected it can actually be fixed on Mac OS as well - by editing the file in a HEX editor.

But I want to put a disclaimer right away: the solution I will be describing in the article is definitely not a universal one, and it won’t apply to all the possible cases of corrupted/broken JPGs. However, it does solve the issue in my specific case (the one described in the intro), so it’s good enough for me to publish it.

Okay, so we need a HEX editor. Here’s a nice one for Mac OS - Hex Fiend. By the way, if you look for it in the App Store, then beside that one you’ll find some paid alternatives which apparently are not really alternatives but complete clones of the original tool with the only difference of them being paid ones. Hilarious.

Anyway, that’s how a JPG file looks like there:

JPG file opened in Hex Fiend

And you guessed it right, the selected part is the one that causes troubles. Let’s investigate it.

If you don’t want (but why?) to read the full JPEG File Interchange Format specification, it is enough to rely just on Wikipedia and this page. Also, if you know russian, this article can be of help, although it’s a bit too heavy for our purpose.

So, what do we have here in the editor? First of all, if you don’t know that already, we are looking at binary data represented in hexadecimal form. Another thing you should know is that the picture contents do not start with the very first byte of the file, so the line I selected on the screenshot above is a part of a so-called JPEG header. And that’s where I found the source of the issue.

Here’s a detailed explanation of the header bytes:

JPG header in HEX

After reading those two article, I don’t see any problems here. But since the file does not open on Mac OS, obviously there are some.

In order to find the problem, I uploaded the file to this online JPEG image optimizer and tried to optimize it without compression:

Online JPEG image optimizer

Having downloaded the “optimized” file, I discovered that it opens on Mac OS just fine!

Okay then, let’s compare it with its broken original version in Hex Fiend:

HEX Fiend compare mode

From that comparison I discovered that the length byte in the “fixed” file is 0x10 (16) instead of 0xE (14), and also there are 2 null bytes in the end of APP0 section (which makes it exactly 16 bytes for the whole section).

JPEG specification states that these 2 bytes are responsible for storing the size of a thumbnail. And it is my guess that Mac OS is sensitive to this, so even if there is no thumbnail embedded into the file, these 2 bytes should be present anyway, despite the fact that they will simply store nulls.

So the correct header of the file should look like this:

JPG header in HEX

Let’s now try to “fix” the “broken” original by editing its header in HEX Fiend:

If video doesn’t play in your browser, you can download it here.

Niceru!

And I want to highlight again: this method only fixes this particular issue - the missing size bytes of an absent thumbnail. If in your case it is something else, then you can try to optimize your picture using the online tool I referenced earlier and then compare the fixed result with the original file. If that won’t help, then I’m afraid you’ll have to spend some time reading the full JPEG format specification (my condolences).

Music used in the video: Ozzed - Byrokrat Apparat.