Debian Codegraph @

gui/GraphLib/libsrc/uimxR5/src/uxcreate.c

raw
1 /*---------------------------------------------------------------------
2 *
3 *
4 * Copyright (c) 1992, Visual Edge Software Ltd.
5 *
6 * ALL RIGHTS RESERVED. Permission to use, copy, modify, and
7 * distribute this software and its documentation for any purpose
8 * and without fee is hereby granted, provided that the above
9 * copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of Visual Edge Software not be
12 * used in advertising or publicity pertaining to distribution of
13 * the software without specific, written prior permission. The year
14 * included in the notice is the year of the creation of the work.
15 *-------------------------------------------------------------------*/
16 /*
17 * Copyright 1990, 1991 GROUPE BULL
18 *
19 * Permission to use, copy, modify, and distribute this software and its
20 * documentation for any purpose and without fee is hereby granted, provided
21 * that the above copyright notice appear in all copies and that both that
22 * copyright notice and this permission notice appear in supporting
23 * documentation, and that the name of GROUPE BULL not be used in advertising
24 * or publicity pertaining to distribution of the software without specific,
25 * written prior permission. GROUPE BULL makes no representations about the
26 * suitability of this software for any purpose. It is provided "as is"
27 * without express or implied warranty.
28 *
29 * GROUPE BULL disclaims all warranties with regard to this software,
30 * including all implied warranties of merchantability and fitness,
31 * in no event shall GROUPE BULL be liable for any special,
32 * indirect or consequential damages or any damages
33 * whatsoever resulting from loss of use, data or profits,
34 * whether in an action of contract, negligence or other tortious
35 * action, arising out of or in connection with the use
36 * or performance of this software.
37 *
38 */
39
40 /*****************************************************************************\
41 * create.c: *
42 * *
43 * XPM library *
44 * Create an X image and possibly its related shape mask *
45 * from the given xpmInternAttrib. *
46 * *
47 * Developed by Arnaud Le Hors *
48
49 .VERSION
50 090818 last modif
51
52 \*****************************************************************************/
53
54 #include <stdlib.h>
55
56 #include "xpmP.h"
57 #ifdef VMS
58 #include "sys$library:ctype.h"
59 #else
60 #include <ctype.h>
61 #endif
62
63 #include <Xm/Xm.h>
64
65 LFUNC(xpmVisualType, int, (Visual *visual));
66
67 LFUNC(SetColor, int, (Display * display, Widget w, Colormap colormap, char *colorname,
68 unsigned int color_index, Pixel * image_pixel,
69 Pixel * mask_pixel, unsigned int * mask_pixel_index,
70 Pixel ** pixels, unsigned int * npixels));
71
72 LFUNC(CreateColors, int, (Display *display, Widget w, XpmAttributes *attributes,
73 char ***ct, unsigned int ncolors, Pixel *ip,
74 Pixel *mp, unsigned int *mask_pixel, Pixel **pixels,
75 unsigned int *npixels));
76
77 LFUNC(CreateXImage, int, (Display * display, Visual * visual,
78 unsigned int depth, unsigned int width,
79 unsigned int height, XImage ** image_return));
80
81 LFUNC(SetImagePixels, int, (XImage * image, unsigned int width,
82 unsigned int height, unsigned int *pixelindex,
83 Pixel * pixels));
84
85 LFUNC(SetImagePixels32, int, (XImage * image, unsigned int width,
86 unsigned int height, unsigned int *pixelindex,
87 Pixel * pixels));
88
89 LFUNC(SetImagePixels16, int, (XImage * image, unsigned int width,
90 unsigned int height, unsigned int *pixelindex,
91 Pixel * pixels));
92
93 LFUNC(SetImagePixels8, int, (XImage * image, unsigned int width,
94 unsigned int height, unsigned int *pixelindex,
95 Pixel * pixels));
96
97 LFUNC(SetImagePixels1, int, (XImage * image, unsigned int width,
98 unsigned int height, unsigned int *pixelindex,
99 Pixel * pixels));
100
101 #ifdef NEED_STRCASECMP
102
103 LFUNC(strcasecmp, int, (char *s1, char *s2));
104
105 /*
106 * in case strcasecmp is not provided by the system here is one
107 * which does the trick
108 */
109 static int
110 strcasecmp(s1, s2)
111 register char *s1, *s2;
112 {
113 register int c1, c2;
114
115 while (*s1 && *s2) {
116 c1 = isupper(*s1) ? tolower(*s1) : *s1;
117 c2 = isupper(*s2) ? tolower(*s2) : *s2;
118 if (c1 != c2)
119 return (1);
120 s1++;
121 s2++;
122 }
123 if (*s1 || *s2)
124 return (1);
125 return (0);
126 }
127
128 #endif
129
130 /*
131 * return the default color key related to the given visual
132 */
133 static int
134 xpmVisualType(visual)
135 Visual *visual;
136 {
137 switch (visual->class) {
138 case StaticGray:
139 case GrayScale:
140 switch (visual->map_entries) {
141 case 2:
142 return (MONO);
143 case 4:
144 return (GRAY4);
145 default:
146 return (GRAY);
147 }
148 default:
149 return (COLOR);
150 }
151 }
152
153 /*
154 * set the color pixel related to the given colorname,
155 * return 0 if success, 1 otherwise.
156 */
157
158 /* UIM/X Modification: To support transparent colors,
159 ** we need to pass the widget on which
160 ** the pixmap will be drawn.
161 */
162
163
164 static int
165 SetColor(display, w, colormap, colorname, color_index,
166 image_pixel, mask_pixel, mask_pixel_index,
167 pixels, npixels)
168 Display *display;
169 Widget w;
170 Colormap colormap;
171 char *colorname;
172 unsigned int color_index;
173 Pixel *image_pixel, *mask_pixel;
174 unsigned int *mask_pixel_index;
175 Pixel **pixels;
176 unsigned int *npixels;
177 {
178 XColor xcolor;
179
180 if (strcasecmp(colorname, TRANSPARENT_COLOR)) {
181 if (!XParseColor(display, colormap, colorname, &xcolor)
182 || (!XAllocColor(display, colormap, &xcolor)))
183 return (1);
184 *image_pixel = xcolor.pixel;
185 *mask_pixel = 1;
186 (*pixels)[*npixels] = xcolor.pixel;
187 (*npixels)++;
188 } else {
189 Pixel px = 0;
190 /* UIM/X Modifications */
191 if (w) {
192 if (XmIsGadget(w)) {
193 w = XtParent(w);
194 }
195 XtVaGetValues(w, XmNbackground, &px, NULL);
196 }
197 *image_pixel = px;
198 *mask_pixel = 0;
199 *mask_pixel_index = color_index;/* store the color table index */
200 }
201 return (0);
202 }
203
204 /* UIM/X Modification: To support transparent colors,
205 ** we need to pass the widget on which
206 ** the pixmap will be drawn.
207 */
208
209 static int
210 CreateColors(display, w, attributes, ct, ncolors,
211 ip, mp, mask_pixel, pixels, npixels)
212 Display *display;
213 Widget w;
214 XpmAttributes *attributes;
215 char ***ct;
216 unsigned int ncolors;
217 Pixel *ip;
218 Pixel *mp;
219 unsigned int *mask_pixel; /* mask pixel index */
220 Pixel **pixels; /* allocated pixels */
221 unsigned int *npixels; /* number of allocated pixels */
222 {
223 /* variables stored in the XpmAttributes structure */
224 Visual *visual;
225 Colormap colormap;
226 XpmColorSymbol *colorsymbols;
227 unsigned int numsymbols;
228
229 char *colorname;
230 unsigned int a, b, l;
231 Boolean pixel_defined;
232 unsigned int key;
233 XpmColorSymbol *cs;
234 char **cts;
235 unsigned int ErrorStatus = XpmSuccess;
236 char *s;
237
238 colorsymbols = (XpmColorSymbol *) 0;
239
240 /*
241 * retrieve information from the XpmAttributes
242 */
243 if (attributes && attributes->valuemask & XpmColorSymbols) {
244 colorsymbols = attributes->colorsymbols;
245 numsymbols = attributes->numsymbols;
246 } else
247 numsymbols = 0;
248
249 if (attributes && attributes->valuemask & XpmVisual)
250 visual = attributes->visual;
251 else
252 visual = DefaultVisual(display, DefaultScreen(display));
253
254 if (attributes && attributes->valuemask & XpmColormap)
255 colormap = attributes->colormap;
256 else
257 colormap = DefaultColormap(display, DefaultScreen(display));
258
259 key = xpmVisualType(visual);
260 for (a = 0; a < ncolors; a++, ct++, ip++, mp++) {
261 colorname = NULL;
262 pixel_defined = False;
263 cts = *ct;
264
265 /*
266 * look for a defined symbol
267 */
268 if (numsymbols && cts[1]) {
269 s = cts[1];
270 for (l = 0, cs = colorsymbols; l < numsymbols; l++, cs++)
271 if (!strcmp(cs->name, s))
272 break;
273 if (l != numsymbols) {
274 if (cs->value)
275 colorname = cs->value;
276 else
277 pixel_defined = True;
278 }
279 }
280 if (!pixel_defined) { /* pixel not given as symbol value */
281 if (colorname) { /* colorname given as symbol value */
282 if (!SetColor(display, w, colormap, colorname, a, ip, mp,
283 mask_pixel, pixels, npixels))
284 pixel_defined = True;
285 else
286 ErrorStatus = XpmColorError;
287 }
288 b = key;
289 while (!pixel_defined && b > 1) {
290 if (cts[b]) {
291 if (!SetColor(display, w, colormap, cts[b], a, ip, mp,
292 mask_pixel, pixels, npixels)) {
293 pixel_defined = True;
294 break;
295 } else
296 ErrorStatus = XpmColorError;
297 }
298 b--;
299 }
300 b = key + 1;
301 while (!pixel_defined && b < NKEYS + 1) {
302 if (cts[b]) {
303 if (!SetColor(display, w, colormap, cts[b], a, ip, mp,
304 mask_pixel, pixels, npixels)) {
305 pixel_defined = True;
306 break;
307 } else
308 ErrorStatus = XpmColorError;
309 }
310 b++;
311 }
312 if (!pixel_defined)
313 return (XpmColorFailed);
314 } else {
315 *ip = colorsymbols[l].pixel;
316 *mp = 1;
317 }
318 }
319 return (ErrorStatus);
320 }
321
322 /* function call in case of error, frees only locally allocated variables */
323 #undef RETURN
324 #ifdef Debug
325 /*
326 * XDestroyImage free the image data but mnemosyne don't know about it
327 * so I free them by hand to avoid mnemalyse report it as lost data.
328 */
329 #define RETURN(status) \
330 { if (image) { \
331 free(image->data); \
332 XDestroyImage(image); } \
333 if (shapeimage) { \
334 free(shapeimage->data); \
335 XDestroyImage(shapeimage); } \
336 if (image_pixels) free(image_pixels); \
337 if (mask_pixels) free(mask_pixels); \
338 if (npixels) XFreeColors(display, colormap, pixels, npixels, 0); \
339 if (pixels) free(pixels); \
340 return (status); }
341
342 #else
343
344 #define RETURN(status) \
345 { if (image) XDestroyImage(image); \
346 if (shapeimage) XDestroyImage(shapeimage); \
347 if (image_pixels) free(image_pixels); \
348 if (mask_pixels) free(mask_pixels); \
349 if (npixels) XFreeColors(display, colormap, pixels, npixels, 0); \
350 if (pixels) free(pixels); \
351 return (status); }
352
353 #endif
354
355 /* UIM/X Modification: To support transparent colors,
356 ** we need to pass the widget on which
357 ** the pixmap will be drawn.
358 */
359
360 int UxxpmCreateImage(display, w, attrib, image_return, shapeimage_return, attributes)
361 Display *display;
362 Widget w;
363 xpmInternAttrib *attrib;
364 XImage **image_return;
365 XImage **shapeimage_return;
366 XpmAttributes *attributes;
367 {
368 /* variables stored in the XpmAttributes structure */
369 Visual *visual;
370 Colormap colormap;
371 unsigned int depth;
372
373 /* variables to return */
374 XImage *image = NULL;
375 XImage *shapeimage = NULL;
376 unsigned int mask_pixel;
377 unsigned int ErrorStatus;
378
379 /* calculation variables */
380 Pixel *image_pixels = NULL;
381 Pixel *mask_pixels = NULL;
382 Pixel *pixels = NULL; /* allocated pixels */
383 unsigned int npixels = 0; /* number of allocated pixels */
384
385 /*
386 * retrieve information from the XpmAttributes
387 */
388 if (attributes && attributes->valuemask & XpmVisual)
389 visual = attributes->visual;
390 else
391 visual = DefaultVisual(display, DefaultScreen(display));
392
393 if (attributes && attributes->valuemask & XpmColormap)
394 colormap = attributes->colormap;
395 else
396 colormap = DefaultColormap(display, DefaultScreen(display));
397
398 if (attributes && attributes->valuemask & XpmDepth)
399 depth = attributes->depth;
400 else
401 depth = DefaultDepth(display, DefaultScreen(display));
402
403 ErrorStatus = XpmSuccess;
404
405 /*
406 * malloc pixels index tables
407 */
408
409 image_pixels = (Pixel *) malloc((size_t)(sizeof(Pixel)*attrib->ncolors));
410 if (!image_pixels)
411 return(XpmNoMemory);
412
413 mask_pixels = (Pixel *) malloc((size_t)(sizeof(Pixel)*attrib->ncolors));
414 if (!mask_pixels)
415 RETURN(ErrorStatus);
416
417 mask_pixel = UNDEF_PIXEL;
418
419 /* maximum of allocated pixels will be the number of colors */
420 pixels = (Pixel *) malloc((size_t)(sizeof(Pixel)*attrib->ncolors));
421 if (!pixels)
422 RETURN(ErrorStatus);
423
424 /*
425 * get pixel colors, store them in index tables
426 */
427
428 ErrorStatus = CreateColors(display, w, attributes, attrib->colorTable,
429 attrib->ncolors, image_pixels, mask_pixels,
430 &mask_pixel, &pixels, &npixels);
431 if (ErrorStatus != XpmSuccess)
432 RETURN(ErrorStatus);
433
434 /*
435 * create the image
436 */
437 if (image_return) {
438 ErrorStatus = CreateXImage(display, visual, depth,
439 attrib->width, attrib->height, &image);
440 if (ErrorStatus != XpmSuccess)
441 RETURN(ErrorStatus);
442
443 /*
444 * set the image data
445 *
446 * In case depth is 1 or bits_per_pixel is 4, 6, 8, 24 or 32 use
447 * optimized functions, otherwise use slower but sure general one.
448 *
449 */
450
451 if (image->depth == 1)
452 SetImagePixels1(image, attrib->width, attrib->height,
453 attrib->pixelindex, image_pixels);
454 else if (image->bits_per_pixel == 8)
455 SetImagePixels8(image, attrib->width, attrib->height,
456 attrib->pixelindex, image_pixels);
457 else if (image->bits_per_pixel == 16)
458 SetImagePixels16(image, attrib->width, attrib->height,
459 attrib->pixelindex, image_pixels);
460 else if (image->bits_per_pixel == 32)
461 SetImagePixels32(image, attrib->width, attrib->height,
462 attrib->pixelindex, image_pixels);
463 else
464 SetImagePixels(image, attrib->width, attrib->height,
465 attrib->pixelindex, image_pixels);
466 }
467
468 /*
469 * create the shape mask image
470 */
471 if (mask_pixel != UNDEF_PIXEL && shapeimage_return) {
472 ErrorStatus = CreateXImage(display, visual, 1, attrib->width,
473 attrib->height, &shapeimage);
474 if (ErrorStatus != XpmSuccess)
475 RETURN(ErrorStatus);
476
477 SetImagePixels1(shapeimage, attrib->width, attrib->height,
478 attrib->pixelindex, mask_pixels);
479 }
480 free(mask_pixels);
481 free(pixels);
482
483 /*
484 * if requested store used pixels in the XpmAttributes structure
485 */
486 if (attributes &&
487 (attributes->valuemask & XpmReturnInfos
488 || attributes->valuemask & XpmReturnPixels)) {
489 if (mask_pixel != UNDEF_PIXEL) {
490 Pixel *pixels, *p1, *p2;
491 unsigned int a;
492
493 attributes->npixels = attrib->ncolors - 1;
494 pixels = (Pixel *) malloc((size_t)(sizeof(Pixel) * attributes->npixels));
495 if (pixels) {
496 p1 = image_pixels;
497 p2 = pixels;
498 for (a = 0; a < attrib->ncolors; a++, p1++)
499 if (a != mask_pixel)
500 *p2++ = *p1;
501 attributes->pixels = pixels;
502 } else {
503 /* if error just say we can't return requested data */
504 attributes->valuemask &= ~XpmReturnPixels;
505 attributes->valuemask &= ~XpmReturnInfos;
506 attributes->pixels = NULL;
507 attributes->npixels = 0;
508 }
509 free(image_pixels);
510 } else {
511 attributes->pixels = image_pixels;
512 attributes->npixels = attrib->ncolors;
513 }
514 attributes->mask_pixel = mask_pixel;
515 } else
516 free(image_pixels);
517
518
519 /*
520 * return created images
521 */
522 if (image_return)
523 *image_return = image;
524
525 if (shapeimage_return)
526 *shapeimage_return = shapeimage;
527
528 return (ErrorStatus);
529 }
530
531
532 /*
533 * Create an XImage
534 */
535 static int
536 CreateXImage(display, visual, depth, width, height, image_return)
537 Display *display;
538 Visual *visual;
539 unsigned int depth;
540 unsigned int width;
541 unsigned int height;
542 XImage **image_return;
543 {
544 int bitmap_pad;
545
546 /* first get bitmap_pad */
547 if (depth > 16)
548 bitmap_pad = 32;
549 else if (depth > 8)
550 bitmap_pad = 16;
551 else
552 bitmap_pad = 8;
553
554 /* then create the XImage with data = NULL and bytes_per_line = 0 */
555
556 *image_return = XCreateImage(display, visual, depth, ZPixmap, 0, 0,
557 width, height, bitmap_pad, 0);
558 if (!*image_return)
559 return (XpmNoMemory);
560
561 /* now that bytes_per_line must have been set properly alloc data */
562
563 (*image_return)->data =
564 (char *) malloc((size_t)((*image_return)->bytes_per_line * height));
565
566 if (!(*image_return)->data) {
567 XDestroyImage(*image_return);
568 *image_return = NULL;
569 return (XpmNoMemory);
570 }
571 return (XpmSuccess);
572 }
573
574
575 /*
576 * The functions below are written from X11R5 MIT's code (XImUtil.c)
577 *
578 * The idea is to have faster functions than the standard XPutPixel function
579 * to build the image data. Indeed we can speed up things by suppressing tests
580 * performed for each pixel. We do the same tests but at the image level.
581 * We also assume that we use only ZPixmap images with null offsets.
582 */
583
584 LFUNC(_putbits, int, (register char *src, int dstoffset,
585 register int numbits, register char *dst));
586
587 /*
588 LFUNC(_XReverse_Bytes, int, (register unsigned char *bpt, register int nb));
589 */
590
591 static unsigned char Const _reverse_byte[0x100] = {
592 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
593 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
594 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
595 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
596 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
597 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
598 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
599 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
600 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
601 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
602 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
603 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
604 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
605 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
606 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
607 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
608 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
609 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
610 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
611 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
612 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
613 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
614 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
615 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
616 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
617 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
618 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
619 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
620 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
621 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
622 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
623 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
624 };
625
626 /*
627 static int
628 _XReverse_Bytes(bpt, nb)
629 register unsigned char *bpt;
630 register int nb;
631 {
632 do {
633 *bpt = _reverse_byte[*bpt];
634 bpt++;
635 } while (--nb > 0);
636 return 0;
637 }
638 */
639
640 #ifdef NOT_USED
641 int
642 xpm_xynormalizeimagebits(bp, img)
643 register unsigned char *bp;
644 register XImage *img;
645 {
646 register unsigned char c;
647
648 if (img->byte_order != img->bitmap_bit_order) {
649 switch (img->bitmap_unit) {
650
651 case 16:
652 c = *bp;
653 *bp = *(bp + 1);
654 *(bp + 1) = c;
655 break;
656
657 case 32:
658 c = *(bp + 3);
659 *(bp + 3) = *bp;
660 *bp = c;
661 c = *(bp + 2);
662 *(bp + 2) = *(bp + 1);
663 *(bp + 1) = c;
664 break;
665 }
666 }
667 if (img->bitmap_bit_order == MSBFirst)
668 _XReverse_Bytes(bp, img->bitmap_unit >> 3);
669 }
670 #endif /* NOT_USED */
671
672 #ifdef NOT_USED
673 int
674 xpm_znormalizeimagebits(bp, img)
675 register unsigned char *bp;
676 register XImage *img;
677 {
678 register unsigned char c;
679
680 switch (img->bits_per_pixel) {
681
682 case 4:
683 *bp = ((*bp >> 4) & 0xF) | ((*bp << 4) & ~0xF);
684 break;
685
686 case 16:
687 c = *bp;
688 *bp = *(bp + 1);
689 *(bp + 1) = c;
690 break;
691
692 case 24:
693 c = *(bp + 2);
694 *(bp + 2) = *bp;
695 *bp = c;
696 break;
697
698 case 32:
699 c = *(bp + 3);
700 *(bp + 3) = *bp;
701 *bp = c;
702 c = *(bp + 2);
703 *(bp + 2) = *(bp + 1);
704 *(bp + 1) = c;
705 break;
706 }
707 }
708 #endif /* NOT_USED */
709
710 static unsigned char Const _lomask[0x09] = {
711 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff};
712 static unsigned char Const _himask[0x09] = {
713 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00};
714
715 static int _putbits(src, dstoffset, numbits, dst)
716 register char *src; /* address of source bit string */
717 int dstoffset; /* bit offset into destination;
718 * range is 0-31 */
719 register int numbits; /* number of bits to copy to
720 * destination */
721 register char *dst; /* address of destination bit string */
722 {
723 register unsigned char chlo, chhi;
724 int hibits;
725
726 dst = dst + (dstoffset >> 3);
727 dstoffset = dstoffset & 7;
728 hibits = 8 - dstoffset;
729 chlo = *dst & _lomask[dstoffset];
730 for (;;) {
731 chhi = (*src << dstoffset) & _himask[dstoffset];
732 if (numbits <= hibits) {
733 chhi = chhi & _lomask[dstoffset + numbits];
734 *dst = (*dst & _himask[dstoffset + numbits]) | chlo | chhi;
735 break;
736 }
737 *dst = chhi | chlo;
738 dst++;
739 numbits = numbits - hibits;
740 chlo = (unsigned char) (*src & _himask[hibits]) >> hibits;
741 src++;
742 if (numbits <= dstoffset) {
743 chlo = chlo & _lomask[numbits];
744 *dst = (*dst & _himask[numbits]) | chlo;
745 break;
746 }
747 numbits = numbits - dstoffset;
748 }
749 return 0;
750 }
751
752 /*
753 * Default method to write pixels into a Z image data structure.
754 * The algorithm used is:
755 *
756 * copy the destination bitmap_unit or Zpixel to temp
757 * normalize temp if needed
758 * copy the pixel bits into the temp
759 * renormalize temp if needed
760 * copy the temp back into the destination image data
761 */
762
763 static int
764 SetImagePixels(image, width, height, pixelindex, pixels)
765 XImage *image;
766 unsigned int width;
767 unsigned int height;
768 unsigned int *pixelindex;
769 Pixel *pixels;
770 {
771 register char *src;
772 register char *dst;
773 register unsigned int *iptr;
774 register int x, y, i;
775 register char *data;
776 Pixel pixel, px;
777 int nbytes, depth, ibu, ibpp;
778
779 data = image->data;
780 iptr = pixelindex;
781 depth = image->depth;
782 if (image->depth == 1) {
783 ibu = image->bitmap_unit;
784 for (y = 0; y < height; y++)
785 for (x = 0; x < width; x++, iptr++) {
786 pixel = pixels[*iptr];
787 for (i = 0, px = pixel;
788 i < sizeof(unsigned long); i++, px >>= 8)
789 ((unsigned char *) &pixel)[i] = px;
790 src = &data[XYINDEX(x, y, image)];
791 dst = (char *) &px;
792 px = 0;
793 nbytes = ibu >> 3;
794 for (i = nbytes; --i >= 0;)
795 *dst++ = *src++;
796 XYNORMALIZE(&px, image);
797 _putbits((char *) &pixel, (x % ibu), 1, (char *) &px);
798 XYNORMALIZE(&px, image);
799 src = (char *) &px;
800 dst = &data[XYINDEX(x, y, image)];
801 for (i = nbytes; --i >= 0;)
802 *dst++ = *src++;
803 }
804 } else {
805 ibpp = image->bits_per_pixel;
806 for (y = 0; y < height; y++)
807 for (x = 0; x < width; x++, iptr++) {
808 pixel = pixels[*iptr];
809 if (depth == 4)
810 pixel &= 0xf;
811 for (i = 0, px = pixel;
812 i < sizeof(unsigned long); i++, px >>= 8)
813 ((unsigned char *) &pixel)[i] = px;
814 src = &data[ZINDEX(x, y, image)];
815 dst = (char *) &px;
816 px = 0;
817 nbytes = (ibpp + 7) >> 3;
818 for (i = nbytes; --i >= 0;)
819 *dst++ = *src++;
820 ZNORMALIZE(&px, image);
821 _putbits((char *) &pixel, (x * ibpp) & 7, ibpp, (char *) &px);
822 ZNORMALIZE(&px, image);
823 src = (char *) &px;
824 dst = &data[ZINDEX(x, y, image)];
825 for (i = nbytes; --i >= 0;)
826 *dst++ = *src++;
827 }
828 }
829 return 0;
830 }
831
832 /*
833 * write pixels into a 32-bits Z image data structure
834 */
835
836 #ifndef WORD64
837 static unsigned long byteorderpixel = MSBFirst << 24;
838
839 #endif
840
841 static int
842 SetImagePixels32(image, width, height, pixelindex, pixels)
843 XImage *image;
844 unsigned int width;
845 unsigned int height;
846 unsigned int *pixelindex;
847 Pixel *pixels;
848 {
849 register unsigned char *addr;
850 register unsigned char *data;
851 register unsigned int *iptr;
852 register int x, y;
853 Pixel pixel;
854
855 data = (unsigned char *) image->data;
856 iptr = pixelindex;
857 #ifndef WORD64
858 if (*((char *) &byteorderpixel) == image->byte_order) {
859 for (y = 0; y < height; y++)
860 for (x = 0; x < width; x++, iptr++) {
861 addr = &data[ZINDEX32(x, y, image)];
862 *((unsigned long *)addr) = pixels[*iptr];
863 }
864 } else
865 #endif
866 if (image->byte_order == MSBFirst)
867 for (y = 0; y < height; y++)
868 for (x = 0; x < width; x++, iptr++) {
869 addr = &data[ZINDEX32(x, y, image)];
870 pixel = pixels[*iptr];
871 addr[0] = pixel >> 24;
872 addr[1] = pixel >> 16;
873 addr[2] = pixel >> 8;
874 addr[3] = pixel;
875 }
876 else
877 for (y = 0; y < height; y++)
878 for (x = 0; x < width; x++, iptr++) {
879 addr = &data[ZINDEX32(x, y, image)];
880 pixel = pixels[*iptr];
881 addr[0] = pixel;
882 addr[1] = pixel >> 8;
883 addr[2] = pixel >> 16;
884 addr[3] = pixel >> 24;
885 }
886 return 0;
887 }
888
889 /*
890 * write pixels into a 16-bits Z image data structure
891 */
892
893 static int
894 SetImagePixels16(image, width, height, pixelindex, pixels)
895 XImage *image;
896 unsigned int width;
897 unsigned int height;
898 unsigned int *pixelindex;
899 Pixel *pixels;
900 {
901 register unsigned char *addr;
902 register unsigned char *data;
903 register unsigned int *iptr;
904 register int x, y;
905
906 data = (unsigned char *) image->data;
907 iptr = pixelindex;
908 if (image->byte_order == MSBFirst)
909 for (y = 0; y < height; y++)
910 for (x = 0; x < width; x++, iptr++) {
911 addr = &data[ZINDEX16(x, y, image)];
912 addr[0] = pixels[*iptr] >> 8;
913 addr[1] = pixels[*iptr];
914 }
915 else
916 for (y = 0; y < height; y++)
917 for (x = 0; x < width; x++, iptr++) {
918 addr = &data[ZINDEX16(x, y, image)];
919 addr[0] = pixels[*iptr];
920 addr[1] = pixels[*iptr] >> 8;
921 }
922 return 0;
923 }
924
925 /*
926 * write pixels into a 8-bits Z image data structure
927 */
928
929 static int
930 SetImagePixels8(image, width, height, pixelindex, pixels)
931 XImage *image;
932 unsigned int width;
933 unsigned int height;
934 unsigned int *pixelindex;
935 Pixel *pixels;
936 {
937 register char *data;
938 register unsigned int *iptr;
939 register int x, y;
940
941 data = image->data;
942 iptr = pixelindex;
943 for (y = 0; y < height; y++)
944 for (x = 0; x < width; x++, iptr++)
945 data[ZINDEX8(x, y, image)] = pixels[*iptr];
946 return 0;
947 }
948
949 /*
950 * write pixels into a 1-bit depth image data structure and **offset null**
951 */
952
953 static int
954 SetImagePixels1(image, width, height, pixelindex, pixels)
955 XImage *image;
956 unsigned int width;
957 unsigned int height;
958 unsigned int *pixelindex;
959 Pixel *pixels;
960 {
961 register unsigned int *iptr;
962 register int x, y;
963 register char *data;
964
965 if (image->byte_order != image->bitmap_bit_order)
966 SetImagePixels(image, width, height, pixelindex, pixels);
967 else {
968 data = image->data;
969 iptr = pixelindex;
970 if (image->bitmap_bit_order == MSBFirst)
971 for (y = 0; y < height; y++)
972 for (x = 0; x < width; x++, iptr++) {
973 if (pixels[*iptr] & 1)
974 data[ZINDEX1(x, y, image)] |= 0x80 >> (x & 7);
975 else
976 data[ZINDEX1(x, y, image)] &= ~(0x80 >> (x & 7));
977 }
978 else
979 for (y = 0; y < height; y++)
980 for (x = 0; x < width; x++, iptr++) {
981 if (pixels[*iptr] & 1)
982 data[ZINDEX1(x, y, image)] |= 1 << (x & 7);
983 else
984 data[ZINDEX1(x, y, image)] &= ~(1 << x & 7);
985 }
986 }
987 return 0;
988 }