root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / RasterFilter.java @ 8026
History | View | Annotate | Download (7.69 KB)
1 | 8026 | nacho | /* Cresques Mapping Suite. Graphic Library for constructing mapping applications.
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2004-5.
|
||
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 | * For more information, contact:
|
||
20 | *
|
||
21 | * cresques@gmail.com
|
||
22 | */
|
||
23 | package org.cresques.filter; |
||
24 | |||
25 | import java.awt.Image; |
||
26 | import java.awt.image.BufferedImage; |
||
27 | import java.util.Hashtable; |
||
28 | |||
29 | import org.cresques.geo.ViewPortData; |
||
30 | import org.cresques.io.data.RasterBuf; |
||
31 | import org.cresques.io.datastruct.Statistic; |
||
32 | import org.cresques.px.Extent; |
||
33 | |||
34 | |||
35 | /**
|
||
36 | * Filtro para raster. Ancestro de todos los filtros.
|
||
37 | * @author Luis W. Sevilla (sevilla_lui@gva.es)
|
||
38 | */
|
||
39 | public abstract class RasterFilter implements IRasterFilter { |
||
40 | /**
|
||
41 | * Nombre del filtro. Este nombre debe asignarlo cada clase de filtro especifico y es necesario
|
||
42 | * para leer el nombre en el listado de fitros de la pila.
|
||
43 | */
|
||
44 | protected String filterName = ""; |
||
45 | protected RasterBuf raster = null; |
||
46 | protected Statistic stats = null; |
||
47 | protected Image image = null; |
||
48 | protected int height = 0; |
||
49 | protected int width = 0; |
||
50 | protected Hashtable params = new Hashtable(); |
||
51 | protected boolean isSupersampling = false; |
||
52 | protected ViewPortData viewPortData = null; |
||
53 | protected Extent extent = null; |
||
54 | /**
|
||
55 | * Variable que control la aplicaci?n o no del filtro. Si est? a false aunque est? en
|
||
56 | * la pila el filtro no se ejecutar?.
|
||
57 | */
|
||
58 | protected boolean exec = true; |
||
59 | |||
60 | /**
|
||
61 | * Vectores con los pasos usados para el supersampling
|
||
62 | */
|
||
63 | protected int[] stepX = null; |
||
64 | protected int[] stepY = null; |
||
65 | /**
|
||
66 | * Posici?n de los vectores de pasos que se est? analizando en un instante dado
|
||
67 | */
|
||
68 | protected int contX = -1, contY = -1; |
||
69 | |||
70 | /**
|
||
71 | * @author Nacho Brodin (brodin_ign@gva.es)
|
||
72 | */
|
||
73 | public static class Kernel{ |
||
74 | public double[][] kernel = null; |
||
75 | protected double divisor = 0; |
||
76 | |||
77 | public Kernel(double[][] k){ |
||
78 | this.kernel = k;
|
||
79 | |||
80 | for (int i=0;i< kernel.length;i++) |
||
81 | for (int j=0;j<kernel[0].length;j++) |
||
82 | divisor = divisor + kernel[i][j]; |
||
83 | } |
||
84 | |||
85 | public Kernel(double[][] k,double divisor){ |
||
86 | this.kernel = k;
|
||
87 | this.divisor = divisor;
|
||
88 | } |
||
89 | |||
90 | public double kernelOperation(Kernel k){ |
||
91 | double res = 0; |
||
92 | for (int i=0;i< kernel.length;i++) |
||
93 | for (int j=0;j<kernel[0].length;j++) |
||
94 | res += kernel[i][j] * k.kernel[i][j]; |
||
95 | return res;
|
||
96 | } |
||
97 | |||
98 | public double convolution (Kernel k){ |
||
99 | double res =0; |
||
100 | res = this.kernelOperation(k);
|
||
101 | if (this.divisor !=0) |
||
102 | res = res/divisor; |
||
103 | return Math.abs(res); |
||
104 | } |
||
105 | |||
106 | public double getDivisor() { |
||
107 | return divisor;
|
||
108 | } |
||
109 | |||
110 | public void setDivisor(double divisor) { |
||
111 | this.divisor = divisor;
|
||
112 | } |
||
113 | |||
114 | public int getLado(){ |
||
115 | return kernel.length;
|
||
116 | } |
||
117 | } |
||
118 | |||
119 | |||
120 | /**
|
||
121 | * Constructor
|
||
122 | */
|
||
123 | public RasterFilter() {
|
||
124 | } |
||
125 | |||
126 | /**
|
||
127 | * Aplica el filtro sobre el raster pasado pixel a pixel
|
||
128 | */
|
||
129 | public void execute() { |
||
130 | pre(); |
||
131 | if (exec) {
|
||
132 | if(isSupersampling()){
|
||
133 | contX = -1;
|
||
134 | for (int col = 0; col < width; col += stepX[contX]){ |
||
135 | contY = -1;
|
||
136 | for (int line = 0; line < height; line += stepY[contY]){ |
||
137 | processSuperSampling(col, line); |
||
138 | contY ++; |
||
139 | } |
||
140 | contX ++; |
||
141 | } |
||
142 | }else{
|
||
143 | for (int col = 0; col < width; col += 1) |
||
144 | for (int line = 0; line < height; line += 1) |
||
145 | process(col, line); |
||
146 | } |
||
147 | } |
||
148 | post(); |
||
149 | } |
||
150 | |||
151 | /**
|
||
152 | * Aplica el filtro sobre el raster pasado por lineas
|
||
153 | */
|
||
154 | public void executeLines() { |
||
155 | if (exec) {
|
||
156 | for (int y = 0; y < height; y++) |
||
157 | processLine(y); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /**
|
||
162 | * A?ade un par?metro al filtro
|
||
163 | * @param name Clave del par?metro
|
||
164 | * @param param Objeto pasado como par?metro
|
||
165 | */
|
||
166 | public void addParam(String name, Object param) { |
||
167 | params.put(name, param); |
||
168 | } |
||
169 | |||
170 | /**
|
||
171 | * Elimina un par?metro del filtro
|
||
172 | * @param name Clave del par?metro a eliminar
|
||
173 | */
|
||
174 | public void removeParam(String name){ |
||
175 | params.remove(name); |
||
176 | } |
||
177 | |||
178 | /**
|
||
179 | * Obtiene un par?metro a partir de la clave
|
||
180 | * @param name Par?metro
|
||
181 | * @return Par?metro
|
||
182 | */
|
||
183 | public Object getParam(String name){ |
||
184 | return params.get(name);
|
||
185 | } |
||
186 | |||
187 | /**
|
||
188 | * Funci?n que contiene el c?digo a ejecutar antes de aplicar el filtro
|
||
189 | */
|
||
190 | abstract public void pre(); |
||
191 | |||
192 | /**
|
||
193 | * Funci?n que contiene el c?digo a ejecutar despues de aplicar el filtro
|
||
194 | */
|
||
195 | abstract public void post(); |
||
196 | |||
197 | /**
|
||
198 | * Ejecuci?n del filtro para un pixel de la imagen
|
||
199 | */
|
||
200 | abstract public void process(int x, int y); |
||
201 | |||
202 | /**
|
||
203 | * Ejecuci?n del filtro para un pixel de la imagen
|
||
204 | */
|
||
205 | abstract public void processSuperSampling(int x, int y); |
||
206 | |||
207 | /**
|
||
208 | * Ejecuci?n del filtro para una l?nea de la imagen
|
||
209 | */
|
||
210 | abstract public void processLine(int y); |
||
211 | |||
212 | /**
|
||
213 | * Obtiene el tipo de datos del raster de entrada
|
||
214 | */
|
||
215 | abstract public int getInRasterDataType(); |
||
216 | |||
217 | /**
|
||
218 | * Obtiene el tipo de datos del raster de salida
|
||
219 | */
|
||
220 | abstract public int getOutRasterDataType(); |
||
221 | |||
222 | /**
|
||
223 | * Obtiene el resultado del filtro despues de su ejecuci?n a trav?s de una clave
|
||
224 | * @param name clave para obtener un objeto resultado del filtro.
|
||
225 | */
|
||
226 | abstract public Object getResult(String name); |
||
227 | |||
228 | /**
|
||
229 | * @param viewPortData The viewPortData to set.
|
||
230 | */
|
||
231 | public void setViewPortData(ViewPortData viewPortData) { |
||
232 | this.viewPortData = viewPortData;
|
||
233 | } |
||
234 | /**
|
||
235 | * @param extent The extent to set.
|
||
236 | */
|
||
237 | public void setExtent(Extent extent) { |
||
238 | this.extent = extent;
|
||
239 | } |
||
240 | |||
241 | /**
|
||
242 | * Obtiene true si el filtro va a ser ejecutado o false si no va a serlo
|
||
243 | * @return
|
||
244 | */
|
||
245 | public boolean isExec() { |
||
246 | return exec;
|
||
247 | } |
||
248 | |||
249 | /**
|
||
250 | * Asigna el valor a la variable exec. Esta estar? a true si el filtro se ejecutar? la pr?xima
|
||
251 | * vez que se repinte o false si no se ejecuta.
|
||
252 | * @param exec
|
||
253 | */
|
||
254 | public void setExec(boolean exec) { |
||
255 | this.exec = exec;
|
||
256 | } |
||
257 | |||
258 | /**
|
||
259 | * Asigna el valor del paso (Solo cuando se hace supersampling).
|
||
260 | * @param stepx paso en X
|
||
261 | * @param stepy paso en Y
|
||
262 | */
|
||
263 | public void setStep(int[] stepX, int[] stepY){ |
||
264 | if(stepX != null && stepY != null){ |
||
265 | isSupersampling = true;
|
||
266 | this.stepX = stepX;
|
||
267 | this.stepY = stepY;
|
||
268 | }else{
|
||
269 | isSupersampling = false;
|
||
270 | stepX = stepY = null;
|
||
271 | } |
||
272 | } |
||
273 | |||
274 | /**
|
||
275 | * Obtiene si la imagen pasada tiene supersampling o no
|
||
276 | * @return true si en el dibujado se ha aplicado supersampling y false si no se ha aplicado.
|
||
277 | */
|
||
278 | protected boolean isSupersampling(){ |
||
279 | return isSupersampling;
|
||
280 | } |
||
281 | |||
282 | /**
|
||
283 | * Obtiene el nombre del filtro. Este nombre debe asignarlo cada clase
|
||
284 | * de filtro especifico y es necesario para leer el nombre en el listado de fitros de la pila.
|
||
285 | * @return Cadena que representa el nombre del filtro.
|
||
286 | */
|
||
287 | public String getFilterName() { |
||
288 | return filterName;
|
||
289 | } |
||
290 | |||
291 | /**
|
||
292 | * ASigna el nombre del filtro.
|
||
293 | * @param fName Cadena que representa el identificador del filtro
|
||
294 | */
|
||
295 | public void setFilterName(String fName) { |
||
296 | filterName = fName; |
||
297 | } |
||
298 | } |