用纯c/c++编写BMP图像没有其他库
在我的算法中,我需要创建信息输出。我需要将布尔矩阵写入 bmp 文件。 它必须是单色图像,如果该元素上的矩阵为真,则像素为白色。 主要问题是 bmp 标头以及如何编写它。
In my algorithm, I need to create an information output. I need to write a boolean matrix into a bmp file.
It must be a monocromic image, where pixels are white if the matrix on such element is true.
Main problem is the bmp header and how to write this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
C++ 答案,灵活的 API,但是假设小端系统。请注意,这使用 bmp 原生 y 轴(底部为 0)。
C++ answer, flexible API, assumes little-endian system however. Note this uses the bmp native y-axis (0 at the bottom).
单个函数中的简单 C++ 版本。这没有很多其他答案的缺陷,例如它是线程安全的,不会无意义地使用类或多个函数,防止整数溢出,使用模糊正确的类型,没有不必要的幻数等等
。不过,我对其进行了广泛的测试。
Simple C++ version in a single function. This doesn't have flaws of a lot of the other answers, e.g. it's thread safe, doesn't pointlessly use classes or multiple functions, prevents integer overflow, uses vaguely correct types, doesn't have unnecessarily magic numbers, etc.
Haven't tested it extensively though.
看看这是否适合你......
在这段代码中,我有 3 个二维数组,分别称为红色、绿色和蓝色。每个元素的大小都是[宽度][高度],每个元素对应一个像素 - 我希望这是有道理的!
See if this works for you...
In this code, I had 3 2-dimensional arrays, called red,green and blue. Each one was of size [width][height], and each element corresponded to a pixel - I hope this makes sense!
用于生成位图 (BMP) 图像的简洁 C 代码
此代码不使用除 stdio.h 之外的任何库。因此,它可以轻松地合并到 C 系列的其他语言中,例如 C++、C#、Java。
Clean C Code for Bitmap (BMP) Image Generation
This code does not use any library other than stdio.h. So, it can be easily incorporated in other languages of C-Family, like- C++, C#, Java.
不使用任何其他库,您可以查看 BMP 文件格式。我过去已经实现过它,不需要太多工作就可以完成。
...有关更多详细信息,请参阅文件格式
Without the use of any other library you can look at the BMP file format. I've implemented it in the past and it can be done without too much work.
... see the file format for more details
这是从复制的示例代码
https://en.wikipedia.org/wiki/User:Evercat/Buddhabrot。
this is a example code copied from
https://en.wikipedia.org/wiki/User:Evercat/Buddhabrot.c
请注意,线条是从下到上保存的,而不是相反。
此外,扫描线的字节长度必须是四的倍数,您应该在行末尾插入填充字节以确保这一点。
Note that the lines are saved from down to up and not the other way around.
Additionally, the scanlines must have a byte-length of multiples of four, you should insert fill bytes at the end of the lines to ensure this.
这是适合我的代码的 C++ 变体。请注意,我必须更改大小计算以考虑行填充。
Here is a C++ variant of the code that works for me. Note I had to change the size computation to account for the line padding.
我只是想分享 Minhas Kamal 代码的改进版本,因为尽管它对于大多数应用程序来说工作得足够好,但我仍然遇到了一些问题。要记住两件非常重要的事情:
下面,您可以看到我对其代码的修改:
I just wanted to share an improved version of Minhas Kamal's code because although it worked well enough for most applications, I had a few issues with it still. Two highly important things to remember:
Below, you can see my revisions to his code:
我编辑了 ralf 的 htp 代码,以便它可以编译(在 gcc 上,运行 ubuntu 16.04 lts)。这只是初始化变量的问题。
I edited ralf's htp code so that it would compile (on gcc, running ubuntu 16.04 lts). It was just a matter of initializing the variables.
最好的位图编码器不是您自己编写的。文件格式比人们想象的要复杂得多。事实证明,所有提出的答案都不会创建单色 (1bpp) 位图,而是写出 24bpp 文件,而这些文件碰巧只使用 2 种颜色。
以下是仅适用于 Windows 的解决方案,使用 Windows 映像组件。除了 Windows 附带的库之外,它不依赖于任何外部/第三方库。
与每个 C++ 程序一样,我们需要包含多个头文件。然后链接到Windowscodecs.lib:
接下来,我们声明我们的容器(一个向量,向量的向量!
bool
!),以及一些智能的方便的指针:一切都解决了之后,我们就可以直接开始实施了。需要进行一些设置才能获得工厂、编码器、框架并准备好一切:
此时一切都已设置完毕,我们有一个框架可以将数据转储到其中。对于1bpp文件,每个字节存储8个像素的信息。最左边的像素存储在 MSB 中,接下来的像素一直到最右边的像素存储在 LSB 中。
代码并不完全重要;无论如何,当您替换输入的数据布局时,您将用适合您需要的任何内容替换它:
剩下的是将更改提交到帧和编码器,这最终会将图像文件写入磁盘:
这是一个测试程序,将图像写入作为第一个命令行参数传递的文件:
它生成以下 64x64 图像(真正的 1bpp,4096 像素,大小 574 字节):
The best bitmap encoder is the one you do not write yourself. The file format is a lot more involved, than one might expect. This is evidenced by the fact, that all proposed answers do not create a monochrome (1bpp) bitmap, but rather write out 24bpp files, that happen to only use 2 colors.
The following is a Windows-only solution, using the Windows Imaging Component. It doesn't rely on any external/3rd party libraries, other than what ships with Windows.
Like every C++ program, we need to include several header files. And link to Windowscodecs.lib while we're at it:
Next up, we declare our container (a vector, of vectors! Of
bool
!), and a few smart pointers for convenience:With that all settled, we can jump right into the implementation. There's a bit of setup required to get a factory, an encoder, a frame, and get everything prepared:
At that point everything is set up, and we have a frame to dump our data into. For 1bpp files, every byte stores the information of 8 pixels. The left-most pixel is stored in the MSB, with pixels following all the way down to the right-most pixel stored in the LSB.
The code isn't entirely important; you'll be replacing that with whatever suits your needs, when you replace the data layout of your input anyway:
What's left is to commit the changes to the frame and the encoder, which will ultimately write the image file to disk:
This is a test program, writing out an image to a file passed as the first command-line argument:
It produces the following 64x64 image (true 1bpp, 4096 pixels, 574 bytes in size):
如果使用上述 C++ 函数在图像中间出现奇怪的颜色切换。确保以二进制模式打开输出:
imgFile.open(文件名, std::ios_base::out | std::ios_base::binary);
否则 Windows 会在文件中间插入不需要的字符! (在这个问题上我已经敲了几个小时)
请参阅此处的相关问题:为什么ofstream要在0x0A之前插入0x0D字节?
If you get strange colors switches in the middle of your image using the above C++ function. Be sure to open the outstream in binary mode:
imgFile.open(filename, std::ios_base::out | std::ios_base::binary);
Otherwise windows inserts unwanted characters in the middle of your file! (been banging my head on this issue for hours)
See related question here: Why does ofstream insert a 0x0D byte before 0x0A?
这是一个简单的 C++ bmp 图像文件类。
Here's a simple c++ bmp image file class.
此代码使用了一些较新的 C++ 功能。我用它来创建 8 位和 24 位 bmp 文件。它只写入 bmp 文件,有一天我们也可以读取它们!
我不喜欢所有的移位和容易出错的字节序安全性。
它可以使用更多注释,但代码非常简单。所谓的运行时字节序检测会导致代码在我(不久前)测试的所有编译器上被优化。
endian_type.h >>> Endian 安全 POD 类型。
以下包含 write_bmp 函数。
bmp_writer.h>> BMP writer header
和使用示例:
This code uses some newer C++ features. I've used it to create 8bit and 24bit bmp files. It only writes bmp files, one day we may read them too!
I didn't like all the shifting and error proneess for endian safety.
It could use lots more comments but the code is pretty straight forward. The supposedly run-time detection of endianness results in code being optimized away on all the compilers I tested (a while ago).
endian_type.h >> Endian safe POD type.
The following contains the write_bmp functions.
bmp_writer.h >> the BMP writer header
And an example of use: