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.
28 #include <gdk-pixbuf/gdk-pixbuf.h>
32 #include "gdkvisual.h"
33 #include "gdkwindow.h"
34 #include "gdkpixbuf.h"
35 #include "gdkpixmap.h"
36 #include "gdkinternals.h"
39 /* Some convenient names
41 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
50 #define SWAP16(d) GUINT16_SWAP_LE_BE(d)
54 static const guint32 mask_table[] = {
55 0x00000000, 0x00000001, 0x00000003, 0x00000007,
56 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
57 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
58 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
59 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
60 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
61 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
62 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
69 * convert bitmap data to pixbuf without alpha,
70 * without using a colormap
73 bitmap1 (GdkImage *image,
85 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
87 d (printf ("bitmap, no alpha\n"));
91 for (yy = y1; yy < y2; yy++)
95 for (xx = x1; xx < x2; xx ++)
97 /* top 29 bits of xx (xx >> 3) indicate the byte the bit is inside,
98 * bottom 3 bits (xx & 7) indicate bit inside that byte,
99 * we don't bother to canonicalize data to 1 or 0, just
100 * leave the relevant bit in-place.
102 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
125 * convert bitmap data to pixbuf with alpha,
126 * without using a colormap
129 bitmap1a (GdkImage *image,
139 register guint8 data;
141 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
143 d (printf ("bitmap, with alpha\n"));
147 for (yy = y1; yy < y2; yy++)
151 for (xx = x1; xx < x2; xx ++)
153 /* see comment in bitmap1() */
154 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
179 * convert 1 bits-pixel data
183 rgb1 (GdkImage *image,
190 GdkColormap *colormap)
194 register guint8 data;
196 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
198 d (printf ("1 bits/pixel\n"));
200 /* convert upto 8 pixels/time */
201 /* its probably not worth trying to make this run very fast, who uses
202 * 1 bit displays anymore?
206 for (yy = y1; yy < y2; yy++)
210 for (xx = x1; xx < x2; xx ++)
212 /* see comment in bitmap1() */
213 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
217 *o++ = colormap->colors[data].red >> 8;
218 *o++ = colormap->colors[data].green >> 8;
219 *o++ = colormap->colors[data].blue >> 8;
227 * convert 1 bits/pixel data
231 rgb1a (GdkImage *image,
238 GdkColormap *colormap)
242 register guint8 data;
244 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
246 d (printf ("1 bits/pixel\n"));
248 /* convert upto 8 pixels/time */
249 /* its probably not worth trying to make this run very fast, who uses
250 * 1 bit displays anymore? */
253 for (yy = y1; yy < y2; yy++)
257 for (xx = x1; xx < x2; xx ++)
259 /* see comment in bitmap1() */
260 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
264 *o++ = colormap->colors[data].red >> 8;
265 *o++ = colormap->colors[data].green >> 8;
266 *o++ = colormap->colors[data].blue >> 8;
275 * convert 8 bits/pixel data
279 rgb8 (GdkImage *image,
286 GdkColormap *colormap)
291 register guint32 data;
292 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
298 d (printf ("8 bit, no alpha output\n"));
300 mask = mask_table[image->depth];
302 for (yy = y1; yy < y2; yy++)
306 for (xx = x1; xx < x2; xx++)
309 *o++ = colormap->colors[data].red >> 8;
310 *o++ = colormap->colors[data].green >> 8;
311 *o++ = colormap->colors[data].blue >> 8;
319 * convert 8 bits/pixel data
323 rgb8a (GdkImage *image,
330 GdkColormap *colormap)
335 register guint32 data;
337 register guint8 *s; /* read 2 pixels at once */
339 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
343 d (printf ("8 bit, with alpha output\n"));
345 mask = mask_table[image->depth];
347 for (xx = x1; xx < colormap->size; xx++)
350 remap[xx] = 0xff000000
351 | (colormap->colors[xx].blue & 0xff00) << 8
352 | (colormap->colors[xx].green & 0xff00)
353 | (colormap->colors[xx].red >> 8);
356 | (colormap->colors[xx].red & 0xff00) << 16
357 | (colormap->colors[xx].green & 0xff00) << 8
358 | (colormap->colors[xx].blue & 0xff00);
362 for (yy = y1; yy < y2; yy++)
365 o = (guint32 *) orow;
366 for (xx = x1; xx < x2; xx ++)
376 /* Bit shifting for 565 and 555 conversion routines
378 * RGB565 == rrrr rggg gggb bbbb, 16 bit native endian
379 * RGB555 == xrrr rrgg gggb bbbb
380 * ABGR8888: ARGB, 32-bit native endian
381 * RGBA8888: RGBA, 32-bit native endian
383 #define R8fromRGB565(d) ((((d) >> 8) & 0xf8) | (((d) >> 13) & 0x7))
384 #define G8fromRGB565(d) ((((d) >> 3) & 0xfc) | (((d) >> 9) & 0x3))
385 #define B8fromRGB565(d) ((((d) << 3) & 0xf8) | (((d) >> 2) & 0x7))
387 #define ABGR8888fromRGB565(d) ( ((d) & 0xf800) >> 8 | ((d) & 0xe000) >> 13 \
388 | ((d) & 0x07e0) << 5 | ((d) & 0x0600) >> 1 \
389 | ((d) & 0x001f) << 19 | ((d) & 0x001c) << 14 \
391 #define RGBA8888fromRGB565(d) ( ((d) & 0xf800) << 16 | ((d) & 0xe000) << 11 \
392 | ((d) & 0x07e0) << 13 | ((d) & 0x0600) << 7 \
393 | ((d) & 0x001f) << 11 | ((d) & 0x001c) << 6 \
396 #define R8fromRGB555(d) (((d) & 0x7c00) >> 7 | ((d) & 0x7000) >> 12)
397 #define G8fromRGB555(d) (((d) & 0x03e0) >> 2 | ((d) & 0x0380) >> 7)
398 #define B8fromRGB555(d) (((d) & 0x001f) << 3 | ((d) & 0x001c) >> 2)
400 #define ABGR8888fromRGB555(d) ( ((d) & 0x7c00) >> 7 | ((d) & 0x7000) >> 12 \
401 | ((d) & 0x03e0) << 6 | ((d) & 0x0380) << 1 \
402 | ((d) & 0x001f) << 19 | ((d) & 0x001c) << 14 \
404 #define RGBA8888fromRGB555(d) ( ((d) & 0x7c00) << 17 | ((d) & 0x7000) << 12 \
405 | ((d) & 0x03e0) << 14 | ((d) & 0x0380) << 9 \
406 | ((d) & 0x001f) << 11 | ((d) & 0x001c) << 6 \
410 * convert 16 bits/pixel data
415 rgb565lsb (GdkImage *image,
422 GdkColormap *colormap)
430 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
434 for (yy = y1; yy < y2; yy++)
436 s = (guint16 *) srow;
438 for (xx = x1; xx < x2; xx ++)
440 register guint32 data = *s++;
442 data = SWAP16 (data);
444 *o++ = R8fromRGB565 (data);
445 *o++ = G8fromRGB565 (data);
446 *o++ = B8fromRGB565 (data);
454 * convert 16 bits/pixel data
459 rgb565msb (GdkImage *image,
466 GdkColormap *colormap)
474 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
478 for (yy = y1; yy < y2; yy++)
480 s = (guint16 *) srow;
482 for (xx = x1; xx < x2; xx ++)
484 register guint32 data = *s++;
486 data = SWAP16 (data);
488 *o++ = R8fromRGB565 (data);
489 *o++ = G8fromRGB565 (data);
490 *o++ = B8fromRGB565 (data);
498 * convert 16 bits/pixel data
503 rgb565alsb (GdkImage *image,
510 GdkColormap *colormap)
518 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
522 for (yy = y1; yy < y2; yy++)
524 s = (guint16 *) srow;
525 o = (guint32 *) orow;
526 for (xx = x1; xx < x2; xx ++)
528 register guint32 data = *s++;
530 *o++ = ABGR8888fromRGB565 (data);
532 data = SWAP16 (data);
533 *o++ = RGBA8888fromRGB565 (data);
542 * convert 16 bits/pixel data
547 rgb565amsb (GdkImage *image,
554 GdkColormap *colormap)
562 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
566 for (yy = y1; yy < y2; yy++)
568 s = (guint16 *) srow;
569 o = (guint32 *) orow;
570 for (xx = x1; xx < x2; xx ++)
572 register guint32 data = *s++;
574 data = SWAP16 (data);
575 *o++ = ABGR8888fromRGB565 (data);
577 *o++ = RGBA8888fromRGB565 (data);
586 * convert 15 bits/pixel data
591 rgb555lsb (GdkImage *image,
598 GdkColormap *colormap)
606 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
610 for (yy = y1; yy < y2; yy++)
612 s = (guint16 *) srow;
614 for (xx = x1; xx < x2; xx ++)
616 register guint32 data = *s++;
618 data = SWAP16 (data);
620 *o++ = R8fromRGB555 (data);
621 *o++ = G8fromRGB555 (data);
622 *o++ = B8fromRGB555 (data);
630 * convert 15 bits/pixel data
635 rgb555msb (GdkImage *image,
642 GdkColormap *colormap)
650 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
654 for (yy = y1; yy < y2; yy++)
656 s = (guint16 *) srow;
658 for (xx = x1; xx < x2; xx ++)
660 register guint32 data = *s++;
662 data = SWAP16 (data);
664 *o++ = R8fromRGB555 (data);
665 *o++ = G8fromRGB555 (data);
666 *o++ = B8fromRGB555 (data);
674 * convert 15 bits/pixel data
679 rgb555alsb (GdkImage *image,
686 GdkColormap *colormap)
691 register guint16 *s; /* read 1 pixels at once */
694 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
698 for (yy = y1; yy < y2; yy++)
700 s = (guint16 *) srow;
701 o = (guint32 *) orow;
702 for (xx = x1; xx < x2; xx++)
704 register guint32 data = *s++;
706 *o++ = ABGR8888fromRGB555 (data);
708 data = SWAP16 (data);
709 *o++ = RGBA8888fromRGB555 (data);
718 * convert 15 bits/pixel data
723 rgb555amsb (GdkImage *image,
730 GdkColormap *colormap)
738 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
742 for (yy = y1; yy < y2; yy++)
744 s = (guint16 *) srow;
745 o = (guint32 *) orow;
746 for (xx = x1; xx < x2; xx++)
748 register guint32 data = *s++;
750 data = SWAP16 (data);
751 *o++ = ABGR8888fromRGB555 (data);
753 *o++ = RGBA8888fromRGB555 (data);
763 rgb888alsb (GdkImage *image,
770 GdkColormap *colormap)
775 guint8 *s; /* for byte order swapping */
777 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
781 d (printf ("32 bits/pixel with alpha\n"));
784 for (yy = y1; yy < y2; yy++)
788 for (xx = x1; xx < x2; xx++)
802 rgb888lsb (GdkImage *image,
809 GdkColormap *colormap)
814 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
819 d (printf ("32 bit, lsb, no alpha\n"));
821 for (yy = y1; yy < y2; yy++)
825 for (xx = x1; xx < x2; xx++)
838 rgb888amsb (GdkImage *image,
845 GdkColormap *colormap)
850 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
854 d (printf ("32 bit, msb, with alpha\n"));
859 for (yy = y1; yy < y2; yy++)
861 s = (guint32 *) srow;
862 o = (guint32 *) orow;
863 for (xx = x1; xx < x2; xx++)
866 *o++ = (*s++ >> 8) | 0xff000000;
868 *o++ = (*s++ << 8) | 0xff;
877 rgb888msb (GdkImage *image,
884 GdkColormap *colormap)
889 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
893 d (printf ("32 bit, msb, no alpha\n"));
897 for (yy = y1; yy < y2; yy++)
901 for (xx = x1; xx < x2; xx++)
914 * This should work correctly with any display/any endianness, but will probably
918 convert_real_slow (GdkImage *image,
929 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 const 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: (allow-none): 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 (gdk_pixbuf_get_colorspace (dest) == GDK_COLORSPACE_RGB, NULL);
1235 g_return_val_if_fail (gdk_pixbuf_get_n_channels (dest) == 3 ||
1236 gdk_pixbuf_get_n_channels (dest) == 4, NULL);
1237 g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (dest) == 8, NULL);
1241 cmap = gdk_drawable_get_colormap (src);
1243 depth = gdk_drawable_get_depth (src);
1245 if (depth != 1 && cmap == NULL)
1247 g_warning ("%s: Source drawable has no colormap; either pass "
1248 "in a colormap, or set the colormap on the drawable "
1249 "with gdk_drawable_set_colormap()", G_STRLOC);
1253 if (cmap != NULL && depth != cmap->visual->depth)
1255 g_warning ("%s: Depth of the source drawable is %d where as "
1256 "the visual depth of the colormap passed is %d",
1257 G_STRLOC, depth, cmap->visual->depth);
1261 /* Coordinate sanity checks */
1263 if (GDK_IS_PIXMAP (src))
1265 gdk_drawable_get_size (src, &src_width, &src_height);
1269 height = src_height;
1271 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1272 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1275 /* Create the pixbuf if needed */
1278 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1285 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1286 g_return_val_if_fail (dest_x + width <= gdk_pixbuf_get_width (dest), NULL);
1287 g_return_val_if_fail (dest_y + height <= gdk_pixbuf_get_height (dest), NULL);
1290 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1292 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1293 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1297 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1299 image = _gdk_image_get_scratch (gdk_drawable_get_screen (src),
1300 width1, height1, depth, &xs0, &ys0);
1302 gdk_drawable_copy_to_image (src, image,
1303 src_x + x0, src_y + y0,
1304 xs0, ys0, width1, height1);
1306 gdk_pixbuf_get_from_image (dest, image, cmap,
1307 xs0, ys0, dest_x + x0, dest_y + y0,
1316 * gdk_pixbuf_get_from_image:
1317 * @dest: (allow-none): Destination pixbuf, or %NULL if a new pixbuf should be created.
1318 * @src: Source #GdkImage.
1319 * @cmap: (allow-none): A colormap, or %NULL to use the one for @src
1320 * @src_x: Source X coordinate within drawable.
1321 * @src_y: Source Y coordinate within drawable.
1322 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1323 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1324 * @width: Width in pixels of region to get.
1325 * @height: Height in pixels of region to get.
1327 * Same as gdk_pixbuf_get_from_drawable() but gets the pixbuf from
1330 * Return value: @dest, newly-created pixbuf if @dest was %NULL, %NULL on error
1333 gdk_pixbuf_get_from_image (GdkPixbuf *dest,
1343 int rowstride, bpp, alpha;
1345 /* General sanity checks */
1347 g_return_val_if_fail (GDK_IS_IMAGE (src), NULL);
1350 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1353 g_return_val_if_fail (gdk_pixbuf_get_colorspace (dest) == GDK_COLORSPACE_RGB, NULL);
1354 g_return_val_if_fail (gdk_pixbuf_get_n_channels (dest) == 3 ||
1355 gdk_pixbuf_get_n_channels (dest) == 4, NULL);
1356 g_return_val_if_fail (gdk_pixbuf_get_bits_per_sample (dest) == 8, NULL);
1360 cmap = gdk_image_get_colormap (src);
1362 if (src->depth != 1 && cmap == NULL)
1364 g_warning ("%s: Source image has no colormap; either pass "
1365 "in a colormap, or set the colormap on the image "
1366 "with gdk_image_set_colormap()", G_STRLOC);
1370 if (cmap != NULL && src->depth != cmap->visual->depth)
1372 g_warning ("%s: Depth of the Source image is %d where as "
1373 "the visual depth of the colormap passed is %d",
1374 G_STRLOC, src->depth, cmap->visual->depth);
1378 /* Coordinate sanity checks */
1380 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1381 g_return_val_if_fail (src_x + width <= src->width && src_y + height <= src->height, NULL);
1385 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1386 g_return_val_if_fail (dest_x + width <= gdk_pixbuf_get_width (dest), NULL);
1387 g_return_val_if_fail (dest_y + height <= gdk_pixbuf_get_height (dest), NULL);
1390 /* Create the pixbuf if needed */
1393 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1398 alpha = gdk_pixbuf_get_has_alpha (dest);
1399 rowstride = gdk_pixbuf_get_rowstride (dest);
1400 bpp = alpha ? 4 : 3;
1402 /* we offset into the image data based on the position we are
1405 rgbconvert (src, gdk_pixbuf_get_pixels (dest) +
1406 (dest_y * rowstride) + (dest_x * bpp),