forked from JayTee42/bitmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitmap.c
1842 lines (1366 loc) · 47.7 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
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "bitmap.h"
//Min / max:
#define BITMAP_MIN(a, b) (((a) < (b)) ? (a) : (b))
#define BITMAP_MAX(a, b) (((a) > (b)) ? (a) : (b))
//Includes from the standard library:
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Includes from POSIX:
#include <unistd.h>
//Constants:
#define BITMAP_MAGIC_NUMBER 0x4D42
//The internal representation of a bitmap.
typedef struct {
//The embedded bitmap parameters:
bitmap_parameters_t parameters;
//The file pointer:
FILE* file;
//Where do the pixels start?
uint32_t pixelOffset;
} bitmap_t;
//Internal logging function based on BITMAP_LOGGING.
//This always writes full lines.
void bitmapLog(bitmap_logging_t logging, const char* format, ...)
{
//Check level:
if (BITMAP_LOGGING < logging)
{
return;
}
//Delegate to vprintf:
va_list args;
va_start(args, format);
vprintf(format, args);
printf("\n");
va_end(args);
}
/**********************************************************************************************************************************************************************
Converting pixels
Thanks to http://stackoverflow.com/questions/3018313/algorithm-to-convert-rgb-to-hsv-and-hsv-to-rgb-in-range-0-255-for-both
Do we need FP? Not right now ...
**********************************************************************************************************************************************************************/
bitmap_pixel_rgb_t pixelToRGB(bitmap_pixel_t pixel, bitmap_color_space_t colorSpace)
{
bitmap_pixel_rgb_t newPixel;
switch (colorSpace)
{
case BITMAP_COLOR_SPACE_HSV:
{
newPixel.c3 = pixel.c3;
if (pixel.c1 == 0)
{
newPixel.r = pixel.c2;
newPixel.g = pixel.c2;
newPixel.b = pixel.c2;
break;
}
bitmap_component_t region = pixel.c0 / 43;
bitmap_component_t remainder = (pixel.c0 - (region * 43)) * 6;
bitmap_component_t p = (pixel.c2 * (255 - pixel.c1)) >> 8;
bitmap_component_t q = (pixel.c2 * (255 - ((pixel.c1 * remainder) >> 8))) >> 8;
bitmap_component_t t = (pixel.c2 * (255 - ((pixel.c1 * (255 - remainder)) >> 8))) >> 8;
switch (region)
{
case 0:
newPixel.r = pixel.c2;
newPixel.g = t;
newPixel.b = p;
break;
case 1:
newPixel.r = q;
newPixel.g = pixel.c2;
newPixel.b = p;
break;
case 2:
newPixel.r = p;
newPixel.g = pixel.c2;
newPixel.b = t;
break;
case 3:
newPixel.r = p;
newPixel.g = q;
newPixel.b = pixel.c2;
break;
case 4:
newPixel.r = t;
newPixel.g = p;
newPixel.b = pixel.c2;
break;
default:
newPixel.r = pixel.c2;
newPixel.g = p;
newPixel.b = q;
}
break;
}
default:
//RGB:
newPixel.r = pixel.c0;
newPixel.g = pixel.c1;
newPixel.b = pixel.c2;
newPixel.c3 = pixel.c3;
}
return newPixel;
}
bitmap_pixel_t rgbToPixel(bitmap_pixel_rgb_t pixel, bitmap_color_space_t colorSpace)
{
bitmap_pixel_t newPixel;
switch (colorSpace)
{
case BITMAP_COLOR_SPACE_HSV:
{
newPixel.c3 = pixel.c3;
bitmap_component_t rgbMin = BITMAP_MIN(pixel.r, BITMAP_MIN(pixel.g, pixel.b));
bitmap_component_t rgbMax = BITMAP_MAX(pixel.r, BITMAP_MAX(pixel.g, pixel.b));
newPixel.c2 = rgbMax;
if (newPixel.c2 == 0)
{
newPixel.c0 = 0;
newPixel.c1 = 0;
break;
}
newPixel.c1 = (bitmap_component_t)((255 * (uint16_t)(rgbMax - rgbMin)) / rgbMax);
if (newPixel.c1 == 0)
{
newPixel.c0 = 0;
break;
}
if (rgbMax == pixel.r)
newPixel.c0 = 0 + ((43 * (pixel.g - pixel.b)) / (rgbMax - rgbMin));
else if (rgbMax == pixel.g)
newPixel.c0 = 85 + ((43 * (pixel.b - pixel.r)) / (rgbMax - rgbMin));
else
newPixel.c0 = 171 + ((43 * (pixel.r - pixel.g)) / (rgbMax - rgbMin));
break;
}
default:
//RGB:
newPixel.c0 = pixel.r;
newPixel.c1 = pixel.g;
newPixel.c2 = pixel.b;
newPixel.c3 = pixel.c3;
}
return newPixel;
}
/**********************************************************************************************************************************************************************
Reading
**********************************************************************************************************************************************************************/
//Internal reading function.
//Always reads "count" bytes.
//
//Errors:
//
//- BITMAP_ERROR_IO A read error has occurred. Includes unexpected EOF.
//
//The file will not be closed by this function.
bitmap_error_t bitmapReadBytes(FILE* file, uint8_t* buffer, size_t count)
{
if (fread(buffer, 1, count, file) != count)
{
if (feof(file))
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Failed to read bytes: Unexpected end of file.");
return BITMAP_ERROR_IO;
}
int err = ferror(file);
if (err)
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Failed to read bytes: IO error (%d).", err);
return BITMAP_ERROR_IO;
}
bitmapLog(BITMAP_LOGGING_DEFAULT, "Failed to read bytes: Unknown IO error.");
return BITMAP_ERROR_IO;
}
return BITMAP_ERROR_SUCCESS;
}
//Some typed functions with the same behavior:
bitmap_error_t bitmapReadU8(FILE* file, uint8_t* value)
{
return bitmapReadBytes(file, value, sizeof(uint8_t));
}
bitmap_error_t bitmapReadI8(FILE* file, int8_t* value)
{
return bitmapReadBytes(file, (uint8_t*)value, sizeof(int8_t));
}
bitmap_error_t bitmapReadU16(FILE* file, uint16_t* value)
{
return bitmapReadBytes(file, (uint8_t*)value, sizeof(uint16_t));
}
bitmap_error_t bitmapReadI16(FILE* file, int16_t* value)
{
return bitmapReadBytes(file, (uint8_t*)value, sizeof(int16_t));
}
bitmap_error_t bitmapReadU32(FILE* file, uint32_t* value)
{
return bitmapReadBytes(file, (uint8_t*)value, sizeof(uint32_t));
}
bitmap_error_t bitmapReadI32(FILE* file, int32_t* value)
{
return bitmapReadBytes(file, (uint8_t*)value, sizeof(int32_t));
}
//Internal pixel row read function (BITMAP_COLOR_DEPTH_1).
//The buffers will not be released by this function.
void bitmapReadRowColorDepth_1(bitmap_t* bitmap, const uint8_t* rowData, bitmap_pixel_t* outputData, uint32_t rowPx)
{
uint32_t widthPx = bitmap->parameters.widthPx;
uint32_t pixelsRead = 0;
size_t baseIndex = rowPx * widthPx;
do
{
uint32_t pixelsToRead = widthPx - pixelsRead;
if (pixelsToRead > 8)
{
pixelsToRead = 8;
}
uint8_t currByte = rowData[pixelsRead / 8];
for (uint32_t i = 0; i < pixelsToRead; i++)
{
outputData[baseIndex + pixelsRead + i] = (currByte & (1 << (7 - i))) ? bitmap->parameters.colorTable[1] : bitmap->parameters.colorTable[0];
}
pixelsRead += pixelsToRead;
} while (pixelsRead < widthPx);
}
//Internal pixel row read function (BITMAP_COLOR_DEPTH_8).
//The buffers will not be released by this function.
void bitmapReadRowColorDepth_8(bitmap_t* bitmap, const uint8_t* rowData, bitmap_pixel_t* outputData, uint32_t rowPx)
{
uint32_t widthPx = bitmap->parameters.widthPx;
size_t baseIndex = rowPx * widthPx;
for (uint32_t colPx = 0; colPx < widthPx; colPx++)
{
outputData[baseIndex + colPx] = bitmap->parameters.colorTable[rowData[colPx]];
}
}
//Internal pixel row read function (BITMAP_COLOR_DEPTH_24).
//The buffers will not be released by this function.
void bitmapReadRowColorDepth_24(bitmap_t* bitmap, const uint8_t* rowData, bitmap_pixel_t* outputData, uint32_t rowPx)
{
uint32_t widthPx = bitmap->parameters.widthPx;
bitmap_color_space_t colorSpace = bitmap->parameters.colorSpace;
size_t baseIndex = rowPx * widthPx;
for (uint32_t colPx = 0; colPx < widthPx; colPx++)
{
bitmap_pixel_rgb_t currPixel;
currPixel.r = rowData[(3 * colPx) + 2];
currPixel.g = rowData[(3 * colPx) + 1];
currPixel.b = rowData[(3 * colPx) + 0];
currPixel.c3 = 0x00;
outputData[baseIndex + colPx] = rgbToPixel(currPixel, colorSpace);
}
}
//Internal pixel row read function (BITMAP_COLOR_DEPTH_32).
//The buffers will not be released by this function.
void bitmapReadRowColorDepth_32(bitmap_t* bitmap, const uint8_t* rowData, bitmap_pixel_t* outputData, uint32_t rowPx)
{
uint32_t widthPx = bitmap->parameters.widthPx;
bitmap_color_space_t colorSpace = bitmap->parameters.colorSpace;
size_t baseIndex = rowPx * widthPx;
for (uint32_t colPx = 0; colPx < widthPx; colPx++)
{
bitmap_pixel_rgb_t currPixel;
currPixel.r = rowData[(4 * colPx) + 3];
currPixel.g = rowData[(4 * colPx) + 2];
currPixel.b = rowData[(4 * colPx) + 1];
currPixel.c3 = rowData[(4 * colPx) + 0];
outputData[baseIndex + colPx] = rgbToPixel(currPixel, colorSpace);
}
}
//Internal pixel read function (BITMAP_COMPRESSION_NONE).
//If the function succeeds, the pixel pointer is valid.
//
//Errors:
//- BITMAP_ERROR_IO An IO error has occurred.
//- BITMAP_ERROR_MEMORY Insufficient memory.
//
//The file will not be closed by this function.
bitmap_error_t bitmapReadPixelsCompression_None(bitmap_t* bitmap, bitmap_pixel_t** pixels)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "Decompressing BITMAP_COMPRESSION_NONE ...");
//Get width and height:
uint32_t widthPx = bitmap->parameters.widthPx;
uint32_t heightPx = bitmap->parameters.heightPx;
uint32_t totalPx = widthPx * heightPx;
//Allocate space for the pixels:
bitmap_pixel_t* outputData = (bitmap_pixel_t*)malloc(totalPx * sizeof(bitmap_pixel_t));
if (!outputData)
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Insufficient memory for allocating pixel buffer.");
return BITMAP_ERROR_MEMORY;
}
//How many bytes are in a row?
size_t bitsPerRow = bitmap->parameters.colorDepth * widthPx;
size_t bytesPerRow = ((bitsPerRow + 31) / 32) * 4;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Bits / bytes per row: %zu / %zu", bitsPerRow, bytesPerRow);
//Allocate memory for a row:
uint8_t* rowData = (uint8_t*)malloc(bytesPerRow);
if (!rowData)
{
//Free the pixel buffer:
free(outputData);
bitmapLog(BITMAP_LOGGING_DEFAULT, "Insufficient memory for allocating row buffer.");
return BITMAP_ERROR_MEMORY;
}
//Status var:
bitmap_error_t success;
//Read row by row:
for (uint32_t rowPx = 0; rowPx < heightPx; rowPx++)
{
//Read the row:
if ((success = bitmapReadBytes(bitmap->file, rowData, bytesPerRow)) != BITMAP_ERROR_SUCCESS)
{
break;
}
//Parse it, depending on the color depth:
switch (bitmap->parameters.colorDepth)
{
case BITMAP_COLOR_DEPTH_1:
bitmapReadRowColorDepth_1(bitmap, rowData, outputData, rowPx);
break;
case BITMAP_COLOR_DEPTH_8:
bitmapReadRowColorDepth_8(bitmap, rowData, outputData, rowPx);
break;
case BITMAP_COLOR_DEPTH_24:
bitmapReadRowColorDepth_24(bitmap, rowData, outputData, rowPx);
break;
case BITMAP_COLOR_DEPTH_32:
bitmapReadRowColorDepth_32(bitmap, rowData, outputData, rowPx);
break;
default:
bitmapLog(BITMAP_LOGGING_DEFAULT, "Color depth is not (yet) supported. Sorry!");
success = BITMAP_ERROR_INVALID_FILE_FORMAT;
}
//Check success:
if (success != BITMAP_ERROR_SUCCESS)
{
break;
}
}
//If we were successful, we assign the pointers:
if (success == BITMAP_ERROR_SUCCESS)
{
*pixels = outputData;
//Finished!
bitmapLog(BITMAP_LOGGING_VERBOSE, "All pixels have been decompressed.");
}
else
{
//Free the pixel buffer:
free(outputData);
}
//Free the row data:
free(rowData);
return success;
}
//Internal DIB header reading function (BITMAP_DIB_HEADER_INFO).
//
//Errors:
//
//- BITMAP_ERROR_INVALID_FILE_FORMAT Something bad inside the DIB header. Or we don't support some feature.
//- BITMAP_ERROR_IO A read error has occurred.
//
//The file will not be closed by this function.
bitmap_error_t bitmapReadDIBHeader_Info(bitmap_t* bitmap)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "Starting to read DIB header (BITMAP_DIB_HEADER_INFO) ...");
//Status var:
bitmap_error_t success;
//Read the width:
if ((success = bitmapReadU32(bitmap->file, &(bitmap->parameters.widthPx))) != BITMAP_ERROR_SUCCESS)
{
return success;
}
//Read the height:
int32_t heightPx;
if ((success = bitmapReadI32(bitmap->file, &heightPx)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
//Top-Down or bottom-up?
if (heightPx > 0)
{
bitmap->parameters.bottomUp = 1;
bitmap->parameters.heightPx = (uint32_t)heightPx;
}
else
{
bitmap->parameters.bottomUp = 0;
bitmap->parameters.heightPx = (uint32_t)-heightPx;
}
//Check size:
if ((bitmap->parameters.widthPx == 0) || (bitmap->parameters.heightPx == 0))
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Width and / or height in pixels is 0.");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Bitmap pixel dimensions: %u x %u (width x height)", bitmap->parameters.widthPx, bitmap->parameters.heightPx);
bitmapLog(BITMAP_LOGGING_VERBOSE, "Bottom-up: %s", (bitmap->parameters.bottomUp ? "true" : "false"));
//Read the biPlanes (ignored):
uint16_t biPlanes;
if ((success = bitmapReadU16(bitmap->file, &biPlanes)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "BiPlanes: %u (ignored)", biPlanes);
if (biPlanes != 1)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "... but shouldn't that be 1 ?!");
}
//Read the color depth:
if ((success = bitmapReadU16(bitmap->file, &(bitmap->parameters.colorDepth))) != BITMAP_ERROR_SUCCESS)
{
return success;
}
switch (bitmap->parameters.colorDepth)
{
case BITMAP_COLOR_DEPTH_1:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color depth: 2 indexed colors");
break;
case BITMAP_COLOR_DEPTH_4:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color depth: 16 indexed colors");
break;
case BITMAP_COLOR_DEPTH_8:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color depth: 256 indexed colors");
break;
case BITMAP_COLOR_DEPTH_16:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color depth: 16 bit");
break;
case BITMAP_COLOR_DEPTH_24:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color depth: 24 bit");
break;
case BITMAP_COLOR_DEPTH_32:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color depth: 32 bit");
break;
default:
bitmapLog(BITMAP_LOGGING_DEFAULT, "Unknown color depth: %u bit", bitmap->parameters.colorDepth);
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
//Read the compression:
uint32_t rawCompression;
if ((success = bitmapReadU32(bitmap->file, &rawCompression)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
switch (rawCompression)
{
case 0:
bitmap->parameters.compression = BITMAP_COMPRESSION_NONE;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Compression: none");
break;
case 1:
bitmap->parameters.compression = BITMAP_COMPRESSION_RLE;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Compression: RLE (8 bit)");
//Make sure this is valid:
if ((bitmap->parameters.colorDepth != BITMAP_COLOR_DEPTH_8) || !(bitmap->parameters.bottomUp))
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Invalid bitmap compression in this context.");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
break;
case 2:
bitmap->parameters.compression = BITMAP_COMPRESSION_RLE;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Compression: RLE (4 bit)");
//Make sure this is valid:
if ((bitmap->parameters.colorDepth != BITMAP_COLOR_DEPTH_4) || !(bitmap->parameters.bottomUp))
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Invalid bitmap compression in this context.");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
break;
case 3:
bitmap->parameters.compression = BITMAP_COMPRESSION_BITFIELD_RGB;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Compression: Bitfield (RGB)");
//Make sure this is valid:
if ((bitmap->parameters.colorDepth != BITMAP_COLOR_DEPTH_16) && (bitmap->parameters.colorDepth != BITMAP_COLOR_DEPTH_32))
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Invalid bitmap compression in this context.");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
break;
case 6:
bitmap->parameters.compression = BITMAP_COMPRESSION_BITFIELD_ARGB;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Compression: Bitfield (ARGB)");
//Make sure this is valid:
if ((bitmap->parameters.colorDepth != BITMAP_COLOR_DEPTH_16) && (bitmap->parameters.colorDepth != BITMAP_COLOR_DEPTH_32))
{
bitmapLog(BITMAP_LOGGING_DEFAULT, "Invalid bitmap compression in this context.");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
break;
default:
bitmapLog(BITMAP_LOGGING_DEFAULT, "Unknown bitmap compression: %u", rawCompression);
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
//Read the size of the pixel data (ignored):
uint32_t pixelDataSize;
if ((success = bitmapReadU32(bitmap->file, &pixelDataSize)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Pixel data size in bytes: %u (ignored)", pixelDataSize);
//Read the pixels per meter (ignored):
uint32_t pixelsPerMeterX;
if ((success = bitmapReadU32(bitmap->file, &pixelsPerMeterX)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
uint32_t pixelsPerMeterY;
if ((success = bitmapReadU32(bitmap->file, &pixelsPerMeterY)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Bitmap destination resolution: %u p/m in X, %u p/m in Y (ignored)", pixelsPerMeterX, pixelsPerMeterY);
//Read the number of valid color table entries:
uint32_t colorTableEntries;
if ((success = bitmapReadU32(bitmap->file, &colorTableEntries)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
//Correct potential zeros:
if (colorTableEntries == 0)
{
switch (bitmap->parameters.colorDepth)
{
case BITMAP_COLOR_DEPTH_1:
colorTableEntries = 2;
break;
case BITMAP_COLOR_DEPTH_4:
colorTableEntries = 16;
break;
case BITMAP_COLOR_DEPTH_8:
colorTableEntries = 256;
break;
}
}
else
{
//Clamp the value if necessary:
if (colorTableEntries > 256)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "Clamping number of color table entries from %u to 256.", colorTableEntries);
colorTableEntries = 256;
}
}
bitmap->parameters.colorTableEntries = colorTableEntries;
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color table entries: %u", bitmap->parameters.colorTableEntries);
//Read the number of important color table entries (ignored):
uint32_t importantColorTableEntries;
if ((success = bitmapReadU32(bitmap->file, &importantColorTableEntries)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Important color table entries: %u (ignored)", importantColorTableEntries);
//Depending on the compression, we have to read the bitmasks now:
if (bitmap->parameters.compression == BITMAP_COMPRESSION_BITFIELD_RGB)
{
if ((success = bitmapReadBytes(bitmap->file, (uint8_t*)&(bitmap->parameters.bitmasks), 3 * sizeof(uint32_t))) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "RGB bitmasks have been read.");
}
else if (bitmap->parameters.compression == BITMAP_COMPRESSION_BITFIELD_ARGB)
{
if ((success = bitmapReadBytes(bitmap->file, (uint8_t*)&(bitmap->parameters.bitmasks), 4 * sizeof(uint32_t))) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "ARGB bitmasks have been read.");
}
else
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "No bitmasks have been read.");
}
//Finally, read the color table:
if (bitmap->parameters.colorTableEntries)
{
bitmap_pixel_rgb_t colorTable[256];
if ((success = bitmapReadBytes(bitmap->file, (uint8_t*)&colorTable, bitmap->parameters.colorTableEntries * sizeof(bitmap_pixel_rgb_t))) != BITMAP_ERROR_SUCCESS)
{
return success;
}
//Now shuffle the table to the right order:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Color table (RGBA):");
for (uint32_t i = 0; i < bitmap->parameters.colorTableEntries; i++)
{
bitmap_pixel_rgb_t currPixel = colorTable[i];
bitmap_pixel_rgb_t newPixel;
newPixel.r = currPixel.b;
newPixel.g = currPixel.g;
newPixel.b = currPixel.r;
newPixel.c3 = currPixel.c3;
bitmapLog(BITMAP_LOGGING_VERBOSE, " (0x%02X, 0x%02X, 0x%02X, 0x%02X)", newPixel.r, newPixel.g, newPixel.b, newPixel.c3);
//Convert the pixel to our color space and insert it:
bitmap->parameters.colorTable[i] = rgbToPixel(newPixel, bitmap->parameters.colorSpace);
}
}
else
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "No color table has been read.");
}
//Okay, that worked :-)
bitmapLog(BITMAP_LOGGING_VERBOSE, "DIB header (info) has successfully been read.");
return BITMAP_ERROR_SUCCESS;
}
//Internal header reading function.
//
//Errors:
//
//- BITMAP_ERROR_INVALID_FILE_FORMAT Something bad inside the header. Or we don't support some feature.
//- BITMAP_ERROR_IO A read error has occurred.
//
//The file will not be closed by this function.
bitmap_error_t bitmapReadHeader(bitmap_t* bitmap)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "Starting to read header ...");
//Status var:
bitmap_error_t success;
//Read and verify the magic number:
uint16_t magicNumber;
if ((success = bitmapReadU16(bitmap->file, &magicNumber)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
if (magicNumber != BITMAP_MAGIC_NUMBER)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "Wrong magic number (0x%04X). This does not look like a bitmap.", magicNumber);
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Magic number is a match.");
//Read and ignore the size in bytes:
uint32_t sizeInBytes;
if ((success = bitmapReadU32(bitmap->file, &sizeInBytes)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Size in bytes: %u (ignored)", sizeInBytes);
//Read and ignore the writer-specific blob:
uint32_t writerSpecific;
if ((success = bitmapReadU32(bitmap->file, &writerSpecific)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Writer-specific blob: %u (ignored)", writerSpecific);
//Read the pixel offset:
if ((success = bitmapReadU32(bitmap->file, &(bitmap->pixelOffset))) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Pixel offset: 0x%04X", bitmap->pixelOffset);
//Read the size of the info header structure:
uint32_t infoHeaderSize;
if ((success = bitmapReadU32(bitmap->file, &infoHeaderSize)) != BITMAP_ERROR_SUCCESS)
{
return success;
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "Info header size in bytes: %u", infoHeaderSize);
//The size tells us about the type.
switch (infoHeaderSize)
{
case BITMAP_DIB_HEADER_CORE_OS2_1X:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_CORE_OS2_1X");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported. Sorry!");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
case BITMAP_DIB_HEADER_OS2_2X:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_OS2_2X");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported. Sorry!");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
case BITMAP_DIB_HEADER_OS2_2X_SHORT:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_OS2_2X_SHORT");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported. Sorry!");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
case BITMAP_DIB_HEADER_INFO_V5:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_INFO_V5");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported.");
bitmapLog(BITMAP_LOGGING_DEFAULT, "Trying fallback to V4 ...");
/* fall through */
case BITMAP_DIB_HEADER_INFO_V4:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_INFO_V4");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported.");
bitmapLog(BITMAP_LOGGING_DEFAULT, "Trying fallback to V3 ...");
/* fall through */
case BITMAP_DIB_HEADER_INFO_V3:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_INFO_V3");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported.");
bitmapLog(BITMAP_LOGGING_DEFAULT, "Trying fallback to V2 ...");
/* fall through */
case BITMAP_DIB_HEADER_INFO_V2:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_INFO_V2");
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is not (yet) supported.");
bitmapLog(BITMAP_LOGGING_DEFAULT, "Trying fallback to V1 ...");
/* fall through */
case BITMAP_DIB_HEADER_INFO:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Identified DIB header: BITMAP_DIB_HEADER_INFO");
if ((success = bitmapReadDIBHeader_Info(bitmap)))
{
return success;
}
break;
default:
bitmapLog(BITMAP_LOGGING_DEFAULT, "DIB header format is unknown.");
return BITMAP_ERROR_INVALID_FILE_FORMAT;
}
//That's it:
bitmapLog(BITMAP_LOGGING_VERBOSE, "Bitmap header parsing has succeeded!");
return BITMAP_ERROR_SUCCESS;
}
//Internal file open function.
//If the function succeeds, the file pointer in the bitmap struct is valid.
//
//Errors:
//- BITMAP_ERROR_INVALID_PATH The given path is not valid in any way (missing file, bad permissions etc.).
//- BITMAP_ERROR_INVALID_FILE_FORMAT The given file is no valid bitmap.
//- BITMAP_ERROR_IO An IO error has occurred.
//- BITMAP_ERROR_MEMORY Insufficient memory.
//
bitmap_error_t bitmapOpenFile(bitmap_t* bitmap, const char* filePath)
{
bitmapLog(BITMAP_LOGGING_VERBOSE, "Going to open file \"%s\" ...", filePath);
//Try to open the file:
FILE* file = fopen(filePath, "rb");
if (!file)
{
switch (errno)
{
case ENOENT:
bitmapLog(BITMAP_LOGGING_DEFAULT, "File path to \"%s\" is invalid.", filePath);
return BITMAP_ERROR_INVALID_PATH;
case ENOMEM:
bitmapLog(BITMAP_LOGGING_DEFAULT, "Insufficient memory for opening file.");
return BITMAP_ERROR_MEMORY;
default:
bitmapLog(BITMAP_LOGGING_DEFAULT, "Error opening file at \"%s\": %s", filePath, strerror(errno));
return BITMAP_ERROR_INVALID_PATH;
}
}
bitmapLog(BITMAP_LOGGING_VERBOSE, "File has been opened successfully.");
//Set file pointer:
bitmap->file = file;
return BITMAP_ERROR_SUCCESS;
}
//User-accessible.
bitmap_error_t bitmapReadPixels(const char* filePath, bitmap_pixel_t** pixels, uint32_t* widthPx, uint32_t* heightPx, bitmap_color_space_t colorSpace)
{
//NULL the pointers:
*pixels = NULL;
*widthPx = 0;
*heightPx = 0;
//Init a bitmap struct:
bitmap_t bitmap;
memset(&bitmap, 0, sizeof(bitmap_t));
//Assign our color space: