1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GTK+ Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
27 #include "gdkdrawable.h"
28 #include "gdkinternals.h"
29 #include "gdkwindow.h"
30 #include "gdkscreen.h"
31 #include "gdk-pixbuf-private.h"
32 #include "gdkpixbuf.h"
34 static GdkImage* gdk_drawable_real_get_image (GdkDrawable *drawable,
39 static GdkDrawable* gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
44 gint *composite_x_offset,
45 gint *composite_y_offset);
46 static GdkRegion * gdk_drawable_real_get_visible_region (GdkDrawable *drawable);
47 static void gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
60 static void gdk_drawable_class_init (GdkDrawableClass *klass);
63 gdk_drawable_get_type (void)
65 static GType object_type = 0;
69 static const GTypeInfo object_info =
71 sizeof (GdkDrawableClass),
73 (GBaseFinalizeFunc) NULL,
74 (GClassInitFunc) gdk_drawable_class_init,
75 NULL, /* class_finalize */
76 NULL, /* class_data */
79 (GInstanceInitFunc) NULL,
82 object_type = g_type_register_static (G_TYPE_OBJECT,
85 G_TYPE_FLAG_ABSTRACT);
92 gdk_drawable_class_init (GdkDrawableClass *klass)
94 klass->get_image = gdk_drawable_real_get_image;
95 klass->get_composite_drawable = gdk_drawable_real_get_composite_drawable;
96 /* Default implementation for clip and visible region is the same */
97 klass->get_clip_region = gdk_drawable_real_get_visible_region;
98 klass->get_visible_region = gdk_drawable_real_get_visible_region;
99 klass->_draw_pixbuf = gdk_drawable_real_draw_pixbuf;
102 /* Manipulation of drawables
106 * gdk_drawable_set_data:
107 * @drawable: a #GdkDrawable
108 * @key: name to store the data under
109 * @data: arbitrary data
110 * @destroy_func: function to free @data, or %NULL
112 * This function is equivalent to g_object_set_data(),
113 * the #GObject variant should be used instead.
117 gdk_drawable_set_data (GdkDrawable *drawable,
120 GDestroyNotify destroy_func)
122 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
124 g_object_set_qdata_full (G_OBJECT (drawable),
125 g_quark_from_string (key),
131 * gdk_drawable_get_data:
132 * @drawable: a #GdkDrawable
133 * @key: name the data was stored under
135 * Equivalent to g_object_get_data(); the #GObject variant should be
138 * Return value: the data stored at @key
141 gdk_drawable_get_data (GdkDrawable *drawable,
144 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
146 return g_object_get_qdata (G_OBJECT (drawable),
147 g_quark_try_string (key));
151 * gdk_drawable_get_size:
152 * @drawable: a #GdkDrawable
153 * @width: location to store drawable's width, or %NULL
154 * @height: location to store drawable's height, or %NULL
156 * Fills *@width and *@height with the size of @drawable.
157 * @width or @height can be %NULL if you only want the other one.
159 * On the X11 platform, if @drawable is a #GdkWindow, the returned
160 * size is the size reported in the most-recently-processed configure
161 * event, rather than the current size on the X server.
165 gdk_drawable_get_size (GdkDrawable *drawable,
169 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
171 GDK_DRAWABLE_GET_CLASS (drawable)->get_size (drawable, width, height);
175 * gdk_drawable_get_visual:
176 * @drawable: a #GdkDrawable
178 * Gets the #GdkVisual describing the pixel format of @drawable.
180 * Return value: a #GdkVisual
183 gdk_drawable_get_visual (GdkDrawable *drawable)
185 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
187 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visual (drawable);
191 * gdk_drawable_get_depth:
192 * @drawable: a #GdkDrawable
194 * Obtains the bit depth of the drawable, that is, the number of bits
195 * that make up a pixel in the drawable's visual. Examples are 8 bits
196 * per pixel, 24 bits per pixel, etc.
198 * Return value: number of bits per pixel
201 gdk_drawable_get_depth (GdkDrawable *drawable)
203 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), 0);
205 return GDK_DRAWABLE_GET_CLASS (drawable)->get_depth (drawable);
208 * gdk_drawable_get_screen:
209 * @drawable: a #GdkDrawable
211 * Gets the #GdkScreen associated with a #GdkDrawable.
213 * Return value: the #GdkScreen associated with @drawable
216 gdk_drawable_get_screen(GdkDrawable *drawable)
218 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
220 return GDK_DRAWABLE_GET_CLASS (drawable)->get_screen (drawable);
224 * gdk_drawable_get_display:
225 * @drawable: a #GdkDrawable
227 * Gets the #GdkDisplay associated with a #GdkDrawable.
229 * Return value: the #GdkDisplay associated with @drawable
232 gdk_drawable_get_display (GdkDrawable *drawable)
234 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
236 return gdk_screen_get_display (gdk_drawable_get_screen (drawable));
240 * gdk_drawable_set_colormap:
241 * @drawable: a #GdkDrawable
242 * @colormap: a #GdkColormap
244 * Sets the colormap associated with @drawable. Normally this will
245 * happen automatically when the drawable is created; you only need to
246 * use this function if the drawable-creating function did not have a
247 * way to determine the colormap, and you then use drawable operations
248 * that require a colormap. The colormap for all drawables and
249 * graphics contexts you intend to use together should match. i.e.
250 * when using a #GdkGC to draw to a drawable, or copying one drawable
251 * to another, the colormaps should match.
255 gdk_drawable_set_colormap (GdkDrawable *drawable,
258 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
259 g_return_if_fail (cmap == NULL || gdk_drawable_get_depth (drawable)
260 == cmap->visual->depth);
262 GDK_DRAWABLE_GET_CLASS (drawable)->set_colormap (drawable, cmap);
266 * gdk_drawable_get_colormap:
267 * @drawable: a #GdkDrawable
269 * Gets the colormap for @drawable, if one is set; returns
272 * Return value: the colormap, or %NULL
275 gdk_drawable_get_colormap (GdkDrawable *drawable)
277 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
279 return GDK_DRAWABLE_GET_CLASS (drawable)->get_colormap (drawable);
284 * @drawable: a #GdkDrawable
286 * Deprecated equivalent of calling g_object_ref() on @drawable.
287 * (Drawables were not objects in previous versions of GDK.)
289 * Return value: the same @drawable passed in
292 gdk_drawable_ref (GdkDrawable *drawable)
294 return (GdkDrawable *) g_object_ref (G_OBJECT (drawable));
298 * gdk_drawable_unref:
299 * @drawable: a #GdkDrawable
301 * Deprecated equivalent of calling g_object_unref() on @drawable.
305 gdk_drawable_unref (GdkDrawable *drawable)
307 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
309 g_object_unref (G_OBJECT (drawable));
315 gdk_draw_point (GdkDrawable *drawable,
322 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
323 g_return_if_fail (GDK_IS_GC (gc));
328 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, &point, 1);
332 gdk_draw_line (GdkDrawable *drawable,
341 g_return_if_fail (drawable != NULL);
342 g_return_if_fail (gc != NULL);
343 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
344 g_return_if_fail (GDK_IS_GC (gc));
350 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, &segment, 1);
354 gdk_draw_rectangle (GdkDrawable *drawable,
362 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
363 g_return_if_fail (GDK_IS_GC (gc));
365 if (width < 0 || height < 0)
370 gdk_drawable_get_size (drawable, &real_width, &real_height);
375 height = real_height;
378 GDK_DRAWABLE_GET_CLASS (drawable)->draw_rectangle (drawable, gc, filled, x, y,
383 gdk_draw_arc (GdkDrawable *drawable,
393 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
394 g_return_if_fail (GDK_IS_GC (gc));
396 if (width < 0 || height < 0)
401 gdk_drawable_get_size (drawable, &real_width, &real_height);
406 height = real_height;
409 GDK_DRAWABLE_GET_CLASS (drawable)->draw_arc (drawable, gc, filled,
410 x, y, width, height, angle1, angle2);
414 gdk_draw_polygon (GdkDrawable *drawable,
420 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
421 g_return_if_fail (GDK_IS_GC (gc));
423 GDK_DRAWABLE_GET_CLASS (drawable)->draw_polygon (drawable, gc, filled,
429 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
431 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
434 gdk_draw_string (GdkDrawable *drawable,
441 gdk_draw_text (drawable, font, gc, x, y, string, _gdk_font_strlen (font, string));
446 * Modified by Li-Da Lho to draw 16 bits and Multibyte strings
448 * Interface changed: add "GdkFont *font" to specify font or fontset explicitely
451 gdk_draw_text (GdkDrawable *drawable,
459 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
460 g_return_if_fail (font != NULL);
461 g_return_if_fail (GDK_IS_GC (gc));
462 g_return_if_fail (text != NULL);
464 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text (drawable, font, gc, x, y, text, text_length);
468 gdk_draw_text_wc (GdkDrawable *drawable,
473 const GdkWChar *text,
476 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
477 g_return_if_fail (font != NULL);
478 g_return_if_fail (GDK_IS_GC (gc));
479 g_return_if_fail (text != NULL);
481 GDK_DRAWABLE_GET_CLASS (drawable)->draw_text_wc (drawable, font, gc, x, y, text, text_length);
486 * @drawable: a #GdkDrawable
487 * @gc: a #GdkGC sharing the drawable's visual and colormap
488 * @src: another #GdkDrawable
489 * @xsrc: X position in @src of rectangle to draw
490 * @ysrc: Y position in @src of rectangle to draw
491 * @xdest: X position in @drawable where the rectangle should be drawn
492 * @ydest: Y position in @drawable where the rectangle should be drawn
493 * @width: width of rectangle to draw, or -1 for entire @src width
494 * @height: height of rectangle to draw, or -1 for entire @src height
496 * Copies the @width x @height region of @src at coordinates (@xsrc,
497 * @ysrc) to coordinates (@xdest, @ydest) in @drawable.
498 * @width and/or @height may be given as -1, in which case the entire
499 * @src drawable will be copied.
501 * Most fields in @gc are not used for this operation, but notably the
502 * clip mask or clip region will be honored.
504 * The source and destination drawables must have the same visual and
505 * colormap, or errors will result. (On X11, failure to match
506 * visual/colormap results in a BadMatch error from the X server.)
507 * A common cause of this problem is an attempt to draw a bitmap to
508 * a color drawable. The way to draw a bitmap is to set the
509 * bitmap as a clip mask on your #GdkGC, then use gdk_draw_rectangle()
510 * to draw a rectangle clipped to the bitmap.
513 gdk_draw_drawable (GdkDrawable *drawable,
523 GdkDrawable *composite;
524 gint composite_x_offset = 0;
525 gint composite_y_offset = 0;
527 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
528 g_return_if_fail (src != NULL);
529 g_return_if_fail (GDK_IS_GC (gc));
531 if (width < 0 || height < 0)
536 gdk_drawable_get_size (src, &real_width, &real_height);
541 height = real_height;
546 GDK_DRAWABLE_GET_CLASS (src)->get_composite_drawable (src,
550 &composite_y_offset);
553 GDK_DRAWABLE_GET_CLASS (drawable)->draw_drawable (drawable, gc, composite,
554 xsrc - composite_x_offset,
555 ysrc - composite_y_offset,
559 g_object_unref (G_OBJECT (composite));
563 gdk_draw_image (GdkDrawable *drawable,
573 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
574 g_return_if_fail (image != NULL);
575 g_return_if_fail (GDK_IS_GC (gc));
578 width = image->width;
580 height = image->height;
582 GDK_DRAWABLE_GET_CLASS (drawable)->draw_image (drawable, gc, image, xsrc, ysrc,
583 xdest, ydest, width, height);
588 * @drawable: Destination drawable.
589 * @gc: a #GdkGC, used for clipping, or %NULL
590 * @pixbuf: a #GdkPixbuf
591 * @src_x: Source X coordinate within pixbuf.
592 * @src_y: Source Y coordinates within pixbuf.
593 * @dest_x: Destination X coordinate within drawable.
594 * @dest_y: Destination Y coordinate within drawable.
595 * @width: Width of region to render, in pixels, or -1 to use pixbuf width.
596 * @height: Height of region to render, in pixels, or -1 to use pixbuf height.
597 * @dither: Dithering mode for GdkRGB.
598 * @x_dither: X offset for dither.
599 * @y_dither: Y offset for dither.
601 * Renders a rectangular portion of a pixbuf to a drawable. The destination
602 * drawable must have a colormap. All windows have a colormap, however, pixmaps
603 * only have colormap by default if they were created with a non-NULL window argument.
604 * Otherwise a colormap must be set on them with gdk_drawable_set_colormap.
606 * On older X servers, rendering pixbufs with an alpha channel involves round trips
607 * to the X server, and may be somewhat slow.
610 _gdk_draw_pixbuf (GdkDrawable *drawable,
623 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
624 g_return_if_fail (gc == NULL || GDK_IS_GC (gc));
625 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
628 width = gdk_pixbuf_get_width (pixbuf);
630 height = gdk_pixbuf_get_height (pixbuf);
632 GDK_DRAWABLE_GET_CLASS (drawable)->_draw_pixbuf (drawable, gc, pixbuf,
633 src_x, src_y, dest_x, dest_y, width, height,
634 dither, x_dither, y_dither);
638 gdk_draw_points (GdkDrawable *drawable,
643 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
644 g_return_if_fail ((points != NULL) && (npoints > 0));
645 g_return_if_fail (GDK_IS_GC (gc));
646 g_return_if_fail (npoints >= 0);
651 GDK_DRAWABLE_GET_CLASS (drawable)->draw_points (drawable, gc, points, npoints);
655 gdk_draw_segments (GdkDrawable *drawable,
660 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
665 g_return_if_fail (segs != NULL);
666 g_return_if_fail (GDK_IS_GC (gc));
667 g_return_if_fail (nsegs >= 0);
669 GDK_DRAWABLE_GET_CLASS (drawable)->draw_segments (drawable, gc, segs, nsegs);
673 gdk_draw_lines (GdkDrawable *drawable,
679 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
680 g_return_if_fail (points != NULL);
681 g_return_if_fail (GDK_IS_GC (gc));
682 g_return_if_fail (npoints >= 0);
687 GDK_DRAWABLE_GET_CLASS (drawable)->draw_lines (drawable, gc, points, npoints);
692 * @drawable: a #GdkDrawable
694 * @font: font to be used
695 * @x: X coordinate of baseline origin
696 * @y: Y coordinate of baseline origin
697 * @glyphs: glyphs to render
699 * This is a low-level function; 99% of text rendering should be done
700 * using gdk_draw_layout() instead.
702 * A glyph is a character in a font. This function draws a sequence of
703 * glyphs. To obtain a sequence of glyphs you have to understand a
704 * lot about internationalized text handling, which you don't want to
705 * understand; thus, use gdk_draw_layout() instead of this function,
706 * gdk_draw_layout() handles the details.
710 gdk_draw_glyphs (GdkDrawable *drawable,
715 PangoGlyphString *glyphs)
717 g_return_if_fail (GDK_IS_DRAWABLE (drawable));
718 g_return_if_fail (GDK_IS_GC (gc));
721 GDK_DRAWABLE_GET_CLASS (drawable)->draw_glyphs (drawable, gc, font, x, y, glyphs);
726 * _gdk_drawable_copy_to_image:
727 * @drawable: a #GdkDrawable
728 * @image: a #GdkDrawable, or %NULL if a new @image should be created.
729 * @src_x: x coordinate on @drawable
730 * @src_y: y coordinate on @drawable
731 * @dest_x: x coordinate within @image. Must be 0 if @image is %NULL
732 * @dest_y: y coordinate within @image. Must be 0 if @image is %NULL
733 * @width: width of region to get
734 * @height: height or region to get
736 * Copies a portion of @drawable into the client side image structure
737 * @image. If @image is %NULL, creates a new image of size @width x @height
738 * and copies into that. See gdk_drawable_get_image() for further details.
740 * Return value: @image, or a new a #GdkImage containing the contents
744 _gdk_drawable_copy_to_image (GdkDrawable *drawable,
753 GdkDrawable *composite;
754 gint composite_x_offset = 0;
755 gint composite_y_offset = 0;
759 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
760 g_return_val_if_fail (src_x >= 0, NULL);
761 g_return_val_if_fail (src_y >= 0, NULL);
763 /* FIXME? Note race condition since we get the size then
764 * get the image, and the size may have changed.
767 if (width < 0 || height < 0)
768 gdk_drawable_get_size (drawable,
769 width < 0 ? &width : NULL,
770 height < 0 ? &height : NULL);
773 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
777 &composite_y_offset);
779 retval = GDK_DRAWABLE_GET_CLASS (composite)->_copy_to_image (composite,
781 src_x - composite_x_offset,
782 src_y - composite_y_offset,
786 g_object_unref (G_OBJECT (composite));
788 if (!image && retval)
790 cmap = gdk_drawable_get_colormap (drawable);
793 gdk_image_set_colormap (retval, cmap);
800 * gdk_drawable_get_image:
801 * @drawable: a #GdkDrawable
802 * @x: x coordinate on @drawable
803 * @y: y coordinate on @drawable
804 * @width: width of region to get
805 * @height: height or region to get
807 * A #GdkImage stores client-side image data (pixels). In contrast,
808 * #GdkPixmap and #GdkWindow are server-side
809 * objects. gdk_drawable_get_image() obtains the pixels from a
810 * server-side drawable as a client-side #GdkImage. The format of a
811 * #GdkImage depends on the #GdkVisual of the current display, which
812 * makes manipulating #GdkImage extremely difficult; therefore, in
813 * most cases you should use gdk_pixbuf_get_from_drawable() instead of
814 * this lower-level function. A #GdkPixbuf contains image data in a
815 * canonicalized RGB format, rather than a display-dependent format.
816 * Of course, there's a convenience vs. speed tradeoff here, so you'll
817 * want to think about what makes sense for your application.
819 * @x, @y, @width, and @height define the region of @drawable to
820 * obtain as an image.
822 * You would usually copy image data to the client side if you intend
823 * to examine the values of individual pixels, for example to darken
824 * an image or add a red tint. It would be prohibitively slow to
825 * make a round-trip request to the windowing system for each pixel,
826 * so instead you get all of them at once, modify them, then copy
827 * them all back at once.
829 * If the X server or other windowing system backend is on the local
830 * machine, this function may use shared memory to avoid copying
833 * If the source drawable is a #GdkWindow and partially offscreen
834 * or obscured, then the obscured portions of the returned image
835 * will contain undefined data.
837 * Return value: a #GdkImage containing the contents of @drawable
840 gdk_drawable_get_image (GdkDrawable *drawable,
846 GdkDrawable *composite;
847 gint composite_x_offset = 0;
848 gint composite_y_offset = 0;
852 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
853 g_return_val_if_fail (x >= 0, NULL);
854 g_return_val_if_fail (y >= 0, NULL);
856 /* FIXME? Note race condition since we get the size then
857 * get the image, and the size may have changed.
860 if (width < 0 || height < 0)
861 gdk_drawable_get_size (drawable,
862 width < 0 ? &width : NULL,
863 height < 0 ? &height : NULL);
866 GDK_DRAWABLE_GET_CLASS (drawable)->get_composite_drawable (drawable,
870 &composite_y_offset);
872 retval = GDK_DRAWABLE_GET_CLASS (composite)->get_image (composite,
873 x - composite_x_offset,
874 y - composite_y_offset,
877 g_object_unref (G_OBJECT (composite));
879 cmap = gdk_drawable_get_colormap (drawable);
882 gdk_image_set_colormap (retval, cmap);
888 gdk_drawable_real_get_image (GdkDrawable *drawable,
894 return _gdk_drawable_copy_to_image (drawable, NULL, x, y, 0, 0, width, height);
898 gdk_drawable_real_get_composite_drawable (GdkDrawable *drawable,
903 gint *composite_x_offset,
904 gint *composite_y_offset)
906 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
908 *composite_x_offset = 0;
909 *composite_y_offset = 0;
911 return GDK_DRAWABLE (g_object_ref (G_OBJECT (drawable)));
915 * gdk_drawable_get_clip_region:
916 * @drawable: a #GdkDrawable
918 * Computes the region of a drawable that potentially can be written
919 * to by drawing primitives. This region will not take into account
920 * the clip region for the GC, and may also not take into account
921 * other factors such as if the window is obscured by other windows,
922 * but no area outside of this region will be affected by drawing
925 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
929 gdk_drawable_get_clip_region (GdkDrawable *drawable)
931 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
933 return GDK_DRAWABLE_GET_CLASS (drawable)->get_clip_region (drawable);
937 * gdk_drawable_get_visible_region:
940 * Computes the region of a drawable that is potentially visible.
941 * This does not necessarily take into account if the window is
942 * obscured by other windows, but no area outside of this region
945 * Return value: a #GdkRegion. This must be freed with gdk_region_destroy()
949 gdk_drawable_get_visible_region (GdkDrawable *drawable)
951 g_return_val_if_fail (GDK_IS_DRAWABLE (drawable), NULL);
953 return GDK_DRAWABLE_GET_CLASS (drawable)->get_visible_region (drawable);
957 gdk_drawable_real_get_visible_region (GdkDrawable *drawable)
964 gdk_drawable_get_size (drawable, &rect.width, &rect.height);
966 return gdk_region_rectangle (&rect);
970 composite (guchar *src_buf,
977 guchar *src = src_buf;
978 guchar *dest = dest_buf;
991 t = a * p[0] + (255 - a) * q[0] + 0x80;
992 q[0] = (t + (t >> 8)) >> 8;
993 t = a * p[1] + (255 - a) * q[1] + 0x80;
994 q[1] = (t + (t >> 8)) >> 8;
995 t = a * p[2] + (255 - a) * q[2] + 0x80;
996 q[2] = (t + (t >> 8)) >> 8;
1002 src += src_rowstride;
1003 dest += dest_rowstride;
1008 composite_0888 (guchar *src_buf,
1011 gint dest_rowstride,
1012 GdkByteOrder dest_byte_order,
1016 guchar *src = src_buf;
1017 guchar *dest = dest_buf;
1021 gint twidth = width;
1025 if (dest_byte_order == GDK_LSB_FIRST)
1031 t = p[3] * p[2] + (255 - p[3]) * q[0] + 0x80;
1032 q[0] = (t + (t >> 8)) >> 8;
1033 t = p[3] * p[1] + (255 - p[3]) * q[1] + 0x80;
1034 q[1] = (t + (t >> 8)) >> 8;
1035 t = p[3] * p[0] + (255 - p[3]) * q[2] + 0x80;
1036 q[2] = (t + (t >> 8)) >> 8;
1047 t = p[3] * p[0] + (255 - p[3]) * q[1] + 0x80;
1048 q[1] = (t + (t >> 8)) >> 8;
1049 t = p[3] * p[1] + (255 - p[3]) * q[2] + 0x80;
1050 q[2] = (t + (t >> 8)) >> 8;
1051 t = p[3] * p[2] + (255 - p[3]) * q[3] + 0x80;
1052 q[3] = (t + (t >> 8)) >> 8;
1058 src += src_rowstride;
1059 dest += dest_rowstride;
1064 composite_565 (guchar *src_buf,
1067 gint dest_rowstride,
1068 GdkByteOrder dest_byte_order,
1072 guchar *src = src_buf;
1073 guchar *dest = dest_buf;
1077 gint twidth = width;
1079 gushort *q = (gushort *)dest;
1085 guint tr1, tg1, tb1;
1089 /* This is fast, and corresponds to what composite() above does
1090 * if we converted to 8-bit first.
1092 tr = (tmp & 0xf800);
1093 tr1 = a * p[0] + (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1094 tg = (tmp & 0x07e0);
1095 tg1 = a * p[1] + (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1096 tb = (tmp & 0x001f);
1097 tb1 = a * p[2] + (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1099 *q = (((tr1 + (tr1 >> 8)) & 0xf800) |
1100 (((tg1 + (tg1 >> 8)) & 0xfc00) >> 5) |
1101 ((tb1 + (tb1 >> 8)) >> 11));
1103 /* This version correspond to the result we get with XRENDER -
1104 * a bit of precision is lost since we convert to 8 bit after premultiplying
1105 * instead of at the end
1107 guint tr2, tg2, tb2;
1108 guint tr3, tg3, tb3;
1110 tr = (tmp & 0xf800);
1111 tr1 = (255 - a) * ((tr >> 8) + (tr >> 13)) + 0x80;
1112 tr2 = a * p[0] + 0x80;
1113 tr3 = ((tr1 + (tr1 >> 8)) >> 8) + ((tr2 + (tr2 >> 8)) >> 8);
1115 tg = (tmp & 0x07e0);
1116 tg1 = (255 - a) * ((tg >> 3) + (tg >> 9)) + 0x80;
1117 tg2 = a * p[0] + 0x80;
1118 tg3 = ((tg1 + (tg1 >> 8)) >> 8) + ((tg2 + (tg2 >> 8)) >> 8);
1120 tb = (tmp & 0x001f);
1121 tb1 = (255 - a) * ((tb << 3) + (tb >> 2)) + 0x80;
1122 tb2 = a * p[0] + 0x80;
1123 tb3 = ((tb1 + (tb1 >> 8)) >> 8) + ((tb2 + (tb2 >> 8)) >> 8);
1125 *q = (((tr3 & 0xf8) << 8) |
1126 ((tg3 & 0xfc) << 3) |
1134 src += src_rowstride;
1135 dest += dest_rowstride;
1140 gdk_drawable_real_draw_pixbuf (GdkDrawable *drawable,
1149 GdkRgbDither dither,
1153 gboolean free_gc = FALSE;
1154 GdkPixbuf *composited = NULL;
1155 gint dwidth, dheight;
1158 GdkRectangle tmp_rect;
1160 g_return_if_fail (GDK_IS_PIXBUF (pixbuf));
1161 g_return_if_fail (pixbuf->colorspace == GDK_COLORSPACE_RGB);
1162 g_return_if_fail (pixbuf->n_channels == 3 || pixbuf->n_channels == 4);
1163 g_return_if_fail (pixbuf->bits_per_sample == 8);
1165 g_return_if_fail (drawable != NULL);
1168 width = pixbuf->width;
1170 height = pixbuf->height;
1172 g_return_if_fail (width >= 0 && height >= 0);
1173 g_return_if_fail (src_x >= 0 && src_x + width <= pixbuf->width);
1174 g_return_if_fail (src_y >= 0 && src_y + height <= pixbuf->height);
1176 /* Clip to the drawable; this is required for get_from_drawable() so
1177 * can't be done implicitly
1194 gdk_drawable_get_size (drawable, &dwidth, &dheight);
1196 if ((dest_x + width) > dwidth)
1197 width = dwidth - dest_x;
1199 if ((dest_y + height) > dheight)
1200 height = dheight - dest_y;
1202 if (width <= 0 || height <= 0)
1205 /* Clip to the clip region; this avoids getting more
1206 * image data from the server than we need to.
1209 tmp_rect.x = dest_x;
1210 tmp_rect.y = dest_y;
1211 tmp_rect.width = width;
1212 tmp_rect.height = height;
1214 drect = gdk_region_rectangle (&tmp_rect);
1215 clip = gdk_drawable_get_clip_region (drawable);
1217 gdk_region_intersect (drect, clip);
1219 gdk_region_get_clipbox (drect, &tmp_rect);
1221 gdk_region_destroy (drect);
1222 gdk_region_destroy (clip);
1224 if (tmp_rect.width == 0 ||
1225 tmp_rect.height == 0)
1232 gc = gdk_gc_new (drawable);
1236 if (pixbuf->has_alpha)
1238 GdkVisual *visual = gdk_drawable_get_visual (drawable);
1239 void (*composite_func) (guchar *src_buf,
1242 gint dest_rowstride,
1243 GdkByteOrder dest_byte_order,
1245 gint height) = NULL;
1247 /* First we see if we have a visual-specific composition function that can composite
1248 * the pixbuf data directly onto the image
1252 gint bits_per_pixel = _gdk_windowing_get_bits_for_depth (gdk_drawable_get_display (drawable),
1255 if (visual->byte_order == (G_BYTE_ORDER == G_BIG_ENDIAN ? GDK_MSB_FIRST : GDK_LSB_FIRST) &&
1256 visual->depth == 16 &&
1257 visual->red_mask == 0xf800 &&
1258 visual->green_mask == 0x07e0 &&
1259 visual->blue_mask == 0x001f)
1260 composite_func = composite_565;
1261 else if (visual->depth == 24 && bits_per_pixel == 32 &&
1262 visual->red_mask == 0xff0000 &&
1263 visual->green_mask == 0x00ff00 &&
1264 visual->blue_mask == 0x0000ff)
1265 composite_func = composite_0888;
1268 /* We can't use our composite func if we are required to dither
1270 if (composite_func && !(dither == GDK_RGB_DITHER_MAX && visual->depth != 24))
1273 for (y0 = 0; y0 < height; y0 += GDK_SCRATCH_IMAGE_HEIGHT)
1275 gint height1 = MIN (height - y0, GDK_SCRATCH_IMAGE_HEIGHT);
1276 for (x0 = 0; x0 < width; x0 += GDK_SCRATCH_IMAGE_WIDTH)
1280 gint width1 = MIN (width - x0, GDK_SCRATCH_IMAGE_WIDTH);
1282 GdkImage *image = _gdk_image_get_scratch (gdk_drawable_get_screen (drawable),
1284 gdk_drawable_get_depth (drawable), &xs0, &ys0);
1286 _gdk_drawable_copy_to_image (drawable, image,
1287 dest_x + x0, dest_y + y0,
1290 (*composite_func) (pixbuf->pixels + (src_y + y0) * pixbuf->rowstride + (src_x + x0) * 4,
1292 (guchar*)image->mem + ys0 * image->bpl + xs0 * image->bpp,
1296 gdk_draw_image (drawable, gc, image,
1298 dest_x + x0, dest_y + y0,
1307 /* No special composition func, convert dest to 24 bit RGB data, composite against
1308 * that, and convert back.
1310 composited = gdk_pixbuf_get_from_drawable (NULL,
1318 composite (pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4,
1321 composited->rowstride,
1330 pixbuf = composited;
1333 if (pixbuf->n_channels == 4)
1335 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 4;
1337 gdk_draw_rgb_32_image_dithalign (drawable, gc,
1341 buf, pixbuf->rowstride,
1342 x_dither, y_dither);
1344 else /* n_channels == 3 */
1346 guchar *buf = pixbuf->pixels + src_y * pixbuf->rowstride + src_x * 3;
1348 gdk_draw_rgb_image_dithalign (drawable, gc,
1352 buf, pixbuf->rowstride,
1353 x_dither, y_dither);
1358 g_object_unref (G_OBJECT (composited));