root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / convolution / ConvolutionImageFilter.java @ 8026
History | View | Annotate | Download (7.7 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
|
20 |
|
21 |
package org.cresques.filter.convolution; |
22 |
|
23 |
import java.awt.Image; |
24 |
import java.awt.image.BufferedImage; |
25 |
|
26 |
import org.cresques.io.data.RasterBuf; |
27 |
import org.gvsig.i18n.Messages; |
28 |
|
29 |
/**
|
30 |
* Filtro de Convoluci?n que se aplica en la imagen. Toma como entrada la imagen,
|
31 |
* el kernel en que se base la convoluci?n y el umbral (cero para no umbralizar).
|
32 |
*
|
33 |
* @author Diego Guerrero Sevilla <diego.guerrero@uclm.es>
|
34 |
*
|
35 |
*/
|
36 |
|
37 |
public class ConvolutionImageFilter extends ConvolutionFilter { |
38 |
|
39 |
/**
|
40 |
* Constructor sin par?metros. Este es usado principalmente desde controlTypes
|
41 |
* ya que se instancia con newInstance y sin par?metros. Poseriormente le aplica
|
42 |
* el nombre al filtro con la funci?n setFilterName de RasterFilter.
|
43 |
*/
|
44 |
public ConvolutionImageFilter() {}
|
45 |
|
46 |
/**
|
47 |
* Constructor para la asignaci?n del identificador del filtro
|
48 |
* @param fName Cadena que representa el identificador del filtro
|
49 |
*/
|
50 |
public ConvolutionImageFilter(String fName) { |
51 |
super(fName);
|
52 |
} |
53 |
|
54 |
public Object getResult(String name) { |
55 |
if (name.equals("raster")) { |
56 |
return (Object) this.imageResult; |
57 |
} else {
|
58 |
return null; |
59 |
} |
60 |
} |
61 |
|
62 |
public void pre() { |
63 |
exec = true;
|
64 |
this.image = (Image) params.get("raster"); |
65 |
height = image.getHeight(null);
|
66 |
width = image.getWidth(null);
|
67 |
imageResult = new BufferedImage(image.getWidth(null),image.getHeight(null), BufferedImage.TYPE_INT_ARGB); |
68 |
|
69 |
super.pre();
|
70 |
} |
71 |
|
72 |
public void process(int col, int line) { |
73 |
int px;
|
74 |
int outR,outG,outB;
|
75 |
int ladoVentana = kernel.getLado();
|
76 |
int semiLado = (ladoVentana-1)>>1; |
77 |
|
78 |
double ventanaR[][]=new double[ladoVentana][ladoVentana]; |
79 |
double ventanaG[][]=new double[ladoVentana][ladoVentana]; |
80 |
double ventanaB[][]=new double[ladoVentana][ladoVentana]; |
81 |
|
82 |
Kernel kernelRGB[]=new Kernel[3]; |
83 |
|
84 |
px = ((BufferedImage) image).getRGB(col, line);
|
85 |
int alpha = (px & 0xff000000); //Extraigo el alpha para mantenerlo. |
86 |
|
87 |
if((col-semiLado >= 0) && (line-semiLado >= 0) &&(col+semiLado < width)&&(line+semiLado < height)) |
88 |
{ |
89 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
90 |
for (int i=-semiLado;i<=semiLado;i++) |
91 |
for(int j=-semiLado;j<=semiLado;j++) |
92 |
{ |
93 |
px = ((BufferedImage) image).getRGB(col + i, line + j);
|
94 |
ventanaR[i+semiLado][j+semiLado] =(px & 0x00ff0000) >> 16; |
95 |
ventanaG[i+semiLado][j+semiLado] =(px & 0x0000ff00) >> 8; |
96 |
ventanaB[i+semiLado][j+semiLado] =(px & 0x000000ff);
|
97 |
} |
98 |
kernelRGB[0]=new Kernel(ventanaR); |
99 |
kernelRGB[1]=new Kernel(ventanaG); |
100 |
kernelRGB[2]=new Kernel(ventanaB); |
101 |
|
102 |
outR= (int)kernel.convolution(kernelRGB[0]); |
103 |
outG= (int)kernel.convolution(kernelRGB[1]); |
104 |
outB= (int)kernel.convolution(kernelRGB[2]); |
105 |
|
106 |
if (umbral>0){ |
107 |
if(outR>=umbral)outR=255; |
108 |
else outR=0; |
109 |
if(outG>=umbral)outG=255; |
110 |
else outG=0; |
111 |
if(outB>=umbral)outB=255; |
112 |
else outB=0; |
113 |
} |
114 |
|
115 |
else{
|
116 |
if (outR<0) outR=0; |
117 |
else if (outR>255) outR=255; |
118 |
if (outG<0) outG=0; |
119 |
else if (outG>255) outG=255; |
120 |
if (outB<0) outB=0; |
121 |
else if (outB>255) outB=255; |
122 |
} |
123 |
|
124 |
((BufferedImage) imageResult).setRGB(col, line, alpha |
|
125 |
((outR << 16) & 0x00ff0000) | |
126 |
((outG << 8) & 0x0000ff00) | |
127 |
(outB & 0x000000ff));
|
128 |
} |
129 |
else ((BufferedImage) imageResult).setRGB(col, line, px); |
130 |
|
131 |
|
132 |
} |
133 |
|
134 |
public void processSuperSampling(int col, int line) { |
135 |
int px;
|
136 |
int outR,outG,outB;
|
137 |
int ladoVentana = kernel.getLado();
|
138 |
|
139 |
Kernel kernelRGB[] = null; |
140 |
|
141 |
px = ((BufferedImage) image).getRGB(col, line);
|
142 |
int alpha = (px & 0xff000000); //Extraigo el alpha para mantenerlo. |
143 |
|
144 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
145 |
kernelRGB=extractSubImage(ladoVentana, col, line); |
146 |
if(kernelRGB!=null) |
147 |
{ |
148 |
outR= (int)kernel.convolution(kernelRGB[0]); |
149 |
outG= (int)kernel.convolution(kernelRGB[1]); |
150 |
outB= (int)kernel.convolution(kernelRGB[2]); |
151 |
|
152 |
if (umbral>0){ |
153 |
if(outR>=umbral)outR=255; |
154 |
else outR=0; |
155 |
if(outG>=umbral)outG=255; |
156 |
else outG=0; |
157 |
if(outB>=umbral)outB=255; |
158 |
else outB=0; |
159 |
} |
160 |
|
161 |
else{
|
162 |
if (outR<0) outR=0; |
163 |
else if (outR>255) outR=255; |
164 |
if (outG<0) outG=0; |
165 |
else if (outG>255) outG=255; |
166 |
if (outB<0) outB=0; |
167 |
else if (outB>255) outB=255; |
168 |
} |
169 |
|
170 |
for(int j = col; j < width && j < (col + stepX[contX + 1]); j++) |
171 |
for(int i = line; i < height && i < (line + stepY[contY + 1]); i++) |
172 |
((BufferedImage) imageResult).setRGB(j, i, alpha |
|
173 |
((outR << 16) & 0x00ff0000) | |
174 |
((outG << 8) & 0x0000ff00) | |
175 |
(outB & 0x000000ff));
|
176 |
|
177 |
|
178 |
} |
179 |
else
|
180 |
for(int j = col; j < width && j < (col + stepX[contX + 1]); j++) |
181 |
for(int i = line; i < height && i < (line + stepY[contY + 1]); i++) |
182 |
((BufferedImage) imageResult).setRGB(j, i,px);
|
183 |
|
184 |
} |
185 |
|
186 |
public int getInRasterDataType() { |
187 |
return RasterBuf.TYPE_IMAGE;
|
188 |
} |
189 |
|
190 |
public int getOutRasterDataType() { |
191 |
return RasterBuf.TYPE_IMAGE;
|
192 |
} |
193 |
|
194 |
public void processLine(int y) { |
195 |
// TODO Auto-generated method stub
|
196 |
|
197 |
} |
198 |
|
199 |
/**
|
200 |
* Obtiene tres ventanas centradas en la coordena (x,y) de tama?o ladoVentana * ladoVentana.
|
201 |
*
|
202 |
* @return null si la ventana se sale de la imagen.
|
203 |
*/
|
204 |
public Kernel[] extractSubImage(int ladoVentana,int x,int y){ |
205 |
|
206 |
int indiceX,indiceY,origenX,origenY;
|
207 |
int px;
|
208 |
int semiLado = (ladoVentana-1)>>1; |
209 |
int offsetX=0; |
210 |
int offsetY=0; |
211 |
|
212 |
Kernel kernelRGB[] = null; |
213 |
|
214 |
if ((contX+1-semiLado>=0) && (contY+1-semiLado>=0)&& (contX+1+semiLado<stepX.length) && (contY+1+semiLado<stepY.length)){ |
215 |
|
216 |
// Calcular el alcance del kernel cuyo centro es (x,y)
|
217 |
for (int i=0;i<semiLado;i++){ |
218 |
offsetX=offsetX+stepX[contX+i+1];
|
219 |
offsetY=offsetY+stepY[contY+i+1];
|
220 |
} |
221 |
if((contY+1-semiLado>=0) && (x+offsetX<width)&&(y+offsetY<height)){ |
222 |
|
223 |
double ventanaR[][] = new double[ladoVentana][ladoVentana]; |
224 |
double ventanaG[][] = new double[ladoVentana][ladoVentana]; |
225 |
double ventanaB[][] = new double[ladoVentana][ladoVentana]; |
226 |
kernelRGB = new Kernel[3]; |
227 |
|
228 |
origenX=x; |
229 |
origenY=y; |
230 |
|
231 |
kernelRGB = new Kernel[3]; |
232 |
for (int i=0;i<semiLado;i++){ |
233 |
origenX=origenX-stepX[contX-i]; |
234 |
origenY=origenY-stepY[contY-i]; |
235 |
} |
236 |
//Recorro el kernel seg?n los step
|
237 |
indiceX=origenX; |
238 |
for (int i=-semiLado;i<=semiLado;i++){ |
239 |
indiceY=origenY; |
240 |
for(int j=-semiLado;j<=semiLado;j++){ |
241 |
px = ((BufferedImage) image).getRGB(indiceX, indiceY);
|
242 |
ventanaR[i+semiLado][j+semiLado] =(px & 0x00ff0000) >> 16; |
243 |
ventanaG[i+semiLado][j+semiLado] =(px & 0x0000ff00) >> 8; |
244 |
ventanaB[i+semiLado][j+semiLado] =(px & 0x000000ff);
|
245 |
indiceY=indiceY+stepY[contY+j+1];
|
246 |
|
247 |
} |
248 |
indiceX=indiceX+stepX[contX+i+1];
|
249 |
} |
250 |
//**************************************************************************************************
|
251 |
kernelRGB[0]= new Kernel(ventanaR); |
252 |
kernelRGB[1]= new Kernel(ventanaG); |
253 |
kernelRGB[2]= new Kernel(ventanaB); |
254 |
} |
255 |
} |
256 |
return kernelRGB;
|
257 |
} |
258 |
|
259 |
} |