Repository navigation
Expand file tree
/
Copy pathbmp_io.c
More file actions
431 lines (361 loc) · 13.5 KB
/
Copy pathbmp_io.c
File metadata and controls
431 lines (361 loc) · 13.5 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
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
#include "bmp_io.h"
#include "os.h"
#include "endian.h"
#include <stdio.h> /* fopen() */
#include <string.h> /* memcpy() */
#if defined(_MSC_VER)
#include "ms_stdbool.h"
#include "ms_stdint.h"
#else
#include <stdbool.h>
#include <stdint.h>
#endif
#pragma pack(push, 1) /* The following structs should be continguous, so we can
* copy them in one read. */
/*
* Standard, initial BMP Header
*/
struct BITMAP_FILE_HEADER {
uint16_t magic; /* First two byes of the file; should be 0x4D42. */
uint32_t fileSize; /* Size of the BMP file in bytes (unreliable). */
uint32_t reserved; /* Application-specific. */
uint32_t imageOffset; /* Offset to bitmap data. */
};
#define BMP_MAGIC 0x4D42 /* The starting key that marks the file as a BMP. */
enum _BMP_COMPRESSION {
kBMP_RGB = 0, /* No compression. */
kBMP_RLE8 = 1, /* Can only be used with 8-bit bitmaps. */
kBMP_RLE4 = 2, /* Can only be used with 4-bit bitmaps. */
kBMP_BITFIELDS = 3, /* Can only be used with 16/32-bit bitmaps. */
kBMP_JPEG = 4, /* Bitmap contains a JPEG image. */
kBMP_PNG = 5 /* Bitmap contains a PNG image. */
};
typedef uint32_t BMP_COMPRESSION;
/*
* Windows 3 Header
*/
struct BITMAP_INFO_HEADER {
uint32_t headerSize; /* The size of this header (40 bytes). */
int32_t width; /* The bitmap width in pixels. */
int32_t height; /* The bitmap height in pixels. */
/* (A negative value denotes that the image
* is flipped.) */
uint16_t colorPlanes; /* The number of color planes; must be 1. */
uint16_t bitsPerPixel; /* The color depth of the image (1, 4, 8, 16,
* 24, or 32). */
BMP_COMPRESSION compression; /* The compression method being used. */
uint32_t imageSize; /* Size of the bitmap in bytes (unreliable).*/
int32_t xRes; /* The horizontal resolution (unreliable). */
int32_t yRes; /* The vertical resolution (unreliable). */
uint32_t colorsUsed; /* The number of colors in the color table,
* or 0 to default to 2^n. */
uint32_t colorsImportant; /* Colors important for displaying bitmap,
* or 0 when every color is equally important;
* ignored. */
};
/*
* OS/2 v1 Header
*/
struct BITMAP_CORE_HEADER {
uint32_t headerSize; /* The size of this header (12 bytes). */
uint16_t width; /* The bitmap width in pixels. */
uint16_t height; /* The bitmap height in pixels. */
uint16_t colorPlanes; /* The number of color planes; must be 1. */
uint16_t bitsPerPixel; /* Color depth of the image (1, 4, 8, or 24). */
};
#pragma pack(pop) /* Let the compiler do what it wants now. */
/* BMP files are always saved in little endian format (x86), so we need to
* convert them if we're not on a little endian machine (e.g., ARM & ppc). */
#if __BYTE_ORDER == __BIG_ENDIAN
/* Converts bitmap file header from to and from little endian, if and only if
* host is big endian. */
static void convertBitmapFileHeader(struct BITMAP_FILE_HEADER *header)
{
header->magic = swapLittleAndHost16(header->magic);
swapLittleAndHost32(header->fileSize);
swapLittleAndHost32(header->reserved);
swapLittleAndHost32(header->imageOffset);
}
/* Converts bitmap info header from to and from little endian, if and only if
* host is big endian. */
static void convertBitmapInfoHeader(struct BITMAP_INFO_HEADER *header)
{
header->headerSize = swapLittleAndHost32(header->headerSize);
header->width = swapLittleAndHost32(header->width);
header->height = swapLittleAndHost32(header->height);
header->colorPlanes = swapLittleAndHost16(header->colorPlanes);
header->bitsPerPixel = swapLittleAndHost16(header->bitsPerPixel);
header->compression = swapLittleAndHost32(header->compression);
header->imageSize = swapLittleAndHost32(header->imageSize);
header->xRes = swapLittleAndHost32(header->xRes);
header->yRes = swapLittleAndHost32(header->yRes);
header->colorsUsed = swapLittleAndHost32(header->colorsUsed);
header->colorsImportant = swapLittleAndHost32(header->colorsImportant);
}
#elif __BYTE_ORDER == __LITTLE_ENDIAN
/* No conversion necessary if we are already little endian. */
#define convertBitmapFileHeader(header)
#define convertBitmapInfoHeader(header)
#endif
/* Returns newly alloc'd image data from bitmap file. The current position of
* the file must be at the start of the image before calling this. */
static uint8_t *readImageData(FILE *fp, size_t width, size_t height,
uint8_t bytesPerPixel, size_t bytewidth);
/* Copys image buffer from |bitmap| to |dest| in BGR format. */
static void copyBGRDataFromMMBitmap(MMBitmapRef bitmap, uint8_t *dest);
const char *MMBMPReadErrorString(MMIOError error)
{
switch (error) {
case kBMPAccessError:
return "Could not open file";
case kBMPInvalidKeyError:
return "Not a BMP file";
case kBMPUnsupportedHeaderError:
return "Unsupported BMP header";
case kBMPInvalidColorPanesError:
return "Invalid number of color panes in BMP file";
case kBMPUnsupportedColorDepthError:
return "Unsupported color depth in BMP file";
case kBMPUnsupportedCompressionError:
return "Unsupported file compression in BMP file";
case kBMPInvalidPixelDataError:
return "Could not read BMP pixel data";
default:
return NULL;
}
}
MMBitmapRef newMMBitmapFromBMP(const char *path, MMBMPReadError *err)
{
FILE *fp;
struct BITMAP_FILE_HEADER fileHeader = {0}; /* Initialize elements to 0. */
struct BITMAP_INFO_HEADER dibHeader = {0};
uint32_t headerSize = 0;
uint8_t bytesPerPixel;
size_t bytewidth;
uint8_t *imageBuf;
if ((fp = fopen(path, "rb")) == NULL) {
if (err != NULL) *err = kBMPAccessError;
return NULL;
}
/* Initialize error code to generic value. */
if (err != NULL) *err = kBMPGenericError;
if (fread(&fileHeader, sizeof(fileHeader), 1, fp) == 0) goto bail;
/* Convert from little-endian if it's not already. */
convertBitmapFileHeader(&fileHeader);
/* First two bytes should always be 0x4D42. */
if (fileHeader.magic != BMP_MAGIC) {
if (err != NULL) *err = kBMPInvalidKeyError;
goto bail;
}
/* Get header size. */
if (fread(&headerSize, sizeof(headerSize), 1, fp) == 0) goto bail;
headerSize = swapLittleAndHost32(headerSize);
/* Back up before reading header. */
if (fseek(fp, -(long)sizeof(headerSize), SEEK_CUR) < 0) goto bail;
if (headerSize == 12) { /* OS/2 v1 header */
struct BITMAP_CORE_HEADER coreHeader = {0};
if (fread(&coreHeader, sizeof(coreHeader), 1, fp) == 0) goto bail;
dibHeader.width = coreHeader.width;
dibHeader.height = coreHeader.height;
dibHeader.colorPlanes = coreHeader.colorPlanes;
dibHeader.bitsPerPixel = coreHeader.bitsPerPixel;
} else if (headerSize == 40 || headerSize == 108 || headerSize == 124) {
/* Windows v3/v4/v5 header */
/* Read only the common part (v3) and skip over the rest. */
if (fread(&dibHeader, sizeof(dibHeader), 1, fp) == 0) goto bail;
} else {
if (err != NULL) *err = kBMPUnsupportedHeaderError;
goto bail;
}
convertBitmapInfoHeader(&dibHeader);
if (dibHeader.colorPlanes != 1) {
if (err != NULL) *err = kBMPInvalidColorPanesError;
goto bail;
}
/* Currently only 24-bit and 32-bit are supported. */
if (dibHeader.bitsPerPixel != 24 && dibHeader.bitsPerPixel != 32) {
if (err != NULL) *err = kBMPUnsupportedColorDepthError;
goto bail;
}
if (dibHeader.compression != kBMP_RGB) {
if (err != NULL) *err = kBMPUnsupportedCompressionError;
goto bail;
}
/* This can happen because we don't fully parse Windows v4/v5 headers. */
if (ftell(fp) != (long)fileHeader.imageOffset) {
fseek(fp, fileHeader.imageOffset, SEEK_SET);
}
/* Get bytes per row, including padding. */
bytesPerPixel = dibHeader.bitsPerPixel / 8;
bytewidth = ADD_PADDING(dibHeader.width * bytesPerPixel);
imageBuf = readImageData(fp, dibHeader.width, abs(dibHeader.height),
bytesPerPixel, bytewidth);
fclose(fp);
if (imageBuf == NULL) {
if (err != NULL) *err = kBMPInvalidPixelDataError;
return NULL;
}
/* A negative height indicates that the image is flipped.
*
* We store our bitmaps as "flipped" according to the BMP format; i.e., (0, 0)
* is the top left, not bottom left. So we only need to flip the bitmap if
* the height is NOT negative. */
if (dibHeader.height < 0) {
dibHeader.height = -dibHeader.height;
} else {
flipBitmapData(imageBuf, dibHeader.width, dibHeader.height, bytewidth);
}
return createMMBitmap(imageBuf, dibHeader.width, dibHeader.height,
bytewidth, (uint8_t)dibHeader.bitsPerPixel,
bytesPerPixel);
bail:
fclose(fp);
return NULL;
}
uint8_t *createBitmapData(MMBitmapRef bitmap, size_t *len)
{
/* BMP files are always aligned to 4 bytes. */
const size_t bytewidth = ((bitmap->width * bitmap->bytesPerPixel) + 3) & ~3;
const size_t imageSize = bytewidth * bitmap->height;
struct BITMAP_FILE_HEADER *fileHeader;
struct BITMAP_INFO_HEADER *dibHeader;
/* Should always be 54. */
const size_t imageOffset = sizeof(*fileHeader) + sizeof(*dibHeader);
uint8_t *data;
const size_t dataLen = imageOffset + imageSize;
data = calloc(1, dataLen);
if (data == NULL) return NULL;
/* Save top header. */
fileHeader = (struct BITMAP_FILE_HEADER *)data;
fileHeader->magic = BMP_MAGIC;
fileHeader->fileSize = (uint32_t)(sizeof(*dibHeader) + imageSize);
fileHeader->imageOffset = (uint32_t)imageOffset;
/* BMP files are always stored as little-endian, so we need to convert back
* if necessary. */
convertBitmapFileHeader(fileHeader);
/* Copy Windows v3 header. */
dibHeader = (struct BITMAP_INFO_HEADER *)(data + sizeof(*fileHeader));
dibHeader->headerSize = sizeof(*dibHeader); /* Should always be 40. */
dibHeader->width = (int32_t)bitmap->width;
dibHeader->height = -(int32_t)bitmap->height; /* Our bitmaps are "flipped". */
dibHeader->colorPlanes = 1;
dibHeader->bitsPerPixel = bitmap->bitsPerPixel;
dibHeader->compression = kBMP_RGB; /* Don't save with compression. */
dibHeader->imageSize = (uint32_t)imageSize;
convertBitmapInfoHeader(dibHeader);
/* Lastly, copy the pixel data. */
copyBGRDataFromMMBitmap(bitmap, data + imageOffset);
if (len != NULL) *len = dataLen;
return data;
}
int saveMMBitmapAsBMP(MMBitmapRef bitmap, const char *path)
{
FILE *fp;
size_t dataLen;
uint8_t *data;
if ((fp = fopen(path, "wb")) == NULL) return -1;
if ((data = createBitmapData(bitmap, &dataLen)) == NULL) {
fclose(fp);
return -1;
}
if (fwrite(data, dataLen, 1, fp) == 0) {
free(data);
fclose(fp);
return -1;
}
free(data);
fclose(fp);
return 0;
}
static uint8_t *readImageData(FILE *fp, size_t width, size_t height,
uint8_t bytesPerPixel, size_t bytewidth)
{
size_t imageSize = bytewidth * height;
uint8_t *imageBuf = calloc(1, imageSize);
if (MMRGB_IS_BGR && (bytewidth % 4) == 0) { /* No conversion needed. */
if (fread(imageBuf, imageSize, 1, fp) == 0) {
free(imageBuf);
return NULL;
}
} else { /* Convert from BGR with 4-byte alignment. */
uint8_t *row = malloc(bytewidth);
size_t y;
const size_t bmp_bytewidth = (width * bytesPerPixel + 3) & ~3;
if (row == NULL) return NULL;
assert(bmp_bytewidth <= bytewidth);
/* Read image data row by row. */
for (y = 0; y < height; ++y) {
const size_t rowOffset = y * bytewidth;
size_t x;
uint8_t *rowptr = row;
if (fread(row, bmp_bytewidth, 1, fp) == 0) {
free(imageBuf);
free(row);
return NULL;
}
for (x = 0; x < width; ++x) {
const size_t colOffset = x * bytesPerPixel;
MMRGBColor *color = (MMRGBColor *)(imageBuf +
rowOffset + colOffset);
/* BMP files are stored in BGR format. */
color->blue = rowptr[0];
color->green = rowptr[1];
color->red = rowptr[2];
rowptr += bytesPerPixel;
}
}
free(row);
}
return imageBuf;
}
static void copyBGRDataFromMMBitmap(MMBitmapRef bitmap, uint8_t *dest)
{
if (MMRGB_IS_BGR && (bitmap->bytewidth % 4) == 0) { /* No conversion needed. */
memcpy(dest, bitmap->imageBuffer, bitmap->bytewidth * bitmap->height);
} else { /* Convert to RGB with other-than-4-byte alignment. */
const size_t bytewidth = (bitmap->width * bitmap->bytesPerPixel + 3) & ~3;
size_t y;
/* Copy image data row by row. */
for (y = 0; y < bitmap->height; ++y) {
uint8_t *rowptr = dest + (y * bytewidth);
size_t x;
for (x = 0; x < bitmap->width; ++x) {
MMRGBColor *color = MMRGBColorRefAtPoint(bitmap, x, y);
/* BMP files are stored in BGR format. */
rowptr[0] = color->blue;
rowptr[1] = color->green;
rowptr[2] = color->red;
rowptr += bitmap->bytesPerPixel;
}
}
}
}
/* Perform an in-place swap from Quadrant 1 to Quadrant III format (upside-down
* PostScript/GL to right side up QD/CG raster format) We do this in-place,
* which requires more copying, but will touch only half the pages.
*
* This is blatantly copied from Apple's glGrab example code. */
void flipBitmapData(void *data, size_t width, size_t height, size_t bytewidth)
{
size_t top, bottom;
void *topP;
void *bottomP;
void *tempbuf;
if (height <= 1) return; /* No flipping necessary if height is <= 1. */
top = 0;
bottom = height - 1;
tempbuf = malloc(bytewidth);
if (tempbuf == NULL) return;
while (top < bottom) {
topP = (void *)((top * bytewidth) + (intptr_t)data);
bottomP = (void *)((bottom * bytewidth) + (intptr_t)data);
/* Save and swap scanlines.
* Does a simple in-place exchange with a temp buffer. */
memcpy(tempbuf, topP, bytewidth);
memcpy(topP, bottomP, bytewidth);
memcpy(bottomP, tempbuf, bytewidth);
++top;
--bottom;
}
free(tempbuf);
}