PerspectiveTransform.java 33.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
package javax.media.jai;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.geom.NoninvertibleTransformException;
import java.io.Serializable;

public final class PerspectiveTransform implements Cloneable, Serializable {

    private static final double PERSPECTIVE_DIVIDE_EPSILON = 1.0e-10;

    double m00, m01, m02, m10, m11, m12, m20, m21, m22;

    /** Constructs an identity PerspectiveTransform. */
    public PerspectiveTransform() {
        m00 = m11 = m22 = 1.0;
        m01 = m02 = m10 = m12 = m20 = m21 = 0.0;
    }

    /**
     * Constructs a new PerspectiveTransform from 9 floats.
     */
    public PerspectiveTransform(float m00, float m01, float m02,
                                float m10, float m11, float m12,
                                float m20, float m21, float m22) {
        this.m00 = m00;
        this.m01 = m01;
        this.m02 = m02;
        this.m10 = m10;
        this.m11 = m11;
        this.m12 = m12;
        this.m20 = m20;
        this.m21 = m21;
        this.m22 = m22;
    }

    /**
     * Constructs a new PerspectiveTransform from 9 doubles.
     */
    public PerspectiveTransform(double m00, double m01, double m02,
                                double m10, double m11, double m12,
                                double m20, double m21, double m22) {
        this.m00 = m00;
        this.m01 = m01;
        this.m02 = m02;
        this.m10 = m10;
        this.m11 = m11;
        this.m12 = m12;
        this.m20 = m20;
        this.m21 = m21;
        this.m22 = m22;
    }

