Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nonkel Vangrauwel

loading and displaying bitmap files

This topic is 6178 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

First off I am absolutely new to OpenGL and rather new to C programming, so sorry for the probably stupid question, but anyway... Here is the problem. I have this function to load bitmap files and for most of the files it works just fine. For some files though I get an error message "Memory could not be read. Click OK to stop or Cancel to debug.". I have absolutely no idea what goes wrong, so any help would be appreciated!!! unsigned char* LoadBMP(char* filename, BITMAPINFOHEADER* BitmapInfoHeader) { FILE* fileptr; BITMAPFILEHEADER BitmapFileHeader; unsigned char* BitmapImage; unsigned char Temp; fileptr = fopen(filename,"rb"); if (!fileptr) { return NULL; } fread(&BitmapFileHeader,sizeof (BITMAPFILEHEADER),1,fileptr); if (BitmapFileHeader.bfType != 0x4D42) { fclose(fileptr); return NULL; } fread(BitmapInfoHeader,sizeof(BITMAPINFOHEADER),1,fileptr); fseek(fileptr,BitmapFileHeader.bfOffBits,SEEK_SET); BitmapImage = (unsigned char*)malloc(BitmapInfoHeader->biSizeImage); if (!BitmapImage) { free(BitmapImage); fclose(fileptr); return NULL; } fread(BitmapImage,1,BitmapInfoHeader->biSizeImage,fileptr); if (!BitmapImage) { fclose(fileptr); return NULL; } for (unsigned int i = 0;i < BitmapInfoHeader->biSizeImage;i += 3) { Temp = BitmapImage; BitmapImage[i] = BitmapImage[i + 2]; BitmapImage[i + 2] = Temp; } fclose(fileptr); return BitmapImage; } Above is the code for the function to load the bmp.

Share this post


Link to post
Share on other sites
Advertisement
ok, this code wasn''t very clear, let''s try again:

  
unsigned char* LoadBMP(char* filename, BITMAPINFOHEADER* BitmapInfoHeader)
{
FILE* fileptr; BITMAPFILEHEADER BitmapFileHeader; unsigned char* BitmapImage; unsigned char Temp; fileptr = fopen(filename,"rb"); if (!fileptr) {
return NULL; }
fread(&BitmapFileHeader,sizeof(BITMAPFILEHEADER),1,fileptr);
if (BitmapFileHeader.bfType != 0x4D42)
{
fclose(fileptr); return NULL; }
fread(BitmapInfoHeader,sizeof(BITMAPINFOHEADER),1,fileptr);
fseek(fileptr,BitmapFileHeader.bfOffBits,SEEK_SET);
BitmapImage = (unsigned char*)malloc(BitmapInfoHeader->biSizeImage);
if (!BitmapImage) {
free(BitmapImage); fclose(fileptr); return NULL; }
fread(BitmapImage,1,BitmapInfoHeader->biSizeImage,fileptr);
if (!BitmapImage) {
fclose(fileptr); return NULL; }
for (unsigned int i = 0;i < BitmapInfoHeader->biSizeImage;i += 3)
{ Temp = BitmapImage[i]; BitmapImage[i] = BitmapImage[i + 2];
BitmapImage[i + 2] = Temp; }
fclose(fileptr); return BitmapImage; }


Hope this looks better. Is it possible the errors has something to do with the size of the bitmaps? I have the impression it only works for smaller images, but might be a coincidence.

Share this post


Link to post
Share on other sites
I think your routine only works with 24bit-color BMP files. You must get the bitmap depth from the bitmap header (BITMAPINFOHEADER)

Share this post


Link to post
Share on other sites
thnx Azdo, I know I have to adapt my code for other bit depths now. The images I use to test are all 8 bits per channel, which gives 24 bits per pixel, right? So I am not sure if it is the 24 bits causing the problem. Thanx for the reply anyway!!

Share this post


Link to post
Share on other sites
Quote from the MSDN :

quote:

A device-independent bitmap consists of two distinct parts: a BITMAPINFO structure describing the dimensions and colors of the bitmap, and an array of bytes defining the pixels of the bitmap. The bits in the array are packed together, but each scan line must be padded with zeroes to end on a LONG data-type boundary.



You should change your code to fit the tricky BMP format...

Share this post


Link to post
Share on other sites
The problem seemed to be in the biImageSize field fo the BITMAPINFOHEADER struct. Apparently it is 0 for some bitmaps (don''t know why). The result being that no memory was allocated... I included a test for this in my code, in which i calculate the imagesize myself in case of 0, and this seems to work so far! Thanks for the help anyway you guys, your answers just made me think in the right direction.
THANKS A BUNCH!!!!!!!!!!!!!!

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
you know you dont have to write your own image loading routines if you dont want to, there is an freeware library that can load any format of picture.

Check out DevIL openil.sourceforge.net

Share this post


Link to post
Share on other sites

  • 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!