Number of resolutions is too high

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
Romasz
Posts: 3
Joined: 2018-09-04T23:18:56-07:00
Authentication code: 1152

Number of resolutions is too high

Post by Romasz »

Hello, I've stumbled into a bug (probably) either in ImageMagick or OpenJpeg2000 - while trying to convert small image like for example https://1drv.ms/u/s!AkG2rWqeWpmHhZI6tB-_C4anEJZAng I'm getting an error (tried with newest ImageMagick under W10). Some output:
PS C:\Bugs\PDFTest> magick convert .\problematic.png prob.jp2
convert: Number of resolutions is too high in comparison to the size of tiles
`OpenJP2' @ error/jp2.c/JP2ErrorHandler/194.
convert: unable to encode image file `prob.jp2' @ error/jp2.c/WriteJP2Image/1089.
PS C:\Bugs\PDFTest> magick -version
Version: ImageMagick 7.0.8-11 Q8 x64 2018-08-29 http://www.imagemagick.org
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Number of resolutions is too high

Post by fmw42 »

First, if using IM 7, use magick, not magick convert and not convert. However, I get your same message for that input PNG using IM 7.0.8.11 Q16 and IM 6.9.10.11. But I can convert many other PNG files to JPG2 with no errors. So perhaps your PNG file is corrupt in some way. I have also tried several older version of ImageMagick and get the same issue.

P.S. You need OpenJPEG v2.1 or higher
Romasz
Posts: 3
Joined: 2018-09-04T23:18:56-07:00
Authentication code: 1152

Re: Number of resolutions is too high

Post by Romasz »

@fmw42 Good to hear that you were able to reproduce the issue. The provided png is fine, not corrupted in any way I know, you can view it in any image browser, edit it or anything. The problem is probably related with image size somehow. The problematic image is just a cropped bitmap, if I choose less crop (bigger image) it gets converted fine to JP2, however with bigger crop (smaller image) you get the error.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Number of resolutions is too high

Post by snibgo »

The problem isn't specific to that particular file. For example:

Code: Select all

f:\web\im>%IMG7%magick -size 99x8 xc: x.jp2

magick: Number of resolutions is too high in comparison to the size of tiles
 `OpenJP2' @ error/jp2.c/JP2ErrorHandler/193.
magick: unable to encode image file `x.jp2' @ error/jp2.c/WriteJP2Image/1072.
Tested with v7.0.7-28 on Windows 8.1.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Number of resolutions is too high

Post by fmw42 »

I confirm snibgo's command error using IM 7.0.8.11 Q16 Mac OSX Sierra

Code: Select all

magick -size 99x8 xc: x.jp2
magick: Number of resolutions is too high in comparison to the size of tiles
`OpenJP2' @ error/jp2.c/JP2ErrorHandler/194.
magick: unable to encode image file `x.jp2' @ error/jp2.c/WriteJP2Image/1089

But this works fine:

Code: Select all

magick logo: logo.jp2
as does

Code: Select all

magick lena.png lena.jp2
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Number of resolutions is too high

Post by snibgo »

The problem seems to be high aspect ratios. For example, 99x16 and 16x99 are okay, but 99x15 and 15x99 fail.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Number of resolutions is too high

Post by fmw42 »

That does seem to be the case, at least this fails

Code: Select all

magick logo:[640x20+0+0] tmp.jp2
magick: Number of resolutions is too high in comparison to the size of tiles
`OpenJP2' @ error/jp2.c/JP2ErrorHandler/194.
magick: unable to encode image file `tmp.jp2' @ error/jp2.c/WriteJP2Image/1089.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Number of resolutions is too high

Post by fmw42 »

The issue is that jp2 has size limitations
code blocks are 64 samples in width and 32 samples in height
So if your height is less than 32, you need to specify only one resolution.

This works:

Code: Select all

convert logo:[640x32+0+0] tmp.jp2
But this fails:

Code: Select all

convert logo:[640x20+0+0] tmp.jp2
But specifying one resolution works again

Code: Select all

convert logo:[640x20+0+0] -define jp2:number-resolutions=1 tmp.jpg2

See https://www.imagemagick.org/script/jp2.php
Romasz
Posts: 3
Joined: 2018-09-04T23:18:56-07:00
Authentication code: 1152

Re: Number of resolutions is too high

Post by Romasz »

JP2 hasn't got any size limitation. It's just the problem of setting the number of resolutions (or the default value). If you take a look at openjpeg2000 code at: https://github.com/uclouvain/openjpeg/b ... 2k.c#L7847 you will see that it compares the size of the tiles to 2^(n-1) if size in x or y is lower then exception is thrown. It means that any image can fail if appropriate number of resolutions is set -> for n = 32 you would probably have to have image with height/width bigger than 2^31.

Unfortunately I haven't got much time to find out how number of resolutions is calculated or what is the default value. The same is for size of a tile, however it seems that for small images it's just a with/height of the image.

Setting number of resolutions to 1 is not a solution, as it result in much bigger file due to lower compression. The fact that IM can work for some images and for others not - dependent only on image size, makes it little useless in case of conversion to that particular format.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Number of resolutions is too high

Post by fmw42 »

I will have to defer to the ImageMagick developers to comment further.
Post Reply