home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tk42r2s.zip / tk4.2 / os2 / tkOS2ImgUtil.c < prev    next >
C/C++ Source or Header  |  1999-07-26  |  3KB  |  125 lines

  1. /* 
  2.  * tkImgUtil.c --
  3.  *
  4.  *    This file contains image related utility functions.
  5.  *
  6.  * Copyright (c) 1996-1998 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.  * SCCS: @(#) tkImgUtil.c 1.3 96/02/15 18:53:12
  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.     if (image->bits_per_pixel != 1) {
  52.     panic("TkAlignImageData: Can't handle image depths greater than 1.");
  53.     }
  54.  
  55.     /*
  56.      * Compute line width for output data buffer.
  57.      */
  58.  
  59.     dataWidth = image->bytes_per_line;
  60.     if (dataWidth % alignment) {
  61.     dataWidth += (alignment - (dataWidth % alignment));
  62.     }
  63.  
  64.     data = ckalloc(dataWidth * image->height);
  65.  
  66.     destPtr = data;
  67.     /* Reverse rows in Y direction */
  68.     for (i = image->height - 1; i >= 0; i--) {
  69.     srcPtr = &image->data[i * image->bytes_per_line];
  70.     for (j = 0; j < dataWidth; j++) {
  71.         if (j >= image->bytes_per_line) {
  72.         *destPtr = 0;
  73.         } else if (image->bitmap_bit_order != bitOrder) {
  74.         *destPtr = xBitReverseTable[(unsigned char)(*(srcPtr++))];
  75.         } else {
  76.         *destPtr = *(srcPtr++);
  77.         }
  78.         destPtr++;
  79.     }
  80.     }
  81.     return data;
  82. }
  83.  
  84. /*
  85.  *----------------------------------------------------------------------
  86.  *
  87.  * TkOS2ReverseImageLines --
  88.  *
  89.  *    This function takes an image and copies the data into an
  90.  *    aligned buffer, reversing the line order.
  91.  *    We need to reverse the lines in OS/2 because of the inverted Y
  92.  *    coordinate system.
  93.  *
  94.  * Results:
  95.  *    Returns a newly allocated buffer that should be freed by the
  96.  *    caller.
  97.  *
  98.  * Side effects:
  99.  *    None.
  100.  *
  101.  *----------------------------------------------------------------------
  102.  */
  103.  
  104. char *
  105. TkOS2ReverseImageLines(image, height)
  106.     XImage *image;        /* Image to be reversed. */
  107.     int height;        /* Height of image, image->height may not be correct */
  108. {
  109.     char *data, *srcPtr, *destPtr;
  110.     int i, j;
  111.  
  112.     data = ckalloc(image->bytes_per_line * height);
  113.  
  114.     destPtr = data;
  115.     /* Reverse rows */
  116.     for (i = height - 1; i >= 0; i--) {
  117.     srcPtr = &image->data[i * image->bytes_per_line];
  118.     for (j = 0; j < image->bytes_per_line; j++) {
  119.             *destPtr = *(srcPtr++);
  120.             destPtr++;
  121.     }
  122.     }
  123.     return data;
  124. }
  125.