Patch for Premulitplying Alpha with RSVG

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
mikerlewis

Patch for Premulitplying Alpha with RSVG

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Patch for Premulitplying Alpha with RSVG

Post by magick »

We'll get your patch in the next point release of ImageMagick. Thanks.
Post Reply