-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBMP.cpp
More file actions
executable file
·271 lines (225 loc) · 6.42 KB
/
BMP.cpp
File metadata and controls
executable file
·271 lines (225 loc) · 6.42 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmp.h"
BMP::BMP(void)
{
memset(&m_Hdr, 0, sizeof(m_Hdr));
memset(&bmInfo.bmiHeader, 0, sizeof(bmInfo.bmiHeader));
memset(m_Palette, 0, sizeof(m_Palette));
m_pImage = NULL;
m_BmpImageSize = 0;
}
BMP::~BMP(void)
{
if (m_pImage) {
delete[] m_pImage;
m_pImage = NULL;
}
}
BOOL
BMP::Load(const char *filename)
{
FILE *fp;
BOOL ok = TRUE;
memset(&m_Hdr, 0, sizeof(m_Hdr));
memset(&bmInfo.bmiHeader, 0, sizeof(bmInfo.bmiHeader));
memset(m_Palette, 0, sizeof(m_Palette));
if (m_pImage) {
delete m_pImage;
m_pImage = NULL;
}
if ((fp = fopen(filename, "rb")) == 0) {
printf("Unable to open file '%s' for loading.\n", filename);
return FALSE;
}
if (ok && fread(&m_Hdr, sizeof(m_Hdr), 1, fp) != 1) {
printf("Read #1 failed from file '%s'\n", filename);
ok = FALSE;
}
if (ok && !IsBmp()) {
printf("The file '%s' is not a bitmap file.\n", filename);
ok = FALSE;
}
if (ok && fread(&bmInfo.bmiHeader, sizeof(bmInfo.bmiHeader), 1, fp) != 1) {
printf("Read #2 failed from file '%s'\n", filename);
ok = FALSE;
}
/*
** Calculate some of the fields that may not be filled in!
*/
if (ok && bmInfo.bmiHeader.biSizeImage == 0) {
long size;
size = (bmInfo.bmiHeader.biWidth * bmInfo.bmiHeader.biBitCount) / 8;
if (size % 4) {
size += 4 - (size % 4);
}
size *= bmInfo.bmiHeader.biHeight;
bmInfo.bmiHeader.biSizeImage = (unsigned long)size;
}
if (ok && !IsBmpVariantOk()) {
printf(
"The bitmap file '%s' is stored in the wrong bitmap sub-format.\n"
"Make sure you save the bitmap with the following options:\n"
"\n"
" 256 colours.\n"
" Windows RGB encoded (not RLE or OS/2 encoded)\n"
"\n",
filename);
ok = FALSE;
}
if (ok && fread(m_Palette, sizeof(m_Palette), 1, fp) != 1) {
printf("Read #3 failed from file '%s'\n", filename);
ok = FALSE;
}
if (ok) {
m_pImage = new unsigned char[(unsigned int)bmInfo.bmiHeader.biSizeImage];
if (m_pImage == NULL) {
printf("Out of memory trying to allocate a buffer for '%s'.\n", filename);
ok = FALSE;
}
else {
memset(m_pImage, 0, (unsigned int)bmInfo.bmiHeader.biSizeImage);
}
}
/*lint -esym(668,fread) Ignore possible null pointer*/
if (ok &&
fread(m_pImage, (unsigned int)bmInfo.bmiHeader.biSizeImage, 1, fp) != 1) {
printf("Read #4 failed from file '%s'\n", filename);
ok = FALSE;
}
/*lint +esym(668,m_pImage) */
fclose(fp);
return ok;
}
BOOL
BMP::Create(unsigned short width, unsigned short height, unsigned char *pImage)
{
unsigned long iw;
iw = width;
if ((iw & 0x03) != 0) {
iw += 4;
iw &= ~0x03;
}
m_BmpImageSize = iw * height;
m_Hdr.bfType = 0x4d42;
m_Hdr.bfSize = sizeof(m_Hdr) + sizeof(bmInfo.bmiHeader) + sizeof(m_Palette) +
m_BmpImageSize;
m_Hdr.bfReserved1 = 0;
m_Hdr.bfReserved2 = 0;
m_Hdr.bfOffBits =
sizeof(m_Hdr) + sizeof(bmInfo.bmiHeader) + sizeof(m_Palette);
bmInfo.bmiHeader.biSize = sizeof(bmInfo.bmiHeader);
bmInfo.bmiHeader.biWidth = (long)width;
bmInfo.bmiHeader.biHeight = (long)height;
bmInfo.bmiHeader.biPlanes = 1;
bmInfo.bmiHeader.biBitCount = 8;
bmInfo.bmiHeader.biCompression = BI_RGB;
bmInfo.bmiHeader.biSizeImage = m_BmpImageSize;
bmInfo.bmiHeader.biXPelsPerMeter = 0;
bmInfo.bmiHeader.biYPelsPerMeter = 0;
bmInfo.bmiHeader.biClrUsed = 256;
bmInfo.bmiHeader.biClrImportant = 256;
m_pImage = new unsigned char[(unsigned int)m_BmpImageSize];
if (m_pImage == NULL) {
printf("Out of memory trying to allocate a buffer for BMP file.\n");
return FALSE;
}
memset(m_pImage, 0, (unsigned int)m_BmpImageSize);
if (pImage) {
unsigned int y;
for (y = 0; y < height; y++) {
memcpy(&m_pImage[y * iw], &pImage[y * width], width);
}
}
InvertImage();
return TRUE;
}
void
BMP::CreateDIB(CBitmap &bitmap)
{
CClientDC dc(NULL);
HBITMAP hBmp = CreateDIBitmap(
dc.m_hDC, // handle to device context
&bmInfo.bmiHeader, // pointer to bitmap size and format data
CBM_INIT, // initialization flag
m_pImage, // pointer to initialization data
&bmInfo, // pointer to bitmap color-format data
DIB_RGB_COLORS); // color-data usage
bitmap.Attach(hBmp);
}
BOOL
BMP::Save(const char *filename)
{
FILE *fp;
BOOL ok;
if ((fp = fopen(filename, "wb")) == 0) {
printf("Unable to open file '%s' for saving.\n", filename);
return FALSE;
}
ok = TRUE;
if (fwrite(&m_Hdr, sizeof(m_Hdr), 1, fp) != 1) {
ok = FALSE;
}
if (ok && fwrite(&bmInfo.bmiHeader, sizeof(bmInfo.bmiHeader), 1, fp) != 1) {
ok = FALSE;
}
if (ok && fwrite(m_Palette, sizeof(m_Palette), 1, fp) != 1) {
ok = FALSE;
}
InvertImage();
if (ok && fwrite(m_pImage, (unsigned int)m_BmpImageSize, 1, fp) != 1) {
ok = FALSE;
}
InvertImage();
fclose(fp);
if (!ok) {
printf("Write to '%s' failed, disk full?\n", filename);
return FALSE;
}
return TRUE;
}
void
BMP::SetPalette(RGBQUAD *pPalette)
{
memcpy(m_Palette, pPalette, sizeof(m_Palette));
}
BOOL
BMP::IsBmp(void)
{
return (m_Hdr.bfType == 0x4d42);
}
BOOL
BMP::IsBmpVariantOk(void)
{
return (IsBmp() && bmInfo.bmiHeader.biPlanes == 1 &&
bmInfo.bmiHeader.biBitCount == 8 &&
bmInfo.bmiHeader.biCompression == BI_RGB);
}
void
BMP::InvertImage(void)
{
unsigned char *pTmpBuff = NULL;
unsigned short iw;
iw = (unsigned short)bmInfo.bmiHeader.biWidth;
if ((iw & 0x03) != 0) {
iw += 4;
iw &= ~0x03;
}
pTmpBuff = new unsigned char[(unsigned int)iw];
if (pTmpBuff == NULL) {
printf("Out of memory trying to allocate invert buffer.\n");
exit(1);
}
for (int i = 0, n = (unsigned short)bmInfo.bmiHeader.biHeight - 1;
i < (unsigned short)bmInfo.bmiHeader.biHeight / 2; i++, n--) {
memcpy(pTmpBuff, &m_pImage[i * iw], iw);
memcpy(&m_pImage[i * iw], &m_pImage[n * iw], iw);
memcpy(&m_pImage[n * iw], pTmpBuff, iw);
}
delete[] pTmpBuff;
}
/*---------------------------------------------------------------------------
** End of File
*/