C/C++ does not call third-party libraries to generate BMP images

PointCloud-Slam-Image-Web3
C++ & Other
Published in
6 min readJul 19, 2024

--

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.

  1. file header
  2. File body (image color information)

So, if you want to write a BMP file in C, we have a total of 4 steps

  1. Calculate file length, request memory
  2. Generate file information header
  3. Write image color
  4. 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…

--

--

PointCloud-Slam-Image-Web3
C++ & Other

Familiar with point cloud data and image processing, interested in web3, take customization or consulting needs, enjoy work remotely, lonlonago@foxmail.com