How to differentiate between a 32bit and 24bit bmp file? Also how do I read a 32bit bmp file into a C++ array?

青春壹個敷衍的年華 提交于 2019-12-13 06:48:14

问题


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.

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


回答1:


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.

Edit:

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:

  • 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.

  • 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).

  • all integers are stored in little endian.

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



来源:https://stackoverflow.com/questions/38545995/how-to-differentiate-between-a-32bit-and-24bit-bmp-file-also-how-do-i-read-a-32

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!