Comment #3 on issue 181 by re...@colorado.edu: PNode.toImage is not centered and can create cropped images for PNodes with non-integral (x,y) bounds
http://code.google.com/p/piccolo2d/issues/detail?id=181

I isolated the problem and identified a potential solution. The problem was being caused around line 2845 in PNode, where the node bounds is expanded to integer dimensions for rendering to a buffered image. Only the width and height should be rounded to integer, but the x and y are also being rounded. This is problematic because (for the node defined in TestPPathToImage) the node bounds are (x,y,w,h) = (-2.5,-2.5,105,105), so this is rounded to (-3,-3,105,105), so instead of setting the pnode to appear at the origin, it is off by (0.5,0.5). I tried solving this problem by expanding only the (w,h) to be integer, and letting the (x,y) remain floating. This yielded the correct behavior; see attached file TestPPathToImage-fixed.png. Here's a minimal patch that resolves the problem:

Index: 
../../../../workingcopy/piccolo2d.java/core/src/main/java/org/piccolo2d/PNode.java
===================================================================
--- ../../../../workingcopy/piccolo2d.java/core/src/main/java/org/piccolo2d/PNode.java (revision 1020) +++ ../../../../workingcopy/piccolo2d.java/core/src/main/java/org/piccolo2d/PNode.java (revision )
@@ -2854,7 +2854,7 @@
         g2.setClip(0, 0, imageWidth, imageHeight);

         final PBounds nodeBounds = getFullBounds();
-        nodeBounds.expandNearestIntegerDimensions();
+ nodeBounds.setSize(Math.ceil(nodeBounds.getWidth()),Math.ceil(nodeBounds.getHeight()));

         final double nodeWidth = nodeBounds.getWidth();
         final double nodeHeight = nodeBounds.getHeight();

I haven't tested the behavior outside of TestPPathToImage, and I'm not sure how many incompatibilities this fix will cause in existing programs. For instance, applications may be working around this problem currently, and may obtain the wrong behavior after this is fixed in piccolo.

Attachments:
        TestPPathToImage-fixed.png  1.7 KB

--
Piccolo2D Developers Group: http://groups.google.com/group/piccolo2d-dev?hl=en

Reply via email to