← Back to the index page

Monochrome 1-bit image parsing

When I tried to use Arduino to make a simple game for TV Sayzo. There was need to parse 1-bit data from bitmap (*.bmp) image to bytes for input to C.

I haven’t found anything suitable on the web that time (2014?). I made decision to write my own utility, which recently I have upgraded code of it.

BMP format

Actually, the BMP format is very simple; it consists of 2 parts: a header with metadata and actual bytes without any compression.

BytesValue/Type
2 bytes0x4D42BMP format identifier
4 bytesuint32_tFile size in bytes
2 bytesint16_tnot in use/reserved
2 bytesint16_tnot in use/reserved
4 bytesuint32_tOffset to image data in bytes from beginning of file
4 bytesuint32_tDIB Header size in bytes
4 bytesint32_tWidth of the image
4 bytesint32_tHeight of the image
2 bytesuint16_tNumber of color planes
2 bytesuint16_tBits per pixel
4 bytesuint32_tCompression type
4 bytesuint32_tImage size in bytes
4 bytesint32_tPixels per meter (X)
4 bytesint32_tPixels per meter (Y)
4 bytesuint32_tNumber of colors
4 bytesuint32_tImportant colors

Data

After the header data, follow plain data in bytes as unit8_t or unsigned char. This makes parsing extremely easy.

Check bmp2bytes to parse the 1-bit images and get image data and header.

Example

Girl 1-bit

bmp2bytes -f "0x%x" -d "," -n ./tests/girl.bmp
Example of output
0xff,0xfe,0x0,0x0,0x5f,0xff,0xff,0x6a,0x80,0x0,0x0,0x7f,0xfa,0xaa,0xad,0xdd,0xfd,0x0,0x0,0x35,0xbb,0xb5,0xb5,0x0,0x0,0x0,0x7f,0x7f,0x55,0x55,0xff,0xef,0x0,0x0,0x3f,0xdd,0xff,0x5a,0x60,0x0,0x0,0x3b,0xea,0xd6,0xd6,0x7f,0x7e,0x0,0x0,0x55,0xff,0xad,0xef,0x80,0x0,0x0,0x3f,0xfd,0x52,0x5b,0xdb,0xfd,0x0,0x0,0x7e,0xdd,0xf6,0xb2,0xe0,0x0,0x0,0x3b,0xfe,0xbb,0x4d,0xff,0xee,0x0,0x0,0xb7,0xff,0xbf,0xdf,0x10,0x0,0x0,0x7e,0xdd,0x4d,0xb6,0xef,0xfc,0x0,0x0,0x5f,0x6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,0x56,0xab,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x5,0x28,0xfc,0xe8,0x0,0x0,0xb9,0x6e,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x2,0xcb,0xfe,0x78,0x0,0x0,0xd,0x32,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x64,0xff,0x78,0x0,0x0,0x2a,0xae,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x5,0x35,0xff,0x30,0x0,0x0,0x1,0x59,0x0,0x0,0x0,0x0,0x0,0x0,0x1,0x40,0x4a,0xff,0xb8,0x0,0x0,0x14,0xa6,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x11,0xff,0xdc,0x0,0x0,0x2,0x29,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x40,0x8c,0xff,0xdc,0x0,0x0,0x1,0x14,0x80,0x0,0x0,0x0,0x0,0x0,0x0,0x10,0x2,0xff,0x9a,0x0,0x0,0x0,0x8a,0x0,0x0,0x40,0x0,0x0,0x0,0x0,0x0,0x48,0xff,0xc6,0x0,0x0,0x2,0x28,0x0,0x0,0x80,0x0,0x0,0x0,0x0,0x10,0x2,0xff,0xf1,0x0,0x0,0x0,0x6,0x80,0x0,0xc0,0x0,0x0,0x0,0x0,0x0,0x0

Creating 1-bit bitmaps

GIMP

  1. Toolbar > Image > Mode > Index
  2. File > Export as…
  3. Save file with extension bmp.

ImageMagick

ImageMagick has an enormous amount of image processing features. It can covert any file to a 1-bit bitmap with a single command:

ImageMagick <7

magick convert -monochrome image.png 1bit.bmp

ImageMagick 7+

magick image.png -monochrome 1bit.bmp

MS Paint and lots of other image editors

Process of creating monochrome images in the most of the image editors is the same. Just set the image to an indexed palette with 2 colors black and white and save.

Feedback

For feedback, please check the contacts section. Before writing, please specify where you came from and who you are. Sometimes spammers go insane. Thank you in advance for your understanding.

← Back to the index page