Bad affine parameters

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

Bad affine parameters

Post by snibgo »

"-distort Affine" take a list of input and output coordinates that correspond, and can output the 6 parameters.

If the input coordinates are in a straight line, the parameters in the forward direction are radically wrong.

For example (Windows BAT syntax):

Code: Select all

%IM%convert -size 7500x5000 gradient: sample.png

%IM%convert ^
  -verbose ^
  sample.png ^
  -distort Affine "0,0,0,0 2000,2000,2490,2500 4000,4000,4999,4999" ^
  a.png

Code: Select all

sample.png PNG 7500x5000 7500x5000+0+0 16-bit sRGB 90.6KB 0.906u 0:00.906
Affine Projection:
  -distort AffineProjection \
      '760152030403201.620000,-40008001603199.469000,-760152030403201.620000,400
08001603199.469000,0.000000,0.000000'
Affine Distort, FX Equivelent:
  -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
       xx=+0.040008*ii +0.760152*jj +0.000000;
       yy=+0.040008*ii +0.760152*jj +0.000000;
       p{ xx-page.x-.5, yy-page.y-.5 }' \
sample.png=>a.png PNG 7500x5000 7500x5000+0+0 16-bit sRGB 632KB 24.078u 0:03.577
Perhaps this is a mathematical problem, and the forward parameters are undefined in this situation. But when the output coordinates are in a straight line and the input coords aren't, we still have bad forward paramters and good forward parameters.

"-distort Perspective" has a similar problem, eg:

Code: Select all

F:\web\im>c:\im\ImageMagick-6.8.9-Q16\convert   -verbose   sample.png   -distort
 Perspective "0,0,0,0 1000,1000,1000,1000 2000,2000,2490,2500 4000,4000,4999,4999"   a.png

sample.png PNG 7500x5000 7500x5000+0+0 16-bit sRGB 90.6KB 0.984u 0:00.985
Perspective Projection:
  -distort PerspectiveProjection \
      '43961906953.080711, -43961906952.112701, -2.333760, 7294679016.666127,
       -7294679015.698006, -0.387245, 614769340.859532, -614769340.859599'
Perspective Distort, FX Equivelent:
  -fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
       rr=-0.016779*ii +0.016846*jj + 1;
       xx=(-0.212195*ii +1.278808*jj -0.000000)/rr;
       yy=(-0.212195*ii +1.278808*jj -0.000000)/rr;
       rr>0 ? p{ xx-page.x-.5, yy-page.y-.5 } : blue' \
snibgo's IM pages: im.snibgo.com
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Bad affine parameters

Post by anthony »

Yes you are probably right. A test should be made for a invalid matrix for both forward and reverse directions.

However even though forward mapping is 'invalid' the reverse mapping is valid. That is compressing image to a straight line, or into a gradient due to the default Virtual Pixel setting "Edge". As such in normal distortions... it is not a error!
For example this works fine...

Code: Select all

convert rose: -distort Affine '0,0,0,0  40,40,45,50 100,100,105,105' a.png
On the other hand it will go really BAD if you had use +distort, which requires the forward affine mapping to determine the containing destination window from the input image. It does that by forward mapping the corners of the source image.

Example this generates a exception when it attempts to generate a HUGE output image!

Code: Select all

convert rose: -verbose +distort Affine '0,0,0,0  40,40,45,50 100,100,105,105' a.png
Affine Distort, FX Equivelent:
-size 66285714045262472x176761904120701248 -page -39999999854899768-106666666279733520 xc: +insert \
-fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
xx=+1.523810*ii -0.571429*jj -0.000000;
yy=+1.523810*ii -0.571429*jj -0.000000;
v.p{ xx-v.page.x-.5, yy-v.page.y-.5 }' \
rose:=>a.png PPM 70x46 70x46+0+0 8-bit sRGB 6.97KB 0.000u 0:00.000
convert: pixel cache allocation failed `ROSE' @ error/cache.c/OpenPixelCache/3507.

As such a test for invalid forward affine mappings should be added, but only for
1/ Verbose Ouput
2/ Automattic output size determination

At the moment I have not got the latest Development Source download setup, but will see if I can get something.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Bad affine parameters

Post by snibgo »

Thanks.
anthony wrote:On the other hand it will go really BAD if you had use +distort, which requires the forward affine mapping to determine the containing destination window from the input image. It does that by forward mapping the corners of the source image.
Ah, that explains a "+distort" that failed when it tried to create a 200 GB temporary file.
snibgo's IM pages: im.snibgo.com
Post Reply