Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

montage: 8-bit PNG images incorrectly converted into 48-bit PNG images #5041

Closed
neofuji opened this issue Apr 13, 2022 · 12 comments
Closed

montage: 8-bit PNG images incorrectly converted into 48-bit PNG images #5041

neofuji opened this issue Apr 13, 2022 · 12 comments

Comments

@neofuji
Copy link

neofuji commented Apr 13, 2022

ImageMagick version

7.1.0-21

Operating system

Windows

Operating system, version and so on

Windows 11 Pro x64 22000.593

Description

From 7.1.0-21, montage converts 8-bit PNG images into 48-bit PNG images with corrupted color.
The output images of 7.1.0-20 were 8-bit PNG, the same format as input images.

Steps to Reproduce

7.1.0-20

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick -version
Version: ImageMagick 7.1.0-20 Q16-HDRI x64 2022-01-22 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP(2.0)
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193030709)

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick identify convex-hull-blocks.png
convex-hull-blocks.png PNG 128x128 128x128+0+0 8-bit sRGB 7257B 0.000u 0:00.000

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick montage convex-hull-blocks.png -geometry 128x128 C:\convex-hull-blocks_7.1.0-20.png

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick identify C:\convex-hull-blocks_7.1.0-20.png
C:\convex-hull-blocks_7.1.0-20.png PNG 128x128 128x128+0+0 8-bit sRGB 7418B 0.000u 0:00.000

7.1.0-21

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick -version
Version: ImageMagick 7.1.0-21 Q16-HDRI x64 2022-01-28 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenCL OpenMP(2.0)
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193030709)

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick identify convex-hull-blocks.png
convex-hull-blocks.png PNG 128x128 128x128+0+0 8-bit sRGB 7257B 0.000u 0:00.000

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick montage convex-hull-blocks.png -geometry 128x128 C:\convex-hull-blocks_7.1.0-21.png

C:\Program Files\ImageMagick-7.1.0-Q16-HDRI\images>magick identify C:\convex-hull-blocks_7.1.0-21.png
C:\convex-hull-blocks_7.1.0-21.png PNG 128x128 128x128+0+0 16-bit sRGB 29887B 0.000u 0:00.000

Images

convex-hull-blocks_7.1.0-20.png

convex-hull-blocks_7.1.0-20

convex-hull-blocks_7.1.0-21.png

convex-hull-blocks_7.1.0-21

@snibgo
Copy link

snibgo commented Apr 13, 2022

Please show your input image, convex-hull-blocks.png.

@neofuji
Copy link
Author

neofuji commented Apr 13, 2022

Please show your input image, convex-hull-blocks.png.

Here is from built-in images in ImageMagick.
convex-hull-blocks.png

@fmw42
Copy link

fmw42 commented Apr 13, 2022

It is normal for ImageMagick to make the output depth the Q depth of your compile when new colors are introduced. So if you are on Q16, then he output from some processing would be 16-bits per pixel per channel.

I am not sure why your IM 7.1.0.20 is giving 8-bits. That is not typical and was likely corrected in 7.1.0.21.

If you want to keep 8-bits, then add -depth 8 to your command line (3x8=24-bit color or use PNG8: for 8-bit color)

@snibgo
Copy link

snibgo commented Apr 13, 2022

magick montage convex-hull-blocks.png -geometry 128x128 C:\convex-hull-blocks_7.1.0-21.png

This command will slightly shrink the input, and add a border. Shrinking (by resizing) is re-sampling, which usually adds new colours "between" existing colours.

When IM saves a PNG, by default it will use 8 bits/channel/pixel unless that would lose precision. If it would lose precision, it will use 16 bits/channel/pixel.

As fmw42 says, use "-depth 8" before the output if you want to force just 8 bits.

@neofuji
Copy link
Author

neofuji commented Apr 17, 2022

magick montage convex-hull-blocks.png -geometry 128x128 C:\convex-hull-blocks_7.1.0-21.png

This command will slightly shrink the input, and add a border. Shrinking (by resizing) is re-sampling, which usually adds new colours "between" existing colours.

The original image convex-hull-blocks.png is 128x128 it doesn't cause resizing/resampling.

The document says "PNG inheriting subformat from original if possible."
Without using -depth 8, IM 7.1.0-20 converts to same subformat.
So I think IM 7.1.0-20 has correct behavior and IM 7.1.0-21 has incorrect behavior.

@snibgo
Copy link

