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 / enhancement / ContrastFilter.java @ 2438

History | View | Annotate | Download (3.5 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 org.gvsig.fmap.dal.coverage.dataset.Buffer;
25
import org.gvsig.fmap.dal.coverage.datastruct.Params;
26
import org.gvsig.fmap.dal.coverage.exception.FilterAddException;
27
import org.gvsig.fmap.dal.coverage.grid.filter.BaseRasterFilter;
28
import org.gvsig.raster.impl.buffer.RasterBuffer;
29
import org.gvsig.raster.impl.store.ParamsImpl;
30
/**
31
 * Clase base para todos los filtros de contraste
32
 *
33
 * @author Miguel ?ngel Querol Carratal?  
34
 */
35
public class ContrastFilter extends BaseRasterFilter {
36
        public static String[]           names           = new String[] {"contrast"};
37

    
38
        /**
39
         * Variable que contendra el incremento del contraste.
40
         */
41
        int incrContraste = 0;
42

    
43
        /**
44
         * Constructor. Llama al constructor de la clase base y asigna el
45
         * nombre del filtro.
46
         */
47
        public ContrastFilter() {
48
                setName(names[0]);
49
        }
50

    
51
        public void pre() throws FilterAddException {
52
                super.pre();
53
                incrContraste = ((Integer) params.get("incrContraste")).intValue();
54
                createBufferResult(Buffer.TYPE_BYTE, raster.getBandCount());
55
        }
56

    
57
        public void post() {
58
                // En caso de que nadie apunte a raster, se liberar? su memoria.
59
                raster = null;
60
        }
61

    
62
        /**
63
         * Obtiene el incremento de contraster que se est? aplicando
64
         * @return entero que representa el incremento de contraste aplicado.
65
         */
66
        public int getContrastIncrease(){
67
                return this.incrContraste;
68
        }
69

    
70
        public String getGroup() {
71
                return "realces";
72
        }
73

    
74
        public Params getUIParams(String nameFilter) {
75
                Params params = new ParamsImpl();
76
                params.setParam("Contrast",
77
                                new Integer(incrContraste),
78
                                Params.SLIDER,
79
                                new String[]{ "-255", "255", "50", "1", "25" }); //min, max, valor defecto, intervalo peque?o, intervalo grande;
80
                return params;
81
        }
82

    
83
        public int getInRasterDataType() {
84
                //No parece que tenga sentido para im?genes que no sean RGB
85
                return RasterBuffer.TYPE_BYTE;
86
        }
87

    
88
        public int getOutRasterDataType() {
89
                return RasterBuffer.TYPE_BYTE;
90
        }
91

    
92
        public void process(int x, int y) {
93
        }
94

    
95
        /**
96
         * Algoritmo de contraste
97
         * @param px valor de la banda
98
         * @return valor de la banda con el algoritmo aplicado
99
         */
100
        protected int calcContrast(int px) {
101
                int result;
102
                int diferencia = 127 - px;
103
                if (incrContraste >= 0) {
104
                        result = (int) (px - (0.02 * diferencia * incrContraste)); // ((5.0 * 0.1) / 25) = 0.02
105
                } else {
106
                        result = (int) (px - (0.004 * diferencia * incrContraste)); // (0.1 / 25) = 0.004;
107
                        if (px < 127) {
108
                                if (result > 127) result = 127;
109
                        } else {
110
                                if (result < 127) result = 127;
111
                        }
112
                }
113
                if (result < 0)
114
                        result = 0;
115
                else {
116
                        if (result > 255)
117
                                result = 255;
118
                }
119
                return result;
120
        }
121

    
122
        public String[] getNames() {
123
                return names;
124
        }
125
}