Page 1 of 1

Patch for Premulitplying Alpha with RSVG

Posted: 2009-11-19T14:22:38-07:00
by mikerlewis
Hi, I had found a bug with image output with RSVG having the alpha premultiplied. (viewtopic.php?f=3&t=15058).

It turns out that cairo_image_surface_create_for_data with the image format CAIRO_FORMAT_ARGB32 returns pixels with the alpha premultiplied, but ImageMagick was treating them as if they weren't premultiplied.

Here's a fix. Not sure if it's the cleanest, but if it's using MAGICKCORE_CAIRO_DELEGATE it will unpremultiply the pixels. It's made from trunk. I'd hope to get this or something to the same effect upstream because it would fix a lot of issues we're having.

Code: Select all

diff --git a/coders/svg.c b/coders/svg.c
index a68dec0..8a5eca5 100644
--- a/coders/svg.c
+++ b/coders/svg.c
@@ -2852,6 +2852,17 @@ static Image *ReadSVGImage(const ImageInfo *image_info,ExceptionInfo *exception)
           fill_color.blue=ScaleCharToQuantum(*p++);
 #endif
           fill_color.opacity=QuantumRange-ScaleCharToQuantum(*p++);
+
+/*
+ * If we're using Cairo, we've already gotten back an image that has premultiplied alpha
+ * (see CAIRO_FORMAT_ARGB32 http://library.gnome.org/devel/cairo/stable/cairo-image-surface.html#cairo-format-t)
+ */
+#if defined(MAGICKCORE_CAIRO_DELEGATE)
+          fill_color.blue  /= 1.0-QuantumScale*fill_color.opacity;
+          fill_color.green /= 1.0-QuantumScale*fill_color.opacity;
+          fill_color.red   /= 1.0-QuantumScale*fill_color.opacity;
+#endif
+
           MagickCompositeOver(&fill_color,fill_color.opacity,q,(MagickRealType)
             q->opacity,q);
           q++;

Thanks,
Mike Lewis

Re: Patch for Premulitplying Alpha with RSVG

Posted: 2009-11-19T18:57:28-07:00
by magick
We'll get your patch in the next point release of ImageMagick. Thanks.