Opaque images: alpha off and resize [NOT A BUG]

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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Opaque images: alpha off and resize [NOT A BUG]

Post by snibgo »

This follows from viewtopic.php?f=1&t=24122

When an image has an alpha channel that is fully opaque, "-alpha off" before a resize shouldn't make a difference, but it does.

Here is a test case. Create a black and white image with alpha channel, but fully opaque:

Code: Select all

convert ^
  -size 32x32 xc:White ^
  -gravity Center -background Black -extent 64x64 ^
  -alpha on ^
  st0.tga
Resize it two ways (with or without "-alpha off"):

Code: Select all

convert ^
  st0.tga ^
  -alpha off -resize 25%% ^
  st1.png

convert ^
  st0.tga ^
  -resize 25%% ^
  st2.png
What is the difference?

Code: Select all

compare -metric RMSE st1.png st2.png NULL:

convert st1.png st2.png -compose Difference -composite -auto-level stDiff.png
The difference is "1762.49 (0.0268939)", and occurs at the black/white boundary. Why is there a difference, and which version is correct?

EDIT: Sorry, forgot to say: tested in 6.8.6-10 on Windows 7.
Last edited by snibgo on 2013-09-24T16:22:38-07:00, edited 1 time in total.
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Opaque images: alpha off and resize

Post by dlemstra »

You should not use '-alpha on', you want '-alpha set'. This will initialize the alpha channel with the correct values. With '-alpha on' the alpha channel will contain 'garbage' and give strange results. More information is here: http://www.imagemagick.org/Usage/masking/#alpha_on. I did not test if that fixes your problem so please let us know.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Opaque images: alpha off and resize

Post by snibgo »

Fair comment that I should have used "-alpha set", not "-alpha on".

However, I did check that all pixels were opaque, and using "-alpha set" doesn't fix the problem.
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Opaque images: alpha off and resize

Post by dlemstra »

I finally figured out what is happening. The resize filter is different when an image has an alpha channel. With alpha on imagemagick uses a Mitchell filter and when it is turned off it uses an Lanczos filter. Detailed information about both filters can be found here: http://www.imagemagick.org/Usage/filter
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Opaque images: alpha off and resize

Post by snibgo »

Ah, fair enough. By explicitly setting the filter and comparing results, I can confirm you are correct: st1.png (alpha switched off) used Lanczos while st2.png used Mitchell.

At this point, I would request that http://www.imagemagick.org/script/comma ... php#filter be changed to say this, but it already does.

So: not a bug, not a problem. Thanks.
snibgo's IM pages: im.snibgo.com
Post Reply