Debian Codegraph @

src/scan.c

raw
1 /*
2 * Copyright (C) 1989-95 GROUPE BULL
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
24 */
25
26 /*****************************************************************************\
27 * scan.c: *
28 * *
29 * XPM library *
30 * Scanning utility for XPM file format *
31 * *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
34
35 /* October 2004, source code review by Thomas Biege <[email protected]> */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40 #include "XpmI.h"
41
42 #define MAXPRINTABLE 92 /* number of printable ascii chars
43 * minus \ and " for string compat
44 * and ? to avoid ANSI trigraphs. */
45
46 static const char *printable =
47 " .XoO+@#$%&*=-;:>,<1234567890qwertyuipasdfghjklzxcvbnmMNBVCZ\
48 ASDFGHJKLPIUYTREWQ!~^/()_`'][{}|";
49
50 /*
51 * printable begin with a space, so in most case, due to my algorithm, when
52 * the number of different colors is less than MAXPRINTABLE, it will give a
53 * char follow by "nothing" (a space) in the readable xpm file
54 */
55
56
57 typedef struct {
58 Pixel *pixels;
59 unsigned int *pixelindex;
60 unsigned int size;
61 unsigned int ncolors;
62 unsigned int mask_pixel; /* whether there is or not */
63 } PixelsMap;
64
65 LFUNC(storePixel, int, (Pixel pixel, PixelsMap *pmap,
66 unsigned int *index_return));
67
68 LFUNC(storeMaskPixel, int, (Pixel pixel, PixelsMap *pmap,
69 unsigned int *index_return));
70
71 typedef int (*storeFuncPtr)(Pixel pixel, PixelsMap *pmap,
72 unsigned int *index_return);
73
74 LFUNC(GetImagePixels, int, (XImage *image, unsigned int width,
75 unsigned int height, PixelsMap *pmap));
76
77 LFUNC(GetImagePixels32, int, (XImage *image, unsigned int width,
78 unsigned int height, PixelsMap *pmap));
79
80 LFUNC(GetImagePixels16, int, (XImage *image, unsigned int width,
81 unsigned int height, PixelsMap *pmap));
82
83 LFUNC(GetImagePixels8, int, (XImage *image, unsigned int width,
84 unsigned int height, PixelsMap *pmap));
85
86 LFUNC(GetImagePixels1, int, (XImage *image, unsigned int width,
87 unsigned int height, PixelsMap *pmap,
88 storeFuncPtr storeFunc));
89
90 LFUNC(ScanTransparentColor, int, (XpmColor *color, unsigned int cpp,
91 XpmAttributes *attributes));
92
93 LFUNC(ScanOtherColors, int, (Display *display, XpmColor *colors,
94 unsigned int ncolors,
95 Pixel *pixels, unsigned int mask,
96 unsigned int cpp, XpmAttributes *attributes));
97
98 /*
99 * This function stores the given pixel in the given arrays which are grown
100 * if not large enough.
101 */
102 static int
103 storePixel(
104 Pixel pixel,
105 PixelsMap *pmap,
106 unsigned int *index_return)
107 {
108 unsigned int i;
109 Pixel *p;
110 unsigned int ncolors;
111
112 if (*index_return) { /* this is a transparent pixel! */
113 *index_return = 0;
114 return 0;
115 }
116 ncolors = pmap->ncolors;
117 p = pmap->pixels + pmap->mask_pixel;
118 for (i = pmap->mask_pixel; i < ncolors; i++, p++)
119 if (*p == pixel)
120 break;
121 if (i == ncolors) {
122 if (ncolors >= pmap->size) {
123 pmap->size *= 2;
124 p = (Pixel *) XpmRealloc(pmap->pixels, sizeof(Pixel) * pmap->size);
125 if (!p)
126 return (1);
127 pmap->pixels = p;
128
129 }
130 (pmap->pixels)[ncolors] = pixel;
131 pmap->ncolors++;
132 }
133 *index_return = i;
134 return 0;
135 }
136
137 static int
138 storeMaskPixel(
139 Pixel pixel,
140 PixelsMap *pmap,
141 unsigned int *index_return)
142 {
143 if (!pixel) {
144 if (!pmap->ncolors) {
145 pmap->ncolors = 1;
146 (pmap->pixels)[0] = 0;
147 pmap->mask_pixel = 1;
148 }
149 *index_return = 1;
150 } else
151 *index_return = 0;
152 return 0;
153 }
154
155 /* function call in case of error */
156 #undef RETURN
157 #define RETURN(status) \
158 do { \
159 ErrorStatus = status; \
160 goto error; \
161 } while(0)
162
163 /*
164 * This function scans the given image and stores the found information in
165 * the given XpmImage structure.
166 */
167 int
168 XpmCreateXpmImageFromImage(
169 Display *display,
170 XImage *image,
171 XImage *shapeimage,
172 XpmImage *xpmimage,
173 XpmAttributes *attributes)
174 {
175 /* variables stored in the XpmAttributes structure */
176 unsigned int cpp;
177
178 /* variables to return */
179 PixelsMap pmap;
180 XpmColor *colorTable = NULL;
181 int ErrorStatus = 0;
182
183 /* calculation variables */
184 unsigned int width = 0;
185 unsigned int height = 0;
186 unsigned int cppm; /* minimum chars per pixel */
187 unsigned int c;
188
189 /* initialize pmap */
190 pmap.pixels = NULL;
191 pmap.pixelindex = NULL;
192 pmap.size = 256; /* should be enough most of the time */
193 pmap.ncolors = 0;
194 pmap.mask_pixel = 0;
195
196 /*
197 * get geometry
198 */
199 if (image) {
200 width = image->width;
201 height = image->height;
202 } else if (shapeimage) {
203 width = shapeimage->width;
204 height = shapeimage->height;
205 }
206
207 /*
208 * retrieve information from the XpmAttributes
209 */
210 if (attributes && (attributes->valuemask & XpmCharsPerPixel
211 /* 3.2 backward compatibility code */
212 || attributes->valuemask & XpmInfos))
213 /* end 3.2 bc */
214 cpp = attributes->cpp;
215 else
216 cpp = 0;
217
218 if ((height > 0 && width >= UINT_MAX / height) ||
219 width * height >= UINT_MAX / sizeof(unsigned int))
220 RETURN(XpmNoMemory);
221 pmap.pixelindex =
222 (unsigned int *) XpmCalloc(width * height, sizeof(unsigned int));
223 if (!pmap.pixelindex)
224 RETURN(XpmNoMemory);
225
226 if (pmap.size >= UINT_MAX / sizeof(Pixel))
227 RETURN(XpmNoMemory);
228
229 pmap.pixels = (Pixel *) XpmMalloc(sizeof(Pixel) * pmap.size);
230 if (!pmap.pixels)
231 RETURN(XpmNoMemory);
232
233 /*
234 * scan shape mask if any
235 */
236 if (shapeimage) {
237 ErrorStatus = GetImagePixels1(shapeimage, width, height, &pmap,
238 storeMaskPixel);
239 if (ErrorStatus != XpmSuccess)
240 RETURN(ErrorStatus);
241 }
242
243 /*
244 * scan the image data
245 *
246 * In case depth is 1 or bits_per_pixel is 4, 6, 8, 24 or 32 use optimized
247 * functions, otherwise use slower but sure general one.
248 *
249 */
250
251 if (image) {
252 if (((image->bits_per_pixel | image->depth) == 1) &&
253 (image->byte_order == image->bitmap_bit_order))
254 ErrorStatus = GetImagePixels1(image, width, height, &pmap,
255 storePixel);
256 else if (image->format == ZPixmap) {
257 if (image->bits_per_pixel == 8)
258 ErrorStatus = GetImagePixels8(image, width, height, &pmap);
259 else if (image->bits_per_pixel == 16)
260 ErrorStatus = GetImagePixels16(image, width, height, &pmap);
261 else if (image->bits_per_pixel == 32)
262 ErrorStatus = GetImagePixels32(image, width, height, &pmap);
263 } else
264 ErrorStatus = GetImagePixels(image, width, height, &pmap);
265 if (ErrorStatus != XpmSuccess)
266 RETURN(ErrorStatus);
267 }
268
269 /*
270 * get rgb values and a string of char, and possibly a name for each
271 * color
272 */
273 if (pmap.ncolors >= UINT_MAX / sizeof(XpmColor))
274 RETURN(XpmNoMemory);
275 colorTable = (XpmColor *) XpmCalloc(pmap.ncolors, sizeof(XpmColor));
276 if (!colorTable)
277 RETURN(XpmNoMemory);
278
279 /* compute the minimal cpp */
280 for (cppm = 1, c = MAXPRINTABLE; pmap.ncolors > c; cppm++)
281 c *= MAXPRINTABLE;
282 if (cpp < cppm)
283 cpp = cppm;
284
285 if (pmap.mask_pixel) {
286 ErrorStatus = ScanTransparentColor(colorTable, cpp, attributes);
287 if (ErrorStatus != XpmSuccess)
288 RETURN(ErrorStatus);
289 }
290
291 ErrorStatus = ScanOtherColors(display, colorTable, pmap.ncolors,
292 pmap.pixels, pmap.mask_pixel, cpp,
293 attributes);
294 if (ErrorStatus != XpmSuccess)
295 RETURN(ErrorStatus);
296
297 /*
298 * store found information in the XpmImage structure
299 */
300 xpmimage->width = width;
301 xpmimage->height = height;
302 xpmimage->cpp = cpp;
303 xpmimage->ncolors = pmap.ncolors;
304 xpmimage->colorTable = colorTable;
305 xpmimage->data = pmap.pixelindex;
306
307 XpmFree(pmap.pixels);
308 return (XpmSuccess);
309
310 /* exit point in case of error, free only locally allocated variables */
311 error:
312 if (pmap.pixelindex)
313 XpmFree(pmap.pixelindex);
314 if (pmap.pixels)
315 XpmFree(pmap.pixels);
316 if (colorTable)
317 xpmFreeColorTable(colorTable, pmap.ncolors);
318
319 return (ErrorStatus);
320 }
321
322 static int
323 ScanTransparentColor(
324 XpmColor *color,
325 unsigned int cpp,
326 XpmAttributes *attributes)
327 {
328 char *s;
329 unsigned int a, b, c;
330
331 /* first get a character string */
332 a = 0;
333 if (cpp >= UINT_MAX - 1)
334 return (XpmNoMemory);
335 if (!(s = color->string = (char *) XpmMalloc(cpp + 1)))
336 return (XpmNoMemory);
337 *s++ = printable[c = a % MAXPRINTABLE];
338 for (b = 1; b < cpp; b++, s++)
339 *s = printable[c = ((a - c) / MAXPRINTABLE) % MAXPRINTABLE];
340 *s = '\0';
341
342 /* then retrieve related info from the attributes if any */
343 if (attributes && (attributes->valuemask & XpmColorTable
344 /* 3.2 backward compatibility code */
345 || attributes->valuemask & XpmInfos)
346 /* end 3.2 bc */
347 && attributes->mask_pixel != XpmUndefPixel) {
348
349 unsigned int key;
350 char **defaults = (char **) color;
351 char **mask_defaults;
352
353 /* 3.2 backward compatibility code */
354 if (attributes->valuemask & XpmColorTable)
355 /* end 3.2 bc */
356 mask_defaults = (char **) (
357 attributes->colorTable + attributes->mask_pixel);
358 /* 3.2 backward compatibility code */
359 else
360 mask_defaults = (char **)
361 ((XpmColor **) attributes->colorTable)[attributes->mask_pixel];
362 /* end 3.2 bc */
363 for (key = 1; key <= NKEYS; key++) {
364 if ((s = mask_defaults[key])) {
365 defaults[key] = strdup(s);
366 if (!defaults[key])
367 return (XpmNoMemory);
368 }
369 }
370 } else {
371 color->c_color = strdup(TRANSPARENT_COLOR);
372 if (!color->c_color)
373 return (XpmNoMemory);
374 }
375 return (XpmSuccess);
376 }
377
378 static int
379 ScanOtherColors(
380 Display *display,
381 XpmColor *colors,
382 unsigned int ncolors,
383 Pixel *pixels,
384 unsigned int mask,
385 unsigned int cpp,
386 XpmAttributes *attributes)
387 {
388 /* variables stored in the XpmAttributes structure */
389 Colormap colormap;
390 char *rgb_fname;
391
392 xpmRgbName rgbn[MAX_RGBNAMES];
393 int rgbn_max = 0;
394 unsigned int i, j, c, i2;
395 XpmColor *color;
396 XColor *xcolors = NULL, *xcolor;
397 char *colorname, *s;
398 XpmColor *colorTable = NULL, **oldColorTable = NULL;
399 unsigned int ancolors = 0;
400 Pixel *apixels = NULL;
401 unsigned int mask_pixel = 0;
402 Bool found;
403
404 /* retrieve information from the XpmAttributes */
405 if (attributes && (attributes->valuemask & XpmColormap))
406 colormap = attributes->colormap;
407 else
408 colormap = XDefaultColormap(display, XDefaultScreen(display));
409 if (attributes && (attributes->valuemask & XpmRgbFilename))
410 rgb_fname = attributes->rgb_fname;
411 else
412 rgb_fname = NULL;
413
414 /* start from the right element */
415 if (mask) {
416 colors++;
417 ncolors--;
418 pixels++;
419 }
420
421 /* first get character strings and rgb values */
422 if (ncolors >= UINT_MAX / sizeof(XColor) || cpp >= UINT_MAX - 1)
423 return (XpmNoMemory);
424 xcolors = (XColor *) XpmMalloc(sizeof(XColor) * ncolors);
425 if (!xcolors)
426 return (XpmNoMemory);
427
428 for (i = 0, i2 = mask, color = colors, xcolor = xcolors;
429 i < ncolors; i++, i2++, color++, xcolor++, pixels++) {
430
431 if (!(s = color->string = (char *) XpmMalloc(cpp + 1))) {
432 XpmFree(xcolors);
433 return (XpmNoMemory);
434 }
435 *s++ = printable[c = i2 % MAXPRINTABLE];
436 for (j = 1; j < cpp; j++, s++)
437 *s = printable[c = ((i2 - c) / MAXPRINTABLE) % MAXPRINTABLE];
438 *s = '\0';
439
440 xcolor->pixel = *pixels;
441 }
442 XQueryColors(display, colormap, xcolors, ncolors);
443
444 /* read the rgb file if any was specified */
445 if (rgb_fname)
446 rgbn_max = xpmReadRgbNames(attributes->rgb_fname, rgbn);
447
448 if (attributes && attributes->valuemask & XpmColorTable) {
449 colorTable = attributes->colorTable;
450 ancolors = attributes->ncolors;
451 apixels = attributes->pixels;
452 mask_pixel = attributes->mask_pixel;
453 }
454 /* 3.2 backward compatibility code */
455 else if (attributes && attributes->valuemask & XpmInfos) {
456 oldColorTable = (XpmColor **) attributes->colorTable;
457 ancolors = attributes->ncolors;
458 apixels = attributes->pixels;
459 mask_pixel = attributes->mask_pixel;
460 }
461 /* end 3.2 bc */
462
463 for (i = 0, color = colors, xcolor = xcolors; i < ncolors;
464 i++, color++, xcolor++) {
465
466 /* look for related info from the attributes if any */
467 found = False;
468 if (ancolors) {
469 unsigned int offset = 0;
470
471 for (j = 0; j < ancolors; j++) {
472 if (j == mask_pixel) {
473 offset = 1;
474 continue;
475 }
476 if (apixels[j - offset] == xcolor->pixel)
477 break;
478 }
479 if (j != ancolors) {
480 unsigned int key;
481 char **defaults = (char **) color;
482 char **adefaults;
483
484 /* 3.2 backward compatibility code */
485 if (oldColorTable)
486 adefaults = (char **) oldColorTable[j];
487 else
488 /* end 3.2 bc */
489 adefaults = (char **) (colorTable + j);
490
491 found = True;
492 for (key = 1; key <= NKEYS; key++) {
493 if ((s = adefaults[key]))
494 defaults[key] = strdup(s);
495 }
496 }
497 }
498 if (!found) {
499 /* if nothing found look for a color name */
500 colorname = NULL;
501 if (rgbn_max)
502 colorname = xpmGetRgbName(rgbn, rgbn_max, xcolor->red,
503 xcolor->green, xcolor->blue);
504 if (colorname)
505 color->c_color = strdup(colorname);
506 else {
507 /* at last store the rgb value */
508 char buf[BUFSIZ];
509 sprintf(buf, "#%04X%04X%04X",
510 xcolor->red, xcolor->green, xcolor->blue);
511 color->c_color = strdup(buf);
512 }
513 if (!color->c_color) {
514 XpmFree(xcolors);
515 xpmFreeRgbNames(rgbn, rgbn_max);
516 return (XpmNoMemory);
517 }
518 }
519 }
520
521 XpmFree(xcolors);
522 xpmFreeRgbNames(rgbn, rgbn_max);
523 return (XpmSuccess);
524 }
525
526 /*
527 * The functions below are written from X11R5 MIT's code (XImUtil.c)
528 *
529 * The idea is to have faster functions than the standard XGetPixel function
530 * to scan the image data. Indeed we can speed up things by suppressing tests
531 * performed for each pixel. We do exactly the same tests but at the image
532 * level.
533 */
534
535 static unsigned long const low_bits_table[] = {
536 0x00000000, 0x00000001, 0x00000003, 0x00000007,
537 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
538 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
539 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
540 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
541 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
542 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
543 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
544 0xffffffff
545 };
546
547 /*
548 * Default method to scan pixels of an image data structure.
549 * The algorithm used is:
550 *
551 * copy the source bitmap_unit or Zpixel into temp
552 * normalize temp if needed
553 * extract the pixel bits into return value
554 *
555 */
556
557 static int
558 GetImagePixels(
559 XImage *image,
560 unsigned int width,
561 unsigned int height,
562 PixelsMap *pmap)
563 {
564 char *src;
565 char *dst;
566 unsigned int *iptr;
567 char *data;
568 unsigned int x, y;
569 int bits, depth, ibu, ibpp, offset, i;
570 unsigned long lbt;
571 Pixel pixel, px;
572
573 data = image->data;
574 iptr = pmap->pixelindex;
575 depth = image->depth;
576 lbt = low_bits_table[depth];
577 ibpp = image->bits_per_pixel;
578 offset = image->xoffset;
579
580 if (image->bitmap_unit < 0)
581 return (XpmNoMemory);
582
583 if ((image->bits_per_pixel | image->depth) == 1) {
584 ibu = image->bitmap_unit;
585 for (y = 0; y < height; y++)
586 for (x = 0; x < width; x++, iptr++) {
587 src = &data[XYINDEX(x, y, image)];
588 dst = (char *) &pixel;
589 pixel = 0;
590 for (i = ibu >> 3; --i >= 0;)
591 *dst++ = *src++;
592 XYNORMALIZE(&pixel, image);
593 bits = (x + offset) % ibu;
594 pixel = ((((char *) &pixel)[bits >> 3]) >> (bits & 7)) & 1;
595 if (ibpp != depth)
596 pixel &= lbt;
597 if (storePixel(pixel, pmap, iptr))
598 return (XpmNoMemory);
599 }
600 } else if (image->format == XYPixmap) {
601 int nbytes, bpl, j;
602 long plane = 0;
603 ibu = image->bitmap_unit;
604 nbytes = ibu >> 3;
605 bpl = image->bytes_per_line;
606 for (y = 0; y < height; y++)
607 for (x = 0; x < width; x++, iptr++) {
608 pixel = 0;
609 plane = 0;
610 for (i = depth; --i >= 0;) {
611 src = &data[XYINDEX(x, y, image) + plane];
612 dst = (char *) &px;
613 px = 0;
614 for (j = nbytes; --j >= 0;)
615 *dst++ = *src++;
616 XYNORMALIZE(&px, image);
617 bits = (x + offset) % ibu;
618 pixel = (pixel << 1) |
619 (((((char *) &px)[bits >> 3]) >> (bits & 7)) & 1);
620 plane = plane + (bpl * height);
621 }
622 if (ibpp != depth)
623 pixel &= lbt;
624 if (storePixel(pixel, pmap, iptr))
625 return (XpmNoMemory);
626 }
627 } else if (image->format == ZPixmap) {
628 for (y = 0; y < height; y++)
629 for (x = 0; x < width; x++, iptr++) {
630 src = &data[ZINDEX(x, y, image)];
631 dst = (char *) &px;
632 px = 0;
633 for (i = (ibpp + 7) >> 3; --i >= 0;)
634 *dst++ = *src++;
635 ZNORMALIZE(&px, image);
636 pixel = 0;
637 for (i = sizeof(unsigned long); --i >= 0;)
638 pixel = (pixel << 8) | ((unsigned char *) &px)[i];
639 if (ibpp == 4) {
640 if (x & 1)
641 pixel >>= 4;
642 else
643 pixel &= 0xf;
644 }
645 if (ibpp != depth)
646 pixel &= lbt;
647 if (storePixel(pixel, pmap, iptr))
648 return (XpmNoMemory);
649 }
650 } else
651 return (XpmColorError); /* actually a bad image */
652 return (XpmSuccess);
653 }
654
655 /*
656 * scan pixels of a 32-bits Z image data structure
657 */
658
659 #if !defined(WORD64) && !defined(LONG64)
660 static unsigned long byteorderpixel = MSBFirst << 24;
661 #endif
662
663 static int
664 GetImagePixels32(
665 XImage *image,
666 unsigned int width,
667 unsigned int height,
668 PixelsMap *pmap)
669 {
670 unsigned char *addr;
671 unsigned char *data;
672 unsigned int *iptr;
673 unsigned int x, y;
674 unsigned long lbt;
675 Pixel pixel;
676 int depth;
677
678 data = (unsigned char *) image->data;
679 iptr = pmap->pixelindex;
680 depth = image->depth;
681 lbt = low_bits_table[depth];
682 #if !defined(WORD64) && !defined(LONG64)
683 if (*((char *) &byteorderpixel) == image->byte_order) {
684 for (y = 0; y < height; y++)
685 for (x = 0; x < width; x++, iptr++) {
686 addr = &data[ZINDEX32(x, y, image)];
687 pixel = *((unsigned long *) addr);
688 if (depth != 32)
689 pixel &= lbt;
690 if (storePixel(pixel, pmap, iptr))
691 return (XpmNoMemory);
692 }
693 } else
694 #endif
695 if (image->byte_order == MSBFirst)
696 for (y = 0; y < height; y++)
697 for (x = 0; x < width; x++, iptr++) {
698 addr = &data[ZINDEX32(x, y, image)];
699 pixel = ((unsigned long) addr[0] << 24 |
700 (unsigned long) addr[1] << 16 |
701 (unsigned long) addr[2] << 8 |
702 addr[3]);
703 if (depth != 32)
704 pixel &= lbt;
705 if (storePixel(pixel, pmap, iptr))
706 return (XpmNoMemory);
707 }
708 else
709 for (y = 0; y < height; y++)
710 for (x = 0; x < width; x++, iptr++) {
711 addr = &data[ZINDEX32(x, y, image)];
712 pixel = (addr[0] |
713 (unsigned long) addr[1] << 8 |
714 (unsigned long) addr[2] << 16 |
715 (unsigned long) addr[3] << 24);
716 if (depth != 32)
717 pixel &= lbt;
718 if (storePixel(pixel, pmap, iptr))
719 return (XpmNoMemory);
720 }
721 return (XpmSuccess);
722 }
723
724 /*
725 * scan pixels of a 16-bits Z image data structure
726 */
727
728 static int
729 GetImagePixels16(
730 XImage *image,
731 unsigned int width,
732 unsigned int height,
733 PixelsMap *pmap)
734 {
735 unsigned char *addr;
736 unsigned char *data;
737 unsigned int *iptr;
738 unsigned int x, y;
739 unsigned long lbt;
740 Pixel pixel;
741 int depth;
742
743 data = (unsigned char *) image->data;
744 iptr = pmap->pixelindex;
745 depth = image->depth;
746 lbt = low_bits_table[depth];
747 if (image->byte_order == MSBFirst)
748 for (y = 0; y < height; y++)
749 for (x = 0; x < width; x++, iptr++) {
750 addr = &data[ZINDEX16(x, y, image)];
751 pixel = addr[0] << 8 | addr[1];
752 if (depth != 16)
753 pixel &= lbt;
754 if (storePixel(pixel, pmap, iptr))
755 return (XpmNoMemory);
756 }
757 else
758 for (y = 0; y < height; y++)
759 for (x = 0; x < width; x++, iptr++) {
760 addr = &data[ZINDEX16(x, y, image)];
761 pixel = addr[0] | addr[1] << 8;
762 if (depth != 16)
763 pixel &= lbt;
764 if (storePixel(pixel, pmap, iptr))
765 return (XpmNoMemory);
766 }
767 return (XpmSuccess);
768 }
769
770 /*
771 * scan pixels of a 8-bits Z image data structure
772 */
773
774 static int
775 GetImagePixels8(
776 XImage *image,
777 unsigned int width,
778 unsigned int height,
779 PixelsMap *pmap)
780 {
781 unsigned int *iptr;
782 unsigned char *data;
783 unsigned int x, y;
784 unsigned long lbt;
785 Pixel pixel;
786 int depth;
787
788 data = (unsigned char *) image->data;
789 iptr = pmap->pixelindex;
790 depth = image->depth;
791 lbt = low_bits_table[depth];
792 for (y = 0; y < height; y++)
793 for (x = 0; x < width; x++, iptr++) {
794 pixel = data[ZINDEX8(x, y, image)];
795 if (depth != 8)
796 pixel &= lbt;
797 if (storePixel(pixel, pmap, iptr))
798 return (XpmNoMemory);
799 }
800 return (XpmSuccess);
801 }
802
803 /*
804 * scan pixels of a 1-bit depth Z image data structure
805 */
806
807 static int
808 GetImagePixels1(
809 XImage *image,
810 unsigned int width,
811 unsigned int height,
812 PixelsMap *pmap,
813 storeFuncPtr storeFunc)
814 {
815 unsigned int *iptr;
816 unsigned int x, y;
817 char *data;
818 Pixel pixel;
819 int xoff, yoff, offset, bpl;
820
821 data = image->data;
822 iptr = pmap->pixelindex;
823 offset = image->xoffset;
824 bpl = image->bytes_per_line;
825
826 if (image->bitmap_bit_order == MSBFirst)
827 for (y = 0; y < height; y++)
828 for (x = 0; x < width; x++, iptr++) {
829 xoff = x + offset;
830 yoff = y * bpl + (xoff >> 3);
831 xoff &= 7;
832 pixel = (data[yoff] & (0x80 >> xoff)) ? 1 : 0;
833 if ((*storeFunc) (pixel, pmap, iptr))
834 return (XpmNoMemory);
835 }
836 else
837 for (y = 0; y < height; y++)
838 for (x = 0; x < width; x++, iptr++) {
839 xoff = x + offset;
840 yoff = y * bpl + (xoff >> 3);
841 xoff &= 7;
842 pixel = (data[yoff] & (1 << xoff)) ? 1 : 0;
843 if ((*storeFunc) (pixel, pmap, iptr))
844 return (XpmNoMemory);
845 }
846 return (XpmSuccess);
847 }
848
849 int
850 XpmCreateXpmImageFromPixmap(
851 Display *display,
852 Pixmap pixmap,
853 Pixmap shapemask,
854 XpmImage *xpmimage,
855 XpmAttributes *attributes)
856 {
857 XImage *ximage = NULL;
858 XImage *shapeimage = NULL;
859 unsigned int width = 0;
860 unsigned int height = 0;
861 int ErrorStatus;
862
863 /* get geometry */
864 if (attributes && attributes->valuemask & XpmSize) {
865 width = attributes->width;
866 height = attributes->height;
867 }
868 /* get the ximages */
869 if (pixmap)
870 xpmCreateImageFromPixmap(display, pixmap, &ximage, &width, &height);
871 if (shapemask)
872 xpmCreateImageFromPixmap(display, shapemask, &shapeimage,
873 &width, &height);
874
875 /* create the related XpmImage */
876 ErrorStatus = XpmCreateXpmImageFromImage(display, ximage, shapeimage,
877 xpmimage, attributes);
878
879 /* destroy the ximages */
880 if (ximage)
881 XDestroyImage(ximage);
882 if (shapeimage)
883 XDestroyImage(shapeimage);
884
885 return (ErrorStatus);
886 }