This repository has been archived by the owner on Jan 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadBitmaps.cpp
executable file
·83 lines (63 loc) · 1.74 KB
/
LoadBitmaps.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*!\file loadBitmaps.cpp
* \brief Loads bitmaps.
* \author Dean N. Butcher
* \version 2.1
* \date August 2004
*/
#include <windows.h>
#include <stdio.h>
/*! Bitmap tpye ID. */
const DWORD BITMAP_ID = 0x4D42;
/*! Bitmap information header. */
BITMAPINFOHEADER bitmapInfoHeader;
/*! Pointer to bitmap file to be manipulated. */
FILE *pFile = NULL;
/*! Bitmap file header. */
BITMAPFILEHEADER bitmapFileHeader;
/*! Pointer to a bitmap image loaded from file. */
unsigned char *bitmapImage = NULL;
/*! Temporary bitmap storgae location. */
unsigned char tempRGB;
unsigned char *LoadBitmapFile(char *bmpFile, BITMAPINFOHEADER *bitmapInfoHeader)
// Loads a bitmap into memory.
{
if((pFile = fopen(bmpFile, "rb")) == NULL)
return NULL;
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, pFile);
if(bitmapFileHeader.bfType != BITMAP_ID)
{
fclose(pFile);
return NULL;
}
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, pFile);
fseek(pFile, bitmapFileHeader.bfOffBits, SEEK_SET);
bitmapImage = (unsigned char*) malloc(bitmapInfoHeader->biSizeImage);
if(!bitmapImage)
{
free(bitmapImage);
fclose(pFile);
return NULL;
}
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, pFile);
if(bitmapImage == NULL)
{
fclose(pFile);
return NULL;
}
for(unsigned int imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}
fclose(pFile);
return bitmapImage;
}
void CleanUpLoadBitmap()
// Cleans up memory used by file pointer.
{
if(pFile)
fclose(pFile);
if(bitmapImage)
free(bitmapImage);
}