Statistics
| Revision:

gvsig-gdal / trunk / org.gvsig.gdal2 / org.gvsig.gdal2.prov / org.gvsig.gdal2.prov.raster.legend / src / main / java / org / gvsig / raster / gdal / provider / legend / RasterGdalGetColorInterpretation.java @ 370

History | View | Annotate | Download (10.7 KB)

1
package org.gvsig.raster.gdal.provider.legend;
2

    
3
import java.awt.Color;
4
import java.util.ArrayList;
5
import java.util.List;
6

    
7
import org.gdal.gdal.Band;
8
import org.gdal.gdal.Dataset;
9
import org.gdal.gdal.gdal;
10

    
11
import org.gvsig.fmap.dal.raster.api.RasterStore;
12
import org.gvsig.raster.gdal.provider.AbstractRasterGdalStoreProvider;
13
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
14
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
15
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation;
16
import org.gvsig.raster.lib.legend.api.colortable.ColorTable;
17
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass;
18
import org.gvsig.tools.ToolsLocator;
19
import org.gvsig.tools.dynobject.DynClass;
20
import org.gvsig.tools.dynobject.DynMethod;
21
import org.gvsig.tools.dynobject.DynObject;
22
import org.gvsig.tools.dynobject.exception.DynMethodException;
23
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
24

    
25
/**
26
 * @author dmartinezizquierdo
27
 *
28
 */
