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"
38 /* Some convenient names
40 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
49 #define SWAP16(d) GUINT16_SWAP_LE_BE(d)
53 static const guint32 mask_table[] = {
54 0x00000000, 0x00000001, 0x00000003, 0x00000007,
55 0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
56 0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
57 0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
58 0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
59 0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
60 0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
61 0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
68 * convert bitmap data to pixbuf without alpha,
69 * without using a colormap
72 bitmap1 (GdkImage *image,
84 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
86 d (printf ("bitmap, no alpha\n"));
90 for (yy = y1; yy < y2; yy++)
94 for (xx = x1; xx < x2; xx ++)
96 /* top 29 bits of xx (xx >> 3) indicate the byte the bit is inside,
97 * bottom 3 bits (xx & 7) indicate bit inside that byte,
98 * we don't bother to canonicalize data to 1 or 0, just
99 * leave the relevant bit in-place.
101 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
124 * convert bitmap data to pixbuf with alpha,
125 * without using a colormap
128 bitmap1a (GdkImage *image,
138 register guint8 data;
140 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
142 d (printf ("bitmap, with alpha\n"));
146 for (yy = y1; yy < y2; yy++)
150 for (xx = x1; xx < x2; xx ++)
152 /* see comment in bitmap1() */
153 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
178 * convert 1 bits-pixel data
182 rgb1 (GdkImage *image,
189 GdkColormap *colormap)
193 register guint8 data;
195 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
197 d (printf ("1 bits/pixel\n"));
199 /* convert upto 8 pixels/time */
200 /* its probably not worth trying to make this run very fast, who uses
201 * 1 bit displays anymore?
205 for (yy = y1; yy < y2; yy++)
209 for (xx = x1; xx < x2; xx ++)
211 /* see comment in bitmap1() */
212 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
216 *o++ = colormap->colors[data].red >> 8;
217 *o++ = colormap->colors[data].green >> 8;
218 *o++ = colormap->colors[data].blue >> 8;
226 * convert 1 bits/pixel data
230 rgb1a (GdkImage *image,
237 GdkColormap *colormap)
241 register guint8 data;
243 guint8 *srow = (guint8*)image->mem + y1 * image->bpl, *orow = pixels;
245 d (printf ("1 bits/pixel\n"));
247 /* convert upto 8 pixels/time */
248 /* its probably not worth trying to make this run very fast, who uses
249 * 1 bit displays anymore? */
252 for (yy = y1; yy < y2; yy++)
256 for (xx = x1; xx < x2; xx ++)
258 /* see comment in bitmap1() */
259 data = srow[xx >> 3] & (image->byte_order == GDK_MSB_FIRST ?
263 *o++ = colormap->colors[data].red >> 8;
264 *o++ = colormap->colors[data].green >> 8;
265 *o++ = colormap->colors[data].blue >> 8;
274 * convert 8 bits/pixel data
278 rgb8 (GdkImage *image,
285 GdkColormap *colormap)
290 register guint32 data;
291 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
297 d (printf ("8 bit, no alpha output\n"));
299 mask = mask_table[image->depth];
301 for (yy = y1; yy < y2; yy++)
305 for (xx = x1; xx < x2; xx++)
308 *o++ = colormap->colors[data].red >> 8;
309 *o++ = colormap->colors[data].green >> 8;
310 *o++ = colormap->colors[data].blue >> 8;
318 * convert 8 bits/pixel data
322 rgb8a (GdkImage *image,
329 GdkColormap *colormap)
334 register guint32 data;
336 register guint8 *s; /* read 2 pixels at once */
338 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
342 d (printf ("8 bit, with alpha output\n"));
344 mask = mask_table[image->depth];
346 for (xx = x1; xx < colormap->size; xx++)
349 remap[xx] = 0xff000000
350 | (colormap->colors[xx].blue & 0xff00) << 8
351 | (colormap->colors[xx].green & 0xff00)
352 | (colormap->colors[xx].red >> 8);
355 | (colormap->colors[xx].red & 0xff00) << 16
356 | (colormap->colors[xx].green & 0xff00) << 8
357 | (colormap->colors[xx].blue & 0xff00);
361 for (yy = y1; yy < y2; yy++)
364 o = (guint32 *) orow;
365 for (xx = x1; xx < x2; xx ++)
375 /* Bit shifting for 565 and 555 conversion routines
377 * RGB565 == rrrr rggg gggb bbbb, 16 bit native endian
378 * RGB555 == xrrr rrgg gggb bbbb
379 * ABGR8888: ARGB, 32-bit native endian
380 * RGBA8888: RGBA, 32-bit native endian
382 #define R8fromRGB565(d) ((((d) >> 8) & 0xf8) | (((d) >> 13) & 0x7))
383 #define G8fromRGB565(d) ((((d) >> 3) & 0xfc) | (((d) >> 9) & 0x3))
384 #define B8fromRGB565(d) ((((d) << 3) & 0xf8) | (((d) >> 2) & 0x7))
386 #define ABGR8888fromRGB565(d) ( ((d) & 0xf800) >> 8 | ((d) & 0xe000) >> 13 \
387 | ((d) & 0x07e0) << 5 | ((d) & 0x0600) >> 1 \
388 | ((d) & 0x001f) << 19 | ((d) & 0x001c) << 14 \
390 #define RGBA8888fromRGB565(d) ( ((d) & 0xf800) << 16 | ((d) & 0xe000) << 11 \
391 | ((d) & 0x07e0) << 13 | ((d) & 0x0600) << 7 \
392 | ((d) & 0x001f) << 11 | ((d) & 0x001c) << 6 \
395 #define R8fromRGB555(d) (((d) & 0x7c00) >> 7 | ((d) & 0x7000) >> 12)
396 #define G8fromRGB555(d) (((d) & 0x03e0) >> 2 | ((d) & 0x0380) >> 7)
397 #define B8fromRGB555(d) (((d) & 0x001f) << 3 | ((d) & 0x001c) >> 2)
399 #define ABGR8888fromRGB555(d) ( ((d) & 0x7c00) >> 7 | ((d) & 0x7000) >> 12 \
400 | ((d) & 0x03e0) << 6 | ((d) & 0x0380) << 1 \
401 | ((d) & 0x001f) << 19 | ((d) & 0x001c) << 14 \
403 #define RGBA8888fromRGB555(d) ( ((d) & 0x7c00) << 17 | ((d) & 0x7000) << 12 \
404 | ((d) & 0x03e0) << 14 | ((d) & 0x0380) << 9 \
405 | ((d) & 0x001f) << 11 | ((d) & 0x001c) << 6 \
409 * convert 16 bits/pixel data
414 rgb565lsb (GdkImage *image,
421 GdkColormap *colormap)
429 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
433 for (yy = y1; yy < y2; yy++)
435 s = (guint16 *) srow;
437 for (xx = x1; xx < x2; xx ++)
439 register guint32 data = *s++;
441 data = SWAP16 (data);
443 *o++ = R8fromRGB565 (data);
444 *o++ = G8fromRGB565 (data);
445 *o++ = B8fromRGB565 (data);
453 * convert 16 bits/pixel data
458 rgb565msb (GdkImage *image,
465 GdkColormap *colormap)
473 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
477 for (yy = y1; yy < y2; yy++)
479 s = (guint16 *) srow;
481 for (xx = x1; xx < x2; xx ++)
483 register guint32 data = *s++;
485 data = SWAP16 (data);
487 *o++ = R8fromRGB565 (data);
488 *o++ = G8fromRGB565 (data);
489 *o++ = B8fromRGB565 (data);
497 * convert 16 bits/pixel data
502 rgb565alsb (GdkImage *image,
509 GdkColormap *colormap)
517 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
521 for (yy = y1; yy < y2; yy++)
523 s = (guint16 *) srow;
524 o = (guint32 *) orow;
525 for (xx = x1; xx < x2; xx ++)
527 register guint32 data = *s++;
529 *o++ = ABGR8888fromRGB565 (data);
531 data = SWAP16 (data);
532 *o++ = RGBA8888fromRGB565 (data);
541 * convert 16 bits/pixel data
546 rgb565amsb (GdkImage *image,
553 GdkColormap *colormap)
561 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
565 for (yy = y1; yy < y2; yy++)
567 s = (guint16 *) srow;
568 o = (guint32 *) orow;
569 for (xx = x1; xx < x2; xx ++)
571 register guint32 data = *s++;
573 data = SWAP16 (data);
574 *o++ = ABGR8888fromRGB565 (data);
576 *o++ = RGBA8888fromRGB565 (data);
585 * convert 15 bits/pixel data
590 rgb555lsb (GdkImage *image,
597 GdkColormap *colormap)
605 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
609 for (yy = y1; yy < y2; yy++)
611 s = (guint16 *) srow;
613 for (xx = x1; xx < x2; xx ++)
615 register guint32 data = *s++;
617 data = SWAP16 (data);
619 *o++ = R8fromRGB555 (data);
620 *o++ = G8fromRGB555 (data);
621 *o++ = B8fromRGB555 (data);
629 * convert 15 bits/pixel data
634 rgb555msb (GdkImage *image,
641 GdkColormap *colormap)
649 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
653 for (yy = y1; yy < y2; yy++)
655 s = (guint16 *) srow;
657 for (xx = x1; xx < x2; xx ++)
659 register guint32 data = *s++;
661 data = SWAP16 (data);
663 *o++ = R8fromRGB555 (data);
664 *o++ = G8fromRGB555 (data);
665 *o++ = B8fromRGB555 (data);
673 * convert 15 bits/pixel data
678 rgb555alsb (GdkImage *image,
685 GdkColormap *colormap)
690 register guint16 *s; /* read 1 pixels at once */
693 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
697 for (yy = y1; yy < y2; yy++)
699 s = (guint16 *) srow;
700 o = (guint32 *) orow;
701 for (xx = x1; xx < x2; xx++)
703 register guint32 data = *s++;
705 *o++ = ABGR8888fromRGB555 (data);
707 data = SWAP16 (data);
708 *o++ = RGBA8888fromRGB555 (data);
717 * convert 15 bits/pixel data
722 rgb555amsb (GdkImage *image,
729 GdkColormap *colormap)
737 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
741 for (yy = y1; yy < y2; yy++)
743 s = (guint16 *) srow;
744 o = (guint32 *) orow;
745 for (xx = x1; xx < x2; xx++)
747 register guint32 data = *s++;
749 data = SWAP16 (data);
750 *o++ = ABGR8888fromRGB555 (data);
752 *o++ = RGBA8888fromRGB555 (data);
762 rgb888alsb (GdkImage *image,
769 GdkColormap *colormap)
774 guint8 *s; /* for byte order swapping */
776 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
780 d (printf ("32 bits/pixel with alpha\n"));
783 for (yy = y1; yy < y2; yy++)
787 for (xx = x1; xx < x2; xx++)
801 rgb888lsb (GdkImage *image,
808 GdkColormap *colormap)
813 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
818 d (printf ("32 bit, lsb, no alpha\n"));
820 for (yy = y1; yy < y2; yy++)
824 for (xx = x1; xx < x2; xx++)
837 rgb888amsb (GdkImage *image,
844 GdkColormap *colormap)
849 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
853 d (printf ("32 bit, msb, with alpha\n"));
858 for (yy = y1; yy < y2; yy++)
860 s = (guint32 *) srow;
861 o = (guint32 *) orow;
862 for (xx = x1; xx < x2; xx++)
865 *o++ = (*s++ >> 8) | 0xff000000;
867 *o++ = (*s++ << 8) | 0xff;
876 rgb888msb (GdkImage *image,
883 GdkColormap *colormap)
888 guint8 *srow = (guint8*)image->mem + y1 * image->bpl + x1 * image->bpp, *orow = pixels;
892 d (printf ("32 bit, msb, no alpha\n"));
896 for (yy = y1; yy < y2; yy++)
900 for (xx = x1; xx < x2; xx++)
913 * This should work correctly with any display/any endianness, but will probably
917 convert_real_slow (GdkImage *image,
928 guint8 *orow = pixels;
935 v = gdk_colormap_get_visual (cmap);
937 if (image->depth != v->depth)
939 g_warning ("%s: The depth of the source image (%d) doesn't "
940 "match the depth of the colormap passed in (%d).",
941 G_STRLOC, image->depth, v->depth);
945 d(printf("rgb mask/shift/prec = %x:%x:%x %d:%d:%d %d:%d:%d\n",
946 v->red_mask, v->green_mask, v->blue_mask,
947 v->red_shift, v->green_shift, v->blue_shift,
948 v->red_prec, v->green_prec, v->blue_prec));
950 for (yy = y1; yy < y2; yy++)
953 for (xx = x1; xx < x2; xx++)
955 pixel = gdk_image_get_pixel (image, xx, yy);
958 /* I assume this is right for static & greyscale's too? */
959 case GDK_VISUAL_STATIC_GRAY:
960 case GDK_VISUAL_GRAYSCALE:
961 case GDK_VISUAL_STATIC_COLOR:
962 case GDK_VISUAL_PSEUDO_COLOR:
963 *o++ = cmap->colors[pixel].red >> 8;
964 *o++ = cmap->colors[pixel].green >> 8;
965 *o++ = cmap->colors[pixel].blue >> 8;
967 case GDK_VISUAL_TRUE_COLOR:
968 /* This is odd because it must sometimes shift left (otherwise
969 * I'd just shift >> (*_shift - 8 + *_prec + <0-7>). This logic
970 * should work for all bit sizes/shifts/etc.
973 for (i = 24; i < 32; i += v->red_prec)
974 component |= ((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> i;
977 for (i = 24; i < 32; i += v->green_prec)
978 component |= ((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> i;
981 for (i = 24; i < 32; i += v->blue_prec)
982 component |= ((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> i;
985 case GDK_VISUAL_DIRECT_COLOR:
986 *o++ = cmap->colors[((pixel & v->red_mask) << (32 - v->red_shift - v->red_prec)) >> 24].red >> 8;
987 *o++ = cmap->colors[((pixel & v->green_mask) << (32 - v->green_shift - v->green_prec)) >> 24].green >> 8;
988 *o++ = cmap->colors[((pixel & v->blue_mask) << (32 - v->blue_shift - v->blue_prec)) >> 24].blue >> 8;
998 typedef void (* cfunc) (GdkImage *image,
1007 static const cfunc convert_map[] = {
1008 rgb1,rgb1,rgb1a,rgb1a,
1009 rgb8,rgb8,rgb8a,rgb8a,
1010 rgb555lsb,rgb555msb,rgb555alsb,rgb555amsb,
1011 rgb565lsb,rgb565msb,rgb565alsb,rgb565amsb,
1012 rgb888lsb,rgb888msb,rgb888alsb,rgb888amsb
1016 * perform actual conversion
1018 * If we can, try and use the optimised code versions, but as a default
1019 * fallback, and always for direct colour, use the generic/slow but complete
1020 * conversion function.
1023 rgbconvert (GdkImage *image,
1037 g_assert ((x + width) <= image->width);
1038 g_assert ((y + height) <= image->height);
1042 /* Only allowed for bitmaps */
1043 g_return_if_fail (image->depth == 1);
1046 bitmap1a (image, pixels, rowstride,
1047 x, y, x + width, y + height);
1049 bitmap1 (image, pixels, rowstride,
1050 x, y, x + width, y + height);
1055 v = gdk_colormap_get_visual (cmap);
1057 if (image->depth != v->depth)
1059 g_warning ("%s: The depth of the source image (%d) doesn't "
1060 "match the depth of the colormap passed in (%d).",
1061 G_STRLOC, image->depth, v->depth);
1065 bank = 5; /* default fallback converter */
1066 index = (image->byte_order == GDK_MSB_FIRST) | (alpha != 0) << 1;
1068 d(printf("masks = %x:%x:%x\n", v->red_mask, v->green_mask, v->blue_mask));
1069 d(printf("image depth = %d, bits per pixel = %d\n", image->depth, image->bits_per_pixel));
1073 /* I assume this is right for static & greyscale's too? */
1074 case GDK_VISUAL_STATIC_GRAY:
1075 case GDK_VISUAL_GRAYSCALE:
1076 case GDK_VISUAL_STATIC_COLOR:
1077 case GDK_VISUAL_PSEUDO_COLOR:
1078 switch (image->bits_per_pixel)
1084 if (image->depth == 8)
1089 case GDK_VISUAL_TRUE_COLOR:
1090 switch (image->depth)
1093 if (v->red_mask == 0x7c00 && v->green_mask == 0x3e0 && v->blue_mask == 0x1f
1094 && image->bits_per_pixel == 16)
1098 if (v->red_mask == 0xf800 && v->green_mask == 0x7e0 && v->blue_mask == 0x1f
1099 && image->bits_per_pixel == 16)
1104 if (v->red_mask == 0xff0000 && v->green_mask == 0xff00 && v->blue_mask == 0xff
1105 && image->bits_per_pixel == 32)
1110 case GDK_VISUAL_DIRECT_COLOR:
1111 /* always use the slow version */
1115 d (g_print ("converting using conversion function in bank %d\n", bank));
1119 convert_real_slow (image, pixels, rowstride,
1120 x, y, x + width, y + height,
1126 d (g_print ("converting with index %d\n", index));
1127 (* convert_map[index]) (image, pixels, rowstride,
1128 x, y, x + width, y + height,
1134 /* Exported functions */
1137 * gdk_pixbuf_get_from_drawable:
1138 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1139 * @src: Source drawable.
1140 * @cmap: A colormap if @src doesn't have one set.
1141 * @src_x: Source X coordinate within drawable.
1142 * @src_y: Source Y coordinate within drawable.
1143 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1144 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1145 * @width: Width in pixels of region to get.
1146 * @height: Height in pixels of region to get.
1148 * Transfers image data from a #GdkDrawable and converts it to an RGB(A)
1149 * representation inside a #GdkPixbuf. In other words, copies
1150 * image data from a server-side drawable to a client-side RGB(A) buffer.
1151 * This allows you to efficiently read individual pixels on the client side.
1153 * If the drawable @src has no colormap (gdk_drawable_get_colormap()
1154 * returns %NULL), then a suitable colormap must be specified.
1155 * Typically a #GdkWindow or a pixmap created by passing a #GdkWindow
1156 * to gdk_pixmap_new() will already have a colormap associated with
1157 * it. If the drawable has a colormap, the @cmap argument will be
1158 * ignored. If the drawable is a bitmap (1 bit per pixel pixmap),
1159 * then a colormap is not required; pixels with a value of 1 are
1160 * assumed to be white, and pixels with a value of 0 are assumed to be
1161 * black. For taking screenshots, gdk_colormap_get_system() returns
1162 * the correct colormap to use.
1164 * If the specified destination pixbuf @dest is %NULL, then this
1165 * function will create an RGB pixbuf with 8 bits per channel and no
1166 * alpha, with the same size specified by the @width and @height
1167 * arguments. In this case, the @dest_x and @dest_y arguments must be
1168 * specified as 0. If the specified destination pixbuf is not %NULL
1169 * and it contains alpha information, then the filled pixels will be
1170 * set to full opacity (alpha = 255).
1172 * If the specified drawable is a pixmap, then the requested source
1173 * rectangle must be completely contained within the pixmap, otherwise
1174 * the function will return %NULL. For pixmaps only (not for windows)
1175 * passing -1 for width or height is allowed to mean the full width
1176 * or height of the pixmap.
1178 * If the specified drawable is a window, and the window is off the
1179 * screen, then there is no image data in the obscured/offscreen
1180 * regions to be placed in the pixbuf. The contents of portions of the
1181 * pixbuf corresponding to the offscreen region are undefined.
1183 * If the window you're obtaining data from is partially obscured by
1184 * other windows, then the contents of the pixbuf areas corresponding
1185 * to the obscured regions are undefined.
1187 * If the target drawable is not mapped (typically because it's
1188 * iconified/minimized or not on the current workspace), then %NULL
1191 * If memory can't be allocated for the return value, %NULL will be returned
1194 * (In short, there are several ways this function can fail, and if it fails
1195 * it returns %NULL; so check the return value.)
1197 * This function calls gdk_drawable_get_image() internally and
1198 * converts the resulting image to a #GdkPixbuf, so the
1199 * documentation for gdk_drawable_get_image() may also be relevant.
1201 * Return value: The same pixbuf as @dest if it was non-%NULL, or a newly-created
1202 * pixbuf with a reference count of 1 if no destination pixbuf was specified, or %NULL on error
1205 gdk_pixbuf_get_from_drawable (GdkPixbuf *dest,
1208 int src_x, int src_y,
1209 int dest_x, int dest_y,
1210 int width, int height)
1212 int src_width, src_height;
1217 /* General sanity checks */
1219 g_return_val_if_fail (src != NULL, NULL);
1221 if (GDK_IS_WINDOW (src))
1222 /* FIXME: this is not perfect, since is_viewable() only tests
1223 * recursively up the Gdk parent window tree, but stops at
1224 * foreign windows or Gdk toplevels. I.e. if a window manager
1225 * unmapped one of its own windows, this won't work.
1227 g_return_val_if_fail (gdk_window_is_viewable (src), NULL);
1230 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1233 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1234 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1235 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1239 cmap = gdk_drawable_get_colormap (src);
1241 depth = gdk_drawable_get_depth (src);
1243 if (depth != 1 && cmap == NULL)
1245 g_warning ("%s: Source drawable has no colormap; either pass "
1246 "in a colormap, or set the colormap on the drawable "
1247 "with gdk_drawable_set_colormap()", G_STRLOC);
1251 if (cmap != NULL && depth != cmap->visual->depth)
1253 g_warning ("%s: Depth of the source drawable is %d where as "
1254 "the visual depth of the colormap passed is %d",
1255 G_STRLOC, depth, cmap->visual->depth);
1259 /* Coordinate sanity checks */
1261 if (GDK_IS_PIXMAP (src))
1263 gdk_drawable_get_size (src, &src_width, &src_height);
1267 height = src_height;
1269 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1270 g_return_val_if_fail (src_x + width <= src_width && src_y + height <= src_height, NULL);
1273 /* Create the pixbuf if needed */
1276 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1283 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1284 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1285 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1288 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1290 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1291 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1295 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1297 image = _gdk_image_get_scratch (gdk_drawable_get_screen (src),
1298 width1, height1, depth, &xs0, &ys0);
1300 gdk_drawable_copy_to_image (src, image,
1301 src_x + x0, src_y + y0,
1302 xs0, ys0, width1, height1);
1304 gdk_pixbuf_get_from_image (dest, image, cmap,
1305 xs0, ys0, dest_x + x0, dest_y + y0,
1314 * gdk_pixbuf_get_from_image:
1315 * @dest: Destination pixbuf, or %NULL if a new pixbuf should be created.
1316 * @src: Source #GdkImage.
1317 * @cmap: A colormap, or %NULL to use the one for @src
1318 * @src_x: Source X coordinate within drawable.
1319 * @src_y: Source Y coordinate within drawable.
1320 * @dest_x: Destination X coordinate in pixbuf, or 0 if @dest is NULL.
1321 * @dest_y: Destination Y coordinate in pixbuf, or 0 if @dest is NULL.
1322 * @width: Width in pixels of region to get.
1323 * @height: Height in pixels of region to get.
1325 * Same as gdk_pixbuf_get_from_drawable() but gets the pixbuf from
1328 * Return value: @dest, newly-created pixbuf if @dest was %NULL, %NULL on error
1331 gdk_pixbuf_get_from_image (GdkPixbuf *dest,
1341 int rowstride, bpp, alpha;
1343 /* General sanity checks */
1345 g_return_val_if_fail (GDK_IS_IMAGE (src), NULL);
1348 g_return_val_if_fail (dest_x == 0 && dest_y == 0, NULL);
1351 g_return_val_if_fail (dest->colorspace == GDK_COLORSPACE_RGB, NULL);
1352 g_return_val_if_fail (dest->n_channels == 3 || dest->n_channels == 4, NULL);
1353 g_return_val_if_fail (dest->bits_per_sample == 8, NULL);
1357 cmap = gdk_image_get_colormap (src);
1359 if (src->depth != 1 && cmap == NULL)
1361 g_warning ("%s: Source image has no colormap; either pass "
1362 "in a colormap, or set the colormap on the image "
1363 "with gdk_image_set_colormap()", G_STRLOC);
1367 if (cmap != NULL && src->depth != cmap->visual->depth)
1369 g_warning ("%s: Depth of the Source image is %d where as "
1370 "the visual depth of the colormap passed is %d",
1371 G_STRLOC, src->depth, cmap->visual->depth);
1375 /* Coordinate sanity checks */
1377 g_return_val_if_fail (src_x >= 0 && src_y >= 0, NULL);
1378 g_return_val_if_fail (src_x + width <= src->width && src_y + height <= src->height, NULL);
1382 g_return_val_if_fail (dest_x >= 0 && dest_y >= 0, NULL);
1383 g_return_val_if_fail (dest_x + width <= dest->width, NULL);
1384 g_return_val_if_fail (dest_y + height <= dest->height, NULL);
1387 /* Create the pixbuf if needed */
1390 dest = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, width, height);
1395 alpha = dest->has_alpha;
1396 rowstride = dest->rowstride;
1397 bpp = alpha ? 4 : 3;
1399 /* we offset into the image data based on the position we are
1402 rgbconvert (src, dest->pixels +
1403 (dest_y * rowstride) + (dest_x * bpp),
1414 #define __GDK_PIXBUF_DRAWABLE_C__
1415 #include "gdkaliasdef.c"