    public PerspectiveTransform(float[] flatmatrix) {
        if ( flatmatrix == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = flatmatrix[0];
        m01 = flatmatrix[1];
        m02 = flatmatrix[2];
        m10 = flatmatrix[3];
        m11 = flatmatrix[4];
        m12 = flatmatrix[5];
        m20 = flatmatrix[6];
        m21 = flatmatrix[7];
        m22 = flatmatrix[8];
    }

    public PerspectiveTransform(float[][] matrix) {
        if ( matrix == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = matrix[0][0];
        m01 = matrix[0][1];
        m02 = matrix[0][2];
        m10 = matrix[1][0];
        m11 = matrix[1][1];
        m12 = matrix[1][2];
        m20 = matrix[2][0];
        m21 = matrix[2][1];
        m22 = matrix[2][2];
    }

    public PerspectiveTransform(double[] flatmatrix) {
        if ( flatmatrix == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = flatmatrix[0];
        m01 = flatmatrix[1];
        m02 = flatmatrix[2];
        m10 = flatmatrix[3];
        m11 = flatmatrix[4];
        m12 = flatmatrix[5];
        m20 = flatmatrix[6];
        m21 = flatmatrix[7];
        m22 = flatmatrix[8];
    }

    /**
     * Constructs a new PerspectiveTransform from a two-dimensional
     * array of doubles.
     */
    public PerspectiveTransform(double[][] matrix) {
        if ( matrix == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = matrix[0][0];
        m01 = matrix[0][1];
        m02 = matrix[0][2];
        m10 = matrix[1][0];
        m11 = matrix[1][1];
        m12 = matrix[1][2];
        m20 = matrix[2][0];
        m21 = matrix[2][1];
        m22 = matrix[2][2];
    }

    /**
     * Constructs a new PerspectiveTransform with the same effect
     * as an existing AffineTransform.
     * @throws IllegalArgumentException if transform is null
     */
    public PerspectiveTransform(AffineTransform transform) {
        if ( transform == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = transform.getScaleX();
        m01 = transform.getShearX();
        m02 = transform.getTranslateX();
        m10 = transform.getShearY();
        m11 = transform.getScaleY();
        m12 = transform.getTranslateY();
        m20 = 0.0;
        m21 = 0.0;
        m22 = 1.0;
    }

    /**
     * Replaces the matrix with its adjoint.
     */
    private final void makeAdjoint() {
        double m00p = m11*m22 - m12*m21;
        double m01p = m12*m20 - m10*m22; 
        double m02p = m10*m21 - m11*m20;
        double m10p = m02*m21 - m01*m22;
        double m11p = m00*m22 - m02*m20;
        double m12p = m01*m20 - m00*m21;
        double m20p = m01*m12 - m02*m11;
        double m21p = m02*m10 - m00*m12;
        double m22p = m00*m11 - m01*m10;

        m00 = m00p;
        m01 = m10p;
        m02 = m20p;
        m10 = m01p;
        m11 = m11p;
        m12 = m21p;
        m20 = m02p;
        m21 = m12p;
        m22 = m22p;
    }

     private final void normalize() {
        double invscale = 1.0/m22;
        m00 *= invscale;
        m01 *= invscale;
        m02 *= invscale;
        m10 *= invscale;
        m11 *= invscale;
        m12 *= invscale;
        m20 *= invscale;
        m21 *= invscale;
        m22 = 1.0;
    }

    private static final void getSquareToQuad(double x0, double y0,
                                              double x1, double y1,
                                              double x2, double y2,
                                              double x3, double y3,
                                              PerspectiveTransform tx) {
        double dx3 = x0 - x1 + x2 - x3;
        double dy3 = y0 - y1 + y2 - y3;

        tx.m22 = 1.0F;

        if ((dx3 == 0.0F) && (dy3 == 0.0F)) { 
            tx.m00 = x1 - x0;
            tx.m01 = x2 - x1;
            tx.m02 = x0;
            tx.m10 = y1 - y0;
            tx.m11 = y2 - y1;
            tx.m12 = y0;
            tx.m20 = 0.0F;
            tx.m21 = 0.0F;
        } else {
            double dx1 = x1 - x2;
            double dy1 = y1 - y2;
            double dx2 = x3 - x2;
            double dy2 = y3 - y2;

            double invdet = 1.0F/(dx1*dy2 - dx2*dy1);
            tx.m20 = (dx3*dy2 - dx2*dy3)*invdet;
            tx.m21 = (dx1*dy3 - dx3*dy1)*invdet;
            tx.m00 = x1 - x0 + tx.m20*x1;
            tx.m01 = x3 - x0 + tx.m21*x3;
            tx.m02 = x0;
            tx.m10 = y1 - y0 + tx.m20*y1;
            tx.m11 = y3 - y0 + tx.m21*y3;
            tx.m12 = y0;
        }
    }

    public static PerspectiveTransform getSquareToQuad(double x0, double y0,
                                                       double x1, double y1,
                                                       double x2, double y2,
                                                       double x3, double y3) {
        PerspectiveTransform tx = new PerspectiveTransform();
        getSquareToQuad(x0, y0, x1, y1, x2, y2, x3, y3, tx);
        return tx;
    }


    public static PerspectiveTransform getSquareToQuad(float x0, float y0,
                                                       float x1, float y1,
                                                       float x2, float y2,
                                                       float x3, float y3) {
        return getSquareToQuad((double)x0, (double)y0,
                               (double)x1, (double)y1,
                               (double)x2, (double)y2,
                               (double)x3, (double)y3);
    }


    /**
     * Creates a PerspectiveTransform that maps an arbitrary
     * quadrilateral onto the unit square.
     */
    public static PerspectiveTransform getQuadToSquare(double x0, double y0,
                                                       double x1, double y1,
                                                       double x2, double y2,
                                                       double x3, double y3) {
        PerspectiveTransform tx = new PerspectiveTransform();
        getSquareToQuad(x0, y0, x1, y1, x2, y2, x3, y3, tx);
        tx.makeAdjoint();
        return tx;
    }

    /**
     * Creates a PerspectiveTransform that maps an arbitrary
     * quadrilateral onto the unit square.
     */
    public static PerspectiveTransform getQuadToSquare(float x0, float y0,
                                                       float x1, float y1,
                                                       float x2, float y2,
                                                       float x3, float y3) {
        return getQuadToSquare((double)x0, (double)y0,
                               (double)x1, (double)y1,
                               (double)x2, (double)y2,
                               (double)x3, (double)y3);
    }

    /**
     * Creates a PerspectiveTransform that maps an arbitrary
     * quadrilateral onto another arbitrary quadrilateral.
     */
    public static PerspectiveTransform getQuadToQuad(double x0, double y0,
                                                     double x1, double y1,
                                                     double x2, double y2,
                                                     double x3, double y3,
                                                     double x0p, double y0p,
                                                     double x1p, double y1p,
                                                     double x2p, double y2p,
                                                     double x3p, double y3p) {
        PerspectiveTransform tx1 =
                          getQuadToSquare(x0, y0, x1, y1, x2, y2, x3, y3);

        PerspectiveTransform tx2 =
                  getSquareToQuad(x0p, y0p, x1p, y1p, x2p, y2p, x3p, y3p);

        tx1.concatenate(tx2);
        return tx1;
    }


    /**
     * Creates a PerspectiveTransform that maps an arbitrary
     * quadrilateral onto another arbitrary quadrilateral.
     */
    public static PerspectiveTransform getQuadToQuad(float x0, float y0,
                                                     float x1, float y1,
                                                     float x2, float y2,
                                                     float x3, float y3,
                                                     float x0p, float y0p,
                                                     float x1p, float y1p,
                                                     float x2p, float y2p,
                                                     float x3p, float y3p) {
        return getQuadToQuad((double)x0, (double)y0,
                              (double)x1, (double)y1,
                              (double)x2, (double)y2,
                              (double)x3, (double)y3,
                              (double)x0p, (double)y0p,
                              (double)x1p, (double)y1p,
                              (double)x2p, (double)y2p,
                              (double)x3p, (double)y3p);
    }

    /**
     * Returns the determinant of the matrix representation of the
     * transform.
     */
    public double getDeterminant() {
	return ( (m00 * ((m11 * m22) - (m12 * m21))) -
                 (m01 * ((m10 * m22) - (m12 * m20))) +
                 (m02 * ((m10 * m21) - (m11 * m20))) );

    }

    /**
     * Retrieves the 9 specifiable values in the 3x3 affine
     * transformation matrix into an array of double precision values.
     * The values are stored into the array as
     * { m00 m01 m02 m10 m11 m12 m20 m21 m22 }.
     */
    public double[] getMatrix(double[] flatmatrix) {
        if (flatmatrix == null) {
            flatmatrix = new double[9];
        }

        flatmatrix[0] = m00;
        flatmatrix[1] = m01;
        flatmatrix[2] = m02;
        flatmatrix[3] = m10;
        flatmatrix[4] = m11;
        flatmatrix[5] = m12;
        flatmatrix[6] = m20;
        flatmatrix[7] = m21;
        flatmatrix[8] = m22;

        return flatmatrix;
    }

    /**
     * Retrieves the 9 specifiable values in the 3x3 affine
     * transformation matrix into a 2-dimensional array of double
     * precision values.  The values are stored into the 2-dimensional
     * array using the row index as the first subscript and the column
     * index as the second.
     *
     */
    public double[][] getMatrix(double[][] matrix) {
        if (matrix == null) {
            matrix = new double[3][3];
        }

        matrix[0][0] = m00;
        matrix[0][1] = m01;
        matrix[0][2] = m02;
        matrix[1][0] = m10;
        matrix[1][1] = m11;
        matrix[1][2] = m12;
        matrix[2][0] = m20;
        matrix[2][1] = m21;
        matrix[2][2] = m22;

        return matrix;
    }

    /**
     * Concatenates this transform with a translation transformation.
     */
    public void translate(double tx, double ty) {
        PerspectiveTransform Tx = new PerspectiveTransform();
        Tx.setToTranslation(tx, ty);
        concatenate(Tx);
    }

    /**
     * Concatenates this transform with a rotation transformation.
     */
    public void rotate(double theta) {
        PerspectiveTransform Tx = new PerspectiveTransform();
        Tx.setToRotation(theta);
        concatenate(Tx);
    }

    /**
     * Concatenates this transform with a translated rotation transformation.
     */
    public void rotate(double theta, double x, double y) {
        PerspectiveTransform Tx = new PerspectiveTransform();
        Tx.setToRotation(theta, x, y);
        concatenate(Tx);
    }

    /**
     * Concatenates this transform with a scaling transformation.
     */
    public void scale(double sx, double sy) {
        PerspectiveTransform Tx = new PerspectiveTransform();
        Tx.setToScale(sx, sy);
        concatenate(Tx);
    }

    /**
     * Concatenates this transform with a shearing transformation.
     */
    public void shear(double shx, double shy) {
        PerspectiveTransform Tx = new PerspectiveTransform();
        Tx.setToShear(shx, shy);
        concatenate(Tx);
    }

    /**
     * Resets this transform to the Identity transform.
     */
    public void setToIdentity() {
        m00 = m11 = m22 = 1.0;
        m01 = m10 = m02 = m20 = m12 = m21 = 0.0;
    }

    /**
     * Sets this transform to a translation transformation.
     */
    public void setToTranslation(double tx, double ty) {
        m00 = 1.0;
        m01 = 0.0;
        m02 = tx;
        m10 = 0.0;
        m11 = 1.0;
        m12 = ty;
        m20 = 0.0;
        m21 = 0.0;
        m22 = 1.0;
    }

    /**
     * Sets this transform to a rotation transformation.
     */
    public void setToRotation(double theta) {
        m00 = Math.cos(theta);
        m01 = -Math.sin(theta);
        m02 = 0.0;
        m10 = - m01;
        m11 = m00;
        m12 = 0.0;
        m20 = 0.0;
        m21 = 0.0;
        m22 = 1.0;
    }

    public void setToRotation(double theta, double x, double y) {
        setToRotation(theta);
	double sin = m10;
	double oneMinusCos = 1.0 - m00;
	m02 = x * oneMinusCos + y * sin;
	m12 = y * oneMinusCos - x * sin;
    }

    public void setToScale(double sx, double sy) {
        m00 = sx;
        m01 = 0.0;
        m02 = 0.0;
        m10 = 0.0;
        m11 = sy;
        m12 = 0.0;
        m20 = 0.0;
        m21 = 0.0;
        m22 = 1.0;
    }

    /**
     * Sets this transform to a shearing transformation
     * with shear factors sx and sy.
     */
    public void setToShear(double shx, double shy) {
        m00 = 1.0;
        m01 = shx;
        m02 = 0.0;
        m10 = shy;
        m11 = 1.0;
        m12 = 0.0;
        m20 = 0.0;
        m21 = 0.0;
        m22 = 1.0;
    }

    /**
     * Sets this transform to a given AffineTransform.
     * @throws IllegalArgumentException if Tx is null
     */
    public void setTransform(AffineTransform Tx) {
        if ( Tx == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = Tx.getScaleX();
        m01 = Tx.getShearX();
        m02 = Tx.getTranslateX();
        m10 = Tx.getShearY();
        m11 = Tx.getScaleY();
        m12 = Tx.getTranslateY();
        m20 = 0.0;
        m21 = 0.0;
        m22 = 1.0;
    }

    /**
     * Sets this transform to a given PerspectiveTransform.
     * @throws IllegalArgumentException if Tx is null
     */
    public void setTransform(PerspectiveTransform Tx) {
        if ( Tx == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = Tx.m00;
        m01 = Tx.m01;
        m02 = Tx.m02;
        m10 = Tx.m10;
        m11 = Tx.m11;
        m12 = Tx.m12;
        m20 = Tx.m20;
        m21 = Tx.m21;
        m22 = Tx.m22;
    }

    public void setTransform(float m00, float m10, float m20,
                             float m01, float m11, float m21,
                             float m02, float m12, float m22) {
        this.m00 = (double)m00;
        this.m01 = (double)m01;
        this.m02 = (double)m02;
        this.m10 = (double)m10;
        this.m11 = (double)m11;
        this.m12 = (double)m12;
        this.m20 = (double)m20;
        this.m21 = (double)m21;
        this.m22 = (double)m22;
    }

    public void setTransform(double[][] matrix) {
        if ( matrix == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        m00 = matrix[0][0];
        m01 = matrix[0][1];
        m02 = matrix[0][2];
        m10 = matrix[1][0];
        m11 = matrix[1][1];
        m12 = matrix[1][2];
        m20 = matrix[2][0];
        m21 = matrix[2][1];
        m22 = matrix[2][2];
    }

    /**
     * Post-concatenates a given AffineTransform to this transform.
     * @throws IllegalArgumentException if Tx is null
     */
    public void concatenate(AffineTransform Tx) {
        if ( Tx == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        double tx_m00 = Tx.getScaleX();
        double tx_m01 = Tx.getShearX();
        double tx_m02 = Tx.getTranslateX();
        double tx_m10 = Tx.getShearY();
        double tx_m11 = Tx.getScaleY();
        double tx_m12 = Tx.getTranslateY();

        double m00p = m00*tx_m00 + m10*tx_m01 + m20*tx_m02;
        double m01p = m01*tx_m00 + m11*tx_m01 + m21*tx_m02;
        double m02p = m02*tx_m00 + m12*tx_m01 + m22*tx_m02;
        double m10p = m00*tx_m10 + m10*tx_m11 + m20*tx_m12;
        double m11p = m01*tx_m10 + m11*tx_m11 + m21*tx_m12;
        double m12p = m02*tx_m10 + m12*tx_m11 + m22*tx_m12;
        double m20p = m20;
        double m21p = m21;
        double m22p = m22;

        m00 = m00p;
        m10 = m10p;
        m20 = m20p;
        m01 = m01p;
        m11 = m11p;
        m21 = m21p;
        m02 = m02p;
        m12 = m12p;
        m22 = m22p;
    }

    /**
     * Post-concatenates a given PerspectiveTransform to this transform.
     * @throws IllegalArgumentException if Tx is null
     */
    public void concatenate(PerspectiveTransform Tx) {
        if ( Tx == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        double m00p = m00*Tx.m00 + m10*Tx.m01 + m20*Tx.m02;
        double m10p = m00*Tx.m10 + m10*Tx.m11 + m20*Tx.m12;
        double m20p = m00*Tx.m20 + m10*Tx.m21 + m20*Tx.m22;
        double m01p = m01*Tx.m00 + m11*Tx.m01 + m21*Tx.m02;
        double m11p = m01*Tx.m10 + m11*Tx.m11 + m21*Tx.m12;
        double m21p = m01*Tx.m20 + m11*Tx.m21 + m21*Tx.m22;
        double m02p = m02*Tx.m00 + m12*Tx.m01 + m22*Tx.m02;
        double m12p = m02*Tx.m10 + m12*Tx.m11 + m22*Tx.m12;
        double m22p = m02*Tx.m20 + m12*Tx.m21 + m22*Tx.m22;

        m00 = m00p;
        m10 = m10p;
        m20 = m20p;
        m01 = m01p;
        m11 = m11p;
        m21 = m21p;
        m02 = m02p;
        m12 = m12p;
        m22 = m22p;
    }

    /**
     * Pre-concatenates a given AffineTransform to this transform.
     * @throws IllegalArgumentException if Tx is null
     */
    public void preConcatenate(AffineTransform Tx) {
        if ( Tx == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        // Extend Tx: Tx.m20 = 0, Tx.m21 = 0, Tx.m22 = 1

        double tx_m00 = Tx.getScaleX();
        double tx_m01 = Tx.getShearX();
        double tx_m02 = Tx.getTranslateX();
        double tx_m10 = Tx.getShearY();
        double tx_m11 = Tx.getScaleY();
        double tx_m12 = Tx.getTranslateY();

        double m00p = tx_m00*m00 + tx_m10*m01;
        double m01p = tx_m01*m00 + tx_m11*m01;
        double m02p = tx_m02*m00 + tx_m12*m01 + m02;
        double m10p = tx_m00*m10 + tx_m10*m11;
        double m11p = tx_m01*m10 + tx_m11*m11;
        double m12p = tx_m02*m10 + tx_m12*m11 + m12;
        double m20p = tx_m00*m20 + tx_m10*m21;
        double m21p = tx_m01*m20 + tx_m11*m21;
        double m22p = tx_m02*m20 + tx_m12*m21 + m22;

        m00 = m00p;
        m10 = m10p;
        m20 = m20p;
        m01 = m01p;
        m11 = m11p;
        m21 = m21p;
        m02 = m02p;
        m12 = m12p;
        m22 = m22p;
    }

    /**
     * Pre-concatenates a given PerspectiveTransform to this transform.
     * @throws IllegalArgumentException if Tx is null
     */
    public void preConcatenate(PerspectiveTransform Tx) {
        if ( Tx == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        double m00p = Tx.m00*m00 + Tx.m10*m01 + Tx.m20*m02;
        double m10p = Tx.m00*m10 + Tx.m10*m11 + Tx.m20*m12;
        double m20p = Tx.m00*m20 + Tx.m10*m21 + Tx.m20*m22;
        double m01p = Tx.m01*m00 + Tx.m11*m01 + Tx.m21*m02;
        double m11p = Tx.m01*m10 + Tx.m11*m11 + Tx.m21*m12;
        double m21p = Tx.m01*m20 + Tx.m11*m21 + Tx.m21*m22;
        double m02p = Tx.m02*m00 + Tx.m12*m01 + Tx.m22*m02;
        double m12p = Tx.m02*m10 + Tx.m12*m11 + Tx.m22*m12;
        double m22p = Tx.m02*m20 + Tx.m12*m21 + Tx.m22*m22;

        m00 = m00p;
        m10 = m10p;
        m20 = m20p;
        m01 = m01p;
        m11 = m11p;
        m21 = m21p;
        m02 = m02p;
        m12 = m12p;
        m22 = m22p;
    }

    /**
     * Returns a new PerpectiveTransform that is the inverse
     * of the current transform.
     * @throws NoninvertibleTransformException if transform cannot be inverted
     */
     public PerspectiveTransform createInverse()
         throws NoninvertibleTransformException, CloneNotSupportedException {

	     PerspectiveTransform tx = (PerspectiveTransform)clone();
	     tx.makeAdjoint();
	     if (Math.abs(tx.m22) <  PERSPECTIVE_DIVIDE_EPSILON) {
  	       throw new NoninvertibleTransformException(JaiI18N.getString("PerspectiveTransform0"));
	     }
	     tx.normalize();
	     return tx;
    }

    /**
     * Returns a new PerpectiveTransform that is the adjoint,
     * of the current transform.  The adjoint is defined as
     * the matrix of cofactors, which in turn are the determinants
     * of the submatrices defined by removing the row and column
     * of each element from the original matrix in turn.
     */
    public PerspectiveTransform createAdjoint()
    throws CloneNotSupportedException{

	    PerspectiveTransform tx = (PerspectiveTransform)clone();
	    tx.makeAdjoint();
	    return tx;
    }

    /**
     * Transforms the specified ptSrc and stores the result in ptDst.
     * If ptDst is null, a new Point2D object will be allocated before
     * storing. In either case, ptDst containing the transformed point
     * is returned for convenience.
     * Note that ptSrc and ptDst can the same. In this case, the input
     * point will be overwritten with the transformed point.
     * @throws IllegalArgumentException if ptSrc is null
     */
    public Point2D transform(Point2D ptSrc, Point2D ptDst) {
        if ( ptSrc == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (ptDst == null) {
            if (ptSrc instanceof Point2D.Double) {
                ptDst = new Point2D.Double();
            } else {
                ptDst = new Point2D.Float();
            }
        }

        double x = ptSrc.getX();
        double y = ptSrc.getY();
        double w = m20 * x + m21 * y + m22;
        ptDst.setLocation((m00 * x + m01 * y + m02) / w,
                          (m10 * x + m11 * y + m12) / w);

        return ptDst;
    }

    /**
     * Transforms an array of point objects by this transform.
     * @throws IllegalArgumentException if ptSrc is null
     * @throws IllegalArgumentException if ptDst is null
     * @throws ArrayIndexOutOfBoundsException if ptSrc is too small
     */
    public void transform(Point2D[] ptSrc, int srcOff,
			  Point2D[] ptDst, int dstOff,
			  int numPts) {

        if ( ptSrc == null || ptDst == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        while (numPts-- > 0) {
            Point2D src = ptSrc[srcOff++];
            Point2D dst = ptDst[dstOff++];
            if (dst == null) {
                if (src instanceof Point2D.Double) {
                    dst = new Point2D.Double();
                } else {
                    dst = new Point2D.Float();
                }
                ptDst[dstOff - 1] = dst;
            }

            double x = src.getX();
            double y = src.getY();
            double w = m20 * x + m21 * y + m22;

            if (w == 0) {
                dst.setLocation(x, y);
            } else {
                dst.setLocation((m00 * x + m01 * y + m02) / w,
                                (m10 * x + m11 * y + m12) / w);
            }
        }
    }

    /**
     * Transforms an array of floating point coordinates by this transform.
     * @throws IllegalArgumentException if srcPts is null
     * @throws ArrayIndexOutOfBoundsException if srcPts is too small
     */
    public void transform(float[] srcPts, int srcOff,
			  float[] dstPts, int dstOff,
			  int numPts) {

        if ( srcPts == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (dstPts == null) {
            dstPts = new float[numPts * 2 + dstOff];
        }

        while (numPts-- > 0) {
            float x = srcPts[srcOff++];
            float y = srcPts[srcOff++];
            double w = m20 * x + m21 * y + m22;

            if (w == 0) {
                dstPts[dstOff++] = x;
                dstPts[dstOff++] = y;
            } else {
                dstPts[dstOff++] = (float)((m00 * x + m01 * y + m02) / w);
                dstPts[dstOff++] = (float)((m10 * x + m11 * y + m12) / w);
            }
        }
    }

    /**
     * Transforms an array of double precision coordinates by this transform.
     * @throws IllegalArgumentException if srcPts is null
     * @throws ArrayIndexOutOfBoundsException if srcPts is too small
     */
    public void transform(double[] srcPts, int srcOff,
			  double[] dstPts, int dstOff,
			  int numPts) {

        if ( srcPts == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (dstPts == null) {
            dstPts = new double[numPts * 2 + dstOff];
        }

        while (numPts-- > 0) {
            double x = srcPts[srcOff++];
            double y = srcPts[srcOff++];
            double w = m20 * x + m21 * y + m22;

            if (w == 0) {
                dstPts[dstOff++] = x;
                dstPts[dstOff++] = y;
            } else {
                dstPts[dstOff++] = (m00 * x + m01 * y + m02) / w;
                dstPts[dstOff++] = (m10 * x + m11 * y + m12) / w;
            }
        }
    }

    /**
     * Transforms an array of floating point coordinates by this transform,
     * storing the results into an array of doubles.
     * @throws IllegalArgumentException if srcPts is null
     * @throws ArrayIndexOutOfBoundsException if srcPts is too small
     */
    public void transform(float[] srcPts, int srcOff,
			  double[] dstPts, int dstOff,
			  int numPts) {

        if ( srcPts == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (dstPts == null) {
            dstPts = new double[numPts * 2 + dstOff];
        }

        while (numPts-- > 0) {
            float x = srcPts[srcOff++];
            float y = srcPts[srcOff++];
            double w = m20 * x + m21 * y + m22;

            if (w == 0) {
                dstPts[dstOff++] = x;
                dstPts[dstOff++] = y;
            } else {
                dstPts[dstOff++] = (m00 * x + m01 * y + m02) / w;
                dstPts[dstOff++] = (m10 * x + m11 * y + m12) / w;
            }
        }
    }

    /**
     * Transforms an array of double precision coordinates by this transform,
     * storing the results into an array of floats.
     * @throws IllegalArgumentException if srcPts is null
     * @throws ArrayIndexOutOfBoundsException if srcPts is too small
     */
    public void transform(double[] srcPts, int srcOff,
			  float[] dstPts, int dstOff,
			  int numPts) {

        if ( srcPts == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (dstPts == null) {
            dstPts = new float[numPts * 2 + dstOff];
        }

        while (numPts-- > 0) {
            double x = srcPts[srcOff++];
            double y = srcPts[srcOff++];
            double w = m20 * x + m21 * y + m22;

            if (w == 0) {
                dstPts[dstOff++] = (float)x;
                dstPts[dstOff++] = (float)y;
            } else {
                dstPts[dstOff++] = (float)((m00 * x + m01 * y + m02) / w);
                dstPts[dstOff++] = (float)((m10 * x + m11 * y + m12) / w);
            }
        }
    }

    /**
     * Inverse transforms the specified ptSrc and stores the result in ptDst.
     * If ptDst is null, a new Point2D object will be allocated before
     * storing. In either case, ptDst containing the transformed point
     * is returned for convenience.
   
     * @throws NoninvertibleTransformException  if the matrix cannot be inverted.
     * @throws IllegalArgumentException if ptSrc is null
     */
    public Point2D inverseTransform(Point2D ptSrc, Point2D ptDst)
	throws NoninvertibleTransformException
    {
        if ( ptSrc == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (ptDst == null) {
	    if (ptSrc instanceof Point2D.Double) {
		ptDst = new Point2D.Double();
	    } else {
		ptDst = new Point2D.Float();
	    }
	}
	// Copy source coords into local variables in case src == dst
	double x = ptSrc.getX();
	double y = ptSrc.getY();

        double tmp_x = (m11*m22 - m12*m21) * x +
            (m02*m21 - m01*m22) * y +
            (m01*m12 - m02*m11);
        double tmp_y = (m12*m20 - m10*m22) * x +
            (m00*m22 - m02*m20) * y +
            (m02*m10 - m00*m12);
        double w = (m10*m21 - m11*m20) * x +
            (m01*m20 - m00*m21) * y +
            (m00*m11 - m01*m10);

        double wabs = w;
        if (w < 0) {
            wabs = - w;
        }
        if (wabs < PERSPECTIVE_DIVIDE_EPSILON) {
            throw new
		NoninvertibleTransformException(
				     JaiI18N.getString("PerspectiveTransform1"));
        }

        ptDst.setLocation(tmp_x/w, tmp_y/w);

        return ptDst;
    }

    /**
     * Inverse transforms an array of double precision coordinates by
     * this transform.
     * @throws NoninvertibleTransformException  if the matrix cannot be inverted.
     * @throws IllegalArgumentException if srcPts is null
     * @throws ArrayIndexOutOfBoundsException if srcPts is too small
     * @throws NoninvertibleTransformException transform cannot be inverted
     */
    public void inverseTransform(double[] srcPts, int srcOff,
                                 double[] dstPts, int dstOff,
                                 int numPts)
        throws NoninvertibleTransformException
    {
        if ( srcPts == null ) {
            throw new IllegalArgumentException(JaiI18N.getString("Generic0"));
        }

        if (dstPts == null) {
            dstPts = new double[numPts * 2 + dstOff];
        }

        while (numPts-- > 0) {
            double x = srcPts[srcOff++];
            double y = srcPts[srcOff++];

            double tmp_x = (m11*m22 - m12*m21) * x +
                (m02*m21 - m01*m22) * y +
                (m01*m12 - m02*m11);
            double tmp_y = (m12*m20 - m10*m22) * x +
                (m00*m22 - m02*m20) * y +
                (m02*m10 - m00*m12);
            double w = (m10*m21 - m11*m20) * x +
                (m01*m20 - m00*m21) * y +
                (m00*m11 - m01*m10);

            double wabs = w;
            if (w < 0) {
                wabs = - w;
            }
            if (wabs < PERSPECTIVE_DIVIDE_EPSILON) {
                throw new NoninvertibleTransformException(
				    JaiI18N.getString("PerspectiveTransform1"));
            }

            dstPts[dstOff++] = tmp_x / w;
            dstPts[dstOff++] = tmp_y / w;
        }
    }

    /**
     * Returns a String that represents the value of this Object.
     */
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("Perspective transform matrix\n");
        sb.append(this.m00);
        sb.append("\t");
        sb.append(this.m01);
        sb.append("\t");
        sb.append(this.m02);
        sb.append("\n");
        sb.append(this.m10);
        sb.append("\t");
        sb.append(this.m11);
        sb.append("\t");
        sb.append(this.m12);
        sb.append("\n");
        sb.append(this.m20);
        sb.append("\t");
        sb.append(this.m21);
        sb.append("\t");
        sb.append(this.m22);
        sb.append("\n");
        return new String(sb);
    }

    /**
     * Returns the boolean true value if this PerspectiveTransform is an
     * identity transform. Returns false otherwise.
     */
    public boolean isIdentity() {
        return m01 == 0.0 && m02 == 0.0 &&
            m10 == 0.0 && m12 == 0.0 &&
            m20 == 0.0 && m21 == 0.0 &&
            m22 != 0.0 && m00/m22 == 1.0 && m11/m22 == 1.0;
    }

    /**
     * Returns a copy of this PerspectiveTransform object.
     */
    public Object clone() {
	try {
	    return super.clone();
	} catch (CloneNotSupportedException e) {
	    // this shouldn't happen, since we are Cloneable
	    throw new InternalError();
	}
    }


    /**
     * Tests if this PerspectiveTransform equals a supplied one.
     */
    public boolean equals(Object obj) {
        if (!(obj instanceof PerspectiveTransform)) {
            return false;
        }

        PerspectiveTransform a = (PerspectiveTransform)obj;

	return ((m00 == a.m00) && (m10 == a.m10) && (m20 == a.m20) &&
		(m01 == a.m01) && (m11 == a.m11) && (m21 == a.m21) &&
		(m02 == a.m02) && (m12 == a.m12) && (m22 == a.m22));
    }
}