Statistics
| Revision:

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 / ConvolutionListManager.java @ 1426

History | View | Annotate | Download (9.75 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 java.util.ArrayList;
25
import java.util.List;
26

    
27
import org.gvsig.fmap.dal.coverage.datastruct.Params;
28
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
29
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
30
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
31
import org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager;
32
import org.gvsig.fmap.dal.coverage.grid.RegistrableFilterListener;
33
import org.gvsig.raster.impl.datastruct.Kernel;
34
import org.gvsig.raster.impl.grid.filter.RasterFilterListManagerImpl;
35
import org.gvsig.raster.impl.store.ParamImpl;
36
import org.gvsig.tools.ToolsLocator;
37
import org.gvsig.tools.extensionpoint.ExtensionPoint;
38
import org.gvsig.tools.extensionpoint.ExtensionPointManager;
39
/**
40
 * Gestion  de la lista de filtros
41
 */
42
public class ConvolutionListManager implements RasterFilterListManager {
43
        protected RasterFilterList filterList = null;
44

    
45
        /**
46
         * Registra ConvolutionListManager en los puntos de extension de RasterFilter
47
         */
48
        public static void register() {
49
                ExtensionPointManager extensionPoints = ToolsLocator.getExtensionPointManager();
50
                ExtensionPoint point = extensionPoints.get("RasterFilter");
51
                point.append("Convolucion", "", ConvolutionListManager.class);
52
        }
53
        
54
        public Class<?> getFilterClassByID(String id) {
55
                if( id.compareTo("media") == 0 ||
56
                        id.compareTo("pasobajo") == 0 ||
57
                        id.compareTo("sharpen") == 0 ||
58
                        id.compareTo("gauss") == 0 ||
59
                        id.compareTo("personalizado") == 0)
60
                        return ConvolutionFilter.class;
61
                return null;
62
        }
63
        
64
        /**
65
         * Default constructor. Sets the filter list.
66
         * @param filterList
67
         */
68
        public ConvolutionListManager(RasterFilterList filterList) {
69
                this.filterList = filterList;
70
        }
71
        
72
        public ConvolutionListManager(RasterFilterListManagerImpl filterListManager) {
73
                this.filterList = filterListManager.getFilterList();
74
        }
75

    
76
        /**
77
         * A?ade un filtro de convolucion  a la pila de filtros.
78
         * @param ladoVentana
79
         * @throws FilterTypeException
80
         */
81
        public void addConvolutionFilter(String Name,int ladoVentana, double agudeza, Kernel kernel) throws FilterTypeException {
82
                RasterFilter filter = new ConvolutionByteFilter();
83

    
84
                // Cuando el filtro esta creado, tomamos los valores y lo a?adimos a la pila
85
                if (filter != null) {
86
                        filter.addParam("ladoVentana", Integer.valueOf(ladoVentana));
87
                        filter.addParam("Agudeza", Double.valueOf(agudeza));
88
                        filter.addParam("filterName", String.valueOf(Name));
89
                        filter.addParam("kernel", kernel);
90
                        filterList.add(filter);
91
                }
92
        }
93

    
94
        /*
95
         * (non-Javadoc)
96
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#getStringsFromFilterList(java.util.ArrayList, org.gvsig.fmap.dal.coverage.grid.RasterFilter)
97
         */
98
        public List<String> getStringsFromFilterList(List<String> filterList, RasterFilter rf) {
99
                if (rf instanceof ConvolutionFilter) {
100
                        filterList.add("filter.convolution.active=true");
101
                        ConvolutionFilter convolutionFilter = (ConvolutionFilter) rf;
102
                        switch (convolutionFilter.ladoVentana) {
103
                                case 0:
104
                                        filterList.add("filter.convolution.ladoVentana=3");
105
                                        break;
106
                                case 1:
107
                                        filterList.add("filter.convolution.ladoVentana=5");
108
                                        break;
109
                                case 2:
110
                                        filterList.add("filter.convolution.ladoVentana=7");
111
                                        break;
112
                        }
113

    
114
                        filterList.add("filter.convolution.filterName=" + convolutionFilter.getName());
115
                        if (convolutionFilter.getName().equals("personalizado")) {
116
                                double[][] listDouble = convolutionFilter.kernel.kernel;
117
                                String listString = "";
118
                                for (int i = 0; i < listDouble.length; i++)
119
                                        for (int j = 0; j < listDouble[0].length; j++)
120
                                                listString = listString + listDouble[i][j] + " ";
121

    
122
                                listString = listString.trim();
123
                                filterList.add("filter.convolution.kernel=" + listString);
124
                        }
125
                }
126

    
127
                return filterList;
128
        }
129

    
130
        public void addFilter(Class<?> classFilter, Params params) throws FilterTypeException {
131
                if (ConvolutionFilter.class.isAssignableFrom(classFilter)) {
132
                        int ladoVentana = 0;
133
                        double agudeza = 0;
134
                        Kernel kernel = null;
135
                        String name = "";
136
                        for (int i = 0; i < params.getNumParams(); i++) {
137
                                if (((ParamImpl)params.getParam(i)).getId().equals("Panel") &&
138
                                                ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof RegistrableFilterListener) {
139
                                        params = ((RegistrableFilterListener) ((ParamImpl)params.getParam(i)).getDefaultValue()).getParams();
140
                                        break;
141
                                }
142
                        }
143

    
144
                        for (int i = 0; i < params.getNumParams(); i++) {
145
                                if (((ParamImpl)params.getParam(i)).getId().equals("LadoVentana") &&
146
                                                ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof Integer)
147
                                        ladoVentana = ((Integer) ((ParamImpl)params.getParam(i)).getDefaultValue()).intValue();
148

    
149
                                if (((ParamImpl)params.getParam(i)).getId().equals("FilterName")) {
150
                                        Object obj = ((ParamImpl)params.getParam(i)).getDefaultValue();
151
                                        if(obj != null)
152
                                                name = new String((String) ((ParamImpl)params.getParam(i)).getDefaultValue());
153
                                }
154

    
155
                                if (((ParamImpl)params.getParam(i)).getId().equals("Kernel") &&
156
                                                ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof Kernel)
157
                                        kernel = (Kernel) ((ParamImpl)params.getParam(i)).getDefaultValue();
158

    
159
                                if (((ParamImpl)params.getParam(i)).getId().equals("Agudeza") &&
160
                                                ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof Double)
161
                                        agudeza = ((Double) ((ParamImpl)params.getParam(i)).getDefaultValue()).doubleValue();
162
                        }
163
                        addConvolutionFilter(name, ladoVentana, agudeza, kernel);
164
                }
165
        }
166
        
167
        /*
168
         * (non-Javadoc)
169
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#addFilter(org.gvsig.fmap.dal.coverage.datastruct.Params)
170
         */
171
        public void addFilter(Params params) throws FilterTypeException {
172
                addFilter(ConvolutionFilter.class, params);
173
        }
174
        
175
        /*
176
         * (non-Javadoc)
177
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#createFilter(org.gvsig.fmap.dal.coverage.datastruct.Params)
178
         */
179
        public RasterFilter createFilter(Params params) {
180
                Integer ladoVentana = ((Integer) params.getParamById("LadoVentana").getDefaultValue());
181
                String name = ((String) params.getParamById("FilterName").getDefaultValue());
182
                Kernel kernel = ((Kernel) params.getParamById("Kernel").getDefaultValue());
183
                Double agudeza = ((Double) params.getParamById("Agudeza").getDefaultValue());
184
                
185
                RasterFilter filter = new ConvolutionByteFilter();
186
                filter.addParam("LadoVentana", ladoVentana);
187
                filter.addParam("Agudeza", agudeza);
188
                filter.addParam("filterName", name);
189
                filter.addParam("kernel", kernel);
190
                return filter;
191
        }
192

    
193
        /*
194
         * (non-Javadoc)
195
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#createFilterListFromStrings(java.util.ArrayList, java.lang.String, int)
196
         */
197
        public int createFilterListFromStrings(List<String> filters, String fil, int filteri) throws FilterTypeException {
198
                if ((fil.startsWith("filter.convolution.active")) && (RasterFilterListManagerImpl.getValue(fil).equals("true"))) {
199
                        int ladoVentana = 0;
200
                        double agudeza = 0;
201
                        String name = "";
202
                        Kernel kernel = null;
203
                        filters.remove(0);
204
                        for (int prop = 0; prop < filters.size(); prop++) {
205
                                String elem = (String) filters.get(prop);
206
                                if (elem.startsWith("filter.convolution.ladoVentana")) {
207
                                        ladoVentana = Integer.parseInt(RasterFilterListManagerImpl.getValue(elem));
208
                                        ladoVentana = (ladoVentana == 7) ? 2 : (ladoVentana == 5) ? 1 : 0;
209
                                        filters.remove(prop);
210
                                        prop--;
211
                                }
212
                                if (elem.startsWith("filter.convolution.filterName")) {
213
                                        name = RasterFilterListManagerImpl.getValue(elem);
214
                                        filters.remove(prop);
215
                                        prop--;
216
                                }
217
                                if (elem.startsWith("filter.convolution.kernel")) {
218
                                        String k = RasterFilterListManagerImpl.getValue(elem);
219
                                        String[] listString = k.split(" ");
220
                                        if (listString != null) {
221
                                                int lado = (ladoVentana == 0) ? 3 : (ladoVentana == 1) ? 5 : 7;
222
                                                double[][] listDouble = new double[lado][lado];
223
                                                int cont = 0;
224
                                                for (int i = 0; i < lado; i++) {
225
                                                        for (int j = 0; j < lado; j++) {
226
                                                                try {
227
                                                                        listDouble[i][j] = Double.parseDouble(listString[cont]);
228
                                                                        cont++;
229
                                                                } catch (NumberFormatException e) {
230
                                                                }
231
                                                        }
232
                                                }
233
                                                kernel = new Kernel(listDouble);
234
                                        }
235
                                        filters.remove(prop);
236
                                        prop--;
237
                                }
238
                                if (elem.startsWith("filter.convolution.agudeza")) {
239
                                        agudeza = Double.parseDouble(RasterFilterListManagerImpl.getValue(elem));
240
                                        filters.remove(prop);
241
                                        prop--;
242
                                }
243
                        }
244
                        addConvolutionFilter(name, ladoVentana, agudeza, kernel);
245
                }
246
                return filteri;
247
        }
248

    
249
        public List<Class<?>> getRasterFilterList() {
250
                List<Class<?>> filters = new ArrayList<Class<?>>();
251
                filters.add(ConvolutionFilter.class);
252
                return filters;
253
        }
254
        
255
        /*
256
         * (non-Javadoc)
257
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#getFilterList()
258
         */
259
        public RasterFilterList getFilterList() {
260
                return filterList;
261
        }
262
        
263
        /*
264
         * (non-Javadoc)
265
         * @see org.gvsig.fmap.dal.coverage.grid.RasterFilterListManager#setFilterList(org.gvsig.fmap.dal.coverage.grid.RasterFilterList)
266
         */
267
        public void setFilterList(RasterFilterList filterList) {
268
                this.filterList = filterList;
269
        }
270
}