Skip to content

Simple and Independent C Library for loading Windows Bitmap format data

Notifications You must be signed in to change notification settings

yyasuda/BMPLoad

Repository files navigation

[ Languages: English, 日本語 ]

BMPLoad

A simple and independent C Library for loading Windows Bitmap formatted data.

What is this?

If you need RGB / RGBA type image data as array of char on your C program, this will help you. BMPLoad loads Microsoft Windows Bitmap format data, then extracts it's image part to RGB / RGBA char array.

The functionality is represented as BmpLoad( ). See main( ) in main.c to see how does it works.

Usage

unsigned char *Data;
int width, height, bits;
Data = BmpLoad("sample.bmp", &width, &height, &bits);
.......
free(Data);

BmpLoad( ) open the specified file then get the attribute information to . width and height represent the size of image. bits means number of bits of single pixel, should be 24 or 32. This function also allocates memory and store the image body as RGB or RGBA char array. You can get the array address as the return value. NULL will be returned when severe error occur.

Test Run

Here is the typical output of main.c program. It loads 6x6_24.bmp and dump the contents in hexdec form. The image is basically white, except a RED (0xff0000) dot on the top-left corner and a YELLOW (0xffff00) dot on the bottom-right.

$ cc BMPLoad.c main.c
$ ./a.out 6x6_24.bmp 
filename = 6x6_24.bmp
width = 6, height = 6
planes = 1
bits/pixel = 24
Data 0x7fc312500000 (108 bytes allocated)
dump (6 x 6) format="RGB"
000 ff0000 ffffff ffffff ffffff ffffff ffffff
001 ffffff ffffff ffffff ffffff ffffff ffffff
002 ffffff ffffff ffffff ffffff ffffff ffffff
003 ffffff ffffff ffffff ffffff ffffff ffffff
004 ffffff ffffff ffffff ffffff ffffff ffffff
005 ffffff ffffff ffffff ffffff ffffff ffff00
PROGRAM normarly ended.
$

You can try other included images. 15x15_24_dots.bmp is 24bit image. 15x15_32_dots_alpha.bmp is 32bit (8bit RGB colors + 8bit Alpha) image.

NOTE : negative value of the height

If you see the negative value as height, the image was scanned from top to down. Usually, Bitmap image scanned from bottom to up. BmpLoad( ) function always stores the image to top-down ordering on the array.

For more detail of this issue, see biHeight specification.

Limitation

BMPLoad can recognize 24 or 32bit color format as basic Windows Bitmap data only. Under technical term, the code only recognizes BITMAPINFOHEADER with full color (does not work with neither compressed format nor palette-color technique). For more detail, see following info; https://wikipedia.org/wiki/Windows_bitmap#BITMAPINFOHEADER

Usually case, this conditions are fulfilled on your Bitmap file.

Why did I wrote this?

When I encouraged students to develop more advanced program in their programming class, I was realized it was a bit difficult to load the bitmap RGB data from usual image data file such as PNG or BMP. Actually, one of my students tried to use OpenGL and tried to handle the bitmap image not only the wire-frame. OpenGL is quite general and popular library, but it does not include any standard way to load the bitmap image. (And it is natual.) On the other side, there are many tools to load the graphics image in various formats such as libpng. But it is too complicated to understand what they are doing, at least for the student in the beginners' class.

Thus I started to write this code to do that task in simple, easy to understand and independent manner. BMPLoad library is written in C. It runs on just general C environment. And it does not rely any other library.

NOTE : for Macintosh users

I am Macinotsh user, so here are some tips for Mac users.

How can i create BMP file?

'sips' command on MacOSX will help you to convert to BMP format from PNG file as follows;

$ sips -s format bmp example.png --out example.bmp

Preview app does not handle Alpha value (transparency)

Could not recognise 15x15_32_dots_alpha.bmp file as 'Alpha' image? So, Preview app of MacOSX does not handle Alpha, at least on my Yosemite (10.10) enviroment. Use the other image utilities to see the Alpha image correctly. I use GraphicConverter app on my Mac.

References

Copyright disclaimer

I do not claim any Copyright about all codes under this BMPLoad repository.

These codes are licensed under CC0.

CC0

Yutaka Yasuda

About

Simple and Independent C Library for loading Windows Bitmap format data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages