forked from mathog/libUEMF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuemf.c
More file actions
5606 lines (5210 loc) · 212 KB
/
uemf.c
File metadata and controls
5606 lines (5210 loc) · 212 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
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
/**
@file uemf.c
@brief Functions for manipulating EMF files and structures.
[U_EMR]_set all take data and return a pointer to memory holding the constructed record.
The size of that record is also returned in recsize.
It is also in the second int32 in the record, but may have been byte swapped and so not usable.
If something goes wrong a NULL pointer is returned and recsize is set to 0.
Compile with "U_VALGRIND" defined defined to enable code which lets valgrind check each record for
uninitialized data.
Compile with "SOL8" defined for Solaris 8 or 9 (Sparc).
*/
/*
File: uemf.c
Version: 0.0.31
Date: 26-JAN-2016
Author: David Mathog, Biology Division, Caltech
email: mathog@caltech.edu
Copyright: 2016 David Mathog and California Institute of Technology (Caltech)
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iconv.h>
#include <wchar.h>
#include <errno.h>
#include <string.h>
#include <limits.h> // for INT_MAX, INT_MIN
#include <math.h> // for U_ROUND()
#include <stddef.h> /* for offsetof() macro */
#if 0
#include <windef.h> //Not actually used, looking for collisions
#include <winnt.h> //Not actually used, looking for collisions
#include <wingdi.h> //Not actually used, looking for collisions
#endif
#include "uemf.h"
//! \cond
/* one prototype from uemf_endian. Put it here because end user should never need to see it, so
not in uemf.h or uemf_endian.h */
void U_swap2(void *ul, unsigned int count);
//! \endcond
/**
\brief Look up the name of the EMR record by type. Returns U_EMR_INVALID if out of range.
\return name of the EMR record, "U_EMR_INVALID" if out of range.
\param idx EMR record type.
*/
char *U_emr_names(unsigned int idx){
if(idx<U_EMR_MIN || idx > U_EMR_MAX){ idx = 0; }
static char *U_EMR_NAMES[U_EMR_MAX+1]={
"U_EMR_INVALID",
"U_EMR_HEADER",
"U_EMR_POLYBEZIER",
"U_EMR_POLYGON",
"U_EMR_POLYLINE",
"U_EMR_POLYBEZIERTO",
"U_EMR_POLYLINETO",
"U_EMR_POLYPOLYLINE",
"U_EMR_POLYPOLYGON",
"U_EMR_SETWINDOWEXTEX",
"U_EMR_SETWINDOWORGEX",
"U_EMR_SETVIEWPORTEXTEX",
"U_EMR_SETVIEWPORTORGEX",
"U_EMR_SETBRUSHORGEX",
"U_EMR_EOF",
"U_EMR_SETPIXELV",
"U_EMR_SETMAPPERFLAGS",
"U_EMR_SETMAPMODE",
"U_EMR_SETBKMODE",
"U_EMR_SETPOLYFILLMODE",
"U_EMR_SETROP2",
"U_EMR_SETSTRETCHBLTMODE",
"U_EMR_SETTEXTALIGN",
"U_EMR_SETCOLORADJUSTMENT",
"U_EMR_SETTEXTCOLOR",
"U_EMR_SETBKCOLOR",
"U_EMR_OFFSETCLIPRGN",
"U_EMR_MOVETOEX",
"U_EMR_SETMETARGN",
"U_EMR_EXCLUDECLIPRECT",
"U_EMR_INTERSECTCLIPRECT",
"U_EMR_SCALEVIEWPORTEXTEX",
"U_EMR_SCALEWINDOWEXTEX",
"U_EMR_SAVEDC",
"U_EMR_RESTOREDC",
"U_EMR_SETWORLDTRANSFORM",
"U_EMR_MODIFYWORLDTRANSFORM",
"U_EMR_SELECTOBJECT",
"U_EMR_CREATEPEN",
"U_EMR_CREATEBRUSHINDIRECT",
"U_EMR_DELETEOBJECT",
"U_EMR_ANGLEARC",
"U_EMR_ELLIPSE",
"U_EMR_RECTANGLE",
"U_EMR_ROUNDRECT",
"U_EMR_ARC",
"U_EMR_CHORD",
"U_EMR_PIE",
"U_EMR_SELECTPALETTE",
"U_EMR_CREATEPALETTE",
"U_EMR_SETPALETTEENTRIES",
"U_EMR_RESIZEPALETTE",
"U_EMR_REALIZEPALETTE",
"U_EMR_EXTFLOODFILL",
"U_EMR_LINETO",
"U_EMR_ARCTO",
"U_EMR_POLYDRAW",
"U_EMR_SETARCDIRECTION",
"U_EMR_SETMITERLIMIT",
"U_EMR_BEGINPATH",
"U_EMR_ENDPATH",
"U_EMR_CLOSEFIGURE",
"U_EMR_FILLPATH",
"U_EMR_STROKEANDFILLPATH",
"U_EMR_STROKEPATH",
"U_EMR_FLATTENPATH",
"U_EMR_WIDENPATH",
"U_EMR_SELECTCLIPPATH",
"U_EMR_ABORTPATH",
"U_EMR_UNDEF69",
"U_EMR_COMMENT",
"U_EMR_FILLRGN",
"U_EMR_FRAMERGN",
"U_EMR_INVERTRGN",
"U_EMR_PAINTRGN",
"U_EMR_EXTSELECTCLIPRGN",
"U_EMR_BITBLT",
"U_EMR_STRETCHBLT",
"U_EMR_MASKBLT",
"U_EMR_PLGBLT",
"U_EMR_SETDIBITSTODEVICE",
"U_EMR_STRETCHDIBITS",
"U_EMR_EXTCREATEFONTINDIRECTW",
"U_EMR_EXTTEXTOUTA",
"U_EMR_EXTTEXTOUTW",
"U_EMR_POLYBEZIER16",
"U_EMR_POLYGON16",
"U_EMR_POLYLINE16",
"U_EMR_POLYBEZIERTO16",
"U_EMR_POLYLINETO16",
"U_EMR_POLYPOLYLINE16",
"U_EMR_POLYPOLYGON16",
"U_EMR_POLYDRAW16",
"U_EMR_CREATEMONOBRUSH",
"U_EMR_CREATEDIBPATTERNBRUSHPT",
"U_EMR_EXTCREATEPEN",
"U_EMR_POLYTEXTOUTA",
"U_EMR_POLYTEXTOUTW",
"U_EMR_SETICMMODE",
"U_EMR_CREATECOLORSPACE",
"U_EMR_SETCOLORSPACE",
"U_EMR_DELETECOLORSPACE",
"U_EMR_GLSRECORD",
"U_EMR_GLSBOUNDEDRECORD",
"U_EMR_PIXELFORMAT",
"U_EMR_DRAWESCAPE",
"U_EMR_EXTESCAPE",
"U_EMR_UNDEF107",
"U_EMR_SMALLTEXTOUT",
"U_EMR_FORCEUFIMAPPING",
"U_EMR_NAMEDESCAPE",
"U_EMR_COLORCORRECTPALETTE",
"U_EMR_SETICMPROFILEA",
"U_EMR_SETICMPROFILEW",
"U_EMR_ALPHABLEND",
"U_EMR_SETLAYOUT",
"U_EMR_TRANSPARENTBLT",
"U_EMR_UNDEF117",
"U_EMR_GRADIENTFILL",
"U_EMR_SETLINKEDUFIS",
"U_EMR_SETTEXTJUSTIFICATION",
"U_EMR_COLORMATCHTOTARGETW",
"U_EMR_CREATECOLORSPACEW"
};
return(U_EMR_NAMES[idx]);
}
/* **********************************************************************************************
These definitions are for code pieces that are used many times in the following implementation. These
definitions are not needed in end user code, so they are here rather than in uemf.h.
*********************************************************************************************** */
//! @cond
// this one may also be used A=Msk,B=MskBmi and F=cbMsk
#define SET_CB_FROM_PXBMI(A,B,C,D,E,F) /* A=Px, B=Bmi, C=cbImage, D=cbImage4, E=cbBmi, F=cbPx */ \
if(A){\
if(!B)return(NULL); /* size is derived from U_BITMAPINFO, but NOT from its size field, go figure*/ \
C = F;\
D = UP4(C); /* pixel array might not be a multiples of 4 bytes*/ \
E = sizeof(U_BITMAPINFOHEADER) + 4 * get_real_color_count((const char *) &(B->bmiHeader)); /* bmiheader + colortable*/ \
}\
else { C = 0; D = 0; E=0; }
// variable "off" must be declared in the function
#define APPEND_PXBMISRC(A,B,C,D,E,F,G) /* A=record, B=U_EMR,C=cbBmi, D=Bmi, E=Px, F=cbImage, G=cbImage4 */ \
if(C){\
memcpy(A + off, D, C);\
((B *) A)->offBmiSrc = off;\
((B *) A)->cbBmiSrc = C;\
off += C;\
memcpy(A + off, E, F);\
((B *) A)->offBitsSrc = off;\
((B *) A)->cbBitsSrc = F;\
if(G - F){ \
off += F;\
memset(A + off, 0, G - F); \
}\
}\
else {\
((B *) A)->offBmiSrc = 0;\
((B *) A)->cbBmiSrc = 0;\
((B *) A)->offBitsSrc = 0;\
((B *) A)->cbBitsSrc = 0;\
}
// variable "off" must be declared in the function
#define APPEND_MSKBMISRC(A,B,C,D,E,F,G) /* A=record, B=U_EMR*,C=cbMskBmi, D=MskBmi, E=Msk, F=cbMskImage, G=cbMskImage4 */ \
if(C){\
memcpy(A + off, D, C);\
((B *) A)->offBmiMask = off;\
((B *) A)->cbBmiMask = C;\
off += C;\
memcpy(A + off, Msk, F);\
((B *) A)->offBitsMask = off;\
((B *) A)->cbBitsMask = F;\
if(G - F){ memset(A + off, 0, G - F); }\
}\
else {\
((B *) A)->offBmiMask = 0;\
((B *) A)->cbBmiMask = 0;\
((B *) A)->offBitsMask = 0;\
((B *) A)->cbBitsMask = 0;\
}
//! @endcond
/* **********************************************************************************************
These functions are used for development and debugging and should be be includied in production code.
*********************************************************************************************** */
/**
\brief Debugging utility, used with valgrind to find uninitialized values. Not for use in production code.
\param buf memory area to examine !
\param size length in bytes of buf!
*/
int memprobe(
const void *buf,
size_t size
){
int sum=0;
char *ptr=(char *)buf;
for(;size;size--,ptr++){ sum += *ptr; } // read all bytes, trigger valgrind warning if any uninitialized
return(sum);
}
/**
\brief Dump an EMFHANDLES structure. Not for use in production code.
\param string Text to output before dumping eht structure
\param handle Handle
\param eht EMFHANDLES structure to dump
*/
void dumpeht(
char *string,
unsigned int *handle,
EMFHANDLES *eht
){
uint32_t i;
printf("%s\n",string);
printf("sptr: %d peak: %d top: %d\n",eht->sptr,eht->peak,eht->top);
if(handle){
printf("handle: %d \n",*handle);
}
for(i=0;i<=5;i++){
printf("table[%d]: %d\n",i,eht->table[i]);
}
for(i=1;i<=5;i++){
printf("stack[%d]: %d\n",i,eht->stack[i]);
}
}
/* **********************************************************************************************
These functions are used for Image conversions and other
utility operations. Character type conversions are in uemf_utf.c
*********************************************************************************************** */
/**
\brief Make up an approximate dx array to pass to emrtext_set(), based on character height and weight.
Take abs. value of character height, get width by multiplying by 0.6, and correct weight
approximately, with formula (measured on screen for one text line of Arial).
Caller is responsible for free() on the returned pointer.
\return pointer to dx array
\param height character height (absolute value will be used)
\param weight LF_Weight Enumeration (character weight)
\param members Number of entries to put into dx
*/
uint32_t *dx_set(
int32_t height,
uint32_t weight,
uint32_t members
){
uint32_t i, width, *dx;
dx = (uint32_t *) malloc(members * sizeof(uint32_t));
if(dx){
if(U_FW_DONTCARE == weight)weight=U_FW_NORMAL;
width = (uint32_t) U_ROUND(((float) (height > 0 ? height : -height)) * 0.6 * (0.00024*(float) weight + 0.904));
for ( i = 0; i < members; i++ ){ dx[i] = width; }
}
return(dx);
}
/**
\brief Look up the properties (a bit map) of a type of EMR record.
Bits that may be set are defined in "Draw Properties" in uemf.h, they are U_DRAW_NOTEMPTY, etc..
\return bitmap of EMR record properties, or U_EMR_INVALID on error or release of all memory
\param type EMR record type. If U_EMR_INVALID release memory. (There is no U_EMR_INVALID EMR record type)
*/
uint32_t emr_properties(uint32_t type){
static uint32_t *table=NULL;
uint32_t result = U_EMR_INVALID; // initialized to indicate an error (on a lookup) or nothing (on a memory release)
if(type == U_EMR_INVALID){
if(table)free(table);
table=NULL;
}
else if(type>=1 && type<U_EMR_MAX){
if(!table){
table = (uint32_t *) malloc(sizeof(uint32_t)*(1 + U_EMR_MAX));
if(!table)return(result);
// 0x80 0x40 0x20 0x10 0x08 0x04 0x02 0x01
// Path properties (U_DRAW_*) TEXT ALTERS ONLYTO VISIBLE
// PATH FORCE CLOSED NOTEMPTY
table[ 0] = 0x00; // Does not map to any EMR record
table[ 1] = 0x80; // U_EMRHEADER 1 0 0 0 0 0 0 0
table[ 2] = 0x83; // U_EMRPOLYBEZIER 1 0 0 0 0 0 1 1
table[ 3] = 0x87; // U_EMRPOLYGON 1 0 0 0 0 1 1 1
table[ 4] = 0x83; // U_EMRPOLYLINE 1 0 0 0 0 0 1 1
table[ 5] = 0x8B; // U_EMRPOLYBEZIERTO 1 0 0 0 1 0 1 1
table[ 6] = 0x8B; // U_EMRPOLYLINETO 1 0 0 0 1 0 1 1
table[ 7] = 0x83; // U_EMRPOLYPOLYLINE 1 0 0 0 0 0 1 1
table[ 8] = 0x87; // U_EMRPOLYPOLYGON 1 0 0 0 0 1 1 1
table[ 9] = 0xA0; // U_EMRSETWINDOWEXTEX 1 0 1 0 0 0 0 0
table[ 10] = 0xA0; // U_EMRSETWINDOWORGEX 1 0 1 0 0 0 0 0
table[ 11] = 0xA0; // U_EMRSETVIEWPORTEXTEX 1 0 1 0 0 0 0 0
table[ 12] = 0xA0; // U_EMRSETVIEWPORTORGEX 1 0 1 0 0 0 0 0
table[ 13] = 0xA0; // U_EMRSETBRUSHORGEX 1 0 1 0 0 0 0 0
table[ 14] = 0x82; // U_EMREOF 1 0 1 0 0 0 0 0 Force out any pending draw
table[ 15] = 0x82; // U_EMRSETPIXELV 1 0 0 0 0 0 1 0
table[ 16] = 0xA0; // U_EMRSETMAPPERFLAGS 1 0 1 0 0 0 0 0
table[ 17] = 0xA0; // U_EMRSETMAPMODE 1 0 1 0 0 0 0 0
table[ 18] = 0x20; // U_EMRSETBKMODE 0 0 1 0 0 0 0 0
table[ 19] = 0xA0; // U_EMRSETPOLYFILLMODE 1 0 1 0 0 0 0 0
table[ 20] = 0xA0; // U_EMRSETROP2 1 0 1 0 0 0 0 0
table[ 21] = 0xA0; // U_EMRSETSTRETCHBLTMODE 1 0 1 0 0 0 0 0
table[ 22] = 0x20; // U_EMRSETTEXTALIGN 0 0 1 0 0 0 0 0
table[ 23] = 0xA0; // U_EMRSETCOLORADJUSTMENT 1 0 1 0 0 0 0 0
table[ 24] = 0x20; // U_EMRSETTEXTCOLOR 0 0 1 0 0 0 0 0
table[ 25] = 0x20; // U_EMRSETBKCOLOR 0 0 1 0 0 0 0 0
table[ 26] = 0xA0; // U_EMROFFSETCLIPRGN 1 0 1 0 0 0 0 0
table[ 27] = 0x89; // U_EMRMOVETOEX 1 0 0 0 1 0 0 1
table[ 28] = 0xA0; // U_EMRSETMETARGN 1 0 1 0 0 0 0 0
table[ 29] = 0xA0; // U_EMREXCLUDECLIPRECT 1 0 1 0 0 0 0 0
table[ 30] = 0xA0; // U_EMRINTERSECTCLIPRECT 1 0 1 0 0 0 0 0
table[ 31] = 0xA0; // U_EMRSCALEVIEWPORTEXTEX 1 0 1 0 0 0 0 0
table[ 32] = 0xA0; // U_EMRSCALEWINDOWEXTEX 1 0 1 0 0 0 0 0
table[ 33] = 0xA0; // U_EMRSAVEDC 1 0 1 0 0 0 0 0
table[ 34] = 0xA0; // U_EMRRESTOREDC 1 0 1 0 0 0 0 0
table[ 35] = 0xA0; // U_EMRSETWORLDTRANSFORM 1 0 1 0 0 0 0 0
table[ 36] = 0xA0; // U_EMRMODIFYWORLDTRANSFORM 1 0 1 0 0 0 0 0
table[ 37] = 0x20; // U_EMRSELECTOBJECT 0 0 1 0 0 0 0 0
table[ 38] = 0x20; // U_EMRCREATEPEN 0 0 1 0 0 0 0 0
table[ 39] = 0x20; // U_EMRCREATEBRUSHINDIRECT 0 0 1 0 0 0 0 0
table[ 40] = 0x20; // U_EMRDELETEOBJECT 0 0 1 0 0 0 0 0
table[ 41] = 0x83; // U_EMRANGLEARC 1 0 0 0 0 0 1 1
table[ 42] = 0x87; // U_EMRELLIPSE 1 0 0 0 0 1 1 1
table[ 43] = 0x87; // U_EMRRECTANGLE 1 0 0 0 0 1 1 1
table[ 44] = 0x87; // U_EMRROUNDRECT 1 0 0 0 0 1 1 1
table[ 45] = 0x83; // U_EMRARC 1 0 0 0 0 0 1 1
table[ 46] = 0x87; // U_EMRCHORD 1 0 0 0 0 1 1 1
table[ 47] = 0x87; // U_EMRPIE 1 0 0 0 0 1 1 1
table[ 48] = 0xA0; // U_EMRSELECTPALETTE 1 0 1 0 0 0 0 0
table[ 49] = 0xA0; // U_EMRCREATEPALETTE 1 0 1 0 0 0 0 0
table[ 50] = 0xA0; // U_EMRSETPALETTEENTRIES 1 0 1 0 0 0 0 0
table[ 51] = 0xA0; // U_EMRRESIZEPALETTE 1 0 1 0 0 0 0 0
table[ 52] = 0xA0; // U_EMRREALIZEPALETTE 1 0 1 0 0 0 0 0
table[ 53] = 0x82; // U_EMREXTFLOODFILL 1 0 0 0 0 0 1 0
table[ 54] = 0x8B; // U_EMRLINETO 1 0 0 0 1 0 1 1
table[ 55] = 0x8B; // U_EMRARCTO 1 0 0 0 1 0 1 1
table[ 56] = 0x83; // U_EMRPOLYDRAW 1 0 0 0 0 0 1 1
table[ 57] = 0xA0; // U_EMRSETARCDIRECTION 1 0 1 0 0 0 0 0
table[ 58] = 0xA0; // U_EMRSETMITERLIMIT 1 0 1 0 0 0 0 0
table[ 59] = 0xE0; // U_EMRBEGINPATH 1 1 1 0 0 0 0 0
table[ 60] = 0x80; // U_EMRENDPATH 1 0 0 0 0 0 0 0
table[ 61] = 0x84; // U_EMRCLOSEFIGURE 1 0 0 0 0 1 0 0
table[ 62] = 0x94; // U_EMRFILLPATH 1 0 0 1 0 1 0 0
table[ 63] = 0x94; // U_EMRSTROKEANDFILLPATH 1 0 0 1 0 1 0 0
table[ 64] = 0x90; // U_EMRSTROKEPATH 1 0 0 1 0 0 0 0
table[ 65] = 0xA0; // U_EMRFLATTENPATH 1 0 1 0 0 0 0 0
table[ 66] = 0xA0; // U_EMRWIDENPATH 1 0 1 0 0 0 0 0
table[ 67] = 0x80; // U_EMRSELECTCLIPPATH 1 0 0 0 0 0 0 0 consumes the path, draws nothing
table[ 68] = 0xA0; // U_EMRABORTPATH 1 0 1 0 0 0 0 0
table[ 69] = 0xA0; // U_EMRUNDEF69 1 0 1 0 0 0 0 0
table[ 70] = 0x00; // U_EMRCOMMENT 0 0 0 0 0 0 0 0
table[ 71] = 0x82; // U_EMRFILLRGN 1 0 0 0 0 0 1 0
table[ 72] = 0x82; // U_EMRFRAMERGN 1 0 0 0 0 0 1 0
table[ 73] = 0x82; // U_EMRINVERTRGN 1 0 0 0 0 0 1 0
table[ 74] = 0x82; // U_EMRPAINTRGN 1 0 0 0 0 0 1 0
table[ 75] = 0xA0; // U_EMREXTSELECTCLIPRGN 1 0 1 0 0 0 0 0
table[ 76] = 0x82; // U_EMRBITBLT 1 0 0 0 0 0 1 0
table[ 77] = 0x82; // U_EMRSTRETCHBLT 1 0 0 0 0 0 1 0
table[ 78] = 0x82; // U_EMRMASKBLT 1 0 0 0 0 0 1 0
table[ 79] = 0x82; // U_EMRPLGBLT 1 0 0 0 0 0 1 0
table[ 80] = 0xA0; // U_EMRSETDIBITSTODEVICE 1 0 1 0 0 0 0 0
table[ 81] = 0xA0; // U_EMRSTRETCHDIBITS 1 0 1 0 0 0 0 0
table[ 82] = 0x20; // U_EMREXTCREATEFONTINDIRECTW 0 0 1 0 0 0 0 0
table[ 83] = 0x02; // U_EMREXTTEXTOUTA 0 0 0 0 0 0 1 0
table[ 84] = 0x02; // U_EMREXTTEXTOUTW 0 0 0 0 0 0 1 0
table[ 85] = 0x83; // U_EMRPOLYBEZIER16 1 0 0 0 0 0 1 1
table[ 86] = 0x83; // U_EMRPOLYGON16 1 0 0 0 0 0 1 1
table[ 87] = 0x83; // U_EMRPOLYLINE16 1 0 0 0 0 0 1 1
table[ 88] = 0x8B; // U_EMRPOLYBEZIERTO16 1 0 0 0 1 0 1 1
table[ 89] = 0x8B; // U_EMRPOLYLINETO16 1 0 0 0 1 0 1 1
table[ 90] = 0x83; // U_EMRPOLYPOLYLINE16 1 0 0 0 0 0 1 1
table[ 91] = 0x87; // U_EMRPOLYPOLYGON16 1 0 0 0 0 1 1 1
table[ 92] = 0x83; // U_EMRPOLYDRAW16 1 0 0 0 0 0 1 1
table[ 93] = 0x80; // U_EMRCREATEMONOBRUSH 1 0 0 0 0 0 0 0 Not selected yet, so no change in drawing conditions
table[ 94] = 0x80; // U_EMRCREATEDIBPATTERNBRUSHPT 1 0 0 0 0 0 0 0 "
table[ 95] = 0x00; // U_EMREXTCREATEPEN 0 0 0 0 0 0 0 0 "
table[ 96] = 0x02; // U_EMRPOLYTEXTOUTA 0 0 0 0 0 0 1 0
table[ 97] = 0x02; // U_EMRPOLYTEXTOUTW 0 0 0 0 0 0 1 0
table[ 98] = 0xA0; // U_EMRSETICMMODE 1 0 1 0 0 0 0 0
table[ 99] = 0xA0; // U_EMRCREATECOLORSPACE 1 0 1 0 0 0 0 0
table[100] = 0xA0; // U_EMRSETCOLORSPACE 1 0 1 0 0 0 0 0
table[101] = 0xA0; // U_EMRDELETECOLORSPACE 1 0 1 0 0 0 0 0
table[102] = 0xA0; // U_EMRGLSRECORD 1 0 1 0 0 0 0 0
table[103] = 0xA0; // U_EMRGLSBOUNDEDRECORD 1 0 1 0 0 0 0 0
table[104] = 0xA0; // U_EMRPIXELFORMAT 1 0 1 0 0 0 0 0
table[105] = 0xA0; // U_EMRDRAWESCAPE 1 0 1 0 0 0 0 0
table[106] = 0xA0; // U_EMREXTESCAPE 1 0 1 0 0 0 0 0
table[107] = 0xA0; // U_EMRUNDEF107 1 0 1 0 0 0 0 0
table[108] = 0x02; // U_EMRSMALLTEXTOUT 0 0 0 0 0 0 1 0
table[109] = 0xA0; // U_EMRFORCEUFIMAPPING 1 0 1 0 0 0 0 0
table[110] = 0xA0; // U_EMRNAMEDESCAPE 1 0 1 0 0 0 0 0
table[111] = 0xA0; // U_EMRCOLORCORRECTPALETTE 1 0 1 0 0 0 0 0
table[112] = 0xA0; // U_EMRSETICMPROFILEA 1 0 1 0 0 0 0 0
table[113] = 0xA0; // U_EMRSETICMPROFILEW 1 0 1 0 0 0 0 0
table[114] = 0x82; // U_EMRALPHABLEND 1 0 0 0 0 0 1 0
table[115] = 0xA0; // U_EMRSETLAYOUT 1 0 1 0 0 0 0 0
table[116] = 0x82; // U_EMRTRANSPARENTBLT 1 0 0 0 0 0 1 0
table[117] = 0xA0; // U_EMRUNDEF117 1 0 1 0 0 0 0 0
table[118] = 0x82; // U_EMRGRADIENTFILL 1 0 1 0 0 0 1 0
table[119] = 0xA0; // U_EMRSETLINKEDUFIS 1 0 1 0 0 0 0 0
table[120] = 0x20; // U_EMRSETTEXTJUSTIFICATION 0 0 1 0 0 0 0 0
table[121] = 0xA0; // U_EMRCOLORMATCHTOTARGETW 1 0 1 0 0 0 0 0
table[122] = 0xA0; // U_EMRCREATECOLORSPACEW 1 0 1 0 0 0 0 0
}
result = table[type];
}
return(result);
}
/**
\brief Derive from bounding rect, start and end radials, for arc, chord, or pie, the center, start, and end points, and the bounding rectangle.
\return 0 on success, other values on errors.
\param rclBox bounding rectangle
\param ArcStart start of arc
\param ArcEnd end of arc
\param f1 1 if rotation angle >= 180, else 0
\param f2 Rotation direction, 1 if counter clockwise, else 0
\param center Center coordinates
\param start Start coordinates (point on the ellipse defined by rect)
\param end End coordinates (point on the ellipse defined by rect)
\param size W,H of the x,y axes of the bounding rectangle.
*/
int emr_arc_points_common(
PU_RECTL rclBox,
PU_POINTL ArcStart,
PU_POINTL ArcEnd,
int *f1,
int f2,
PU_PAIRF center,
PU_PAIRF start,
PU_PAIRF end,
PU_PAIRF size
){
U_PAIRF estart; // EMF start position, defines a radial
U_PAIRF eend; // EMF end position, defines a radial
U_PAIRF vec_estart; // define a unit vector from the center to estart
U_PAIRF vec_eend; // define a unit vector from the center to eend
U_PAIRF radii; // x,y radii of ellipse
U_PAIRF ratio; // intermediate value
float scale, cross;
center->x = ((float)(rclBox->left + rclBox->right ))/2.0;
center->y = ((float)(rclBox->top + rclBox->bottom))/2.0;
size->x = (float)(rclBox->right - rclBox->left );
size->y = (float)(rclBox->bottom - rclBox->top );
estart.x = (float)(ArcStart->x);
estart.y = (float)(ArcStart->y);
eend.x = (float)(ArcEnd->x);
eend.y = (float)(ArcEnd->y);
radii.x = size->x/2.0;
radii.y = size->y/2.0;
vec_estart.x = (estart.x - center->x); // initial vector, not unit length
vec_estart.y = (estart.y - center->y);
scale = sqrt(vec_estart.x*vec_estart.x + vec_estart.y*vec_estart.y);
if(!scale)return(1); // bogus record, has start at center
vec_estart.x /= scale; // now a unit vector
vec_estart.y /= scale;
vec_eend.x = (eend.x - center->x); // initial vector, not unit length
vec_eend.y = (eend.y - center->y);
scale = sqrt(vec_eend.x*vec_eend.x + vec_eend.y*vec_eend.y);
if(!scale)return(2); // bogus record, has end at center
vec_eend.x /= scale; // now a unit vector
vec_eend.y /= scale;
// Find the intersection of the vectors with the ellipse. With no loss of generality
// we can translate the ellipse to the origin, then we just need to find tu (t a factor, u the unit vector)
// that also satisfies (x/Rx)^2 + (y/Ry)^2 = 1. x is t*(ux), y is t*(uy), where ux,uy are the x,y components
// of the unit vector. Substituting gives:
// (t*(ux)/Rx)^2 + (t*(uy)/Ry)^2 = 1
// t^2 = 1/( (ux/Rx)^2 + (uy/Ry)^2 )
// t = sqrt(1/( (ux/Rx)^2 + (uy/Ry)^2 ))
ratio.x = vec_estart.x/radii.x;
ratio.y = vec_estart.y/radii.y;
ratio.x *= ratio.x; // we only use the square
ratio.y *= ratio.y;
scale = 1.0/sqrt(ratio.x + ratio.y);
start->x = center->x + scale * vec_estart.x;
start->y = center->y + scale * vec_estart.y;
ratio.x = vec_eend.x/radii.x;
ratio.y = vec_eend.y/radii.y;
ratio.x *= ratio.x; // we only use the square
ratio.y *= ratio.y;
scale = 1.0/sqrt(ratio.x + ratio.y);
end->x = center->x + scale * vec_eend.x;
end->y = center->y + scale * vec_eend.y;
//lastly figure out if the swept angle is >180 degrees or not, based on the direction of rotation
//and the two unit vectors.
cross = vec_estart.x * vec_eend.y - vec_estart.y * vec_eend.x;
if(!f2){ // counter clockwise rotation
if(cross >=0){ *f1 = 1; }
else { *f1 = 0; }
}
else {
if(cross >=0){ *f1 = 0; }
else { *f1 = 1; }
}
return(0);
}
/**
\brief Derive from an EMF arc, chord, or pie the center, start, and end points, and the bounding rectangle.
\return 0 on success, other values on errors.
\param record U_EMRPIE, U_EMRCHORD, or _EMRARC record
\param f1 1 if rotation angle >= 180, else 0
\param f2 Rotation direction, 1 if counter clockwise, else 0
\param center Center coordinates
\param start Start coordinates (point on the ellipse defined by rect)
\param end End coordinates (point on the ellipse defined by rect)
\param size W,H of the x,y axes of the bounding rectangle.
*/
int emr_arc_points(
PU_ENHMETARECORD record,
int *f1,
int f2,
PU_PAIRF center,
PU_PAIRF start,
PU_PAIRF end,
PU_PAIRF size
){
PU_EMRARC pEmr = (PU_EMRARC) (record);
return emr_arc_points_common(&(pEmr->rclBox), &(pEmr->ptlStart), &(pEmr->ptlEnd), f1, f2, center, start, end, size );
}
/**
\brief Convert a U_RGBA 32 bit pixmap to one of many different types of DIB pixmaps.
Conversions to formats using color tables assume that the color table can hold every color
in the input image. If that assumption is false then the conversion will fail. Conversion
from 8 bit color to N bit colors (N<8) do so by shifting the appropriate number of bits.
\return 0 on success, other values on errors.
\param px DIB pixel array
\param cbPx DIB pixel array size in bytes
\param ct DIB color table
\param numCt DIB color table number of entries
\param rgba_px U_RGBA pixel array (32 bits)
\param w Width of pixel array
\param h Height of pixel array
\param stride Row stride of input pixel array in bytes
\param colortype DIB BitCount Enumeration
\param use_ct If true use color table (only for 1-16 bit DIBs).
\param invert If DIB rows are in opposite order from RGBA rows
*/
int RGBA_to_DIB(
char **px,
uint32_t *cbPx,
PU_RGBQUAD *ct,
int *numCt,
const char *rgba_px,
int w,
int h,
int stride,
uint32_t colortype,
int use_ct,
int invert
){
int bs;
int pad;
int i,j,k;
int istart, iend, iinc;
uint8_t r,g,b,a,tmp8;
char *pxptr;
const char *rptr;
int found;
int usedbytes;
U_RGBQUAD color;
PU_RGBQUAD lct;
int32_t index;
*px=NULL;
*ct=NULL;
*numCt=0;
*cbPx=0;
// sanity checking
if(!w || !h || !stride || !colortype || !rgba_px)return(1);
if(use_ct && colortype >= U_BCBM_COLOR16)return(2); //color tables not used above 16 bit pixels
if(!use_ct && colortype < U_BCBM_COLOR16)return(3); //color tables mandatory for < 16 bit
bs = colortype/8;
if(bs<1){
usedbytes = (w*colortype + 7)/8; // width of line in fully and partially occupied bytes
}
else {
usedbytes = w*bs;
}
pad = UP4(usedbytes) - usedbytes; // DIB rows must be aligned on 4 byte boundaries, they are padded at the end to accomplish this.;
*cbPx = h * (usedbytes + pad); // Rows must start on a 4 byte boundary!
*px = (char *) malloc(*cbPx);
if(!px)return(4);
if(use_ct){
*numCt = 1<< colortype;
if(*numCt >w*h)*numCt=w*h;
lct = (PU_RGBQUAD) malloc(*numCt * sizeof(U_RGBQUAD));
if(!lct)return(5);
*ct = lct;
}
if(invert){
istart = h-1;
iend = -1;
iinc = -1;
}
else {
istart = 0;
iend = h;
iinc = 1;
}
found = 0;
tmp8 = 0;
pxptr = *px;
for(i=istart; i!=iend; i+=iinc){
rptr= rgba_px + i*stride;
for(j=0; j<w; j++){
r = *rptr++;
g = *rptr++;
b = *rptr++;
a = *rptr++;
if(use_ct){
color = U_BGRA(r,g,b,a); // color has order in memory: b,g,r,a, same as EMF+ ARGB
index = -1;
for(lct = *ct, k=0; k<found; k++,lct++){ // Is this color in the table (VERY inefficient if there are a lot of colors!!!)
if(*(uint32_t *)lct != *(uint32_t *) &color)continue;
index =k;
break;
}
if(index==-1){ // add a color
found++;
if(found > *numCt){ // More colors found than are supported by the color table
free(*ct);
free(*px);
*numCt=0;
*cbPx=0;
return(6);
}
index = found - 1;
*lct = color;
}
switch(colortype){
case U_BCBM_MONOCHROME: // 2 colors. bmiColors array has two entries
tmp8 = tmp8 >> 1; // This seems wrong, as it fills from the top of each byte. But it works.
tmp8 |= index << 7;
if(!((j+1) % 8)){
*pxptr++ = tmp8;
tmp8 = 0;
}
break;
case U_BCBM_COLOR4: // 2^4 colors. bmiColors array has 16 entries
tmp8 = tmp8 << 4;
tmp8 |= index;
if(!((j+1) % 2)){
*pxptr++ = tmp8;
tmp8 = 0;
}
break;
case U_BCBM_COLOR8: // 2^8 colors. bmiColors array has 256 entries
tmp8 = index;
*pxptr++ = tmp8;
break;
case U_BCBM_COLOR16: // 2^16 colors. (Several different color methods))
case U_BCBM_COLOR24: // 2^24 colors. bmiColors is not used. Pixels are U_RGBTRIPLE.
case U_BCBM_COLOR32: // 2^32 colors. bmiColors is not used. Pixels are U_RGBQUAD.
case U_BCBM_EXPLICIT: // Derinved from JPG or PNG compressed image or ?
default:
return(7); // This should not be possible, but might happen with memory corruption
}
}
else {
switch(colortype){
case U_BCBM_COLOR16: // 2^16 colors. (Several different color methods))
b /= 8; g /= 8; r /= 8;
// Do it in this way so that the bytes are always stored Little Endian
tmp8 = b;
tmp8 |= g<<5; // least significant 3 bits of green
*pxptr++ = tmp8;
tmp8 = g>>3; // most significant 2 bits of green (there are only 5 bits of data)
tmp8 |= r<<2;
*pxptr++ = tmp8;
break;
case U_BCBM_COLOR24: // 2^24 colors. bmiColors is not used. Pixels are U_RGBTRIPLE.
*pxptr++ = b;
*pxptr++ = g;
*pxptr++ = r;
break;
case U_BCBM_COLOR32: // 2^32 colors. bmiColors is not used. Pixels are U_RGBQUAD.
*pxptr++ = b;
*pxptr++ = g;
*pxptr++ = r;
*pxptr++ = a;
break;
case U_BCBM_MONOCHROME: // 2 colors. bmiColors array has two entries
case U_BCBM_COLOR4: // 2^4 colors. bmiColors array has 16 entries
case U_BCBM_COLOR8: // 2^8 colors. bmiColors array has 256 entries
case U_BCBM_EXPLICIT: // Derinved from JPG or PNG compressed image or ?
default:
return(7); // This should not be possible, but might happen with memory corruption
}
}
}
if( use_ct && colortype == U_BCBM_MONOCHROME && (j % 8) ){
*pxptr++ = tmp8; // Write last few indices
tmp8 = 0;
}
if( use_ct && colortype == U_BCBM_COLOR4 && (j % 2) ){
*pxptr++ = tmp8; // Write last few indices
tmp8 = 0;
}
if(pad){
memset(pxptr,0,pad); // not strictly necessary, but set all bytes so that we can find important unset ones with valgrind
pxptr += pad;
}
}
return(0);
}
/**
\brief Get the actual number of colors in the color table from the BitMapInfoHeader.
\return Number of entries in the color table.
\param Bmih char * pointer to the U_BITMAPINFOHEADER
BitmapInfoHeader may list 0 for some types which implies the maximum value.
If the image is big enough, that is set by the bit count, as in 256 for an 8
bit image.
If the image is smaller it is set by width * height.
Note, this may be called by WMF code, so it is not safe to assume the data is aligned.
*/
int get_real_color_count(
const char *Bmih
){
int Colors, BitCount, Width, Height;
uint32_t utmp4;
uint16_t utmp2;
int32_t tmp4;
char *cBmih = (char *) Bmih;
memcpy(&utmp4, cBmih + offsetof(U_BITMAPINFOHEADER,biClrUsed), 4); Colors = utmp4;
memcpy(&utmp2, cBmih + offsetof(U_BITMAPINFOHEADER,biBitCount), 2); BitCount = utmp2;
memcpy(&tmp4, cBmih + offsetof(U_BITMAPINFOHEADER,biWidth), 4); Width = tmp4;
memcpy(&tmp4, cBmih + offsetof(U_BITMAPINFOHEADER,biHeight), 4); Height = tmp4;
return(get_real_color_icount(Colors, BitCount, Width, Height));
}
/**
\brief Get the actual number of colors in the color table from the ClrUsed, BitCount, Width, and Height.
\return Number of entries in the color table.
\param Colors Number of colors in the table.
\param BitCount BitCount Enumeration
\param Width bitmap width
\param Height bitmap height
*/
int get_real_color_icount(
int Colors,
int BitCount,
int Width,
int Height
){
int area = Width * Height;
if(area < 0){ area = -area; } /* Height might be negative */
if(Colors == 0){
if( BitCount == U_BCBM_MONOCHROME){ Colors = 2; }
else if(BitCount == U_BCBM_COLOR4 ){ Colors = 16; }
else if(BitCount == U_BCBM_COLOR8 ){ Colors = 256; }
if(Colors > area){ Colors = area; }
}
return(Colors);
}
/**
\brief Get the DIB parameters from the BMI of the record for use by DBI_to_RGBA()
\return BI_Compression Enumeration. For anything other than U_BI_RGB values other than px may not be valid.
\param record pointer to EMR record that has a U_BITMAPINFO and bitmap
\param offBitsSrc Offset to the bitmap
\param offBmiSrc Offset to the U_BITMAPINFO
\param px pointer to DIB pixel array in pEmr
\param ct pointer to DIB color table in pEmr
\param numCt DIB color table number of entries, for PNG or JPG returns the number of bytes in the image
\param width Width of pixel array
\param height Height of pixel array (always returned as a positive number)
\param colortype DIB BitCount Enumeration
\param invert If DIB rows are in opposite order from RGBA rows
*/
int get_DIB_params(
const char *record,
uint32_t offBitsSrc,
uint32_t offBmiSrc,
const char **px,
const U_RGBQUAD **ct,
uint32_t *numCt,
uint32_t *width,
uint32_t *height,
uint32_t *colortype,
uint32_t *invert
){
uint32_t bic;
PU_BITMAPINFO Bmi = (PU_BITMAPINFO)(record + offBmiSrc);
PU_BITMAPINFOHEADER Bmih = &(Bmi->bmiHeader);
/* if biCompression is not U_BI_RGB some or all of the following might not hold real values */
bic = Bmih->biCompression;
*width = Bmih->biWidth;
*colortype = Bmih->biBitCount;
if(Bmih->biHeight < 0){
*height = -Bmih->biHeight;
*invert = 1;
}
else {
*height = Bmih->biHeight;
*invert = 0;
}
if(bic == U_BI_RGB){
*numCt = get_real_color_count((const char *) Bmih);
if( numCt){ *ct = (PU_RGBQUAD) ((char *)Bmi + sizeof(U_BITMAPINFOHEADER)); }
else { *ct = NULL; }
}
else if(bic == U_BI_BITFIELDS){ /* to date only encountered once, for 32 bit, from PPT*/
*numCt = 0;
*ct = NULL;
bic = U_BI_RGB; /* there seems to be no difference, at least for the 32 bit images */
}
else {
*numCt = Bmih->biSizeImage;
*ct = NULL;
}
*px = record + offBitsSrc;
return(bic);
}
/**
\brief Convert one of many different types of DIB pixmaps to an RGBA 32 bit pixmap.
\return 0 on success, other values on errors.
\param px DIB pixel array
\param ct DIB color table
\param numCt DIB color table number of entries
\param rgba_px U_RGBA pixel array (32 bits), created by this routine, caller must free.
\param w Width of pixel array in the record
\param h Height of pixel array in the record
\param colortype DIB BitCount Enumeration
\param use_ct Kept for symmetry with RGBA_to_DIB, should be set to numCt
\param invert If DIB rows are in opposite order from RGBA rows
*/
int DIB_to_RGBA(
const char *px,
const U_RGBQUAD *ct,
int numCt,
char **rgba_px,
int w,
int h,
uint32_t colortype,
int use_ct,
int invert
){
uint32_t cbRgba_px;
int stride;
int bs;
int pad;
int i,j;
int istart, iend, iinc;
uint8_t r,g,b,a,tmp8;
const char *pxptr;
char *rptr;
int usedbytes;
U_RGBQUAD color;
int32_t index;
// sanity checking
if(!w || !h || !colortype || !px)return(1);
if(use_ct && colortype >= U_BCBM_COLOR16)return(2); //color tables not used above 16 bit pixels
if(!use_ct && colortype < U_BCBM_COLOR16)return(3); //color tables mandatory for < 16 bit
if(use_ct && !numCt)return(4); //color table not adequately described
stride = w * 4;
cbRgba_px = stride * h;
bs = colortype/8;
if(bs<1){
usedbytes = (w*colortype + 7)/8; // width of line in fully and partially occupied bytes
}
else {
usedbytes = w*bs;
}
pad = UP4(usedbytes) - usedbytes; // DIB rows must be aligned on 4 byte boundaries, they are padded at the end to accomplish this.;
*rgba_px = (char *) malloc(cbRgba_px);
if(!rgba_px)return(4);
if(invert){
istart = h-1;
iend = -1;
iinc = -1;
}
else {
istart = 0;
iend = h;
iinc = 1;
}
pxptr = px;
tmp8 = 0; // silences a compiler warning, tmp8 always sets when j=0, so never used uninitialized
for(i=istart; i!=iend; i+=iinc){
rptr= *rgba_px + i*stride;
for(j=0; j<w; j++){
if(use_ct){
switch(colortype){
case U_BCBM_MONOCHROME: // 2 colors. bmiColors array has two entries
if(!(j % 8)){ tmp8 = *pxptr++; }
index = 0x80 & tmp8; // This seems wrong, as lowest position is top bit, but it works.
index = index >> 7;
tmp8 = tmp8 << 1;
break;
case U_BCBM_COLOR4: // 2^4 colors. bmiColors array has 16 entries
if(!(j % 2)){ tmp8 = *pxptr++; }
index = 0xF0 & tmp8;
index = index >> 4;
tmp8 = tmp8 << 4;
break;
case U_BCBM_COLOR8: // 2^8 colors. bmiColors array has 256 entries
index = (uint8_t) *pxptr++;;
break;
case U_BCBM_COLOR16: // 2^16 colors. (Several different color methods))