decoding jpeg file format

I am trying to write a JPEG / JFIF encoder and decoder from scratch using C. I have experimented with writing a sample JPEG file, but it seems that I cannot open it using MS paint, Firefox. But I can decode it using JPEGsnoop ( http://www.impulseadventure.com/photo/jpeg-snoop.html?ver=1.5.2 ) and http://nothings.org/stb_image.c . I think the sample JPEG file conforms to the JPEG / JFIF standard, I donโ€™t know why applications like MS paint and Firefox cannot open it.

Here's what an example JPEG looks like:

     SOI
        APP0 segment
        DQT segment (contains two quantization tables)
        COM segment
        SOF0 segment
        DHT segment (contains four Huffman tables)
        SOS segment
        huffman encoded data
     Eoi

An example JPEG file contains three components of Y Cb Cr. No subsampling for the Cb Cr component. Two quantization tables are filled with one. The four huffman tables in the DHT segment are identical, it looks like

       [0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0 0]
       [0,1,2, ..., 254]

This means that all codes have 8 bits, so huffman encoding really does not compress the data.

Huffman encoded data is as follows:

        [0x0000 (DC) 0x0000 (AC)] (Y)  
        [0x0000 (DC) 0x0000 (AC)] (Cb) 
        [0x0000 (DC) 0x0000 (AC)] (Cr) for all (i, j) MCUs except (10, 10)

        the data in (10, 10) MCU: 
        [0x0008 (DC) 0x0000 (DC), 0x0000 (AC)] (Y)  
        [0x0000 (DC) 0x0000 (AC)] (Cb) 
        [0x0000 (DC) 0x0000 (AC)] (Cr)

Can someone tell me what is wrong with this sample jpeg file? Thank you

Here is a link to a sample JPEG file (ha.jpg) http://www.guoxiaoyong.net/ha.jpg

+9
c image file-format compression jpeg
source share
3 answers

I had a similar problem a few years ago with some PNG code (although I did not write it from scratch). It turns out my code was more standards compliant than Windows libraries, some browsers, etc. They coped well with typical cases, but were choked by unusual and far-fetched images, even if they were fully compliant with the standard. A common way to turn them off was to use an odd pixel width for the image. Almost half of my test suite was not viewable on Windows. (It was many versions back, like Windows 95. Windows codecs have improved significantly.)

I finished creating the open source PNG library and used it as my reference implementation. While the images created by my code can be analyzed by the reference implementation and vice versa, I called it good. I also verified that my code can display any image that Windows can display. Every time I found an error, I added the image to my test suite before fixing it. It was good enough for my project.

You can do the same. I believe there is an open source JPEG library that is widely used as a reference implementation.

If you really want to understand why Firefox (or something else) cannot open your image, you can try starting with the image that opens in Firefox. Gradually make small changes (for example, using the hex editor) to make it look more like an image that doesn't work. This can help you narrow down which aspect of your image disables the application. Admittedly, some of these steps can be difficult to try.

+4
source share

I think your file is very unconventionally encoded. I would advise you to find the reference file and try to mimic this structure. In addition, I would use example tables from the standard. Your Huffman data is full of zeros, bringing zero DC value, and then the end of the block.

If you look in jpegsnoop, your image will be in two shades, but it should be uniform. I assume that you do not have enough data to encode an image with the specified resolution. I believe that many decoders assume that this means that your file is corrupt.

0
source share

Firefox (and many other AFAIK applications) are based on the open source JPEG library from the independent JPEG group .

You can download the source for this, and then see why and when it doesnโ€™t like your file.

In addition, this will save you from reusing the wheel :-)

0
source share

All Articles