使用 zlib 解压缩 PNG
如何使用 zlib 库解压缩 PNG 文件?我需要在 gcc 编译器下使用 C 读取 PNG 文件。
How can I use zlib library to decompress a PNG file? I need to read a PNG file using a C under gcc compiler.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
为什么不使用 libpng ? PNG 文件格式相当简单,但有许多不同的可能变化和编码方法,并且确保涵盖所有情况可能相当乏味。像 libpng 这样的东西会自动为您处理所有转换和其他内容。
Why not use libpng? The PNG file format is fairly simple, but there are many different possible variations and encoding methods and it can be fairly tedious to ensure you cover all of the cases. Something like libpng handles all the conversion and stuff for you automatically.
我曾经编写过一个用于读取/写入 PNG 文件的基本 Java 库: http://code.google .com/p/pngj/
它不支持调色板图像,但除此之外[更新:它现在支持所有 PNG 变体]它相当完整、简单,并且代码没有外部依赖项(即它仅使用标准 JSE API,其中包括 zip 解压缩)。并且代码可用。我想你可以毫不费力地将它移植到 C 语言。I've code once a basic Java library for reading/writing PNG files: http://code.google.com/p/pngj/
It does not support palleted images but apart from that[Updated: it supports all PNG variants now] it's fairly complete, simple and the code has no external dependencies (i.e. it only uses the standard JSE API, which includes zip decompression). And the code is available. I guess you could port it to C with not much effort.如果这是一项家庭作业,并且您确实仅限于标准 C 库,那么您应该查看官方 PNG 文件格式规范:http://www.w3.org/TR/PNG/。但是,您确定确实需要解码 PNG 文件吗?如果您所需要做的只是以某种方式显示它,那么您就走错了路。
为任何一般的 PNG 文件编写解码器都相当复杂且耗时,但对于简单的文件来说还不错。事实上,因为 PNG 格式允许压缩它的各个部分,所以仅使用标准 C 库来完成它需要您实现 gzip 解压缩(对于中级本科课程来说这是一个合理的家庭作业,但我的猜测是您在分配给您之前,您会花费大量时间讨论压缩算法)
但是,如果您将自己限制在非压缩、非隔行扫描的 PNG 文件上,这并不是非常困难。我曾经用 Python 编写过一个解码器,只在几个小时内处理了简单的情况,所以我确信它在 C 中也是可行的。
If this is a homework assignment and you really are only restricted to the standard C library, you to be looking at the official PNG file format specification: http://www.w3.org/TR/PNG/. However, are you sure you really need to be decoding the PNG file? If all you need to do is display it somehow, you're headed on the wrong path.
It will be rather complex and time consuming to write a decoder for any general PNG file, but not too bad for simple ones. In fact, because the PNG format allows for pieces of it to be compressed, to do it with only standard C libraries would require you to implement gzip decompress (a reasonable homework assignment for a mid-level undergrad course, but my guess is that you would have spent a lot of discussing compression algoirthms before this was assigned to you)
However, it isn't terribly difficult if you restrict yourself to non-compressed, non-interlaced PNG files. I wrote a decoder once in Python that handled only the easy cases in a couple of hours, so I'm sure it'll be doable in C.
您可能应该阅读二进制文件格式的工作原理,并使用十六进制编辑器而不是文本编辑器来查看文件。一般来说,您应该使用 libpng 来处理 png 文件,如前所述,但如果您想自己解码它,您需要阅读大量内容。
我建议阅读此 http://www.libpng.org/pub/png/书/chapter13.html
You should probably read up on how a binary file-format works and use a hex-editor instead of a text-editor to look at the files. Generally you should use libpng to handle png-files as stated earlier but if you want to decode it yourself you have alot of reading to do.
I recommend reading this http://www.libpng.org/pub/png/book/chapter13.html