Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/lib/libtk/generic/tkImgUtil.c
1810 views
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
9
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
*
11
* SCCS: @(#) tkImgUtil.c 1.3 96/02/15 18:53:12
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
24
* aligned buffer, performing any necessary bit swapping.
25
*
26
* Results:
27
* Returns a newly allocated buffer that should be freed by the
28
* caller.
29
*
30
* Side effects:
31
* None.
32
*
33
*----------------------------------------------------------------------
34
*/
35
36
char *
37
TkAlignImageData(image, alignment, bitOrder)
38
XImage *image; /* Image to be aligned. */
39
int alignment; /* Number of bytes to which the data should
40
* be aligned (e.g. 2 or 4) */
41
int bitOrder; /* Desired bit order: LSBFirst or MSBFirst. */
42
{
43
long dataWidth;
44
char *data, *srcPtr, *destPtr;
45
int i, j;
46
47
if (image->bits_per_pixel != 1) {
48
panic("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(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