jpeg文件格式解码
我正在尝试使用 C 从头开始编写 JPEG/JFIF 编码器和解码器。我尝试编写示例 JPEG 文件,但似乎无法使用 MS Paint、Firefox 打开它。但我可以使用 JPEGsnoop 对其进行解码( http://www.impulseadventure .com/photo/jpeg-snoop.html?ver=1.5.2)和 http://nothings.org/stb_image.c 。我认为示例 JPEG 文件符合 JPEG/JFIF 标准,我不知道为什么像 MS Paint 和 Firefox 这样的应用程序无法打开它。
示例 JPEG 如下所示:
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
示例 JPEG 文件具有三个分量 Y Cb Cr。 Cb Cr 分量没有二次采样。 两个量化表都填满了。 DHT段中的四个霍夫曼表都是相同的,看起来像这样
[0 0 0 0 0 0 0 255 0 0 0 0 0 0 0 0] [0,1,2, ... , 254]
这意味着所有代码都是8位,因此霍夫曼编码并没有真正压缩数据。
霍夫曼编码数据如下所示:
[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)
谁能告诉我这个示例 JPEG 文件有什么问题吗?谢谢。
以下是示例 JPEG 文件 (ha.jpg) 的链接 http://www.guoxiaoyong.net/ha .jpg
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
几年前,我在一些 PNG 代码上遇到了类似的问题(尽管我不是从头开始编写的)。事实证明,我的代码比 Windows、某些浏览器等的库更符合标准。它们在典型情况下表现良好,但在不寻常和人为的图像上表现不佳,即使它们完全符合标准。一种常见的解决方法是对图像使用奇数像素宽度。我的测试套件中几乎有一半在 Windows 中无法查看。 (这是许多版本之前的事情,比如 Windows 95。Windows 编解码器已经有了很大的改进。)
我最终构建了开源 PNG 库并将其用作我的参考实现。只要我的代码生成的图像可以被参考实现解析,反之亦然,我就认为它很好。我还检查了我的代码是否可以显示 Windows 可以显示的任何图像。每次我发现错误时,我都会在修复它之前将图像添加到我的测试套件中。这对于我的项目来说已经足够好了。
你也可以这样做。我相信有一个开源 JPEG 库被广泛用作参考实现。
如果您确实想弄清楚为什么 Firefox(或其他)无法打开您的图像,您可以尝试从在 Firefox 中打开的图像开始。逐步进行小的更改(例如,使用十六进制编辑器),使其更像失败的图像。这可能会帮助您缩小图像的哪些方面导致应用程序出错的范围。诚然,其中一些步骤可能很难尝试。
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.
Firefox(以及许多其他应用程序 AFAIK)基于 独立 JPEG 组 的开源 JPEG 库。
您可以下载其源代码,然后查看它不喜欢您的文件的确切原因和时间。
另外,这可以让你避免重新发明轮子:-)
Firefox, (and many other apps AFAIK) is based on the open-source JPEG library from the Independent JPEG group.
You could download the source for this, and then see exactly why and when it doesn't like your file.
Also, this would save you reinventing the wheel :-)
我认为你的文件编码非常非常规。我建议您找到一个参考文件并尝试模仿该结构。另外,我会使用标准中的示例表。您的霍夫曼数据充满了零,使得每个 DC 值都为零,然后是块结束。
如果您查看 jpegsnoop,您的图像有两种色调,但它应该是均匀的。我的猜测是您没有足够的数据来以您指定的分辨率对图像进行编码。我相信很多解码器会认为这意味着您的文件已损坏。
I think your file is very unconventionally coded. I would suggest that you find a reference file and try to mimic that structure. Also, I would use the sample tables from the standard. Your Huffman data is full of zeros making every DC-value zero, followed by and End-of-block.
If you look in jpegsnoop your image is in two shades but it should be homogeneous. My guess is that you haven't got enough data to code the image at the resolution you've specified. I believe a lot of decoders would assume that it means your file is corrupt.