potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

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: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by fmw42 »

When resizing an image with transparency, the background region in the underlying rgb channels is set to black.

In the following image, there are 3 colored circles in the lower left quadrant in the original that are under full transparency. When the image is resized by 50%, those 3 colored circles are made black to match the rest of the background. Should they not be preserved when resizing?

Transparent Input:
Image

Alpha channel extracted:

Code: Select all

convert in.png -alpha extract alpha.png
Image

Alpha channel disabled, RGB channels only:

Code: Select all

convert in.png -alpha off aoff.png
Image

Resize Original:

Code: Select all

convert in.png -resize 50% in2.png
Image

RGB channels of resized image:

Code: Select all

convert in2.png -alpha off aoff2.png
Image

P.S. It is a resize issue and not a PNG issue. The same thing happens if I save to TIF

loremipsum
Posts: 2
Joined: 2020-03-26T09:01:14-07:00
Authentication code: 1152

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by loremipsum »

I found a workaround, it is not pretty and certainly quite slow but does the job:

Code: Select all

void resize_image(Magick::Image& image,
                  unsigned new_width,
                  unsigned new_height)
{
    Magick::Geometry new_size(new_width, new_height);

    if (image.channels() == 4) {
        std::vector<Magick::Image> channels;

        Magick::Image r = image.separate(Magick::RedChannel);
        r.resize(new_size);
        channels.push_back(std::move(r));

        Magick::Image g = image.separate(Magick::GreenChannel);
        g.resize(new_size);
        channels.push_back(std::move(g));

        Magick::Image b = image.separate(Magick::BlueChannel);
        b.resize(new_size);
        channels.push_back(std::move(b));

        Magick::Image a = image.separate(Magick::AlphaChannel);
        a.resize(new_size);
        channels.push_back(std::move(a));

        Magick::Image new_image(channels[0]);
        Magick::combineImages(
            &new_image,
            channels.begin(), channels.end(),
            static_cast<MagickCore::ChannelType>(
               Magick::RedChannel
               | Magick::GreenChannel
               | Magick::BlueChannel
               | Magick::AlphaChannel
            )
        );
        image = new_image;
    } else {
        image.resize(new_size);
    }
}

User avatar
magick
Site Admin
Posts: 11214
Joined: 2003-05-31T11:32:55-07:00

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by magick »

You are getting expected results as a fully transparent color is sRGB(0,0,0,0). What we think you want is to treat the alpha channel independently rather than blending, for that try -channel sync for IMv7:

Code: Select all

$ convert in.png -resize 50% txt: | grep ^24,186
24,186: (0,0,0,0)  #00000000  none

$ convert in.png -channel sync -resize 50% txt: | grep ^24,186
24,186: (255,0,0,0)  #FF000000  srgba(255,0,0,0)
ImageMagick V6 does not support syncing channels when resizing.

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by fmw42 »

That does not work. I also tried -channel rgba and it does not work.

Code: Select all

convert in.png -channel sync -resize 50% in2.png
convert in2.png -alpha off aoff2.png
aoff.png does not keep the colored circles.

I note that the image is not rgba(0,0,0,0) at the pixels that have color under the transparency in the original and should not be changed to rgba(0,0,0,0) when resizing, in my opinion. The RGB channels should not change just because the alpha channel = 0 at that location. I am not trying to use -background black -alpha background. So I would think that it should preserve the data in the underlying RGB channels.

However, -channel sync or rgba should work, in my opinion, and does not. So is there a bug there?

P.S. I tried with -alpha discrete in IM 7 and that does not fix this issue either.

User avatar
magick
Site Admin
Posts: 11214
Joined: 2003-05-31T11:32:55-07:00

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by magick »

Note sure why it works for us and not for you. Changing transparent pixels to rgba(0,0,0,0) aka "none" has been what we've done for years per discussions with Anthony way way back. It is the correct behavior when blending a fully transparent pixel and extends to all algorithms in ImageMagick that blend with alpha. It may not seem intuitive but it is the correct behavior since transparent pixels are devoid of color. Semi-transparent pixels behave differently since they have some color. Mod the red pixel so its semi-transparent, now resize by 50%. Some red should remain in the resized image. If not, we would consider that a bug.

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by fmw42 »

I do not get your same results. For me on IM 6.9.11.2 Q16 Mac OSX, I get the same transparent black in both cases

Code: Select all

convert in.png -resize 50% txt: | grep ^24,186
24,186: (0,0,0,0) #00000000 none

Code: Select all

convert in.png -channel sync -resize 50% txt: | grep ^24,186
24,186: (0,0,0,0) #00000000 none

Code: Select all

convert -version
Version: ImageMagick 6.9.11-2 Q16 x86_64 2020-03-22 https://imagemagick.org
Copyright: © 1999-2020 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP(3.1) 
Delegates (built-in): bzlib cairo djvu fftw fontconfig freetype gslib gvc jbig jng jp2 jpeg lcms lqr ltdl lzma openexr png ps raw rsvg tiff webp x xml zlib

P.S. Perhaps it is related to -resize. I tested -scale and -sample. The -scale gave the bad results, but the -sample gave your results.

Code: Select all

convert in.png -channel sync -scale 50% txt: | grep ^24,186
24,186: (0,0,0,0) #00000000 none

Code: Select all

convert in.png -channel sync -sample 50% txt: | grep ^24,186
24,186: (255,0,0,0) #FF000000 srgba(255,0,0,0)

In fact, I get the colored circles after -sample even without the -channle sync.

So why does -sample work, but -resize and -scale do not? Does it have to do with the resampling from neighboring pixels?

User avatar
magick
Site Admin
Posts: 11214
Joined: 2003-05-31T11:32:55-07:00

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by magick »

Sampling selects existing pixels that are in the image (full coverage) whereas scale and resize blends pixels (partial coverage). Each pixel component is first multiplied by the alpha value and then again my the percentage of coverage of neighboring pixels to return the final color for the resized/scaled destination pixel. An alpha value of transparent, e.g. 0, of course returns a zero color or none, e.g. 0 * pixel color = 0, thus the expected rgba(0,0,0,0).

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by fmw42 »

OK. Thanks. That is what I suspected. But why is -channel sync not working for me on my Mac IM 6.9.11.2 or 7.0.10.2?

User avatar
magick
Site Admin
Posts: 11214
Joined: 2003-05-31T11:32:55-07:00

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by magick »

Don't know, we'll need to do further testing.

User avatar
fmw42
Posts: 26261
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: potential bug -resize IM 6.9.11.2 and IM 7.0.10.2 Q16 Mac OSX Sierra

Post by fmw42 »

Thanks

Post Reply