29
public class RasterGdalGetColorInterpretation implements DynMethod {
30

    
31
    private static Integer code = null;
32

    
33
    static void register(DynClass storeClass) {
34
        if (code != null) {
35
            return;
36
        }
37
        code = ToolsLocator.getDynObjectManager()
38
                .registerDynMethod(storeClass, new RasterGdalGetColorInterpretation());
39

    
40
    }
41

    
42
    @Override
43
    public int getCode() throws DynMethodNotSupportedException {
44
        return code;
45
    }
46

    
47
    @Override
48
    public String getDescription() {
49
        return "Raster GDAL Color Interpretation";
50
    }
51

    
52
    @Override
53
    public String getName() {
54
        return RasterStore.DYNMETHOD_GETCOLORINTERPRETATION_NAME;
55
    }
56

    
57
    @Override
58
    public Object invoke(DynObject self, Object[] args)
59
        throws DynMethodException {
60
        ColorInterpretation colorInterpretation = null;
61

    
62
        AbstractRasterGdalStoreProvider rasterGdalProvider =
63
            (AbstractRasterGdalStoreProvider) self;
64

    
65
        RasterLegendManager legendManager =
66
            RasterLegendLocator.getRasterLegendManager();
67
        Dataset gdalDataSet = rasterGdalProvider.getGdalDataSet();
68

    
69
        String[] bandColorInterpretations =
70
            new String[gdalDataSet.getRasterCount()];
71
        for (int i = 0; i < gdalDataSet.getRasterCount(); i++) {
72
            // Bands begin in 1 instead of 0
73
            int bandNumber = i + 1;
74
            Band gdalBand = gdalDataSet.GetRasterBand(bandNumber);
75
            String bandColorInterpretion =
76
                colorInterpretationFromGdal(gdalBand.GetRasterColorInterpretation());
77
            bandColorInterpretations[i] = bandColorInterpretion;
78
        }
79
        //FIXME:Intenta suponer cual es la forma correcta de interpretar los colores en funcion del numero de bandas
80
        //Seguramente haya que arreglarlo cuando haya GUI para que el usuario pueda especificar la forma correcta.
81
        guessingColorInterpretation(bandColorInterpretations);
82

    
83
        colorInterpretation =
84
            legendManager.createColorInterpretation(bandColorInterpretations);
85

    
86

    
87
        for (int bandNumber = 1; bandNumber <= gdalDataSet.getRasterCount(); bandNumber++) {
88
            //Band in GDAL begins in 1, meanwhile we begin in 0
89
            int indexColorInterpretation=bandNumber-1;
90
            if (ColorInterpretation.PALETTE_BAND.equals(colorInterpretation.get(indexColorInterpretation))) {
91
                ColorTable colorTable = null;
92
                Band gdalBand = gdalDataSet.GetRasterBand(bandNumber);
93
                org.gdal.gdal.ColorTable gdalBandColorTable = gdalBand.GetColorTable();
94
                if (gdalBandColorTable != null) {
95
                    List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>();
96

    
97
                    for (int i = 0; i < gdalBandColorTable.GetCount(); i++) {
98
                        String className = i + "";
99
                        double value = ((byte) i);
100
                        double interpolation = 50.0;
101
                        Color color = gdalBandColorTable.GetColorEntry(i);
102
                        ColorTableClass colorTableClass =
103
                            legendManager.createColorTableClass(className, value, interpolation, color);
104
                        colorTableClasses.add(colorTableClass);
105
                    }
106
                    String colorTableName = rasterGdalProvider.getName() + "_color_table";
107
                    colorTable = legendManager.createColorTable(colorTableName, colorTableClasses, true);
108
                    // FIXME: De momento devolvemos el primero solo.
109
//                    return colorTable;
110
                } else {
111
                    List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>();
112
                    double[] minMax = new double[2];
113
                    gdalBand.ComputeRasterMinMax(minMax);
114
                    Double increment = (minMax[1] - minMax[0]) / 256;
115

    
116
                    for (int i = 0; i < 255; i++) {
117
                        String className = i + "";
118
                        double value = ((minMax[0] + (i * increment)));
119
                        double interpolation = 50.0;
120
                        int intARGB = ((i & 0xFF) << 24) | // alpha
121
                            ((i & 0xFF) << 16) | // red
122
                            ((i & 0xFF) << 8) | // green
123
                            ((i & 0xFF) << 0); // blue
124
                        Color color = new Color(intARGB);
125
                        ColorTableClass colorTableClass =
126
                            legendManager.createColorTableClass(className, value, interpolation, color);
127
                        colorTableClasses.add(colorTableClass);
128
                    }
129
                    String colorTableName = rasterGdalProvider.getName() + "_color_table";
130
                    colorTable = legendManager.createColorTable(colorTableName, colorTableClasses, true);
131
                    // FIXME: De momento devolvemos el primero solo.
132
//                    return colorTable;
133
                }
134
                colorInterpretation.setPalette(colorTable);
135
                colorInterpretation.setPaletteBand(bandNumber);
136
            }
137
        }
138

    
139
        return colorInterpretation;
140
    }
141

    
142
    private void guessingColorInterpretation(String[] bandColorInterpretations){
143
        //FIXME:Intenta suponer cual es la forma correcta de interpretar los colores en funcion del numero de bandas
144
        //Seguramente haya que arreglarlo cuando haya GUI para que el usuario pueda especificar la forma correcta.
145
        if (bandColorInterpretations.length==1){
146
            if (bandColorInterpretations[0].equals(ColorInterpretation.UNDEFINED_BAND)){
147
                bandColorInterpretations[0]=ColorInterpretation.PALETTE_BAND;
148
            }
149
        }
150
        if (bandColorInterpretations.length==3 || bandColorInterpretations.length>4){
151
            if (bandColorInterpretations[0].equals(ColorInterpretation.UNDEFINED_BAND)  ||
152
                bandColorInterpretations[1].equals(ColorInterpretation.UNDEFINED_BAND) ||
153
                bandColorInterpretations[2].equals(ColorInterpretation.UNDEFINED_BAND) ){
154

    
155
                bandColorInterpretations[0]=ColorInterpretation.RED_BAND;
156
                bandColorInterpretations[1]=ColorInterpretation.GREEN_BAND;
157
                bandColorInterpretations[2]=ColorInterpretation.BLUE_BAND;
158
            }
159
        }
160
        if (bandColorInterpretations.length==4){
161
            if (bandColorInterpretations[0].equals(ColorInterpretation.UNDEFINED_BAND)  &&
162
                bandColorInterpretations[1].equals(ColorInterpretation.UNDEFINED_BAND) &&
163
                bandColorInterpretations[2].equals(ColorInterpretation.UNDEFINED_BAND) &&
164
                bandColorInterpretations[3].equals(ColorInterpretation.UNDEFINED_BAND) ){
165

    
166
                bandColorInterpretations[0]=ColorInterpretation.RED_BAND;
167
                bandColorInterpretations[1]=ColorInterpretation.GREEN_BAND;
168
                bandColorInterpretations[2]=ColorInterpretation.BLUE_BAND;
169
                bandColorInterpretations[3]=ColorInterpretation.ALPHA_BAND;
170
            } else if (bandColorInterpretations[0].equals(ColorInterpretation.RED_BAND)  &&
171
                bandColorInterpretations[1].equals(ColorInterpretation.GREEN_BAND) &&
172
                bandColorInterpretations[2].equals(ColorInterpretation.BLUE_BAND) &&
173
                bandColorInterpretations[3].equals(ColorInterpretation.UNDEFINED_BAND) ){
174

    
175
                bandColorInterpretations[3]=ColorInterpretation.ALPHA_BAND;
176

    
177
            }
178
        }
179
    }
180

    
181
    /**
182
     * Obtiene la cadena que representa el tipo de banda de color. Los tipos posibles son:
183
     * <UL>
184
     *  <LI>0 = "Undefined" </LI>
185
     *  <LI>1 = "Gray";</LI>
186
     *  <LI>2 = "Palette";</LI>
187
     *  <LI>3 = "Red";</LI>
188
     *  <LI>4 = "Green";</LI>
189
     *  <LI>5 = "Blue";</LI>
190
     *  <LI>6 = "Alpha";</LI>
191
     *  <LI>7 = "Hue";</LI>
192
     *  <LI>8 = "Saturation";</LI>
193
     *  <LI>9 = "Lightness";</LI>
194
     *  <LI>10 = "Cyan";</LI>
195
     *  <LI>11 = "Magenta";</LI>
196
     *  <LI>12 = "Yellow";</LI>
197
     *  <LI>13 = "Black";</LI>
198
     *  <LI>14 = "YCbCr_Y";</LI>
199
     *  <LI>15 = "YCbCr_Cb";</LI>
200
     *  <LI>16 = "YCbCr_Cr";</LI>
201
     * </UL>
202
     * @return  Cadena con el nombre del tipo de banda de colorn
203
     */
204
    private String colorInterpretationFromGdal(int gdalCode){
205
        String gdalNameCI=gdal.GetColorInterpretationName(gdalCode);
206

    
207
        switch (gdalNameCI) {
208
        case "Undefined" :
209
            return ColorInterpretation.UNDEFINED_BAND;
210
        case "Gray" :
211
            return ColorInterpretation.GRAY_BAND;
212
        case "Palette" :
213
            return ColorInterpretation.PALETTE_BAND;
214
        case "Red" :
215
            return ColorInterpretation.RED_BAND;
216
        case "Green" :
217
            return ColorInterpretation.GREEN_BAND;
218
        case "Blue" :
219
            return ColorInterpretation.BLUE_BAND;
220
        case "Alpha" :
221
            return ColorInterpretation.ALPHA_BAND;
222
        case "Hue" :
223
            return ColorInterpretation.HUE_BAND;
224
        case "Saturation" :
225
            return ColorInterpretation.SATURATION_BAND;
226
        case "Lightness" :
227
            return ColorInterpretation.LIGHTNESS_BAND;
228
        case "Cyan" :
229
            return ColorInterpretation.CYAN_BAND;
230
        case "Magenta" :
231
            return ColorInterpretation.MAGENTA_BAND;
232
        case "Yellow" :
233
            return ColorInterpretation.YELLOW_BAND;
234
        case "Black" :
235
            return ColorInterpretation.BLACK_BAND;
236
        case "YCbCr_Y" :
237
            return ColorInterpretation.YCBCR_Y_BAND;
238
        case "YCbCr_Cb" :
239
            return ColorInterpretation.YCBCR_CB_BAND;
240
        case "YCbCr_Cr" :
241
            return ColorInterpretation.YCBCR_CR_BAND;
242
        default:
243
            return ColorInterpretation.UNDEFINED_BAND;
244
        }
245
    }
246

    
247
    @Override
248
    public Object clone() throws CloneNotSupportedException {
249
        return super.clone();
250
    }
251
}