29 unsigned char** textureDataPtrPtr)
32 "Invalid file name.");
33 ASSERT(textureDataPtrPtr != NULL,
34 "Cannot use NULL pointer to store image data.");
36 tagBITMAPFILEHEADER bitmapFileHeader;
37 tagBITMAPINFOHEADER bitmapInfoHeader;
39 unsigned char* loadedTexture = NULL;
42 file = fopen(fileName,
"rb");
44 ASSERT(file != NULL,
"Failed to open file");
53 loadedTexture = (
unsigned char*) malloc(bitmapInfoHeader.biSizeImage);
55 ASSERT(loadedTexture != NULL,
"Could not allocate memory to store texture image data.");
58 fseek(file, bitmapFileHeader.bfOffBits, 0);
61 fread(loadedTexture, bitmapInfoHeader.biSizeImage, 1, file);
63 unsigned char tempElement;
66 for (
unsigned int imageIdx = 0;
67 imageIdx < bitmapInfoHeader.biSizeImage;
70 tempElement = loadedTexture[imageIdx];
71 loadedTexture[imageIdx] = loadedTexture[imageIdx + 2];
72 loadedTexture[imageIdx + 2] = tempElement;
79 *textureDataPtrPtr = loadedTexture;
82 if (imageHeightPtr != NULL)
84 *imageHeightPtr = bitmapInfoHeader.biHeight;
87 if (imageWidthPtr != NULL)
89 *imageWidthPtr = bitmapInfoHeader.biWidth;
97 bitmapFileHeaderPtr != NULL,
98 "Invalid arguments used to read bitmap file header.");
100 fread(&bitmapFileHeaderPtr->bfType,
sizeof(bitmapFileHeaderPtr->bfType), 1, filePtr);
101 fread(&bitmapFileHeaderPtr->bfSize,
sizeof(bitmapFileHeaderPtr->bfSize), 1, filePtr);
102 fread(&bitmapFileHeaderPtr->bfReserved1,
sizeof(bitmapFileHeaderPtr->bfReserved1), 1, filePtr);
103 fread(&bitmapFileHeaderPtr->bfReserved2,
sizeof(bitmapFileHeaderPtr->bfReserved2), 1, filePtr);
104 fread(&bitmapFileHeaderPtr->bfOffBits,
sizeof(bitmapFileHeaderPtr->bfOffBits), 1, filePtr);
107 ASSERT(bitmapFileHeaderPtr->bfType == 0x4D42,
108 "Invalid file type read");
115 bitmapInfoHeaderPtr != NULL,
116 "Invalid arguments used to read bitmap info header.");
118 fread(&bitmapInfoHeaderPtr->biSize,
sizeof(bitmapInfoHeaderPtr->biSize), 1, filePtr);
119 fread(&bitmapInfoHeaderPtr->biWidth,
sizeof(bitmapInfoHeaderPtr->biWidth), 1, filePtr);
120 fread(&bitmapInfoHeaderPtr->biHeight,
sizeof(bitmapInfoHeaderPtr->biHeight), 1, filePtr);
121 fread(&bitmapInfoHeaderPtr->biPlanes,
sizeof(bitmapInfoHeaderPtr->biPlanes), 1, filePtr);
122 fread(&bitmapInfoHeaderPtr->biBitCount,
sizeof(bitmapInfoHeaderPtr->biBitCount), 1, filePtr);
123 fread(&bitmapInfoHeaderPtr->biCompression,
sizeof(bitmapInfoHeaderPtr->biCompression), 1, filePtr);
124 fread(&bitmapInfoHeaderPtr->biSizeImage,
sizeof(bitmapInfoHeaderPtr->biSizeImage), 1, filePtr);
125 fread(&bitmapInfoHeaderPtr->biXPelsPerMeter,
sizeof(bitmapInfoHeaderPtr->biXPelsPerMeter), 1, filePtr);
126 fread(&bitmapInfoHeaderPtr->biYPelsPerMeter,
sizeof(bitmapInfoHeaderPtr->biYPelsPerMeter), 1, filePtr);
127 fread(&bitmapInfoHeaderPtr->biClrUsed,
sizeof(bitmapInfoHeaderPtr->biClrUsed), 1, filePtr);
128 fread(&bitmapInfoHeaderPtr->biClrImportant,
sizeof(bitmapInfoHeaderPtr->biClrImportant), 1, filePtr);
static void readBitmapFileHeader(FILE *filePtr, tagBITMAPFILEHEADER *bitmapFileHeaderPtr)
Read BMP file header.
static void readBitmapInforHeader(FILE *filePtr, tagBITMAPINFOHEADER *bitmapInfoHeaderPtr)
Read BMP info header.
static void loadBmpImageData(const char *fileName, int *imageWidthPtr, int *imageHeightPtr, unsigned char **textureDataPtrPtr)
Load BMP texture data from a file into memory.