-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCDIB.cpp
More file actions
executable file
·219 lines (193 loc) · 6.35 KB
/
Copy pathCDIB.cpp
File metadata and controls
executable file
·219 lines (193 loc) · 6.35 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
///////////////////////////////////////////////////////////
// CTextureMap.C: Implementation file for the DIB class.
///////////////////////////////////////////////////////////
#include "stdafx.h"
#include "CDib.h"
#include "windowsx.h"
///////////////////////////////////////////////////////////
// CTextureMap::CTextureMap()
///////////////////////////////////////////////////////////
CTextureMap::CTextureMap(char* bits)
{
// Load the bitmap and initialize
// the class's data members.
// LoadBitmapFile(fileName);
CreateTextureMapBitmap(bits);
}
///////////////////////////////////////////////////////////
// CTextureMap::~CTextureMap()
///////////////////////////////////////////////////////////
CTextureMap::~CTextureMap()
{
// Free the memory assigned to the bitmap.
GlobalFreePtr(m_pBmInfo);
}
///////////////////////////////////////////////////////////
// CTextureMap::LoadBitmapFile()
//
// This function loads a DIB from disk into memory. It
// also initializes the various class data members.
///////////////////////////////////////////////////////////
void
CTextureMap::LoadBitmapFile(const char* fileName)
{
// Construct and open a file object.
CFile file(fileName, CFile::modeRead);
// Read the bitmap's file header into memory.
BITMAPFILEHEADER bmFileHeader;
file.Read((void*)&bmFileHeader, sizeof(bmFileHeader));
// Check whether the file is really a bitmap.
if (bmFileHeader.bfType != 0x4d42) {
AfxMessageBox("Not a bitmap file");
m_pBmFileHeader = 0;
m_pBmInfo = 0;
m_pBmInfoHeader = 0;
m_pRGBTable = 0;
m_pDibBits = 0;
m_numColors = 0;
}
// If the file checks out okay, continue loading.
else {
// Calculate the size of the DIB, which is the
// file size minus the size of the file header.
ULONGLONG fileLength = file.GetLength();
ULONGLONG dibSize = fileLength - sizeof(bmFileHeader);
// Allocate enough memory to fit the bitmap.
BYTE* pDib = (BYTE*)GlobalAllocPtr(GMEM_MOVEABLE, static_cast<SIZE_T>(dibSize));
// Read the bitmap into memory and close the file.
file.Read((void*)pDib, static_cast<UINT>(dibSize));
file.Close();
CreateTextureMapBitmap(pDib);
}
}
void
CTextureMap::CreateTextureMapBitmap(void* bits)
{
m_pBmFileHeader = 0;
m_pBmInfo = 0;
m_pBmInfoHeader = 0;
m_pRGBTable = 0;
m_pDibBits = 0;
m_numColors = 0;
BYTE* pDib = (BYTE*)bits;
// Initialize pointers to the bitmap's BITMAPINFO
// and BITMAPINFOHEADER structures.
m_pBmInfo = (LPBITMAPINFO)pDib;
m_pBmInfoHeader = (LPBITMAPINFOHEADER)pDib;
// Calculate a pointer to the bitmap's color table.
m_pRGBTable = (RGBQUAD*)(pDib + m_pBmInfoHeader->biSize);
// Get the number of colors in the bitmap.
int m_numColors = GetDibNumColors();
// Calculate the bitmap image's size.
m_pBmInfoHeader->biSizeImage = GetDibSizeImage();
// Make sure the biClrUsed field
// is initialized properly.
if (m_pBmInfoHeader->biClrUsed == 0) m_pBmInfoHeader->biClrUsed = m_numColors;
// Calculate a pointer to the bitmap's actual data.
DWORD clrTableSize = m_numColors * sizeof(RGBQUAD);
m_pDibBits = pDib + m_pBmInfoHeader->biSize + clrTableSize;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibSizeImage()
//
// This function calculates and returns the size of the
// bitmap's image in bytes.
///////////////////////////////////////////////////////////
DWORD
CTextureMap::GetDibSizeImage()
{
// If the bitmap's biSizeImage field contains
// invalid information, calculate the correct size.
if (m_pBmInfoHeader->biSizeImage == 0) {
// Get the width in bytes of a single row.
DWORD byteWidth = (DWORD)GetDibWidth();
// Get the height of the bitmap.
DWORD height = (DWORD)GetDibHeight();
// Multiply the byte width by the number of rows.
DWORD imageSize = byteWidth * height;
return imageSize;
}
// Otherwise, just return the size stored in
// the BITMAPINFOHEADER structure.
else
return m_pBmInfoHeader->biSizeImage;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibWidth()
//
// This function returns the width in bytes of a single
// row in the bitmap.
///////////////////////////////////////////////////////////
UINT
CTextureMap::GetDibWidth()
{
return (UINT)m_pBmInfoHeader->biWidth;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibHeight()
//
// This function returns the bitmap's height in pixels.
///////////////////////////////////////////////////////////
UINT
CTextureMap::GetDibHeight()
{
return (UINT)m_pBmInfoHeader->biHeight;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibNumColors()
//
// This function returns the number of colors in the
// bitmap.
///////////////////////////////////////////////////////////
UINT
CTextureMap::GetDibNumColors()
{
if ((m_pBmInfoHeader->biClrUsed == 0) && (m_pBmInfoHeader->biBitCount < 9))
return (1 << m_pBmInfoHeader->biBitCount);
else
return (int)m_pBmInfoHeader->biClrUsed;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibInfoHeaderPtr()
//
// This function returns a pointer to the bitmap's
// BITMAPINFOHEADER structure.
///////////////////////////////////////////////////////////
LPBITMAPINFOHEADER
CTextureMap::GetDibInfoHeaderPtr()
{
return m_pBmInfoHeader;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibInfoPtr()
//
// This function returns a pointer to the bitmap's
// BITMAPINFO structure.
///////////////////////////////////////////////////////////
LPBITMAPINFO
CTextureMap::GetDibInfoPtr()
{
return m_pBmInfo;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibRGBTablePtr()
//
// This function returns a pointer to the bitmap's
// color table.
///////////////////////////////////////////////////////////
LPRGBQUAD
CTextureMap::GetDibRGBTablePtr()
{
return m_pRGBTable;
}
///////////////////////////////////////////////////////////
// CTextureMap::GetDibBitsPtr()
//
// This function returns a pointer to the bitmap's
// actual image data.
///////////////////////////////////////////////////////////
BYTE*
CTextureMap::GetDibBitsPtr()
{
return m_pDibBits;
}