Statistics
| Revision:

root / trunk / libraries / libCq_CMS_praster / src / org / cresques / filter / convolution / ConvolutionStackManager.java @ 8026

History | View | Annotate | Download (6.25 KB)

1 8026 nacho
/* 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
package org.cresques.filter.convolution;
21
22
import java.util.ArrayList;
23
24
import org.cresques.filter.IStackManager;
25
import org.cresques.filter.RasterFilter;
26
import org.cresques.filter.RasterFilterStack;
27
import org.cresques.filter.RasterFilterStackManager;
28
import org.cresques.filter.RasterFilter.Kernel;
29
import org.cresques.io.GeoRasterFile;
30
import org.cresques.io.data.RasterBuf;
31
32
/**
33
 * Gestor de la pila de filtros para el filtro de convoluci?n.
34
 *
35
 * @author Diego Guerrero Sevilla  <diego.guerrero@uclm.es>
36
 *
37
 */
38
39
public class ConvolutionStackManager implements IStackManager {
40
        public final static int                                convolution = 11;
41
        protected RasterFilterStack                 filterStack = null;
42
        private RasterFilterStackManager        filterStackManager = null;
43
        private GeoRasterFile[]                         grfList = null;
44
45
        public ConvolutionStackManager(RasterFilterStackManager filterStackManager){
46
                this.filterStackManager = filterStackManager;
47
                this.filterStack = filterStackManager.getFilterStack();
48
                filterStackManager.addTypeFilter("convolution", ConvolutionStackManager.convolution, 8);
49
        }
50
51
        /**
52
         * Obtiene el valor entero que corresponde al tipo de filtro.
53
         * @param rasterFilter
54
         * @return
55
         */
56
        public int getType(RasterFilter rasterFilter){
57
                if(rasterFilter instanceof ConvolutionFilter)
58
                        return ConvolutionStackManager.convolution;
59
60
                return -1;
61
        }
62
63
        /**
64
         * A?ade un filtro de convoluci?n a la pila de filtros.
65
         * @param kernel Kernel usado en la convoluci?n
66
         * @param umbral Umbral aplicado al resultado de la convoluci?n. Si 0, no umbraliza.
67
         * @param filterName Nombre del filtro.
68
         */
69
        public void addConvolutionFilter(RasterFilter.Kernel kernel, int umbral, String filterName){
70
71
                RasterFilter filtro = null;
72
73
                switch(filterStack.getDataTypeInFilter(((Integer)this.filterStackManager.getTypeFilters().get("convolution")).intValue())){
74
                case RasterBuf.TYPE_IMAGE:
75
                        filtro = new ConvolutionImageFilter(filterName);
76
                        break;
77
                case RasterBuf.TYPE_SHORT:
78
                        filtro = new ConvolutionShortFilter(filterName);
79
                        break;
80
                case RasterBuf.TYPE_USHORT:
81
                        filtro = new ConvolutionShortFilter(filterName);
82
                        break;
83
                case RasterBuf.TYPE_INT:
84
                        filtro = new ConvolutionIntegerFilter(filterName);
85
                        break;
86
                case RasterBuf.TYPE_FLOAT:
87
                        filtro = new ConvolutionFloatFilter(filterName);
88
                        break;
89
                case RasterBuf.TYPE_DOUBLE:
90
                        filtro = new ConvolutionDoubleFilter(filterName);
91
                        break;
92
                }
93
94
                //Cuando el filtro esta creado, tomamos los valores y lo a?adimos a la pila
95
96
                if(filtro != null){
97
                        filtro.addParam("kernel", kernel);
98
                        filtro.addParam("umbral",new Integer(umbral));
99
                        filterStack.addFilter(((Integer) filterStackManager.getTypeFilters().get("convolution")).intValue(), filtro);
100
                        filterStackManager.controlTypes();
101
                }
102
        }
103
104
        public ArrayList getStringsFromStack() {
105
                // TODO Auto-generated method stub
106
                return null;
107
        }
108
109
        public ArrayList getStringsFromStack(ArrayList filterList, RasterFilter cf) {
110
111
                        if(cf instanceof ConvolutionFilter){
112
                                filterList.add("filter.convolution.active=true");
113
                                Kernel ker =((ConvolutionFilter)cf).kernel;
114
                                ConvolutionFilter convolution = (ConvolutionFilter)cf;
115
                                String listValues = "";
116
                                for(int col = 0; col < ker.kernel.length; col ++)
117
                                        for(int fila = 0; fila < ker.kernel[col].length; fila++)
118
                                                listValues += ker.kernel[col][fila]+ " ";
119
                                filterList.add("filter.convolution.divisor="+ker.getDivisor());
120
                                filterList.add("filter.convolution.filterName="+convolution.getFilterName());
121
                                filterList.add("filter.convolution.umbral="+convolution.umbral);
122
                                filterList.add("filter.convolution.kernel="+listValues);
123
                        }
124
                        return filterList;
125
        }
126
127
        public void createStackFromStrings(ArrayList f, Integer pos) {
128
                // TODO Auto-generated method stub
129
130
        }
131
132
        /**
133
     * Crea una pila de filtros a partir de un Array de Strings. Cada elemento del array debe
134
     * tener la forma elemento=valor.
135
     * @param filters
136
     */
137
        public int createStackFromStrings(ArrayList filters, String fil,
138
                        GeoRasterFile[] grfList, int filteri) {
139
                this.grfList = grfList;
140
141
                if((fil.startsWith("filter.convolution.active"))&&
142
                                (RasterFilterStackManager.getValue(fil).equals("true"))){
143
144
                        RasterFilter.Kernel kernel = null;
145
                        String filterName = null;
146
                        int umbral = 0;
147
                        double divisor = 0;
148
                        double k[][] = null;
149
150
                        filters.remove(0);
151
152
                        for(int prop = 0; prop < filters.size() ; prop++){
153
                                String elem = (String) filters.get(prop);
154
                                if(elem.startsWith("filter.convolution.filterName")){
155
                                        filterName = RasterFilterStackManager.getValue(elem);
156
                                        filters.remove(prop);
157
                                        prop--;
158
                                }
159
                                if(elem.startsWith("filter.convolution.divisor")){
160
                                        divisor = Double.parseDouble(RasterFilterStackManager.getValue(elem));
161
                                        filters.remove(prop);
162
                                        prop--;
163
                                }
164
                                if(elem.startsWith("filter.convolution.umbral")){
165
                                        umbral = Integer.parseInt(RasterFilterStackManager.getValue(elem));
166
                                        filters.remove(prop);
167
                                        prop--;
168
                                }
169
                                if(elem.startsWith("filter.convolution.kernel")){
170
                                        String listValues = RasterFilterStackManager.getValue(elem);
171
                                        String stringArray[] = listValues.split(" ");
172
                                        int ladoVentana =  (int)Math.sqrt(stringArray.length);
173
                                        k = new double[ladoVentana][ladoVentana];
174
                                        for(int col = 0; col < ladoVentana; col ++)
175
                                                for(int fila = 0; fila < ladoVentana; fila++)
176
                                                        k[col][fila]=Double.parseDouble(stringArray[col*fila]);
177
                                        kernel = new Kernel(k,divisor);
178
                                        filters.remove(prop);
179
                                        prop--;
180
                                }
181
                        }
182
                        addConvolutionFilter(kernel,umbral,filterName);
183
                }
184
        //return filteri;
185
                return -1;
186
        }
187
188
}