1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /* GdkPixbuf library - convert X drawable information to RGB
4 * Copyright (C) 1999 Michael Zucchi
6 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
7 * Cody Russell <bratsche@dfw.net>
8 * Federico Mena-Quintero <federico@gimp.org>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
29 #include "gdk-pixbuf.h"
31 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
38 static guint32 mask_table[] = {
39 0x00000000, 0x00000001, 0x00000003, 0x00000007,
40 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
41 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
42 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
43 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
44 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
45 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
46 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
53 convert 1 bits-pixel data
57 rgb1 (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
65 guint8 *srow = image->mem, *orow = pixels;
67 d (printf ("1 bits/pixel\n"));
69 /* convert upto 8 pixels/time */
70 /* its probably not worth trying to make this run very fast, who uses
71 1 bit displays anymore? */
73 height = image->height;
76 for (yy = 0; yy < height; yy++) {
80 for (xx = 0; xx < width; xx ++) {
81 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
82 *o++ = colormap->colors[data].red;
83 *o++ = colormap->colors[data].green;
84 *o++ = colormap->colors[data].blue;
92 convert 1 bits/pixel data
96 rgb1a (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
102 register guint8 data;
104 guint8 *srow = image->mem, *orow = pixels;
107 d (printf ("1 bits/pixel\n"));
109 /* convert upto 8 pixels/time */
110 /* its probably not worth trying to make this run very fast, who uses
111 1 bit displays anymore? */
112 width = image->width;
113 height = image->height;
116 for (xx = 0; xx < 2; xx++) {
118 remap[xx] = 0xff000000
119 | colormap->colors[xx].blue << 16
120 | colormap->colors[xx].green << 8
121 | colormap->colors[xx].red;
124 | colormap->colors[xx].red << 24
125 | colormap->colors[xx].green << 16
126 | colormap->colors[xx].blue << 8;
130 for (yy = 0; yy < height; yy++) {
134 for (xx = 0; xx < width; xx ++) {
135 data = srow[xx >> 3] >> (7 - (xx & 7)) & 1;
144 convert 8 bits/pixel data
148 rgb8 (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
154 register guint32 data;
155 guint8 *srow = image->mem, *orow = pixels;
159 width = image->width;
160 height = image->height;
163 d (printf ("8 bit, no alpha output\n"));
165 mask = mask_table[image->depth];
167 for (yy = 0; yy < height; yy++) {
170 for (xx = 0; xx < width; xx++) {
172 *o++ = colormap->colors[data].red;
173 *o++ = colormap->colors[data].green;
174 *o++ = colormap->colors[data].blue;
182 convert 8 bits/pixel data
186 rgb8a (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
192 register guint32 data;
194 register guint8 *s; /* read 2 pixels at once */
196 guint8 *srow = image->mem, *orow = pixels;
198 width = image->width;
199 height = image->height;
202 d (printf ("8 bit, with alpha output\n"));
204 mask = mask_table[image->depth];
206 for (xx = 0; xx < colormap->size; xx++) {
208 remap[xx] = 0xff000000
209 | colormap->colors[xx].blue << 16
210 | colormap->colors[xx].green << 8
211 | colormap->colors[xx].red;
214 | colormap->colors[xx].red << 24
215 | colormap->colors[xx].green << 16
216 | colormap->colors[xx].blue << 8;
220 for (yy = 0; yy < height; yy++) {
222 o = (guint32 *) orow;
223 for (xx = 0; xx < width; xx ++) {
233 convert 16 bits/pixel data
238 rgb565lsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
245 register guint32 *s; /* read 2 pixels at once */
247 register guint8 *s; /* read 2 pixels at once */
250 guint8 *srow = image->mem, *orow = pixels;
252 width = image->width;
253 height = image->height;
256 for (yy = 0; yy < height; yy++) {
258 s = (guint32 *) srow;
260 s = (guint32 *) srow;
262 o = (guint16 *) orow;
263 for (xx = 1; xx < width; xx += 2) {
264 register guint32 data;
267 *o++ = (data & 0xf800) >> 8 | (data & 0x7e0) << 5;
268 *o++ = (data & 0x1f) << 3 | (data & 0xf8000000) >> 16;
269 *o++ = ((data & 0x7e00000) >> 19) | (data & 0x1f0000) >> 5;
271 /* swap endianness first */
272 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
274 *o++ = (data & 0xf800) | (data & 0x7e0) >> 3;
275 *o++ = (data & 0x1f) << 11 | (data & 0xf8000000) >> 24;
276 *o++ = ((data & 0x7e00000) >> 11) | (data & 0x1f0000) >> 13;
279 /* check for last remaining pixel */
281 register guint16 data;
283 data = *((short *) s);
285 data = *((short *) s);
286 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
288 ((char *) o)[0] = (data >> 8) & 0xf8;
289 ((char *) o)[1] = (data >> 3) & 0xfc;
290 ((char *) o)[2] = (data << 3) & 0xf8;
298 convert 16 bits/pixel data
303 rgb565msb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
310 register guint8 *s; /* need to swap data order */
312 register guint32 *s; /* read 2 pixels at once */
315 guint8 *srow = image->mem, *orow = pixels;
317 width = image->width;
318 height = image->height;
321 for (yy = 0; yy < height; yy++) {
325 s = (guint32 *) srow;
327 o = (guint16 *) orow;
328 for (xx = 1; xx < width; xx += 2) {
329 register guint32 data;
331 /* swap endianness first */
332 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
334 *o++ = (data & 0xf800) >> 8 | (data & 0x7e0) << 5;
335 *o++ = (data & 0x1f) << 3 | (data & 0xf8000000) >> 16;
336 *o++ = ((data & 0x7e00000) >> 19) | (data & 0x1f0000) >> 5;
339 *o++ = (data & 0xf800) | (data & 0x7e0) >> 3;
340 *o++ = (data & 0x1f) << 11 | (data & 0xf8000000) >> 24;
341 *o++ = ((data & 0x7e00000) >> 11) | (data & 0x1f0000) >> 13;
344 /* check for last remaining pixel */
346 register guint16 data;
348 data = *((short *) s);
349 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
351 data = *((short *) s);
353 ((char *) o)[0] = (data >> 8) & 0xf8;
354 ((char *) o)[1] = (data >> 3) & 0xfc;
355 ((char *) o)[2] = (data << 3) & 0xf8;
363 convert 16 bits/pixel data
368 rgb565alsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
375 register guint16 *s; /* read 1 pixels at once */
381 guint8 *srow = image->mem, *orow = pixels;
383 width = image->width;
384 height = image->height;
387 for (yy = 0; yy < height; yy++) {
389 s = (guint16 *) srow;
393 o = (guint32 *) orow;
394 for (xx = 0; xx < width; xx ++) {
395 register guint32 data;
396 /* rrrrrggg gggbbbbb -> rrrrr000 gggggg00 bbbbb000 aaaaaaaa */
397 /* little endian: aaaaaaaa bbbbb000 gggggg00 rrrrr000 */
400 *o++ = (data & 0xf800) >> 8 | (data & 0x7e0) << 5
401 | (data & 0x1f) << 19 | 0xff000000;
403 /* swap endianness first */
404 data = s[0] | s[1] << 8;
406 *o++ = (data & 0xf800) << 16 | (data & 0x7e0) << 13
407 | (data & 0x1f) << 11 | 0xff;
416 convert 16 bits/pixel data
421 rgb565amsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
430 register guint16 *s; /* read 1 pixels at once */
434 guint8 *srow = image->mem, *orow = pixels;
436 width = image->width;
437 height = image->height;
440 for (yy = 0; yy < height; yy++) {
442 o = (guint32 *) orow;
443 for (xx = 0; xx < width; xx ++) {
444 register guint32 data;
445 /* rrrrrggg gggbbbbb -> rrrrr000 gggggg00 bbbbb000 aaaaaaaa */
446 /* little endian: aaaaaaaa bbbbb000 gggggg00 rrrrr000 */
448 /* swap endianness first */
449 data = s[0] | s[1] << 8;
451 *o++ = (data & 0xf800) >> 8 | (data & 0x7e0) << 5
452 | (data & 0x1f) << 19 | 0xff000000;
455 *o++ = (data & 0xf800) << 16 | (data & 0x7e0) << 13
456 | (data & 0x1f) << 11 | 0xff;
465 convert 15 bits/pixel data
470 rgb555lsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
477 register guint32 *s; /* read 2 pixels at once */
479 register guint8 *s; /* read 2 pixels at once */
482 guint8 *srow = image->mem, *orow = pixels;
484 width = image->width;
485 height = image->height;
488 for (yy = 0; yy < height; yy++) {
490 s = (guint32 *) srow;
494 o = (guint16 *) orow;
495 for (xx = 1; xx < width; xx += 2) {
496 register guint32 data;
499 *o++ = (data & 0x7c00) >> 7 | (data & 0x3e0) << 6;
500 *o++ = (data & 0x1f) << 3 | (data & 0x7c000000) >> 15;
501 *o++ = ((data & 0x3e00000) >> 18) | (data & 0x1f0000) >> 5;
503 /* swap endianness first */
504 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
506 *o++ = (data & 0x7c00) << 1 | (data & 0x3e0) >> 2;
507 *o++ = (data & 0x1f) << 11 | (data & 0x7c000000) >> 23;
508 *o++ = ((data & 0x3e00000) >> 10) | (data & 0x1f0000) >> 13;
511 /* check for last remaining pixel */
513 register guint16 data;
515 data = *((short *) s);
517 data = *((short *) s);
518 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
520 ((char *) o)[0] = (data & 0x7c0) >> 7;
521 ((char *) o)[1] = (data & 0x3e0) >> 2;
522 ((char *) o)[2] = (data & 0x1f) << 3;
530 convert 15 bits/pixel data
535 rgb555msb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
542 register guint8 *s; /* read 2 pixels at once */
544 register guint32 *s; /* read 2 pixels at once */
547 guint8 *srow = image->mem, *orow = pixels;
549 width = image->width;
550 height = image->height;
553 for (yy = 0; yy < height; yy++) {
555 o = (guint16 *) orow;
556 for (xx = 1; xx < width; xx += 2) {
557 register guint32 data;
559 /* swap endianness first */
560 data = s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
562 *o++ = (data & 0x7c00) >> 7 | (data & 0x3e0) << 6;
563 *o++ = (data & 0x1f) << 3 | (data & 0x7c000000) >> 15;
564 *o++ = ((data & 0x3e00000) >> 18) | (data & 0x1f0000) >> 5;
567 *o++ = (data & 0x7c00) << 1 | (data & 0x3e0) >> 2;
568 *o++ = (data & 0x1f) << 11 | (data & 0x7c000000) >> 23;
569 *o++ = ((data & 0x3e00000) >> 10) | (data & 0x1f0000) >> 13;
572 /* check for last remaining pixel */
574 register guint16 data;
576 data = *((short *) s);
577 data = ((data >> 8) & 0xff) | ((data & 0xff) << 8);
579 data = *((short *) s);
581 ((char *) o)[0] = (data & 0x7c0) >> 7;
582 ((char *) o)[1] = (data & 0x3e0) >> 2;
583 ((char *) o)[2] = (data & 0x1f) << 3;
591 convert 15 bits/pixel data
596 rgb555alsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
603 register guint16 *s; /* read 1 pixels at once */
609 guint8 *srow = image->mem, *orow = pixels;
611 width = image->width;
612 height = image->height;
615 for (yy = 0; yy < height; yy++) {
617 s = (guint16 *) srow;
621 o = (guint32 *) orow;
622 for (xx = 0; xx < width; xx++) {
623 register guint32 data;
624 /* rrrrrggg gggbbbbb -> rrrrr000 gggggg00 bbbbb000 aaaaaaaa */
625 /* little endian: aaaaaaaa bbbbb000 gggggg00 rrrrr000 */
628 *o++ = (data & 0x7c00) >> 7 | (data & 0x3e0) << 6
629 | (data & 0x1f) << 19 | 0xff000000;
631 /* swap endianness first */
632 data = s[0] | s[1] << 8;
634 *o++ = (data & 0x7c00) << 17 | (data & 0x3e0) << 14
635 | (data & 0x1f) << 11 | 0xff;
644 convert 15 bits/pixel data
649 rgb555amsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
656 register guint16 *s; /* read 1 pixels at once */
662 guint8 *srow = image->mem, *orow = pixels;
664 width = image->width;
665 height = image->height;
668 for (yy = 0; yy < height; yy++) {
670 s = (guint16 *) srow;
674 o = (guint32 *) orow;
675 for (xx = 0; xx < width; xx++) {
676 register guint32 data;
677 /* rrrrrggg gggbbbbb -> rrrrr000 gggggg00 bbbbb000 aaaaaaaa */
678 /* little endian: aaaaaaaa bbbbb000 gggggg00 rrrrr000 */
680 /* swap endianness first */
681 data = s[0] | s[1] << 8;
683 *o++ = (data & 0x7c00) >>7 | (data & 0x3e0) << 6
684 | (data & 0x1f) << 19 | 0xff000000;
687 *o++ = (data & 0x7c00) << 17 | (data & 0x3e0) << 14
688 | (data & 0x1f) << 11 | 0xff;
698 rgb888alsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
704 guint8 *s; /* for byte order swapping */
706 guint8 *srow = image->mem, *orow = pixels;
708 width = image->width;
709 height = image->height;
712 d (printf ("32 bits/pixel with alpha\n"));
715 for (yy = 0; yy < height; yy++) {
718 for (xx = 0; xx < width; xx++) {
731 rgb888lsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
737 guint8 *srow = image->mem, *orow = pixels;
740 width = image->width;
741 height = image->height;
744 d (printf ("32 bit, lsb, no alpha\n"));
746 for (yy = 0; yy < height; yy++) {
749 for (xx = 0; xx < width; xx++) {
761 rgb888amsb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
767 guint8 *srow = image->mem, *orow = pixels;
772 guint8 *s; /* for byte order swapping */
776 d (printf ("32 bit, msb, with alpha\n"));
778 width = image->width;
779 height = image->height;
783 for (yy = 0; yy < height; yy++) {
785 s = (guint32 *) srow;
786 o = (guint32 *) orow;
791 for (xx = 0; xx < width; xx++) {
799 *o++ = (*s << 8) | 0xff; /* untested */
809 rgb888msb (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *colormap)
815 guint8 *srow = image->mem, *orow = pixels;
819 d (printf ("32 bit, msb, no alpha\n"));
821 width = image->width;
822 height = image->height;
825 for (yy = 0; yy < height; yy++) {
828 for (xx = 0; xx < width; xx++) {
840 This should work correctly with any display/any endianness, but will probably
844 convert_real_slow (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *cmap, int alpha)
849 guint8 *srow = image->mem, *orow = pixels;
855 width = image->width;
856 height = image->height;
858 v = gdk_colormap_get_visual(cmap);
860 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
861 v->red_mask, v->green_mask, v->blue_mask,
862 v->red_shift, v->green_shift, v->blue_shift,
863 v->red_prec, v->green_prec, v->blue_prec));
865 for (yy = 0; yy < height; yy++) {
868 for (xx = 0; xx < width; xx++) {
869 pixel = gdk_image_get_pixel(image, xx, yy);
871 /* I assume this is right for static & greyscale's too? */
872 case GDK_VISUAL_STATIC_GRAY:
873 case GDK_VISUAL_GRAYSCALE:
874 case GDK_VISUAL_STATIC_COLOR:
875 case GDK_VISUAL_PSEUDO_COLOR:
876 *o++ = cmap->colors[pixel].red;
877 *o++ = cmap->colors[pixel].green;
878 *o++ = cmap->colors[pixel].blue;
880 case GDK_VISUAL_TRUE_COLOR:
881 /* this is odd because it must sometimes shift left (otherwise
882 i'd just shift >> *_shift - 8 + *_prec), so this logic
883 should work for all bit sizes/shifts/etc */
884 *o++ = ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24;
885 *o++ = ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24;
886 *o++ = ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24;
888 case GDK_VISUAL_DIRECT_COLOR:
889 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red;
890 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green;
891 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue;
902 typedef void (* cfunc) (GdkImage *image, art_u8 *pixels, int rowstride, GdkColormap *cmap);
904 static cfunc convert_map[] = {
905 rgb1,rgb1,rgb1a,rgb1a,
906 rgb8,rgb8,rgb8a,rgb8a,
907 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
908 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
909 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
913 perform actual conversion
915 If we can, try and use the optimised code versions, but as a default
916 fallback, and always for direct colour, use the generic/slow but complete
920 rgbconvert (GdkImage *image, art_u8 *pixels, int rowstride, int alpha, GdkColormap *cmap)
922 int index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
923 int bank=5; /* default fallback converter */
924 GdkVisual *v = gdk_colormap_get_visual(cmap);
926 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
927 d(printf("image depth = %d, bpp = %d\n", image->depth, image->bpp));
930 /* I assume this is right for static & greyscale's too? */
931 case GDK_VISUAL_STATIC_GRAY:
932 case GDK_VISUAL_GRAYSCALE:
933 case GDK_VISUAL_STATIC_COLOR:
934 case GDK_VISUAL_PSEUDO_COLOR:
935 switch (image->bpp) {
944 case GDK_VISUAL_TRUE_COLOR:
945 switch (image->depth) {
947 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
952 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
958 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
964 case GDK_VISUAL_DIRECT_COLOR:
965 /* always use the slow version */
969 d(printf("converting using conversion function in bank %d\n", bank));
972 convert_real_slow(image, pixels, rowstride, cmap, alpha);
975 (* convert_map[index]) (image, pixels, rowstride, cmap);
980 /* Exported functions */
983 * gdk_pixbuf_get_from_drawable:
984 * @dest: Destination pixbuf, or NULL if a new pixbuf should be created.
985 * @src: Source drawable.
986 * @cmap: A colormap if @src is a pixmap. If it is a window, this argument will
988 * @src_x: Source X coordinate within drawable.
989 * @src_y: Source Y coordinate within drawable.
990 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
991 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
992 * @width: Width in pixels of region to get.
993 * @height: Height in pixels of region to get.
995 * Transfers image data from a Gdk drawable and converts it to an RGB(A)
996 * representation inside a GdkPixbuf.
998 * If the drawable @src is a pixmap, then a suitable colormap must be specified,
999 * since pixmaps are just blocks of pixel data without an associated colormap.
1000 * If the drawable is a window, the @cmap argument will be ignored and the
1001 * window's own colormap will be used instead.
1003 * If the specified destination pixbuf @dest is #NULL, then this function will
1004 * create an RGB pixbuf with 8 bits per channel and no alpha, with the same size
1005 * specified by the @width and @height arguments. In this case, the @dest_x and
1006 * @dest_y arguments must be specified as 0, otherwise the function will return
1007 * #NULL. If the specified destination pixbuf is not NULL and it contains alpha
1008 * information, then the filled pixels will be set to full opacity.
1010 * If the specified drawable is a pixmap, then the requested source rectangle
1011 * must be completely contained within the pixmap, otherwise the function will
1014 * If the specified drawable is a window, then it must be viewable, i.e. all of
1015 * its ancestors up to the root window must be mapped. Also, the specified
1016 * source rectangle must be completely contained within the window and within
1017 * the screen. If regions of the window are obscured by noninferior windows, the
1018 * contents of those regions are undefined. The contents of regions obscured by
1019 * inferior windows of a different depth than that of the source window will also
1022 * Return value: The same pixbuf as @dest if it was non-NULL, or a newly-created
1023 * pixbuf with a reference count of 1 if no destination pixbuf was specified.
1026 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1027 GdkDrawable *src, GdkColormap *cmap,
1028 int src_x, int src_y,
1029 int dest_x, int dest_y,
1030 int width, int height)
1032 GdkWindowType window_type;
1033 int src_width, src_height;
1034 ArtPixBuf *apb = NULL;
1036 int rowstride, bpp, alpha;
1038 /* General sanity checks */
1040 g_return_val_if_fail (src != NULL, NULL);
1042 window_type = gdk_window_get_type (src);
1044 if (window_type == GDK_WINDOW_PIXMAP)
1045 g_return_val_if_fail (cmap != NULL, NULL);
1047 /* FIXME: this is not perfect, since is_viewable() only tests
1048 * recursively up the Gdk parent window tree, but stops at
1049 * foreign windows or Gdk toplevels. I.e. if a window manager
1050 * unmapped one of its own windows, this won't work.
1052 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1055 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1057 apb = dest->art_pixbuf;
1059 g_return_val_if_fail (apb->format == ART_PIX_RGB, NULL);
1060 g_return_val_if_fail (apb->n_channels == 3 || apb->n_channels == 4, NULL);
1061 g_return_val_if_fail (apb->bits_per_sample == 8, NULL);
1064 /* Coordinate sanity checks */
1066 gdk_window_get_size (src, &src_width, &src_height);
1068 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1069 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1072 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1073 g_return_val_if_fail (dest_x + width <= apb->width, NULL);
1074 g_return_val_if_fail (dest_y + height <= apb->height, NULL);
1077 if (window_type != GDK_WINDOW_PIXMAP) {
1079 int src_xorigin, src_yorigin;
1080 int screen_width, screen_height;
1081 int screen_srcx, screen_srcy;
1083 ret = gdk_window_get_origin (src, &src_xorigin, &src_yorigin);
1084 g_return_val_if_fail (ret != FALSE, NULL);
1086 screen_width = gdk_screen_width ();
1087 screen_height = gdk_screen_height ();
1089 screen_srcx = src_xorigin + src_x;
1090 screen_srcy = src_yorigin + src_y;
1092 g_return_val_if_fail (screen_srcx >= 0 && screen_srcy >= 0, NULL);
1093 g_return_val_if_fail (screen_srcx + width <= screen_width, NULL);
1094 g_return_val_if_fail (screen_srcy + height <= screen_height, NULL);
1097 /* Get Image in ZPixmap format (packed bits). */
1098 image = gdk_image_get (src, src_x, src_y, width, height);
1099 g_return_val_if_fail( image != NULL, NULL);
1101 /* Create the pixbuf if needed */
1103 dest = gdk_pixbuf_new (ART_PIX_RGB, FALSE, 8, width, height);
1105 gdk_image_destroy(image);
1109 apb = dest->art_pixbuf;
1112 /* Get the colormap if needed */
1113 if (window_type != GDK_WINDOW_PIXMAP)
1114 cmap = gdk_window_get_colormap (src);
1116 alpha = gdk_pixbuf_get_has_alpha(dest);
1117 rowstride = gdk_pixbuf_get_rowstride(dest);
1120 /* we offset into the image data based on the position we are retrieving from */
1121 rgbconvert(image, gdk_pixbuf_get_pixels(dest) +
1122 (dest_y * rowstride) + (dest_x * bpp),
1127 gdk_image_destroy(image);