加载位图的 C 代码
有谁知道一个好的 C 示例可以加载位图并处理所有情况:rle、黑白位图等?
代码应该是跨平台的。
谢谢。
Does anybody know a good C sample that loads bitmaps and handles all the cases: rle, b/w bitmaps, so on?
Code should be cross-platform.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议使用像 SDL image 这样的库
I would suggest using a library like SDL image
如果您正在寻找最小的 BMP 加载器,此链接将为您提供有关 BMP 格式、数据结构和示例代码所需的所有信息,无需加载任何库依赖项:
http://paulbourke.net/dataformats/bmp/。
它还包含在 open gl 纹理中查看加载的 BMP 的代码,所以几乎满足您的所有需求...
If you are looking for a minimal bmp loader this link will give you all you need to know about the BMP format, data structures and sample code without any library dependency to load:
http://paulbourke.net/dataformats/bmp/.
It also contains code to see the loaded BMP in a open gl texture, so pretty much all you need...
Chris Backhouse 制作了一个实用的小型 BMP 加载器(着眼于将它们用作 OpenGL 纹理)。它是 C++,而不是 C,他承认它不是跨平台的。然而,它很小而且很容易理解,所以我想我应该在这里添加链接:
http://users.ox.ac.uk/~orie1330/bmploader.html
Chris Backhouse made a functional little BMP loader (with an eye to using them as OpenGL textures). It's C++, not C, and he admits it's not cross platform. However, it's small and easy to understand, so I thought I'd add the link here:
http://users.ox.ac.uk/~orie1330/bmploader.html
您需要一些外部库来执行此操作(我推荐 ImageMagick)。 ImageMagick 网站还包括文档和示例。
You need some external library to do this (I recommend ImageMagick). The ImageMagick web site also includes documentation and examples.
查看由 Intel 开发的 OpenCV 库。
Check out for OpenCV Library developed by Intel .
如果您使用的是 BMP 文件格式,那么您可以自己查看标题并获取像素,这非常简单。 查看此 Google 搜索。更有趣的匹配之一是此处。最违反直觉的部分是每行像素都是 4 字节对齐的。另外,请注意压缩的 BMP...(我的经验是,许多第三方工具在处理压缩的 BMP 时遇到问题,所以也许您遇到的某些库也会...)
如果您不依赖于 BMP 文件格式,我推荐libpng。 手册提供了一些非常清晰的示例代码。
If you are tied to the BMP file format, it's pretty simple to look at the header yourself and get the pixels. See this google search. One of the more interesting matches is here. The most counter-intuitive part is that every line of pixels is 4-byte aligned. Also, watch out for compressed BMPs... (My experience is that many third-party tools have trouble with compressed BMPs, so maybe some libraries you encounter will also..)
If you aren't tied to the BMP file format, I recommend libpng. The manual provides some sample code which is pretty clear.
正如其他人所建议的,您可能想要使用 SDL 等外部库。如果您想学习一些东西并自己做,请参阅我对这个非常相似的问题的回答:从 24bpp 位图获取每个像素的 RGB 值,以便在 C 中转换为 GBA 格式,您可以在其中找到打印每个像素的 C 代码,并查看有关 bmp 文件的维基百科页面,因为它非常好。
As others suggested you might want to use an external library like SDL. If you want to learn something and do it yourself, see my answer to this very similar question: Getting RGB values for each pixel from a 24bpp Bitmap for conversion to GBA format in C where you'll find C code which prints out each pixel, and have a look at the wikipedia page about bmp files, because it's very good.