possible bug in -border with transparency IM 6.5.2-4

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

possible bug in -border with transparency IM 6.5.2-4

Post by fmw42 »

IM 6.5.2-4 Q16 Mac OSX Tiger.

I was testing my texteffect script with transparent background for a user and ran into this puzzling side-effect. I am basically doing a blur text effect with text color opaque blue rgba(0,0,255,1) and transparent white background rgba(255,255,255,0). The problem is that at the very end, just before the -border step (where I try to add back a transparent white border) all is fine, but afterwards much of the image turns black (like an undercolor of black) after transparency is removed. I cannot figure out if I am making a mistake, do not understand something critical or if there is a bug in -border when used with transparency. I cannot duplicate this by adding, trimming and adding back a transparent white background around the rose image. Thus I am providing the following, which is as simple as I can make it. See the images produced at various stages before and after turning off transparency.


convert -size 139x57 xc:"rgba(255,255,255,0)" \
-fill "rgba(0,0,255,1)" -stroke none \
-font Arial -pointsize 48 \
-gravity center -annotate 0x0+0+0 "TEST" -blur 0x3 -write texteffect_tmp1.png \
-trim +repage -write texteffect_tmp2.png \
-bordercolor "rgba(255,255,255,0)" -border 5 texteffect_tmp3.png

Image
convert texteffect_tmp1.png -alpha off texteffect_tmp1_aoff.png
Image


Image
convert texteffect_tmp2.png -alpha off texteffect_tmp2_aoff.png
Image


Image
convert texteffect_tmp3.png -alpha off texteffect_tmp3_aoff.png
Image

I hope someone can explain this to me where the black is coming from (and if possible how to avoid that without leaving off the -trim ... -bordercolor ... -border). Thanks

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: possible bug in -border with transparency IM 6.5.2-4

Post by anthony »

fmw42 wrote:I hope someone can explain this to me where the black is coming from (and if possible how to avoid that without leaving off the -trim ... -bordercolor ... -border). Thanks
Any pixel with is transparent has a completely undefined color!

However PNG files will still save that color even though it is fully-transparent.

As such texteffect_tmp2.png contains the text color blurred with white in the transparent areas
But texteffect_tmp3.png has the fully-transparent pixel color set to black (very typical handling for fully-transparent pixels).

The black in this case was caused by the alpha composition method "over" that the -border operator used to add an extra border to the image. It is Correct handling!

On technique that could fix this is to 'turn off the transparency befor using -border (-alpha off) but then just turn it on again (-alpha off) afterward. As alpha is turned off while border is being applied, then it should (I hope) be ignored and preserved, and remove its effect on the composition.

Code: Select all

convert texteffect_tmp2.png -alpha off -bordercolor white -border 5 -alpha on texteffect_tmp3.png
convert texteffect_tmp3.png -alpha off texteffect_tmp3_aoff.png
YEAP that works perfectly, the colors were preserved as they were thought of as being opaque (alpha channel was switched off) for the duration of the -border and its internal alpha composition.

Tricky but does the job.

WARNING: a PNG with variable color for hidden by full transparency does not compress as well as one where that color was reset to black (or some other color).

See Discussion in
viewtopic.php?t=13746
and the addition to IM examples in
http://www.imagemagick.org/Usage/basics/#alpha_color
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug in -border with transparency IM 6.5.2-4

Post by fmw42 »

Thanks.

Fred
Post Reply