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
|
/*
Copyright 2006 Jerry Huxtable
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.jhlabs.image;
import java.awt.*;
import java.awt.image.*;
import java.awt.geom.*;
public class PerspectiveFilter extends TransformFilter {
private float x0, y0, x1, y1, x2, y2, x3, y3;
private float dx1, dy1, dx2, dy2, dx3, dy3;
private float A, B, C, D, E, F, G, H, I;
public PerspectiveFilter() {
this(0, 0, 100, 0, 100, 100, 0, 100);
}
public PerspectiveFilter(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) {
setCorners(x0, y0, x1, y1, x2, y2, x3, y3);
}
public void setCorners(float x0, float y0, float x1, float y1, float x2, float y2, float x3, float y3) {
this.x0 = x0;
this.y0 = y0;
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.x3 = x3;
this.y3 = y3;
dx1 = x1-x2;
dy1 = y1-y2;
dx2 = x3-x2;
dy2 = y3-y2;
dx3 = x0-x1+x2-x3;
dy3 = y0-y1+y2-y3;
float a11, a12, a13, a21, a22, a23, a31, a32;
if (dx3 == 0 && dy3 == 0) {
a11 = x1-x0;
a21 = x2-x1;
a31 = x0;
a12 = y1-y0;
a22 = y2-y1;
a32 = y0;
a13 = a23 = 0;
} else {
a13 = (dx3*dy2-dx2*dy3)/(dx1*dy2-dy1*dx2);
a23 = (dx1*dy3-dy1*dx3)/(dx1*dy2-dy1*dx2);
a11 = x1-x0+a13*x1;
a21 = x3-x0+a23*x3;
a31 = x0;
a12 = y1-y0+a13*y1;
a22 = y3-y0+a23*y3;
a32 = y0;
}
A = a22 - a32*a23;
B = a31*a23 - a21;
C = a21*a32 - a31*a22;
D = a32*a13 - a12;
E = a11 - a31*a13;
F = a31*a12 - a11*a32;
G = a12*a23 - a22*a13;
H = a21*a13 - a11*a23;
I = a11*a22 - a21*a12;
}
protected void transformSpace(Rectangle rect) {
rect.x = (int)Math.min( Math.min( x0, x1 ), Math.min( x2, x3 ) );
rect.y = (int)Math.min( Math.min( y0, y1 ), Math.min( y2, y3 ) );
rect.width = (int)Math.max( Math.max( x0, x1 ), Math.max( x2, x3 ) ) - rect.x;
rect.height = (int)Math.max( Math.max( y0, y1 ), Math.max( y2, y3 ) ) - rect.y;
}
public float getOriginX() {
return x0 - (int)Math.min( Math.min( x0, x1 ), Math.min( x2, x3 ) );
}
public float getOriginY() {
return y0 - (int)Math.min( Math.min( y0, y1 ), Math.min( y2, y3 ) );
}
/*
public Point2D getPoint2D( Point2D srcPt, Point2D dstPt ) {
if ( dstPt == null )
dstPt = new Point2D.Double();
dx1 = x1-x2;
dy1 = y1-y2;
dx2 = x3-x2;
dy2 = y3-y2;
dx3 = x0-x1+x2-x3;
dy3 = y0-y1+y2-y3;
float a11, a12, a13, a21, a22, a23, a31, a32;
if (dx3 == 0 && dy3 == 0) {
a11 = x1-x0;
a21 = x2-x1;
a31 = x0;
a12 = y1-y0;
a22 = y2-y1;
a32 = y0;
a13 = a23 = 0;
} else {
a13 = (dx3*dy2-dx2*dy3)/(dx1*dy2-dy1*dx2);
a23 = (dx1*dy3-dy1*dx3)/(dx1*dy2-dy1*dx2);
a11 = x1-x0+a13*x1;
a21 = x3-x0+a23*x3;
a31 = x0;
a12 = y1-y0+a13*y1;
a22 = y3-y0+a23*y3;
a32 = y0;
}
float x = (float)srcPt.getX();
float y = (float)srcPt.getY();
float D = 1.0f/(a13*x + a23*y + 1);
dstPt.setLocation( (a11*x + a21*y + a31)*D, (a12*x + a22*y + a32)*D );
return dstPt;
}
*/
protected void transformInverse(int x, int y, float[] out) {
out[0] = originalSpace.width * (A*x+B*y+C)/(G*x+H*y+I);
out[1] = originalSpace.height * (D*x+E*y+F)/(G*x+H*y+I);
}
public String toString() {
return "Distort/Perspective...";
}
}
|