如何区分32bit和24bit bmp文件?另外,如何将32位bmp文件读入C ++数组? [英] How to differentiate between a 32bit and 24bit bmp file? Also how do I read a 32bit bmp file into a C++ array?

查看:207
本文介绍了如何区分32bit和24bit bmp文件?另外,如何将32位bmp文件读入C ++数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法找到在线上可用的bmp文件之间的任何区别,因此我可以轻松分辨出它们是24位还是32位.

I wasn't able to find any difference between bmp files available online, so that I could easily tell whether they were 24 or 32 bit.

我需要使用C ++将32位bmp文件读取到rgb数组中,并且大多数教程仅适用于32位.

I need to read a 32 bit bmp file into rgb array using C++ and most tutorials exist only for 32 bit.

推荐答案

位图信息标题,其中包含您在字段biBitCount.

The format of bitmap files is described here on MSDN: it starts with a file header of 14 bytes, followed by a bitmap info header, which contains the information you're looking for in the field biBitCount.

iinspectable在注释中指出,位图格式可能很复杂.因此,对于Windows,最好的方法是使用Windows API访问上述结构的信息.

As noted by iinspectable in the comments, the bitmap format can be complex. So with Windows, the best is to access to the information of the structures described above using the windows API.

如果您正在跨平台工作,则必须注意许多细节:

If you're working cross platform, you'll have to take care yourself of many details:

  • 不同的文件格式版本:实际上,您需要读取文件的偏移量14处的DWORD(32位无符号),以找出 WORD ,因此它是16位无符号的.

  • the different file format versions: In fact you need to read the DWORD (32 bits unsigned) at offset 14 of the file to find out which version of the data structure is used. The information you're looking fore is at offset 24 (core version) or 28 (other versions) of the file. It's a WORD, so it's 16 bits unsigned.

可以压缩文件格式.核心版本不是这种情况.对于其他版本,在以下DWORD中指示(偏移量为30).

the file format could be compressed. This is not the case for the core version. For the other versions, it's indicated in the following DWORD (at offset 30).

所有整数都存储在little endian中.

all integers are stored in little endian.

但是,除了自己完成所有操作外,您还可以考虑 CImg 或其他库.

But instead of doing all this by yourself, you could as well consider CImg or another library.

这篇关于如何区分32bit和24bit bmp文件?另外,如何将32位bmp文件读入C ++数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