Sign in to follow this  
bballmitch

Loading a Bitmap

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this