[gtk+/rendering-cleanup: 31/144] API: Add gdk_cairo_region_create_from_surface()
- From: Benjamin Otte <otte src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk+/rendering-cleanup: 31/144] API: Add gdk_cairo_region_create_from_surface()
- Date: Thu, 23 Sep 2010 21:19:59 +0000 (UTC)
commit 0ff15d5d1ffe5da28604b9742edc08546e634d13
Author: Benjamin Otte <otte redhat com>
Date: Sat Aug 14 04:40:52 2010 +0200
API: Add gdk_cairo_region_create_from_surface()
The function converts the given surface into an alpha bitmap mask. This
is mostly useful for setting shape regions.
Also adds a new internal function _gdk_cairo_surface_extents() that
computes a surface's extents.
docs/reference/gdk/gdk3-sections.txt | 1 +
gdk/gdk.symbols | 1 +
gdk/gdkcairo.c | 141 ++++++++++++++++++++++++++++++++++
gdk/gdkcairo.h | 4 +
gdk/gdkinternals.h | 2 +
5 files changed, 149 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gdk/gdk3-sections.txt b/docs/reference/gdk/gdk3-sections.txt
index 554d5c8..0f4534e 100644
--- a/docs/reference/gdk/gdk3-sections.txt
+++ b/docs/reference/gdk/gdk3-sections.txt
@@ -656,6 +656,7 @@ gdk_cairo_set_source_pixmap
gdk_cairo_rectangle
gdk_cairo_region
gdk_cairo_reset_clip
+gdk_cairo_region_create_from_surface
</SECTION>
<SECTION>
diff --git a/gdk/gdk.symbols b/gdk/gdk.symbols
index a040f42..9d048fe 100644
--- a/gdk/gdk.symbols
+++ b/gdk/gdk.symbols
@@ -293,6 +293,7 @@ gdk_cairo_set_source_pixbuf
gdk_cairo_set_source_pixmap
gdk_cairo_rectangle
gdk_cairo_region
+gdk_cairo_region_create_from_surface
#endif
#endif
diff --git a/gdk/gdkcairo.c b/gdk/gdkcairo.c
index c5ead4e..c5a55c5 100644
--- a/gdk/gdkcairo.c
+++ b/gdk/gdkcairo.c
@@ -18,6 +18,9 @@
*/
#include "gdkcairo.h"
+
+#include <math.h>
+
#include "gdkdrawable.h"
#include "gdkinternals.h"
@@ -287,3 +290,141 @@ gdk_cairo_set_source_pixmap (cairo_t *cr,
cairo_set_source_surface (cr, surface, pixmap_x, pixmap_y);
cairo_surface_destroy (surface);
}
+
+/**
+ * _gdk_cairo_surface_extents:
+ * @surface: surface to measure
+ * @extents: (out): rectangle to put the extents
+ *
+ * Measures the area covered by @surface and puts it into @extents.
+ * Note that this function respects device offsets set on @surface.
+ * if @surface is unbounded, the resulting extents will be empty and
+ * not be a maximal sized rectangle. This is to avoid careless coding.
+ * You must explicitly check the return value of you want to handle
+ * that case.
+ *
+ * Returns: %TRUE if the extents fit in a #GdkRectangle, %FALSE if not.
+ **/
+gboolean
+_gdk_cairo_surface_extents (cairo_surface_t *surface,
+ GdkRectangle *extents)
+{
+ double x1, x2, y1, y2;
+ cairo_t *cr;
+
+ g_return_val_if_fail (surface != NULL, FALSE);
+ g_return_val_if_fail (extents != NULL, FALSE);
+
+ cr = cairo_create (surface);
+ cairo_clip_extents (cr, &x1, &y1, &x2, &y2);
+
+ x1 = floor (x1);
+ y1 = floor (y1);
+ x2 = ceil (x2);
+ y2 = ceil (y2);
+ x2 -= x1;
+ y2 -= y1;
+
+ if (x1 < G_MININT || x1 > G_MAXINT ||
+ y1 < G_MININT || y1 > G_MAXINT ||
+ x2 > G_MAXINT || y2 > G_MAXINT)
+ {
+ extents->x = extents->y = extents->width = extents->height = 0;
+ return FALSE;
+ }
+
+ extents->x = x1;
+ extents->y = y1;
+ extents->width = x2;
+ extents->height = y2;
+
+ return TRUE;
+}
+
+/* This function originally from Jean-Edouard Lachand-Robert, and
+ * available at www.codeguru.com. Simplified for our needs, not sure
+ * how much of the original code left any longer. Now handles just
+ * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
+ * bitmaps (and not pixmaps), with zero pixels being transparent.
+ */
+/**
+ * gdk_cairo_region_create_from_surface:
+ * @surface: A surface
+ *
+ * Creates region that describes covers the area where the given @surface
+ * is more than 50% opaque. This function takes into account device
+ * offsets that might be set with cairo_surface_set_device_offset().
+ *
+ * Returns: A new region
+ **/
+cairo_region_t *
+gdk_cairo_region_create_from_surface (cairo_surface_t *surface)
+{
+ cairo_region_t *region;
+ GdkRectangle extents, rect;
+ cairo_surface_t *image;
+ cairo_t *cr;
+ gint x, y, stride;
+ guchar *data;
+
+ _gdk_cairo_surface_extents (surface, &extents);
+
+ if (cairo_surface_get_content (surface) == CAIRO_CONTENT_COLOR)
+ return cairo_region_create_rectangle (&extents);
+
+ if (cairo_surface_get_type (surface) != CAIRO_SURFACE_TYPE_IMAGE ||
+ cairo_image_surface_get_format (surface) != CAIRO_FORMAT_A1)
+ {
+ /* coerce to an A1 image */
+ image = cairo_image_surface_create (CAIRO_FORMAT_A1,
+ extents.width, extents.height);
+ cr = cairo_create (image);
+ cairo_set_source_surface (cr, surface, extents.x, extents.y);
+ cairo_paint (cr);
+ cairo_destroy (cr);
+ }
+ else
+ image = cairo_surface_reference (surface);
+
+ data = cairo_image_surface_get_data (image);
+ stride = cairo_image_surface_get_stride (image);
+
+ region = cairo_region_create ();
+
+ for (y = 0; y < extents.height; y++)
+ {
+ for (x = 0; x < extents.width; x++)
+ {
+ /* Search for a continuous range of "non transparent pixels"*/
+ gint x0 = x;
+ while (x < extents.width)
+ {
+ if (((data[x / 8] >> (x%8)) & 1) == 0)
+ /* This pixel is "transparent"*/
+ break;
+ x++;
+ }
+
+ if (x > x0)
+ {
+ /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
+ * in the region
+ */
+ rect.x = x0;
+ rect.width = x - x0;
+ rect.y = y;
+ rect.height = 1;
+
+ cairo_region_union_rectangle (region, &rect);
+ }
+ }
+ data += stride;
+ }
+
+ cairo_surface_destroy (image);
+
+ cairo_region_translate (region, extents.x, extents.y);
+
+ return region;
+}
+
diff --git a/gdk/gdkcairo.h b/gdk/gdkcairo.h
index 4ecb037..c2a71e8 100644
--- a/gdk/gdkcairo.h
+++ b/gdk/gdkcairo.h
@@ -50,6 +50,10 @@ void gdk_cairo_rectangle (cairo_t *cr,
void gdk_cairo_region (cairo_t *cr,
const cairo_region_t *region);
+cairo_region_t *
+ gdk_cairo_region_create_from_surface
+ (cairo_surface_t *surface);
+
G_END_DECLS
#endif /* __GDK_CAIRO_H__ */
diff --git a/gdk/gdkinternals.h b/gdk/gdkinternals.h
index 7740bce..eda1e55 100644
--- a/gdk/gdkinternals.h
+++ b/gdk/gdkinternals.h
@@ -313,6 +313,8 @@ void gdk_synthesize_window_state (GdkWindow *window,
GdkDeviceManager * _gdk_device_manager_new (GdkDisplay *display);
+gboolean _gdk_cairo_surface_extents (cairo_surface_t *surface,
+ GdkRectangle *extents);
cairo_surface_t *_gdk_drawable_ref_cairo_surface (GdkDrawable *drawable);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]