Statistics
| Revision:

root / trunk / extensions / extRasterTools / src / org / gvsig / rasterTools / brightnessContrast / filter / HistogramImageFilter.java @ 5982

History | View | Annotate | Download (3.74 KB)

1
/* 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.gvsig.rasterTools.brightnessContrast.filter;
21

    
22
import java.awt.Image;
23
import java.awt.image.BufferedImage;
24
import java.util.Vector;
25

    
26
import org.cresques.filter.RasterBuf;
27
/**
28
 * Filtro para crear histogramas de brillo para cada una de las bandas de la imagen.
29
 * Toma como entrada la imagen y da como salida un histograma para cada una de las
30
 * bandas representado mediante una matriz.
31
 * @author Miguel ?ngel Querol Carratal?  <querol_mig@gva.es>
32
 *
33
 */
34

    
35
public class HistogramImageFilter extends HistogramFilter {
36

    
37
        
38
        /**
39
         * Matriz donde se van a almacenar los histogramas.
40
         */
41
        int histoRed[] = new int[256];
42
        int histoGreen[] =  new int[256];
43
        int histoBlue[] = new int[256];
44
        
45
        
46
        /**
47
         * Constructor
48
         *
49
         */
50
        
51
        
52
        public HistogramImageFilter(){
53
                super();
54
        }
55
        
56
        public void pre(){
57
                this.exec=true;
58
                this.image = (Image) params.get("raster");
59
                this.width = image.getWidth(null);
60
                this.height = image.getHeight(null);
61
                
62
                for(int i = 0 ; i <= 255 ; i++){
63
                        histoRed[i]= 0; 
64
                        histoGreen[i] = 0;
65
                        histoBlue[i] = 0;
66
                }
67
                
68
                super.pre();
69
        }
70
        
71
        
72
        public void post() {
73
        }
74

    
75
        public void process(int x, int y) {
76
                int px = ((BufferedImage) image).getRGB(x, y);
77
                int px3[] = {(px & 0xff0000) >> 16, (px & 0xff00) >> 8, (px & 0xff)};
78
                
79
                this.histoRed[px3[0]]+=1;                  //Histograma para la banda R
80
                this.histoGreen[px3[1]]+=1;          //Histograma para la banda G
81
                this.histoBlue[px3[2]]+=1;          //Histograma para la banda B
82
                pixel++;                                                //Cuenta de los pixels de la imagen
83
        }
84

    
85
        /**
86
         * Metodo para obtener el valor medio de brillo para una banda de color.
87
         * @param vector
88
         * @return
89
         */
90
        public int calculaMedia(int []vector){
91
                int media = 0;
92
                int suma = 0;
93
                
94
                for (int i = 1 ; i <= 255 ; i++){
95
                        suma += vector[i]*i;
96
                }
97
                media = suma/pixel;
98
                
99
                return media;
100
        }
101
        
102
        public int getPixel(){
103
                return this.pixel;
104
        }
105
        
106
        public void processLine(int y) {
107
                // TODO Auto-generated method stub
108

    
109
        }
110

    
111
        public int getInRasterDataType() {
112
                return RasterBuf.TYPE_IMAGE;
113
        }
114

    
115
        public int getOutRasterDataType() {
116
                return RasterBuf.TYPE_IMAGE;
117
        }
118

    
119
        public Object getResult(String name) {
120
                if (name.equals("raster")) {
121
            return (Object) this.image;
122
        } else {
123
            return null;
124
        }
125
        }
126
        
127
        /**
128
         * Metodos para acceder al los valores medios de brillo para cada banda
129
         * @return Media de brillo para la banda de color correspondiente.
130
         */
131
        
132
        public int getMediaRed(){
133
                return calculaMedia(this.histoRed);
134
        }
135

    
136
        public int getMediaGreen(){
137
                return calculaMedia(this.histoGreen);
138
        }
139
        
140
        public int getMediaBlue(){
141
                return calculaMedia(this.histoBlue);
142
        }
143
        
144
        
145
        /**
146
         * Metodos para acceder desde fuera a los histogramas calculados por el filtro
147
         * @return Histogramas de cada una de las bandas de color.
148
         */
149
        public Object getRedHistogram(){
150
                return (Object) this.histoRed;
151
        }
152
        
153
        public Object getGreenHistogram(){
154
                return (Object) this.histoGreen;
155
        }
156
        
157
        public Object getBlueHistogram(){
158
                return (Object) this.histoBlue;
159
        }
160
        
161
        
162
}