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
|
/*
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.*;
public class PolarFilter extends TransformFilter {
public final static int RECT_TO_POLAR = 0;
public final static int POLAR_TO_RECT = 1;
public final static int INVERT_IN_CIRCLE = 2;
private int type;
private float width, height;
private float centreX, centreY;
private float radius;
public PolarFilter() {
this(RECT_TO_POLAR);
}
public PolarFilter(int type) {
this.type = type;
setEdgeAction(CLAMP);
}
public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
this.width = src.getWidth();
this.height = src.getHeight();
centreX = width/2;
centreY = height/2;
radius = Math.max(centreY, centreX);
return super.filter( src, dst );
}
public void setType(int type) {
this.type = type;
}
public int getType() {
return type;
}
private float sqr(float x) {
return x*x;
}
protected void transformInverse(int x, int y, float[] out) {
float theta, t;
float m, xmax, ymax;
float r = 0;
switch (type) {
case RECT_TO_POLAR:
theta = 0;
if (x >= centreX) {
if (y > centreY) {
theta = ImageMath.PI - (float)Math.atan(((float)(x - centreX))/((float)(y - centreY)));
r = (float)Math.sqrt(sqr (x - centreX) + sqr (y - centreY));
} else if (y < centreY) {
theta = (float)Math.atan (((float)(x - centreX))/((float)(centreY - y)));
r = (float)Math.sqrt (sqr (x - centreX) + sqr (centreY - y));
} else {
theta = ImageMath.HALF_PI;
r = x - centreX;
}
} else if (x < centreX) {
if (y < centreY) {
theta = ImageMath.TWO_PI - (float)Math.atan (((float)(centreX -x))/((float)(centreY - y)));
r = (float)Math.sqrt (sqr (centreX - x) + sqr (centreY - y));
} else if (y > centreY) {
theta = ImageMath.PI + (float)Math.atan (((float)(centreX - x))/((float)(y - centreY)));
r = (float)Math.sqrt (sqr (centreX - x) + sqr (y - centreY));
} else {
theta = 1.5f * ImageMath.PI;
r = centreX - x;
}
}
if (x != centreX)
m = Math.abs (((float)(y - centreY)) / ((float)(x - centreX)));
else
m = 0;
if (m <= ((float)height / (float)width)) {
if (x == centreX) {
xmax = 0;
ymax = centreY;
} else {
xmax = centreX;
ymax = m * xmax;
}
} else {
ymax = centreY;
xmax = ymax / m;
}
out[0] = (width-1) - (width - 1)/ImageMath.TWO_PI * theta;
out[1] = height * r / radius;
break;
case POLAR_TO_RECT:
theta = x / width * ImageMath.TWO_PI;
float theta2;
if (theta >= 1.5f * ImageMath.PI)
theta2 = ImageMath.TWO_PI - theta;
else if (theta >= ImageMath.PI)
theta2 = theta - ImageMath.PI;
else if (theta >= 0.5f * ImageMath.PI)
theta2 = ImageMath.PI - theta;
else
theta2 = theta;
t = (float)Math.tan(theta2);
if (t != 0)
m = 1.0f / t;
else
m = 0;
if (m <= ((float)(height) / (float)(width))) {
if (theta2 == 0) {
xmax = 0;
ymax = centreY;
} else {
xmax = centreX;
ymax = m * xmax;
}
} else {
ymax = centreY;
xmax = ymax / m;
}
r = radius * (float)(y / (float)(height));
float nx = -r * (float)Math.sin(theta2);
float ny = r * (float)Math.cos(theta2);
if (theta >= 1.5f * ImageMath.PI) {
out[0] = (float)centreX - nx;
out[1] = (float)centreY - ny;
} else if (theta >= Math.PI) {
out[0] = (float)centreX - nx;
out[1] = (float)centreY + ny;
} else if (theta >= 0.5 * Math.PI) {
out[0] = (float)centreX + nx;
out[1] = (float)centreY + ny;
} else {
out[0] = (float)centreX + nx;
out[1] = (float)centreY - ny;
}
break;
case INVERT_IN_CIRCLE:
float dx = x-centreX;
float dy = y-centreY;
float distance2 = dx*dx+dy*dy;
out[0] = centreX + centreX*centreX * dx/distance2;
out[1] = centreY + centreY*centreY * dy/distance2;
break;
}
}
public String toString() {
return "Distort/Polar Coordinates...";
}
}
|