gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / buffer / BufferInterpolation.java @ 5462
History | View | Annotate | Download (38 KB)
1 | 2443 | nbrodin | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | package org.gvsig.raster.impl.buffer; |
||
23 | |||
24 | import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
||
25 | import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException; |
||
26 | import org.gvsig.fmap.dal.coverage.process.IncrementableTask; |
||
27 | import org.gvsig.raster.impl.DefaultRasterManager; |
||
28 | import org.gvsig.raster.impl.process.RasterTask; |
||
29 | import org.gvsig.raster.impl.process.RasterTaskQueue; |
||
30 | |||
31 | /**
|
||
32 | * Clase que contiene la funcionalidad para poder interpolar un buffer de datos
|
||
33 | * por distintos m?todos.
|
||
34 | *
|
||
35 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
36 | * @author Victor Olaya
|
||
37 | */
|
||
38 | public class BufferInterpolation implements IncrementableTask { |
||
39 | |||
40 | private Buffer buffer = null; |
||
41 | private double percent = 0; |
||
42 | |||
43 | /**
|
||
44 | * Constructor. Asigna un RasterBuffer.
|
||
45 | * @param buf
|
||
46 | */
|
||
47 | public BufferInterpolation(Buffer buf) { |
||
48 | this.buffer = buf;
|
||
49 | } |
||
50 | |||
51 | /**
|
||
52 | * Ajusta el raster al ancho y alto solicitado por el vecino m?s cercano. Promedia el valor de dos
|
||
53 | * pixeles contiguos.
|
||
54 | * @param w Nuevo ancho
|
||
55 | * @param h Nuevo alto
|
||
56 | */
|
||
57 | public Buffer adjustRasterNearestNeighbourInterpolation(int w, int h) throws ProcessInterruptedException { |
||
58 | RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
||
59 | |||
60 | double stepX = (double) w / (double) buffer.getWidth(); |
||
61 | double stepY = (double) h / (double) buffer.getHeight(); |
||
62 | Buffer rasterBuf = DefaultRasterManager.getInstance().createBuffer(buffer.getDataType(), w, h, buffer.getBandCount(), true); |
||
63 | |||
64 | |||
65 | int[] bands = new int[rasterBuf.getBandCount()]; |
||
66 | for (int iBand = 0; iBand < rasterBuf.getBandCount(); iBand++) |
||
67 | bands[iBand] = iBand; |
||
68 | |||
69 | percent = 0;
|
||
70 | double multip = 100.0D / (buffer.getHeight() * bands.length); |
||
71 | switch (buffer.getDataType()) {
|
||
72 | case RasterBuffer.TYPE_BYTE:
|
||
73 | for (int iBand = 0; iBand < bands.length; iBand++) { |
||
74 | if (w <= buffer.getWidth()) { // submuestreo |
||
75 | for (int iRow = 0; iRow < buffer.getHeight(); iRow++) { |
||
76 | for (int iCol = 0; iCol < buffer.getWidth(); iCol++) |
||
77 | rasterBuf.setElem((int) (iRow * stepY), (int) (iCol * stepX), bands[iBand], buffer.getElemByte(iRow, iCol, iBand)); |
||
78 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
79 | if (task.getEvent() != null) |
||
80 | task.manageEvent(task.getEvent()); |
||
81 | } |
||
82 | } else { // supermuestreo |
||
83 | for (int iRow = 0; iRow < h; iRow++) { |
||
84 | for (int iCol = 0; iCol < w; iCol++) |
||
85 | rasterBuf.setElem(iRow, iCol, bands[iBand], buffer.getElemByte((int) (iRow / stepY), (int) (iCol / stepX), iBand)); |
||
86 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
87 | if (task.getEvent() != null) |
||
88 | task.manageEvent(task.getEvent()); |
||
89 | } |
||
90 | } |
||
91 | } |
||
92 | break;
|
||
93 | case RasterBuffer.TYPE_DOUBLE:
|
||
94 | for (int iBand = 0; iBand < bands.length; iBand++) { |
||
95 | if (w <= buffer.getWidth()) { // submuestreo |
||
96 | for (int iRow = 0; iRow < buffer.getHeight(); iRow++) { |
||
97 | for (int iCol = 0; iCol < buffer.getWidth(); iCol++) |
||
98 | rasterBuf.setElem((int) (iRow * stepY), (int) (iCol * stepX), bands[iBand], buffer.getElemDouble(iRow, iCol, iBand)); |
||
99 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
100 | if (task.getEvent() != null) |
||
101 | task.manageEvent(task.getEvent()); |
||
102 | } |
||
103 | } else { // supermuestreo |
||
104 | for (int iRow = 0; iRow < h; iRow++) { |
||
105 | for (int iCol = 0; iCol < w; iCol++) |
||
106 | rasterBuf.setElem(iRow, iCol, bands[iBand], buffer.getElemDouble((int) (iRow / stepY), (int) (iCol / stepX), iBand)); |
||
107 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
108 | if (task.getEvent() != null) |
||
109 | task.manageEvent(task.getEvent()); |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 | break;
|
||
114 | case RasterBuffer.TYPE_FLOAT:
|
||
115 | for (int iBand = 0; iBand < bands.length; iBand++) { |
||
116 | if (w <= buffer.getWidth()) { // submuestreo |
||
117 | for (int iRow = 0; iRow < buffer.getHeight(); iRow++) { |
||
118 | for (int iCol = 0; iCol < buffer.getWidth(); iCol++) |
||
119 | rasterBuf.setElem((int) (iRow * stepY), (int) (iCol * stepX), bands[iBand], buffer.getElemFloat(iRow, iCol, iBand)); |
||
120 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
121 | if (task.getEvent() != null) |
||
122 | task.manageEvent(task.getEvent()); |
||
123 | } |
||
124 | } else { // supermuestreo |
||
125 | for (int iRow = 0; iRow < h; iRow++) { |
||
126 | for (int iCol = 0; iCol < w; iCol++) |
||
127 | rasterBuf.setElem(iRow, iCol, bands[iBand], buffer.getElemFloat((int) (iRow / stepY), (int) (iCol / stepX), iBand)); |
||
128 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
129 | if (task.getEvent() != null) |
||
130 | task.manageEvent(task.getEvent()); |
||
131 | } |
||
132 | } |
||
133 | } |
||
134 | break;
|
||
135 | case RasterBuffer.TYPE_INT:
|
||
136 | for (int iBand = 0; iBand < bands.length; iBand++) { |
||
137 | if (w <= buffer.getWidth()) { // submuestreo |
||
138 | for (int iRow = 0; iRow < buffer.getHeight(); iRow++) { |
||
139 | for (int iCol = 0; iCol < buffer.getWidth(); iCol++) |
||
140 | rasterBuf.setElem((int) (iRow * stepY), (int) (iCol * stepX), bands[iBand], buffer.getElemInt(iRow, iCol, iBand)); |
||
141 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
142 | if (task.getEvent() != null) |
||
143 | task.manageEvent(task.getEvent()); |
||
144 | } |
||
145 | } else { // supermuestreo |
||
146 | for (int iRow = 0; iRow < h; iRow++) { |
||
147 | for (int iCol = 0; iCol < w; iCol++) |
||
148 | rasterBuf.setElem(iRow, iCol, bands[iBand], buffer.getElemInt((int) (iRow / stepY), (int) (iCol / stepX), iBand)); |
||
149 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
150 | if (task.getEvent() != null) |
||
151 | task.manageEvent(task.getEvent()); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | break;
|
||
156 | case RasterBuffer.TYPE_USHORT:
|
||
157 | case RasterBuffer.TYPE_SHORT:
|
||
158 | for (int iBand = 0; iBand < bands.length; iBand++) { |
||
159 | if (w <= buffer.getWidth()) { // submuestreo |
||
160 | for (int iRow = 0; iRow < buffer.getHeight(); iRow++) { |
||
161 | for (int iCol = 0; iCol < buffer.getWidth(); iCol++) |
||
162 | rasterBuf.setElem((int) (iRow * stepY), (int) (iCol * stepX), bands[iBand], buffer.getElemShort(iRow, iCol, iBand)); |
||
163 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
164 | if (task.getEvent() != null) |
||
165 | task.manageEvent(task.getEvent()); |
||
166 | } |
||
167 | } else { // supermuestreo |
||
168 | for (int iRow = 0; iRow < h; iRow++) { |
||
169 | for (int iCol = 0; iCol < w; iCol++) |
||
170 | rasterBuf.setElem(iRow, iCol, bands[iBand], buffer.getElemShort((int) (iRow / stepY), (int) (iCol / stepX), iBand)); |
||
171 | percent = (iBand * buffer.getHeight() + iRow) * multip; |
||
172 | if (task.getEvent() != null) |
||
173 | task.manageEvent(task.getEvent()); |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | break;
|
||
178 | } |
||
179 | return rasterBuf;
|
||
180 | } |
||
181 | |||
182 | /**
|
||
183 | * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n bilineal. Promedia
|
||
184 | * el valor de cuatro pixeles adyacentes.
|
||
185 | * <P>
|
||
186 | * Para cada pixel del raster A:(x, y) obtiene el B:(x + 1, y), C:(x, y + 1), D:(x + 1, y + 1)
|
||
187 | * Para cada valor del kernel se calcula un valor 'd' que es un peso dependiendo de su posici?n.
|
||
188 | * Este peso depende de la posici?n del pixel destino dentro del origen. La posici?n del pixel destino
|
||
189 | * en el origen es un valor decimal que puede ir de 0 a 1. Si est? muy pegado a la esquina superior
|
||
190 | * izquierda estar? cercano a 0 y si est? muy pegado a la esquina inferior derecha estar? cercano a 1.
|
||
191 | * Este valor est? representado por 'dx' y 'dy'.
|
||
192 | * </P>
|
||
193 | * <P>
|
||
194 | * Los pesos aplicados son a
|
||
195 | * <UL>
|
||
196 | * <LI>A (1-dx) * (1-dy)</LI>
|
||
197 | * <LI>B dx * (1-dy)</LI>
|
||
198 | * <LI>C (1-dx) * dy</LI>
|
||
199 | * <LI>D dx * dy</LI>
|
||
200 | * </UL>
|
||
201 | * La variable 'z' contiene el valor acumulado de cada peso por el valor del pixel.
|
||
202 | * La variable 'n' contiene el valor acumulado de los pesos de los cuatro pixeles.
|
||
203 | * El valor final del pixel ser? 'z/n', es decir un promedio del valor de los cuatro teniendo
|
||
204 | * en cuenta el peso de cada uno.
|
||
205 | * </P>
|
||
206 | * @param w Nuevo ancho del buffer de salida
|
||
207 | * @param h Nuevo alto del buffer de salida
|
||
208 | */
|
||
209 | public Buffer adjustRasterBilinearInterpolation(int w, int h) throws ProcessInterruptedException { |
||
210 | RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
||
211 | |||
212 | double pxSizeX = (double) buffer.getWidth() / (double) w; |
||
213 | double pxSizeY = (double) buffer.getHeight() / (double) h; |
||
214 | Buffer rasterBuf = DefaultRasterManager.getInstance().createBuffer(buffer.getDataType(), w, h, buffer.getBandCount(), true); |
||
215 | |||
216 | double posX, posY;
|
||
217 | double dx = 0D, dy = 0D; |
||
218 | |||
219 | percent = 0;
|
||
220 | double multip = 100.0D / (h * buffer.getBandCount()); |
||
221 | |||
222 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) { |
||
223 | posY = pxSizeY / 2D;
|
||
224 | switch (buffer.getDataType()) {
|
||
225 | case RasterBuffer.TYPE_BYTE:
|
||
226 | for (int iRow = 0; iRow < h; iRow++) { |
||
227 | dy = posY - ((int) posY);
|
||
228 | posX = pxSizeX / 2D;
|
||
229 | for (int iCol = 0; iCol < w; iCol++) { |
||
230 | dx = posX - ((int) posX);
|
||
231 | try {
|
||
232 | double[] kernel = getKernelByte(((int) posX), ((int) posY), iBand); |
||
233 | rasterBuf.setElem(iRow, iCol, iBand, (byte) ((byte) getBilinearValue(dx, dy, kernel) & 0xff)); |
||
234 | } catch (ArrayIndexOutOfBoundsException e) { |
||
235 | // System.out.println(posX + " " + posY);
|
||
236 | } |
||
237 | posX += pxSizeX; |
||
238 | } |
||
239 | posY += pxSizeY; |
||
240 | percent = (iBand * h + iRow) * multip; |
||
241 | if (task.getEvent() != null) |
||
242 | task.manageEvent(task.getEvent()); |
||
243 | } |
||
244 | break;
|
||
245 | case RasterBuffer.TYPE_SHORT:
|
||
246 | for (int iRow = 0; iRow < h; iRow++) { |
||
247 | dy = posY - ((int) posY);
|
||
248 | posX = pxSizeX / 2D;
|
||
249 | for (int iCol = 0; iCol < w; iCol++) { |
||
250 | dx = posX - ((int) posX);
|
||
251 | try {
|
||
252 | double[] kernel = getKernelShort(((int) posX), ((int) posY), iBand); |
||
253 | rasterBuf.setElem(iRow, iCol, iBand, (short) ((short) getBilinearValue(dx, dy, kernel) & 0xffff)); |
||
254 | } catch (ArrayIndexOutOfBoundsException e) { |
||
255 | // System.out.println(posX + " " + posY);
|
||
256 | } |
||
257 | posX += pxSizeX; |
||
258 | } |
||
259 | posY += pxSizeY; |
||
260 | percent = (iBand * h + iRow) * multip; |
||
261 | if (task.getEvent() != null) |
||
262 | task.manageEvent(task.getEvent()); |
||
263 | } |
||
264 | break;
|
||
265 | case RasterBuffer.TYPE_INT:
|
||
266 | for (int iRow = 0; iRow < h; iRow++) { |
||
267 | dy = posY - ((int) posY);
|
||
268 | posX = pxSizeX / 2D;
|
||
269 | for (int iCol = 0; iCol < w; iCol++) { |
||
270 | dx = posX - ((int) posX);
|
||
271 | try {
|
||
272 | double[] kernel = getKernelInt(((int) posX), ((int) posY), iBand); |
||
273 | rasterBuf.setElem(iRow, iCol, iBand, (int) ((int) getBilinearValue(dx, dy, kernel) & 0xff)); |
||
274 | } catch (ArrayIndexOutOfBoundsException e) { |
||
275 | // System.out.println(posX + " " + posY);
|
||
276 | } |
||
277 | posX += pxSizeX; |
||
278 | } |
||
279 | posY += pxSizeY; |
||
280 | percent = (iBand * h + iRow) * multip; |
||
281 | if (task.getEvent() != null) |
||
282 | task.manageEvent(task.getEvent()); |
||
283 | } |
||
284 | break;
|
||
285 | case RasterBuffer.TYPE_FLOAT:
|
||
286 | for (int iRow = 0; iRow < h; iRow++) { |
||
287 | dy = posY - ((int) posY);
|
||
288 | posX = pxSizeX / 2D;
|
||
289 | for (int iCol = 0; iCol < w; iCol++) { |
||
290 | dx = posX - ((int) posX);
|
||
291 | try {
|
||
292 | double[] kernel = getKernelFloat(((int) posX), ((int) posY), iBand); |
||
293 | rasterBuf.setElem(iRow, iCol, iBand, (float) getBilinearValue(dx, dy, kernel));
|
||
294 | } catch (ArrayIndexOutOfBoundsException e) { |
||
295 | // System.out.println(posX + " " + posY);
|
||
296 | } |
||
297 | posX += pxSizeX; |
||
298 | } |
||
299 | posY += pxSizeY; |
||
300 | percent = (iBand * h + iRow) * multip; |
||
301 | if (task.getEvent() != null) |
||
302 | task.manageEvent(task.getEvent()); |
||
303 | } |
||
304 | break;
|
||
305 | case RasterBuffer.TYPE_DOUBLE:
|
||
306 | for (int iRow = 0; iRow < h; iRow++) { |
||
307 | dy = posY - ((int) posY);
|
||
308 | posX = pxSizeX / 2D;
|
||
309 | for (int iCol = 0; iCol < w; iCol++) { |
||
310 | dx = posX - ((int) posX);
|
||
311 | try {
|
||
312 | double[] kernel = getKernelDouble(((int) posX), ((int) posY), iBand); |
||
313 | rasterBuf.setElem(iRow, iCol, iBand, (double) getBilinearValue(dx, dy, kernel));
|
||
314 | } catch (ArrayIndexOutOfBoundsException e) { |
||
315 | // System.out.println(posX + " " + posY);
|
||
316 | } |
||
317 | posX += pxSizeX; |
||
318 | } |
||
319 | posY += pxSizeY; |
||
320 | percent = (iBand * h + iRow) * multip; |
||
321 | if (task.getEvent() != null) |
||
322 | task.manageEvent(task.getEvent()); |
||
323 | } |
||
324 | break;
|
||
325 | } |
||
326 | } |
||
327 | |||
328 | return rasterBuf;
|
||
329 | } |
||
330 | |||
331 | /**
|
||
332 | * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n de distancia inversa.
|
||
333 | * Asigna el valor de un pixel en funci?n inversa de la distancia.
|
||
334 | * <P>
|
||
335 | * Para cada pixel del raster A:(x, y) obtiene el B:(x + 1, y), C:(x, y + 1), D:(x + 1, y + 1)
|
||
336 | * Para cada valor del kernel se calcula un valor 'd' que es un peso dependiendo de su posici?n.
|
||
337 | * Este peso ser? dependiente de la posici?n del pixel destino dentro del origen. La posici?n del pixel destino
|
||
338 | * en el origen es un valor decimal que puede ir de 0 a 1. Si est? muy pegado a la esquina superior
|
||
339 | * izquierda estar? cercano a 0 y si est? muy pegado a la esquina inferior derecha estar? cercano a 1.
|
||
340 | * Este valor est? representado por 'dx' y 'dy'. En este caso, y a diferencia del m?todo
|
||
341 | * bilinear el peso vendr? representado por la inversa de la distancia entre la posici?n
|
||
342 | * dentro del pixel y el origen del mismo.
|
||
343 | * </P>
|
||
344 | * <P>
|
||
345 | * Los pesos aplicados son a
|
||
346 | * <UL>
|
||
347 | * <LI>A 1 / sqrt((1-dx) * (1-dy))</LI>
|
||
348 | * <LI>B 1 / sqrt(dx * (1-dy))</LI>
|
||
349 | * <LI>C 1 / sqrt((1-dx) * dy)</LI>
|
||
350 | * <LI>D 1 / sqrt(dx * dy)</LI>
|
||
351 | * </UL>
|
||
352 | * La variable 'z' contiene el valor acumulado de cada peso por el valor del pixel.
|
||
353 | * La variable 'n' contiene el valor acumulado de los pesos de los cuatro pixeles.
|
||
354 | * El valor final del pixel ser? 'z/n', es decir un promedio del valor de los cuatro teniendo
|
||
355 | * en cuenta el peso de cada uno.
|
||
356 | * </P>
|
||
357 | * @param w Nuevo ancho del buffer de salida
|
||
358 | * @param h Nuevo alto del buffer de salida
|
||
359 | */
|
||
360 | public Buffer adjustRasterInverseDistanceInterpolation(int w, int h) throws ProcessInterruptedException { |
||
361 | RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
||
362 | |||
363 | double pxSizeX = (double) buffer.getWidth() / (double) w; |
||
364 | double pxSizeY = (double) buffer.getHeight() / (double) h; |
||
365 | Buffer rasterBuf = DefaultRasterManager.getInstance().createBuffer(buffer.getDataType(), w, h, buffer.getBandCount(), true); |
||
366 | |||
367 | double posX, posY;
|
||
368 | double dx = 0D, dy = 0D; |
||
369 | |||
370 | percent = 0;
|
||
371 | double multip = 100.0D / (h * buffer.getBandCount()); |
||
372 | |||
373 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) { |
||
374 | posY = pxSizeX / 2D;
|
||
375 | switch (buffer.getDataType()) {
|
||
376 | case RasterBuffer.TYPE_BYTE:
|
||
377 | for (int iRow = 0; iRow < h; iRow++) { |
||
378 | dy = posY - ((int) posY);
|
||
379 | posX = pxSizeX / 2D;
|
||
380 | for (int iCol = 0; iCol < w; iCol++) { |
||
381 | dx = posX - ((int) posX);
|
||
382 | try {
|
||
383 | double[] kernel = getKernelByte(((int) posX), ((int) posY), iBand); |
||
384 | rasterBuf.setElem(iRow, iCol, iBand, (byte) ((byte) getInverseDistanceValue(dx, dy, kernel) & 0xff)); |
||
385 | } catch (ArrayIndexOutOfBoundsException e) { |
||
386 | // System.out.println(posX + " " + posY);
|
||
387 | } |
||
388 | posX += pxSizeX; |
||
389 | } |
||
390 | posY += pxSizeY; |
||
391 | percent = (iBand * h + iRow) * multip; |
||
392 | if (task.getEvent() != null) |
||
393 | task.manageEvent(task.getEvent()); |
||
394 | } |
||
395 | break;
|
||
396 | case RasterBuffer.TYPE_SHORT:
|
||
397 | for (int iRow = 0; iRow < h; iRow++) { |
||
398 | dy = posY - ((int) posY);
|
||
399 | posX = pxSizeX / 2D;
|
||
400 | for (int iCol = 0; iCol < w; iCol++) { |
||
401 | dx = posX - ((int) posX);
|
||
402 | try {
|
||
403 | double[] kernel = getKernelShort(((int) posX), ((int) posY), iBand); |
||
404 | rasterBuf.setElem(iRow, iCol, iBand, (short) ((short) getInverseDistanceValue(dx, dy, kernel) & 0xffff)); |
||
405 | } catch (ArrayIndexOutOfBoundsException e) { |
||
406 | // System.out.println(posX + " " + posY);
|
||
407 | } |
||
408 | posX += pxSizeX; |
||
409 | } |
||
410 | posY += pxSizeY; |
||
411 | percent = (iBand * h + iRow) * multip; |
||
412 | if (task.getEvent() != null) |
||
413 | task.manageEvent(task.getEvent()); |
||
414 | } |
||
415 | break;
|
||
416 | case RasterBuffer.TYPE_INT:
|
||
417 | for (int iRow = 0; iRow < h; iRow++) { |
||
418 | dy = posY - ((int) posY);
|
||
419 | posX = pxSizeX / 2D;
|
||
420 | for (int iCol = 0; iCol < w; iCol++) { |
||
421 | dx = posX - ((int) posX);
|
||
422 | try {
|
||
423 | double[] kernel = getKernelInt(((int) posX), ((int) posY), iBand); |
||
424 | rasterBuf.setElem(iRow, iCol, iBand, (int) ((int) getInverseDistanceValue(dx, dy, kernel) & 0xff)); |
||
425 | } catch (ArrayIndexOutOfBoundsException e) { |
||
426 | // System.out.println(posX + " " + posY);
|
||
427 | } |
||
428 | posX += pxSizeX; |
||
429 | } |
||
430 | posY += pxSizeY; |
||
431 | percent = (iBand * h + iRow) * multip; |
||
432 | if (task.getEvent() != null) |
||
433 | task.manageEvent(task.getEvent()); |
||
434 | } |
||
435 | break;
|
||
436 | case RasterBuffer.TYPE_FLOAT:
|
||
437 | for (int iRow = 0; iRow < h; iRow++) { |
||
438 | dy = posY - ((int) posY);
|
||
439 | posX = pxSizeX / 2D;
|
||
440 | for (int iCol = 0; iCol < w; iCol++) { |
||
441 | dx = posX - ((int) posX);
|
||
442 | try {
|
||
443 | double[] kernel = getKernelFloat(((int) posX), ((int) posY), iBand); |
||
444 | rasterBuf.setElem(iRow, iCol, iBand, (float) getInverseDistanceValue(dx, dy, kernel));
|
||
445 | } catch (ArrayIndexOutOfBoundsException e) { |
||
446 | // System.out.println(posX + " " + posY);
|
||
447 | } |
||
448 | posX += pxSizeX; |
||
449 | } |
||
450 | posY += pxSizeY; |
||
451 | percent = (iBand * h + iRow) * multip; |
||
452 | if (task.getEvent() != null) |
||
453 | task.manageEvent(task.getEvent()); |
||
454 | } |
||
455 | break;
|
||
456 | case RasterBuffer.TYPE_DOUBLE:
|
||
457 | for (int iRow = 0; iRow < h; iRow++) { |
||
458 | dy = posY - ((int) posY);
|
||
459 | posX = pxSizeX / 2D;
|
||
460 | for (int iCol = 0; iCol < w; iCol++) { |
||
461 | dx = posX - ((int) posX);
|
||
462 | try {
|
||
463 | double[] kernel = getKernelDouble(((int) posX), ((int) posY), iBand); |
||
464 | rasterBuf.setElem(iRow, iCol, iBand, (double) getInverseDistanceValue(dx, dy, kernel));
|
||
465 | } catch (ArrayIndexOutOfBoundsException e) { |
||
466 | // System.out.println(posX + " " + posY);
|
||
467 | } |
||
468 | posX += pxSizeX; |
||
469 | } |
||
470 | posY += pxSizeY; |
||
471 | percent = (iBand * h + iRow) * multip; |
||
472 | if (task.getEvent() != null) |
||
473 | task.manageEvent(task.getEvent()); |
||
474 | } |
||
475 | break;
|
||
476 | } |
||
477 | } |
||
478 | return rasterBuf;
|
||
479 | } |
||
480 | |||
481 | /**
|
||
482 | * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n BSpline. Promedia
|
||
483 | * el valor de cuatro pixeles adyacentes.
|
||
484 | * @param w Nuevo ancho
|
||
485 | * @param h Nuevo alto
|
||
486 | */
|
||
487 | public Buffer adjustRasterBSplineInterpolation(int w, int h) throws ProcessInterruptedException { |
||
488 | RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
||
489 | |||
490 | double pxSizeX = (double) buffer.getWidth() / (double) w; |
||
491 | double pxSizeY = (double) buffer.getHeight() / (double) h; |
||
492 | Buffer rasterBuf = DefaultRasterManager.getInstance().createBuffer(buffer.getDataType(), w, h, buffer.getBandCount(), true); |
||
493 | |||
494 | double posX, posY;
|
||
495 | double dx = 0D, dy = 0D; |
||
496 | |||
497 | percent = 0;
|
||
498 | double multip = 100.0D / (h * buffer.getBandCount()); |
||
499 | |||
500 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) { |
||
501 | posY = pxSizeY / 2D;
|
||
502 | switch (buffer.getDataType()) {
|
||
503 | case RasterBuffer.TYPE_BYTE:
|
||
504 | for (int iRow = 0; iRow < h; iRow++) { |
||
505 | dy = posY - ((int) posY);
|
||
506 | posX = pxSizeX / 2D;
|
||
507 | for (int iCol = 0; iCol < w; iCol++) { |
||
508 | dx = posX - ((int) posX);
|
||
509 | try {
|
||
510 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
511 | if (kernel == null) { |
||
512 | double[] k = getKernelByte(((int) posX), ((int) posY), iBand); |
||
513 | rasterBuf.setElem(iRow, iCol, iBand, (byte) ((byte) getBilinearValue(dx, dy, k) & 0xff)); |
||
514 | } else
|
||
515 | rasterBuf.setElem(iRow, iCol, iBand, (byte) ((byte) getBSplineValue(dx, dy, kernel) & 0xff)); |
||
516 | } catch (ArrayIndexOutOfBoundsException e) { |
||
517 | // System.out.println(posX + " " + posY);
|
||
518 | } |
||
519 | posX += pxSizeX; |
||
520 | } |
||
521 | posY += pxSizeY; |
||
522 | percent = (iBand * h + iRow) * multip; |
||
523 | if (task.getEvent() != null) |
||
524 | task.manageEvent(task.getEvent()); |
||
525 | } |
||
526 | break;
|
||
527 | case RasterBuffer.TYPE_SHORT:
|
||
528 | for (int iRow = 0; iRow < h; iRow++) { |
||
529 | dy = posY - ((int) posY);
|
||
530 | posX = pxSizeX / 2D;
|
||
531 | for (int iCol = 0; iCol < w; iCol++) { |
||
532 | dx = posX - ((int) posX);
|
||
533 | try {
|
||
534 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
535 | if (kernel == null) { |
||
536 | double[] k = getKernelShort(((int) posX), ((int) posY), iBand); |
||
537 | rasterBuf.setElem(iRow, iCol, iBand, (short) ((short) getBilinearValue(dx, dy, k) & 0xffff)); |
||
538 | } else
|
||
539 | rasterBuf.setElem(iRow, iCol, iBand, (short) ((short) getBSplineValue(dx, dy, kernel) & 0xffff)); |
||
540 | } catch (ArrayIndexOutOfBoundsException e) { |
||
541 | // System.out.println(posX + " " + posY);
|
||
542 | } |
||
543 | posX += pxSizeX; |
||
544 | } |
||
545 | posY += pxSizeY; |
||
546 | percent = (iBand * h + iRow) * multip; |
||
547 | if (task.getEvent() != null) |
||
548 | task.manageEvent(task.getEvent()); |
||
549 | } |
||
550 | break;
|
||
551 | case RasterBuffer.TYPE_INT:
|
||
552 | for (int iRow = 0; iRow < h; iRow++) { |
||
553 | dy = posY - ((int) posY);
|
||
554 | posX = pxSizeX / 2D;
|
||
555 | for (int iCol = 0; iCol < w; iCol++) { |
||
556 | dx = posX - ((int) posX);
|
||
557 | try {
|
||
558 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
559 | if (kernel == null) { |
||
560 | double[] k = getKernelInt(((int) posX), ((int) posY), iBand); |
||
561 | rasterBuf.setElem(iRow, iCol, iBand, (int) ((int) getBilinearValue(dx, dy, k) & 0xffffffff)); |
||
562 | } else
|
||
563 | rasterBuf.setElem(iRow, iCol, iBand, (int) ((int) getBSplineValue(dx, dy, kernel) & 0xffffffff)); |
||
564 | } catch (ArrayIndexOutOfBoundsException e) { |
||
565 | // System.out.println(posX + " " + posY);
|
||
566 | } |
||
567 | posX += pxSizeX; |
||
568 | } |
||
569 | posY += pxSizeY; |
||
570 | percent = (iBand * h + iRow) * multip; |
||
571 | if (task.getEvent() != null) |
||
572 | task.manageEvent(task.getEvent()); |
||
573 | } |
||
574 | break;
|
||
575 | case RasterBuffer.TYPE_FLOAT:
|
||
576 | for (int iRow = 0; iRow < h; iRow++) { |
||
577 | dy = posY - ((int) posY);
|
||
578 | posX = pxSizeX / 2D;
|
||
579 | for (int iCol = 0; iCol < w; iCol++) { |
||
580 | dx = posX - ((int) posX);
|
||
581 | try {
|
||
582 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
583 | if (kernel == null) { |
||
584 | double[] k = getKernelFloat(((int) posX), ((int) posY), iBand); |
||
585 | rasterBuf.setElem(iRow, iCol, iBand, (float) getBilinearValue(dx, dy, k));
|
||
586 | } else
|
||
587 | rasterBuf.setElem(iRow, iCol, iBand, (float) getBSplineValue(dx, dy, kernel));
|
||
588 | } catch (ArrayIndexOutOfBoundsException e) { |
||
589 | // System.out.println(posX + " " + posY);
|
||
590 | } |
||
591 | posX += pxSizeX; |
||
592 | } |
||
593 | posY += pxSizeY; |
||
594 | percent = (iBand * h + iRow) * multip; |
||
595 | if (task.getEvent() != null) |
||
596 | task.manageEvent(task.getEvent()); |
||
597 | } |
||
598 | break;
|
||
599 | case RasterBuffer.TYPE_DOUBLE:
|
||
600 | for (int iRow = 0; iRow < h; iRow++) { |
||
601 | dy = posY - ((int) posY);
|
||
602 | posX = pxSizeX / 2D;
|
||
603 | for (int iCol = 0; iCol < w; iCol++) { |
||
604 | dx = posX - ((int) posX);
|
||
605 | try {
|
||
606 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
607 | if (kernel == null) { |
||
608 | double[] k = getKernelDouble(((int) posX), ((int) posY), iBand); |
||
609 | rasterBuf.setElem(iRow, iCol, iBand, (double) getBilinearValue(dx, dy, k));
|
||
610 | } else
|
||
611 | rasterBuf.setElem(iRow, iCol, iBand, (double) getBSplineValue(dx, dy, kernel));
|
||
612 | } catch (ArrayIndexOutOfBoundsException e) { |
||
613 | // System.out.println(posX + " " + posY);
|
||
614 | } |
||
615 | posX += pxSizeX; |
||
616 | } |
||
617 | posY += pxSizeY; |
||
618 | percent = (iBand * h + iRow) * multip; |
||
619 | if (task.getEvent() != null) |
||
620 | task.manageEvent(task.getEvent()); |
||
621 | } |
||
622 | break;
|
||
623 | } |
||
624 | } |
||
625 | return rasterBuf;
|
||
626 | } |
||
627 | |||
628 | /**
|
||
629 | * Ajusta el raster al ancho y alto solicitado ajustando con una interpolaci?n de spline bicubica.
|
||
630 | * @param w Nuevo ancho
|
||
631 | * @param h Nuevo alto
|
||
632 | */
|
||
633 | public Buffer adjustRasterBicubicSplineInterpolation(int w, int h) throws ProcessInterruptedException { |
||
634 | RasterTask task = RasterTaskQueue.get(Thread.currentThread().getId() + ""); |
||
635 | |||
636 | double pxSizeX = (double) buffer.getWidth() / (double) w; |
||
637 | double pxSizeY = (double) buffer.getHeight() / (double) h; |
||
638 | Buffer rasterBuf = DefaultRasterManager.getInstance().createBuffer(buffer.getDataType(), w, h, buffer.getBandCount(), true); |
||
639 | |||
640 | double posX, posY;
|
||
641 | double dx = 0D, dy = 0D; |
||
642 | |||
643 | percent = 0;
|
||
644 | double multip = 100.0D / (h * buffer.getBandCount()); |
||
645 | |||
646 | for (int iBand = 0; iBand < buffer.getBandCount(); iBand++) { |
||
647 | posY = pxSizeY / 2D;
|
||
648 | switch (buffer.getDataType()) {
|
||
649 | case RasterBuffer.TYPE_BYTE:
|
||
650 | for (int iRow = 0; iRow < h; iRow++) { |
||
651 | dy = posY - ((int) posY);
|
||
652 | posX = pxSizeX / 2D;
|
||
653 | for (int iCol = 0; iCol < w; iCol++) { |
||
654 | dx = posX - ((int) posX);
|
||
655 | try {
|
||
656 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
657 | if (kernel == null) { |
||
658 | double[] k = getKernelByte(((int) posX), ((int) posY), iBand); |
||
659 | rasterBuf.setElem(iRow, iCol, iBand, (byte) ((byte) getBilinearValue(dx, dy, k) & 0xff)); |
||
660 | } else
|
||
661 | rasterBuf.setElem(iRow, iCol, iBand, (byte) ((byte) getBicubicSplineValue(dx, dy, kernel) & 0xff)); |
||
662 | } catch (ArrayIndexOutOfBoundsException e) { |
||
663 | // System.out.println(posX + " " + posY);
|
||
664 | } |
||
665 | posX += pxSizeX; |
||
666 | } |
||
667 | posY += pxSizeY; |
||
668 | percent = (iBand * h + iRow) * multip; |
||
669 | if (task.getEvent() != null) |
||
670 | task.manageEvent(task.getEvent()); |
||
671 | } |
||
672 | break;
|
||
673 | case RasterBuffer.TYPE_SHORT:
|
||
674 | for (int iRow = 0; iRow < h; iRow++) { |
||
675 | dy = posY - ((int) posY);
|
||
676 | posX = pxSizeX / 2D;
|
||
677 | for (int iCol = 0; iCol < w; iCol++) { |
||
678 | dx = posX - ((int) posX);
|
||
679 | try {
|
||
680 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
681 | if (kernel == null) { |
||
682 | double[] k = getKernelShort(((int) posX), ((int) posY), iBand); |
||
683 | rasterBuf.setElem(iRow, iCol, iBand, (short) ((short) getBilinearValue(dx, dy, k) & 0xffff)); |
||
684 | } else
|
||
685 | rasterBuf.setElem(iRow, iCol, iBand, (short) ((short) getBicubicSplineValue(dx, dy, kernel) & 0xffff)); |
||
686 | } catch (ArrayIndexOutOfBoundsException e) { |
||
687 | // System.out.println(posX + " " + posY);
|
||
688 | } |
||
689 | posX += pxSizeX; |
||
690 | } |
||
691 | posY += pxSizeY; |
||
692 | percent = (iBand * h + iRow) * multip; |
||
693 | if (task.getEvent() != null) |
||
694 | task.manageEvent(task.getEvent()); |
||
695 | } |
||
696 | break;
|
||
697 | case RasterBuffer.TYPE_INT:
|
||
698 | for (int iRow = 0; iRow < h; iRow++) { |
||
699 | dy = posY - ((int) posY);
|
||
700 | posX = pxSizeX / 2D;
|
||
701 | for (int iCol = 0; iCol < w; iCol++) { |
||
702 | dx = posX - ((int) posX);
|
||
703 | try {
|
||
704 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
705 | if (kernel == null) { |
||
706 | double[] k = getKernelInt(((int) posX), ((int) posY), iBand); |
||
707 | rasterBuf.setElem(iRow, iCol, iBand, (int) ((int) getBilinearValue(dx, dy, k) & 0xffffffff)); |
||
708 | } else
|
||
709 | rasterBuf.setElem(iRow, iCol, iBand, (int) ((int) getBicubicSplineValue(dx, dy, kernel) & 0xffffffff)); |
||
710 | } catch (ArrayIndexOutOfBoundsException e) { |
||
711 | // System.out.println(posX + " " + posY);
|
||
712 | } |
||
713 | posX += pxSizeX; |
||
714 | } |
||
715 | posY += pxSizeY; |
||
716 | percent = (iBand * h + iRow) * multip; |
||
717 | if (task.getEvent() != null) |
||
718 | task.manageEvent(task.getEvent()); |
||
719 | } |
||
720 | break;
|
||
721 | case RasterBuffer.TYPE_FLOAT:
|
||
722 | for (int iRow = 0; iRow < h; iRow++) { |
||
723 | dy = posY - ((int) posY);
|
||
724 | posX = pxSizeX / 2D;
|
||
725 | for (int iCol = 0; iCol < w; iCol++) { |
||
726 | dx = posX - ((int) posX);
|
||
727 | try {
|
||
728 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
729 | if (kernel == null) { |
||
730 | double[] k = getKernelFloat(((int) posX), ((int) posY), iBand); |
||
731 | rasterBuf.setElem(iRow, iCol, iBand, (float) getBilinearValue(dx, dy, k));
|
||
732 | } else
|
||
733 | rasterBuf.setElem(iRow, iCol, iBand, (float) getBicubicSplineValue(dx, dy, kernel));
|
||
734 | } catch (ArrayIndexOutOfBoundsException e) { |
||
735 | // System.out.println(posX + " " + posY);
|
||
736 | } |
||
737 | posX += pxSizeX; |
||
738 | } |
||
739 | posY += pxSizeY; |
||
740 | percent = (iBand * h + iRow) * multip; |
||
741 | if (task.getEvent() != null) |
||
742 | task.manageEvent(task.getEvent()); |
||
743 | } |
||
744 | break;
|
||
745 | case RasterBuffer.TYPE_DOUBLE:
|
||
746 | for (int iRow = 0; iRow < h; iRow++) { |
||
747 | dy = posY - ((int) posY);
|
||
748 | posX = pxSizeX / 2D;
|
||
749 | for (int iCol = 0; iCol < w; iCol++) { |
||
750 | dx = posX - ((int) posX);
|
||
751 | try {
|
||
752 | double[][] kernel = get4x4Submatrix(((int) posX), ((int) posY), iBand); |
||
753 | if (kernel == null) { |
||
754 | double[] k = getKernelDouble(((int) posX), ((int) posY), iBand); |
||
755 | rasterBuf.setElem(iRow, iCol, iBand, (double) getBilinearValue(dx, dy, k));
|
||
756 | } else
|
||
757 | rasterBuf.setElem(iRow, iCol, iBand, (double) getBicubicSplineValue(dx, dy, kernel));
|
||
758 | } catch (ArrayIndexOutOfBoundsException e) { |
||
759 | // System.out.println(posX + " " + posY);
|
||
760 | } |
||
761 | posX += pxSizeX; |
||
762 | } |
||
763 | posY += pxSizeY; |
||
764 | percent = (iBand * h + iRow) * multip; |
||
765 | if (task.getEvent() != null) |
||
766 | task.manageEvent(task.getEvent()); |
||
767 | } |
||
768 | break;
|
||
769 | } |
||
770 | } |
||
771 | return rasterBuf;
|
||
772 | } |
||
773 | |||
774 | /**
|
||
775 | *
|
||
776 | * @param dx
|
||
777 | * @param dy
|
||
778 | * @param kernel
|
||
779 | * @return
|
||
780 | */
|
||
781 | private double getBicubicSplineValue(double dx, double dy, double[][] kernel) { |
||
782 | int i;
|
||
783 | double a0, a2, a3, b1, b2, b3;
|
||
784 | double[] c = new double[4]; |
||
785 | |||
786 | for(i = 0; i < 4; i++) { |
||
787 | a0 = kernel[0][i] - kernel[1][i]; |
||
788 | a2 = kernel[2][i] - kernel[1][i]; |
||
789 | a3 = kernel[3][i] - kernel[1][i]; |
||
790 | |||
791 | b1 = -a0 / 3.0 + a2 - a3 / 6.0; |
||
792 | b2 = a0 / 2.0 + a2 / 2.0; |
||
793 | b3 = -a0 / 6.0 - a2 / 2.0 + a3 / 6.0; |
||
794 | |||
795 | c[i] = kernel[1][i] + b1 * dx + b2 * (dx * dx) + b3 * (dx * dx * dx);
|
||
796 | } |
||
797 | |||
798 | a0 = c[0] - c[1]; |
||
799 | a2 = c[2] - c[1]; |
||
800 | a3 = c[3] - c[1]; |
||
801 | |||
802 | b1 = -a0 / 3.0 + a2 - a3 / 6.0; |
||
803 | b2 = a0 / 2.0 + a2 / 2.0; |
||
804 | b3 = -a0 / 6.0 - a2 / 2.0 + a3 / 6.0; |
||
805 | |||
806 | return( c[1] + b1 * dy + b2 * (dy * dy) + b3 * (dy * dy * dy) ); |
||
807 | } |
||
808 | |||
809 | /**
|
||
810 | *
|
||
811 | * @param dx
|
||
812 | * @param dy
|
||
813 | * @param kernel
|
||
814 | * @return
|
||
815 | */
|
||
816 | private double getBSplineValue(double dx, double dy, double[][] kernel) { |
||
817 | int i = 0, ix = 0, iy = 0; |
||
818 | double px = 0, py = 0, z = 0; |
||
819 | double[] Rx = new double[4]; |
||
820 | double[] Ry = new double[4]; |
||
821 | |||
822 | for(i = 0, px = -1.0 - dx, py = -1.0 - dy; i < 4; i++, px++, py++){ |
||
823 | Rx[i] = 0.0;
|
||
824 | Ry[i] = 0.0;
|
||
825 | |||
826 | if( (z = px + 2.0) > 0.0 ) |
||
827 | Rx[i] += z * z * z; |
||
828 | if( (z = px + 1.0) > 0.0 ) |
||
829 | Rx[i] += -4.0 * z * z * z;
|
||
830 | if( (z = px + 0.0) > 0.0 ) |
||
831 | Rx[i] += 6.0 * z * z * z;
|
||
832 | if( (z = px - 1.0) > 0.0 ) |
||
833 | Rx[i] += -4.0 * z * z * z;
|
||
834 | if( (z = py + 2.0) > 0.0 ) |
||
835 | Ry[i] += z * z * z; |
||
836 | if( (z = py + 1.0) > 0.0 ) |
||
837 | Ry[i] += -4.0 * z * z * z;
|
||
838 | if( (z = py + 0.0) > 0.0 ) |
||
839 | Ry[i] += 6.0 * z * z * z;
|
||
840 | if( (z = py - 1.0) > 0.0 ) |
||
841 | Ry[i] += -4.0 * z * z * z;
|
||
842 | |||
843 | Rx[i] /= 6.0;
|
||
844 | Ry[i] /= 6.0;
|
||
845 | } |
||
846 | |||
847 | for(iy = 0, z = 0.0; iy < 4; iy++) { |
||
848 | for(ix = 0; ix < 4; ix++) { |
||
849 | z += kernel[ix][iy] * Rx[ix] * Ry[iy]; |
||
850 | } |
||
851 | } |
||
852 | return z;
|
||
853 | } |
||
854 | |||
855 | /**
|
||
856 | * Calcula los valores N y Z para el m?todo bilinear y obtiene el valor del pixel como
|
||
857 | * Z / N
|
||
858 | * @param dx distancia en X desde el centro del pixel hasta el punto. Es un valor entre 0 y 1
|
||
859 | * @param dy distancia en Y desde el centro del pixel hasta el punto. Es un valor entre 0 y 1
|
||
860 | * @param kernel valor del pixel y alrededor
|
||
861 | * @return valor del pixel
|
||
862 | */
|
||
863 | private double getBilinearValue(double dx, double dy, double[] kernel) { |
||
864 | double z = 0.0, n = 0.0, d; |
||
865 | d = (1.0 - dx) * (1.0 - dy); |
||
866 | z += d * kernel[0];
|
||
867 | n += d; |
||
868 | |||
869 | d = dx * (1.0 - dy);
|
||
870 | z += d * kernel[1];
|
||
871 | n += d; |
||
872 | |||
873 | d = (1.0 - dx) * dy;
|
||
874 | z += d * kernel[2];
|
||
875 | n += d; |
||
876 | |||
877 | d = dx * dy; |
||
878 | z += d * kernel[3];
|
||
879 | n += d; |
||
880 | |||
881 | double b = 0; |
||
882 | if(n > 0.0) |
||
883 | b = (z / n); |
||
884 | return b;
|
||
885 | } |
||
886 | |||
887 | /**
|
||
888 | * Calcula los valores N y Z para el m?todo de distancia inversa y calcula el valor del
|
||
889 | * pixel como Z / N.
|
||
890 | * @param dx distancia en X desde el centro del pixel hasta el punto. Es un valor entre 0 y 1
|
||
891 | * @param dy distancia en Y desde el centro del pixel hasta el punto. Es un valor entre 0 y 1
|
||
892 | * @param kernel valor del pixel y alrededor
|
||
893 | * @return valor del pixel
|
||
894 | */
|
||
895 | private double getInverseDistanceValue(double dx, double dy, double[] kernel) { |
||
896 | double z = 0.0, n = 0.0, d; |
||
897 | double t = Math.sqrt(dx * dx + dy * dy); |
||
898 | d = 1.0 / ((t == 0) ? 0.5 : t); |
||
899 | z += d * kernel[0];
|
||
900 | n += d; |
||
901 | |||
902 | t = Math.sqrt((1.0 - dx) * ( 1.0 - dx) + dy * dy); |
||
903 | d = 1.0 / ((t == 0) ? 0.5 : t); |
||
904 | z += d * kernel[1];
|
||
905 | n += d; |
||
906 | |||
907 | t = Math.sqrt(dx * dx + (1.0 - dy) * (1.0 - dy)); |
||
908 | d = 1.0 / ((t == 0) ? 0.5 : t); |
||
909 | z += d * kernel[2];
|
||
910 | n += d; |
||
911 | |||
912 | t = Math.sqrt((1.0 - dx) *( 1.0 - dx) + (1.0 - dy) * (1.0 - dy)); |
||
913 | d = 1.0 / ((t == 0) ? 0.5 : t); |
||
914 | z += d * kernel[3];
|
||
915 | n += d; |
||
916 | |||
917 | double b = 0; |
||
918 | if(n > 0.0) |
||
919 | b = (z / n); |
||
920 | return b;
|
||
921 | } |
||
922 | |||
923 | /**
|
||
924 | * Obtiene un kernel de 4x4 elementos. Si alguno de los elementos se sale de la imagen
|
||
925 | * , por ejemplo en los bordes devuelve null.
|
||
926 | * @param x
|
||
927 | * @param y
|
||
928 | * @param band
|
||
929 | * @return
|
||
930 | */
|
||
931 | private double[][] get4x4Submatrix(int x, int y, int band) { |
||
932 | int ix, iy, px, py;
|
||
933 | double[][] z_xy = new double[4][4]; |
||
934 | |||
935 | for(iy = 0, py = y - 1; iy < 4; iy++, py++) { |
||
936 | for(ix = 0, px = x - 1; ix < 4; ix++, px++) { |
||
937 | if (!buffer.isInside(px, py))
|
||
938 | return null; |
||
939 | else {
|
||
940 | switch(buffer.getDataType()) {
|
||
941 | case Buffer.TYPE_BYTE: z_xy[ix][iy] = (buffer.getElemByte(py, px, band) & 0xff); break; |
||
942 | case Buffer.TYPE_SHORT: z_xy[ix][iy] = (buffer.getElemShort(py, px, band) & 0xffff); break; |
||
943 | case Buffer.TYPE_INT: z_xy[ix][iy] = (buffer.getElemInt(py, px, band) & 0xffffffff); break; |
||
944 | case Buffer.TYPE_FLOAT: z_xy[ix][iy] = buffer.getElemFloat(py, px, band); break; |
||
945 | case Buffer.TYPE_DOUBLE: z_xy[ix][iy] = buffer.getElemDouble(py, px, band); break; |
||
946 | } |
||
947 | } |
||
948 | } |
||
949 | } |
||
950 | return z_xy;
|
||
951 | } |
||
952 | |||
953 | /**
|
||
954 | * Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y),
|
||
955 | * (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen
|
||
956 | * se tomar? x e y.
|
||
957 | * @param x Coordenada X del pixel inicial
|
||
958 | * @param y Coordenada Y del pixel inicial
|
||
959 | * @param band N?mero de banda.
|
||
960 | * @return Kernel solicitado en forma de array.
|
||
961 | */
|
||
962 | private double[] getKernelByte(int x, int y, int band) { |
||
963 | double[] d = new double[4]; |
||
964 | d[0] = (buffer.getElemByte(y, x, band) & 0xff); |
||
965 | int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
||
966 | int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
||
967 | d[1] = (buffer.getElemByte(y, nextX, band) & 0xff); |
||
968 | d[2] = (buffer.getElemByte(nextY, x, band) & 0xff); |
||
969 | d[3] = (buffer.getElemByte(nextY, nextX, band) & 0xff); |
||
970 | return d;
|
||
971 | } |
||
972 | |||
973 | /**
|
||
974 | * Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y),
|
||
975 | * (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen
|
||
976 | * se tomar? x e y.
|
||
977 | * @param x Coordenada X del pixel inicial
|
||
978 | * @param y Coordenada Y del pixel inicial
|
||
979 | * @param band N?mero de banda.
|
||
980 | * @return Kernel solicitado en forma de array.
|
||
981 | */
|
||
982 | private double[] getKernelShort(int x, int y, int band) { |
||
983 | double[] d = new double[4]; |
||
984 | d[0] = (buffer.getElemShort(y, x, band) & 0xffff); |
||
985 | int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
||
986 | int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
||
987 | d[1] = (buffer.getElemShort(y, nextX, band) & 0xffff); |
||
988 | d[2] = (buffer.getElemShort(nextY, x, band) & 0xffff); |
||
989 | d[3] = (buffer.getElemShort(nextY, nextX, band) & 0xffff); |
||
990 | return d;
|
||
991 | } |
||
992 | |||
993 | /**
|
||
994 | * Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y),
|
||
995 | * (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen
|
||
996 | * se tomar? x e y.
|
||
997 | * @param x Coordenada X del pixel inicial
|
||
998 | * @param y Coordenada Y del pixel inicial
|
||
999 | * @param band N?mero de banda.
|
||
1000 | * @return Kernel solicitado en forma de array.
|
||
1001 | */
|
||
1002 | private double[] getKernelInt(int x, int y, int band) { |
||
1003 | double[] d = new double[4]; |
||
1004 | d[0] = (buffer.getElemInt(y, x, band) & 0xffffffff); |
||
1005 | int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
||
1006 | int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
||
1007 | d[1] = (buffer.getElemInt(y, nextX, band) & 0xffffffff); |
||
1008 | d[2] = (buffer.getElemInt(nextY, x, band) & 0xffffffff); |
||
1009 | d[3] = (buffer.getElemInt(nextY, nextX, band) & 0xffffffff); |
||
1010 | return d;
|
||
1011 | } |
||
1012 | |||
1013 | /**
|
||
1014 | * Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y),
|
||
1015 | * (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen
|
||
1016 | * se tomar? x e y.
|
||
1017 | * @param x Coordenada X del pixel inicial
|
||
1018 | * @param y Coordenada Y del pixel inicial
|
||
1019 | * @param band N?mero de banda.
|
||
1020 | * @return Kernel solicitado en forma de array.
|
||
1021 | */
|
||
1022 | private double[] getKernelFloat(int x, int y, int band) { |
||
1023 | double[] d = new double[4]; |
||
1024 | d[0] = buffer.getElemFloat(y, x, band);
|
||
1025 | int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
||
1026 | int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
||
1027 | d[1] = buffer.getElemFloat(y, nextX, band);
|
||
1028 | d[2] = buffer.getElemFloat(nextY, x, band);
|
||
1029 | d[3] = buffer.getElemFloat(nextY, nextX, band);
|
||
1030 | return d;
|
||
1031 | } |
||
1032 | |||
1033 | /**
|
||
1034 | * Obtiene un kernel de cuatro elemento que corresponden a los pixeles (x, y), (x + 1, y),
|
||
1035 | * (x, y + 1), (x + 1, y + 1). Si los pixeles x + 1 o y + 1 se salen del raster de origen
|
||
1036 | * se tomar? x e y.
|
||
1037 | * @param x Coordenada X del pixel inicial
|
||
1038 | * @param y Coordenada Y del pixel inicial
|
||
1039 | * @param band N?mero de banda.
|
||
1040 | * @return Kernel solicitado en forma de array.
|
||
1041 | */
|
||
1042 | private double[] getKernelDouble(int x, int y, int band) { |
||
1043 | double[] d = new double[4]; |
||
1044 | d[0] = buffer.getElemDouble(y, x, band);
|
||
1045 | int nextX = ((x + 1) >= buffer.getWidth()) ? x : (x + 1); |
||
1046 | int nextY = ((y + 1) >= buffer.getHeight()) ? y : (y + 1); |
||
1047 | d[1] = buffer.getElemDouble(y, nextX, band);
|
||
1048 | d[2] = buffer.getElemDouble(nextY, x, band);
|
||
1049 | d[3] = buffer.getElemDouble(nextY, nextX, band);
|
||
1050 | return d;
|
||
1051 | } |
||
1052 | |||
1053 | public int getPercent() { |
||
1054 | return Math.min((int) percent, 100); |
||
1055 | } |
||
1056 | |||
1057 | public String getLog() { |
||
1058 | return null; |
||
1059 | } |
||
1060 | |||
1061 | public boolean isCancelable() { |
||
1062 | return true; |
||
1063 | } |
||
1064 | |||
1065 | public boolean isPausable() { |
||
1066 | return false; |
||
1067 | } |
||
1068 | |||
1069 | public void setPercent(int value) { |
||
1070 | this.percent = value;
|
||
1071 | } |
||
1072 | } |