home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tkisrc04.zip / tk / os2 / tkOS2Image.c < prev    next >
C/C++ Source or Header  |  1998-08-07  |  3KB  |  136 lines

  1. /* 
  2.  * tkOS2Image.c --
  3.  *
  4.  *    This file contains routines for manipulation full-color images.
  5.  *
  6.  * Copyright (c) 1996-1997 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        PutPixel (XImage *image, int x, int y,
  17.                 unsigned long pixel);
  18.  
  19. /*
  20.  *----------------------------------------------------------------------
  21.  *
  22.  * PutPixel --
  23.  *
  24.  *    Set a single pixel in an image.
  25.  *
  26.  * Results:
  27.  *    None.
  28.  *
  29.  * Side effects:
  30.  *    None.
  31.  *
  32.  *----------------------------------------------------------------------
  33.  */
  34.  
  35. static int
  36. PutPixel(image, x, y, pixel)
  37.     XImage *image;
  38.     int x, y;
  39.     unsigned long pixel;
  40. {
  41.     char *destPtr;
  42.  
  43.     destPtr = &(image->data[(y * image->bytes_per_line)
  44.         + (x * (image->bits_per_pixel >> 3))]);
  45.  
  46. #ifdef DEBUG
  47.     printf("PutPixel %x", pixel);
  48. #endif
  49.     destPtr[0] = GetRValue(pixel);
  50.     destPtr[1] = GetGValue(pixel);
  51.     destPtr[2] = GetBValue(pixel);
  52. #ifdef DEBUG
  53.     printf(" (R%x,G%x,B%x)\n", destPtr[0], destPtr[1], destPtr[2]);
  54. #endif
  55.     return 0;
  56. }
  57.  
  58. /*
  59.  *----------------------------------------------------------------------
  60.  *
  61.  * XCreateImage --
  62.  *
  63.  *    Allocates storage for a new XImage.
  64.  *
  65.  * Results:
  66.  *    Returns a newly allocated XImage.
  67.  *
  68.  * Side effects:
  69.  *    None.
  70.  *
  71.  *----------------------------------------------------------------------
  72.  */
  73.  
  74. XImage *
  75. XCreateImage(display, visual, depth, format, offset, data, width, height,
  76.     bitmap_pad, bytes_per_line)
  77.     Display* display;
  78.     Visual* visual;
  79.     unsigned int depth;
  80.     int format;
  81.     int offset;
  82.     char* data;
  83.     unsigned int width;
  84.     unsigned int height;
  85.     int bitmap_pad;
  86.     int bytes_per_line;
  87. {
  88.     XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
  89.  
  90. #ifdef DEBUG
  91.     printf("XCreateImage\n");
  92. #endif
  93.     if (imagePtr) {
  94.         imagePtr->width = width;
  95.         imagePtr->height = height;
  96.         imagePtr->xoffset = offset;
  97.         imagePtr->format = format;
  98.         imagePtr->data = data;
  99.         imagePtr->byte_order = MSBFirst;
  100.         imagePtr->bitmap_unit = 32;
  101.         imagePtr->bitmap_bit_order = MSBFirst;
  102.         imagePtr->bitmap_pad = bitmap_pad;
  103.         imagePtr->depth = depth;
  104.  
  105.         /*
  106.          * Round to the nearest word boundary.
  107.          */
  108.     
  109.         imagePtr->bytes_per_line = bytes_per_line ? bytes_per_line
  110.          : ((depth * width + 31) >> 3) & ~3;
  111.     
  112.         /*
  113.          * If the screen supports TrueColor, then we use 3 bytes per
  114.          * pixel, and we have to install our own pixel routine.
  115.          */
  116.     
  117.         if (visual->class == TrueColor) {
  118.         imagePtr->bits_per_pixel = 24;
  119.         imagePtr->f.put_pixel = PutPixel;
  120.         } else {
  121.         imagePtr->bits_per_pixel = 8;
  122.         imagePtr->f.put_pixel = NULL;
  123.         }
  124.         imagePtr->red_mask = visual->red_mask;
  125.         imagePtr->green_mask = visual->green_mask;
  126.         imagePtr->blue_mask = visual->blue_mask;
  127.         imagePtr->f.create_image = NULL;
  128.         imagePtr->f.destroy_image = NULL;
  129.         imagePtr->f.get_pixel = NULL;
  130.         imagePtr->f.sub_image = NULL;
  131.         imagePtr->f.add_pixel = NULL;
  132.     }
  133.     
  134.     return imagePtr;
  135. }
  136.