Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jpeg file format decoding

I'm trying to write a JPEG/JFIF encoder and decoder from scratch using C. I experimented 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 complies the JPEG/JFIF standard, I don't know why applications like MS paint and Firefox cannot open it.

Here is how the sample 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

The sample JPEG file has three component Y Cb Cr. No subsampling for Cb Cr component. The two quantization tables are all filled with ones. The Four huffman tables in DHT segment are all identical, it looks like this


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

That means all the codes are 8bits, so huffman encoding does not really compress data.

The huffman encoded data look like this:


       [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 anyone tell me what is wrong with this sample JPEG file? thanks.

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

like image 823
PeopleMoutainPeopleSea Avatar asked Jul 21 '11 13:07

PeopleMoutainPeopleSea


1 Answers

I had a similar problem years ago with some PNG code (though I didn't write it from scratch). It turns out my code was more standards compliant than the libraries by Windows, some browsers, etc. They did fine on typical cases, but choked on unusual and contrived images, even if they were completely in line with the standard. A common way to trip them up was to use an odd pixel width for the image. Almost half of my test suite was not viewable with Windows. (This was many versions ago, like Windows 95. The Windows codecs have improved substantially.)

I ended up building the open source PNG library and using it as my reference implementation. As long as the images that my code produced could be parsed by the reference implementation and vice versa, I called it good. I also checked that my code could display any image that Windows could display. Every time I found a bug, I added the image to my test suite before I fixed it. That was good enough for my project.

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

If you really want to figure out why Firefox (or whatever) cannot open your image, you could try starting with an image that does open in Firefox. Incrementally make small changes (e.g, with a hex editor) to make it more like the image that fails. That might help you narrow down what aspect of your image is tripping up the application. Admittedly, some of those steps may be hard to try.

like image 67
Adrian McCarthy Avatar answered Sep 30 '22 23:09

Adrian McCarthy