Page 1 of 1

rectangle size off by 1 pixel

Posted: 2009-08-07T23:08:48-07:00
by emery
The right and bottom sides of a rectangle seem to be 1 pixel too large for me, in every case. A 2x2 rectangle renders 3x3.
Version: ImageMagick 6.5.2-9 2009-06-16 Q16
OS X leopard

I'm using PHP Imagick 2.3.0. Haven't tested with command line.

Code: Select all

<?php 
$img = new Imagick(); 
$img->newImage(20,20,'white');
$rect = new ImagickDraw();
$rect->rectangle(8,8,12,12);
$img->drawImage($rect);

$img->setImageFormat("png"); 
header("Content-Type: image/png"); 
echo $img;
Renders a 5x5 rectangle, instead of 4x4.

Re: rectangle size off by 1 pixel

Posted: 2009-08-08T09:36:36-07:00
by fmw42
8,8 to 12,12 is a 5 pixel range as you have to count the starting pixel and go to the ending pixel. thus 12-8+1=5. Remember you have to include the start pixel as it is inclusive --- you start to draw on the first pixel and end the draw on the last pixel; so 8,9,10,11,12 that is 5 pixels

Re: rectangle size off by 1 pixel

Posted: 2009-08-08T18:58:52-07:00
by emery
But images don't follow the same rule. I have to subtract 1 from the right and bottom sides to make them fit together. A 100,100 image is the same size as a 99x99 rectangle. They match in every other graphics library I've used. Are you sure this isn't a bug? =)

Re: rectangle size off by 1 pixel

Posted: 2009-08-08T19:36:26-07:00
by magick
ImageMagick follows the SVG specification. If you can show that ImageMagick is violating the specification, that would be a bug.

Re: rectangle size off by 1 pixel

Posted: 2009-08-08T20:43:01-07:00
by fmw42
emery wrote:But images don't follow the same rule. I have to subtract 1 from the right and bottom sides to make them fit together. A 100,100 image is the same size as a 99x99 rectangle. They match in every other graphics library I've used. Are you sure this isn't a bug? =)
That is because the index starts at zero. Thus an image that goes from 0 to 99 is 100 pixels wide. The same kind of arithmetic as in drawing a box. The width of a subsection is xmax-xmin+1. With image dimensions xmax=99 xmin=0 thus 99-0+1=100

Re: rectangle size off by 1 pixel

Posted: 2009-08-09T19:21:04-07:00
by anthony
fmw42 wrote:[That is because the index starts at zero. Thus an image that goes from 0 to 99 is 100 pixels wide. The same kind of arithmetic as in drawing a box. The width of a subsection is xmax-xmin+1. With image dimensions xmax=99 xmin=0 thus 99-0+1=100
Taking that further. If you draw a rectangle on a 100x100 image you would draw it from the top-left pixel 0,0 to the bottom right pixel 99,99 That is $rect->rectangle(0,0,99,99);

Rectangles are inclusive, Pixel positions are not.

Re: rectangle size off by 1 pixel

Posted: 2009-08-10T22:20:52-07:00
by emery
Ah, ok, my mistake. =)