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
|
/*
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 SmartBlurFilter extends AbstractBufferedImageOp {
private int hRadius = 5;
private int vRadius = 5;
private int threshold = 10;
public BufferedImage filter( BufferedImage src, BufferedImage dst ) {
int width = src.getWidth();
int height = src.getHeight();
if ( dst == null )
dst = createCompatibleDestImage( src, null );
int[] inPixels = new int[width*height];
int[] outPixels = new int[width*height];
getRGB( src, 0, 0, width, height, inPixels );
Kernel kernel = GaussianFilter.makeKernel(hRadius);
thresholdBlur( kernel, inPixels, outPixels, width, height, true );
thresholdBlur( kernel, outPixels, inPixels, height, width, true );
setRGB( dst, 0, 0, width, height, inPixels );
return dst;
}
/**
* Convolve with a kernel consisting of one row
*/
public void thresholdBlur(Kernel kernel, int[] inPixels, int[] outPixels, int width, int height, boolean alpha) {
int index = 0;
float[] matrix = kernel.getKernelData( null );
int cols = kernel.getWidth();
int cols2 = cols/2;
for (int y = 0; y < height; y++) {
int ioffset = y*width;
int outIndex = y;
for (int x = 0; x < width; x++) {
float r = 0, g = 0, b = 0, a = 0;
int moffset = cols2;
int rgb1 = inPixels[ioffset+x];
int a1 = (rgb1 >> 24) & 0xff;
int r1 = (rgb1 >> 16) & 0xff;
int g1 = (rgb1 >> 8) & 0xff;
int b1 = rgb1 & 0xff;
float af = 0, rf = 0, gf = 0, bf = 0;
for (int col = -cols2; col <= cols2; col++) {
float f = matrix[moffset+col];
if (f != 0) {
int ix = x+col;
if (!(0 <= ix && ix < width))
ix = x;
int rgb2 = inPixels[ioffset+ix];
int a2 = (rgb2 >> 24) & 0xff;
int r2 = (rgb2 >> 16) & 0xff;
int g2 = (rgb2 >> 8) & 0xff;
int b2 = rgb2 & 0xff;
int d;
d = a1-a2;
if ( d >= -threshold && d <= threshold ) {
a += f * a2;
af += f;
}
d = r1-r2;
if ( d >= -threshold && d <= threshold ) {
r += f * r2;
rf += f;
}
d = g1-g2;
if ( d >= -threshold && d <= threshold ) {
g += f * g2;
gf += f;
}
d = b1-b2;
if ( d >= -threshold && d <= threshold ) {
b += f * b2;
bf += f;
}
}
}
a = af == 0 ? a1 : a/af;
r = rf == 0 ? r1 : r/rf;
g = gf == 0 ? g1 : g/gf;
b = bf == 0 ? b1 : b/bf;
int ia = alpha ? PixelUtils.clamp((int)(a+0.5)) : 0xff;
int ir = PixelUtils.clamp((int)(r+0.5));
int ig = PixelUtils.clamp((int)(g+0.5));
int ib = PixelUtils.clamp((int)(b+0.5));
outPixels[outIndex] = (ia << 24) | (ir << 16) | (ig << 8) | ib;
outIndex += height;
}
}
}
public void setHRadius(int hRadius) {
this.hRadius = hRadius;
}
public int getHRadius() {
return hRadius;
}
public void setVRadius(int vRadius) {
this.vRadius = vRadius;
}
public int getVRadius() {
return vRadius;
}
public void setRadius(int radius) {
this.hRadius = this.vRadius = radius;
}
public int getRadius() {
return hRadius;
}
public void setThreshold(int threshold) {
this.threshold = threshold;
}
public int getThreshold() {
return threshold;
}
public String toString() {
return "Blur/Smart Blur...";
}
}
|