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
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
Copyright (c) 1997-2006, Destroyer Studios LLC.
AUTHOR: John DiCamillo
OVERVIEW
========
BMP image file loader
*/
#include "MemDebug.h"
#include "Bmp.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// +--------------------------------------------------------------------+
BmpImage::BmpImage()
: width(0), height(0), image(0)
{ }
BmpImage::BmpImage(short w, short h, unsigned long* hibits)
{
ZeroMemory(this, sizeof(BmpImage));
width = w;
height = h;
file_hdr.type = 19778; // 'BM'
file_hdr.size = sizeof(BmpFileHeader) +
sizeof(BmpInfoHeader) +
w * h * 3;
file_hdr.offset = sizeof(BmpFileHeader) +
sizeof(BmpInfoHeader);
info_hdr.hdr_size = sizeof(BmpInfoHeader);
info_hdr.width = width;
info_hdr.height = height;
info_hdr.planes = 1;
info_hdr.bit_count = 24;
int pixels = width * height;
image = new(__FILE__,__LINE__) DWORD [pixels];
if (image && pixels) {
for (int i = 0; i < pixels; i++)
image[i] = hibits[i];
}
}
BmpImage::~BmpImage()
{
delete [] image;
}
// +--------------------------------------------------------------------+
int BmpImage::Load(char *filename)
{
int status = BMP_INVALID;
FILE* f;
fopen_s(&f, filename,"rb");
if (f == NULL)
return BMP_NOFILE;
fread(&file_hdr.type, sizeof(WORD), 1, f);
fread(&file_hdr.size, sizeof(DWORD), 1, f);
fread(&file_hdr.rsvd1, sizeof(WORD), 1, f);
fread(&file_hdr.rsvd2, sizeof(WORD), 1, f);
fread(&file_hdr.offset, sizeof(DWORD), 1, f);
fread(&info_hdr, BMP_INFO_HDR_SIZE, 1, f);
if (info_hdr.width > 32768 || info_hdr.height > 32768 ||
(info_hdr.width&3) || (info_hdr.height&3) ||
info_hdr.compression != 0) {
fclose(f);
return BMP_INVALID;
}
width = (WORD) info_hdr.width;
height = (WORD) info_hdr.height;
// read 256 color BMP file
if (info_hdr.bit_count == 8) {
fread(palette, sizeof(palette), 1, f);
int pixels = width*height;
delete [] image;
image = new(__FILE__,__LINE__) DWORD[pixels];
if (image == NULL)
return BMP_NOMEM;
for (int row = height-1; row >= 0; row--) {
for (int col = 0; col < width; col++) {
BYTE index = fgetc(f);
image[row*width+col] = palette[index];
}
}
status = BMP_OK;
}
// read 24-bit (true COLOR) BMP file
else if (info_hdr.bit_count == 24) {
int pixels = width*height;
delete [] image;
image = new(__FILE__,__LINE__) DWORD[pixels];
if (image == NULL)
return BMP_NOMEM;
for (int row = height-1; row >= 0; row--) {
for (int col = 0; col < width; col++) {
DWORD blue = fgetc(f);
DWORD green = fgetc(f);
DWORD red = fgetc(f);
image[row*width+col] = 0xff000000 | (red << 16) | (green << 8) | blue;
}
}
status = BMP_OK;
}
fclose(f);
return status;
}
// +--------------------------------------------------------------------+
int BmpImage::LoadBuffer(unsigned char* buf, int len)
{
int status = BMP_INVALID;
BYTE* fp;
if (buf == NULL)
return BMP_NOFILE;
fp = buf;
memcpy(&info_hdr, buf + BMP_FILE_HDR_SIZE, BMP_INFO_HDR_SIZE);
fp += BMP_FILE_HDR_SIZE + BMP_INFO_HDR_SIZE;
if (info_hdr.width > 32768 || info_hdr.height > 32768 ||
(info_hdr.width&3) || (info_hdr.height&3) ||
info_hdr.compression != 0) {
return BMP_INVALID;
}
width = (WORD) info_hdr.width;
height = (WORD) info_hdr.height;
// read 256 color BMP file
if (info_hdr.bit_count == 8) {
memcpy(palette, fp, sizeof(palette));
fp += sizeof(palette);
int pixels = width*height;
delete [] image;
image = new(__FILE__,__LINE__) DWORD[pixels];
if (image == NULL)
return BMP_NOMEM;
for (int row = height-1; row >= 0; row--) {
for (int col = 0; col < width; col++) {
BYTE index = *fp++;
image[row*width+col] = palette[index];
}
}
status = BMP_OK;
}
// read 24-bit (true COLOR) BMP file
else if (info_hdr.bit_count == 24) {
int pixels = width*height;
delete [] image;
image = new(__FILE__,__LINE__) DWORD[pixels];
if (image == NULL)
return BMP_NOMEM;
for (int row = height-1; row >= 0; row--) {
for (int col = 0; col < width; col++) {
DWORD blue = *fp++;
DWORD green = *fp++;
DWORD red = *fp++;
image[row*width+col] = 0xff000000 | (red << 16) | (green << 8) | blue;
}
}
status = BMP_OK;
}
return status;
}
// +--------------------------------------------------------------------+
int BmpImage::Save(char *filename)
{
int status = BMP_INVALID;
FILE* f;
fopen_s(&f, filename,"wb");
if (f == NULL)
return BMP_NOFILE;
info_hdr.bit_count = 24;
info_hdr.compression = 0;
fwrite(&file_hdr.type, sizeof(WORD), 1, f);
fwrite(&file_hdr.size, sizeof(DWORD), 1, f);
fwrite(&file_hdr.rsvd1, sizeof(WORD), 1, f);
fwrite(&file_hdr.rsvd2, sizeof(WORD), 1, f);
fwrite(&file_hdr.offset, sizeof(DWORD), 1, f);
fwrite(&info_hdr, BMP_INFO_HDR_SIZE, 1, f);
// write 24-bit (TRUE COLOR) BMP file
for (int row = height-1; row >= 0; row--) {
for (int col = 0; col < width; col++) {
DWORD pixel = image[row*width+col];
BYTE blue = (BYTE) ((pixel & 0x000000ff) >> 0);
BYTE green = (BYTE) ((pixel & 0x0000ff00) >> 8);
BYTE red = (BYTE) ((pixel & 0x00ff0000) >> 16);
fwrite(&blue, 1, 1, f);
fwrite(&green, 1, 1, f);
fwrite(&red, 1, 1, f);
}
}
status = BMP_OK;
fclose(f);
return status;
}
|