1/*
2 * tkImgUtil.c --
3 *
4 *	This file contains image related utility functions.
5 *
6 * Copyright (c) 1995 Sun Microsystems, Inc.
7 *
8 * See the file "license.terms" for information on usage and redistribution of
9 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
10 *
11 * RCS: @(#) $Id$
12 */
13
14#include "tkInt.h"
15#include "xbytes.h"
16
17
18/*
19 *----------------------------------------------------------------------
20 *
21 * TkAlignImageData --
22 *
23 *	This function takes an image and copies the data into an aligned
24 *	buffer, performing any necessary bit swapping.
25 *
26 * Results:
27 *	Returns a newly allocated buffer that should be freed by the caller.
28 *
29 * Side effects:
30 *	None.
31 *
32 *----------------------------------------------------------------------
33 */
34
35char *
36TkAlignImageData(
37    XImage *image,		/* Image to be aligned. */
38    int alignment,		/* Number of bytes to which the data should be
39				 * aligned (e.g. 2 or 4) */
40    int bitOrder)		/* Desired bit order: LSBFirst or MSBFirst. */
41{
42    long dataWidth;
43    char *data, *srcPtr, *destPtr;
44    int i, j;
45
46    if (image->bits_per_pixel != 1) {
47	Tcl_Panic(
48		"TkAlignImageData: Can't handle image depths greater than 1.");
49    }
50
51    /*
52     * Compute line width for output data buffer.
53     */
54
55    dataWidth = image->bytes_per_line;
56    if (dataWidth % alignment) {
57	dataWidth += (alignment - (dataWidth % alignment));
58    }
59
60    data = ckalloc((unsigned) dataWidth * image->height);
61
62    destPtr = data;
63    for (i = 0; i < image->height; i++) {
64	srcPtr = &image->data[i * image->bytes_per_line];
65	for (j = 0; j < dataWidth; j++) {
66	    if (j >= image->bytes_per_line) {
67		*destPtr = 0;
68	    } else if (image->bitmap_bit_order != bitOrder) {
69		*destPtr = xBitReverseTable[(unsigned char)(*(srcPtr++))];
70	    } else {
71		*destPtr = *(srcPtr++);
72	    }
73	    destPtr++;
74	}
75    }
76    return data;
77}
78
79/*
80 * Local Variables:
81 * mode: c
82 * c-basic-offset: 4
83 * fill-column: 78
84 * End:
85 */
86