Tuesday, March 14, 2017

Differences In Image Formats

So I know a person who back in my Java 1 class thought it would be cool to show me a trick. Essentially the trick was (he thought) that he can convert an image from one format to another simply by changing its extension. Needless to say I wasn't impressed, in fact I got pretty angry. I told him "No you didn't convert the image, you only changed the extension". He continued to state that he did since it worked. Now just because the software you are using is smart enough to detect the actual format of an image or dumb enough to abandon standards to do that is irrelevant. In this post I will describe the basics of what makes an image format different from another.

Extension:
Now the extension is the first difference which everyone sees and is arguably the first difference to be noticed. It is simply text appended to the end of the filename. In terms of image formats you might have BMP, PNG, GIF, JPG, TIFF, WEBP, etc...
The extension though is only related to the file name, it is NOT related to the actual data of the file. The file name doesn't reside as data in the image, if you look at the file in a hex editor you will not find the filename. The filename and extension is for allocation in the file system used. If you open the allocation table of your drive you then will see it there. So changing the extension doesn't even change a single bit in the image file.

Header:
The header of an image format or any file format specification is actually in the data. Changing this might or might not affect the use of the file depending on where in the header you mess with. There are several parts in the header and each format has a different type and number of headers but typically the Magic/Signature is the first thing you will see.

Magic:
The Magic is the first few bytes which identifies what format the file is in and in some cases provide metadata. Parsing this will allow the ability to know the file's format but even more importantly its structure. Here you can see the Magic between the same image but one in JPG and one in PNG.



I'm not going to explain the entire header of these two formats but I will present them so that the difference can be seen.
JPG Header:

PNG Header:

Note: JPG has several different headers depending on options and type. You can see from the image I posted above that the JPG used here is of the EXIF type.

Now we can see that the structures of the image is completely different. A JPG decoder can not interpret a meaningful image if it tried to decode a PNG and vice versa.

Even if the headers were the same the inner workings of the two formats would be completely different which leads use to the encoding and decoding of the image data.

Encode/Decode:
JPG works by use of huffman tables and quantization tables. These tables are computed and is what gives the image the compression and lossy factors. This is also what allows different encoded images to be decoded the same way.

PNG on the other hand uses compressed chunks. Chunks are used so that PNG can be presented in a streamed manner but the image is encoded typically using a compression algorithm of the lossless kind. This gives per-pixel integrity at the cost of usually producing a larger file size (than JPG).