-
Notifications
You must be signed in to change notification settings - Fork 2
/
bitmap.c
2860 lines (2527 loc) · 80.4 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
/*
* @(#)bitmap.c
*
* Copyright 1997-1999, Wes Cherry (mailto:wesc@technosis.com)
* 2000-2005, Aaron Ardiri (mailto:aaron@ardiri.com)
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, please write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Revisions:
* ==========
*
* pre 18-Jun-2000 <numerous developers>
* creation
* 18-Jun-2000 Aaron Ardiri
* GNU GPL documentation additions
* 18-Aug-2003 Ben Combee
* Applied Eric Clonniger's fix for 32-bit BMPs
* Feb-2007 Alexander Pruss
* LE32 fixes
*/
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include "pilrc.h"
#include "bitmap.h"
typedef unsigned char PILRC_BYTE; /* b */
typedef unsigned short PILRC_USHORT; /* us */
typedef unsigned int PILRC_ULONG; /* ul */
typedef int bool; /* f */
//
// data structures
//
/*
* Microsoft Windows .BMP file format
*/
// NCR: 16-feb-00 = bitmapfileheader already defined if compiling for windows
#if !defined(CW_PLUGIN) || (CWPLUGIN_HOST != CWPLUGIN_HOST_WIN32)
// define structure using USHORT types to make correct alignment
// more likely -- resolves SourceForge bug 540640
typedef struct BITMAPFILEHEADER
{
PILRC_USHORT bfType;
PILRC_USHORT bfSize1;
PILRC_USHORT bfSize2;
PILRC_USHORT bfReserved1;
PILRC_USHORT bfReserved2;
PILRC_USHORT bfOffBits1;
PILRC_USHORT bfOffBits2;
} BITMAPFILEHEADER;
typedef struct BITMAPINFOHEADER
{
PILRC_ULONG biSize;
PILRC_ULONG biWidth;
PILRC_ULONG biHeight;
PILRC_USHORT biPlanes;
PILRC_USHORT biBitCount;
PILRC_ULONG biCompression;
PILRC_ULONG biSizeImage;
PILRC_ULONG biXPelsPerMeter;
PILRC_ULONG biYPelsPerMeter;
PILRC_ULONG biClrUsed;
PILRC_ULONG biClrImportant;
} BITMAPINFOHEADER;
typedef struct RGBQUAD
{
PILRC_BYTE rgbBlue;
PILRC_BYTE rgbGreen;
PILRC_BYTE rgbRed;
PILRC_BYTE rgbReserved;
} RGBQUAD;
typedef struct BITMAPINFO
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1]; /* size is indefinite */
} BITMAPINFO;
#endif
#define BI_RGB 0
#define BI_RLE4 1
#define BI_RLE8 2
#define BI_BITFIELDS 3
// *INDENT-OFF*
/*
* The 1bit-2 color system palette for Palm Computing Devices.
*/
int PalmPalette1bpp[2][3] =
{
{ 255, 255, 255}, { 0, 0, 0 }
};
/*
* The 2bit-4 color system palette for Palm Computing Devices.
*/
int PalmPalette2bpp[4][3] =
{
{ 255, 255, 255}, { 192, 192, 192}, { 128, 128, 128 }, { 0, 0, 0 }
};
/*
* The 4bit-16 color system palette for Palm Computing Devices.
*/
int PalmPalette4bpp[16][3] =
{
{ 255, 255, 255}, { 238, 238, 238 }, { 221, 221, 221 }, { 204, 204, 204 },
{ 187, 187, 187}, { 170, 170, 170 }, { 153, 153, 153 }, { 136, 136, 136 },
{ 119, 119, 119}, { 102, 102, 102 }, { 85, 85, 85 }, { 68, 68, 68 },
{ 51, 51, 51}, { 34, 34, 34 }, { 17, 17, 17 }, { 0, 0, 0 }
};
/*
* The 4bit-16 color system palette for Palm Computing Devices.
*/
int PalmPalette4bppColor[16][3] =
{
{ 255, 255, 255}, { 128, 128, 128 }, { 128, 0, 0 }, { 128, 128, 0 },
{ 0, 128, 0}, { 0, 128, 128 }, { 0, 0, 128 }, { 128, 0, 128 },
{ 255, 0, 255}, { 192, 192, 192 }, { 255, 0, 0 }, { 255, 255, 0 },
{ 0, 255, 0}, { 0, 255, 255 }, { 0, 0, 255 }, { 0, 0, 0 }
};
/*
* The 8bit-256 color system palette for Palm Computing Devices.
*/
int PalmPalette8bpp[256][3] =
{
{ 255, 255, 255 }, { 255, 204, 255 }, { 255, 153, 255 }, { 255, 102, 255 },
{ 255, 51, 255 }, { 255, 0, 255 }, { 255, 255, 204 }, { 255, 204, 204 },
{ 255, 153, 204 }, { 255, 102, 204 }, { 255, 51, 204 }, { 255, 0, 204 },
{ 255, 255, 153 }, { 255, 204, 153 }, { 255, 153, 153 }, { 255, 102, 153 },
{ 255, 51, 153 }, { 255, 0, 153 }, { 204, 255, 255 }, { 204, 204, 255 },
{ 204, 153, 255 }, { 204, 102, 255 }, { 204, 51, 255 }, { 204, 0, 255 },
{ 204, 255, 204 }, { 204, 204, 204 }, { 204, 153, 204 }, { 204, 102, 204 },
{ 204, 51, 204 }, { 204, 0, 204 }, { 204, 255, 153 }, { 204, 204, 153 },
{ 204, 153, 153 }, { 204, 102, 153 }, { 204, 51, 153 }, { 204, 0, 153 },
{ 153, 255, 255 }, { 153, 204, 255 }, { 153, 153, 255 }, { 153, 102, 255 },
{ 153, 51, 255 }, { 153, 0, 255 }, { 153, 255, 204 }, { 153, 204, 204 },
{ 153, 153, 204 }, { 153, 102, 204 }, { 153, 51, 204 }, { 153, 0, 204 },
{ 153, 255, 153 }, { 153, 204, 153 }, { 153, 153, 153 }, { 153, 102, 153 },
{ 153, 51, 153 }, { 153, 0, 153 }, { 102, 255, 255 }, { 102, 204, 255 },
{ 102, 153, 255 }, { 102, 102, 255 }, { 102, 51, 255 }, { 102, 0, 255 },
{ 102, 255, 204 }, { 102, 204, 204 }, { 102, 153, 204 }, { 102, 102, 204 },
{ 102, 51, 204 }, { 102, 0, 204 }, { 102, 255, 153 }, { 102, 204, 153 },
{ 102, 153, 153 }, { 102, 102, 153 }, { 102, 51, 153 }, { 102, 0, 153 },
{ 51, 255, 255 }, { 51, 204, 255 }, { 51, 153, 255 }, { 51, 102, 255 },
{ 51, 51, 255 }, { 51, 0, 255 }, { 51, 255, 204 }, { 51, 204, 204 },
{ 51, 153, 204 }, { 51, 102, 204 }, { 51, 51, 204 }, { 51, 0, 204 },
{ 51, 255, 153 }, { 51, 204, 153 }, { 51, 153, 153 }, { 51, 102, 153 },
{ 51, 51, 153 }, { 51, 0, 153 }, { 0, 255, 255 }, { 0, 204, 255 },
{ 0, 153, 255 }, { 0, 102, 255 }, { 0, 51, 255 }, { 0, 0, 255 },
{ 0, 255, 204 }, { 0, 204, 204 }, { 0, 153, 204 }, { 0, 102, 204 },
{ 0, 51, 204 }, { 0, 0, 204 }, { 0, 255, 153 }, { 0, 204, 153 },
{ 0, 153, 153 }, { 0, 102, 153 }, { 0, 51, 153 }, { 0, 0, 153 },
{ 255, 255, 102 }, { 255, 204, 102 }, { 255, 153, 102 }, { 255, 102, 102 },
{ 255, 51, 102 }, { 255, 0, 102 }, { 255, 255, 51 }, { 255, 204, 51 },
{ 255, 153, 51 }, { 255, 102, 51 }, { 255, 51, 51 }, { 255, 0, 51 },
{ 255, 255, 0 }, { 255, 204, 0 }, { 255, 153, 0 }, { 255, 102, 0 },
{ 255, 51, 0 }, { 255, 0, 0 }, { 204, 255, 102 }, { 204, 204, 102 },
{ 204, 153, 102 }, { 204, 102, 102 }, { 204, 51, 102 }, { 204, 0, 102 },
{ 204, 255, 51 }, { 204, 204, 51 }, { 204, 153, 51 }, { 204, 102, 51 },
{ 204, 51, 51 }, { 204, 0, 51 }, { 204, 255, 0 }, { 204, 204, 0 },
{ 204, 153, 0 }, { 204, 102, 0 }, { 204, 51, 0 }, { 204, 0, 0 },
{ 153, 255, 102 }, { 153, 204, 102 }, { 153, 153, 102 }, { 153, 102, 102 },
{ 153, 51, 102 }, { 153, 0, 102 }, { 153, 255, 51 }, { 153, 204, 51 },
{ 153, 153, 51 }, { 153, 102, 51 }, { 153, 51, 51 }, { 153, 0, 51 },
{ 153, 255, 0 }, { 153, 204, 0 }, { 153, 153, 0 }, { 153, 102, 0 },
{ 153, 51, 0 }, { 153, 0, 0 }, { 102, 255, 102 }, { 102, 204, 102 },
{ 102, 153, 102 }, { 102, 102, 102 }, { 102, 51, 102 }, { 102, 0, 102 },
{ 102, 255, 51 }, { 102, 204, 51 }, { 102, 153, 51 }, { 102, 102, 51 },
{ 102, 51, 51 }, { 102, 0, 51 }, { 102, 255, 0 }, { 102, 204, 0 },
{ 102, 153, 0 }, { 102, 102, 0 }, { 102, 51, 0 }, { 102, 0, 0 },
{ 51, 255, 102 }, { 51, 204, 102 }, { 51, 153, 102 }, { 51, 102, 102 },
{ 51, 51, 102 }, { 51, 0, 102 }, { 51, 255, 51 }, { 51, 204, 51 },
{ 51, 153, 51 }, { 51, 102, 51 }, { 51, 51, 51 }, { 51, 0, 51 },
{ 51, 255, 0 }, { 51, 204, 0 }, { 51, 153, 0 }, { 51, 102, 0 },
{ 51, 51, 0 }, { 51, 0, 0 }, { 0, 255, 102 }, { 0, 204, 102 },
{ 0, 153, 102 }, { 0, 102, 102 }, { 0, 51, 102 }, { 0, 0, 102 },
{ 0, 255, 51 }, { 0, 204, 51 }, { 0, 153, 51 }, { 0, 102, 51 },
{ 0, 51, 51 }, { 0, 0, 51 }, { 0, 255, 0 }, { 0, 204, 0 },
{ 0, 153, 0 }, { 0, 102, 0 }, { 0, 51, 0 }, { 17, 17, 17 },
{ 34, 34, 34 }, { 68, 68, 68 }, { 85, 85, 85 }, { 119, 119, 119 },
{ 136, 136, 136 }, { 170, 170, 170 }, { 187, 187, 187 }, { 221, 221, 221 },
{ 238, 238, 238 }, { 192, 192, 192 }, { 128, 0, 0 }, { 128, 0, 128 },
{ 0, 128, 0 }, { 0, 128, 128 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 },
{ 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }, { 0, 0, 0 }
};
#define COLOR_TABLE_SIZE 1026
#define LE_BITMAP_VERSION_MASK 0x80
/*
* The 4bit-16 color user palette for Palm Computing Devices.
*/
int UserPalette4bpp[16][3];
/*
* The 8bit-256 color user palette for Palm Computing Devices.
*/
int UserPalette8bpp[256][3];
// *INDENT-ON*
//
// local function prototypes
//
// *INDENT-OFF*
static PILRC_ULONG LLoadX86(PILRC_ULONG w);
static PILRC_USHORT WLoadX86(PILRC_USHORT w);
static int BMP_RGBToColorIndex(int, int, int, int[][3], int);
static int BMP_GetBits1bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static int BMP_GetBits4bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static int BMP_GetBits8bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static int BMP_GetBits15bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static int BMP_GetBits16bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static int BMP_GetBits24bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static int BMP_GetBits32bpp(BITMAPINFO *, int, PILRC_BYTE *,
int, int, int, int *, int *, int *, int *);
static void BMP_SetBits1bpp(int, PILRC_BYTE *, int, int, int);
static void BMP_SetBits2bpp(int, PILRC_BYTE *, int, int, int, int);
static void BMP_SetBits4bpp(int, PILRC_BYTE *, int, int, int, int);
static void BMP_SetBits8bpp(int, PILRC_BYTE *, int, int, int, int);
static void BMP_SetBits16bpp(int, PILRC_BYTE *, int, int, int, int);
static void BMP_SetBits24bpp(int, PILRC_BYTE *, int, int, int, int);
static void BMP_SetBits32bpp(int, PILRC_BYTE *, int, int, int, int);
static void BMP_ConvertWindowsBitmap(RCBITMAP *, PILRC_BYTE *, int, BOOL, int *, int);
static void BMP_ConvertTextBitmap(RCBITMAP *, PILRC_BYTE *, int);
static void BMP_ConvertX11Bitmap(RCBITMAP *, PILRC_BYTE *, int);
static void BMP_ConvertPNMBitmap(RCBITMAP *, PILRC_BYTE *, int, int, BOOL, int *);
static void BMP_CompressBitmap(RCBITMAP *, int, int, BOOL, BOOL);
static void BMP_CompressDumpBitmap(RCBITMAP *, int, int, BOOL, BOOL, BOOL, BOOL, int, int *);
static void BMP_InvalidExtension(const char *);
static void BMP_FillBitmapV3Header(RCBITMAP *, RCBITMAP_V3 *, int *, int);
// *INDENT-ON*
//
// code
//
/**
* Set the 4bpp user palette.
*
* @param p palette
* @param nColors the number of colors
*/
void
SetUserPalette4bpp(int p[][3],
int nColors)
{
int c;
#ifdef BITMAP_CALL_LOG
if (!vfQuiet)
{
printf("SetUserPalette4bpp(");
for (c = 0; c < nColors; c++)
printf("%d:%d:%d, ", p[c][0], p[c][1], p[c][2]);
printf("%d)\n", nColors);
}
#endif
nColors = (nColors > 16) ? 16 : nColors;
// copy colors
for (c = 0; c < nColors; c++)
{
UserPalette4bpp[c][0] = p[c][0];
UserPalette4bpp[c][1] = p[c][1];
UserPalette4bpp[c][2] = p[c][2];
}
// fill remainder with blacks
for (c = nColors; c < 16; c++)
{
UserPalette4bpp[c][0] = 0;
UserPalette4bpp[c][1] = 0;
UserPalette4bpp[c][2] = 0;
}
}
/**
* Set the 4bpp user palette to the system palette
*/
void
SetUserPalette4bppToDefault4bpp()
{
#ifdef BITMAP_CALL_LOG
if (!vfQuiet)
printf("SetUserPalette4bppToDefault4bpp(): ");
#endif
SetUserPalette4bpp(PalmPalette4bppColor, 16);
}
/**
* Set the 8bpp user palette.
*
* @param p palette
* @param nColors the number of colors
*/
void
SetUserPalette8bpp(int p[][3],
int nColors)
{
int c;
#ifdef BITMAP_CALL_LOG
if (!vfQuiet)
{
printf("SetUserPalette8bpp(");
for (c = 0; c < nColors; c++)
printf("%d:%d:%d, ", p[c][0], p[c][1], p[c][2]);
printf("%d)\n", nColors);
}
#endif
nColors = (nColors > 256) ? 256 : nColors;
// copy colors
for (c = 0; c < nColors; c++)
{
UserPalette8bpp[c][0] = p[c][0];
UserPalette8bpp[c][1] = p[c][1];
UserPalette8bpp[c][2] = p[c][2];
}
// fill remainder with blacks
for (c = nColors; c < 256; c++)
{
UserPalette8bpp[c][0] = 0;
UserPalette8bpp[c][1] = 0;
UserPalette8bpp[c][2] = 0;
}
}
/**
* Set the 8bpp user palette to the system palette
*/
void
SetUserPalette8bppToDefault8bpp()
{
#ifdef BITMAP_CALL_LOG
if (!vfQuiet)
printf("SetUserPalette8bppToDefault8bpp(): ");
#endif
SetUserPalette8bpp(PalmPalette8bpp, 256);
}
/**
* Convert a RGB triplet to a index within the PalmPalette256[][] table.
*
* @param r the red RGB value
* @param g the green RGB value
* @param b the blue RGB value
* @param palette a pointer to a color pallete array
* @param paletteSize the number of items in the palette array (triples)
* @return the index of the RGB triplet in the palette table.
*/
static int
BMP_RGBToColorIndex(int r,
int g,
int b,
int palette[][3],
int paletteSize)
{
int index, lowValue, i, *diffArray;
// generate the color "differences" for all colors in the palette
diffArray = (int *)malloc(paletteSize * sizeof(int));
for (i = 0; i < paletteSize; i++)
{
diffArray[i] = ((palette[i][0] - r) * (palette[i][0] - r)) +
((palette[i][1] - g) * (palette[i][1] - g)) +
((palette[i][2] - b) * (palette[i][2] - b));
}
// find the palette index that has the smallest color "difference"
// OLD ALGORITHM
//
// index = 0;
// lowValue = diffArray[0];
// for (i=1; i<paletteSize; i++) {
// NEW ALGORITHM
//
// by Scott Ludwig - ensures (0,0,0) in 8bpp color is index 255
index = paletteSize - 1;
lowValue = diffArray[index];
for (i = index - 1; i >= 0; i--)
{
if (diffArray[i] < lowValue)
{
lowValue = diffArray[i];
index = i;
}
}
// clean up
free(diffArray);
return index;
}
/**
* Convert an unsigned long from x86 to Palm Computing Platform format.
*
* @param ul the unsigned long to convert
* @return the unsigned long in Palm Computing Platform format.
*/
static PILRC_ULONG
LLoadX86(PILRC_ULONG ul)
{
PILRC_BYTE *pb;
PILRC_ULONG ulOut;
pb = (PILRC_BYTE *) & ul;
ulOut =
(PILRC_ULONG) ((*(pb + 3) << 24) | (*(pb + 2) << 16) |
(*(pb + 1) << 8) | (*pb));
return ulOut;
}
/**
* Convert an unsigned short from x86 to Palm Computing Platform format.
*
* @param ul the unsigned short to convert
* @return the unsigned short in Palm Computing Platform format.
*/
static PILRC_USHORT
WLoadX86(PILRC_USHORT w)
{
PILRC_BYTE *pb;
PILRC_USHORT wOut;
pb = (PILRC_BYTE *) & w;
wOut = (PILRC_USHORT) ((*(pb + 1) << 8) | (*pb));
return wOut;
}
/**
* Get the number of bytes each bitmap row requires.
*
* @param cx the width of the bitmap.
* @param cbpp the number of bits per byte.
* @param cBitsAlign the os-dependant byte alignment definition.
* @return the number of bytes each bitmap row requires.
*/
static int
BMP_CbRow(int cx,
int cbpp,
int cBitsAlign)
{
return ((cx * cbpp + (cBitsAlign - 1)) & ~(cBitsAlign - 1)) >> 3;
}
/**
* Get a single bit from a 1bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return zero if the bit not set, non-zero otherwise.
*/
static int
BMP_GetBits1bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
int w;
cbRow = BMP_CbRow(cx, 1, cBitsAlign);
pb += cbRow * y + (x >> 3);
w = (*pb & (0x01 << (7 - (x & 7)))) ? 1 : 0;
// return the values we need
*a = 0;
*r = pbmi->bmiColors[w].rgbRed;
*g = pbmi->bmiColors[w].rgbGreen;
*b = pbmi->bmiColors[w].rgbBlue;
return w;
}
/**
* Set a single bit in a 1bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits1bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 1, cBitsAlign);
pb += cbRow * y + (x >> 3);
if (vfLE32)
*pb |= (0x01 << (x & 7));
else
*pb |= (0x01 << (7 - (x & 7)));
}
/**
* Set bits in a 2bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param bits the bits to set (0..3).
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits2bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int bits,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 2, cBitsAlign);
pb += cbRow * y + (x >> 2);
*pb |= (bits << ((3 - (x & 3)) * 2));
}
/**
* Get bits from a 4bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return zero if the bit not set, non-zero otherwise.
* @return the bit representation for the (x,y) pixel.
*/
static int
BMP_GetBits4bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
int w;
cbRow = BMP_CbRow(cx, 4, cBitsAlign);
pb += cbRow * y + (x >> 1);
w = ((x & 1) != 0) ? (*pb & 0x0f) : ((*pb & 0xf0) >> 4);
// return the values we need
*a = 0;
*r = pbmi->bmiColors[w].rgbRed;
*g = pbmi->bmiColors[w].rgbGreen;
*b = pbmi->bmiColors[w].rgbBlue;
return w;
}
/**
* Set bits in a 4bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param bits the bits to set (0..15).
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits4bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int bits,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 4, cBitsAlign);
pb += cbRow * y + (x >> 1);
*pb |= ((x & 1) != 0) ? bits : (bits << 4);
}
/**
* Get bits from a 8bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return the bit representation for the (x,y) pixel.
*/
static int
BMP_GetBits8bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
int w;
cbRow = BMP_CbRow(cx, 8, cBitsAlign);
pb += cbRow * y + x;
w = *pb;
// return the values we need
*a = 0;
*r = pbmi->bmiColors[w].rgbRed;
*g = pbmi->bmiColors[w].rgbGreen;
*b = pbmi->bmiColors[w].rgbBlue;
return w;
}
/**
* Set bits in a 8bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param bits the bits to set (0..255).
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits8bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int bits,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 8, cBitsAlign);
pb += cbRow * y + x;
*pb = bits;
}
/**
* Set bits in a 16bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param bits the bits to set (0..65535).
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits16bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int bits,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 16, cBitsAlign);
pb += cbRow * y + (x * 2);
*pb = (PILRC_BYTE) ((bits & 0xFF00) >> 8); // 5-6-5 (r-g-b) bit
// layout
*(pb + 1) = (PILRC_BYTE) (bits & 0xFF);
}
/**
* Set bits in a 24bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param bits the bits to set (0..65535).
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits24bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int bits,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 24, cBitsAlign);
pb += cbRow * y + (x * 3);
pb[0] = (PILRC_BYTE) ((bits & 0xFF0000) >> 16); // red
pb[1] = (PILRC_BYTE) ((bits & 0x00FF00) >> 8); // green
pb[2] = (PILRC_BYTE) (bits & 0xFF); // blue
}
/**
* Set bits in a 32bpp bitmap
*
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param bits the bits to set (0..65535).
* @param cBitsAlign the os-dependant byte alignment definition.
*/
static void
BMP_SetBits32bpp(int cx,
PILRC_BYTE * pb,
int x,
int y,
int bits,
int cBitsAlign)
{
int cbRow;
cbRow = BMP_CbRow(cx, 32, cBitsAlign);
pb += cbRow * y + (x * 4);
pb[0] = (PILRC_BYTE) ((bits & 0xFF000000) >> 24); // alpha
pb[1] = (PILRC_BYTE) ((bits & 0x00FF0000) >> 16); // red
pb[2] = (PILRC_BYTE) ((bits & 0x0000FF00) >> 8); // green
pb[3] = (PILRC_BYTE) (bits & 0xFF); // blue
}
/**
* Get bits from a 15bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return the bit representation for the (x,y) pixel.
*/
static int
BMP_GetBits15bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
int w;
cbRow = BMP_CbRow(cx, 16, cBitsAlign);
pb += cbRow * y + (x * 2);
// get the pixel
w = (pb[1] << 8) | pb[0];
// return the values we need
*a = 0;
*r = (((w & 0x7C00) >> 7) | ((w & 0x1C00) >> 10));
*g = (((w & 0x03E0) >> 2) | ((w & 0x00e0) >> 5));
*b = (((w & 0x001F) << 3) | (w & 0x0007));
return -1; // no index, direct color
}
/**
* Get bits from a 16bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return the bit representation for the (x,y) pixel.
*/
static int
BMP_GetBits16bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
int w;
cbRow = BMP_CbRow(cx, 16, cBitsAlign);
pb += cbRow * y + (x * 2);
// get the pixel
w = (pb[1] << 8) | pb[0];
// return the values we need
*a = 0;
*r = (((w & 0xF800) >> 8) | ((w & 0x3800) >> 11));
*g = (((w & 0x07E0) >> 3) | ((w & 0x0060) >> 5));
*b = (((w & 0x001F) << 3) | (w & 0x0007)); // MiR 1st July 2002 was *b = (((w & 0x001F) >> 3) | (w & 0x0007));
// which shifts the blue bits the wrong way resulting in color corruption
// in Bitmap displayed on Palm OS
// This should be mirror operation of shift carried out in "BMP_ConvertWindowsBitmap()"
return -1; // no index, direct color
}
/**
* Get bits from a 24bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return the bit representation for the (x,y) pixel.
*/
static int
BMP_GetBits24bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
cbRow = BMP_CbRow(cx, 24, cBitsAlign);
pb += cbRow * y + (x * 3);
// return the values we need
*b = pb[0];
*g = pb[1];
*r = pb[2];
*a = 0;
return -1; // no index, direct color
}
/**
* Get bits from a 32bpp bitmap
*
* @param pbmi bitmap information
* @param cx the width of the bitmap.
* @param pb a reference to the bitmap resource.
* @param x the x-coordinate of the pixel to process.
* @param y the y-coordinate of the pixel to process.
* @param cBitsAlign the os-dependant byte alignment definition.
* @param a alpha channel of pixel
* @param r red channel of pixel
* @param g green channel of pixel
* @param b blue channel of pixel
* @return the bit representation for the (x,y) pixel.
*/
static int
BMP_GetBits32bpp(BITMAPINFO * pbmi,
int cx,
PILRC_BYTE * pb,
int x,
int y,
int cBitsAlign,
int *a,
int *r,
int *g,
int *b)
{
int cbRow;
cbRow = BMP_CbRow(cx, 32, cBitsAlign);
pb += cbRow * y + (x * 4);
// return the values we need
*b = pb[0];
*g = pb[1];
*r = pb[2];
*a = pb[3];
return -1; // no index, direct color
}
/**
* Convert a Microsoft Windows BMP file to Palm Computing resource data.
*
* @param rcbmp a reference to the Palm Computing resource data.
* @param pbResData a reference to the bitmap resource.
* @param bitmaptype the type of bitmap (B+W, Grey, Grey16 or Color)?
* @param colortable does a color table need to be generated?
* @param transparencyData anything we need to know about transparency
*/
static void
BMP_ConvertWindowsBitmap(RCBITMAP * rcbmp,
PILRC_BYTE * pbResData,
int bitmaptype,
BOOL colortable,