-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbmpfile.c
More file actions
744 lines (628 loc) · 18.5 KB
/
Copy pathbmpfile.c
File metadata and controls
744 lines (628 loc) · 18.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
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
/**
* @file bmpfile.c
* @brief The BMP library implementation
*
* libbmp - BMP library
* Copyright (C) 2009 lidaibin(超越无限)
* mail: lidaibin@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* $Id$
*/
/*
* BMP File Header Stores general information about the BMP file.
* DIB header Stores detailed information about the bitmap image.
* Color Palette Stores the definition of the colors being used for
* indexed color bitmaps.
* Bitmap Data Stores the actual image, pixel by pixel.
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bmpfile.h"
#define DEFAULT_DPI_X 3780
#define DEFAULT_DPI_Y 3780
#define DPI_FACTOR 39.37007874015748
typedef struct {
uint32_t filesz; /* the size of the BMP file in bytes */
uint16_t creator1; /* reserved. */
uint16_t creator2; /* reserved. */
uint32_t offset; /* the offset, i.e. starting address,
of the byte where the bitmap data can be found. */
} bmp_header_t;
typedef struct {
uint32_t header_sz; /* the size of this header (40 bytes) */
uint32_t width; /* the bitmap width in pixels */
uint32_t height; /* the bitmap height in pixels */
uint16_t nplanes; /* the number of color planes being used.
Must be set to 1. */
uint16_t depth; /* the number of bits per pixel,
which is the color depth of the image.
Typical values are 1, 4, 8, 16, 24 and 32. */
uint32_t compress_type; /* the compression method being used.
See also bmp_compression_method_t. */
uint32_t bmp_bytesz; /* the image size. This is the size of the raw bitmap
data (see below), and should not be confused
with the file size. */
uint32_t hres; /* the horizontal resolution of the image.
(pixel per meter) */
uint32_t vres; /* the vertical resolution of the image.
(pixel per meter) */
uint32_t ncolors; /* the number of colors in the color palette,
or 0 to default to 2<sup><i>n</i></sup>. */
uint32_t nimpcolors; /* the number of important colors used,
or 0 when every color is important;
generally ignored. */
} bmp_dib_v3_header_t;
struct _bmpfile {
uint8_t magic[2]; /* the magic number used to identify the BMP file:
0x42 0x4D (Hex code points for B and M).
The following entries are possible:
BM - Windows 3.1x, 95, NT, ... etc
BA - OS/2 Bitmap Array
CI - OS/2 Color Icon
CP - OS/2 Color Pointer
IC - OS/2 Icon
PT - OS/2 Pointer. */
bmp_header_t header;
bmp_dib_v3_header_t dib;
rgb_pixel_t **pixels;
rgb_pixel_t *colors;
};
static uint32_t
uint32_pow(uint32_t base, uint32_t depth)
{
uint32_t i, result = 1;
for (i = 0; i < depth; ++i)
result *= base;
return result;
}
/**
* Create the standard color table for BMP object
*/
static void
bmp_create_standard_color_table(bmpfile_t *bmp)
{
int i, j, k, ell;
switch (bmp->dib.depth) {
case 1:
for (i = 0; i < 2; ++i) {
bmp->colors[i].red = i * 255;
bmp->colors[i].green = i * 255;
bmp->colors[i].blue = i * 255;
bmp->colors[i].alpha = 0;
}
break;
case 4:
i = 0;
for (ell = 0; ell < 2; ++ell) {
for (k = 0; k < 2; ++k) {
for (j = 0; j < 2; ++j) {
bmp->colors[i].red = j * 128;
bmp->colors[i].green = k * 128;
bmp->colors[i].blue = ell * 128;
bmp->colors[i].alpha = 0;
++i;
}
}
}
for (ell = 0; ell < 2; ++ell) {
for (k = 0; k < 2; ++k) {
for (j = 0; j < 2; ++j) {
bmp->colors[i].red = j * 255;
bmp->colors[i].green = k * 255;
bmp->colors[i].blue = ell * 255;
bmp->colors[i].alpha = 0;
++i;
}
}
}
i = 8;
bmp->colors[i].red = 192;
bmp->colors[i].green = 192;
bmp->colors[i].blue = 192;
bmp->colors[i].alpha = 0;
break;
case 8:
i = 0;
for (ell = 0; ell < 4; ++ell) {
for (k = 0; k < 8; ++k) {
for (j = 0; j < 8; ++j) {
bmp->colors[i].red = j * 32;
bmp->colors[i].green = k * 32;
bmp->colors[i].blue = ell * 64;
bmp->colors[i].alpha = 0;
++i;
}
}
}
i = 0;
for (ell = 0; ell < 2; ++ell) {
for (k = 0; k < 2; ++k) {
for (j = 0; j < 2; ++j) {
bmp->colors[i].red = j * 128;
bmp->colors[i].green = k * 128;
bmp->colors[i].blue = ell * 128;
++i;
}
}
}
// overwrite colors 7, 8, 9
i = 7;
bmp->colors[i].red = 192;
bmp->colors[i].green = 192;
bmp->colors[i].blue = 192;
i++; // 8
bmp->colors[i].red = 192;
bmp->colors[i].green = 220;
bmp->colors[i].blue = 192;
i++; // 9
bmp->colors[i].red = 166;
bmp->colors[i].green = 202;
bmp->colors[i].blue = 240;
// overwrite colors 246 to 255
i = 246;
bmp->colors[i].red = 255;
bmp->colors[i].green = 251;
bmp->colors[i].blue = 240;
i++; // 247
bmp->colors[i].red = 160;
bmp->colors[i].green = 160;
bmp->colors[i].blue = 164;
i++; // 248
bmp->colors[i].red = 128;
bmp->colors[i].green = 128;
bmp->colors[i].blue = 128;
i++; // 249
bmp->colors[i].red = 255;
bmp->colors[i].green = 0;
bmp->colors[i].blue = 0;
i++; // 250
bmp->colors[i].red = 0;
bmp->colors[i].green = 255;
bmp->colors[i].blue = 0;
i++; // 251
bmp->colors[i].red = 255;
bmp->colors[i].green = 255;
bmp->colors[i].blue = 0;
i++; // 252
bmp->colors[i].red = 0;
bmp->colors[i].green = 0;
bmp->colors[i].blue = 255;
i++; // 253
bmp->colors[i].red = 255;
bmp->colors[i].green = 0;
bmp->colors[i].blue = 255;
i++; // 254
bmp->colors[i].red = 0;
bmp->colors[i].green = 255;
bmp->colors[i].blue = 255;
i++; // 255
bmp->colors[i].red = 255;
bmp->colors[i].green = 255;
bmp->colors[i].blue = 255;
break;
}
}
/**
* Create grayscale color table for BMP object
*/
static void
bmp_create_grayscale_color_table(bmpfile_t *bmp)
{
int i;
uint8_t step_size;
if (!bmp->colors) return;
if (bmp->dib.depth != 1)
step_size = 255 / (bmp->dib.ncolors - 1);
else
step_size = 255;
for (i = 0; i < bmp->dib.ncolors; ++i) {
uint8_t value = i * step_size;
rgb_pixel_t color = {value, value, value, 0};
bmp->colors[i] = color;
}
}
/**
* Malloc the memory for color palette
*/
static void
bmp_malloc_colors(bmpfile_t *bmp)
{
bmp->dib.ncolors = uint32_pow(2, bmp->dib.depth);
if (bmp->dib.depth == 1 || bmp->dib.depth == 4 || bmp->dib.depth == 8) {
bmp->colors = (rgb_pixel_t*) malloc(sizeof(rgb_pixel_t) * bmp->dib.ncolors);
bmp_create_standard_color_table(bmp);
}
}
/**
* Free the memory of color palette
*/
static void
bmp_free_colors(bmpfile_t *bmp)
{
if (bmp->colors)
free(bmp->colors);
}
/**
* Malloc the memory for pixels
*/
static void
bmp_malloc_pixels(bmpfile_t *bmp)
{
int i, j;
bmp->pixels = (rgb_pixel_t**)malloc(sizeof(rgb_pixel_t *) * bmp->dib.width);
for (i = 0; i < bmp->dib.width; ++i) {
bmp->pixels[i] = (rgb_pixel_t*)malloc(sizeof(rgb_pixel_t) * bmp->dib.height);
for (j = 0; j < bmp->dib.height; ++j) {
bmp->pixels[i][j].red = 255;
bmp->pixels[i][j].green = 255;
bmp->pixels[i][j].blue = 255;
bmp->pixels[i][j].alpha = 0;
}
}
}
/**
* Free the memory of pixels
*/
static void
bmp_free_pixels(bmpfile_t *bmp)
{
int i;
for (i = 0; i < bmp->dib.width; ++i)
free(bmp->pixels[i]);
free(bmp->pixels), bmp->pixels = NULL;
}
/**
* Create the BMP object with specified width and height and depth.
*/
bmpfile_t *
bmp_create(uint32_t width, uint32_t height, uint32_t depth)
{
bmpfile_t *result;
if (depth != 1 && depth != 4 && depth != 8 && depth != 16 && depth != 24 &&
depth != 32)
return NULL;
result = (bmpfile_t*)malloc(sizeof(bmpfile_t));
memset(result, 0, sizeof(bmpfile_t));
result->magic[0] = 'B';
result->magic[1] = 'M';
result->dib.header_sz = 40;
result->dib.width = width;
result->dib.height = height;
result->dib.nplanes = 1;
result->dib.depth = depth;
result->dib.hres = DEFAULT_DPI_X;
result->dib.vres = DEFAULT_DPI_Y;
if (depth == 16)
result->dib.compress_type = BI_BITFIELDS;
else
result->dib.compress_type = BI_RGB;
bmp_malloc_pixels(result);
bmp_malloc_colors(result);
return result;
}
void
bmp_destroy(bmpfile_t *bmp)
{
bmp_free_pixels(bmp);
bmp_free_colors(bmp);
free(bmp);
}
uint32_t
bmp_get_width(bmpfile_t *bmp)
{
return bmp->dib.width;
}
uint32_t
bmp_get_height(bmpfile_t *bmp)
{
return bmp->dib.height;
}
uint32_t
bmp_get_depth(bmpfile_t *bmp)
{
return bmp->dib.depth;
}
bmp_compression_method_t
bmp_get_compression_method(bmpfile_t *bmp)
{
return (bmp_compression_method_t)bmp->dib.compress_type;
}
void
bmp_set_compression_method(bmpfile_t *bmp, bmp_compression_method_t t)
{
bmp->dib.compress_type = t;
}
uint32_t
bmp_get_dpi_x(bmpfile_t *bmp)
{
return (uint32_t)(bmp->dib.hres / DPI_FACTOR);
}
uint32_t
bmp_get_dpi_y(bmpfile_t *bmp)
{
return (uint32_t)(bmp->dib.vres / DPI_FACTOR);
}
void
bmp_set_dpi(bmpfile_t *bmp, uint32_t x, uint32_t y)
{
bmp->dib.hres = (uint32_t)(x * DPI_FACTOR);
bmp->dib.vres = (uint32_t)(y * DPI_FACTOR);
}
rgb_pixel_t
bmp_get_pixel(bmpfile_t *bmp, uint32_t x, uint32_t y)
{
//if ((x >= bmp->dib.width) || (y >= bmp->dib.height))
//return 0;
return bmp->pixels[x][y];
}
int
bmp_set_pixel(bmpfile_t *bmp, uint32_t x, uint32_t y, rgb_pixel_t pixel)
{
if ((x >= bmp->dib.width) || (y >= bmp->dib.height))
return 0;
bmp->pixels[x][y] = pixel;
return 1;
}
static int
_is_big_endian(void)
{
uint16_t value = 0x0001;
return (*(uint8_t *)&value) != 0x01;
}
#define UINT16_SWAP_LE_BE_CONSTANT(val) \
((uint16_t) \
( \
(uint16_t) ((uint16_t) (val) >> 8) | \
(uint16_t) ((uint16_t) (val) << 8)))
#define UINT32_SWAP_LE_BE_CONSTANT(val) \
((uint32_t) \
( \
(((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
(((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
(((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
(((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
static void
bmp_header_swap_endianess(bmp_header_t *header)
{
header->filesz = UINT32_SWAP_LE_BE_CONSTANT(header->filesz);
header->creator1 = UINT16_SWAP_LE_BE_CONSTANT(header->creator1);
header->creator2 = UINT16_SWAP_LE_BE_CONSTANT(header->creator2);
header->offset = UINT32_SWAP_LE_BE_CONSTANT(header->offset);
}
static void
bmp_dib_v3_header_swap_endianess(bmp_dib_v3_header_t *dib)
{
dib->header_sz = UINT32_SWAP_LE_BE_CONSTANT(dib->header_sz);
dib->width = UINT32_SWAP_LE_BE_CONSTANT(dib->width);
dib->height = UINT32_SWAP_LE_BE_CONSTANT(dib->height);
dib->nplanes = UINT16_SWAP_LE_BE_CONSTANT(dib->nplanes);
dib->depth = UINT16_SWAP_LE_BE_CONSTANT(dib->depth);
dib->compress_type = UINT32_SWAP_LE_BE_CONSTANT(dib->compress_type);
dib->bmp_bytesz = UINT32_SWAP_LE_BE_CONSTANT(dib->bmp_bytesz);
dib->hres = UINT32_SWAP_LE_BE_CONSTANT(dib->hres);
dib->vres = UINT32_SWAP_LE_BE_CONSTANT(dib->vres);
dib->ncolors = UINT32_SWAP_LE_BE_CONSTANT(dib->ncolors);
dib->nimpcolors = UINT32_SWAP_LE_BE_CONSTANT(dib->nimpcolors);
}
static void
bmp_write_header(bmpfile_t *bmp, FILE *fp)
{
bmp_header_t header = bmp->header;
if (_is_big_endian()) bmp_header_swap_endianess(&header);
fwrite(bmp->magic, sizeof(bmp->magic), 1, fp);
fwrite(&(header.filesz), sizeof(uint32_t), 1, fp);
fwrite(&(header.creator1), sizeof(uint16_t), 1, fp);
fwrite(&(header.creator2), sizeof(uint16_t), 1, fp);
fwrite(&(header.offset), sizeof(uint32_t), 1, fp);
}
static void
bmp_write_dib(bmpfile_t *bmp, FILE *fp)
{
bmp_dib_v3_header_t dib = bmp->dib;
if (_is_big_endian()) bmp_dib_v3_header_swap_endianess(&dib);
fwrite(&(dib.header_sz), sizeof(uint32_t), 1, fp);
fwrite(&(dib.width), sizeof(uint32_t), 1, fp);
fwrite(&(dib.height), sizeof(uint32_t), 1, fp);
fwrite(&(dib.nplanes), sizeof(uint16_t), 1, fp);
fwrite(&(dib.depth), sizeof(uint16_t), 1, fp);
fwrite(&(dib.compress_type), sizeof(uint32_t), 1, fp);
fwrite(&(dib.bmp_bytesz), sizeof(uint32_t), 1, fp);
fwrite(&(dib.hres), sizeof(uint32_t), 1, fp);
fwrite(&(dib.vres), sizeof(uint32_t), 1, fp);
fwrite(&(dib.ncolors), sizeof(uint32_t), 1, fp);
fwrite(&(dib.nimpcolors), sizeof(uint32_t), 1, fp);
}
static void
bmp_write_palette(bmpfile_t *bmp, FILE *fp)
{
if (bmp->dib.depth == 1 || bmp->dib.depth == 4 || bmp->dib.depth == 8) {
int i;
for (i = 0; i < bmp->dib.ncolors; ++i)
fwrite(&(bmp->colors[i]), sizeof(rgb_pixel_t), 1, fp);
}
else if (bmp->dib.depth == 16) { /* the bit masks, not palette */
uint16_t red_mask = 63488; /* bits 1-5 */
uint16_t green_mask = 2016; /* bits 6-11 */
uint16_t blue_mask = 31; /* bits 12-16 */
uint16_t zero_word = 0;
if (_is_big_endian()) {
red_mask = UINT16_SWAP_LE_BE_CONSTANT(red_mask);
green_mask = UINT16_SWAP_LE_BE_CONSTANT(green_mask);
blue_mask = UINT16_SWAP_LE_BE_CONSTANT(blue_mask);
}
fwrite(&red_mask, sizeof(uint16_t), 1, fp);
fwrite(&zero_word, sizeof(uint16_t), 1, fp);
fwrite(&green_mask, sizeof(uint16_t), 1, fp);
fwrite(&zero_word, sizeof(uint16_t), 1, fp);
fwrite(&blue_mask, sizeof(uint16_t), 1, fp);
fwrite(&zero_word, sizeof(uint16_t), 1, fp);
}
}
#define INT_SQUARE(v) ((int)((v) * (v)))
static int
find_closest_color(bmpfile_t *bmp, rgb_pixel_t pixel)
{
int i, best = 0;
int best_match = 999999;
for (i = 0; i < bmp->dib.ncolors; ++i) {
rgb_pixel_t color = bmp->colors[i];
int temp_match = INT_SQUARE(color.red - pixel.red) +
INT_SQUARE(color.green - pixel.green) +
INT_SQUARE(color.blue - pixel.blue);
if (temp_match < best_match) {
best = i;
best_match = temp_match;
}
if (best_match < 1)
break;
}
return best;
}
static void
bmp_get_row_data_for_1(bmpfile_t *bmp, unsigned char *buf, size_t buf_len,
uint32_t row)
{
uint8_t pos_weights[8] = {128, 64, 32, 16, 8, 4, 2, 1};
uint32_t i = 0, j, k = 0;
uint32_t index;
if (bmp->dib.width > 8 * buf_len) return;
while (i < bmp->dib.width) {
for (j = 0, index = 0; j < 8 && i < bmp->dib.width; ++i, ++j)
index += pos_weights[j] * find_closest_color(bmp, bmp->pixels[i][row]);
buf[k++] = index & 0xff;
}
}
static void
bmp_get_row_data_for_4(bmpfile_t *bmp, unsigned char *buf, size_t buf_len,
uint32_t row)
{
uint8_t pos_weights[2] = {16, 1};
uint32_t i = 0, j, k = 0;
uint32_t index;
if (bmp->dib.width > 2 * buf_len) return;
while (i < bmp->dib.width) {
for (j = 0, index = 0; j < 2 && i < bmp->dib.width; ++i, ++j)
index += pos_weights[j] * find_closest_color(bmp, bmp->pixels[i][row]);
buf[k++] = index & 0xff;
}
}
static void
bmp_get_row_data_for_8(bmpfile_t *bmp, unsigned char *buf, size_t buf_len,
uint32_t row)
{
int i;
if (bmp->dib.width > buf_len) return;
for (i = 0; i < bmp->dib.width; ++i)
buf[i] = find_closest_color(bmp, bmp->pixels[i][row]);
}
static void
bmp_get_row_data_for_24(bmpfile_t *bmp, unsigned char *buf, size_t buf_len,
uint32_t row)
{
int i;
if (bmp->dib.width * 3 > buf_len) return;
for (i = 0; i < bmp->dib.width; ++i)
memcpy(buf + 3 * i, (uint8_t *)&(bmp->pixels[i][row]), 3);
}
static void
bmp_get_row_data_for_32(bmpfile_t *bmp, unsigned char *buf, size_t buf_len,
uint32_t row)
{
int i;
if (bmp->dib.width * 4 > buf_len) return;
for (i = 0; i < bmp->dib.width; ++i)
memcpy(buf + 4 * i, (uint8_t *)&(bmp->pixels[i][row]), 4);
}
int
bmp_save(bmpfile_t *bmp, const char *filename)
{
FILE *fp;
double bytes_per_pixel;
uint32_t bytes_per_line;
uint32_t palette_size;
int row;
unsigned char *buf;
/* Create the file */
if ((fp = fopen(filename, "wb")) == NULL)
return 0;
/* Calculate the field value of header and DIB */
bytes_per_pixel = (bmp->dib.depth * 1.0) / 8.0;
bytes_per_line = (int)ceil(bytes_per_pixel * bmp->dib.width);
if (bytes_per_line % 4 != 0)
bytes_per_line += 4 - bytes_per_line % 4;
bmp->dib.bmp_bytesz = bytes_per_line * bmp->dib.height;
palette_size = 0;
if (bmp->dib.depth == 1 || bmp->dib.depth == 4 || bmp->dib.depth == 8)
palette_size = uint32_pow(2, bmp->dib.depth) * 4;
else if (bmp->dib.depth == 16)
palette_size = 3 * 4;
bmp->header.offset = 14 + bmp->dib.header_sz + palette_size;
bmp->header.filesz = bmp->header.offset + bmp->dib.bmp_bytesz;
/* Write the file */
bmp_write_header(bmp, fp);
bmp_write_dib(bmp, fp);
bmp_write_palette(bmp, fp);
if (bmp->dib.depth == 16) {
uint32_t data_bytes = bmp->dib.width * 2;
uint32_t padding_bytes = 4 - data_bytes % 4;
for (row = bmp->dib.height - 1; row >= 0; --row) {
int i;
unsigned char zero_byte = 0;
uint32_t write_number = 0;
for (i = 0; write_number < data_bytes; ++i, write_number += 2) {
uint16_t red = (uint16_t)(bmp->pixels[i][row].red / 8);
uint16_t green = (uint16_t)(bmp->pixels[i][row].green / 4);
uint16_t blue = (uint16_t)(bmp->pixels[i][row].blue / 8);
uint16_t value = (red << 11) + (green << 5) + blue;
if (_is_big_endian()) value = UINT16_SWAP_LE_BE_CONSTANT(value);
fwrite(&value, sizeof(uint16_t), 1, fp);
}
for (write_number = 0; write_number < padding_bytes; ++write_number)
fwrite(&zero_byte, 1, 1, fp);
}
}
else {
buf = (unsigned char *)malloc(bytes_per_line);
for (row = bmp->dib.height - 1; row >= 0; --row) {
memset(buf, 0, bytes_per_line);
switch (bmp->dib.depth) {
case 1:
bmp_get_row_data_for_1(bmp, buf, bytes_per_line, row);
break;
case 4:
bmp_get_row_data_for_4(bmp, buf, bytes_per_line, row);
break;
case 8:
bmp_get_row_data_for_8(bmp, buf, bytes_per_line, row);
break;
case 24:
bmp_get_row_data_for_24(bmp, buf, bytes_per_line, row);
break;
case 32:
bmp_get_row_data_for_32(bmp, buf, bytes_per_line, row);
break;
}
fwrite(buf, bytes_per_line, 1, fp);
}
free(buf);
}
fclose(fp);
return 1;
}