[librsvg] Use cairo_image_surface_create() directly



commit 6ceaecc069bdc606f9cbf9ed3bcd57fb6861f4f0
Author: Christian Persch <chpe gnome org>
Date:   Sat Nov 26 22:35:11 2011 +0100

    Use cairo_image_surface_create() directly
    
    Instead of allocating the pixels then creating a surface from data,
    just create the surface directly. This has the added benefit that
    cairo checks the for valid width and height.

 rsvg-cairo-draw.c |   37 +++++++++++++++++++------------------
 1 files changed, 19 insertions(+), 18 deletions(-)
---
diff --git a/rsvg-cairo-draw.c b/rsvg-cairo-draw.c
index 2611f88..fe93c36 100644
--- a/rsvg-cairo-draw.c
+++ b/rsvg-cairo-draw.c
@@ -565,9 +565,18 @@ rsvg_cairo_render_image (RsvgDrawingCtx * ctx, const GdkPixbuf * pixbuf,
     if (pixbuf == NULL)
         return;
 
-    cairo_pixels = g_try_malloc (4 * width * height);
-    if (!cairo_pixels)
+    if (n_channels == 3)
+        format = CAIRO_FORMAT_RGB24;
+    else
+        format = CAIRO_FORMAT_ARGB32;
+
+    surface = cairo_image_surface_create (format, width, height);
+    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
+        cairo_surface_destroy (surface);
         return;
+    }
+
+    cairo_pixels = cairo_image_surface_get_data (surface);
 
     rsvg_bbox_init (&bbox, &state->affine);
     bbox.rect.x = pixbuf_x;
@@ -581,15 +590,6 @@ rsvg_cairo_render_image (RsvgDrawingCtx * ctx, const GdkPixbuf * pixbuf,
     pixbuf_x *= dwidth / w;
     pixbuf_y *= dheight / h;
 
-    if (n_channels == 3)
-        format = CAIRO_FORMAT_RGB24;
-    else
-        format = CAIRO_FORMAT_ARGB32;
-
-    surface = cairo_image_surface_create_for_data ((unsigned char *) cairo_pixels,
-                                                   format, width, height, 4 * width);
-    cairo_surface_set_user_data (surface, &surface_pixel_data_key, cairo_pixels, (cairo_destroy_func_t) g_free);
-
     for (j = height; j; j--) {
         guchar *p = gdk_pixels;
         guchar *q = cairo_pixels;
@@ -680,9 +680,14 @@ rsvg_cairo_generate_mask (cairo_t * cr, RsvgMask * self, RsvgDrawingCtx * ctx, R
     double sx, sy, sw, sh;
     gboolean nest = cr != render->initial_cr;
 
-    pixels = g_try_malloc0 (height * rowstride);
-    if (pixels == NULL)
-          return;
+    surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, width, height);
+    if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS) {
+        cairo_surface_destroy (surface);
+        return;
+    }
+
+    pixels = cairo_image_surface_get_data (surface);
+    rowstride = cairo_image_surface_get_stride (surface);
 
     if (self->maskunits == objectBoundingBox)
         _rsvg_push_view_box (ctx, 1, 1);
@@ -695,10 +700,6 @@ rsvg_cairo_generate_mask (cairo_t * cr, RsvgMask * self, RsvgDrawingCtx * ctx, R
     if (self->maskunits == objectBoundingBox)
         _rsvg_pop_view_box (ctx);
 
-    surface = cairo_image_surface_create_for_data (pixels,
-                                                   CAIRO_FORMAT_ARGB32, width, height, rowstride);
-    cairo_surface_set_user_data (surface, &surface_pixel_data_key, pixels, (cairo_destroy_func_t) g_free);
-
     mask_cr = cairo_create (surface);
     save_cr = render->cr;
     render->cr = mask_cr;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]