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
|
/*
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.geom.*;
import java.awt.image.*;
/**
* A page curl effect.
*/
public class CurlFilter extends TransformFilter {
private float angle = 0;
private float transition = 0.0f;
private float width;
private float height;
private float radius;
/**
* Construct a CurlFilter with no distortion.
*/
public CurlFilter() {
setEdgeAction( ZERO );
}
public void setTransition( float transition ) {
this.transition = transition;
}
public float getTransition() {
return transition;
}
public void setAngle(float angle) {
this.angle = angle;
}
public float getAngle() {
return angle;
}
public void setRadius( float radius ) {
this.radius = radius;
}
public float getRadius() {
return radius;
}
/*
public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
this.width = src.getWidth();
this.height = src.getHeight();
return super.filter( src, dst );
}
*/
static class Sampler {
private int edgeAction;
private int width, height;
private int[] inPixels;
public Sampler( BufferedImage image ) {
int width = image.getWidth();
int height = image.getHeight();
int type = image.getType();
inPixels = ImageUtils.getRGB( image, 0, 0, width, height, null );
}
public int sample( float x, float y ) {
int srcX = (int)Math.floor( x );
int srcY = (int)Math.floor( y );
float xWeight = x-srcX;
float yWeight = y-srcY;
int nw, ne, sw, se;
if ( srcX >= 0 && srcX < width-1 && srcY >= 0 && srcY < height-1) {
// Easy case, all corners are in the image
int i = width*srcY + srcX;
nw = inPixels[i];
ne = inPixels[i+1];
sw = inPixels[i+width];
se = inPixels[i+width+1];
} else {
// Some of the corners are off the image
nw = getPixel( inPixels, srcX, srcY, width, height );
ne = getPixel( inPixels, srcX+1, srcY, width, height );
sw = getPixel( inPixels, srcX, srcY+1, width, height );
se = getPixel( inPixels, srcX+1, srcY+1, width, height );
}
return ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
}
final private int getPixel( int[] pixels, int x, int y, int width, int height ) {
if (x < 0 || x >= width || y < 0 || y >= height) {
switch (edgeAction) {
case ZERO:
default:
return 0;
case WRAP:
return pixels[(ImageMath.mod(y, height) * width) + ImageMath.mod(x, width)];
case CLAMP:
return pixels[(ImageMath.clamp(y, 0, height-1) * width) + ImageMath.clamp(x, 0, width-1)];
}
}
return pixels[ y*width+x ];
}
}
public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
int width = src.getWidth();
int height = src.getHeight();
this.width = src.getWidth();
this.height = src.getHeight();
int type = src.getType();
originalSpace = new Rectangle(0, 0, width, height);
transformedSpace = new Rectangle(0, 0, width, height);
transformSpace(transformedSpace);
if ( dst == null ) {
ColorModel dstCM = src.getColorModel();
dst = new BufferedImage(dstCM, dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height), dstCM.isAlphaPremultiplied(), null);
}
WritableRaster dstRaster = dst.getRaster();
int[] inPixels = getRGB( src, 0, 0, width, height, null );
if ( interpolation == NEAREST_NEIGHBOUR )
return filterPixelsNN( dst, width, height, inPixels, transformedSpace );
int srcWidth = width;
int srcHeight = height;
int srcWidth1 = width-1;
int srcHeight1 = height-1;
int outWidth = transformedSpace.width;
int outHeight = transformedSpace.height;
int outX, outY;
int index = 0;
int[] outPixels = new int[outWidth];
outX = transformedSpace.x;
outY = transformedSpace.y;
float[] out = new float[4];
for (int y = 0; y < outHeight; y++) {
for (int x = 0; x < outWidth; x++) {
transformInverse(outX+x, outY+y, out);
int srcX = (int)Math.floor( out[0] );
int srcY = (int)Math.floor( out[1] );
float xWeight = out[0]-srcX;
float yWeight = out[1]-srcY;
int nw, ne, sw, se;
if ( srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
// Easy case, all corners are in the image
int i = srcWidth*srcY + srcX;
nw = inPixels[i];
ne = inPixels[i+1];
sw = inPixels[i+srcWidth];
se = inPixels[i+srcWidth+1];
} else {
// Some of the corners are off the image
nw = getPixel( inPixels, srcX, srcY, srcWidth, srcHeight );
ne = getPixel( inPixels, srcX+1, srcY, srcWidth, srcHeight );
sw = getPixel( inPixels, srcX, srcY+1, srcWidth, srcHeight );
se = getPixel( inPixels, srcX+1, srcY+1, srcWidth, srcHeight );
}
int rgb = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
int r = (rgb >> 16) & 0xff;
int g = (rgb >> 8) & 0xff;
int b = rgb & 0xff;
float shade = out[2];
r = (int)(r * shade);
g = (int)(g * shade);
b = (int)(b * shade);
rgb = (rgb & 0xff000000) | (r << 16) | (g << 8) | b;
if ( out[3] != 0 )
outPixels[x] = PixelUtils.combinePixels( rgb, inPixels[srcWidth*y + x], PixelUtils.NORMAL );
else
outPixels[x] = rgb;
}
setRGB( dst, 0, y, transformedSpace.width, 1, outPixels );
}
return dst;
}
final private int getPixel( int[] pixels, int x, int y, int width, int height ) {
if (x < 0 || x >= width || y < 0 || y >= height) {
switch (edgeAction) {
case ZERO:
default:
return 0;
case WRAP:
return pixels[(ImageMath.mod(y, height) * width) + ImageMath.mod(x, width)];
case CLAMP:
return pixels[(ImageMath.clamp(y, 0, height-1) * width) + ImageMath.clamp(x, 0, width-1)];
}
}
return pixels[ y*width+x ];
}
protected void transformInverse(int x, int y, float[] out) {
/*Fisheye
float mirrorDistance = width*centreX;
float mirrorRadius = width*centreY;
float cx = width*.5f;
float cy = height*.5f;
float dx = x-cx;
float dy = y-cy;
float r2 = dx*dx+dy*dy;
float r = (float)Math.sqrt( r2 );
float phi = (float)(Math.PI*.5-Math.asin( Math.sqrt( mirrorRadius*mirrorRadius-r2 )/mirrorRadius ));
r = r > mirrorRadius ? width : mirrorDistance * (float)Math.tan( phi );
phi = (float)Math.atan2( dy, dx );
out[0] = cx + r*(float)Math.cos( phi );
out[1] = cy + r*(float)Math.sin( phi );
*/
float t = transition;
float px = x, py = y;
float s = (float)Math.sin( angle );
float c = (float)Math.cos( angle );
float tx = t*width;
tx = t * (float)Math.sqrt( width*width + height*height);
// Start from the correct corner according to the angle
float xoffset = c < 0 ? width : 0;
float yoffset = s < 0 ? height : 0;
// Transform into unrotated coordinates
px -= xoffset;
py -= yoffset;
float qx = px * c + py * s;
float qy = -px * s + py * c;
boolean outside = qx < tx;
boolean unfolded = qx > tx*2;
boolean oncurl = !(outside || unfolded);
qx = qx > tx*2 ? qx : 2*tx-qx;
// Transform back into rotated coordinates
px = qx * c - qy * s;
py = qx * s + qy * c;
px += xoffset;
py += yoffset;
// See if we're off the edge of the page
boolean offpage = px < 0 || py < 0 || px >= width || py >= height;
// If we're off the edge, but in the curl...
if ( offpage && oncurl ) {
px = x;
py = y;
}
// Shade the curl
float shade = !offpage && oncurl ? 1.9f * (1.0f-(float)Math.cos( Math.exp((qx-tx)/radius) )) : 0;
out[2] = 1-shade;
if ( outside ) {
out[0] = out[1] = -1;
} else {
out[0] = px;
out[1] = py;
}
out[3] = !offpage && oncurl ? 1 : 0;
}
public String toString() {
return "Distort/Curl...";
}
}
|