Page 1 of 2

[SOLVED] RGBA issues with Draw(text)

Posted: 2015-05-31T20:39:08-07:00
by olear
Hi,

Currently using ImageMagick as an backend for various video plugins. And run into a problem with my Text generator.

The Text Generator works as following (only the important stuff):

Code: Select all

Magick::Image image(Magick::Geometry(width,height),Magick::Color("rgba(0,0,0,0)"));
image.fontPointSize(fontSize);
std::list<Magick::Drawable> text_draw_list;
text_draw_list.push_back(Magick::DrawableFont(fontName));
text_draw_list.push_back(Magick::DrawableText(xtext, ytext, text));
text_draw_list.push_back(Magick::DrawableFillColor(textRGBA));
image.draw(text_draw_list);
image.write(0,0,width,height,channels,Magick::FloatPixel,(float*)POINTER_DST);
Image
This works, but if I add a image:

Code: Select all

Magick::Image image(Magick::Geometry(width,height),Magick::Color("rgba(0,0,0,0)"));
image.read(width,height,channels,Magick::FloatPixel,(float*)POINTER_SRC);
image.fontPointSize(fontSize);
std::list<Magick::Drawable> text_draw_list;
text_draw_list.push_back(Magick::DrawableFont(fontName));
text_draw_list.push_back(Magick::DrawableText(xtext, ytext, text));
text_draw_list.push_back(Magick::DrawableFillColor(textRGBA));
image.draw(text_draw_list);
image.write(0,0,width,height,channels,Magick::FloatPixel,(float*)POINTER_DST);
If the input is RGB (perfect):
Image
If the input is RGBA (issue):
Image

If I recreate this example with png as input and output it works.

Any feedback, suggestions, patches welcome, can also provide more information if needed.

Tested 6.8.9-10-6.9.1-3, 6.9.0-3/5 don't have the issue, but the font is not anti-aliased (very jagged).

Also noted that using composite with only RGBA images also results in strange behaviour.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T09:17:04-07:00
by fmw42
I believe that the font aliasing is fixed in either 6.9.1.4 or most likely or being fixed in 6.9.1.5. If not the former, check the beta of the latter.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T14:39:27-07:00
by olear
Thanks for you reply, but 6.9.1-4 actually broke anti-aliased text, and RGBA is worse.

No background (text is now jagged, this worked in -2 and -3, now broken in -4):
Image

RGBA background (worse than -2 and -3):
Image

6.9.1-2 is the last version where text works, but transparency issues is present in all versions in 6.9.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T14:53:05-07:00
by magick
The current release is 6.9.1-4. If this release fails for you, let us know. Ideally you would provide a way to reproduce the problem from the command-line so we can investigate / fix the bug.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T14:58:28-07:00
by snibgo
I don't use the CPP interface, so can't help there.

I'm puzzled about the RGB/RGBA distinction. When IM reads an image, it is stored in RGBA format. The presence or absence of alpha in the input image should make no difference. Alpha may be "on" or "off", but that shouldn't cause this problem.

In the OP, the third image has each letter of "Enter text" in front of a dark colour. That colour isn't constant between characters or even within one character. I can't see where that colour has come from.

The text "Position" doesn't have this problem. You show some code that seems to draw one of these text strings, but not both. Which text do you draw first?

Can you publish the background image, as read by your code?

I don't understand your comment, "If I recreate this example with png as input and output it works." Do you mean at the command line? Does that do exactly the same as your code?

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T15:21:18-07:00
by olear
Snibgo: If I use PNG instead of RGBA32f raw (what I use) as input/output then everything works, I don't use the commandline.

If the input image is RGB then everything is ok, if the input image is RGBA then I get issues, if I remove the matte then everything works. This also happens when using composite, if the images are RGBA then I get unwanted result.

Can try to make a small example (c++), but does not seem to happen if the input/output is a image file(png, jpg etc).

EDIT:
The text "Position" doesn't have this problem. You show some code that seems to draw one of these text strings, but not both. Which text do you draw first?
That text is from the software, "Enter Text" is generated from ImageMagick.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T15:37:49-07:00
by snibgo
"RGBA32f" -- this sounds like 32 bits/channel/pixel, floating-point, so I suppose you are using Q32 HDRI. Not many people use this, so it sometimes throws a bug that isn't present in Q16.