snibgo commented Apr 17, 2022

That is only if you use "PNG00:"

PNG00 RW Portable Network Graphics PNG inheriting subformat from original if possible

But you haven't use "PNG00:".

@neofuji
Copy link
Author

neofuji commented Apr 24, 2022

@urban-warrior
This issue is not resolved. Please reopen.

I tried adding PNG00: but the result is same as adding no PNG00:, IM 7.1.0-21 still converts PNG8 image into PNG48 incorrectly.
So PNG00: on IM 7.1.0-21 is clearly corrupted.

The changelog doesn't say any PNG encoder/decoder changed, so this problem is a software regression.
Please investigate the cause in ImageMagick program.

This problem is still represented on IM 7.1.0-31.

convex-hull-blocks_7.1.0-20-PNG00.png PNG 128x128 128x128+0+0 8-bit sRGB 7418B 0.000u 0:00.000

convex-hull-blocks_7.1.0-20-PNG00

convex-hull-blocks_7.1.0-21-PNG00.png PNG 128x128 128x128+0+0 16-bit sRGB 29887B 0.000u 0:00.000

convex-hull-blocks_7.1.0-21-PNG00

@urban-warrior
Copy link
Member

Its not a bug. The issue has nothing to do with PNG. Instead montage resizes the image with ThumbnailImage(). Previously, your image was at a size that coincidentally was sampled rather than resized. Resizing introduces new colors. The new thumbnail algorithm prefers resizing to theoretically produce "better" thumbnails. Of course, if you simply convert your source image to PNG, you will get an exact replica.

@neofuji
Copy link
Author

neofuji commented Apr 24, 2022

@urban-warrior
Then has montage been no longer usable to concatenate MxN images without any loss?

@urban-warrior
Copy link
Member

We will add a patch to clone an image when its resize size is identical to its actual size. That should resolve this issue.

@fmw42
Copy link

fmw42 commented Apr 24, 2022

You can resize (actually sample if you do not want any change in colors) outside montage and pipe the results to montage.

magick convex-hull-blocks.png -sample 128x128 miff:- | magick montage - C:\convex-hull-blocks_7.1.0-21.png

or

magick convex-hull-blocks.png -sample 128x128 PNG00:- | magick montage - C:\convex-hull-blocks_7.1.0-21.png

urban-warrior pushed a commit to ImageMagick/ImageMagick6 that referenced this issue Apr 24, 2022
netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue May 3, 2022
Make tiff optional (default on, like before)

Merged

    Add support for FITS images with NaN values #5084
    Fixes grammar/typos/markdown #5083

Commits

    prep Beta release d2c4a6f
    ImageMagick/ImageMagick#3661 9090b17
    possible obscure memory leak 8e0c95e
    check event mask before logging eab8155
    check event mask before logging 7dcb276
    Also create changelog in the daily build. 878a667
    Removed %GITHUB_WORKSPACE% from other builds. 2540c48
    improve/optimize logging 7ba113a
    ImageMagick/ImageMagick#5041 c942d53
    need Changelog.md, referenced by web pages 6b6e7f3
    ilatest autoconf update 9dfee98
    move to wbsite repo 3e445a0
    optimize thumbnail resizing 09ab692
    ImageMagick/ImageMagick#4533 (comment) d13f844
    Rename extent to length. b056cec
    Allow exif profile that has a zero length (#5082). 0433d6e
    Moved length check. 267a891
    Removed code that was used to create a test image. bf925e6
    Use consistent code style. c4c031b
    revert bca2fb8
    Added missing CoderEndianSupportFlag (#5090). 0952c6a
    Sort commits by date instead. 7b95dda
    https://oss-fuzz.com/testcase?key=5120317075357696 5622990
    Removed unused arguments. c704997
    Added missing typecasts. 6820800
    check for image width/height exceeding INT_MAX 6d2c75e
    eliminate compiler exception 8649f4f
    add comment c506f55
    Use set_arg_encoding as suggestion in #5092. 67a2613
    optimize logging b2eaec1
    fix copyright c2fc57a
    initalize logging variable 263bfae
    possible divide by zero dce1bcf
    more informative exception message b7d240b
    more informative exception message 354669f
    make some logging coditional 372b5df
    ImageMagick/ImageMagick#3859 f49b826
    release a2b2c08
@neofuji
Copy link
Author

neofuji commented May 4, 2022

@urban-warrior
Solved in IM 7.1.0-32. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants