This repository has been archived by the owner on Apr 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap.c
667 lines (583 loc) · 29.8 KB
/
bitmap.c
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
//
// flare16x core
// Developed 2019 by Benedikt Muessig <[email protected]>
// Licensed under GPLv3
//
// bitmap.c: Lightweight bitmap read/write implementation
//
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "error.h"
#include "canvas.h"
#include "bitmap.h"
// Creates a new 16-bit RGB565 bitmap, allocates the required memory and fills in the values
// Will overwrite any existing state and WILL leak memory if a previous state is re-used
flare16x_error flare16x_bitmap_create16(uint16_t width, uint16_t height, flare16x_bitmap* bitmap)
{
// Make sure the bitmap is not null
if (bitmap == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Verify width and height
if (width == 0 || height == 0 || height * width > FLARE16X_BITMAP_MAX_PIXELS)
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_BITMAP);
// Clear the target struct
memset(bitmap, 0, sizeof(flare16x_bitmap));
// Calculate the stride
bitmap->stride = ((((width * 16) + 31) & ~31) >> 3);
// Calculate the required memory
bitmap->dib_size = sizeof(flare16x_bitmap_dib);
bitmap->mask_size = sizeof(flare16x_bitmap_mask);
bitmap->pixels_size = height * bitmap->stride;
size_t total_size = bitmap->dib_size + bitmap->mask_size + bitmap->pixels_size + sizeof(flare16x_bitmap_header);
// Allocate the required memory and clear it
bitmap->header = malloc(sizeof(flare16x_bitmap_header));
if (bitmap->header == NULL)
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
memset(bitmap->header, 0, sizeof(flare16x_bitmap_header));
bitmap->dib = malloc(bitmap->dib_size + bitmap->mask_size);
bitmap->mask = (void*)bitmap->dib + bitmap->dib_size;
if (bitmap->dib == NULL)
{
free(bitmap->header);
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
memset(bitmap->dib, 0, bitmap->dib_size + bitmap->mask_size);
bitmap->pixels = malloc(bitmap->pixels_size);
if (bitmap->pixels == NULL)
{
free(bitmap->dib);
free(bitmap->header);
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
memset(bitmap->pixels, 0, bitmap->pixels_size);
// Next, fill in the header struct
bitmap->header->magic = FLARE16X_BITMAP_HEADER_MAGIC;
bitmap->header->reserved = FLARE16X_BITMAP_HEADER_RESERVED;
bitmap->header->file_size = total_size;
bitmap->header->payload_offset = bitmap->dib_size + bitmap->mask_size + sizeof(flare16x_bitmap_header);
// Followed by the DIB struct
bitmap->dib->height = -height;
bitmap->dib->width = width;
bitmap->dib->compression = FLARE16X_BITMAP_COMPRESSION_BITFIELDS;
bitmap->dib->bit_count = 16;
bitmap->dib->planes = 1;
bitmap->dib->size = bitmap->dib_size;
bitmap->dib->size_image = bitmap->pixels_size;
bitmap->dib->colors_important = 0;
bitmap->dib->colors_used = 0;
bitmap->dib->hor_px_per_meter = 0;
bitmap->dib->ver_px_per_meter = 0;
// And followed by the mask struct
bitmap->mask->mask_red = 0xf800;
bitmap->mask->mask_green = 0x7e0;
bitmap->mask->mask_blue = 0x1f;
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Creates a new 24-bit RGB888 bitmap, allocates the required memory and fills in the values
// Will overwrite any existing state and WILL leak memory if a previous state is re-used
flare16x_error flare16x_bitmap_create24(uint16_t width, uint16_t height, flare16x_bitmap* bitmap)
{
// Make sure the bitmap is not null
if (bitmap == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Verify width and height
if (width == 0 || height == 0 || height * width > FLARE16X_BITMAP_MAX_PIXELS)
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_BITMAP);
// Clear the target struct
memset(bitmap, 0, sizeof(flare16x_bitmap));
// Calculate the stride
bitmap->stride = ((((width * 24) + 31) & ~31) >> 3);
// Calculate the required memory
bitmap->dib_size = sizeof(flare16x_bitmap_dib);
bitmap->mask_size = 0;
bitmap->pixels_size = height * bitmap->stride;
size_t total_size = bitmap->dib_size + bitmap->pixels_size + sizeof(flare16x_bitmap_header);
// Allocate the required memory and clear it
bitmap->header = malloc(sizeof(flare16x_bitmap_header));
if (bitmap->header == NULL)
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
memset(bitmap->header, 0, sizeof(flare16x_bitmap_header));
bitmap->dib = malloc(bitmap->dib_size);
bitmap->mask = NULL;
if (bitmap->dib == NULL)
{
free(bitmap->header);
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
memset(bitmap->dib, 0, bitmap->dib_size + bitmap->mask_size);
bitmap->pixels = malloc(bitmap->pixels_size);
if (bitmap->pixels == NULL)
{
free(bitmap->dib);
free(bitmap->header);
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
memset(bitmap->pixels, 0, bitmap->pixels_size);
// Next, fill in the header struct
bitmap->header->magic = FLARE16X_BITMAP_HEADER_MAGIC;
bitmap->header->reserved = FLARE16X_BITMAP_HEADER_RESERVED;
bitmap->header->file_size = total_size;
bitmap->header->payload_offset = bitmap->dib_size + sizeof(flare16x_bitmap_header);
// Followed by the DIB struct
bitmap->dib->height = -height;
bitmap->dib->width = width;
bitmap->dib->compression = FLARE16X_BITMAP_COMPRESSION_RGB;
bitmap->dib->bit_count = 24;
bitmap->dib->planes = 1;
bitmap->dib->size = bitmap->dib_size;
bitmap->dib->size_image = bitmap->pixels_size;
bitmap->dib->colors_important = 0;
bitmap->dib->colors_used = 0;
bitmap->dib->hor_px_per_meter = 0;
bitmap->dib->ver_px_per_meter = 0;
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Creates a new 32-bit RGBA8888 bitmap, allocates the required memory and fills in the values
// Will overwrite any existing state and WILL leak memory if a previous state is re-used
flare16x_error flare16x_bitmap_create32(uint16_t width, uint16_t height, flare16x_bitmap* bitmap)
{
// Make sure the bitmap is not null
if (bitmap == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Verify width and height
if (width == 0 || height == 0 || height * width > FLARE16X_BITMAP_MAX_PIXELS)
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_BITMAP);
// Clear the target struct
memset(bitmap, 0, sizeof(flare16x_bitmap));
// Calculate the stride
bitmap->stride = ((((width * 32) + 31) & ~31) >> 3);
// Calculate the required memory
bitmap->dib_size = sizeof(flare16x_bitmap_dib);
bitmap->mask_size = 0;
bitmap->pixels_size = height * bitmap->stride;
size_t total_size = bitmap->dib_size + bitmap->pixels_size + sizeof(flare16x_bitmap_header);
// Allocate the required memory and clear it
bitmap->header = malloc(sizeof(flare16x_bitmap_header));
if (bitmap->header == NULL)
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
memset(bitmap->header, 0, sizeof(flare16x_bitmap_header));
bitmap->dib = malloc(bitmap->dib_size);
bitmap->mask = NULL;
if (bitmap->dib == NULL)
{
free(bitmap->header);
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
memset(bitmap->dib, 0, bitmap->dib_size + bitmap->mask_size);
bitmap->pixels = malloc(bitmap->pixels_size);
if (bitmap->pixels == NULL)
{
free(bitmap->dib);
free(bitmap->header);
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
memset(bitmap->pixels, 0, bitmap->pixels_size);
// Next, fill in the header struct
bitmap->header->magic = FLARE16X_BITMAP_HEADER_MAGIC;
bitmap->header->reserved = FLARE16X_BITMAP_HEADER_RESERVED;
bitmap->header->file_size = total_size;
bitmap->header->payload_offset = bitmap->dib_size + sizeof(flare16x_bitmap_header);
// Followed by the DIB struct
bitmap->dib->height = -height;
bitmap->dib->width = width;
bitmap->dib->compression = FLARE16X_BITMAP_COMPRESSION_RGB;
bitmap->dib->bit_count = 32;
bitmap->dib->planes = 1;
bitmap->dib->size = bitmap->dib_size;
bitmap->dib->size_image = bitmap->pixels_size;
bitmap->dib->colors_important = 0;
bitmap->dib->colors_used = 0;
bitmap->dib->hor_px_per_meter = 0;
bitmap->dib->ver_px_per_meter = 0;
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Attempts to load a bitmap from file
// Will overwrite any existing state and WILL leak memory if a previous state is re-used
flare16x_error flare16x_bitmap_load(FILE* bitmap_file, flare16x_bitmap* bitmap_struct)
{
// Make sure that there are no null pointers
if (bitmap_file == NULL || bitmap_struct == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Next, clean up the target struct
memset(bitmap_struct, 0, sizeof(flare16x_bitmap));
// Allocate the memory for the header struct
bitmap_struct->header = malloc(sizeof(flare16x_bitmap_header));
if (bitmap_struct->header == NULL)
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
// Then, read the header from the input file
if (fread(bitmap_struct->header, sizeof(flare16x_bitmap_header), 1, bitmap_file) != 1)
{
// On failure, free the buffer struct again and return failure
free(bitmap_struct->header);
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Now, validate the resulting struct starting with the magic number, reserved field, size and offset
if (bitmap_struct->header->magic != FLARE16X_BITMAP_HEADER_MAGIC ||
bitmap_struct->header->reserved != FLARE16X_BITMAP_HEADER_RESERVED ||
bitmap_struct->header->file_size == 0 ||
(bitmap_struct->header->payload_offset != 0x36 && bitmap_struct->header->payload_offset != 0x42))
{
// On failure, free the buffer struct again and return failure
free(bitmap_struct->header);
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Allocate more memory for the DIB and mask structs
// The data offset minus the size of the file header yields the entire DIB plus optional mask size
size_t dib_mask_size = bitmap_struct->header->payload_offset - sizeof(flare16x_bitmap_header);
bitmap_struct->dib = malloc(dib_mask_size);
if (bitmap_struct->dib == NULL)
{
// On failure, free the buffer struct again and return failure
free(bitmap_struct->header);
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Read the DIB struct from the input file
if (fread(bitmap_struct->dib, dib_mask_size, 1, bitmap_file) != 1)
{
// On failure, free the buffer structs again and return failure
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Verify, that the reported DIB size, planes, bit-count, compression and maximum size matches
if ((bitmap_struct->dib->size + sizeof(flare16x_bitmap_mask) != dib_mask_size &&
bitmap_struct->dib->size != dib_mask_size) || bitmap_struct->dib->planes != 1 ||
bitmap_struct->dib->width <= 0 || bitmap_struct->dib->height == 0 ||
bitmap_struct->dib->width * abs(bitmap_struct->dib->height) > FLARE16X_BITMAP_MAX_PIXELS)
{
// On failure, free the buffer structs again and return failure
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Copy the dib size
bitmap_struct->dib_size = bitmap_struct->dib->size;
// Calculate the stride
bitmap_struct->stride = ((((bitmap_struct->dib->width * bitmap_struct->dib->bit_count) + 31) & ~31) >> 3);
// TODO: Simplify this if-clause and merge the two equal outcomes
// Next, determine the number of bits and calculate the image data size
if (bitmap_struct->dib->bit_count == 16 && bitmap_struct->header->payload_offset == 0x42 &&
bitmap_struct->dib->compression == FLARE16X_BITMAP_COMPRESSION_BITFIELDS)
{
// For 16 bit RGB565 calculate the mask struct pointer
bitmap_struct->mask = (flare16x_bitmap_mask*)((void*)bitmap_struct->dib + bitmap_struct->dib->size);
bitmap_struct->mask_size = dib_mask_size - bitmap_struct->dib->size;
// As well as the pixel size
bitmap_struct->pixels_size = bitmap_struct->stride * abs(bitmap_struct->dib->height);
// And verify the masks
if (bitmap_struct->mask->mask_red != FLARE16X_BITMAP_MASK_RGB565_RED ||
bitmap_struct->mask->mask_green != FLARE16X_BITMAP_MASK_RGB565_GREEN ||
bitmap_struct->mask->mask_blue != FLARE16X_BITMAP_MASK_RGB565_BLUE)
{
// On failure, free the buffer structs again and return failure
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
} else if (bitmap_struct->dib->bit_count == 24 && bitmap_struct->header->payload_offset == 0x36 &&
bitmap_struct->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB)
{
// For 24-bit RGB888 calculate the pixel size
bitmap_struct->pixels_size = bitmap_struct->stride * abs(bitmap_struct->dib->height);
} else if (bitmap_struct->dib->bit_count == 32 && bitmap_struct->header->payload_offset == 0x36 &&
bitmap_struct->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB)
{
// For 32-bit RGBA8888 calculate the pixel size
bitmap_struct->pixels_size = bitmap_struct->stride * abs(bitmap_struct->dib->height);
} else
{
// On failure, free the buffer structs again and return failure
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Since the header worked out well, allocate space for the image data next
bitmap_struct->pixels = malloc(bitmap_struct->pixels_size);
if (bitmap_struct->pixels == NULL)
{
// On failure, free the buffer structs again and return failure
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Now, read the image data
if (fread(bitmap_struct->pixels, bitmap_struct->pixels_size, 1, bitmap_file) != 1)
{
// On failure, free the buffer structs again and return failure
free(bitmap_struct->pixels);
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->pixels = NULL;
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
}
// If the image is a stupid bottom up one, flip the darn thing
if (bitmap_struct->dib->height > 0)
{
// A higher efficiency would be a nice improvement in the future, as C# uses this format (the device does not)
uint8_t* new_pixel_data = malloc(bitmap_struct->pixels_size);
if (new_pixel_data == NULL)
{
free(bitmap_struct->pixels);
free(bitmap_struct->dib);
free(bitmap_struct->header);
bitmap_struct->pixels = NULL;
bitmap_struct->dib = NULL;
bitmap_struct->header = NULL;
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Reorder the data line by line from bottom to top
int y;
for (y = 0; y < abs(bitmap_struct->dib->height); y++)
memcpy(new_pixel_data + (y * bitmap_struct->stride),
bitmap_struct->pixels + ((abs(bitmap_struct->dib->height) - y - 1) * bitmap_struct->stride),
bitmap_struct->stride);
// Free the old buffer
free(bitmap_struct->pixels);
// And assign the new buffer
bitmap_struct->pixels = (void*)new_pixel_data;
// Finally flip the sign of the height
bitmap_struct->dib->height = -bitmap_struct->dib->height;
}
// As everything worked out, return success
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Attempts to store a bitmap to file
flare16x_error flare16x_bitmap_store(flare16x_bitmap* bitmap_struct, FILE* bitmap_file)
{
// Make sure that there are no null pointers
if (bitmap_struct == NULL || bitmap_file == NULL || bitmap_struct->header == NULL || bitmap_struct->dib == NULL ||
bitmap_struct->pixels == NULL || (bitmap_struct->mask_size > 0 && bitmap_struct->mask == NULL))
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Validate the format
// Note that this requires short-circuiting to work properly!
if (bitmap_struct->dib_size == 0 ||
bitmap_struct->pixels_size == 0 || bitmap_struct->dib->width <= 0 || bitmap_struct->dib->height >= 0 ||
bitmap_struct->dib->height * bitmap_struct->dib->width > FLARE16X_BITMAP_MAX_PIXELS || bitmap_struct->stride == 0 ||
bitmap_struct->dib->planes != 1 || (bitmap_struct->dib->bit_count != 16 && bitmap_struct->dib->bit_count != 24 &&
bitmap_struct->dib->bit_count != 32) || (bitmap_struct->dib->compression != FLARE16X_BITMAP_COMPRESSION_RGB &&
bitmap_struct->dib->compression != FLARE16X_BITMAP_COMPRESSION_BITFIELDS) ||
(bitmap_struct->dib->compression == FLARE16X_BITMAP_COMPRESSION_BITFIELDS &&
bitmap_struct->dib->bit_count != 16) || (bitmap_struct->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB &&
!(bitmap_struct->dib->bit_count == 24 || bitmap_struct->dib->bit_count == 32)))
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
// First, write the header struct
if (fwrite(bitmap_struct->header, sizeof(flare16x_bitmap_header), 1, bitmap_file) != 1)
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
// Then, write the DIB struct
if (fwrite(bitmap_struct->dib, bitmap_struct->dib_size, 1, bitmap_file) != 1)
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
// If desired, write the mask struct
if (bitmap_struct->mask_size > 0 && bitmap_struct->dib->compression == FLARE16X_BITMAP_COMPRESSION_BITFIELDS)
if (fwrite(bitmap_struct->mask, bitmap_struct->mask_size, 1, bitmap_file) != 1)
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
// Finally, write the pixel data
if (fwrite(bitmap_struct->pixels, bitmap_struct->pixels_size, 1, bitmap_file) != 1)
return flare16x_error_make(FLARE16X_ERROR_IO, FLARE16X_ERROR_SOURCE_BITMAP);
// And, it's done!
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Destroys an existing bitmap context and frees the resources
flare16x_error flare16x_bitmap_destroy(flare16x_bitmap* bitmap)
{
// Make sure the bitmap is no null pointer
if (bitmap == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Free all allocated memory
free(bitmap->header);
free(bitmap->dib);
free(bitmap->pixels);
// And zero the struct to get rid of all pointers and state
memset(bitmap, 0, sizeof(flare16x_bitmap));
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Copies a region from a bitmap buffer to a canvas buffer
// Will overwrite any existing canvas state and WILL leak memory if a previous state is re-used
flare16x_error flare16x_bitmap_edit(flare16x_bitmap* bitmap, uint16_t offset_x, uint16_t offset_y,
uint16_t width, uint16_t height, flare16x_canvas* canvas)
{
// Make sure that there are no null pointers
if (bitmap == NULL || canvas == NULL || bitmap->dib == NULL || bitmap->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Validate the format
// Note that this requires short-circuiting to work properly!
if (bitmap->dib_size == 0 || bitmap->pixels_size == 0 || bitmap->dib->width <= 0 ||
bitmap->dib->height >= 0 || bitmap->dib->planes != 1 || bitmap->stride == 0 ||
(-bitmap->dib->height) * bitmap->dib->width > FLARE16X_BITMAP_MAX_PIXELS)
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
// Validate the width and height
if (width == 0 || height == 0 || width + offset_x > bitmap->dib->width ||
height + offset_y > (-bitmap->dib->height))
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_BITMAP);
// Copy width and height
canvas->width = width;
canvas->height = height;
// Next, determine the size of the canvas buffer and allocate it
size_t canvas_size = width * height * sizeof(uint16_t);
canvas->pixels = malloc(canvas_size);
if (canvas->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_MALLOC, FLARE16X_ERROR_SOURCE_BITMAP);
// Now, check the format and copy and convert the pixel data to RGB565
if (bitmap->dib->bit_count == 16 && bitmap->dib->compression == FLARE16X_BITMAP_COMPRESSION_BITFIELDS)
{
// RGB565 just requires a copy operation
// For good measure, verify the masks
if (bitmap->mask->mask_red != FLARE16X_BITMAP_MASK_RGB565_RED ||
bitmap->mask->mask_green != FLARE16X_BITMAP_MASK_RGB565_GREEN ||
bitmap->mask->mask_blue != FLARE16X_BITMAP_MASK_RGB565_BLUE)
{
free(canvas->pixels);
canvas->pixels = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Now, copy the pixels line by line, pixel by pixel without changing them
int x, y;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
canvas->pixels[y * width + x] = bitmap->pixels565[(y + offset_y) *
(bitmap->stride / sizeof(uint16_t)) + x + offset_x];
} else if (bitmap->dib->bit_count == 24 && bitmap->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB)
{
// RGB888 requires reducing the resolution and remapping the image data
// Now, process the pixels line by line, pixel by pixel
int x, y;
uint8_t r, g, b;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
// Fetch the r, g and b components and truncate them to RGB565
r = bitmap->pixels[(y + offset_y) * bitmap->stride + (x + offset_x) * 4] >> 3; // R5
g = bitmap->pixels[(y + offset_y) * bitmap->stride + (x + offset_x) * 4 + 1] >> 2; // G6
b = bitmap->pixels[(y + offset_y) * bitmap->stride + (x + offset_x) * 4 + 2] >> 3; // B5
// Assemble the new RGB565 pixel
canvas->pixels[y * width + x] = r << 11 | g << 5 | b;
}
} else if (bitmap->dib->bit_count == 32 && bitmap->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB)
{
// RGBA8888 requires reducing the resolution, discarding the alpha channel and remapping the image data
// Now, process the pixels line by line, pixel by pixel
int x, y;
uint8_t r, g, b;
uint32_t p;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
{
// Fetch the whole pixel
p = bitmap->pixels8888[(y + offset_y) * (bitmap->stride / sizeof(uint32_t)) + x + offset_x];
// Mask the pixel, split it into it's components components and truncate them to RGB565
r = (p & 0x00ff0000ul) >> (3 + (8*2)); // R5
g = (p & 0x0000ff00ul) >> (2 + 8); // G6
b = (p & 0x000000fful) >> 3; // B5
// Assemble the new RGB565 pixel
canvas->pixels[y * width + x] = (r & 0x1fu) << 11 | (g & 0x3fu) << 5 | (b & 0x1fu);
}
} else
{
free(canvas->pixels);
canvas->pixels = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// And, it's done!
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Copies a region from a canvas buffer to a bitmap buffer
flare16x_error flare16x_bitmap_merge(flare16x_canvas* canvas, uint16_t offset_x, uint16_t offset_y,
flare16x_bitmap* bitmap)
{
// Make sure that there are no null pointers
if (canvas == NULL || bitmap == NULL || canvas->pixels == NULL || bitmap->dib == NULL || bitmap->pixels == NULL)
return flare16x_error_make(FLARE16X_ERROR_NULL, FLARE16X_ERROR_SOURCE_BITMAP);
// Validate the format
// Note that this requires short-circuiting to work properly!
if (canvas->width == 0 || canvas->height == 0 || bitmap->dib_size == 0 || bitmap->pixels_size == 0 ||
bitmap->dib->width <= 0 || bitmap->dib->height >= 0 || bitmap->dib->planes != 1 || bitmap->stride == 0 ||
bitmap->dib->height * bitmap->dib->width > FLARE16X_BITMAP_MAX_PIXELS)
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
// Validate the width and height
if (canvas->width + offset_x > bitmap->dib->width || canvas->height + offset_y > (-bitmap->dib->height))
return flare16x_error_make(FLARE16X_ERROR_RANGE, FLARE16X_ERROR_SOURCE_BITMAP);
// Now, check the format and copy and convert the pixel data from RGB565 to the bitmap format
if (bitmap->dib->bit_count == 16 && bitmap->dib->compression == FLARE16X_BITMAP_COMPRESSION_BITFIELDS)
{
// RGB565 just requires a copy operation
// For good measure, verify the masks
if (bitmap->mask->mask_red != FLARE16X_BITMAP_MASK_RGB565_RED ||
bitmap->mask->mask_green != FLARE16X_BITMAP_MASK_RGB565_GREEN ||
bitmap->mask->mask_blue != FLARE16X_BITMAP_MASK_RGB565_BLUE)
{
free(canvas->pixels);
canvas->pixels = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// Now, copy the pixels line by line, pixel by pixel without changing them
int x, y;
for (y = 0; y < canvas->height; y++)
for (x = 0; x < canvas->width; x++)
bitmap->pixels565[(y + offset_y) * (bitmap->stride / sizeof(uint16_t)) + x + offset_x] =
canvas->pixels[y * canvas->width + x];
} else if (bitmap->dib->bit_count == 24 && bitmap->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB)
{
// RGB888 requires reducing the resolution and remapping the image data
// Now, process the pixels line by line, pixel by pixel
int x, y;
uint16_t p;
uint8_t r, g, b;
for (y = 0; y < canvas->height; y++)
for (x = 0; x < canvas->width; x++)
{
// Fetch the RGB565 pixel
p = canvas->pixels[y * canvas->width + x];
// Unpack it and separate the r, g and b components while expanding them to RGB888
r = p & 0xf800u; // R5
g = (p << 5) & 0xfc00u; // G6
b = (p << 11) & 0xf800u; // B5
// Store the components
bitmap->pixels[(y + offset_x) * bitmap->stride + (x + offset_x) * 3] = r;
bitmap->pixels[(y + offset_y) * bitmap->stride + (x + offset_x) * 3 + 1] = g;
bitmap->pixels[(y + offset_y) * bitmap->stride + (x + offset_x) * 3 + 2] = b;
}
} else if (bitmap->dib->bit_count == 32 && bitmap->dib->compression == FLARE16X_BITMAP_COMPRESSION_RGB)
{
// RGBA8888 requires reducing the resolution, discarding the alpha channel and remapping the image data
// Now, process the pixels line by line, pixel by pixel
int x, y;
uint16_t p;
uint8_t r, g, b;
for (y = offset_y; y < offset_y + canvas->height; y++)
for (x = offset_x; x < offset_x + canvas->width; x++)
{
// Fetch the RGB565 pixel
p = canvas->pixels[y * canvas->width + x];
// Unpack it and separate the r, g and b components while expanding them to RGB888
r = p & 0xf800u; // R5
g = (p << 5) & 0xfc00u; // G6
b = (p << 11) & 0xf800u; // B5
// Store the components with a 0xff alpha
bitmap->pixels8888[(y + offset_x) * (bitmap->stride / sizeof(uint16_t)) + (x + offset_x) * 4] =
0xff000000ul | (r << 16) | (g << 8) | b;
}
} else
{
free(canvas->pixels);
canvas->pixels = NULL;
return flare16x_error_make(FLARE16X_ERROR_FORMAT, FLARE16X_ERROR_SOURCE_BITMAP);
}
// And, it's done!
return flare16x_error_make(FLARE16X_ERROR_NONE, FLARE16X_ERROR_SOURCE_BITMAP);
}