Can you save the image, just before the text is written, as MIFF? Then write another file, just after the text is written? Check that the second shows the problem.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T15:44:17-07:00
by olear
I use Q16-HDRI and Q32-HDRI, same issue.

Will do some image writes and upload.

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T16:21:38-07:00
by olear
MIFF's from 6.9.1-2 and 6.9.1-4: https://fxarena.net/~olear/misc/bug.tgz

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T16:53:30-07:00
by magick
We can't help unless we can reproduce the problem with the command line or a complete small program that illustrates the flaw. We're getting proper aliasing, for example, with this command:
  • convert -size 600x600 xc:none -pointsize 60 -gravity center -fill cyan -annotate +0+0 "Enter text" text.png
We get expected results for PNG and MIFF. Now let's composite the text onto a solid background:
  • convert \( -size 600x600 xc:black \) \( -size 600x600 xc:none -pointsize 60 -gravity center -fill cyan \
    -annotate +0+0 "Enter text" \) -composite text.png
Again, we get the expected results. Or is there a flaw we're overlooking?

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T17:53:52-07:00
by olear
I noted some changes to font color in -4, and adjusted to changes since -2, and I now get white text with -4, but the text is still not smooth, and if I write to MIFF then the text is smooth. Strange...

Re: RGBA issues with Draw(text)

Posted: 2015-06-01T18:59:55-07:00
by olear
Found a workaround.
Image
If I revert all changes to composite-private.h and readd if fabs then my problems are resolved.

Code: Select all

