Debian Codegraph @

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