| 164 | } |
| 165 | |
| 166 | unsigned char *LoadBitmapFile(const char *filename, BITMAPINFOHEADER *bitmapInfoHeader) |
| 167 | { |
| 168 | FILE *filePtr; //our file pointer |
| 169 | BITMAPFILEHEADER bitmapFileHeader; //our bitmap file header |
| 170 | unsigned char *bitmapImage; //store image data |
| 171 | //int imageIdx = 0; //image index counter |
| 172 | unsigned char tempRGB; //our swap variable |
| 173 | |
| 174 | //open filename in read binary mode |
| 175 | filePtr = fopen(filename, "rb"); |
| 176 | if (filePtr == NULL) |
| 177 | return NULL; |
| 178 | |
| 179 | //read the bitmap file header |
| 180 | fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr); |
| 181 | |
| 182 | //verify that this is a bmp file by check bitmap id |
| 183 | if (bitmapFileHeader.bfType != 0x4D42) |
| 184 | { |
| 185 | fclose(filePtr); |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | //read the bitmap info header |
| 190 | fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr); // small edit. forgot to add the closing bracket at sizeof |
| 191 | |
| 192 | //move file point to the begging of bitmap data |
| 193 | fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET); |
| 194 | |
| 195 | //Only incompressed 24 byte RGB is supported |
| 196 | if (bitmapInfoHeader->biCompression != BI_RGB || bitmapInfoHeader->biBitCount != 24) |
| 197 | { |
| 198 | return nullptr; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | bitmapInfoHeader->biSizeImage = 3 * bitmapInfoHeader->biHeight * bitmapInfoHeader->biHeight; |
| 203 | } |
| 204 | |
| 205 | //allocate enough memory for the bitmap image data |
| 206 | bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage); |
| 207 | |
| 208 | //verify memory allocation |
| 209 | if (!bitmapImage) |
| 210 | { |
| 211 | free(bitmapImage); |
| 212 | fclose(filePtr); |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | //read in the bitmap image data |
| 217 | fread(bitmapImage, sizeof(uint8_t), bitmapInfoHeader->biSizeImage, filePtr); |
| 218 | |
| 219 | //make sure bitmap image data was read |
| 220 | if (bitmapImage == NULL) |
| 221 | { |
| 222 | fclose(filePtr); |
| 223 | return NULL; |