root / branches / piloto3d / libraries / libCq CMS for java.old / src / org / cresques / filter / convolution / ConvolutionIntegerFilter.java @ 9523
History | View | Annotate | Download (6.35 KB)
1 |
package org.cresques.filter.convolution; |
---|---|
2 |
|
3 |
import org.cresques.filter.RasterFilter; |
4 |
import org.cresques.io.data.RasterBuf; |
5 |
|
6 |
public class ConvolutionIntegerFilter extends ConvolutionFilter { |
7 |
|
8 |
/**
|
9 |
* Constructor sin par?metros. Este es usado principalmente desde controlTypes
|
10 |
* ya que se instancia con newInstance y sin par?metros. Poseriormente le aplica
|
11 |
* el nombre al filtro con la funci?n setFilterName de RasterFilter.
|
12 |
*/
|
13 |
public ConvolutionIntegerFilter() {}
|
14 |
|
15 |
/**
|
16 |
* Constructor para la asignaci?n del identificador del filtro
|
17 |
* @param fName Cadena que representa el identificador del filtro
|
18 |
*/
|
19 |
public ConvolutionIntegerFilter(String fName) { |
20 |
super(fName);
|
21 |
} |
22 |
|
23 |
public void process(int col, int line) { |
24 |
int[] pxOut = new int[4]; |
25 |
int[] px = new int[4]; |
26 |
int ladoVentana = kernel.getLado();
|
27 |
int semiLado = (ladoVentana-1)>>1; |
28 |
|
29 |
double ventanaR[][]=new double[ladoVentana][ladoVentana]; |
30 |
double ventanaG[][]=new double[ladoVentana][ladoVentana]; |
31 |
double ventanaB[][]=new double[ladoVentana][ladoVentana]; |
32 |
|
33 |
Kernel kernelRGB[]=new Kernel[3]; |
34 |
|
35 |
raster.getElemInt(line, col, px); |
36 |
|
37 |
if((col-semiLado >= 0) && (line-semiLado >= 0) &&(col+semiLado < width)&&(line+semiLado < height)) |
38 |
{ |
39 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
40 |
for (int i=-semiLado;i<=semiLado;i++) |
41 |
for(int j=-semiLado;j<=semiLado;j++) |
42 |
{ |
43 |
raster.getElemInt(line+j, col+i,px); |
44 |
ventanaR[i+semiLado][j+semiLado] =px[0];
|
45 |
ventanaG[i+semiLado][j+semiLado] =px[1];
|
46 |
ventanaB[i+semiLado][j+semiLado] =px[2];
|
47 |
} |
48 |
kernelRGB[0]=new Kernel(ventanaR); |
49 |
kernelRGB[1]=new Kernel(ventanaG); |
50 |
kernelRGB[2]=new Kernel(ventanaB); |
51 |
|
52 |
pxOut[0]= (int)kernel.convolution(kernelRGB[0]); |
53 |
pxOut[1]= (int)kernel.convolution(kernelRGB[1]); |
54 |
pxOut[2]= (int)kernel.convolution(kernelRGB[2]); |
55 |
|
56 |
if (umbral>0){ |
57 |
if(pxOut[0]>=umbral)pxOut[0]=255; |
58 |
else pxOut[0]=0; |
59 |
if(pxOut[1]>=umbral)pxOut[1]=255; |
60 |
else pxOut[1]=0; |
61 |
if(pxOut[2]>=umbral)pxOut[2]=255; |
62 |
else pxOut[2]=0; |
63 |
} |
64 |
else{
|
65 |
if (pxOut[0]<0) pxOut[0]=0; |
66 |
else if (pxOut[0]>255) pxOut[0]=255; |
67 |
if (pxOut[1]<0) pxOut[1]=0; |
68 |
else if (pxOut[1]>255) pxOut[1]=255; |
69 |
if (pxOut[2]<0) pxOut[2]=0; |
70 |
else if (pxOut[2]>255) pxOut[2]=255; |
71 |
} |
72 |
|
73 |
rasterResult.setElemInt(line, col,pxOut); |
74 |
} |
75 |
else rasterResult.setElemInt(line, col,pxOut);
|
76 |
|
77 |
} |
78 |
|
79 |
public void processSuperSampling(int col, int line) { |
80 |
int[] px = new int[4]; |
81 |
int[] pxOut = new int[4]; |
82 |
int ladoVentana = kernel.getLado();
|
83 |
RasterFilter.Kernel kernelRGB[];
|
84 |
|
85 |
raster.getElemInt(line, col,px); |
86 |
|
87 |
// Obtener el vector con la ventanas de muestras (una por componente RGB)
|
88 |
kernelRGB=extractSubRaster(ladoVentana,col,line); |
89 |
if(kernelRGB!=null) |
90 |
{ |
91 |
pxOut[0]= (int)kernel.convolution(kernelRGB[0]); |
92 |
pxOut[1]= (int)kernel.convolution(kernelRGB[1]); |
93 |
pxOut[2]= (int)kernel.convolution(kernelRGB[2]); |
94 |
|
95 |
if (umbral>0){ |
96 |
if(pxOut[0]>=umbral)pxOut[0]=255; |
97 |
else pxOut[0]=0; |
98 |
if(pxOut[1]>=umbral)pxOut[1]=255; |
99 |
else pxOut[1]=0; |
100 |
if(pxOut[2]>=umbral)pxOut[2]=255; |
101 |
else pxOut[2]=0; |
102 |
} |
103 |
else{
|
104 |
if (pxOut[0]<0) pxOut[0]=0; |
105 |
else if (pxOut[0]>255) pxOut[0]=255; |
106 |
if (pxOut[1]<0) pxOut[1]=0; |
107 |
else if (pxOut[1]>255) pxOut[1]=255; |
108 |
if (pxOut[2]<0) pxOut[2]=0; |
109 |
else if (pxOut[2]>255) pxOut[2]=255; |
110 |
} |
111 |
|
112 |
for(int i = col; i < width && i < (col + stepX[contX + 1]); i++) |
113 |
for(int j = line; j < height && j < (line + stepY[contY + 1]); j++) |
114 |
rasterResult.setElemInt(j, i,pxOut); |
115 |
} |
116 |
else
|
117 |
for(int i = col; i < rasterResult.getWidth() && i < (col + stepX[contX + 1]); i++) |
118 |
for(int j = line; j < rasterResult.getHeight() && j < (line + stepY[contY + 1]); j++) |
119 |
rasterResult.setElemInt(j, i,px); |
120 |
|
121 |
} |
122 |
|
123 |
public void processLine(int y) { |
124 |
// TODO Auto-generated method stub
|
125 |
|
126 |
} |
127 |
|
128 |
public int getInRasterDataType() { |
129 |
return RasterBuf.TYPE_INT;
|
130 |
} |
131 |
|
132 |
public int getOutRasterDataType() { |
133 |
return RasterBuf.TYPE_INT;
|
134 |
} |
135 |
|
136 |
public Object getResult(String name) { |
137 |
if (name.equals("raster")) { |
138 |
return (Object) this.rasterResult; |
139 |
} else {
|
140 |
return null; |
141 |
} |
142 |
} |
143 |
|
144 |
public void pre() { |
145 |
exec = true;
|
146 |
this.raster = (RasterBuf)params.get("raster"); |
147 |
height = raster.getHeight(); |
148 |
width = raster.getWidth(); |
149 |
rasterResult = new RasterBuf(raster.getDataType(),width,height,raster.getBandCount(),true); |
150 |
|
151 |
super.pre();
|
152 |
} |
153 |
|
154 |
/**
|
155 |
* @param ladoVentana Lado del subraster a extraer
|
156 |
* @param x Coordenada x del pixel central
|
157 |
* @param y Coordenada y del pixel central
|
158 |
* @return
|
159 |
*/
|
160 |
public Kernel[] extractSubRaster(int ladoVentana,int x,int y){ |
161 |
|
162 |
int k,indiceX,indiceY,origenX,origenY;
|
163 |
int[] px = new int[4]; |
164 |
int semiLado = (ladoVentana-1)>>1; |
165 |
int offsetX=0; |
166 |
int offsetY=0; |
167 |
|
168 |
Kernel kernelRGB[] = null; |
169 |
|
170 |
// Obtener el vector con las ventanas de muestras (una por componente RGB) **************************
|
171 |
// Me situo en la esquina superior izquierda del "kernel"
|
172 |
if ((contX+1-semiLado>=0) && (contY+1-semiLado>=0)&& (contX+1+semiLado<stepX.length) && (contY+1+semiLado<stepY.length)){ |
173 |
|
174 |
// Calcular el alcance del kernel cuyo centro es (x,y)
|
175 |
for (int i=0;i<semiLado;i++){ |
176 |
offsetX=offsetX+stepX[contX+i+1];
|
177 |
offsetY=offsetY+stepY[contY+i+1];
|
178 |
} |
179 |
if((x+offsetX<width)&&(y+offsetY<height)){
|
180 |
|
181 |
double ventanaR[][] = new double[ladoVentana][ladoVentana]; |
182 |
double ventanaG[][] = new double[ladoVentana][ladoVentana]; |
183 |
double ventanaB[][] = new double[ladoVentana][ladoVentana]; |
184 |
kernelRGB = new Kernel[3]; |
185 |
|
186 |
origenX=x; |
187 |
origenY=y; |
188 |
for (int i=0;i<semiLado;i++){ |
189 |
origenX=origenX-stepX[contX-i]; |
190 |
origenY=origenY-stepY[contY-i]; |
191 |
} |
192 |
//Recorro el kernel seg?n los step
|
193 |
k=0;
|
194 |
indiceX=origenX; |
195 |
for (int i=-semiLado;i<=semiLado;i++){ |
196 |
indiceY=origenY; |
197 |
for(int j=-semiLado;j<=semiLado;j++){ |
198 |
raster.getElemInt(indiceY,indiceX,px); |
199 |
ventanaR[i+semiLado][j+semiLado] =px[0];
|
200 |
ventanaG[i+semiLado][j+semiLado] =px[1];
|
201 |
ventanaB[i+semiLado][j+semiLado] =px[2];
|
202 |
indiceY=indiceY+stepY[contY+j+1];
|
203 |
k++; |
204 |
} |
205 |
indiceX=indiceX+stepX[contX+i+1];
|
206 |
} |
207 |
//**************************************************************************************************
|
208 |
kernelRGB[0]= new Kernel(ventanaR); |
209 |
kernelRGB[1]= new Kernel(ventanaG); |
210 |
kernelRGB[2]= new Kernel(ventanaB); |
211 |
} |
212 |
} |
213 |
return kernelRGB;
|
214 |
} |
215 |
|
216 |
} |