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
| package millie.plugins.core.transform;
import java.awt.image.BufferedImage;
import java.util.Arrays;
import millie.plugins.GenericPluginFilter;
import millie.plugins.parameters.IntSliderParameter;
/**
* Lanczos resampling of an image
*
* @author Xavier Philippeau
*/
public class Resample extends GenericPluginFilter {
/**
* inner class: Kernel
*/
private class Kernel1D {
int size, normalizer, coefs[];
Kernel1D(int size, int[] c, int n) {
this.size=size;
this.coefs=c;
this.normalizer=n;
}
}
private int minsupport = 0;
public Resample() {
setPluginName("Resampling");
setLongProcessing(true);
addParameter(new IntSliderParameter("scalex", "Redimensionnement horizontal (%)", 1,200,100));
addParameter(new IntSliderParameter("scaley", "Redimensionnement vertical (%)", 1,200,100));
addParameter(new IntSliderParameter("support", "taille minimum du support", 1,5,1));
}
// ---------------------------------------------------------------------------
// GenericPluginFilter interface
// ---------------------------------------------------------------------------
@Override
public BufferedImage filter() throws Exception {
BufferedImage input = getInputImage();
double xfactor = getIntValue("scalex")*0.01;
double yfactor = getIntValue("scaley")*0.01;
this.minsupport = getIntValue("support")*2+1;
// reset kernel cache
this.kernelsCacheX = new Kernel1D[100];
this.kernelsCacheY = new Kernel1D[100];
// create empty output image
int width = input.getWidth();
int height = input.getHeight();
int newwidth = (int)(0.5+width*xfactor);
int newheight = (int)(0.5+height*yfactor);
BufferedImage output = new BufferedImage(newwidth, newheight, BufferedImage.TYPE_INT_ARGB);
// for each pixel in output image
for (int y=0; y<newheight; y++) {
for (int x=0; x<newwidth; x++) {
// ideal sample point in the source image
double xo = x/xfactor;
double yo = y/yfactor;
// separate integer part and fractionnal part
int x_int = (int)xo; double x_frac = xo - x_int;
int y_int = (int)yo; double y_frac = yo - y_int;
// get/compute resampling Kernels
Kernel1D kx = getKernelX(xfactor, x_frac);
Kernel1D ky = getKernelY(yfactor, y_frac);
// compute resampled value
int rgb = fastconvolve(input, x_int, y_int, kx, ky);
// set to output image
output.setRGB(x,y,rgb);
}
}
return output;
}
// ---------------------------------------------------------------------------
// Compute and store Lanczos kernels
// ---------------------------------------------------------------------------
private Kernel1D[] kernelsCacheX = null;
private Kernel1D[] kernelsCacheY = null;
/**
* Lanczos function: sinc(d.pi)*sinc(d.pi/support)
*
* @param support,d input parameters
* @return value of the function
*/
private double lanczos(double support, double d) {
if (d==0) return 1.0;
if (d>=support) return 0.0;
double t = d * Math.PI;
return support*Math.sin(t)*Math.sin(t/support)/(t*t);
}
/**
* Compute a Lanczos resampling kernel
*
* @param scale scale to apply on the original image
* @param frac fractionnal part of ideal sample point
* @return the Lanczos resampling kernel
*/
private Kernel1D precompute(double scale, double frac) {
// compute support size = how many source pixels for 1 sampled pixel ?
int support=(int)(1+1.0/scale);
// minimum support
if (support<minsupport) support=minsupport;
// support size must be odd
if (support%2==0) support++;
// scale limiter (minimum unit = 1 pixel)
scale = Math.min(scale, 1.0);
// construct an empty kernel
Kernel1D kernel = new Kernel1D(support,new int[support],0);
int i=0;
int halfwindow = support/2;
for(int dx=-halfwindow;dx<=halfwindow;dx++) {
// ideal sample points (in the source image)
double x = scale*(dx+frac);
// corresponding weight (=contribution) of closest source pixel
double coef = lanczos( halfwindow , x );
// store to kernel
int c = (int)(1000*coef+0.5);
kernel.coefs[i++] = c;
kernel.normalizer += c;
}
return kernel;
}
private Kernel1D getKernel(Kernel1D[] cache,double scale, double frac) {
int kid = (int)(frac*100);
Kernel1D k = cache[kid];
if(k==null) {
k = precompute(scale, frac);
cache[kid]=k;
if (k.size>tmpbuffer_r.length) tmpbuffer_r = new int[k.size];
if (k.size>tmpbuffer_g.length) tmpbuffer_g = new int[k.size];
if (k.size>tmpbuffer_b.length) tmpbuffer_b = new int[k.size];
}
return k;
}
private Kernel1D getKernelX(double scale, double frac) {
return getKernel(kernelsCacheX,scale,frac);
}
private Kernel1D getKernelY(double scale, double frac) {
return getKernel(kernelsCacheY,scale,frac);
}
// ---------------------------------------------------------------------------
// Fast 2D convolution (separation of the 2 kernels)
// ---------------------------------------------------------------------------
// temporary buffer used for convolution
private int[] tmpbuffer_r = new int[0];
private int[] tmpbuffer_g = new int[0];
private int[] tmpbuffer_b = new int[0];
/**
* convolve an image with a kernel for one pixel
*
* @param c input image
* @param x,y coords of the pixel
* @param kernelx,kernely kernels to use
* @return new value of the pixel
*/
private int fastconvolve(BufferedImage img, int x, int y, Kernel1D kernelx, Kernel1D kernely) {
int halfwindowy = kernely.size/2; // assume a odd size
int halfwindowx = kernelx.size/2; // assume a odd size
// empty tmpbuffer
Arrays.fill(tmpbuffer_r, 0);
Arrays.fill(tmpbuffer_g, 0);
Arrays.fill(tmpbuffer_b, 0);
// pass 1 : horizontal convolution of image lines aree stored in tmpbuffer
for(int dy=-halfwindowy;dy<=halfwindowy;dy++) {
if (y+dy<0 || y+dy>=img.getHeight()) continue;
for(int dx=-halfwindowx;dx<=halfwindowx;dx++) {
if (x+dx<0 || x+dx>=img.getWidth()) continue;
int rgb = img.getRGB(x+dx, y+dy);
int r = (rgb>>16)&0xFF;
int g = (rgb>>8)&0xFF;
int b = (rgb)&0xFF;
tmpbuffer_r[halfwindowy+dy] += kernelx.coefs[halfwindowx-dx] * r;
tmpbuffer_g[halfwindowy+dy] += kernelx.coefs[halfwindowx-dx] * g;
tmpbuffer_b[halfwindowy+dy] += kernelx.coefs[halfwindowx-dx] * b;
}
}
// pass 2 : vertical convolution of values stored in tmpbuffer
double rc=0,gc=0,bc=0;
for(int dy=-halfwindowy;dy<=halfwindowy;dy++) {
rc += kernely.coefs[halfwindowy-dy] * tmpbuffer_r[halfwindowy+dy];
gc += kernely.coefs[halfwindowy-dy] * tmpbuffer_g[halfwindowy+dy];
bc += kernely.coefs[halfwindowy-dy] * tmpbuffer_b[halfwindowy+dy];
}
// normalization
double norm = kernelx.normalizer*kernely.normalizer;
rc/=norm; gc/=norm; bc/=norm;
// return in argb format
int r = (int)Math.min(255,Math.max(0,rc+0.5));
int g = (int)Math.min(255,Math.max(0,gc+0.5));
int b = (int)Math.min(255,Math.max(0,bc+0.5));
return 0xFF000000 + (r<<16) + (g<<8) + b;
}
} |
Partager