简体   繁体   English

如何区分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?

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. 我找不到在线可用的bmp文件之间的任何区别,因此我可以轻松分辨出它们是24位还是32位。

I need to read a 32 bit bmp file into rgb array using C++ and most tutorials exist only for 32 bit. 我需要使用C ++将32位bmp文件读入rgb数组,大多数教程仅适用于32位。

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 . 位图文件的格式在MSDN上进行了描述:它以14个字节的文件头开头,后跟一个位图信息头 ,其中包含您要在字段biBitCount查找的信息。

Edit: 编辑:

As noted by iinspectable in the comments, the bitmap format can be complex. 正如iinspectable在评论中指出的那样,位图格式可能很复杂。 So with Windows, the best is to access to the information of the structures described above using the windows API. 因此,对于Windows,最好的方法是使用Windows API访问上述结构的信息。

If you're working cross platform, you'll have to take care yourself of many details: 如果您在跨平台上工作,则必须注意许多细节:

  • 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. 不同的文件格式版本 :实际上,您需要读取文件的偏移量14处的DWORD (32位无符号),以找出使用哪个版本的数据结构 The information you're looking fore is at offset 24 (core version) or 28 (other versions) of the file. 您要查找的信息位于文件的偏移量24(核心版本)或28(其他版本)处。 It's a WORD , so it's 16 bits unsigned. 这是一个WORD ,所以它是16位无符号的。

  • 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). 对于其他版本,它在以下DWORD指示(偏移量为30)。

  • all integers are stored in little endian. 所有整数都存储在little endian中。

But instead of doing all this by yourself, you could as well consider CImg or another library. 但是,除了自己完成所有这些工作之外,您还可以考虑使用CImg或其他库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM