I have an array of raw bytes and I want to make a bmp file from those bytes. That is, I have to fill bitmap header structre and other things, then write down the bytes so I have a bmp file in the proper format.
As I need this for some quick check only, I wonder if there is a portable method to do this - take raw bytes and save them as a bmp file. Any Windows version won't do as I'm writing that on Unix.
Alternatively, I can save those bytes as any other image format - I only need to have a quick look at the resulting picture.
The following will give you a .ppm image from a byte array. The
"P6"
specifies binary format 3 byte per pixel, but plain text is also supported and various forms of grayscale. The reason you should use this is that it is easy as can be and most *nix systems have a bunch of ppmto*-tools: ppmtobmp, ppmtojpeg, ..., ppmtopng... you name it.There is also ppmtocad... who would have guessed?
Boost GIL supports read/write to JPG, TIFF and PNG.
Being heavily template-based you can adapt your image format to the library. It may be an overkill for you tho.
This is the code I use for .bmp Greyscale images
To save as color bitmap, just make sure about not using the palette (for 24bits)
Try EasyBMP, it is open-source cross-platform C++ library and it is just fun to create BMP files with it:
Just take a look at the typedefs for
BITMAPFILEHEADER
andBITMAPINFOHEADER
from WinGDI, the fields are easy. Make sure you have 16-bit int for WORD and 32-bit int for DWORD. If you're doing RGBRGBRGB... it's super-easy to fill in the headers and write out the data...You could use SOIL, it's lightweight, portable, and although aimed for OpenGL it can load images (and also save images) and give you back the raw data.
Here's some example usage usage (from the SOIL site)
Here's the readable formats that SOIL has to offer:
EDIT: You can also use stb_image (which is also cross-platform and portable) if you prefer, which has all documentation and such in the single file.