Statistics
| Revision:

root / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / io / raster / PercentTailTrimFilter.java @ 2669

History | View | Annotate | Download (6.35 KB)

1
/*
2
 * Cresques Mapping Suite. Graphic Library for constructing mapping applications.
3
 *
4
 * Copyright (C) 2004-5.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 * cresques@gmail.com
23
 */
24
package org.cresques.io.raster;
25

    
26
import java.util.ArrayList;
27

    
28

    
29
/**
30
 * Filtro de recorte de colas. Este filtro toma pixels de la imagen (todos o algunas muestras
31
 * dependiendo de la variable percentSample) y los ordena. Recorta un porcentaje controlado
32
 * por tailPercenten ambos extremos del vector ordenado. El nuevo m?ximo y m?nimo coinciden
33
 * con el valor de la posici?n del vector recortado. Por arriba para el m?ximo y por abajo
34
 * para el m?nimo.
35
 * El execute de este filtro no recorre toda la imagen sino que lo
36
 * hace en funci?n del porcentaje de muestras que quieren tomarse y
37
 * calculando a partir de este porcentaje un incremento.
38
 * @author Nacho Brodin (brodin_ign@gva.es)
39
 */
40
public abstract class PercentTailTrimFilter extends RasterFilter {
41
    protected int[] min = {
42
                              Integer.MAX_VALUE, Integer.MAX_VALUE,
43
                              Integer.MAX_VALUE
44
                          };
45
    protected int[] max = {
46
                              Integer.MIN_VALUE, Integer.MIN_VALUE,
47
                              Integer.MIN_VALUE
48
                          };
49
    protected int count = 0;
50
    protected int[][] sample;
51
    private int tailSize = 0;
52
    private int nSamples = 0;
53
    private boolean removeMaxValue = false;
54

    
55
    //Par?metros del filtro
56
    protected double tailPercent = 0D;
57
    public double percentSamples = 0D;
58
    protected ArrayList fileList = new ArrayList();
59

    
60
    public PercentTailTrimFilter() {
61
    }
62

    
63
    /**
64
     * Calcula el incremento de X y de Y para la toma de muestras en el calculo de
65
     * valores para el recorte
66
     */
67
    public void pre() {
68
        //Obtenemos par?metros comunes
69
        this.stats = (RasterStats) params.get("stats");
70
        this.tailPercent = ((Double) params.get("tail")).doubleValue();
71
        this.percentSamples = ((Double) params.get("samples")).doubleValue();
72
        this.removeMaxValue = ((Boolean) params.get("remove")).booleanValue();
73

    
74
        if (exec) {
75
            this.stats.tailPercent = this.tailPercent;
76
            count = 0;
77

    
78
            if (this.percentSamples == 0) { //Se toman todos los pixeles de la imagen 
79
                nSamples = height * width;
80
                sample = new int[3][height * width];
81
                tailSize = (int) Math.round(this.tailPercent * nSamples);
82
            } else { //Se toma un porcentaje de pixeles de la imagen para el calculo
83
                incX = (int) Math.round(width / (int) Math.round(this.percentSamples * width));
84
                incY = (int) Math.round(height / (int) Math.round(this.percentSamples * height));
85
                nSamples = (int) ((Math.round(width / incX) + 1) * (Math.round(height / incY) +
86
                           1));
87
                sample = new int[3][nSamples];
88
                tailSize = (int) (nSamples * this.tailPercent);
89
            }
90
        }
91
    }
92

    
93
    /**
94
     * Ordena las muestras , recorta y asigna m?ximo y m?nimo dependiendo del porcentaje
95
     * de recorte
96
     */
97
    public void post() {
98
        if (exec) {
99
            QuickSort q = new QuickSort();
100

    
101
            int posInit = 0;
102
            int posFin = sample[0].length - 1;
103

    
104
            //Ordenamos los vectores
105
            for (int i = 0; i < 3; i++)
106
                q.quicksort(sample[i]);
107

    
108
            //Si est? marcada la opci?n removeMaxValue se calcula la posici?n en la que el m?ximo valor
109
            //y el m?nimo ya no estan, teniendo as? un subconjunto del vector que elimina estos valores
110
            if (removeMaxValue) {
111
                for (int i = 0; i < sample[0].length; i++) {
112
                    if ((sample[0][i] != sample[0][0]) ||
113
                            (sample[1][i] != sample[1][0]) ||
114
                            (sample[2][i] != sample[2][0])) {
115
                        posInit = i;
116

    
117
                        break;
118
                    }
119
                }
120

    
121
                for (int i = sample[0].length - 1; i > 0; i--) {
122
                    if ((sample[0][i] != sample[0][sample[0].length - 1]) ||
123
                            (sample[1][i] != sample[1][sample[0].length - 1]) ||
124
                            (sample[2][i] != sample[2][sample[0].length - 1])) {
125
                        posFin = i;
126

    
127
                        break;
128
                    }
129
                }
130

    
131
                //Calculamos de nuevo el n?mero de muestras ya que hemos quitado los valores m?ximo y m?nimo
132
                nSamples = posFin - posInit;
133

    
134
                //Como ha podido cambiar nSamples recalculamos tailsize
135
                tailSize = (int) (nSamples * this.tailPercent);
136
            }
137

    
138
            //Ordenamos los elementos y cogemos el minimo y m?ximo para cada banda
139
            for (int i = 0; i < 3; i++) {
140
                stats.minBandValue[i] = sample[i][posInit + tailSize];
141
                stats.maxBandValue[i] = sample[i][(posInit + nSamples) -
142
                                        tailSize - 1];
143
            }
144
        }
145
    }
146

    
147
    /**
148
     * Obtiene el objeto stats con el m?ximo y m?nimo calculado
149
     */
150
    public Object getResult(String name) {
151
        if (name.equals("stats")) {
152
            return (Object) this.stats;
153
        } else {
154
            return null;
155
        }
156
    }
157

    
158
    /**
159
     * Obtiene el porcentaje de recorte
160
     * @return porcentaje de recorte
161
     */
162
    public double getTailPercent() {
163
        return tailPercent;
164
    }
165

    
166
    /**
167
     * Devuelve true si se eliminan los extremos de la serie antes del calculo del recorte de colas
168
     * o false si no se eliminan.
169
     * @return
170
     */
171
    public boolean removeMaxValue() {
172
        return this.removeMaxValue;
173
    }
174
}