home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcltk805.zip / tcl805s.zip / tk8.0.5 / os2 / tkOS2Image.c < prev    next >
C/C++ Source or Header  |  2000-01-01  |  8KB  |  307 lines

  1. /* 
  2.  * tkOS2Image.c --
  3.  *
  4.  *    This file contains routines for manipulation full-color of images.
  5.  *
  6.  * Copyright (c) 1996-2000 Illya Vaes
  7.  * Copyright (c) 1995 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  */
  12.  
  13.  
  14. #include "tkOS2Int.h"
  15.  
  16. static int             DestroyImage _ANSI_ARGS_((XImage* data));
  17. static unsigned long   ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
  18. static int             PutPixel (XImage *image, int x, int y,
  19.                 unsigned long pixel);
  20.  
  21. /*
  22.  *----------------------------------------------------------------------
  23.  *
  24.  * DestroyImage --
  25.  *
  26.  *      This is a trivial wrapper around ckfree to make it possible to
  27.  *      pass ckfree as a pointer.
  28.  *
  29.  * Results:
  30.  *      None.
  31.  *
  32.  * Side effects:
  33.  *      Deallocates the image.
  34.  *
  35.  *----------------------------------------------------------------------
  36.  */
  37.  
  38. int
  39. DestroyImage(imagePtr)
  40.      XImage *imagePtr;          /* image to free */
  41. {
  42.     if (imagePtr) {
  43.         if (imagePtr->data) {
  44.             ckfree((char*)imagePtr->data);
  45.         }
  46.         ckfree((char*)imagePtr);
  47.     }
  48.     return 0;
  49. }
  50.  
  51. /*
  52.  *----------------------------------------------------------------------
  53.  *
  54.  * ImageGetPixel --
  55.  *
  56.  *      Get a single pixel from an image.
  57.  *
  58.  * Results:
  59.  *      Returns the 32 bit pixel value.
  60.  *
  61.  * Side effects:
  62.  *      None.
  63.  *
  64.  *----------------------------------------------------------------------
  65.  */
  66.  
  67. unsigned long
  68. ImageGetPixel(image, x, y)
  69.     XImage *image;
  70.     int x, y;
  71. {
  72.     unsigned long pixel = 0;
  73.     unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
  74.             + ((x * image->bits_per_pixel) / NBBY)]);
  75.  
  76. #ifdef VERBOSE
  77.     printf("ImageGetPixel %x (%d,%d)\n", image, x, y);
  78. #endif
  79. /*
  80.     switch (image->bits_per_pixel) {
  81.         case 32:
  82.         case 24:
  83.             pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
  84.             break;
  85.         case 16:
  86.             pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
  87.                     ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
  88.                     ((((WORD*)srcPtr)[0]) << 3) & 0xf8);
  89.             break;
  90.         case 8:
  91.             pixel = srcPtr[0];
  92.             break;
  93.         case 4:
  94.             pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
  95.             break;
  96.         case 1:
  97.             pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
  98.             break;
  99.     }
  100. */
  101.     pixel = RGB(srcPtr[0], srcPtr[1], srcPtr[2]);
  102.     return pixel;
  103. }
  104.  
  105. /*
  106.  *----------------------------------------------------------------------
  107.  *
  108.  * PutPixel --
  109.  *
  110.  *    Set a single pixel in an image.
  111.  *
  112.  * Results:
  113.  *    None.
  114.  *
  115.  * Side effects:
  116.  *    None.
  117.  *
  118.  *----------------------------------------------------------------------
  119.  */
  120.  
  121. static int
  122. PutPixel(image, x, y, pixel)
  123.     XImage *image;
  124.     int x, y;
  125.     unsigned long pixel;
  126. {
  127.     unsigned char *destPtr;
  128.  
  129. #ifdef VERBOSE
  130.     printf("PutPixel %x image %x (%d,%d) bytes_p_l %d, bits_p_p %d\n", pixel,
  131.            image, x, y, image->bytes_per_line, image->bits_per_pixel);
  132. #endif
  133.     destPtr = &(image->data[(y * image->bytes_per_line)
  134.               + ((x * image->bits_per_pixel) / NBBY)]);
  135.  
  136. /*
  137.     rc = GpiQueryLogColorTable(globalPS, 0L, pixel, 1, &pixel);
  138. */
  139.     destPtr[0] = destPtr[1] = destPtr[2] = 0;
  140.     destPtr[0] = GetRValue(pixel);
  141.     destPtr[1] = GetGValue(pixel);
  142.     destPtr[2] = GetBValue(pixel);
  143. #ifdef VERBOSE
  144.     printf(" pixel now %x, RGB (%x,%x,%x) destPtr %x\n", pixel, destPtr[0],
  145.            destPtr[1], destPtr[2], (ULONG)destPtr);
  146. #endif
  147.  
  148. /*
  149.     LONG *destPtr;
  150.  
  151.     destPtr = (LONG *) &(image->data[(y * image->bytes_per_line)
  152.               + ((x * image->bits_per_pixel) / NBBY)]);
  153.     *destPtr = pixel;
  154. #ifdef VERBOSE
  155.     printf("PutPixel %x image %x (%d,%d) bytes_p_l %d, bits_p_p %d: %x\n",
  156.            pixel, image, x, y, image->bytes_per_line, image->bits_per_pixel,
  157.            *(destPtr));
  158. #endif
  159. */
  160.  
  161.     return 0;
  162. }
  163.  
  164. /*
  165.  *----------------------------------------------------------------------
  166.  *
  167.  * XCreateImage --
  168.  *
  169.  *    Allocates storage for a new XImage.
  170.  *
  171.  * Results:
  172.  *    Returns a newly allocated XImage.
  173.  *
  174.  * Side effects:
  175.  *    None.
  176.  *
  177.  *----------------------------------------------------------------------
  178.  */
  179.  
  180. XImage *
  181. XCreateImage(display, visual, depth, format, offset, data, width, height,
  182.     bitmap_pad, bytes_per_line)
  183.     Display* display;
  184.     Visual* visual;
  185.     unsigned int depth;
  186.     int format;
  187.     int offset;
  188.     char* data;
  189.     unsigned int width;
  190.     unsigned int height;
  191.     int bitmap_pad;
  192.     int bytes_per_line;
  193. {
  194.     XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
  195.  
  196.     if (imagePtr) {
  197.         imagePtr->width = width;
  198.         imagePtr->height = height;
  199.         imagePtr->xoffset = offset;
  200.         imagePtr->format = format;
  201.         imagePtr->data = data;
  202.         imagePtr->byte_order = MSBFirst;
  203.         imagePtr->bitmap_unit = 32;
  204.         imagePtr->bitmap_bit_order = MSBFirst;
  205.         imagePtr->bitmap_pad = bitmap_pad;
  206.         imagePtr->bits_per_pixel = 24;
  207.         imagePtr->depth = depth;
  208.  
  209.         /*
  210.          * Bitmap_pad must be on 4-byte boundary.
  211.          */
  212.  
  213. #define LONGBITS    (sizeof(LONG) * 8)
  214.  
  215.         bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
  216. #ifdef VERBOSE
  217.     printf("XCreateImage bpp %d, depth %d, pad %d (was %d)\n",
  218.            imagePtr->bits_per_pixel, imagePtr->depth, imagePtr->bitmap_pad,
  219.            bitmap_pad);
  220. #endif
  221.  
  222.         /*
  223.          * Round to the nearest bitmap_pad boundary.
  224.          */
  225.  
  226.         if (bytes_per_line) {
  227.             imagePtr->bytes_per_line = bytes_per_line;
  228.         } else {
  229.             imagePtr->bytes_per_line = (((depth * width)
  230.                     + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
  231.         }
  232.  
  233.         imagePtr->red_mask = 0;
  234.         imagePtr->green_mask = 0;
  235.         imagePtr->blue_mask = 0;
  236.  
  237.         imagePtr->f.put_pixel = PutPixel;
  238.         imagePtr->f.get_pixel = ImageGetPixel;
  239.         imagePtr->f.destroy_image = DestroyImage;
  240.         imagePtr->f.create_image = NULL;
  241.         imagePtr->f.sub_image = NULL;
  242.         imagePtr->f.add_pixel = NULL;
  243.     }
  244.     
  245.     return imagePtr;
  246. }
  247.  
  248. /*
  249.  *----------------------------------------------------------------------
  250.  *
  251.  * XGetImage --
  252.  *
  253.  *      This function copies data from a pixmap or window into an
  254.  *      XImage.
  255.  *
  256.  * Results:
  257.  *      Returns a newly allocated image containing the data from the
  258.  *      given rectangle of the given drawable.
  259.  *
  260.  * Side effects:
  261.  *      None.
  262.  *
  263.  *----------------------------------------------------------------------
  264.  */
  265.  
  266. XImage *
  267. XGetImage(display, d, x, y, width, height, plane_mask, format)
  268.     Display* display;
  269.     Drawable d;
  270.     int x;
  271.     int y;
  272.     unsigned int width;
  273.     unsigned int height;
  274.     unsigned long plane_mask;
  275.     int format;
  276. {
  277.     TkOS2Drawable *todPtr = (TkOS2Drawable *)d;
  278.     XImage *imagePtr;
  279.     BITMAPINFO2 infoBuf;
  280.  
  281. #ifdef VERBOSE
  282.     printf("XGetImage\n");
  283. #endif
  284.  
  285.     if ((todPtr->type != TOD_BITMAP) || (todPtr->bitmap.handle == NULLHANDLE)
  286.             || (format != XYPixmap) || (plane_mask != 1)) {
  287.         panic("XGetImage: not implemented");
  288.     }
  289.  
  290.  
  291.     imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
  292.             width, height, 32, 0);
  293.     imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
  294.  
  295.     infoBuf.cbFix = 20;
  296.     infoBuf.cx = width;
  297.     infoBuf.cy = height;
  298.     infoBuf.cPlanes = 1;
  299.     infoBuf.cBitCount = 1;
  300.     infoBuf.ulCompression = BCA_UNCOMP;
  301.  
  302.     rc = GpiQueryBitmapBits(todPtr->bitmap.hps, 0L, (LONG)height,
  303.                             (PBYTE)imagePtr->data, &infoBuf);
  304.  
  305.     return imagePtr;
  306. }
  307.