C/C++ does not call third-party libraries to generate BMP images
Hello everyone, there is a requirement to write BMP images in C or C++.
But this low-level code, for the convenience of porting, we all use C
The BMP file consists of two parts.
- file header
- File body (image color information)
So, if you want to write a BMP file in C, we have a total of 4 steps
- Calculate file length, request memory
- Generate file information header
- Write image color
- Write the entire memory space to a file in binary
The other parts are very simple, the key is this second part, we need to understand the second step before we can complete the other steps
So let’s first look at the file information header [1] of BMP, which consists of the following parts in total.
Note that these data add up to a total of 54 Bytes, not 8 Multiples of bytes, therefore, for convenient byte alignment.
Instead of typedef struct
, we dereference char*
directly and write data to a fixed byte.
Let’s first define our interface declaration.
static inline void writeBMP(const char* fileName,const…