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

  1. /* 
  2.  * tkOS2ImgUtil.c --
  3.  *
  4.  *    This file contains image related utility functions.
  5.  *
  6.  * Copyright (c) 1995 Sun Microsystems, Inc.
  7.  * Copyright (c) 1996-2000 Illya Vaes
  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.  * RCS: @(#) $Id: tkImgUtil.c,v 1.2 1998/09/14 18:23:13 stanton Exp $
  13.  */
  14.  
  15. #include "tkInt.h"
  16. #include "tkPort.h"
  17. #include "xbytes.h"
  18.  
  19.  
  20. /*
  21.  *----------------------------------------------------------------------
  22.  *
  23.  * TkAlignImageData --
  24.  *
  25.  *    This function takes an image and copies the data into an
  26.  *    aligned buffer, performing any necessary bit swapping.
  27.  *    We need to reverse the lines in OS/2 because of the inverted Y
  28.  *    coordinate system.
  29.  *
  30.  * Results:
  31.  *    Returns a newly allocated buffer that should be freed by the
  32.  *    caller.
  33.  *
  34.  * Side effects:
  35.  *    None.
  36.  *
  37.  *----------------------------------------------------------------------
  38.  */
  39.  
  40. char *
  41. TkAlignImageData(image, alignment, bitOrder)
  42.     XImage *image;        /* Image to be aligned. */
  43.     int alignment;        /* Number of bytes to which the data should
  44.                  * be aligned (e.g. 2 or 4) */
  45.     int bitOrder;        /* Desired bit order: LSBFirst or MSBFirst. */
  46. {
  47.     long dataWidth;
  48.     char *data, *srcPtr, *destPtr;
  49.     int i, j;
  50.  
  51. #ifdef VERBOSE
  52.     printf("TkAlignImageData, image->bitmap_bit_order %d, bitOrder %d\n",
  53.            image->bitmap_bit_order, bitOrder);
  54. #endif
  55.  
  56.     if (image->bits_per_pixel != 1) {
  57.     panic("TkAlignImageData: Can't handle image depths greater than 1.");
  58.     }
  59.  
  60.     /*
  61.      * Compute line width for output data buffer.
  62.      */
  63.  
  64.     dataWidth = image->bytes_per_line;
  65.     if (dataWidth % alignment) {
  66.     dataWidth += (alignment - (dataWidth % alignment));
  67.     }
  68.  
  69.     data = ckalloc(dataWidth * image->height);
  70.  
  71.     destPtr = data;
  72.     /* Reverse rows in Y direction */
  73.     for (i = image->height - 1; i >= 0; i--) {
  74.     srcPtr = &image->data[i * image->bytes_per_line];
  75.     for (j = 0; j < dataWidth; j++) {
  76.         if (j >= image->bytes_per_line) {
  77.         *destPtr = 0;
  78.         } else if (image->bitmap_bit_order != bitOrder) {
  79.         *destPtr = xBitReverseTable[(unsigned char)(*(srcPtr++))];
  80.         } else {
  81.         *destPtr = *(srcPtr++);
  82.         }
  83.         destPtr++;
  84.     }
  85.     }
  86.     return data;
  87. }
  88.  
  89. /*
  90.  *----------------------------------------------------------------------
  91.  *
  92.  * TkOS2ReverseImageLines --
  93.  *
  94.  *    This function takes an image and copies the data into an
  95.  *    aligned buffer, reversing the line order.
  96.  *    We need to reverse the lines in OS/2 because of the inverted Y
  97.  *    coordinate system.
  98.  *
  99.  * Results:
  100.  *    Returns a newly allocated buffer that should be freed by the
  101.  *    caller.
  102.  *
  103.  * Side effects:
  104.  *    None.
  105.  *
  106.  *----------------------------------------------------------------------
  107.  */
  108.  
  109. char *
  110. TkOS2ReverseImageLines(image, height)
  111.     XImage *image;        /* Image to be reversed. */
  112.     int height;        /* Height of image, image->height may not be correct */
  113. {
  114.     char *data, *srcPtr, *destPtr;
  115.     int i, j;
  116.  
  117. #ifdef VERBOSE
  118.     printf("TkOS2ReverseImageLines image %x height %d\n", image, height);
  119. #endif
  120.     data = ckalloc(image->bytes_per_line * height);
  121.  
  122.     destPtr = data;
  123.     /* Reverse rows */
  124.     for (i = height - 1; i >= 0; i--) {
  125.     srcPtr = &image->data[i * image->bytes_per_line];
  126.     for (j = 0; j < image->bytes_per_line; j++) {
  127.             *destPtr = *(srcPtr++);
  128.             destPtr++;
  129.     }
  130.     }
  131.     return data;
  132. }
  133.