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 / grid / filter / convolution / ConvolutionByteFilter.java @ 2443
History | View | Annotate | Download (3.25 KB)
1 |
/* 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.grid.filter.convolution; |
23 |
|
24 |
import org.gvsig.fmap.dal.coverage.dataset.Buffer; |
25 |
import org.gvsig.fmap.dal.coverage.exception.FilterAddException; |
26 |
import org.gvsig.raster.impl.DefaultRasterManager; |
27 |
import org.gvsig.raster.impl.buffer.RasterBuffer; |
28 |
import org.gvsig.raster.impl.datastruct.Kernel; |
29 |
|
30 |
/**
|
31 |
* Filtro de convolucion para Buffer de tipo Byte
|
32 |
* @author Alejandro Mu?oz <alejandro.munoz@uclm.es>
|
33 |
* @author Diego Guerrero Sevilla <diego.guerrero@uclm.es>
|
34 |
* */
|
35 |
public class ConvolutionByteFilter extends ConvolutionFilter { |
36 |
|
37 |
public ConvolutionByteFilter() {
|
38 |
super();
|
39 |
} |
40 |
|
41 |
/**
|
42 |
* @param Kernel a aplicar. En caso de que no se trate de un kernel definido en ConvolutionFilter, se puede pasar como
|
43 |
* parametro el kernel se pretende aplicar.
|
44 |
* **/
|
45 |
public ConvolutionByteFilter(Kernel k) { |
46 |
super();
|
47 |
super.kernel = k;
|
48 |
} |
49 |
|
50 |
|
51 |
public void pre() throws FilterAddException { |
52 |
super.pre();
|
53 |
rasterResult = DefaultRasterManager.getInstance().createBuffer(Buffer.TYPE_BYTE, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true); |
54 |
} |
55 |
|
56 |
/** Aplicacion del filtro para el pixel de la posicion line, col */
|
57 |
public void process(int col, int line) { |
58 |
int lado = kernel.getLado();
|
59 |
int semiLado = (lado - 1) >> 1; |
60 |
double ventana[][] = new double[lado][lado]; |
61 |
double resultConvolution = 0; |
62 |
for (int band = 0; band < numberOfBandsToProcess(); band++) { |
63 |
if ((col - semiLado >= 0) && (line - semiLado >= 0) && (col + semiLado < width) && (line + semiLado < height)) { |
64 |
for (int j = -semiLado; j <= semiLado; j++) |
65 |
for (int i = -semiLado; i <= semiLado; i++) |
66 |
ventana[i + semiLado][j + semiLado] = raster.getElemByte(line + j, col + i, band) & 0xff;
|
67 |
Kernel Kventana = new Kernel(ventana); |
68 |
|
69 |
resultConvolution = kernel.convolution(Kventana); |
70 |
if (resultConvolution > 255) |
71 |
resultConvolution = 255;
|
72 |
else
|
73 |
if (resultConvolution < 0) |
74 |
resultConvolution = 0;
|
75 |
|
76 |
rasterResult.setElem(line, col, band, (byte) resultConvolution);
|
77 |
} else
|
78 |
rasterResult.setElem(line, col, band, (byte) raster.getElemByte(line, col, band));
|
79 |
} |
80 |
|
81 |
writeAlphaBand(line, col); |
82 |
|
83 |
} |
84 |
|
85 |
/**
|
86 |
* @return tipo de dato del buffer de entrada
|
87 |
* */
|
88 |
public int getInRasterDataType() { |
89 |
return RasterBuffer.TYPE_BYTE;
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* @return tipo de dato del buffer de salida
|
94 |
* */
|
95 |
public int getOutRasterDataType() { |
96 |
return RasterBuffer.TYPE_BYTE;
|
97 |
} |
98 |
} |