Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libRaster / src / org / gvsig / raster / grid / filter / enhancement / ContrastShortFilter.java @ 11899

History | View | Annotate | Download (4.12 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 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
package org.gvsig.raster.grid.filter.enhancement;
20

    
21
import org.gvsig.raster.buffer.RasterBuffer;
22
import org.gvsig.raster.dataset.IBuffer;
23

    
24
/**
25
 * Filtro de contraste para im?genes de 16 bits. En el m?todo de proceso 
26
 * procesa un solo pixel short. Varia su contraste en relaci?n a los par?metros
27
 * asignados en el m?todo pre();
28
 * 
29
 * @author Miguel ?ngel Querol Carratal? (miguelangel.querol@iver.es)
30
 *
31
 */
32
public class ContrastShortFilter extends ContrastFilter {
33
        protected IBuffer                rasterResult = null;
34

    
35
        /**
36
         * Constructor de Contraste para layers de tipo short
37
         */
38
        public ContrastShortFilter(){
39
                super();
40
        }
41

    
42
        /*
43
         * (non-Javadoc)
44
         * @see org.gvsig.raster.grid.filter.enhancement.ContrastFilter#pre()
45
         */
46
        public void pre(){
47
                exec = true;
48
                this.raster = (RasterBuffer) params.get("raster");
49
                height = raster.getHeight();
50
                width = raster.getWidth();
51
                this.incrContraste = ((Integer) this.params.get("incrContraste")).shortValue();
52

    
53
                rasterResult = RasterBuffer.getBuffer(IBuffer.TYPE_SHORT, raster.getWidth(), raster.getHeight(), raster.getBandCount(), true);
54

    
55
                super.pre();
56
        }
57

    
58
        /**
59
         * Algoritmo de contraste
60
         * @param px3 valores ARGB
61
         * @return valores ARGB con el algoritmo aplicado
62
         */
63
        private short[] calcContrast(short[] px) {
64
                short result = 0;
65
                short diferencia;
66
                for (int i = 0; i < 3; i++) {
67
                        px[i] = (short) (px[i] % 256);
68
                        if (incrContraste >= 0) {
69
                                if (px[i] < 127) {
70
                                        diferencia = (short) (127 - px[i]);
71
                                        result = (short) (px[i] - (short) ((diferencia * 5) * (incrContraste * 0.1) / 25));
72
                                        if (result < 0)
73
                                                result = 0;
74

    
75
                                }
76
                                if (px[i] > 127) {
77
                                        diferencia = (short) (px[i] - 127);
78
                                        result = (short) (px[i] + (short) ((diferencia * 5) * (incrContraste * 0.1) / 25));
79
                                        if (result > 255)
80
                                                result = 255;
81
                                }
82
                        } else { // incrContraste < 0
83
                                if (px[i] < 127) {
84
                                        diferencia = (short) (127 - px[i]);
85
                                        result = (short) (px[i] + (diferencia * (short) (-incrContraste * 0.1) / 25));
86
                                        if (result > 127)
87
                                                result = 127;
88
                                        if (px[i] == 0 || px[i] == 0)
89
                                                if (incrContraste > -255)
90
                                                        result = 0;
91
                                }
92
                                if (px[i] > 127) {
93
                                        diferencia = (short) (px[i] - 127);
94
                                        result = (short) (px[i] - (diferencia * (short) (-incrContraste * 0.1) / 25));
95
                                        if (result < 127)
96
                                                result = 127;
97
                                        if (px[i] == 255 || px[i] == 254)
98
                                                if (incrContraste > -255)
99
                                                        result = 255;
100
                                }
101
                        }
102
                        px[i] = result;
103
                }
104
                return px;
105
        }
106
        
107
        /*
108
         * (non-Javadoc)
109
         * @see org.gvsig.raster.grid.filter.enhancement.ContrastFilter#process(int, int)
110
         */
111
        public void process(int col, int line) {
112
                short[] px = new short[4];
113
                raster.getElemShort(line, col, px);
114
                px = calcContrast(px);
115
                rasterResult.setElemShort(line, col, px);
116
        }
117
        
118
        /*
119
         * (non-Javadoc)
120
         * @see org.gvsig.raster.grid.filter.enhancement.ContrastFilter#getInRasterDataType()
121
         */
122
        public int getInRasterDataType() {
123
                return RasterBuffer.TYPE_SHORT;
124
        }
125

    
126
        /*
127
         * (non-Javadoc)
128
         * @see org.gvsig.raster.grid.filter.enhancement.ContrastFilter#getOutRasterDataType()
129
         */
130
        public int getOutRasterDataType() {
131
                return RasterBuffer.TYPE_SHORT;
132
        }
133

    
134
        /*
135
         * (non-Javadoc)
136
         * @see org.gvsig.raster.grid.filter.enhancement.ContrastFilter#getResult(java.lang.String)
137
         */
138
        public Object getResult(String name) {
139
                if (name.equals("raster"))
140
                        return (Object) this.rasterResult;
141
                return null;
142
        }
143
}