Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.gdal / org.gvsig.raster.gdal.provider.legend / src / main / java / org / gvsig / raster / gdal / provider / legend / RasterGdalGetColorInterpretation.java @ 6316

History | View | Annotate | Download (5.12 KB)

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

    
3
import java.awt.Color;
4
import java.awt.image.ColorModel;
5
import java.awt.image.DirectColorModel;
6
import java.awt.image.IndexColorModel;
7

    
8
import org.gdal.gdal.Band;
9
import org.gdal.gdal.ColorTable;
10
import org.gdal.gdal.Dataset;
11
import org.gdal.gdal.gdal;
12
import org.gdal.gdalconst.gdalconst;
13
import org.gdal.gdalconst.gdalconstConstants;
14
import org.gdal.gdalconst.gdalconstJNI;
15
import org.gvsig.fmap.dal.raster.api.RasterStore;
16
import org.gvsig.raster.gdal.provider.RasterGdalStoreProvider;
17
import org.gvsig.raster.lib.legend.api.ColorInterpretation;
18
import org.gvsig.raster.lib.legend.api.RasterLegendLocator;
19
import org.gvsig.raster.lib.legend.api.RasterLegendManager;
20
import org.gvsig.tools.ToolsLocator;
21
import org.gvsig.tools.dynobject.DynClass;
22
import org.gvsig.tools.dynobject.DynMethod;
23
import org.gvsig.tools.dynobject.DynObject;
24
import org.gvsig.tools.dynobject.exception.DynMethodException;
25
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException;
26

    
27
/**
28
 * @author dmartinezizquierdo
29
 *
30
 */
31
public class RasterGdalGetColorInterpretation implements DynMethod {
32

    
33
    private static Integer code = null;
34

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

    
42
    }
43

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

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

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

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

    
64
        RasterGdalStoreProvider rasterGdalProvider =
65
            (RasterGdalStoreProvider) self;
66

    
67
        RasterLegendManager legendManager =
68
            RasterLegendLocator.getRasterLegendManager();
69
        Dataset gdalDataSet = rasterGdalProvider.getGdalDataSet();
70

    
71
        String[] bandColorInterpretations =
72
            new String[gdalDataSet.getRasterCount()];
73
        for (int i = 0; i < gdalDataSet.getRasterCount(); i++) {
74
            // Bands begin in 1 instead of 0
75
            int bandNumber = i + 1;
76
            Band gdalBand = gdalDataSet.GetRasterBand(bandNumber);
77
            String bandColorInterpretion =
78
                colorInterpretationFromGdal(gdalBand.GetColorInterpretation());
79
            bandColorInterpretations[i] = bandColorInterpretion;
80
        }
81
        colorInterpretation =
82
            legendManager.createColorInterpretation(bandColorInterpretations);
83

    
84
        return colorInterpretation;
85
    }
86

    
87
    /**
88
     * Obtiene la cadena que representa el tipo de banda de color. Los tipos posibles son:
89
     * <UL>
90
     *  <LI>0 = "Undefined" </LI>
91
     *  <LI>1 = "Gray";</LI>
92
     *  <LI>2 = "Palette";</LI>
93
     *  <LI>3 = "Red";</LI>
94
     *  <LI>4 = "Green";</LI>
95
     *  <LI>5 = "Blue";</LI>
96
     *  <LI>6 = "Alpha";</LI>
97
     *  <LI>7 = "Hue";</LI>
98
     *  <LI>8 = "Saturation";</LI>
99
     *  <LI>9 = "Lightness";</LI>
100
     *  <LI>10 = "Cyan";</LI>
101
     *  <LI>11 = "Magenta";</LI>
102
     *  <LI>12 = "Yellow";</LI>
103
     *  <LI>13 = "Black";</LI>
104
     *  <LI>14 = "YCbCr_Y";</LI>
105
     *  <LI>15 = "YCbCr_Cb";</LI>
106
     *  <LI>16 = "YCbCr_Cr";</LI>
107
     * </UL>
108
     * @return  Cadena con el nombre del tipo de banda de colorn
109
     */
110
    private String colorInterpretationFromGdal(int gdalCode){
111
        String gdalNameCI=gdal.GetColorInterpretationName(gdalCode);
112

    
113
        switch (gdalNameCI) {
114
        case "Undefined" :
115
            return ColorInterpretation.PALETTE_BAND;
116
        case "Gray" :
117
            return ColorInterpretation.GRAY_BAND;
118
        case "Palette" :
119
            return ColorInterpretation.PALETTE_BAND;
120
        case "Red" :
121
            return ColorInterpretation.RED_BAND;
122
        case "Green" :
123
            return ColorInterpretation.GREEN_BAND;
124
        case "Blue" :
125
            return ColorInterpretation.BLUE_BAND;
126
        case "Alpha" :
127
            return ColorInterpretation.ALPHA_BAND;
128
        case "Hue" :
129
            return ColorInterpretation.HUE_BAND;
130
        case "Saturation" :
131
            return ColorInterpretation.SATURATION_BAND;
132
        case "Lightness" :
133
            return ColorInterpretation.LIGHTNESS_BAND;
134
        case "Cyan" :
135
            return ColorInterpretation.CYAN_BAND;
136
        case "Magenta" :
137
            return ColorInterpretation.MAGENTA_BAND;
138
        case "Yellow" :
139
            return ColorInterpretation.YELLOW_BAND;
140
        case "Black" :
141
            return ColorInterpretation.BLACK_BAND;
142
        case "YCbCr_Y" :
143
            return ColorInterpretation.YCBCR_Y_BAND;
144
        case "YCbCr_Cb" :
145
            return ColorInterpretation.YCBCR_CB_BAND;
146
        case "YCbCr_Cr" :
147
            return ColorInterpretation.YCBCR_CR_BAND;
148
        default:
149
            return ColorInterpretation.UNDEFINED_BAND;
150
        }
151
    }
152

    
153
    @Override
154
    public Object clone() throws CloneNotSupportedException {
155
        return super.clone();
156
    }
157
}