-
Notifications
You must be signed in to change notification settings - Fork 0
/
qdbmp.cpp
457 lines (357 loc) · 11.6 KB
/
qdbmp.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
#include "qdbmp.h"
#include <stdlib.h>
#include <string.h>
/* Bitmap header */
typedef struct _BMP_Header
{
USHORT Magic; /* Magic identifier: "BM" */
UINT FileSize; /* Size of the BMP file in bytes */
USHORT Reserved1; /* Reserved */
USHORT Reserved2; /* Reserved */
UINT DataOffset; /* Offset of image data relative to the file's start */
UINT HeaderSize; /* Size of the header in bytes */
UINT Width; /* Bitmap's width */
UINT Height; /* Bitmap's height */
USHORT Planes; /* Number of color planes in the bitmap */
USHORT BitsPerPixel; /* Number of bits per pixel */
UINT CompressionType; /* Compression type */
UINT ImageDataSize; /* Size of uncompressed image's data */
UINT HPixelsPerMeter; /* Horizontal resolution (pixels per meter) */
UINT VPixelsPerMeter; /* Vertical resolution (pixels per meter) */
UINT ColorsUsed; /* Number of color indexes in the color table that are actually used by the bitmap */
UINT ColorsRequired; /* Number of color indexes that are required for displaying the bitmap */
} BMP_Header;
/* Private data structure */
struct _BMP
{
BMP_Header Header;
UCHAR* Palette;
UCHAR* Data;
};
/* Holds the last error code */
static BMP_STATUS BMP_LAST_ERROR_CODE = BMP_OK;
/* Error description strings */
static const char* BMP_ERROR_STRING[] =
{
"",
"General error",
"Could not allocate enough memory to complete the operation",
"File input/output error",
"File not found",
"File is not a supported BMP variant (must be uncompressed 8, 24 or 32 BPP)",
"File is not a valid BMP image",
"An argument is invalid or out of range",
"The requested action is not compatible with the BMP's type"
};
/* Size of the palette data for 8 BPP bitmaps */
#define BMP_PALETTE_SIZE ( 256 * 4 )
/*********************************** Public methods **********************************/
/**************************************************************
Creates a blank BMP image with the specified dimensions
and bit depth.
**************************************************************/
BMP* BMP_Create( UINT width, UINT height, USHORT depth )
{
BMP* bmp;
int bytes_per_pixel = depth >> 3;
UINT bytes_per_row;
if ( height <= 0 || width <= 0 )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
return NULL;
}
if ( depth != 8 && depth != 24 && depth != 32 )
{
BMP_LAST_ERROR_CODE = BMP_FILE_NOT_SUPPORTED;
return NULL;
}
/* Allocate the bitmap data structure */
bmp = (BMP*) calloc( 1, sizeof( BMP ) );
if ( bmp == NULL )
{
BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY;
return NULL;
}
/* Set header' default values */
bmp->Header.Magic = 0x4D42;
bmp->Header.Reserved1 = 0;
bmp->Header.Reserved2 = 0;
bmp->Header.HeaderSize = 40;
bmp->Header.Planes = 1;
bmp->Header.CompressionType = 0;
bmp->Header.HPixelsPerMeter = 0;
bmp->Header.VPixelsPerMeter = 0;
bmp->Header.ColorsUsed = 0;
bmp->Header.ColorsRequired = 0;
/* Calculate the number of bytes used to store a single image row. This is always
rounded up to the next multiple of 4. */
bytes_per_row = width * bytes_per_pixel;
bytes_per_row += ( bytes_per_row % 4 ? 4 - bytes_per_row % 4 : 0 );
/* Set header's image specific values */
bmp->Header.Width = width;
bmp->Header.Height = height;
bmp->Header.BitsPerPixel = depth;
bmp->Header.ImageDataSize = bytes_per_row * height;
bmp->Header.FileSize = bmp->Header.ImageDataSize + 54 + ( depth == 8 ? BMP_PALETTE_SIZE : 0 );
bmp->Header.DataOffset = 54 + ( depth == 8 ? BMP_PALETTE_SIZE : 0 );
/* Allocate palette */
if ( bmp->Header.BitsPerPixel == 8 )
{
bmp->Palette = (UCHAR*) calloc( BMP_PALETTE_SIZE, sizeof( UCHAR ) );
if ( bmp->Palette == NULL )
{
BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY;
free( bmp );
return NULL;
}
}
else
{
bmp->Palette = NULL;
}
/* Allocate pixels */
bmp->Data = (UCHAR*) calloc( bmp->Header.ImageDataSize, sizeof( UCHAR ) );
if ( bmp->Data == NULL )
{
BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY;
free( bmp->Palette );
free( bmp );
return NULL;
}
BMP_LAST_ERROR_CODE = BMP_OK;
return bmp;
}
/**************************************************************
Frees all the memory used by the specified BMP image.
**************************************************************/
void BMP_Free( BMP* bmp )
{
if ( bmp == NULL )
{
return;
}
if ( bmp->Palette != NULL )
{
free( bmp->Palette );
}
if ( bmp->Data != NULL )
{
free( bmp->Data );
}
free( bmp );
BMP_LAST_ERROR_CODE = BMP_OK;
}
/**************************************************************
Returns the image's width.
**************************************************************/
UINT BMP_GetWidth( BMP* bmp )
{
if ( bmp == NULL )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
return -1;
}
BMP_LAST_ERROR_CODE = BMP_OK;
return ( bmp->Header.Width );
}
/**************************************************************
Returns the image's height.
**************************************************************/
UINT BMP_GetHeight( BMP* bmp )
{
if ( bmp == NULL )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
return -1;
}
BMP_LAST_ERROR_CODE = BMP_OK;
return ( bmp->Header.Height );
}
/**************************************************************
Returns the image's color depth (bits per pixel).
**************************************************************/
USHORT BMP_GetDepth( BMP* bmp )
{
if ( bmp == NULL )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
return -1;
}
BMP_LAST_ERROR_CODE = BMP_OK;
return ( bmp->Header.BitsPerPixel );
}
/**************************************************************
Populates the arguments with the specified pixel's RGB
values.
**************************************************************/
void BMP_GetPixelRGB( BMP* bmp, UINT x, UINT y, UCHAR* r, UCHAR* g, UCHAR* b )
{
UCHAR* pixel;
UINT bytes_per_row;
UCHAR bytes_per_pixel;
if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
}
else
{
BMP_LAST_ERROR_CODE = BMP_OK;
bytes_per_pixel = bmp->Header.BitsPerPixel >> 3;
/* Row's size is rounded up to the next multiple of 4 bytes */
bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height;
/* Calculate the location of the relevant pixel (rows are flipped) */
pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x * bytes_per_pixel );
/* In indexed color mode the pixel's value is an index within the palette */
if ( bmp->Header.BitsPerPixel == 8 )
{
pixel = bmp->Palette + *pixel * 4;
}
/* Note: colors are stored in BGR order */
if ( r ) *r = *( pixel + 2 );
if ( g ) *g = *( pixel + 1 );
if ( b ) *b = *( pixel + 0 );
}
}
/**************************************************************
Sets the specified pixel's RGB values.
**************************************************************/
void BMP_SetPixelRGB( BMP* bmp, UINT x, UINT y, UCHAR r, UCHAR g, UCHAR b )
{
UCHAR* pixel;
UINT bytes_per_row;
UCHAR bytes_per_pixel;
if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
}
else if ( bmp->Header.BitsPerPixel != 24 && bmp->Header.BitsPerPixel != 32 )
{
BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH;
}
else
{
BMP_LAST_ERROR_CODE = BMP_OK;
bytes_per_pixel = bmp->Header.BitsPerPixel >> 3;
/* Row's size is rounded up to the next multiple of 4 bytes */
bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height;
/* Calculate the location of the relevant pixel (rows are flipped) */
pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x * bytes_per_pixel );
/* Note: colors are stored in BGR order */
*( pixel + 2 ) = r;
*( pixel + 1 ) = g;
*( pixel + 0 ) = b;
}
}
/**************************************************************
Gets the specified pixel's color index.
**************************************************************/
void BMP_GetPixelIndex( BMP* bmp, UINT x, UINT y, UCHAR* val )
{
UCHAR* pixel;
UINT bytes_per_row;
if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
}
else if ( bmp->Header.BitsPerPixel != 8 )
{
BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH;
}
else
{
BMP_LAST_ERROR_CODE = BMP_OK;
/* Row's size is rounded up to the next multiple of 4 bytes */
bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height;
/* Calculate the location of the relevant pixel */
pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x );
if ( val ) *val = *pixel;
}
}
/**************************************************************
Sets the specified pixel's color index.
**************************************************************/
void BMP_SetPixelIndex( BMP* bmp, UINT x, UINT y, UCHAR val )
{
UCHAR* pixel;
UINT bytes_per_row;
if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
}
else if ( bmp->Header.BitsPerPixel != 8 )
{
BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH;
}
else
{
BMP_LAST_ERROR_CODE = BMP_OK;
/* Row's size is rounded up to the next multiple of 4 bytes */
bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height;
/* Calculate the location of the relevant pixel */
pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x );
*pixel = val;
}
}
/**************************************************************
Gets the color value for the specified palette index.
**************************************************************/
void BMP_GetPaletteColor( BMP* bmp, UCHAR index, UCHAR* r, UCHAR* g, UCHAR* b )
{
if ( bmp == NULL )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
}
else if ( bmp->Header.BitsPerPixel != 8 )
{
BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH;
}
else
{
if ( r ) *r = *( bmp->Palette + index * 4 + 2 );
if ( g ) *g = *( bmp->Palette + index * 4 + 1 );
if ( b ) *b = *( bmp->Palette + index * 4 + 0 );
BMP_LAST_ERROR_CODE = BMP_OK;
}
}
/**************************************************************
Sets the color value for the specified palette index.
**************************************************************/
void BMP_SetPaletteColor( BMP* bmp, UCHAR index, UCHAR r, UCHAR g, UCHAR b )
{
if ( bmp == NULL )
{
BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT;
}
else if ( bmp->Header.BitsPerPixel != 8 )
{
BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH;
}
else
{
*( bmp->Palette + index * 4 + 2 ) = r;
*( bmp->Palette + index * 4 + 1 ) = g;
*( bmp->Palette + index * 4 + 0 ) = b;
BMP_LAST_ERROR_CODE = BMP_OK;
}
}
/**************************************************************
Returns the last error code.
**************************************************************/
BMP_STATUS BMP_GetError()
{
return BMP_LAST_ERROR_CODE;
}
/**************************************************************
Returns a description of the last error code.
**************************************************************/
const char* BMP_GetErrorDescription()
{
if ( BMP_LAST_ERROR_CODE > 0 && BMP_LAST_ERROR_CODE < BMP_ERROR_NUM )
{
return BMP_ERROR_STRING[ BMP_LAST_ERROR_CODE ];
}
else
{
return NULL;
}
}