Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a JPEG file decoder from scratch

Tags:

jpeg

I know there are many tools for this already. My goal is to learn. So I can read a JPEG file with fopen(), I know this is a binary file. Then what? I know that i can learn the JPEG specifications. But it doesn't seem to tell what is the structure of the binary jpeg file.

This file contains zeros and ones How can i transform this or how can i know which chain of bits means what ?

I've come across this example: nano jpeg decoder But it's pretty hard to read the code

Thanks in advance

PS: This princeton guy has done a project on this which provides a good reference

like image 679
McBear Holden Avatar asked Jul 29 '11 09:07

McBear Holden


People also ask

What is a JPEG decoder?

The JPEG decoder takes a JPEG image and converts it to a (uncompressed) bitmap using a decoding method called the baseline decoding process. A short introduction to the JPEG decoding process can be found in chapter 2 and appendix of the report "Design and implementation of a JPEG decoder" by Sander Stuijk.

How is a JPEG encoded?

The JPEG compression is a block based compression. The data reduction is done by the subsampling of the color information, the quantization of the DCT-coefficients and the Huffman-Coding (reorder and coding). The user can control the amount of image quality loss due to the data reduction by setting (or chose presets).

How do I make a JPEG file?

You can also right-click the file, point to the “Open With” menu, and then click the “Preview” option. In the Preview window, click the “File” menu and then click the “Export” command. In the window that pops up, select JPEG as the format and use the “Quality” slider to change the compression used to save the image.

Is JPEG An encoding?

JPEG, the group of people, created JPEG, a standard for digital image compression, in 1992. Anyone who's ever used the internet has probably seen a JPEG-encoded image. It is by far the most ubiquitous way of encoding, sending and storing images.


2 Answers

This page has a lot of info on how to process a jpeg file. Also, you can take a look at my own attempt at writing a jpeg decoder in Python.

The short variable names in the program often correspond directly to variables in the standard. So if you have the standard ready, it'll help a lot. It's called ITU-1150 and is freely available on the Internet.

like image 67
onemasse Avatar answered Jan 12 '23 05:01

onemasse


Jpegs are tricky if you're just starting. You need to work with huffmann tables, have some sort of fast inverse discrete cosine transform function, and the ability to interpret quantization tables.

http://en.wikipedia.org/wiki/JPEG is rather helpful.

If you want to start with something simpler, look at PNGs. The format is basically a header, followed by a bunch of variable length, chunks, and then a zlib stream. Decompressing that leaves you with almost-raw pixels, but they've been filtered. Unfiltering is easy.

like image 23
Dave Avatar answered Jan 12 '23 06:01

Dave