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
|
/*
* Copyright (C) 1989-95 GROUPE BULL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of GROUPE BULL shall not be
* used in advertising or otherwise to promote the sale, use or other dealings
* in this Software without prior written authorization from GROUPE BULL.
*/
/*****************************************************************************\
* scan.c: *
* *
* XPM library *
* Scanning utility for XPM file format *
* *
* Developed by Arnaud Le Hors *
\*****************************************************************************/
/*
* The code related to FOR_MSW has been added by
* HeDu (hedu@cul-ipn.uni-kiel.de) 4/94
*/
/* October 2004, source code review by Thomas Biege <thomas@suse.de> */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "XpmI.h"
#define MAXPRINTABLE 92 /* number of printable ascii chars
* minus \ and " for string compat
* and ? to avoid ANSI trigraphs. */
static const char *printable =
" .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjklzxcvbnmMNBVCZ\
ASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
/*
* printable begin with a space, so in most case, due to my algorithm, when
* the number of different colors is less than MAXPRINTABLE, it will give a
* char follow by "nothing" (a space) in the readable xpm file
*/
typedef struct {
Pixel *pixels;
unsigned int *pixelindex;
unsigned int size;
unsigned int ncolors;
unsigned int mask_pixel; /* whether there is or not */
} PixelsMap;
LFUNC(storePixel, int, (Pixel pixel, PixelsMap *pmap,
unsigned int *index_return));
LFUNC(storeMaskPixel, int, (Pixel pixel, PixelsMap *pmap,
unsigned int *index_return));
typedef int (*storeFuncPtr)(Pixel pixel, PixelsMap *pmap,
unsigned int *index_return);
#ifndef FOR_MSW
LFUNC(GetImagePixels, int, (XImage *image, unsigned int width,
unsigned int height, PixelsMap *pmap));
LFUNC(GetImagePixels32, int, (XImage *image, unsigned int width,
unsigned int height, PixelsMap *pmap));
LFUNC(GetImagePixels16, int, (XImage *image, unsigned int width,
unsigned int height, PixelsMap *pmap));
LFUNC(GetImagePixels8, int, (XImage *image, unsigned int width,
unsigned int height, PixelsMap *pmap));
LFUNC(GetImagePixels1, int, (XImage *image, unsigned int width,
unsigned int height, PixelsMap *pmap,
storeFuncPtr storeFunc));
#else /* ndef FOR_MSW */
LFUNC(MSWGetImagePixels, int, (Display *d, XImage *image, unsigned int width,
unsigned int height, PixelsMap *pmap,
storeFuncPtr storeFunc));
#endif
LFUNC(ScanTransparentColor, int, (XpmColor *color, unsigned int cpp,
XpmAttributes *attributes));
LFUNC(ScanOtherColors, int, (Display *display, XpmColor *colors,
unsigned int ncolors,
Pixel *pixels, unsigned int mask,
unsigned int cpp, XpmAttributes *attributes));
/*
* This function stores the given pixel in the given arrays which are grown
* if not large enough.
*/
static int
storePixel(
Pixel pixel,
PixelsMap *pmap,
unsigned int *index_return)
{
unsigned int i;
Pixel *p;
unsigned int ncolors;
if (*index_return) { /* this is a transparent pixel! */
*index_return = 0;
return 0;
}
ncolors = pmap->ncolors;
p = pmap->pixels + pmap->mask_pixel;
for (i = pmap->mask_pixel; i < ncolors; i++, p++)
if (*p == pixel)
break;
if (i == ncolors) {
if (ncolors >= pmap->size) {
pmap->size *= 2;
p = (Pixel *) XpmRealloc(pmap->pixels, sizeof(Pixel) * pmap->size);
if (!p)
return (1);
pmap->pixels = p;
}
(pmap->pixels)[ncolors] = pixel;
pmap->ncolors++;
}
*index_return = i;
return 0;
}
static int
storeMaskPixel(
Pixel pixel,
PixelsMap *pmap,
unsigned int *index_return)
{
if (!pixel) {
if (!pmap->ncolors) {
pmap->ncolors = 1;
(pmap->pixels)[0] = 0;
pmap->mask_pixel = 1;
}
*index_return = 1;
} else
*index_return = 0;
return 0;
}
/* function call in case of error */
#undef RETURN
#define RETURN(status) \
do { \
ErrorStatus = status; \
goto error; \
} while(0)
/*
* This function scans the given image and stores the found information in
* the given XpmImage structure.
*/
int
XpmCreateXpmImageFromImage(
Display *display,
XImage *image,
XImage *shapeimage,
XpmImage *xpmimage,
XpmAttributes *attributes)
{
/* variables stored in the XpmAttributes structure */
unsigned int cpp;
/* variables to return */
PixelsMap pmap;
XpmColor *colorTable = NULL;
int ErrorStatus = 0;
/* calculation variables */
unsigned int width = 0;
unsigned int height = 0;
unsigned int cppm; /* minimum chars per pixel */
unsigned int c;
/* initialize pmap */
pmap.pixels = NULL;
pmap.pixelindex = NULL;
pmap.size = 256; /* should be enough most of the time */
pmap.ncolors = 0;
pmap.mask_pixel = 0;
/*
* get geometry
*/
if (image) {
width = image->width;
height = image->height;
} else if (shapeimage) {
width = shapeimage->width;
height = shapeimage->height;
}
/*
* retrieve information from the XpmAttributes
*/
if (attributes && (attributes->valuemask & XpmCharsPerPixel
/* 3.2 backward compatibility code */
|| attributes->valuemask & XpmInfos))
/* end 3.2 bc */
cpp = attributes->cpp;
else
cpp = 0;
if ((height > 0 && width >= UINT_MAX / height) ||
width * height >= UINT_MAX / sizeof(unsigned int))
RETURN(XpmNoMemory);
pmap.pixelindex =
(unsigned int *) XpmCalloc(width * height, sizeof(unsigned int));
if (!pmap.pixelindex)
RETURN(XpmNoMemory);
if (pmap.size >= UINT_MAX / sizeof(Pixel))
RETURN(XpmNoMemory);
pmap.pixels = (Pixel *) XpmMalloc(sizeof(Pixel) * pmap.size);
if (!pmap.pixels)
RETURN(XpmNoMemory);
/*
* scan shape mask if any
*/
if (shapeimage) {
#ifndef FOR_MSW
ErrorStatus = GetImagePixels1(shapeimage, width, height, &pmap,
storeMaskPixel);
#else
ErrorStatus = MSWGetImagePixels(display, shapeimage, width, height,
&pmap, storeMaskPixel);
#endif
if (ErrorStatus != XpmSuccess)
RETURN(ErrorStatus);
}
/*
* scan the image data
*
* In case depth is 1 or bits_per_pixel is 4, 6, 8, 24 or 32 use optimized
* functions, otherwise use slower but sure general one.
*
*/
if (image) {
#ifndef FOR_MSW
if (((image->bits_per_pixel | image->depth) == 1) &&
(image->byte_order == image->bitmap_bit_order))
ErrorStatus = GetImagePixels1(image, width, height, &pmap,
storePixel);
else if (image->format == ZPixmap) {
if (image->bits_per_pixel == 8)
ErrorStatus = GetImagePixels8(image, width, height, &pmap);
else if (image->bits_per_pixel == 16)
ErrorStatus = GetImagePixels16(image, width, height, &pmap);
else if (image->bits_per_pixel == 32)
ErrorStatus = GetImagePixels32(image, width, height, &pmap);
} else
ErrorStatus = GetImagePixels(image, width, height, &pmap);
#else
ErrorStatus = MSWGetImagePixels(display, image, width, height, &pmap,
storePixel);
#endif
if (ErrorStatus != XpmSuccess)
RETURN(ErrorStatus);
}
/*
* get rgb values and a string of char, and possibly a name for each
* color
*/
if (pmap.ncolors >= UINT_MAX / sizeof(XpmColor))
RETURN(XpmNoMemory);
colorTable = (XpmColor *) XpmCalloc(pmap.ncolors, sizeof(XpmColor));
if (!colorTable)
RETURN(XpmNoMemory);
/* compute the minimal cpp */
for (cppm = 1, c = MAXPRINTABLE; pmap.ncolors > c; cppm++)
c *= MAXPRINTABLE;
if (cpp < cppm)
cpp = cppm;
if (pmap.mask_pixel) {
ErrorStatus = ScanTransparentColor(colorTable, cpp, attributes);
if (ErrorStatus != XpmSuccess)
RETURN(ErrorStatus);
}
ErrorStatus = ScanOtherColors(display, colorTable, pmap.ncolors,
pmap.pixels, pmap.mask_pixel, cpp,
attributes);
if (ErrorStatus != XpmSuccess)
RETURN(ErrorStatus);
/*
* store found information in the XpmImage structure
*/
xpmimage->width = width;
xpmimage->height = height;
xpmimage->cpp = cpp;
xpmimage->ncolors = pmap.ncolors;
xpmimage->colorTable = colorTable;
xpmimage->data = pmap.pixelindex;
XpmFree(pmap.pixels);
return (XpmSuccess);
/* exit point in case of error, free only locally allocated variables */
error:
if (pmap.pixelindex)
XpmFree(pmap.pixelindex);
if (pmap.pixels)
XpmFree(pmap.pixels);
if (colorTable)
xpmFreeColorTable(colorTable, pmap.ncolors);
return (ErrorStatus);
}
static int
ScanTransparentColor(
XpmColor *color,
unsigned int cpp,
XpmAttributes *attributes)
{
char *s;
unsigned int a, b, c;
/* first get a character string */
a = 0;
if (cpp >= UINT_MAX - 1)
return (XpmNoMemory);
if (!(s = color->string = (char *) XpmMalloc(cpp + 1)))
return (XpmNoMemory);
*s++ = printable[c = a % MAXPRINTABLE];
for (b = 1; b < cpp; b++, s++)
*s = printable[c = ((a - c) / MAXPRINTABLE) % MAXPRINTABLE];
*s = '\0';
/* then retrieve related info from the attributes if any */
if (attributes && (attributes->valuemask & XpmColorTable
/* 3.2 backward compatibility code */
|| attributes->valuemask & XpmInfos)
/* end 3.2 bc */
&& attributes->mask_pixel != XpmUndefPixel) {
unsigned int key;
char **defaults = (char **) color;
char **mask_defaults;
/* 3.2 backward compatibility code */
if (attributes->valuemask & XpmColorTable)
/* end 3.2 bc */
mask_defaults = (char **) (
attributes->colorTable + attributes->mask_pixel);
/* 3.2 backward compatibility code */
else
mask_defaults = (char **)
((XpmColor **) attributes->colorTable)[attributes->mask_pixel];
/* end 3.2 bc */
for (key = 1; key <= NKEYS; key++) {
if ((s = mask_defaults[key])) {
defaults[key] = (char *) xpmstrdup(s);
if (!defaults[key])
return (XpmNoMemory);
}
}
} else {
color->c_color = (char *) xpmstrdup(TRANSPARENT_COLOR);
if (!color->c_color)
return (XpmNoMemory);
}
return (XpmSuccess);
}
static int
ScanOtherColors(
Display *display,
XpmColor *colors,
unsigned int ncolors,
Pixel *pixels,
unsigned int mask,
unsigned int cpp,
XpmAttributes *attributes)
{
/* variables stored in the XpmAttributes structure */
Colormap colormap;
char *rgb_fname;
#ifndef FOR_MSW
xpmRgbName rgbn[MAX_RGBNAMES];
#else
xpmRgbName *rgbn = NULL;
#endif
int rgbn_max = 0;
unsigned int i, j, c, i2;
XpmColor *color;
XColor *xcolors = NULL, *xcolor;
char *colorname, *s;
XpmColor *colorTable = NULL, **oldColorTable = NULL;
unsigned int ancolors = 0;
Pixel *apixels = NULL;
unsigned int mask_pixel = 0;
Bool found;
/* retrieve information from the XpmAttributes */
if (attributes && (attributes->valuemask & XpmColormap))
colormap = attributes->colormap;
else
colormap = XDefaultColormap(display, XDefaultScreen(display));
if (attributes && (attributes->valuemask & XpmRgbFilename))
rgb_fname = attributes->rgb_fname;
else
rgb_fname = NULL;
/* start from the right element */
if (mask) {
colors++;
ncolors--;
pixels++;
}
/* first get character strings and rgb values */
if (ncolors >= UINT_MAX / sizeof(XColor) || cpp >= UINT_MAX - 1)
return (XpmNoMemory);
xcolors = (XColor *) XpmMalloc(sizeof(XColor) * ncolors);
if (!xcolors)
return (XpmNoMemory);
for (i = 0, i2 = mask, color = colors, xcolor = xcolors;
i < ncolors; i++, i2++, color++, xcolor++, pixels++) {
if (!(s = color->string = (char *) XpmMalloc(cpp + 1))) {
XpmFree(xcolors);
return (XpmNoMemory);
}
*s++ = printable[c = i2 % MAXPRINTABLE];
for (j = 1; j < cpp; j++, s++)
*s = printable[c = ((i2 - c) / MAXPRINTABLE) % MAXPRINTABLE];
*s = '\0';
xcolor->pixel = *pixels;
}
XQueryColors(display, colormap, xcolors, ncolors);
#ifndef FOR_MSW
/* read the rgb file if any was specified */
if (rgb_fname)
rgbn_max = xpmReadRgbNames(attributes->rgb_fname, rgbn);
#else
/* FOR_MSW: rgb names and values are hardcoded in rgbtab.h */
rgbn_max = xpmReadRgbNames(NULL, NULL);
#endif
if (attributes && attributes->valuemask & XpmColorTable) {
colorTable = attributes->colorTable;
ancolors = attributes->ncolors;
apixels = attributes->pixels;
mask_pixel = attributes->mask_pixel;
}
/* 3.2 backward compatibility code */
else if (attributes && attributes->valuemask & XpmInfos) {
oldColorTable = (XpmColor **) attributes->colorTable;
ancolors = attributes->ncolors;
apixels = attributes->pixels;
mask_pixel = attributes->mask_pixel;
}
/* end 3.2 bc */
for (i = 0, color = colors, xcolor = xcolors; i < ncolors;
i++, color++, xcolor++) {
/* look for related info from the attributes if any */
found = False;
if (ancolors) {
unsigned int offset = 0;
for (j = 0; j < ancolors; j++) {
if (j == mask_pixel) {
offset = 1;
continue;
}
if (apixels[j - offset] == xcolor->pixel)
break;
}
if (j != ancolors) {
unsigned int key;
char **defaults = (char **) color;
char **adefaults;
/* 3.2 backward compatibility code */
if (oldColorTable)
adefaults = (char **) oldColorTable[j];
else
/* end 3.2 bc */
adefaults = (char **) (colorTable + j);
found = True;
for (key = 1; key <= NKEYS; key++) {
if ((s = adefaults[key]))
defaults[key] = (char *) xpmstrdup(s);
}
}
}
if (!found) {
/* if nothing found look for a color name */
colorname = NULL;
if (rgbn_max)
colorname = xpmGetRgbName(rgbn, rgbn_max, xcolor->red,
xcolor->green, xcolor->blue);
if (colorname)
color->c_color = (char *) xpmstrdup(colorname);
else {
/* at last store the rgb value */
char buf[BUFSIZ];
#ifndef FOR_MSW
sprintf(buf, "#%04X%04X%04X",
xcolor->red, xcolor->green, xcolor->blue);
#else
sprintf(buf, "#%02x%02x%02x",
xcolor->red, xcolor->green, xcolor->blue);
#endif
color->c_color = (char *) xpmstrdup(buf);
}
if (!color->c_color) {
XpmFree(xcolors);
xpmFreeRgbNames(rgbn, rgbn_max);
return (XpmNoMemory);
}
}
}
XpmFree(xcolors);
xpmFreeRgbNames(rgbn, rgbn_max);
return (XpmSuccess);
}
#ifndef FOR_MSW
/*
* The functions below are written from X11R5 MIT's code (XImUtil.c)
*
* The idea is to have faster functions than the standard XGetPixel function
* to scan the image data. Indeed we can speed up things by suppressing tests
* performed for each pixel. We do exactly the same tests but at the image
* level.
*/
static unsigned long const low_bits_table[] = {
0x00000000, 0x00000001, 0x00000003, 0x00000007,
0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
0xffffffff
};
/*
* Default method to scan pixels of an image data structure.
* The algorithm used is:
*
* copy the source bitmap_unit or Zpixel into temp
* normalize temp if needed
* extract the pixel bits into return value
*
*/
static int
GetImagePixels(
XImage *image,
unsigned int width,
unsigned int height,
PixelsMap *pmap)
{
char *src;
char *dst;
unsigned int *iptr;
char *data;
unsigned int x, y;
int bits, depth, ibu, ibpp, offset, i;
unsigned long lbt;
Pixel pixel, px;
data = image->data;
iptr = pmap->pixelindex;
depth = image->depth;
lbt = low_bits_table[depth];
ibpp = image->bits_per_pixel;
offset = image->xoffset;
if (image->bitmap_unit < 0)
return (XpmNoMemory);
if ((image->bits_per_pixel | image->depth) == 1) {
ibu = image->bitmap_unit;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
src = &data[XYINDEX(x, y, image)];
dst = (char *) &pixel;
pixel = 0;
for (i = ibu >> 3; --i >= 0;)
*dst++ = *src++;
XYNORMALIZE(&pixel, image);
bits = (x + offset) % ibu;
pixel = ((((char *) &pixel)[bits >> 3]) >> (bits & 7)) & 1;
if (ibpp != depth)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
} else if (image->format == XYPixmap) {
int nbytes, bpl, j;
long plane = 0;
ibu = image->bitmap_unit;
nbytes = ibu >> 3;
bpl = image->bytes_per_line;
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
pixel = 0;
plane = 0;
for (i = depth; --i >= 0;) {
src = &data[XYINDEX(x, y, image) + plane];
dst = (char *) &px;
px = 0;
for (j = nbytes; --j >= 0;)
*dst++ = *src++;
XYNORMALIZE(&px, image);
bits = (x + offset) % ibu;
pixel = (pixel << 1) |
(((((char *) &px)[bits >> 3]) >> (bits & 7)) & 1);
plane = plane + (bpl * height);
}
if (ibpp != depth)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
} else if (image->format == ZPixmap) {
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
src = &data[ZINDEX(x, y, image)];
dst = (char *) &px;
px = 0;
for (i = (ibpp + 7) >> 3; --i >= 0;)
*dst++ = *src++;
ZNORMALIZE(&px, image);
pixel = 0;
for (i = sizeof(unsigned long); --i >= 0;)
pixel = (pixel << 8) | ((unsigned char *) &px)[i];
if (ibpp == 4) {
if (x & 1)
pixel >>= 4;
else
pixel &= 0xf;
}
if (ibpp != depth)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
} else
return (XpmColorError); /* actually a bad image */
return (XpmSuccess);
}
/*
* scan pixels of a 32-bits Z image data structure
*/
#if !defined(WORD64) && !defined(LONG64)
static unsigned long byteorderpixel = MSBFirst << 24;
#endif
static int
GetImagePixels32(
XImage *image,
unsigned int width,
unsigned int height,
PixelsMap *pmap)
{
unsigned char *addr;
unsigned char *data;
unsigned int *iptr;
unsigned int x, y;
unsigned long lbt;
Pixel pixel;
int depth;
data = (unsigned char *) image->data;
iptr = pmap->pixelindex;
depth = image->depth;
lbt = low_bits_table[depth];
#if !defined(WORD64) && !defined(LONG64)
if (*((char *) &byteorderpixel) == image->byte_order) {
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
addr = &data[ZINDEX32(x, y, image)];
pixel = *((unsigned long *) addr);
if (depth != 32)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
} else
#endif
if (image->byte_order == MSBFirst)
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
addr = &data[ZINDEX32(x, y, image)];
pixel = ((unsigned long) addr[0] << 24 |
(unsigned long) addr[1] << 16 |
(unsigned long) addr[2] << 8 |
addr[3]);
if (depth != 32)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
else
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
addr = &data[ZINDEX32(x, y, image)];
pixel = (addr[0] |
(unsigned long) addr[1] << 8 |
(unsigned long) addr[2] << 16 |
(unsigned long) addr[3] << 24);
if (depth != 32)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
return (XpmSuccess);
}
/*
* scan pixels of a 16-bits Z image data structure
*/
static int
GetImagePixels16(
XImage *image,
unsigned int width,
unsigned int height,
PixelsMap *pmap)
{
unsigned char *addr;
unsigned char *data;
unsigned int *iptr;
unsigned int x, y;
unsigned long lbt;
Pixel pixel;
int depth;
data = (unsigned char *) image->data;
iptr = pmap->pixelindex;
depth = image->depth;
lbt = low_bits_table[depth];
if (image->byte_order == MSBFirst)
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
addr = &data[ZINDEX16(x, y, image)];
pixel = addr[0] << 8 | addr[1];
if (depth != 16)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
else
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
addr = &data[ZINDEX16(x, y, image)];
pixel = addr[0] | addr[1] << 8;
if (depth != 16)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
return (XpmSuccess);
}
/*
* scan pixels of a 8-bits Z image data structure
*/
static int
GetImagePixels8(
XImage *image,
unsigned int width,
unsigned int height,
PixelsMap *pmap)
{
unsigned int *iptr;
unsigned char *data;
unsigned int x, y;
unsigned long lbt;
Pixel pixel;
int depth;
data = (unsigned char *) image->data;
iptr = pmap->pixelindex;
depth = image->depth;
lbt = low_bits_table[depth];
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
pixel = data[ZINDEX8(x, y, image)];
if (depth != 8)
pixel &= lbt;
if (storePixel(pixel, pmap, iptr))
return (XpmNoMemory);
}
return (XpmSuccess);
}
/*
* scan pixels of a 1-bit depth Z image data structure
*/
static int
GetImagePixels1(
XImage *image,
unsigned int width,
unsigned int height,
PixelsMap *pmap,
storeFuncPtr storeFunc)
{
unsigned int *iptr;
unsigned int x, y;
char *data;
Pixel pixel;
int xoff, yoff, offset, bpl;
data = image->data;
iptr = pmap->pixelindex;
offset = image->xoffset;
bpl = image->bytes_per_line;
if (image->bitmap_bit_order == MSBFirst)
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
xoff = x + offset;
yoff = y * bpl + (xoff >> 3);
xoff &= 7;
pixel = (data[yoff] & (0x80 >> xoff)) ? 1 : 0;
if ((*storeFunc) (pixel, pmap, iptr))
return (XpmNoMemory);
}
else
for (y = 0; y < height; y++)
for (x = 0; x < width; x++, iptr++) {
xoff = x + offset;
yoff = y * bpl + (xoff >> 3);
xoff &= 7;
pixel = (data[yoff] & (1 << xoff)) ? 1 : 0;
if ((*storeFunc) (pixel, pmap, iptr))
return (XpmNoMemory);
}
return (XpmSuccess);
}
#else /* ndef FOR_MSW */
static int
MSWGetImagePixels(
Display *display,
XImage *image,
unsigned int width,
unsigned int height,
PixelsMap *pmap,
int (*storeFunc) (Pixel, PixelsMap*, unsigned int *))
{
unsigned int *iptr;
unsigned int x, y;
Pixel pixel;
iptr = pmap->pixelindex;
SelectObject(*display, image->bitmap);
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++, iptr++) {
pixel = GetPixel(*display, x, y);
if ((*storeFunc) (pixel, pmap, iptr))
return (XpmNoMemory);
}
}
return (XpmSuccess);
}
#endif
#ifndef FOR_MSW
int
XpmCreateXpmImageFromPixmap(
Display *display,
Pixmap pixmap,
Pixmap shapemask,
XpmImage *xpmimage,
XpmAttributes *attributes)
{
XImage *ximage = NULL;
XImage *shapeimage = NULL;
unsigned int width = 0;
unsigned int height = 0;
int ErrorStatus;
/* get geometry */
if (attributes && attributes->valuemask & XpmSize) {
width = attributes->width;
height = attributes->height;
}
/* get the ximages */
if (pixmap)
xpmCreateImageFromPixmap(display, pixmap, &ximage, &width, &height);
if (shapemask)
xpmCreateImageFromPixmap(display, shapemask, &shapeimage,
&width, &height);
/* create the related XpmImage */
ErrorStatus = XpmCreateXpmImageFromImage(display, ximage, shapeimage,
xpmimage, attributes);
/* destroy the ximages */
if (ximage)
XDestroyImage(ximage);
if (shapeimage)
XDestroyImage(shapeimage);
return (ErrorStatus);
}
#endif /* ndef FOR_MSW */
|