Debian Codegraph @

pTk/mTk/win/tkWinImage.c

raw
1 /*
2 * tkWinImage.c --
3 *
4 * This file contains routines for manipulation full-color images.
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 * RCS: @(#) $Id: tkWinImage.c,v 1.6.2.2 2003/10/29 01:08:02 hobbs Exp $
12 */
13
14 #include "tkWinInt.h"
15
16 static int DestroyImage _ANSI_ARGS_((XImage* data));
17 static unsigned long ImageGetPixel _ANSI_ARGS_((XImage *image, int x, int y));
18 static int PutPixel _ANSI_ARGS_((XImage *image, int x, int y,
19 unsigned long pixel));
20
21 /*
22 *----------------------------------------------------------------------
23 *
24 * DestroyImage --
25 *
26 * This is a trivial wrapper around ckfree to make it possible to
27 * pass ckfree as a pointer.
28 *
29 * Results:
30 * None.
31 *
32 * Side effects:
33 * Deallocates the image.
34 *
35 *----------------------------------------------------------------------
36 */
37
38 static int
39 DestroyImage(imagePtr)
40 XImage *imagePtr; /* image to free */
41 {
42 if (imagePtr) {
43 if (imagePtr->data) {
44 ckfree((char*)imagePtr->data);
45 }
46 ckfree((char*)imagePtr);
47 }
48 return 0;
49 }
50
51 /*
52 *----------------------------------------------------------------------
53 *
54 * ImageGetPixel --
55 *
56 * Get a single pixel from an image.
57 *
58 * Results:
59 * Returns the 32 bit pixel value.
60 *
61 * Side effects:
62 * None.
63 *
64 *----------------------------------------------------------------------
65 */
66
67 static unsigned long
68 ImageGetPixel(image, x, y)
69 XImage *image;
70 int x, y;
71 {
72 unsigned long pixel = 0;
73 unsigned char *srcPtr = &(image->data[(y * image->bytes_per_line)
74 + ((x * image->bits_per_pixel) / NBBY)]);
75
76 switch (image->bits_per_pixel) {
77 case 32:
78 case 24:
79 pixel = RGB(srcPtr[2], srcPtr[1], srcPtr[0]);
80 break;
81 case 16:
82 pixel = RGB(((((WORD*)srcPtr)[0]) >> 7) & 0xf8,
83 ((((WORD*)srcPtr)[0]) >> 2) & 0xf8,
84 ((((WORD*)srcPtr)[0]) << 3) & 0xf8);
85 break;
86 case 8:
87 pixel = srcPtr[0];
88 break;
89 case 4:
90 pixel = ((x%2) ? (*srcPtr) : ((*srcPtr) >> 4)) & 0x0f;
91 break;
92 case 1:
93 pixel = ((*srcPtr) & (0x80 >> (x%8))) ? 1 : 0;
94 break;
95 }
96 return pixel;
97 }
98
99 /*
100 *----------------------------------------------------------------------
101 *
102 * PutPixel --
103 *
104 * Set a single pixel in an image.
105 *
106 * Results:
107 * None.
108 *
109 * Side effects:
110 * None.
111 *
112 *----------------------------------------------------------------------
113 */
114
115 static int
116 PutPixel(image, x, y, pixel)
117 XImage *image;
118 int x, y;
119 unsigned long pixel;
120 {
121 unsigned char *destPtr = &(image->data[(y * image->bytes_per_line)
122 + ((x * image->bits_per_pixel) / NBBY)]);
123
124 switch (image->bits_per_pixel) {
125 case 32:
126 /*
127 * Pixel is DWORD: 0x00BBGGRR
128 */
129
130 destPtr[3] = 0;
131 case 24:
132 /*
133 * Pixel is triplet: 0xBBGGRR.
134 */
135
136 destPtr[0] = (unsigned char) GetBValue(pixel);
137 destPtr[1] = (unsigned char) GetGValue(pixel);
138 destPtr[2] = (unsigned char) GetRValue(pixel);
139 break;
140 case 16:
141 /*
142 * Pixel is WORD: 5-5-5 (R-G-B)
143 */
144
145 (*(WORD*)destPtr) =
146 ((GetRValue(pixel) & 0xf8) << 7)
147 | ((GetGValue(pixel) & 0xf8) <<2)
148 | ((GetBValue(pixel) & 0xf8) >> 3);
149 break;
150 case 8:
151 /*
152 * Pixel is 8-bit index into color table.
153 */
154
155 (*destPtr) = (unsigned char) pixel;
156 break;
157 case 4:
158 /*
159 * Pixel is 4-bit index in MSBFirst order.
160 */
161 if (x%2) {
162 (*destPtr) = (unsigned char) (((*destPtr) & 0xf0)
163 | (pixel & 0x0f));
164 } else {
165 (*destPtr) = (unsigned char) (((*destPtr) & 0x0f)
166 | ((pixel << 4) & 0xf0));
167 }
168 break;
169 case 1: {
170 /*
171 * Pixel is bit in MSBFirst order.
172 */
173
174 int mask = (0x80 >> (x%8));
175 if (pixel) {
176 (*destPtr) |= mask;
177 } else {
178 (*destPtr) &= ~mask;
179 }
180 }
181 break;
182 }
183 return 0;
184 }
185
186
187 static int
188 ImgDestroy(image)
189 XImage *image;
190 {
191 if (image) {
192 if (image->data)
193 ckfree(image->data);
194 ckfree((char *) image);
195 }
196 return 0;
197 }
198
199 /*
200 *----------------------------------------------------------------------
201 *
202 * XCreateImage --
203 *
204 * Allocates storage for a new XImage.
205 *
206 * Results:
207 * Returns a newly allocated XImage.
208 *
209 * Side effects:
210 * None.
211 *
212 *----------------------------------------------------------------------
213 */
214
215 XImage *
216 XCreateImage(display, visual, depth, format, offset, data, width, height,
217 bitmap_pad, bytes_per_line)
218 Display* display;
219 Visual* visual;
220 unsigned int depth;
221 int format;
222 int offset;
223 char* data;
224 unsigned int width;
225 unsigned int height;
226 int bitmap_pad;
227 int bytes_per_line;
228 {
229 XImage* imagePtr = (XImage *) ckalloc(sizeof(XImage));
230 imagePtr->width = width;
231 imagePtr->height = height;
232 imagePtr->xoffset = offset;
233 imagePtr->format = format;
234 imagePtr->data = data;
235 imagePtr->byte_order = LSBFirst;
236 imagePtr->bitmap_unit = 8;
237 imagePtr->bitmap_bit_order = LSBFirst;
238 imagePtr->bitmap_pad = bitmap_pad;
239 imagePtr->bits_per_pixel = depth;
240 imagePtr->depth = depth;
241
242 /*
243 * Under Windows, bitmap_pad must be on an LONG data-type boundary.
244 */
245
246 #define LONGBITS (sizeof(LONG) * 8)
247
248 bitmap_pad = (bitmap_pad + LONGBITS - 1) / LONGBITS * LONGBITS;
249
250 /*
251 * Round to the nearest bitmap_pad boundary.
252 */
253
254 if (bytes_per_line) {
255 imagePtr->bytes_per_line = bytes_per_line;
256 } else {
257 imagePtr->bytes_per_line = (((depth * width)
258 + (bitmap_pad - 1)) >> 3) & ~((bitmap_pad >> 3) - 1);
259 }
260
261 imagePtr->red_mask = 0;
262 imagePtr->green_mask = 0;
263 imagePtr->blue_mask = 0;
264
265 imagePtr->f.put_pixel = PutPixel;
266 imagePtr->f.get_pixel = ImageGetPixel;
267 imagePtr->f.destroy_image = DestroyImage;
268 imagePtr->f.create_image = NULL;
269 imagePtr->f.sub_image = NULL;
270 imagePtr->f.add_pixel = NULL;
271
272 return imagePtr;
273 }
274
275 /*
276 *----------------------------------------------------------------------
277 * XGetImageZPixmap --
278 *
279 * This function copies data from a pixmap or window into an
280 * XImage. This handles the ZPixmap case only.
281 *
282 * Results:
283 * Returns a newly allocated image containing the data from the
284 * given rectangle of the given drawable.
285 *
286 * Side effects:
287 * None.
288 *
289 * This procedure is adapted from the XGetImage implementation in TkNT.
290 * That code is Copyright (c) 1994 Software Research Associates, Inc.
291 *
292 *----------------------------------------------------------------------
293 */
294
295 static XImage *
296 XGetImageZPixmap(display, d, x, y, width, height, plane_mask, format)
297 Display* display;
298 Drawable d;
299 int x;
300 int y;
301 unsigned int width;
302 unsigned int height;
303 unsigned long plane_mask;
304 int format;
305 {
306 TkWinDrawable *twdPtr = (TkWinDrawable *)d;
307 XImage *ret_image;
308 HDC hdc, hdcMem;
309 HBITMAP hbmp, hbmpPrev;
310 BITMAPINFO *bmInfo = NULL;
311 HPALETTE hPal, hPalPrev1, hPalPrev2;
312 int size;
313 unsigned int n;
314 unsigned int depth;
315 unsigned char *data;
316 TkWinDCState state;
317 BOOL ret;
318
319 if (format != ZPixmap) {
320 TkpDisplayWarning(
321 "XGetImageZPixmap: only ZPixmap types are implemented",
322 "XGetImageZPixmap Failure");
323 return NULL;
324 }
325
326 hdc = TkWinGetDrawableDC(display, d, &state);
327
328 /* Need to do a Blt operation to copy into a new bitmap */
329 hbmp = CreateCompatibleBitmap(hdc, width, height);
330 hdcMem = CreateCompatibleDC(hdc);
331 hbmpPrev = SelectObject(hdcMem, hbmp);
332 hPal = state.palette;
333 if (hPal) {
334 hPalPrev1 = SelectPalette(hdcMem, hPal, FALSE);
335 n = RealizePalette(hdcMem);
336 if (n > 0) {
337 UpdateColors (hdcMem);
338 }
339 hPalPrev2 = SelectPalette(hdc, hPal, FALSE);
340 n = RealizePalette(hdc);
341 if (n > 0) {
342 UpdateColors (hdc);
343 }
344 }
345
346 ret = BitBlt(hdcMem, 0, 0, width, height, hdc, x, y, SRCCOPY);
347 if (hPal) {
348 SelectPalette(hdc, hPalPrev2, FALSE);
349 }
350 SelectObject(hdcMem, hbmpPrev);
351 TkWinReleaseDrawableDC(d, hdc, &state);
352 if (ret == FALSE) {
353 goto cleanup;
354 }
355 if (twdPtr->type == TWD_WINDOW) {
356 depth = Tk_Depth((Tk_Window) twdPtr->window.winPtr);
357 } else {
358 depth = twdPtr->bitmap.depth;
359 }
360
361 size = sizeof(BITMAPINFO);
362 if (depth <= 8) {
363 size += sizeof(unsigned short) * (1 << depth);
364 }
365 bmInfo = (BITMAPINFO *) ckalloc(size);
366
367 bmInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
368 bmInfo->bmiHeader.biWidth = width;
369 bmInfo->bmiHeader.biHeight = -(int) height;
370 bmInfo->bmiHeader.biPlanes = 1;
371 bmInfo->bmiHeader.biBitCount = depth;
372 bmInfo->bmiHeader.biCompression = BI_RGB;
373 bmInfo->bmiHeader.biSizeImage = 0;
374 bmInfo->bmiHeader.biXPelsPerMeter = 0;
375 bmInfo->bmiHeader.biYPelsPerMeter = 0;
376 bmInfo->bmiHeader.biClrUsed = 0;
377 bmInfo->bmiHeader.biClrImportant = 0;
378
379 if (depth == 1) {
380 unsigned char *p, *pend;
381 GetDIBits(hdcMem, hbmp, 0, height, NULL, bmInfo, DIB_PAL_COLORS);
382 data = ckalloc(bmInfo->bmiHeader.biSizeImage);
383 if (!data) {
384 /* printf("Failed to allocate data area for XImage.\n"); */
385 ret_image = NULL;
386 goto cleanup;
387 }
388 ret_image = XCreateImage(display, NULL, depth, ZPixmap, 0, data,
389 width, height, 32, ((width + 31) >> 3) & ~1);
390 if (ret_image == NULL) {
391 ckfree(data);
392 goto cleanup;
393 }
394
395 /* Get the BITMAP info into the Image. */
396 if (GetDIBits(hdcMem, hbmp, 0, height, data, bmInfo,
397 DIB_PAL_COLORS) == 0) {
398 ckfree((char *) ret_image->data);
399 ckfree((char *) ret_image);
400 ret_image = NULL;
401 goto cleanup;
402 }
403 p = data;
404 pend = data + bmInfo->bmiHeader.biSizeImage;
405 while (p < pend) {
406 *p = ~*p;
407 p++;
408 }
409 } else if (depth == 8) {
410 unsigned short *palette;
411 unsigned int i;
412 unsigned char *p;
413
414 GetDIBits(hdcMem, hbmp, 0, height, NULL, bmInfo, DIB_PAL_COLORS);
415 data = ckalloc(bmInfo->bmiHeader.biSizeImage);
416 if (!data) {
417 /* printf("Failed to allocate data area for XImage.\n"); */
418 ret_image = NULL;
419 goto cleanup;
420 }
421 ret_image = XCreateImage(display, NULL, 8, ZPixmap, 0, data,
422 width, height, 8, width);
423 if (ret_image == NULL) {
424 ckfree((char *) data);
425 goto cleanup;
426 }
427
428 /* Get the BITMAP info into the Image. */
429 if (GetDIBits(hdcMem, hbmp, 0, height, data, bmInfo,
430 DIB_PAL_COLORS) == 0) {
431 ckfree((char *) ret_image->data);
432 ckfree((char *) ret_image);
433 ret_image = NULL;
434 goto cleanup;
435 }
436 p = data;
437 palette = (unsigned short *) bmInfo->bmiColors;
438 for (i = 0; i < bmInfo->bmiHeader.biSizeImage; i++, p++) {
439 *p = (unsigned char) palette[*p];
440 }
441 } else if (depth == 16) {
442 GetDIBits(hdcMem, hbmp, 0, height, NULL, bmInfo, DIB_RGB_COLORS);
443 data = ckalloc(bmInfo->bmiHeader.biSizeImage);
444 if (!data) {
445 /* printf("Failed to allocate data area for XImage.\n"); */
446 ret_image = NULL;
447 goto cleanup;
448 }
449 ret_image = XCreateImage(display, NULL, 16, ZPixmap, 0, data,
450 width, height, 16, 0 /* will be calc'ed from bitmap_pad */);
451 if (ret_image == NULL) {
452 ckfree((char *) data);
453 goto cleanup;
454 }
455
456 /* Get the BITMAP info directly into the Image. */
457 if (GetDIBits(hdcMem, hbmp, 0, height, ret_image->data, bmInfo,
458 DIB_RGB_COLORS) == 0) {
459 ckfree((char *) ret_image->data);
460 ckfree((char *) ret_image);
461 ret_image = NULL;
462 goto cleanup;
463 }
464 } else {
465 GetDIBits(hdcMem, hbmp, 0, height, NULL, bmInfo, DIB_RGB_COLORS);
466 data = ckalloc(width * height * 4);
467 if (!data) {
468 /* printf("Failed to allocate data area for XImage.\n"); */
469 ret_image = NULL;
470 goto cleanup;
471 }
472 ret_image = XCreateImage(display, NULL, 32, ZPixmap, 0, data,
473 width, height, 0, width * 4);
474 if (ret_image == NULL) {
475 ckfree((char *) data);
476 goto cleanup;
477 }
478
479 if (depth <= 24) {
480 /*
481 * This used to handle 16 and 24 bpp, but now just handles 24.
482 * It can likely be optimized for that. -- hobbs
483 */
484 unsigned char *smallBitData, *smallBitBase, *bigBitData;
485 unsigned int byte_width, h, w;
486
487 byte_width = ((width * 3 + 3) & ~3);
488 smallBitBase = ckalloc(byte_width * height);
489 if (!smallBitBase) {
490 ckfree((char *) ret_image->data);
491 ckfree((char *) ret_image);
492 ret_image = NULL;
493 goto cleanup;
494 }
495 smallBitData = smallBitBase;
496
497 /* Get the BITMAP info into the Image. */
498 if (GetDIBits(hdcMem, hbmp, 0, height, smallBitData, bmInfo,
499 DIB_RGB_COLORS) == 0) {
500 ckfree((char *) ret_image->data);
501 ckfree((char *) ret_image);
502 ckfree((char *) smallBitBase);
503 ret_image = NULL;
504 goto cleanup;
505 }
506 /* Copy the 24 Bit Pixmap to a 32-Bit one. */
507 for (h = 0; h < height; h++) {
508 bigBitData = ret_image->data + h * ret_image->bytes_per_line;
509 smallBitData = smallBitBase + h * byte_width;
510
511 for (w = 0; w < width; w++) {
512 *bigBitData++ = ((*smallBitData++));
513 *bigBitData++ = ((*smallBitData++));
514 *bigBitData++ = ((*smallBitData++));
515 *bigBitData++ = 0;
516 }
517 }
518 /* Free the Device contexts, and the Bitmap */
519 ckfree((char *) smallBitBase);
520 } else {
521 /* Get the BITMAP info directly into the Image. */
522 if (GetDIBits(hdcMem, hbmp, 0, height, ret_image->data, bmInfo,
523 DIB_RGB_COLORS) == 0) {
524 ckfree((char *) ret_image->data);
525 ckfree((char *) ret_image);
526 ret_image = NULL;
527 goto cleanup;
528 }
529 }
530 }
531
532 cleanup:
533 if (bmInfo) {
534 ckfree((char *) bmInfo);
535 }
536 if (hPal) {
537 SelectPalette(hdcMem, hPalPrev1, FALSE);
538 }
539 DeleteDC(hdcMem);
540 DeleteObject(hbmp);
541
542 return ret_image;
543 }
544
545 /*
546 *----------------------------------------------------------------------
547 *
548 * XGetImage --
549 *
550 * This function copies data from a pixmap or window into an
551 * XImage.
552 *
553 * Results:
554 * Returns a newly allocated image containing the data from the
555 * given rectangle of the given drawable.
556 *
557 * Side effects:
558 * None.
559 *
560 *----------------------------------------------------------------------
561 */
562
563 XImage *
564 XGetImage(display, d, x, y, width, height, plane_mask, format)
565 Display* display;
566 Drawable d;
567 int x;
568 int y;
569 unsigned int width;
570 unsigned int height;
571 unsigned long plane_mask;
572 int format;
573 {
574 TkWinDrawable *twdPtr = (TkWinDrawable *)d;
575 XImage *imagePtr;
576 HDC dc;
577
578 display->request++;
579
580 if (twdPtr == NULL) {
581 /*
582 * Avoid unmapped windows or bad drawables
583 */
584 return NULL;
585 }
586
587 if (twdPtr->type != TWD_BITMAP) {
588 /*
589 * This handles TWD_WINDOW or TWD_WINDC, always creating a 32bit
590 * image. If the window being copied isn't visible (unmapped or
591 * obscured), we quietly stop copying (no user error).
592 * The user will see black where the widget should be.
593 * This branch is likely followed in favor of XGetImageZPixmap as
594 * postscript printed widgets require RGB data.
595 */
596 TkWinDCState state;
597 unsigned int xx, yy, size;
598 COLORREF pixel;
599
600 dc = TkWinGetDrawableDC(display, d, &state);
601
602 imagePtr = XCreateImage(display, NULL, 32,
603 format, 0, NULL, width, height, 32, 0);
604 size = imagePtr->bytes_per_line * imagePtr->height;
605 imagePtr->data = ckalloc(size);
606 ZeroMemory(imagePtr->data, size);
607
608 for (yy = 0; yy < height; yy++) {
609 for (xx = 0; xx < width; xx++) {
610 pixel = GetPixel(dc, x+(int)xx, y+(int)yy);
611 if (pixel == CLR_INVALID) {
612 break;
613 }
614 PutPixel(imagePtr, xx, yy, pixel);
615 }
616 }
617
618 TkWinReleaseDrawableDC(d, dc, &state);
619 } else if (format == ZPixmap) {
620 /*
621 * This actually handles most TWD_WINDOW requests, but it varies
622 * from the above in that it really does a screen capture of
623 * an area, which is consistent with the Unix behavior, but does
624 * not appear to handle all bit depths correctly. -- hobbs
625 */
626 imagePtr = XGetImageZPixmap(display, d, x, y,
627 width, height, plane_mask, format);
628 } else {
629 char *errMsg = NULL;
630 char infoBuf[sizeof(BITMAPINFO) + sizeof(RGBQUAD)];
631 BITMAPINFO *infoPtr = (BITMAPINFO*)infoBuf;
632
633 if (twdPtr->bitmap.handle == NULL) {
634 errMsg = "XGetImage: not implemented for empty bitmap handles";
635 } else if (format != XYPixmap) {
636 errMsg = "XGetImage: not implemented for format != XYPixmap";
637 } else if (plane_mask != 1) {
638 errMsg = "XGetImage: not implemented for plane_mask != 1";
639 }
640 if (errMsg != NULL) {
641 /*
642 * Do a soft warning for the unsupported XGetImage types.
643 */
644 TkpDisplayWarning(errMsg, "XGetImage Failure");
645 return NULL;
646 }
647
648 imagePtr = XCreateImage(display, NULL, 1, XYBitmap, 0, NULL,
649 width, height, 32, 0);
650 imagePtr->data = ckalloc(imagePtr->bytes_per_line * imagePtr->height);
651
652 dc = GetDC(NULL);
653
654 GetDIBits(dc, twdPtr->bitmap.handle, 0, height, NULL,
655 infoPtr, DIB_RGB_COLORS);
656
657 infoPtr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
658 infoPtr->bmiHeader.biWidth = width;
659 infoPtr->bmiHeader.biHeight = -(LONG)height;
660 infoPtr->bmiHeader.biPlanes = 1;
661 infoPtr->bmiHeader.biBitCount = 1;
662 infoPtr->bmiHeader.biCompression = BI_RGB;
663 infoPtr->bmiHeader.biSizeImage = 0;
664 infoPtr->bmiHeader.biXPelsPerMeter = 0;
665 infoPtr->bmiHeader.biYPelsPerMeter = 0;
666 infoPtr->bmiHeader.biClrUsed = 0;
667 infoPtr->bmiHeader.biClrImportant = 0;
668
669 GetDIBits(dc, twdPtr->bitmap.handle, 0, height, imagePtr->data,
670 infoPtr, DIB_RGB_COLORS);
671 ReleaseDC(NULL, dc);
672 }
673
674 return imagePtr;
675 }
676