diff -burN ImageMagick-6.9.1-4/magick/composite-private.h ImageMagick-6.9.1-3/magick/composite-private.h
--- magick/composite-private.h	2015-05-30 02:49:19.000000000 +0200
+++ magick/composite-private.h	2015-04-12 20:09:13.000000000 +0200
@@ -51,9 +51,6 @@
   const MagickRealType alpha,const PixelPacket *q,const MagickRealType beta,
   PixelPacket *composite)
 {
-  double
-    gamma;
-
   MagickRealType
     Da,
     Sa;
@@ -61,31 +58,24 @@
   /*
     Compose pixel p over pixel q with the given opacities.
   */
-  if (fabs((double) (alpha-TransparentOpacity)) < MagickEpsilon)
-    { 
-      *composite=(*q);
-      return;
-    }
   Sa=1.0-QuantumScale*alpha;
   Da=1.0-QuantumScale*beta;
-  gamma=Sa*(-Da)+Sa+Da;
-  gamma=PerceptibleReciprocal(gamma);
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
   SetPixelOpacity(composite,(MagickRealType) (QuantumRange*
     (1.0-RoundToUnity(Sa+Da-Sa*Da))+0.5));
-  SetPixelRed(composite,ClampToQuantum(gamma*MagickOver_((MagickRealType)
-    GetPixelRed(p),alpha,(MagickRealType) GetPixelRed(q),beta)));
-  SetPixelGreen(composite,ClampToQuantum(gamma*MagickOver_((MagickRealType)
-    GetPixelGreen(p),alpha,(MagickRealType) GetPixelGreen(q),beta)));
-  SetPixelBlue(composite,ClampToQuantum(gamma*MagickOver_((MagickRealType)
-    GetPixelBlue(p),alpha,(MagickRealType) GetPixelBlue(q),beta)));
+  SetPixelRed(composite,MagickOver_((MagickRealType)
+    GetPixelRed(p),alpha,(MagickRealType) GetPixelRed(q),beta)+0.5);
+  SetPixelGreen(composite,MagickOver_((MagickRealType)
+    GetPixelGreen(p),alpha,(MagickRealType) GetPixelGreen(q),beta)+0.5);
+  SetPixelBlue(composite,MagickOver_((MagickRealType)
+    GetPixelBlue(p),alpha,(MagickRealType) GetPixelBlue(q),beta)+0.5);
 #else
   SetPixelOpacity(composite,QuantumRange*(1.0-RoundToUnity(Sa+Da-Sa*Da)));
-  SetPixelRed(composite,gamma*MagickOver_((MagickRealType)
+  SetPixelRed(composite,MagickOver_((MagickRealType)
     GetPixelRed(p),alpha,(MagickRealType) GetPixelRed(q),beta));
-  SetPixelGreen(composite,gamma*MagickOver_((MagickRealType)
+  SetPixelGreen(composite,MagickOver_((MagickRealType)
     GetPixelGreen(p),alpha,(MagickRealType) GetPixelGreen(q),beta));
-  SetPixelBlue(composite,gamma*MagickOver_((MagickRealType)
+  SetPixelBlue(composite,MagickOver_((MagickRealType)
     GetPixelBlue(p),alpha,(MagickRealType) GetPixelBlue(q),beta));
 #endif
 }
@@ -94,9 +84,6 @@
   const MagickRealType alpha,const MagickPixelPacket *q,
   const MagickRealType beta,MagickPixelPacket *composite)
 {
-  double
-    gamma;
-
   MagickRealType
     Da,
     Sa;
@@ -106,15 +93,13 @@
   */
   Sa=1.0-QuantumScale*alpha;
   Da=1.0-QuantumScale*beta;
-  gamma=Sa*(-Da)+Sa+Da;
-  gamma=PerceptibleReciprocal(gamma);
   composite->opacity=(MagickRealType) (QuantumRange*(1.0-
     RoundToUnity(Sa+Da-Sa*Da)));
-  composite->red=gamma*MagickOver_(p->red,alpha,q->red,beta);
-  composite->green=gamma*MagickOver_(p->green,alpha,q->green,beta);
-  composite->blue=gamma*MagickOver_(p->blue,alpha,q->blue,beta);
+  composite->red=MagickOver_(p->red,alpha,q->red,beta);
+  composite->green=MagickOver_(p->green,alpha,q->green,beta);
+  composite->blue=MagickOver_(p->blue,alpha,q->blue,beta);
   if (q->colorspace == CMYKColorspace)
-    composite->index=gamma*MagickOver_(p->index,alpha,q->index,beta);
+    composite->index=MagickOver_(p->index,alpha,q->index,beta);
 }
 
 static inline void MagickPixelCompositePlus(const MagickPixelPacket *p,

Code: Select all

--- magick/composite-private.h.orig	2015-06-02 03:46:03.914115735 +0200
+++ magick/composite-private.h	2015-06-02 03:46:36.947582226 +0200
@@ -58,6 +58,12 @@
   /*
     Compose pixel p over pixel q with the given opacities.
   */
+  if (fabs((double) (alpha-TransparentOpacity)) < MagickEpsilon)
+  {
+    *composite=(*q);
+    return;
+  }
+
   Sa=1.0-QuantumScale*alpha;
   Da=1.0-QuantumScale*beta;
 #if !defined(MAGICKCORE_HDRI_SUPPORT)
Version: ImageMagick 6.9.1-4 Q32 x86_64 2015-06-02 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenMP
Delegates (built-in): fontconfig freetype png zlib
EDIT:

But that broke another plugin I got.... never ending story.

Re: RGBA issues with Draw(text)

Posted: 2015-06-02T13:02:23-07:00
by olear
Sorry for possible stupid question, but what's the difference between image.display and image.write. I wonder cause display show jagged fonts if no background, but write does not. Could be related?

Re: RGBA issues with Draw(text)

Posted: 2015-06-03T05:30:36-07:00
by olear
Another example using Arc instead of Draw.

My input image:
Image
Result in 6.9.1-3:
Image
Ignore transparency issue, fixed in 6.9.1-4, but look at the shape, smooth edges.

Result in 6.9.1-4:
Image
The transparency issues are fixes, but notice edge, very jagged, same as with the Draw function with fonts.

Re: RGBA issues with Draw(text)

Posted: 2015-06-03T07:00:26-07:00
by olear
6.9.1-5~beta20150602 reverted back to 6.9.1-3 behaviour on Arc, smooth edges but with transparency issues. Font drawing still jagged.