Debian Codegraph @

nx-X11/lib/src/ImUtil.c

raw
1 /*
2
3 Copyright 1986, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
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 THE
17 OPEN GROUP 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 The Open Group 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 The Open Group.
24
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <nx-X11/Xlibint.h>
31 #include <nx-X11/Xutil.h>
32 #include <stdio.h>
33 #include "ImUtil.h"
34
35 static int _XDestroyImage(XImage *);
36 static unsigned long _XGetPixel(XImage *, int, int);
37 static unsigned long _XGetPixel1(XImage *, int, int);
38 static unsigned long _XGetPixel8(XImage *, int, int);
39 static unsigned long _XGetPixel16(XImage *, int, int);
40 static unsigned long _XGetPixel32(XImage *, int, int);
41 static int _XPutPixel(XImage *, int, int, unsigned long);
42 static int _XPutPixel1(XImage *, int, int, unsigned long);
43 static int _XPutPixel8(XImage *, int, int, unsigned long);
44 static int _XPutPixel16(XImage *, int, int, unsigned long);
45 static int _XPutPixel32(XImage *, int, int, unsigned long);
46 static XImage *_XSubImage(XImage *, int, int, unsigned int, unsigned int);
47 static int _XAddPixel(XImage *, long);
48
49 static unsigned char const _lomask[0x09] = { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
50 static unsigned char const _himask[0x09] = { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };
51
52 /* These two convenience routines return the scanline_pad and bits_per_pixel
53 associated with a specific depth of ZPixmap format image for a
54 display. */
55
56 int
57 _XGetScanlinePad(
58 Display *dpy,
59 int depth)
60 {
61 register ScreenFormat *fmt = dpy->pixmap_format;
62 register int i;
63
64 for (i = dpy->nformats + 1; --i; ++fmt)
65 if (fmt->depth == depth)
66 return(fmt->scanline_pad);
67
68 return(dpy->bitmap_pad);
69 }
70
71 int
72 _XGetBitsPerPixel(
73 Display *dpy,
74 int depth)
75 {
76 register ScreenFormat *fmt = dpy->pixmap_format;
77 register int i;
78
79 for (i = dpy->nformats + 1; --i; ++fmt)
80 if (fmt->depth == depth)
81 return(fmt->bits_per_pixel);
82 if (depth <= 4)
83 return 4;
84 if (depth <= 8)
85 return 8;
86 if (depth <= 16)
87 return 16;
88 return 32;
89 }
90
91
92 /*
93 * This module provides rudimentary manipulation routines for image data
94 * structures. The functions provided are:
95 *
96 * XCreateImage Creates a default XImage data structure
97 * _XDestroyImage Deletes an XImage data structure
98 * _XGetPixel Reads a pixel from an image data structure
99 * _XGetPixel32 Reads a pixel from a 32-bit Z image data structure
100 * _XGetPixel16 Reads a pixel from a 16-bit Z image data structure
101 * _XGetPixel8 Reads a pixel from an 8-bit Z image data structure
102 * _XGetPixel1 Reads a pixel from an 1-bit image data structure
103 * _XPutPixel Writes a pixel into an image data structure
104 * _XPutPixel32 Writes a pixel into a 32-bit Z image data structure
105 * _XPutPixel16 Writes a pixel into a 16-bit Z image data structure
106 * _XPutPixel8 Writes a pixel into an 8-bit Z image data structure
107 * _XPutPixel1 Writes a pixel into an 1-bit image data structure
108 * _XSubImage Clones a new (sub)image from an existing one
109 * _XSetImage Writes an image data pattern into another image
110 * _XAddPixel Adds a constant value to every pixel in an image
111 *
112 * The logic contained in these routines makes several assumptions about
113 * the image data structures, and at least for current implementations
114 * these assumptions are believed to be true. They are:
115 *
116 * For all formats, bits_per_pixel is less than or equal to 32.
117 * For XY formats, bitmap_unit is always less than or equal to bitmap_pad.
118 * For XY formats, bitmap_unit is 8, 16, or 32 bits.
119 * For Z format, bits_per_pixel is 1, 4, 8, 16, 24, or 32 bits.
120 */
121 static void _xynormalizeimagebits (
122 register unsigned char *bp,
123 register XImage *img)
124 {
125 register unsigned char c;
126
127 if (img->byte_order != img->bitmap_bit_order) {
128 switch (img->bitmap_unit) {
129
130 case 16:
131 c = *bp;
132 *bp = *(bp + 1);
133 *(bp + 1) = c;
134 break;
135
136 case 32:
137 c = *(bp + 3);
138 *(bp + 3) = *bp;
139 *bp = c;
140 c = *(bp + 2);
141 *(bp + 2) = *(bp + 1);
142 *(bp + 1) = c;
143 break;
144 }
145 }
146 if (img->bitmap_bit_order == MSBFirst)
147 _XReverse_Bytes (bp, img->bitmap_unit >> 3);
148 }
149
150 static void _znormalizeimagebits (
151 register unsigned char *bp,
152 register XImage *img)
153 {
154 register unsigned char c;
155 switch (img->bits_per_pixel) {
156
157 case 4:
158 *bp = ((*bp >> 4) & 0xF) | ((*bp << 4) & ~0xF);
159 break;
160
161 case 16:
162 c = *bp;
163 *bp = *(bp + 1);
164 *(bp + 1) = c;
165 break;
166
167 case 24:
168 c = *(bp + 2);
169 *(bp + 2) = *bp;
170 *bp = c;
171 break;
172
173 case 32:
174 c = *(bp + 3);
175 *(bp + 3) = *bp;
176 *bp = c;
177 c = *(bp + 2);
178 *(bp + 2) = *(bp + 1);
179 *(bp + 1) = c;
180 break;
181 }
182 }
183
184 static void _putbits(
185 register char *src, /* address of source bit string */
186 int dstoffset, /* bit offset into destination; range is 0-31 */
187 register int numbits,/* number of bits to copy to destination */
188 register char *dst) /* address of destination bit string */
189 {
190 register unsigned char chlo, chhi;
191 int hibits;
192 dst = dst + (dstoffset >> 3);
193 dstoffset = dstoffset & 7;
194 hibits = 8 - dstoffset;
195 chlo = *dst & _lomask[dstoffset];
196 for (;;) {
197 chhi = (*src << dstoffset) & _himask[dstoffset];
198 if (numbits <= hibits) {
199 chhi = chhi & _lomask[dstoffset + numbits];
200 *dst = (*dst & _himask[dstoffset + numbits]) | chlo | chhi;
201 break;
202 }
203 *dst = chhi | chlo;
204 dst++;
205 numbits = numbits - hibits;
206 chlo = (unsigned char) (*src & _himask[hibits]) >> hibits;
207 src++;
208 if (numbits <= dstoffset) {
209 chlo = chlo & _lomask[numbits];
210 *dst = (*dst & _himask[numbits]) | chlo;
211 break;
212 }
213 numbits = numbits - dstoffset;
214 }
215 }
216
217
218 /*
219 * Macros
220 *
221 * The ROUNDUP macro rounds up a quantity to the specified boundary,
222 * then truncates to bytes.
223 *
224 * The XYNORMALIZE macro determines whether XY format data requires
225 * normalization and calls a routine to do so if needed. The logic in
226 * this module is designed for LSBFirst byte and bit order, so
227 * normalization is done as required to present the data in this order.
228 *
229 * The ZNORMALIZE macro performs byte and nibble order normalization if
230 * required for Z format data.
231 *
232 * The XYINDEX macro computes the index to the starting byte (char) boundary
233 * for a bitmap_unit containing a pixel with coordinates x and y for image
234 * data in XY format.
235 *
236 * The ZINDEX macro computes the index to the starting byte (char) boundary
237 * for a pixel with coordinates x and y for image data in ZPixmap format.
238 *
239 */
240
241 #define ROUNDUP(nbytes, pad) ((((nbytes) + ((pad)-1)) / (pad)) * ((pad)>>3))
242
243 #define XYNORMALIZE(bp, img) \
244 if ((img->byte_order == MSBFirst) || (img->bitmap_bit_order == MSBFirst)) \
245 _xynormalizeimagebits((unsigned char *)(bp), img)
246
247 #define ZNORMALIZE(bp, img) \
248 if (img->byte_order == MSBFirst) \
249 _znormalizeimagebits((unsigned char *)(bp), img)
250
251 #define XYINDEX(x, y, img) \
252 ((y) * img->bytes_per_line) + \
253 (((x) + img->xoffset) / img->bitmap_unit) * (img->bitmap_unit >> 3)
254
255 #define ZINDEX(x, y, img) ((y) * img->bytes_per_line) + \
256 (((x) * img->bits_per_pixel) >> 3)
257
258 /*
259 * This routine initializes the image object function pointers. The
260 * intent is to provide native (i.e. fast) routines for native format images
261 * only using the generic (i.e. slow) routines when fast ones don't exist.
262 * However, with the current rather botched external interface, clients may
263 * have to mung image attributes after the image gets created, so the fast
264 * routines always have to check to make sure the optimization is still
265 * valid, and reinit the functions if not.
266 */
267 void _XInitImageFuncPtrs (
268 XImage *image)
269 {
270 image->f.create_image = XCreateImage;
271 image->f.destroy_image = _XDestroyImage;
272 if ((image->format == ZPixmap) && (image->bits_per_pixel == 8)) {
273 image->f.get_pixel = _XGetPixel8;
274 image->f.put_pixel = _XPutPixel8;
275 } else if (((image->bits_per_pixel | image->depth) == 1) &&
276 (image->byte_order == image->bitmap_bit_order)) {
277 image->f.get_pixel = _XGetPixel1;
278 image->f.put_pixel = _XPutPixel1;
279 } else if ((image->format == ZPixmap) &&
280 (image->bits_per_pixel == 32)) {
281 image->f.get_pixel = _XGetPixel32;
282 image->f.put_pixel = _XPutPixel32;
283 } else if ((image->format == ZPixmap) &&
284 (image->bits_per_pixel == 16)) {
285 image->f.get_pixel = _XGetPixel16;
286 image->f.put_pixel = _XPutPixel16;
287 } else {
288 image->f.get_pixel = _XGetPixel;
289 image->f.put_pixel = _XPutPixel;
290 }
291 image->f.sub_image = _XSubImage;
292 /* image->f.set_image = _XSetImage;*/
293 image->f.add_pixel = _XAddPixel;
294 }
295
296 /*
297 * CreateImage
298 *
299 * Allocates the memory necessary for an XImage data structure.
300 * Initializes the structure with "default" values and returns XImage.
301 *
302 */
303
304 XImage *XCreateImage (
305 register Display *dpy,
306 register Visual *visual,
307 unsigned int depth,
308 int format,
309 int offset, /*How many pixels from the start of the data does the
310 picture to be transmitted start?*/
311
312 char *data,
313 unsigned int width,
314 unsigned int height,
315 int xpad,
316 int image_bytes_per_line)
317 /*How many bytes between a pixel on one line and the pixel with
318 the same X coordinate on the next line? 0 means
319 XCreateImage can calculate it.*/
320 {
321 register XImage *image;
322 int bits_per_pixel = 1;
323 int min_bytes_per_line;
324
325 if (depth == 0 || depth > 32 ||
326 (format != XYBitmap && format != XYPixmap && format != ZPixmap) ||
327 (format == XYBitmap && depth != 1) ||
328 (xpad != 8 && xpad != 16 && xpad != 32) ||
329 offset < 0)
330 return (XImage *) NULL;
331 if ((image = Xcalloc(1, sizeof(XImage))) == NULL)
332 return (XImage *) NULL;
333
334 image->width = width;
335 image->height = height;
336 image->format = format;
337 image->byte_order = dpy->byte_order;
338 image->bitmap_unit = dpy->bitmap_unit;
339 image->bitmap_bit_order = dpy->bitmap_bit_order;
340 if (visual != NULL) {
341 image->red_mask = visual->red_mask;
342 image->green_mask = visual->green_mask;
343 image->blue_mask = visual->blue_mask;
344 }
345 else {
346 image->red_mask = image->green_mask = image->blue_mask = 0;
347 }
348 if (format == ZPixmap)
349 {
350 bits_per_pixel = _XGetBitsPerPixel(dpy, (int) depth);
351 }
352
353 image->xoffset = offset;
354 image->bitmap_pad = xpad;
355 image->depth = depth;
356 image->data = data;
357 /*
358 * compute per line accelerator.
359 */
360 {
361 if (format == ZPixmap)
362 min_bytes_per_line =
363 ROUNDUP((bits_per_pixel * width), image->bitmap_pad);
364 else
365 min_bytes_per_line =
366 ROUNDUP((width + offset), image->bitmap_pad);
367 }
368 if (image_bytes_per_line == 0) {
369 image->bytes_per_line = min_bytes_per_line;
370 } else if (image_bytes_per_line < min_bytes_per_line) {
371 Xfree(image);
372 return NULL;
373 } else {
374 image->bytes_per_line = image_bytes_per_line;
375 }
376
377 image->bits_per_pixel = bits_per_pixel;
378 image->obdata = NULL;
379 _XInitImageFuncPtrs (image);
380
381 return image;
382 }
383
384 Status XInitImage (XImage *image)
385 {
386 int min_bytes_per_line;
387
388 if (image->depth == 0 || image->depth > 32 ||
389 image->bits_per_pixel > 32 || image->bitmap_unit > 32 ||
390 image->bits_per_pixel < 0 || image->bitmap_unit < 0 ||
391 (image->format != XYBitmap &&
392 image->format != XYPixmap &&
393 image->format != ZPixmap) ||
394 (image->format == XYBitmap && image->depth != 1) ||
395 (image->bitmap_pad != 8 &&
396 image->bitmap_pad != 16 &&
397 image->bitmap_pad != 32) ||
398 image->xoffset < 0)
399 return 0;
400
401 /*
402 * compute per line accelerator.
403 */
404 if (image->format == ZPixmap)
405 min_bytes_per_line =
406 ROUNDUP((image->bits_per_pixel * image->width),
407 image->bitmap_pad);
408 else
409 min_bytes_per_line =
410 ROUNDUP((image->width + image->xoffset), image->bitmap_pad);
411
412 if (image->bytes_per_line == 0) {
413 image->bytes_per_line = min_bytes_per_line;
414 } else if (image->bytes_per_line < min_bytes_per_line) {
415 return 0;
416 }
417
418 _XInitImageFuncPtrs (image);
419
420 return 1;
421 }
422
423 /*
424 * _DestroyImage
425 *
426 * Deallocates the memory associated with the ximage data structure.
427 * this version handles the case of the image data being malloc'd
428 * entirely by the library.
429 */
430
431 static int _XDestroyImage (XImage *ximage)
432 {
433 Xfree(ximage->data);
434 Xfree(ximage->obdata);
435 Xfree(ximage);
436 return 1;
437 }
438
439
440 /*
441 * GetPixel
442 *
443 * Returns the specified pixel. The X and Y coordinates are relative to
444 * the origin (upper left [0,0]) of the image. The pixel value is returned
445 * in normalized format, i.e. the LSB of the long is the LSB of the pixel.
446 * The algorithm used is:
447 *
448 * copy the source bitmap_unit or Zpixel into temp
449 * normalize temp if needed
450 * extract the pixel bits into return value
451 *
452 */
453
454 static unsigned long const low_bits_table[] = {
455 0x00000000, 0x00000001, 0x00000003, 0x00000007,
456 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
457 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
458 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
459 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
460 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
461 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
462 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
463 0xffffffff
464 };
465
466 static unsigned long _XGetPixel (
467 register XImage *ximage,
468 int x,
469 int y)
470
471 {
472 unsigned long pixel, px;
473 register char *src;
474 register char *dst;
475 register int i, j;
476 int bits, nbytes;
477 long plane;
478
479 if ((ximage->bits_per_pixel | ximage->depth) == 1) {
480 src = &ximage->data[XYINDEX(x, y, ximage)];
481 dst = (char *)&pixel;
482 pixel = 0;
483 for (i = ximage->bitmap_unit >> 3; --i >= 0; ) *dst++ = *src++;
484 XYNORMALIZE(&pixel, ximage);
485 bits = (x + ximage->xoffset) % ximage->bitmap_unit;
486 pixel = ((((char *)&pixel)[bits>>3])>>(bits&7)) & 1;
487 } else if (ximage->format == XYPixmap) {
488 pixel = 0;
489 plane = 0;
490 nbytes = ximage->bitmap_unit >> 3;
491 for (i = ximage->depth; --i >= 0; ) {
492 src = &ximage->data[XYINDEX(x, y, ximage)+ plane];
493 dst = (char *)&px;
494 px = 0;
495 for (j = nbytes; --j >= 0; ) *dst++ = *src++;
496 XYNORMALIZE(&px, ximage);
497 bits = (x + ximage->xoffset) % ximage->bitmap_unit;
498 pixel = (pixel << 1) |
499 (((((char *)&px)[bits>>3])>>(bits&7)) & 1);
500 plane = plane + (ximage->bytes_per_line * ximage->height);
501 }
502 } else if (ximage->format == ZPixmap) {
503 src = &ximage->data[ZINDEX(x, y, ximage)];
504 dst = (char *)&px;
505 px = 0;
506 for (i = (ximage->bits_per_pixel + 7) >> 3; --i >= 0; )
507 *dst++ = *src++;
508 ZNORMALIZE(&px, ximage);
509 pixel = 0;
510 for (i=sizeof(unsigned long); --i >= 0; )
511 pixel = (pixel << 8) | ((unsigned char *)&px)[i];
512 if (ximage->bits_per_pixel == 4) {
513 if (x & 1)
514 pixel >>= 4;
515 else
516 pixel &= 0xf;
517 }
518 } else {
519 return 0; /* bad image */
520 }
521 if (ximage->bits_per_pixel == ximage->depth)
522 return pixel;
523 else
524 return (pixel & low_bits_table[ximage->depth]);
525 }
526
527 static CARD32 const byteorderpixel = MSBFirst << 24;
528
529 static unsigned long _XGetPixel32 (
530 register XImage *ximage,
531 int x,
532 int y)
533 {
534 register unsigned char *addr;
535 unsigned long pixel;
536
537 if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 32)) {
538 addr = &((unsigned char *)ximage->data)
539 [y * ximage->bytes_per_line + (x << 2)];
540 if (*((const char *)&byteorderpixel) == ximage->byte_order)
541 pixel = *((CARD32 *)addr);
542 else if (ximage->byte_order == MSBFirst)
543 pixel = ((unsigned long)addr[0] << 24 |
544 (unsigned long)addr[1] << 16 |
545 (unsigned long)addr[2] << 8 |
546 addr[3]);
547 else
548 pixel = ((unsigned long)addr[3] << 24 |
549 (unsigned long)addr[2] << 16 |
550 (unsigned long)addr[1] << 8 |
551 addr[0]);
552 if (ximage->depth != 32)
553 pixel &= low_bits_table[ximage->depth];
554 return pixel;
555 } else {
556 _XInitImageFuncPtrs(ximage);
557 return XGetPixel(ximage, x, y);
558 }
559 }
560
561 static unsigned long _XGetPixel16 (
562 register XImage *ximage,
563 int x,
564 int y)
565 {
566 register unsigned char *addr;
567 unsigned long pixel;
568
569 if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 16)) {
570 addr = &((unsigned char *)ximage->data)
571 [y * ximage->bytes_per_line + (x << 1)];
572 if (ximage->byte_order == MSBFirst)
573 pixel = addr[0] << 8 | addr[1];
574 else
575 pixel = addr[1] << 8 | addr[0];
576 if (ximage->depth != 16)
577 pixel &= low_bits_table[ximage->depth];
578 return pixel;
579 } else {
580 _XInitImageFuncPtrs(ximage);
581 return XGetPixel(ximage, x, y);
582 }
583 }
584
585 static unsigned long _XGetPixel8 (
586 register XImage *ximage,
587 int x,
588 int y)
589 {
590 unsigned char pixel;
591
592 if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 8)) {
593 pixel = ((unsigned char *)ximage->data)
594 [y * ximage->bytes_per_line + x];
595 if (ximage->depth != 8)
596 pixel &= low_bits_table[ximage->depth];
597 return pixel;
598 } else {
599 _XInitImageFuncPtrs(ximage);
600 return XGetPixel(ximage, x, y);
601 }
602 }
603
604 static unsigned long _XGetPixel1 (
605 register XImage *ximage,
606 int x,
607 int y)
608 {
609 unsigned char bit;
610 int xoff, yoff;
611
612 if (((ximage->bits_per_pixel | ximage->depth) == 1) &&
613 (ximage->byte_order == ximage->bitmap_bit_order)) {
614 xoff = x + ximage->xoffset;
615 yoff = y * ximage->bytes_per_line + (xoff >> 3);
616 xoff &= 7;
617 if (ximage->bitmap_bit_order == MSBFirst)
618 bit = 0x80 >> xoff;
619 else
620 bit = 1 << xoff;
621 return (ximage->data[yoff] & bit) ? 1 : 0;
622 } else {
623 _XInitImageFuncPtrs(ximage);
624 return XGetPixel(ximage, x, y);
625 }
626 }
627
628 /*
629 * PutPixel
630 *
631 * Overwrites the specified pixel. The X and Y coordinates are relative to
632 * the origin (upper left [0,0]) of the image. The input pixel value must be
633 * in normalized format, i.e. the LSB of the long is the LSB of the pixel.
634 * The algorithm used is:
635 *
636 * copy the destination bitmap_unit or Zpixel to temp
637 * normalize temp if needed
638 * copy the pixel bits into the temp
639 * renormalize temp if needed
640 * copy the temp back into the destination image data
641 *
642 */
643
644 static int _XPutPixel (
645 register XImage *ximage,
646 int x,
647 int y,
648 unsigned long pixel)
649
650 {
651 unsigned long px, npixel;
652 register char *src;
653 register char *dst;
654 register int i;
655 int j, nbytes;
656 long plane;
657
658 if (ximage->depth == 4)
659 pixel &= 0xf;
660 npixel = pixel;
661 for (i=0, px=pixel; i<sizeof(unsigned long); i++, px>>=8)
662 ((unsigned char *)&pixel)[i] = px;
663 if ((ximage->bits_per_pixel | ximage->depth) == 1) {
664 src = &ximage->data[XYINDEX(x, y, ximage)];
665 dst = (char *)&px;
666 px = 0;
667 nbytes = ximage->bitmap_unit >> 3;
668 for (i = nbytes; --i >= 0; ) *dst++ = *src++;
669 XYNORMALIZE(&px, ximage);
670 i = ((x + ximage->xoffset) % ximage->bitmap_unit);
671 _putbits ((char *)&pixel, i, 1, (char *)&px);
672 XYNORMALIZE(&px, ximage);
673 src = (char *) &px;
674 dst = &ximage->data[XYINDEX(x, y, ximage)];
675 for (i = nbytes; --i >= 0; ) *dst++ = *src++;
676 } else if (ximage->format == XYPixmap) {
677 plane = (ximage->bytes_per_line * ximage->height) *
678 (ximage->depth - 1); /* do least signif plane 1st */
679 nbytes = ximage->bitmap_unit >> 3;
680 for (j = ximage->depth; --j >= 0; ) {
681 src = &ximage->data[XYINDEX(x, y, ximage) + plane];
682 dst = (char *) &px;
683 px = 0;
684 for (i = nbytes; --i >= 0; ) *dst++ = *src++;
685 XYNORMALIZE(&px, ximage);
686 i = ((x + ximage->xoffset) % ximage->bitmap_unit);
687 _putbits ((char *)&pixel, i, 1, (char *)&px);
688 XYNORMALIZE(&px, ximage);
689 src = (char *)&px;
690 dst = &ximage->data[XYINDEX(x, y, ximage) + plane];
691 for (i = nbytes; --i >= 0; ) *dst++ = *src++;
692 npixel = npixel >> 1;
693 for (i=0, px=npixel; i<sizeof(unsigned long); i++, px>>=8)
694 ((unsigned char *)&pixel)[i] = px;
695 plane = plane - (ximage->bytes_per_line * ximage->height);
696 }
697 } else if (ximage->format == ZPixmap) {
698 src = &ximage->data[ZINDEX(x, y, ximage)];
699 dst = (char *)&px;
700 px = 0;
701 nbytes = (ximage->bits_per_pixel + 7) >> 3;
702 for (i = nbytes; --i >= 0; ) *dst++ = *src++;
703 ZNORMALIZE(&px, ximage);
704 _putbits ((char *)&pixel,
705 (x * ximage->bits_per_pixel) & 7,
706 ximage->bits_per_pixel, (char *)&px);
707 ZNORMALIZE(&px, ximage);
708 src = (char *)&px;
709 dst = &ximage->data[ZINDEX(x, y, ximage)];
710 for (i = nbytes; --i >= 0; ) *dst++ = *src++;
711 } else {
712 return 0; /* bad image */
713 }
714 return 1;
715 }
716
717 static int _XPutPixel32 (
718 register XImage *ximage,
719 int x,
720 int y,
721 unsigned long pixel)
722 {
723 unsigned char *addr;
724
725 if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 32)) {
726 addr = &((unsigned char *)ximage->data)
727 [y * ximage->bytes_per_line + (x << 2)];
728 if (*((const char *)&byteorderpixel) == ximage->byte_order)
729 *((CARD32 *)addr) = pixel;
730 else if (ximage->byte_order == MSBFirst) {
731 addr[0] = pixel >> 24;
732 addr[1] = pixel >> 16;
733 addr[2] = pixel >> 8;
734 addr[3] = pixel;
735 } else {
736 addr[3] = pixel >> 24;
737 addr[2] = pixel >> 16;
738 addr[1] = pixel >> 8;
739 addr[0] = pixel;
740 }
741 return 1;
742 } else {
743 _XInitImageFuncPtrs(ximage);
744 return XPutPixel(ximage, x, y, pixel);
745 }
746 }
747
748 static int _XPutPixel16 (
749 register XImage *ximage,
750 int x,
751 int y,
752 unsigned long pixel)
753 {
754 unsigned char *addr;
755
756 if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 16)) {
757 addr = &((unsigned char *)ximage->data)
758 [y * ximage->bytes_per_line + (x << 1)];
759 if (ximage->byte_order == MSBFirst) {
760 addr[0] = pixel >> 8;
761 addr[1] = pixel;
762 } else {
763 addr[1] = pixel >> 8;
764 addr[0] = pixel;
765 }
766 return 1;
767 } else {
768 _XInitImageFuncPtrs(ximage);
769 return XPutPixel(ximage, x, y, pixel);
770 }
771 }
772
773 static int _XPutPixel8 (
774 register XImage *ximage,
775 int x,
776 int y,
777 unsigned long pixel)
778 {
779 if ((ximage->format == ZPixmap) && (ximage->bits_per_pixel == 8)) {
780 ximage->data[y * ximage->bytes_per_line + x] = pixel;
781 return 1;
782 } else {
783 _XInitImageFuncPtrs(ximage);
784 return XPutPixel(ximage, x, y, pixel);
785 }
786 }
787
788 static int _XPutPixel1 (
789 register XImage *ximage,
790 int x,
791 int y,
792 unsigned long pixel)
793 {
794 unsigned char bit;
795 int xoff, yoff;
796
797 if (((ximage->bits_per_pixel | ximage->depth) == 1) &&
798 (ximage->byte_order == ximage->bitmap_bit_order)) {
799 xoff = x + ximage->xoffset;
800 yoff = y * ximage->bytes_per_line + (xoff >> 3);
801 xoff &= 7;
802 if (ximage->bitmap_bit_order == MSBFirst)
803 bit = 0x80 >> xoff;
804 else
805 bit = 1 << xoff;
806 if (pixel & 1)
807 ximage->data[yoff] |= bit;
808 else
809 ximage->data[yoff] &= ~bit;
810 return 1;
811 } else {
812 _XInitImageFuncPtrs(ximage);
813 return XPutPixel(ximage, x, y, pixel);
814 }
815 }
816
817 /*
818 * SubImage
819 *
820 * Creates a new image that is a subsection of an existing one.
821 * Allocates the memory necessary for the new XImage data structure.
822 * Pointer to new image is returned. The algorithm used is repetitive
823 * calls to get and put pixel.
824 *
825 */
826
827 static XImage *_XSubImage (
828 XImage *ximage,
829 register int x, /* starting x coordinate in existing image */
830 register int y, /* starting y coordinate in existing image */
831 unsigned int width, /* width in pixels of new subimage */
832 unsigned int height)/* height in pixels of new subimage */
833
834 {
835 register XImage *subimage;
836 int dsize;
837 register int row, col;
838 register unsigned long pixel;
839 char *data;
840
841 if ((subimage = Xcalloc (1, sizeof (XImage))) == NULL)
842 return (XImage *) NULL;
843 subimage->width = width;
844 subimage->height = height;
845 subimage->xoffset = 0;
846 subimage->format = ximage->format;
847 subimage->byte_order = ximage->byte_order;
848 subimage->bitmap_unit = ximage->bitmap_unit;
849 subimage->bitmap_bit_order = ximage->bitmap_bit_order;
850 subimage->bitmap_pad = ximage->bitmap_pad;
851 subimage->bits_per_pixel = ximage->bits_per_pixel;
852 subimage->depth = ximage->depth;
853 /*
854 * compute per line accelerator.
855 */
856 if (subimage->format == ZPixmap)
857 subimage->bytes_per_line =
858 ROUNDUP(subimage->bits_per_pixel * width,
859 subimage->bitmap_pad);
860 else
861 subimage->bytes_per_line =
862 ROUNDUP(width, subimage->bitmap_pad);
863 subimage->obdata = NULL;
864 _XInitImageFuncPtrs (subimage);
865 dsize = subimage->bytes_per_line * height;
866 if (subimage->format == XYPixmap) dsize = dsize * subimage->depth;
867 if (((data = Xcalloc (1, dsize)) == NULL) && (dsize > 0)) {
868 Xfree(subimage);
869 return (XImage *) NULL;
870 }
871 subimage->data = data;
872
873 /*
874 * Test for cases where the new subimage is larger than the region
875 * that we are copying from the existing data. In those cases,
876 * copy the area of the existing image, and allow the "uncovered"
877 * area of new subimage to remain with zero filled pixels.
878 */
879 if (height > ximage->height - y ) height = ximage->height - y;
880 if (width > ximage->width - x ) width = ximage->width - x;
881
882 for (row = y; row < (y + height); row++) {
883 for (col = x; col < (x + width); col++) {
884 pixel = XGetPixel(ximage, col, row);
885 XPutPixel(subimage, (col - x), (row - y), pixel);
886 }
887 }
888 return subimage;
889 }
890
891
892 /*
893 * SetImage
894 *
895 * Overwrites a section of one image with all of the data from another.
896 * If the two images are not of the same format (i.e. XYPixmap and ZPixmap),
897 * the image data is converted to the destination format. The following
898 * restrictions apply:
899 *
900 * 1. The depths of the source and destination images must be equal.
901 *
902 * 2. If the height of the source image is too large to fit between
903 * the specified y starting point and the bottom of the image,
904 * then scanlines are truncated on the bottom.
905 *
906 * 3. If the width of the source image is too large to fit between
907 * the specified x starting point and the end of the scanline,
908 * then pixels are truncated on the right.
909 *
910 * The images need not have the same bitmap_bit_order, byte_order,
911 * bitmap_unit, bits_per_pixel, bitmap_pad, or xoffset.
912 *
913 */
914
915 int _XSetImage(
916 XImage *srcimg,
917 XImage *dstimg,
918 int x,
919 int y)
920 {
921 register unsigned long pixel;
922 register int row, col;
923 int width, height, startrow, startcol;
924 if (x < 0) {
925 startcol = -x;
926 x = 0;
927 } else
928 startcol = 0;
929 if (y < 0) {
930 startrow = -y;
931 y = 0;
932 } else
933 startrow = 0;
934 width = dstimg->width - x;
935 if (srcimg->width < width)
936 width = srcimg->width;
937 height = dstimg->height - y;
938 if (srcimg->height < height)
939 height = srcimg->height;
940
941 /* this is slow, will do better later */
942 for (row = startrow; row < height; row++) {
943 for (col = startcol; col < width; col++) {
944 pixel = XGetPixel(srcimg, col, row);
945 XPutPixel(dstimg, x + col, y + row, pixel);
946 }
947 }
948 return 1;
949 }
950
951 /*
952 * AddPixel
953 *
954 * Adds a constant value to every pixel in a pixmap.
955 *
956 */
957
958 static int
959 _XAddPixel (
960 register XImage *ximage,
961 register long value)
962 {
963 register int x;
964 register int y;
965
966 if (!value)
967 return 0;
968 if ((ximage->bits_per_pixel | ximage->depth) == 1) {
969 /* The only value that we can add here to an XYBitmap
970 * is one. Since 1 + value = ~value for one bit wide
971 * data, we do this quickly by taking the ones complement
972 * of the entire bitmap data (offset and pad included!).
973 * Note that we don't need to be concerned with bit or
974 * byte order at all.
975 */
976 register unsigned char *dp = (unsigned char *) ximage->data;
977 x = ximage->bytes_per_line * ximage->height;
978 while (--x >= 0) {
979 *dp = ~*dp;
980 dp++;
981 }
982 } else if ((ximage->format == ZPixmap) &&
983 (ximage->bits_per_pixel == 8)) {
984 register unsigned char *dp = (unsigned char *) ximage->data;
985 x = ximage->bytes_per_line * ximage->height;
986 while (--x >= 0)
987 *dp++ += value;
988 } else if ((ximage->format == ZPixmap) &&
989 (ximage->bits_per_pixel == 16) &&
990 (*((const char *)&byteorderpixel) == ximage->byte_order)) {
991 register unsigned short *dp = (unsigned short *) ximage->data;
992 x = (ximage->bytes_per_line >> 1) * ximage->height;
993 while (--x >= 0)
994 *dp++ += value;
995 } else if ((ximage->format == ZPixmap) &&
996 (ximage->bits_per_pixel == 32) &&
997 (*((const char *)&byteorderpixel) == ximage->byte_order)) {
998 register CARD32 *dp = (CARD32 *) ximage->data;
999 x = (ximage->bytes_per_line >> 2) * ximage->height;
1000 while (--x >= 0)
1001 *dp++ += value;
1002 } else {
1003 for (y = ximage->height; --y >= 0; ) {
1004 for (x = ximage->width; --x >= 0; ) {
1005 register unsigned long pixel = XGetPixel(ximage, x, y);
1006 pixel = pixel + value;
1007 XPutPixel(ximage, x, y, pixel);
1008 }
1009 }
1010 }
1011 return 0;
1012 }
1013