1 /* GdkPixbuf library - convert X drawable information to RGB
3 * Copyright (C) 1999 Michael Zucchi
5 * Authors: Michael Zucchi <zucchi@zedzone.mmc.com.au>
6 * Cody Russell <bratsche@dfw.net>
7 * Federico Mena-Quintero <federico@gimp.org>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
30 #include "gdkvisual.h"
31 #include "gdkwindow.h"
32 #include "gdkpixbuf.h"
33 #include "gdkpixmap.h"
34 #include "gdk-pixbuf-private.h"
35 #include "gdkinternals.h"
37 /* Some convenient names
39 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
48 #define SWAP16(d) GUINT16_SWAP_LE_BE(d)
52 static guint32 mask_table[] = {
53 0x00000000, 0x00000001, 0x00000003, 0x00000007,
54 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
55 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
56 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
57 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
58 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
59 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
60 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
67 * convert bitmap data to pixbuf without alpha,
68 * without using a colormap
71 bitmap1 (GdkImage *image,
83 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
85 d (printf ("bitmap, no alpha\n"));
89 for (yy = y1; yy < y2; yy++)
93 for (xx = x1; xx < x2; xx ++)
95 /* top 29 bits of xx (xx >> 3) indicate the byte the bit is inside,
96 * bottom 3 bits (xx & 7) indicate bit inside that byte,
97 * we don't bother to canonicalize data to 1 or 0, just
98 * leave the relevant bit in-place.
100 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
123 * convert bitmap data to pixbuf with alpha,
124 * without using a colormap
127 bitmap1a (GdkImage *image,
137 register guint8 data;
139 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
141 d (printf ("bitmap, with alpha\n"));
145 for (yy = y1; yy < y2; yy++)
149 for (xx = x1; xx < x2; xx ++)
151 /* see comment in bitmap1() */
152 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
177 * convert 1 bits-pixel data
181 rgb1 (GdkImage *image,
188 GdkColormap *colormap)
192 register guint8 data;
194 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
196 d (printf ("1 bits/pixel\n"));
198 /* convert upto 8 pixels/time */
199 /* its probably not worth trying to make this run very fast, who uses
200 * 1 bit displays anymore?
204 for (yy = y1; yy < y2; yy++)
208 for (xx = x1; xx < x2; xx ++)
210 /* see comment in bitmap1() */
211 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
215 *o++ = colormap->colors[data].red >> 8;
216 *o++ = colormap->colors[data].green >> 8;
217 *o++ = colormap->colors[data].blue >> 8;
225 * convert 1 bits/pixel data
229 rgb1a (GdkImage *image,
236 GdkColormap *colormap)
240 register guint8 data;
242 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
244 d (printf ("1 bits/pixel\n"));
246 /* convert upto 8 pixels/time */
247 /* its probably not worth trying to make this run very fast, who uses
248 * 1 bit displays anymore? */
251 for (yy = y1; yy < y2; yy++)
255 for (xx = x1; xx < x2; xx ++)
257 /* see comment in bitmap1() */
258 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
262 *o++ = colormap->colors[data].red >> 8;
263 *o++ = colormap->colors[data].green >> 8;
264 *o++ = colormap->colors[data].blue >> 8;
273 * convert 8 bits/pixel data
277 rgb8 (GdkImage *image,
284 GdkColormap *colormap)
289 register guint32 data;
290 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
296 d (printf ("8 bit, no alpha output\n"));
298 mask = mask_table[image->depth];
300 for (yy = y1; yy < y2; yy++)
304 for (xx = x1; xx < x2; xx++)
307 *o++ = colormap->colors[data].red >> 8;
308 *o++ = colormap->colors[data].green >> 8;
309 *o++ = colormap->colors[data].blue >> 8;
317 * convert 8 bits/pixel data
321 rgb8a (GdkImage *image,
328 GdkColormap *colormap)
333 register guint32 data;
335 register guint8 *s; /* read 2 pixels at once */
337 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
341 d (printf ("8 bit, with alpha output\n"));
343 mask = mask_table[image->depth];
345 for (xx = x1; xx < colormap->size; xx++)
348 remap[xx] = 0xff000000
349 | (colormap->colors[xx].blue & 0xff00) << 8
350 | (colormap->colors[xx].green & 0xff00)
351 | (colormap->colors[xx].red >> 8);
354 | (colormap->colors[xx].red & 0xff00) << 16
355 | (colormap->colors[xx].green & 0xff00) << 8
356 | (colormap->colors[xx].blue & 0xff00);
360 for (yy = y1; yy < y2; yy++)
363 o = (guint32 *) orow;
364 for (xx = x1; xx < x2; xx ++)
374 /* Bit shifting for 565 and 555 conversion routines
376 * RGB565 == rrrr rggg gggb bbbb, 16 bit native endian
377 * RGB555 == xrrr rrgg gggb bbbb
378 * ABGR8888: ARGB, 32-bit native endian
379 * RGBA8888: RGBA, 32-bit native endian
381 #define R8fromRGB565(d) ((((d) >> 8) & 0xf8) | (((d) >> 13) & 0x7))
382 #define B8fromRGB565(d) ((((d) >> 3) & 0xfc) | (((d) >> 9) & 0x3))
383 #define G8fromRGB565(d) ((((d) << 3) & 0xf8) | (((d) >> 2) & 0x7))
385 #define ABGR8888fromRGB565(d) ( ((d) & 0xf800) >> 8 | ((d) & 0xe000) >> 13 \
386 | ((d) & 0x07e0) << 5 | ((d) & 0x0600) >> 1 \
387 | ((d) & 0x001f) << 19 | ((d) & 0x001c) << 14 \
389 #define RGBA8888fromRGB565(d) ( ((d) & 0xf800) << 16 | ((d) & 0xe000) << 11 \
390 | ((d) & 0x07e0) << 13 | ((d) & 0x0600) << 7 \
391 | ((d) & 0x001f) << 11 | ((d) & 0x001c) << 6 \
394 #define R8fromRGB555(d) (((d) & 0x7c00) >> 7 | ((d) & 0x7000) >> 12)
395 #define B8fromRGB555(d) (((d) & 0x03e0) >> 2 | ((d) & 0x0380) >> 7)
396 #define G8fromRGB555(d) (((d) & 0x001f) << 3 | ((d) & 0x001c) >> 2)
398 #define ABGR8888fromRGB555(d) ( ((d) & 0x7c00) >> 7 | ((d) & 0x7000) >> 12 \
399 | ((d) & 0x03e0) << 6 | ((d) & 0x0380) << 1 \
400 | ((d) & 0x001f) << 19 | ((d) & 0x001c) << 14 \
402 #define RGBA8888fromRGB555(d) ( ((d) & 0x7c00) << 17 | ((d) & 0x7000) << 12 \
403 | ((d) & 0x03e0) << 14 | ((d) & 0x0380) << 9 \
404 | ((d) & 0x001f) << 11 | ((d) & 0x001c) << 6 \
408 * convert 16 bits/pixel data
413 rgb565lsb (GdkImage *image,
420 GdkColormap *colormap)
428 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
432 for (yy = y1; yy < y2; yy++)
434 s = (guint16 *) srow;
436 for (xx = x1; xx < x2; xx ++)
438 register guint32 data = *s++;
440 data = SWAP16 (data);
442 *o++ = R8fromRGB565 (data);
443 *o++ = G8fromRGB565 (data);
444 *o++ = B8fromRGB565 (data);
452 * convert 16 bits/pixel data
457 rgb565msb (GdkImage *image,
464 GdkColormap *colormap)
472 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
476 for (yy = y1; yy < y2; yy++)
478 s = (guint16 *) srow;
480 for (xx = x1; xx < x2; xx ++)
482 register guint32 data = *s++;
484 data = SWAP16 (data);
486 *o++ = R8fromRGB565 (data);
487 *o++ = G8fromRGB565 (data);
488 *o++ = B8fromRGB565 (data);
496 * convert 16 bits/pixel data
501 rgb565alsb (GdkImage *image,
508 GdkColormap *colormap)
516 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
520 for (yy = y1; yy < y2; yy++)
522 s = (guint16 *) srow;
523 o = (guint32 *) orow;
524 for (xx = x1; xx < x2; xx ++)
526 register guint32 data = *s++;
528 *o++ = ABGR8888fromRGB565 (data);
530 data = SWAP16 (data);
531 *o++ = RGBA8888fromRGB565 (data);
540 * convert 16 bits/pixel data
545 rgb565amsb (GdkImage *image,
552 GdkColormap *colormap)
560 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
564 for (yy = y1; yy < y2; yy++)
566 s = (guint16 *) srow;
567 o = (guint32 *) orow;
568 for (xx = x1; xx < x2; xx ++)
570 register guint32 data = *s++;
572 data = SWAP16 (data);
573 *o++ = ABGR8888fromRGB565 (data);
575 *o++ = RGBA8888fromRGB565 (data);
584 * convert 15 bits/pixel data
589 rgb555lsb (GdkImage *image,
596 GdkColormap *colormap)
604 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
608 for (yy = y1; yy < y2; yy++)
610 s = (guint16 *) srow;
612 for (xx = x1; xx < x2; xx ++)
614 register guint32 data = *s++;
616 data = SWAP16 (data);
618 *o++ = R8fromRGB555 (data);
619 *o++ = G8fromRGB555 (data);
620 *o++ = B8fromRGB555 (data);
628 * convert 15 bits/pixel data
633 rgb555msb (GdkImage *image,
640 GdkColormap *colormap)
648 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
652 for (yy = y1; yy < y2; yy++)
654 s = (guint16 *) srow;
656 for (xx = x1; xx < x2; xx ++)
658 register guint32 data = *s++;
660 data = SWAP16 (data);
662 *o++ = R8fromRGB555 (data);
663 *o++ = G8fromRGB555 (data);
664 *o++ = B8fromRGB555 (data);
672 * convert 15 bits/pixel data
677 rgb555alsb (GdkImage *image,
684 GdkColormap *colormap)
689 register guint16 *s; /* read 1 pixels at once */
692 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
696 for (yy = y1; yy < y2; yy++)
698 s = (guint16 *) srow;
699 o = (guint32 *) orow;
700 for (xx = x1; xx < x2; xx++)
702 register guint32 data = *s++;
704 *o++ = ABGR8888fromRGB555 (data);
706 data = SWAP16 (data);
707 *o++ = RGBA8888fromRGB555 (data);
716 * convert 15 bits/pixel data
721 rgb555amsb (GdkImage *image,
728 GdkColormap *colormap)
736 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
740 for (yy = y1; yy < y2; yy++)
742 s = (guint16 *) srow;
743 o = (guint32 *) orow;
744 for (xx = x1; xx < x2; xx++)
746 register guint32 data = *s++;
748 data = SWAP16 (data);
749 *o++ = ABGR8888fromRGB555 (data);
751 *o++ = RGBA8888fromRGB555 (data);
761 rgb888alsb (GdkImage *image,
768 GdkColormap *colormap)
773 guint8 *s; /* for byte order swapping */
775 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
779 d (printf ("32 bits/pixel with alpha\n"));
782 for (yy = y1; yy < y2; yy++)
786 for (xx = x1; xx < x2; xx++)
800 rgb888lsb (GdkImage *image,
807 GdkColormap *colormap)
812 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
817 d (printf ("32 bit, lsb, no alpha\n"));
819 for (yy = y1; yy < y2; yy++)
823 for (xx = x1; xx < x2; xx++)
836 rgb888amsb (GdkImage *image,
843 GdkColormap *colormap)
848 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
852 d (printf ("32 bit, msb, with alpha\n"));
857 for (yy = y1; yy < y2; yy++)
859 s = (guint32 *) srow;
860 o = (guint32 *) orow;
861 for (xx = x1; xx < x2; xx++)
864 *o++ = (*s++ >> 8) | 0xff000000;
866 *o++ = (*s++ << 8) | 0xff;
875 rgb888msb (GdkImage *image,
882 GdkColormap *colormap)
887 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
891 d (printf ("32 bit, msb, no alpha\n"));
895 for (yy = y1; yy < y2; yy++)
899 for (xx = x1; xx < x2; xx++)
912 * This should work correctly with any display/any endianness, but will probably
916 convert_real_slow (GdkImage *image,
928 guint8 *orow = pixels;
936 v = gdk_colormap_get_visual(cmap);
938 if (image->depth != v->depth)
940 g_warning ("%s: The depth of the source image (%d) doesn't "
941 "match the depth of the colormap passed in (%d).",
942 G_STRLOC, image->depth, v->depth);
946 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
947 v->red_mask, v->green_mask, v->blue_mask,
948 v->red_shift, v->green_shift, v->blue_shift,
949 v->red_prec, v->green_prec, v->blue_prec));
951 for (yy = y1; yy < y2; yy++)
954 for (xx = x1; xx < x2; xx++)
956 pixel = gdk_image_get_pixel(image, xx, yy);
959 /* I assume this is right for static & greyscale's too? */
960 case GDK_VISUAL_STATIC_GRAY:
961 case GDK_VISUAL_GRAYSCALE:
962 case GDK_VISUAL_STATIC_COLOR:
963 case GDK_VISUAL_PSEUDO_COLOR:
964 *o++ = cmap->colors[pixel].red >> 8;
965 *o++ = cmap->colors[pixel].green >> 8;
966 *o++ = cmap->colors[pixel].blue >> 8;
968 case GDK_VISUAL_TRUE_COLOR:
969 /* This is odd because it must sometimes shift left (otherwise
970 * I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
971 * should work for all bit sizes/shifts/etc.
974 for (i = 24; i < 32; i += v->red_prec)
975 component |= ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> i;
978 for (i = 24; i < 32; i += v->green_prec)
979 component |= ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> i;
982 for (i = 24; i < 32; i += v->blue_prec)
983 component |= ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> i;
986 case GDK_VISUAL_DIRECT_COLOR:
987 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red >> 8;
988 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green >> 8;
989 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue >> 8;
999 typedef void (* cfunc) (GdkImage *image,
1008 static cfunc convert_map[] = {
1009 rgb1,rgb1,rgb1a,rgb1a,
1010 rgb8,rgb8,rgb8a,rgb8a,
1011 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1012 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1013 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1017 * perform actual conversion
1019 * If we can, try and use the optimised code versions, but as a default
1020 * fallback, and always for direct colour, use the generic/slow but complete
1021 * conversion function.
1024 rgbconvert (GdkImage *image,
1038 g_assert ((x + width) <= image->width);
1039 g_assert ((y + height) <= image->height);
1043 /* Only allowed for bitmaps */
1044 g_return_if_fail (image->depth == 1);
1047 bitmap1a (image, pixels, rowstride,
1048 x, y, x + width, y + height);
1050 bitmap1 (image, pixels, rowstride,
1051 x, y, x + width, y + height);
1056 v = gdk_colormap_get_visual (cmap);
1058 if (image->depth != v->depth)
1060 g_warning ("%s: The depth of the source image (%d) doesn't "
1061 "match the depth of the colormap passed in (%d).",
1062 G_STRLOC, image->depth, v->depth);
1066 bank = 5; /* default fallback converter */
1067 index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
1069 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1070 d(printf("image depth = %d, bits per pixel = %d\n", image->depth, image->bits_per_pixel));
1074 /* I assume this is right for static & greyscale's too? */
1075 case GDK_VISUAL_STATIC_GRAY:
1076 case GDK_VISUAL_GRAYSCALE:
1077 case GDK_VISUAL_STATIC_COLOR:
1078 case GDK_VISUAL_PSEUDO_COLOR:
1079 switch (image->bits_per_pixel)
1085 if (image->depth == 8)
1090 case GDK_VISUAL_TRUE_COLOR:
1091 switch (image->depth)
1094 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1095 && image->bits_per_pixel == 16)
1099 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1100 && image->bits_per_pixel == 16)
1105 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1106 && image->bits_per_pixel == 32)
1111 case GDK_VISUAL_DIRECT_COLOR:
1112 /* always use the slow version */
1116 d (g_print ("converting using conversion function in bank %d\n", bank));
1120 convert_real_slow (image, pixels, rowstride,
1121 x, y, x + width, y + height,
1127 d (g_print ("converting with index %d\n", index));
1128 (* convert_map[index]) (image, pixels, rowstride,
1129 x, y, x + width, y + height,
1135 /* Exported functions */
1138 * gdk_pixbuf_get_from_drawable:
1139 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1140 * @src: Source drawable.
1141 * @cmap: A colormap if @src doesn't have one set.
1142 * @src_x: Source X coordinate within drawable.
1143 * @src_y: Source Y coordinate within drawable.
1144 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1145 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1146 * @width: Width in pixels of region to get.
1147 * @height: Height in pixels of region to get.
1149 * Transfers image data from a #GdkDrawable and converts it to an RGB(A)
1150 * representation inside a #GdkPixbuf. In other words, copies
1151 * image data from a server-side drawable to a client-side RGB(A) buffer.
1152 * This allows you to efficiently read individual pixels on the client side.
1154 * If the drawable @src has no colormap (gdk_drawable_get_colormap()
1155 * returns %NULL), then a suitable colormap must be specified.
1156 * Typically a #GdkWindow or a pixmap created by passing a #GdkWindow
1157 * to gdk_pixmap_new() will already have a colormap associated with
1158 * it. If the drawable has a colormap, the @cmap argument will be
1159 * ignored. If the drawable is a bitmap (1 bit per pixel pixmap),
1160 * then a colormap is not required; pixels with a value of 1 are
1161 * assumed to be white, and pixels with a value of 0 are assumed to be
1162 * black. For taking screenshots, gdk_colormap_get_system() returns
1163 * the correct colormap to use.
1165 * If the specified destination pixbuf @dest is %NULL, then this
1166 * function will create an RGB pixbuf with 8 bits per channel and no
1167 * alpha, with the same size specified by the @width and @height
1168 * arguments. In this case, the @dest_x and @dest_y arguments must be
1169 * specified as 0. If the specified destination pixbuf is not %NULL
1170 * and it contains alpha information, then the filled pixels will be
1171 * set to full opacity (alpha = 255).
1173 * If the specified drawable is a pixmap, then the requested source
1174 * rectangle must be completely contained within the pixmap, otherwise
1175 * the function will return %NULL. For pixmaps only (not for windows)
1176 * passing -1 for width or height is allowed to mean the full width
1177 * or height of the pixmap.
1179 * If the specified drawable is a window, and the window is off the
1180 * screen, then there is no image data in the obscured/offscreen
1181 * regions to be placed in the pixbuf. The contents of portions of the
1182 * pixbuf corresponding to the offscreen region are undefined.
1184 * If the window you're obtaining data from is partially obscured by
1185 * other windows, then the contents of the pixbuf areas corresponding
1186 * to the obscured regions are undefined.
1188 * If the target drawable is not mapped (typically because it's
1189 * iconified/minimized or not on the current workspace), then %NULL
1192 * If memory can't be allocated for the return value, %NULL will be returned
1195 * (In short, there are several ways this function can fail, and if it fails
1196 * it returns %NULL; so check the return value.)
1198 * This function calls gdk_drawable_get_image() internally and
1199 * converts the resulting image to a #GdkPixbuf, so the
1200 * documentation for gdk_drawable_get_image() may also be relevant.
1202 * Return value: The same pixbuf as @dest if it was non-%NULL, or a newly-created
1203 * pixbuf with a reference count of 1 if no destination pixbuf was specified, or %NULL on error
1206 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1209 int src_x, int src_y,
1210 int dest_x, int dest_y,
1211 int width, int height)
1213 int src_width, src_height;
1218 /* General sanity checks */
1220 g_return_val_if_fail (src != NULL, NULL);
1222 if (GDK_IS_WINDOW (src))
1223 /* FIXME: this is not perfect, since is_viewable() only tests
1224 * recursively up the Gdk parent window tree, but stops at
1225 * foreign windows or Gdk toplevels. I.e. if a window manager
1226 * unmapped one of its own windows, this won't work.
1228 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1231 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1234 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1235 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1236 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1240 cmap = gdk_drawable_get_colormap (src);
1242 depth = gdk_drawable_get_depth (src);
1244 if (depth != 1 && cmap == NULL)
1246 g_warning ("%s: Source drawable has no colormap; either pass "
1247 "in a colormap, or set the colormap on the drawable "
1248 "with gdk_drawable_set_colormap()", G_STRLOC);
1252 if (cmap != NULL && depth != cmap->visual->depth)
1254 g_warning ("%s: Depth of the source drawable is %d where as "
1255 "the visual depth of the colormap passed is %d",
1256 G_STRLOC, depth, cmap->visual->depth);
1260 /* Coordinate sanity checks */
1262 if (GDK_IS_PIXMAP (src))
1264 gdk_drawable_get_size (src, &src_width, &src_height);
1268 height = src_height;
1270 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1271 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1274 /* Create the pixbuf if needed */
1277 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1284 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1285 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1286 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1289 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1291 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1292 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1296 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1298 image = _gdk_image_get_scratch (gdk_drawable_get_screen (src),
1299 width1, height1, depth, &xs0, &ys0);
1301 _gdk_drawable_copy_to_image (src, image,
1302 src_x + x0, src_y + y0,
1303 xs0, ys0, width1, height1);
1305 gdk_pixbuf_get_from_image (dest, image, cmap,
1306 xs0, ys0, dest_x + x0, dest_y + y0,
1315 * gdk_pixbuf_get_from_image:
1316 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1317 * @src: Source #GdkImage.
1318 * @cmap: A colormap, or %NULL to use the one for @src
1319 * @src_x: Source X coordinate within drawable.
1320 * @src_y: Source Y coordinate within drawable.
1321 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1322 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1323 * @width: Width in pixels of region to get.
1324 * @height: Height in pixels of region to get.
1326 * Same as gdk_pixbuf_get_from_drawable() but gets the pixbuf from
1329 * Return value: @dest, newly-created pixbuf if @dest was %NULL, %NULL on error
1332 gdk_pixbuf_get_from_image (GdkPixbuf *dest,
1342 int rowstride, bpp, alpha;
1344 /* General sanity checks */
1346 g_return_val_if_fail (GDK_IS_IMAGE (src), NULL);
1349 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1352 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1353 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1354 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1358 cmap = gdk_image_get_colormap (src);
1360 if (src->depth != 1 && cmap == NULL)
1362 g_warning ("%s: Source image has no colormap; either pass "
1363 "in a colormap, or set the colormap on the image "
1364 "with gdk_image_set_colormap()", G_STRLOC);
1368 if (cmap != NULL && src->depth != cmap->visual->depth)
1370 g_warning ("%s: Depth of the Source image is %d where as "
1371 "the visual depth of the colormap passed is %d",
1372 G_STRLOC, src->depth, cmap->visual->depth);
1376 /* Coordinate sanity checks */
1378 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1379 g_return_val_if_fail (src_x + width <= src->width && src_y + height <= src->height, NULL);
1383 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1384 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1385 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1388 /* Create the pixbuf if needed */
1391 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1396 alpha = dest->has_alpha;
1397 rowstride = dest->rowstride;
1398 bpp = alpha ? 4 : 3;
1400 /* we offset into the image data based on the position we are
1403 rgbconvert (src, dest->pixels +
1404 (dest_y * rowstride) + (dest_x * bpp),