bool PBitmap::loadBitmap(const char *fileName) // loads a bitmap into memory { FILE *in; // file stream for reading char *tempData; // temp storage for image data int numColours; // total available colours loaded = false; // bitmap is not loaded yet in = fopen(fileName, "rb"); // open the file for reading in binary mode if(in == NULL) // if the file does not exist return in error { error = "File not found"; MessageBox(NULL,"The path or filename for a Bitmap is Incorrect. \n" " The program will now exit.","Bitmap Load Error!", MB_OK); PostQuitMessage(0); fclose(in); return false; } //========== read in the BITMAPFILEHEADER ==========// fread(&bmfh, sizeof(BitmapFileHeader), 1, in); if(bmfh.bfType != BITMAP_MAGIC_NUMBER) // check magic number that says this is a bitmap { error = "File is not in DIB format"; MessageBox(NULL,"The file attempting to be loaded is not a bitmap file. \n" " Please check the file extension, path, or name. \n" "The Program will now Quit!","Bitmap Load Error!",MB_OK); PostQuitMessage(0); fclose(in); return false; } //========== read in the BITMAPINFOHEADER =========// fread(&bmih, sizeof(BitmapInfoHeader), 1, in); //writeLog(logFile, fileName); // write data to file width = bmih.biWidth; // set object data fields height = bmih.biHeight; bpp = bmih.biBitCount; if(bpp < 8) // test for correct bit-per-pixel depth { error = "File is not 8 or 24 bits per pixel"; fclose(in); return false; } // calculate the size of image data + padding dataSize = (width * height * (unsigned int)(bpp / 8.0) ); if(bpp == 8) // load the color palette for 8 bits per pixel { numColours = 1 << bpp; // calculate the number of available colours colours = new RGBQuad[numColours]; fread(colours, sizeof(RGBQuad), numColours, in); // read in the color palette } tempData = new char[dataSize]; // set up the temporary buffer for the image data if(tempData == NULL) // exit if there is not enough memory { error = "Not enough memory to allocate a temporary buffer"; fclose(in); return false; } fread(tempData, sizeof(char), dataSize, in); // read in the entire image fclose(in); // close the file now that we have all the info // set both values to the byte total of the file, without padding byteWidth = padWidth = (int)( (float)width * (float)bpp / 8.0 ); while(padWidth % 4 != 0) // adjust the width for padding as necessary { padWidth++; } if(bpp == 8) // change format from GBR to RGB { loaded = convert8(tempData); } else if(bpp == 24) { loaded = convert24(tempData); } delete [] tempData; tempData = NULL; // clean up memory error = "Bitmap loaded"; // bitmap is now loaded return loaded; // return success }
//load a bitmap from a file and represent it correctly //in memory bool Bitmap::loadBMP(char *file) { FILE *in; //file stream for reading char *tempData; //temp storage for image data int numColours; //total available colours //bitmap is not loaded yet loaded=false; //make sure memory is not lost if(colours!=0) { delete[] colours; } if(data!=0) { delete[] data; } //open the file for reading in binary mode in=fopen(file,"rb"); //if the file does not exist return in error if(in==NULL) { error="File not found"; fclose(in); return false; } //read in the entire BITMAPFILEHEADER fread(&bmfh,sizeof(BitmapFileHeader),1,in); cout << "sizeof(BitmapFileHeader)=" << sizeof(BitmapFileHeader) << endl; //check for the magic number that says this is a bitmap if(bmfh.bfType!=BITMAP_MAGIC_NUMBER) { error="File is not in DIB format"; fclose(in); return false; } //read in the entire BITMAPINFOHEADER fread(&bmih,sizeof(BitmapInfoHeader),1,in); cout << "sizeof(BitmapInfoHeader)=" << sizeof(BitmapInfoHeader) << endl; //save the width, height and bits per pixel for external use width=bmih.biWidth; height=bmih.biHeight; bpp=bmih.biBitCount; //calculate the size of the image data with padding dataSize=(width*height*(unsigned int)(bmih.biBitCount/8.0)); //calculate the number of available colours numColours=1<<bmih.biBitCount; //if the bitmap is not 8 bits per pixel or more //return in error if(bpp<8) { error="File is not 8 or 24 bits per pixel"; fclose(in); return false; } //load the palette for 8 bits per pixel if(bpp==8) { colours=new RGBQuad[numColours]; fread(colours,sizeof(RGBQuad),numColours,in); } //set up the temporary buffer for the image data tempData=new char[dataSize]; //exit if there is not enough memory if(tempData==NULL) { error="Not enough memory to allocate a temporary buffer"; fclose(in); return false; } //read in the entire image fread(tempData,sizeof(char),dataSize,in); //close the file now that we have all the info fclose(in); //calculate the witdh of the final image in bytes byteWidth=padWidth=(int)((float)width*(float)bpp/8.0); //adjust the width for padding as necessary while(padWidth%4!=0) { padWidth++; } //change format from GBR to RGB if(bpp==8) { loaded=convert8(tempData); } else if(bpp==24) { loaded=convert24(tempData); } //clean up memory delete[] tempData; //bitmap is now loaded error="Bitmap loaded"; //return success return loaded; }