Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...