[OpenJDK 2D-Dev] More incompatibilities

Roman Kennke roman.kennke at aicas.com
Tue Mar 3 15:17:26 UTC 2009


Hi there,

> 4. StrokeShapeTest: createStrokedShape() behaves differently.

It turns out that there is an arithmetic overflow here. The pisces
stroker does a stupid thing here. First it initializes the
scaledLineWidth like this:

        this.scaledLineWidth2 = ((long)transform.m00*lineWidth2);

which is infact wrong, because in fixed point arithmetics you need to
apply >> 16, because the decimal moves.

However, in another place it uses this factor like this:

                dx = (int)( (ly*scaledLineWidth2)/ilen >> 16);
                dy = (int)(-(lx*scaledLineWidth2)/ilen >> 16);

which makes it ok in the end. The only problem is, that we start with an
incredibly large number, then multiply another incredibly large number
and _after_ that (when the overflow already occured) do the >> 16. The
patch moves the >> 16 to the initialization of scaledLineWidth, which
makes it clearer, more correct and even a tad faster, because we do the
second calculation at least 2x.

Of course, the real fix here would be to turn the whole implementation
into floating point. This particular testcase is fixed by this patch,
and the 'range of correct behaviour' is much large now, but if you deal
with very large numbers in your shapes, then you will get trouble again.

/Roman
-- 
Dipl.-Inform. (FH) Roman Kennke, Software Engineer, http://kennke.org
aicas Allerton Interworks Computer Automated Systems GmbH
Haid-und-Neu-Straße 18 * D-76131 Karlsruhe * Germany
http://www.aicas.com   * Tel: +49-721-663 968-48
USt-Id: DE216375633, Handelsregister HRB 109481, AG Karlsruhe
Geschäftsführer: Dr. James J. Hunt

-------------- next part --------------
diff --git a/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java b/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
--- a/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
+++ b/src/share/classes/sun/java2d/pisces/PiscesRenderingEngine.java
@@ -171,9 +171,9 @@
         float lw;
         if (thin) {
             if (antialias) {
-                lw = 0.5f;
+                lw = userSpaceLineWidth(at, 0.5f);
             } else {
-                lw = 1.0f;
+                lw = userSpaceLineWidth(at, 1.0f);
             }
         } else {
             lw = bs.getLineWidth();
@@ -187,6 +187,67 @@
                  bs.getDashArray(),
                  bs.getDashPhase(),
                  lsink);
+    }
+
+    private float userSpaceLineWidth(AffineTransform at, float lw) {
+        double widthScale;
+        if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM |
+                            AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
+            widthScale = Math.sqrt(at.getDeterminant());
+        } else {
+            /* First calculate the "maximum scale" of this transform. */
+            double A = at.getScaleX();       // m00
+            double C = at.getShearX();       // m01
+            double B = at.getShearY();       // m10
+            double D = at.getScaleY();       // m11
+
+            /*
+             * Given a 2 x 2 affine matrix [ A B ] such that
+             *                             [ C D ]
+             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
+             * find the maximum magnitude (norm) of the vector v'
+             * with the constraint (x^2 + y^2 = 1).
+             * The equation to maximize is
+             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
+             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
+             * Since sqrt is monotonic we can maximize |v'|^2
+             * instead and plug in the substitution y = sqrt(1 - x^2).
+             * Trigonometric equalities can then be used to get
+             * rid of most of the sqrt terms.
+             */
+            double EA = A*A + B*B;          // x^2 coefficient
+            double EB = 2*(A*C + B*D);      // xy coefficient
+            double EC = C*C + D*D;          // y^2 coefficient
+
+            /*
+             * There is a lot of calculus omitted here.
+             *
+             * Conceptually, in the interests of understanding the
+             * terms that the calculus produced we can consider
+             * that EA and EC end up providing the lengths along
+             * the major axes and the hypot term ends up being an
+             * adjustment for the additional length along the off-axis
+             * angle of rotated or sheared ellipses as well as an
+             * adjustment for the fact that the equation below
+             * averages the two major axis lengths.  (Notice that
+             * the hypot term contains a part which resolves to the
+             * difference of these two axis lengths in the absence
+             * of rotation.)
+             *
+             * In the calculus, the ratio of the EB and (EA-EC) terms
+             * ends up being the tangent of 2*theta where theta is
+             * the angle that the long axis of the ellipse makes
+             * with the horizontal axis.  Thus, this equation is
+             * calculating the length of the hypotenuse of a triangle
+             * along that axis.
+             */
+            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
+
+            /* sqrt omitted, compare to squared limits below. */
+            double widthsquared = ((EA + EC + hypot)/2.0);
+            widthScale = Math.sqrt(widthsquared);
+        }
+        return (float) (lw / widthScale);
     }
 
     void strokeTo(Shape src,


More information about the 2d-dev mailing list