Jump to content
  • Advertisement
Sign in to follow this  
nickwinters

Need to load a bmp and read pixel rgba values

This topic is 4953 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!