home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / evbl0627.zip / everblue_20010627.zip / x11 / Xlib_ImUtil.c < prev    next >
C/C++ Source or Header  |  1999-11-02  |  32KB  |  1,065 lines

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