Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster_dataaccess_refactoring / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / filter / enhancement / EqualizationManager.java @ 2328

History | View | Annotate | Download (6.45 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.enhancement;
23

    
24
import java.util.ArrayList;
25
import java.util.List;
26

    
27
import org.gvsig.fmap.dal.coverage.RasterLibrary;
28
import org.gvsig.fmap.dal.coverage.dataset.Buffer;
29
import org.gvsig.fmap.dal.coverage.datastruct.BufferHistogram;
30
import org.gvsig.fmap.dal.coverage.datastruct.Params;
31
import org.gvsig.fmap.dal.coverage.exception.FileNotOpenException;
32
import org.gvsig.fmap.dal.coverage.exception.FilterTypeException;
33
import org.gvsig.fmap.dal.coverage.exception.ProcessInterruptedException;
34
import org.gvsig.fmap.dal.coverage.exception.RasterDriverException;
35
import org.gvsig.fmap.dal.coverage.grid.AbstractRasterFilterManager;
36
import org.gvsig.fmap.dal.coverage.grid.RasterFilter;
37
import org.gvsig.fmap.dal.coverage.grid.RasterFilterList;
38
import org.gvsig.fmap.dal.coverage.store.props.Statistics;
39
import org.gvsig.raster.impl.grid.filter.RasterFilterListManagerImpl;
40
import org.gvsig.raster.impl.store.ParamImpl;
41
/**
42
 * Gestor de la pila de filtros para el filtro de ecualizaci?n de histograma.
43
 *
44
 * @author Nacho Brodin (nachobrodin@gmail.com)
45
 */
46
public class EqualizationManager extends AbstractRasterFilterManager {
47
        private static String   ID = "Equalization";
48
        
49
        /**
50
         * Default constructor. Sets the filter list.
51
         * @param filterList
52
         */
53
        public EqualizationManager(RasterFilterList filterList) {
54
                super(filterList);
55
        }
56
        
57
        public String getManagerID() {
58
                return ID;
59
        }
60

    
61
        public static void register() {
62
                AbstractRasterFilterManager.register(ID, EqualizationManager.class);
63
        }
64

    
65
        /**
66
         * Constructor
67
         * @param filterListManager
68
         */
69
        public EqualizationManager(RasterFilterListManagerImpl filterListManager) {
70
                super(filterListManager.getFilterList());
71
        }
72
        
73
        public boolean isDataTypeSupported(int dataType) {
74
                if(dataType != Buffer.TYPE_BYTE)
75
                        return false;
76
                return true;
77
        }
78
        
79
        public Class<?> getFilterClassByID(String id) {
80
                if(id.compareTo("equalization") == 0)
81
                        return EqualizationFilter.class;
82
                return null;
83
        }
84

    
85
        /**
86
         * A?ade un filtro de ecualizaci?n de histograma.
87
         * @throws FilterTypeException
88
         */
89
        public void addEqualizationFilter(Statistics stats, int[] renderBands, BufferHistogram hist, int[] ecualizedBands) throws FilterTypeException {
90
                try {
91
                        if (!stats.isCalculated()) {
92
                                try {
93
                                        stats.calculate(RasterLibrary.statisticsScale);
94
                                } catch (FileNotOpenException e) {
95
                                        // No podemos aplicar el filtro
96
                                        return;
97
                                } catch (RasterDriverException e) {
98
                                        // No podemos aplicar el filtro
99
                                        return;
100
                                }
101
                        }
102

    
103
                        RasterFilter filter = createEnhancedFilter(stats, renderBands, hist, ecualizedBands);
104
                        if (filter != null)
105
                                getFilterList().add(filter);
106
                } catch (ProcessInterruptedException e) {
107
                        //Si se ha interrumpido no a?adimos el filtro
108
                }
109
        }
110

    
111
        /**
112
         * Crea un filtro de Ecualizaci?n de histograma.
113
         * @param stats
114
         * @param renderBands
115
         * @return
116
         */
117
        public static RasterFilter createEnhancedFilter(Statistics stats, int[] renderBands, BufferHistogram hist, int[] ecualizedBands) {
118
                RasterFilter filter = new EqualizationByteFilter();
119
                if (filter != null) {
120
                        filter.addParam("stats", stats);
121
                        filter.addParam("renderBands", renderBands);
122
                        filter.addParam("histogram", hist);
123
                        filter.addParam("ecualizedBands", ecualizedBands);
124
                }
125
                return filter;
126
        }
127

    
128
        /**
129
         * Obtiene un Array de Strings a partir de una pila de filtros. Cada elemento
130
         * del array tendr? la forma de elemento=valor.
131
         */
132
        public List<String> getStringsFromFilterList(List<String> filterList, RasterFilter rf) {
133
                return filterList;
134
        }
135

    
136
        public int createFilterListFromStrings(List<String> filters, String fil, int filteri) throws FilterTypeException {
137
                return filteri;
138
        }
139

    
140
        public List<Class<?>> getRasterFilterList() {
141
                List<Class<?>> filters = new ArrayList<Class<?>>();
142
                filters.add(EqualizationFilter.class);
143
                return filters;
144
        }
145

    
146

    
147
        public void addFilter(Class<?> classFilter, Params params) throws FilterTypeException {
148
                if (EqualizationFilter.class.isAssignableFrom(classFilter)) {
149
                        int[] renderBands = { 0, 1, 2, 3 };
150
                        int[] ecualizedBands = { 0, 1, 2 };
151
                        BufferHistogram hist = null;
152
                        for (int i = 0; i < params.getNumParams(); i++) {
153
                                if (((ParamImpl)params.getParam(i)).getId().equals("RenderBands") &&
154
                                        ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof String) {
155
                                        String[] bands = new String((String) ((ParamImpl)params.getParam(i)).getDefaultValue()).split(" ");
156
                                        renderBands[0] = new Integer(bands[0]).intValue();
157
                                        renderBands[1] = new Integer(bands[1]).intValue();
158
                                        renderBands[2] = new Integer(bands[2]).intValue();
159
                                        continue;
160
                                }
161
                                if (((ParamImpl)params.getParam(i)).getId().equals("alphaBand") &&
162
                                                ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof Integer) {
163
                                        renderBands[3] = (Integer)((ParamImpl)params.getParam(i)).getDefaultValue();
164
                                        continue;
165
                                }
166
                                if (((ParamImpl)params.getParam(i)).getId().equals("Histogram") &&
167
                                        ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof BufferHistogram) {
168
                                        hist = (BufferHistogram)((ParamImpl)params.getParam(i)).getDefaultValue();
169
                                }
170
                                if (((ParamImpl)params.getParam(i)).getId().equals("EcualizedBands") &&
171
                                        ((ParamImpl)params.getParam(i)).getDefaultValue() instanceof int[]) {
172
                                        ecualizedBands = (int[])((ParamImpl)params.getParam(i)).getDefaultValue();
173
                                }
174
                        }
175

    
176
                        addEqualizationFilter((Statistics) getFilterList().getEnvParam("SrcStatistics"), renderBands, hist, ecualizedBands);
177
                }
178
        }
179
        
180
        public void addFilter(Params params) throws FilterTypeException {
181
                addFilter(EqualizationFilter.class, params);
182
        }
183
        
184
        public RasterFilter createFilter(Params params) {
185
                //TODO: crear un filtro de este tipo
186
                return null;
187
        }
188
}