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.
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
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!
Clean C Code for Bitmap (BMP) Image Generation
Generated Image:
The code does not use any library other than stdio.h. So, the code can be easily incorporated in other languages of C-Family, like- C++, C#, Java.
this is the best low level example i know, written by Evercat. 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.
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.
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.
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?
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:
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 image (true 1bpp, 574 bytes in size):
I just made this for fun today. This uses the C standard library. This renders a 24-bit image of color red, green, and blue, from bottom to top, respectively.
I posted it in gist if you would like to see: https://gist.github.com/harieamjari/b1991624866556ab3e6baaf1ae79a313