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
|
/*
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.*;
import java.awt.color.*;
import com.jhlabs.math.*;
/**
* A filter which use FFTs to simulate lens blur on an image
*/
public class LensBlurFilter extends AbstractBufferedImageOp {
private float radius = 10;
private float bloom = 2;
private float bloomThreshold = 192;
private float angle = 0;
private int sides = 5;
/**
* Set the radius of the kernel, and hence the amount of blur.
* @param radius the radius of the blur in pixels.
*/
public void setRadius(float radius) {
this.radius = radius;
}
/**
* Get the radius of the kernel.
* @return the radius
*/
public float getRadius() {
return radius;
}
public void setSides(int sides) {
this.sides = sides;
}
public int getSides() {
return sides;
}
public void setBloom(float bloom) {
this.bloom = bloom;
}
public float getBloom() {
return bloom;
}
public void setBloomThreshold(float bloomThreshold) {
this.bloomThreshold = bloomThreshold;
}
public float getBloomThreshold() {
return bloomThreshold;
}
public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
int width = src.getWidth();
int height = src.getHeight();
int rows = 1, cols = 1;
int log2rows = 0, log2cols = 0;
int iradius = (int)Math.ceil(radius);
int tileWidth = 128;
int tileHeight = tileWidth;
int adjustedWidth = (int)(width + iradius*2);
int adjustedHeight = (int)(height + iradius*2);
tileWidth = iradius < 32 ? Math.min(128, width+2*iradius) : Math.min(256, width+2*iradius);
tileHeight = iradius < 32 ? Math.min(128, height+2*iradius) : Math.min(256, height+2*iradius);
if ( dst == null )
dst = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB );
while (rows < tileHeight) {
rows *= 2;
log2rows++;
}
while (cols < tileWidth) {
cols *= 2;
log2cols++;
}
int w = cols;
int h = rows;
tileWidth = w;
tileHeight = h;//FIXME-tileWidth, w, and cols are always all the same
FFT fft = new FFT( Math.max(log2rows, log2cols) );
int[] rgb = new int[w*h];
float[][] mask = new float[2][w*h];
float[][] gb = new float[2][w*h];
float[][] ar = new float[2][w*h];
// Create the kernel
double polyAngle = Math.PI/sides;
double polyScale = 1.0f / Math.cos(polyAngle);
double r2 = radius*radius;
double rangle = Math.toRadians(angle);
float total = 0;
int i = 0;
for ( int y = 0; y < h; y++ ) {
for ( int x = 0; x < w; x++ ) {
double dx = x-w/2f;
double dy = y-h/2f;
double r = dx*dx+dy*dy;
double f = r < r2 ? 1 : 0;
if (f != 0) {
r = Math.sqrt(r);
if ( sides != 0 ) {
double a = Math.atan2(dy, dx)+rangle;
a = ImageMath.mod(a, polyAngle*2)-polyAngle;
f = Math.cos(a) * polyScale;
} else
f = 1;
f = f*r < radius ? 1 : 0;
}
total += (float)f;
mask[0][i] = (float)f;
mask[1][i] = 0;
i++;
}
}
// Normalize the kernel
i = 0;
for ( int y = 0; y < h; y++ ) {
for ( int x = 0; x < w; x++ ) {
mask[0][i] /= total;
i++;
}
}
fft.transform2D( mask[0], mask[1], w, h, true );
for ( int tileY = -iradius; tileY < height; tileY += tileHeight-2*iradius ) {
for ( int tileX = -iradius; tileX < width; tileX += tileWidth-2*iradius ) {
// System.out.println("Tile: "+tileX+" "+tileY+" "+tileWidth+" "+tileHeight);
// Clip the tile to the image bounds
int tx = tileX, ty = tileY, tw = tileWidth, th = tileHeight;
int fx = 0, fy = 0;
if ( tx < 0 ) {
tw += tx;
fx -= tx;
tx = 0;
}
if ( ty < 0 ) {
th += ty;
fy -= ty;
ty = 0;
}
if ( tx+tw > width )
tw = width-tx;
if ( ty+th > height )
th = height-ty;
src.getRGB( tx, ty, tw, th, rgb, fy*w+fx, w );
// Create a float array from the pixels. Any pixels off the edge of the source image get duplicated from the edge.
i = 0;
for ( int y = 0; y < h; y++ ) {
int imageY = y+tileY;
int j;
if ( imageY < 0 )
j = fy;
else if ( imageY > height )
j = fy+th-1;
else
j = y;
j *= w;
for ( int x = 0; x < w; x++ ) {
int imageX = x+tileX;
int k;
if ( imageX < 0 )
k = fx;
else if ( imageX > width )
k = fx+tw-1;
else
k = x;
k += j;
ar[0][i] = ((rgb[k] >> 24) & 0xff);
float r = ((rgb[k] >> 16) & 0xff);
float g = ((rgb[k] >> 8) & 0xff);
float b = (rgb[k] & 0xff);
// Bloom...
if ( r > bloomThreshold )
r *= bloom;
// r = bloomThreshold + (r-bloomThreshold) * bloom;
if ( g > bloomThreshold )
g *= bloom;
// g = bloomThreshold + (g-bloomThreshold) * bloom;
if ( b > bloomThreshold )
b *= bloom;
// b = bloomThreshold + (b-bloomThreshold) * bloom;
ar[1][i] = r;
gb[0][i] = g;
gb[1][i] = b;
i++;
k++;
}
}
// Transform into frequency space
fft.transform2D( ar[0], ar[1], cols, rows, true );
fft.transform2D( gb[0], gb[1], cols, rows, true );
// Multiply the transformed pixels by the transformed kernel
i = 0;
for ( int y = 0; y < h; y++ ) {
for ( int x = 0; x < w; x++ ) {
float re = ar[0][i];
float im = ar[1][i];
float rem = mask[0][i];
float imm = mask[1][i];
ar[0][i] = re*rem-im*imm;
ar[1][i] = re*imm+im*rem;
re = gb[0][i];
im = gb[1][i];
gb[0][i] = re*rem-im*imm;
gb[1][i] = re*imm+im*rem;
i++;
}
}
// Transform back
fft.transform2D( ar[0], ar[1], cols, rows, false );
fft.transform2D( gb[0], gb[1], cols, rows, false );
// Convert back to RGB pixels, with quadrant remapping
int row_flip = w >> 1;
int col_flip = h >> 1;
int index = 0;
//FIXME-don't bother converting pixels off image edges
for ( int y = 0; y < w; y++ ) {
int ym = y ^ row_flip;
int yi = ym*cols;
for ( int x = 0; x < w; x++ ) {
int xm = yi + (x ^ col_flip);
int a = (int)ar[0][xm];
int r = (int)ar[1][xm];
int g = (int)gb[0][xm];
int b = (int)gb[1][xm];
// Clamp high pixels due to blooming
if ( r > 255 )
r = 255;
if ( g > 255 )
g = 255;
if ( b > 255 )
b = 255;
int argb = (a << 24) | (r << 16) | (g << 8) | b;
rgb[index++] = argb;
}
}
// Clip to the output image
tx = tileX+iradius;
ty = tileY+iradius;
tw = tileWidth-2*iradius;
th = tileHeight-2*iradius;
if ( tx+tw > width )
tw = width-tx;
if ( ty+th > height )
th = height-ty;
dst.setRGB( tx, ty, tw, th, rgb, iradius*w+iradius, w );
}
}
return dst;
}
public String toString() {
return "Blur/Lens Blur...";
}
}
|