Jump to content
  • Advertisement
Sign in to follow this  
bballmitch

Loading a Bitmap

This topic is 4503 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 can't seem to get this to work and its kind of frustrating. Here are the relevant pieces of code: In Seperate Header File
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
         FILE *filePtr;
         BITMAPFILEHEADER bitmapFileHeader;
         unsigned char *bitmapImage;
         int imageIdx = 0;
         unsigned char tempRGB;
         
         filePtr = fopen(filename, "rb");
         if(filePtr = NULL)
                    return NULL;
         
         fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 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 == NULL)
         {
                        fclose(filePtr);
                        return NULL;
         }
         
         for(imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
         {
                      tempRGB = bitmapImage[imageIdx];
                      bitmapImage[imageIdx] = bitmapImage[imageIdx+2];
                      bitmapImage[imageIdx+2] = tempRGB;
         }
         
         fclose(filePtr);
         return bitmapImage;
}


In Main.cpp
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;
.
.
.
int initGL(GLvoid)
{
     bitmapData = LoadBitmapFile("MitchenGames.bmp", &bitmapInfoHeader);
     .
     .
     .
}
.
.
.
int DrawGLScene(GLvoid)
{
     .
     .
     .
glPushMatrix();
          glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
          glRasterPos2i(1,-1);
          glDrawPixels(bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
          glPopMatrix();
     .
     .
     .
}

.
.
.



I don't get what the problem is. I know that my bitmap is in the right folder. The program compiles, but everytime I run the program, it has a Windows warning menu and forces me to quit the program. Can someone help me out here?

Share this post


Link to post
Share on other sites
Advertisement
I can't see where are you loading data for bitmapInfoHeader variable. But you are using it (for example: bitmapInfoHeader->biSizeImage).

Share this post


Link to post
Share on other sites
Heres what I use to load my Windows BitMap Files -


void BMPImageFile::read()
{
DWORD bytesRead;
bool isitGood = false;

ReadFile (fileHandle,&fileHeader,sizeof(BITMAPFILEHEADER),&bytesRead,NULL); // read file header
ReadFile (fileHandle,&infoHeader,sizeof(BITMAPINFOHEADER),&bytesRead,NULL); // read info header
image = VirtualAlloc(NULL,infoHeader.biSizeImage,MEM_COMMIT,PAGE_READWRITE | PAGE_NOCACHE);
ReadFile (fileHandle,image,infoHeader.biSizeImage,&bytesRead,NULL); // read image
BGRFilter (infoHeader.biSizeImage,image);
height = infoHeader.biHeight;
width = infoHeader.biWidth;

};



void File::assign(char* filePath)
{
strcpy(fileName,filePath);
};

void File::open()
{
fileHandle = CreateFile(fileName,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
fileSize = GetFileSize(fileHandle,NULL);
};

void File::close()
{
CloseHandle(fileHandle);
};

class BMPImageFile : public File // Windows Bit Map (GDI) Format IMPLIMENTATION
{
public :
BITMAPFILEHEADER fileHeader;
BITMAPINFOHEADER infoHeader;

void* image;
long int width;
long int height;
void read ();

};







BGR Filter, reorders the bits, because windows bit maps are oposite order instead of RGB, its BGR.


Why do you use Fseek instead of Fread? why not fread the file and infoheader?

Share this post


Link to post
Share on other sites
well...I'm not sure but...I have seen an error in your first code:


if(filePtr = NULL)
return NULL;


You must not use '=' here and yes '=='.


if(filePtr == NULL)
return NULL;


I really cant beleive this is the error but...It can be :P

Share this post


Link to post
Share on other sites
Ok. I got it to work. Somehow, it was the == thing.

Anyway, here's the new problem: when I display the bitmap, it shows up too big. So I tried using glScalef() and that didn't do ANYTHING. then I tried translating the position so its farther away: that didn't work either. How do I make the bitmap look smaller?

Share this post


Link to post
Share on other sites
did you call glFlush? and are you "swapping the buffers"? did you generate a texture?

Do you have the Red Book?

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!