rectangle size off by 1 pixel

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
emery

rectangle size off by 1 pixel

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: rectangle size off by 1 pixel

Post 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
emery

Re: rectangle size off by 1 pixel

Post 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? =)
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: rectangle size off by 1 pixel

Post by magick »

ImageMagick follows the SVG specification. If you can show that ImageMagick is violating the specification, that would be a bug.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: rectangle size off by 1 pixel

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: rectangle size off by 1 pixel

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
emery

Re: rectangle size off by 1 pixel

Post by emery »

Ah, ok, my mistake. =)
Post Reply