/*1* tkImgUtil.c --2*3* This file contains image related utility functions.4*5* Copyright (c) 1995 Sun Microsystems, Inc.6*7* See the file "license.terms" for information on usage and redistribution8* of this file, and for a DISCLAIMER OF ALL WARRANTIES.9*10* SCCS: @(#) tkImgUtil.c 1.3 96/02/15 18:53:1211*/1213#include "tkInt.h"14#include "xbytes.h"151617/*18*----------------------------------------------------------------------19*20* TkAlignImageData --21*22* This function takes an image and copies the data into an23* aligned buffer, performing any necessary bit swapping.24*25* Results:26* Returns a newly allocated buffer that should be freed by the27* caller.28*29* Side effects:30* None.31*32*----------------------------------------------------------------------33*/3435char *36TkAlignImageData(image, alignment, bitOrder)37XImage *image; /* Image to be aligned. */38int alignment; /* Number of bytes to which the data should39* be aligned (e.g. 2 or 4) */40int bitOrder; /* Desired bit order: LSBFirst or MSBFirst. */41{42long dataWidth;43char *data, *srcPtr, *destPtr;44int i, j;4546if (image->bits_per_pixel != 1) {47panic("TkAlignImageData: Can't handle image depths greater than 1.");48}4950/*51* Compute line width for output data buffer.52*/5354dataWidth = image->bytes_per_line;55if (dataWidth % alignment) {56dataWidth += (alignment - (dataWidth % alignment));57}5859data = ckalloc(dataWidth * image->height);6061destPtr = data;62for (i = 0; i < image->height; i++) {63srcPtr = &image->data[i * image->bytes_per_line];64for (j = 0; j < dataWidth; j++) {65if (j >= image->bytes_per_line) {66*destPtr = 0;67} else if (image->bitmap_bit_order != bitOrder) {68*destPtr = xBitReverseTable[(unsigned char)(*(srcPtr++))];69} else {70*destPtr = *(srcPtr++);71}72destPtr++;73}74}75return data;76}777879