问题
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