1 |
/* $Header$ */ |
2 |
|
3 |
/* |
4 |
* tkWinImage.c -- |
5 |
* |
6 |
* This file contains routines for manipulation full-color images. |
7 |
* |
8 |
* Copyright (c) 1995 Sun Microsystems, Inc. |
9 |
* |
10 |
* See the file "license.terms" for information on usage and redistribution |
11 |
* of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
12 |
* |
13 |
* RCS: @(#) $Id: tkwinimage.c,v 1.1.1.1 2001/06/13 05:13:34 dtashley Exp $ |
14 |
*/ |
15 |
|
16 |
#include "tkWinInt.h" |
17 |
|
18 |
static int DestroyImage _ANSI_ARGS_((XImage* data)); |
19 |
static unsigned long ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y)); |
20 |
static int PutPixel _ANSI_ARGS_((XImage *image, int x, int y, |
21 |
unsigned long pixel)); |
22 |
|
23 |
/* |
24 |
*---------------------------------------------------------------------- |
25 |
* |
26 |
* DestroyImage -- |
27 |
* |
28 |
* This is a trivial wrapper around ckfree to make it possible to |
29 |
* pass ckfree as a pointer. |
30 |
* |
31 |
* Results: |
32 |
* None. |
33 |
* |
34 |
* Side effects: |
35 |
* Deallocates the image. |
36 |
* |
37 |
*---------------------------------------------------------------------- |
38 |
*/ |
39 |
|
40 |
static int |
41 |
DestroyImage(imagePtr) |
42 |
XImage *imagePtr; /* image to free */ |
43 |
{ |
44 |
if (imagePtr) { |
45 |
if (imagePtr->data) { |
46 |
ckfree((char*)imagePtr->data); |
47 |
} |
48 |
ckfree((char*)imagePtr); |
49 |
} |
50 |
return 0; |
51 |
} |
52 |
|
53 |
/* |
54 |
*---------------------------------------------------------------------- |
55 |
* |
56 |
* ImageGetPixel -- |
57 |
* |
58 |
* Get a single pixel from an image. |
59 |
* |
60 |
* Results: |
61 |
* Returns the 32 bit pixel value. |
62 |
* |
63 |
* Side effects: |
64 |
* None. |
65 |
* |
66 |
*---------------------------------------------------------------------- |
67 |
*/ |
68 |
|
69 |
static unsigned long |
70 |
ImageGetPixel(image, x, y) |
71 |
XImage *image; |
72 |
int x, y; |
73 |
{ |
74 |
unsigned long pixel = 0; |
75 |
unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line) |
76 |
+ ((x * image->bits_per_pixel) / NBBY)]); |
77 |
|
78 |
switch (image->bits_per_pixel) { |
79 |
case 32: |
80 |
case 24: |
81 |
pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]); |
82 |
break; |
83 |
case 16: |
84 |
pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8, |
85 |
((((WORD*)srcPtr)[0]) >> 2) & 0xf8, |
86 |
((((WORD*)srcPtr)[0]) << 3) & 0xf8); |
87 |
break; |
88 |
case 8: |
89 |
pixel = srcPtr[0]; |
90 |
break; |
91 |
case 4: |
92 |
pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f; |
93 |
break; |
94 |
case 1: |
95 |
pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0; |
96 |
break; |
97 |
} |
98 |
return pixel; |
99 |
} |
100 |
|
101 |
/* |
102 |
*---------------------------------------------------------------------- |
103 |
* |
104 |
* PutPixel -- |
105 |
* |
106 |
* Set a single pixel in an image. |
107 |
* |
108 |
* Results: |
109 |
* None. |
110 |
* |
111 |
* Side effects: |
112 |
* None. |
113 |
* |
114 |
*---------------------------------------------------------------------- |
115 |
*/ |
116 |
|
117 |
static int |
118 |
PutPixel(image, x, y, pixel) |
119 |
XImage *image; |
120 |
int x, y; |
121 |
unsigned long pixel; |
122 |
{ |
123 |
unsigned char *destPtr = &(image->data[(y * image->bytes_per_line) |
124 |
+ ((x * image->bits_per_pixel) / NBBY)]); |
125 |
|
126 |
switch (image->bits_per_pixel) { |
127 |
case 32: |
128 |
/* |
129 |
* Pixel is DWORD: 0x00BBGGRR |
130 |
*/ |
131 |
|
132 |
destPtr[3] = 0; |
133 |
case 24: |
134 |
/* |
135 |
* Pixel is triplet: 0xBBGGRR. |
136 |
*/ |
137 |
|
138 |
destPtr[0] = (unsigned char) GetBValue(pixel); |
139 |
destPtr[1] = (unsigned char) GetGValue(pixel); |
140 |
destPtr[2] = (unsigned char) GetRValue(pixel); |
141 |
break; |
142 |
case 16: |
143 |
/* |
144 |
* Pixel is WORD: 5-5-5 (R-G-B) |
145 |
*/ |
146 |
|
147 |
(*(WORD*)destPtr) = |
148 |
((GetRValue(pixel) & 0xf8) << 7) |
149 |
| ((GetGValue(pixel) & 0xf8) <<2) |
150 |
| ((GetBValue(pixel) & 0xf8) >> 3); |
151 |
break; |
152 |
case 8: |
153 |
/* |
154 |
* Pixel is 8-bit index into color table. |
155 |
*/ |
156 |
|
157 |
(*destPtr) = (unsigned char) pixel; |
158 |
break; |
159 |
case 4: |
160 |
/* |
161 |
* Pixel is 4-bit index in MSBFirst order. |
162 |
*/ |
163 |
if (x%2) { |
164 |
(*destPtr) = (unsigned char) (((*destPtr) & 0xf0) |
165 |
| (pixel & 0x0f)); |
166 |
} else { |
167 |
(*destPtr) = (unsigned char) (((*destPtr) & 0x0f) |
168 |
| ((pixel << 4) & 0xf0)); |
169 |
} |
170 |
break; |
171 |
case 1: { |
172 |
/* |
173 |
* Pixel is bit in MSBFirst order. |
174 |
*/ |
175 |
|
176 |
int mask = (0x80 >> (x%8)); |
177 |
if (pixel) { |
178 |
(*destPtr) |= mask; |
179 |
} else { |
180 |
(*destPtr) &= ~mask; |
181 |
} |
182 |
} |
183 |
break; |
184 |
} |
185 |
return 0; |
186 |
} |
187 |
|
188 |
/* |
189 |
*---------------------------------------------------------------------- |
190 |
* |
191 |
* XCreateImage -- |
192 |
* |
193 |
* Allocates storage for a new XImage. |
194 |
* |
195 |
* Results: |
196 |
* Returns a newly allocated XImage. |
197 |
* |
198 |
* Side effects: |
199 |
* None. |
200 |
* |
201 |
*---------------------------------------------------------------------- |
202 |
*/ |
203 |
|
204 |
XImage * |
205 |
XCreateImage(display, visual, depth, format, offset, data, width, height, |
206 |
bitmap_pad, bytes_per_line) |
207 |
Display* display; |
208 |
Visual* visual; |
209 |
unsigned int depth; |
210 |
int format; |
211 |
int offset; |
212 |
char* data; |
213 |
unsigned int width; |
214 |
unsigned int height; |
215 |
int bitmap_pad; |
216 |
int bytes_per_line; |
217 |
{ |
218 |
XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage)); |
219 |
imagePtr->width = width; |
220 |
imagePtr->height = height; |
221 |
imagePtr->xoffset = offset; |
222 |
imagePtr->format = format; |
223 |
imagePtr->data = data; |
224 |
imagePtr->byte_order = LSBFirst; |
225 |
imagePtr->bitmap_unit = 8; |
226 |
imagePtr->bitmap_bit_order = MSBFirst; |
227 |
imagePtr->bitmap_pad = bitmap_pad; |
228 |
imagePtr->bits_per_pixel = depth; |
229 |
imagePtr->depth = depth; |
230 |
|
231 |
/* |
232 |
* Under Windows, bitmap_pad must be on an LONG data-type boundary. |
233 |
*/ |
234 |
|
235 |
#define LONGBITS (sizeof(LONG) * 8) |
236 |
|
237 |
bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS; |
238 |
|
239 |
/* |
240 |
* Round to the nearest bitmap_pad boundary. |
241 |
*/ |
242 |
|
243 |
if (bytes_per_line) { |
244 |
imagePtr->bytes_per_line = bytes_per_line; |
245 |
} else { |
246 |
imagePtr->bytes_per_line = (((depth * width) |
247 |
+ (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1); |
248 |
} |
249 |
|
250 |
imagePtr->red_mask = 0; |
251 |
imagePtr->green_mask = 0; |
252 |
imagePtr->blue_mask = 0; |
253 |
|
254 |
imagePtr->f.put_pixel = PutPixel; |
255 |
imagePtr->f.get_pixel = ImageGetPixel; |
256 |
imagePtr->f.destroy_image = DestroyImage; |
257 |
imagePtr->f.create_image = NULL; |
258 |
imagePtr->f.sub_image = NULL; |
259 |
imagePtr->f.add_pixel = NULL; |
260 |
|
261 |
return imagePtr; |
262 |
} |
263 |
|
264 |
/* |
265 |
*---------------------------------------------------------------------- |
266 |
* |
267 |
* XGetImage -- |
268 |
* |
269 |
* This function copies data from a pixmap or window into an |
270 |
* XImage. |
271 |
* |
272 |
* Results: |
273 |
* Returns a newly allocated image containing the data from the |
274 |
* given rectangle of the given drawable. |
275 |
* |
276 |
* Side effects: |
277 |
* None. |
278 |
* |
279 |
*---------------------------------------------------------------------- |
280 |
*/ |
281 |
|
282 |
XImage * |
283 |
XGetImage(display, d, x, y, width, height, plane_mask, format) |
284 |
Display* display; |
285 |
Drawable d; |
286 |
int x; |
287 |
int y; |
288 |
unsigned int width; |
289 |
unsigned int height; |
290 |
unsigned long plane_mask; |
291 |
int format; |
292 |
{ |
293 |
TkWinDrawable *twdPtr = (TkWinDrawable *)d; |
294 |
XImage *imagePtr; |
295 |
HDC dc; |
296 |
char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)]; |
297 |
BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf; |
298 |
|
299 |
if ((twdPtr->type != TWD_BITMAP) || (twdPtr->bitmap.handle == NULL) |
300 |
|| (format != XYPixmap) || (plane_mask != 1)) { |
301 |
panic("XGetImage: not implemented"); |
302 |
} |
303 |
|
304 |
|
305 |
imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL, |
306 |
width, height, 32, 0); |
307 |
imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height); |
308 |
|
309 |
dc = GetDC(NULL); |
310 |
|
311 |
GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL, |
312 |
infoPtr, DIB_RGB_COLORS); |
313 |
|
314 |
infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); |
315 |
infoPtr->bmiHeader.biWidth = width; |
316 |
infoPtr->bmiHeader.biHeight = -(LONG)height; |
317 |
infoPtr->bmiHeader.biPlanes = 1; |
318 |
infoPtr->bmiHeader.biBitCount = 1; |
319 |
infoPtr->bmiHeader.biCompression = BI_RGB; |
320 |
infoPtr->bmiHeader.biCompression = 0; |
321 |
infoPtr->bmiHeader.biXPelsPerMeter = 0; |
322 |
infoPtr->bmiHeader.biYPelsPerMeter = 0; |
323 |
infoPtr->bmiHeader.biClrUsed = 0; |
324 |
infoPtr->bmiHeader.biClrImportant = 0; |
325 |
|
326 |
GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data, |
327 |
infoPtr, DIB_RGB_COLORS); |
328 |
ReleaseDC(NULL, dc); |
329 |
|
330 |
return imagePtr; |
331 |
} |
332 |
|
333 |
/* End of tkwinimage.c */ |