Need to load a bmp and read pixel rgba values

Started by
4 comments, last by b2b3 19 years ago
I'm trying to find a library or sample code that loads a bmp, and lets me read pixel values with a simple function call. Are there any libraries or example code that does this? I've been looking at OpenIL/DevIl, but I can't find a function that lets me read a single pixel. Thanks. -Nick
Advertisement
GDI will, you will have a hard time reading an alaph channel from a bmp however:) MFC has the CBitmap class that will allow you to get a pointer to the raw bits as well.

Cheers
Chris
CheersChris
Any method that's not only for Windows?
Intel has a freely available image library. I don't remember teh name. There must be tones of code out there.

Hmm how about googleing loading bitmaps source code:)

You need to do some work on your own;)

CHeers
Chris
CheersChris

FILE * f = fopen("filename", "rb");BITMAPFILEHEADER fh;BITMAPINFOHEADER ih;fread(&fh, sizeof(BITMAPFILEHEADER), 1, f);fread(&ih, sizeof(BITMAPINFOHEADER), 1, f);unsigned char * pixels = (unsigned char *)malloc(ih.biWidth * ih.biHeight * ih.biBitCount / 8);unsigned int linewidth = ih.biWidth * ih.biBitCount / 8;unsigned int padwidth  = ((((ih.biWidth * ih.biBitCount / 8) >> 2) + 1) << 2) - linewidth;unsigned int fullwidth = linewidth + padwidth;for (int c = 0; c < ih.biHeight; c ++){		fread(&(pixels + (c * fullwidth)), sizeof(unsigned char), linewidth, f);	fseek(f, padwidth, SEEK_CUR);	}// your data is stored in pixels


This will only work for single plane 24 bit bitmap files! To make it compatible for all sorts of bitmaps, youll have to look up the information by yourself. And as you can see, there is no error checking in there.
BMP files are pretty simple. They contain 54 byte header, palette (for paletted images) and then image data. You should be able to write import pretty quickly.
This is pretty simple description and this one contains info about compression and much more.
HTH

This topic is closed to new replies.

Advertisement