gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.fmap.dal.file.jimi.legend / src / main / java / org / gvsig / fmap / dal / file / jimi / legend / JimiGetColorInterpretation.java @ 6900
History | View | Annotate | Download (3.93 KB)
1 |
package org.gvsig.fmap.dal.file.jimi.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 |
import java.util.ArrayList; |
8 |
import java.util.List; |
9 |
|
10 |
import org.gvsig.fmap.dal.file.jimi.JimiRasterStoreProvider; |
11 |
import org.gvsig.fmap.dal.file.jimi.MemoryImage; |
12 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
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 JimiGetColorInterpretation 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 JimiGetColorInterpretation());
|
39 |
|
40 |
} |
41 |
|
42 |
@Override
|
43 |
public int getCode() throws DynMethodNotSupportedException { |
44 |
return code;
|
45 |
} |
46 |
|
47 |
@Override
|
48 |
public String getDescription() { |
49 |
return "Jimi 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) throws DynMethodException { |
59 |
ColorInterpretation colorInterpretation = null;
|
60 |
// try {
|
61 |
JimiRasterStoreProvider jimiProvider = (JimiRasterStoreProvider) self; |
62 |
|
63 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
64 |
MemoryImage image = jimiProvider.getMemoryImage(); |
65 |
|
66 |
ColorModel colorModel = image.colorModel;
|
67 |
if (colorModel instanceof DirectColorModel) { |
68 |
if (image.bands == 3) { |
69 |
colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.RGB); |
70 |
} else if (image.bands == 4) { |
71 |
colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.ARGB); |
72 |
} |
73 |
} else if (colorModel instanceof IndexColorModel) { |
74 |
colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.PALETTE); |
75 |
|
76 |
ColorTable colorTable = null;
|
77 |
IndexColorModel indexColorModel = (IndexColorModel) colorModel; |
78 |
int[] rgbs = new int[indexColorModel.getMapSize()]; |
79 |
boolean hasalpha = indexColorModel.hasAlpha();
|
80 |
indexColorModel.getRGBs(rgbs); |
81 |
|
82 |
List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>(); |
83 |
|
84 |
for (int i = 0; i < rgbs.length; i++) { |
85 |
String className = i + ""; |
86 |
double value = ((byte) i); |
87 |
double interpolation = 50.0; |
88 |
Color color = new Color(rgbs[i], hasalpha); |
89 |
ColorTableClass colorTableClass = |
90 |
legendManager.createColorTableClass(className, value, interpolation, color); |
91 |
colorTableClasses.add(colorTableClass); |
92 |
} |
93 |
String colorTableName = jimiProvider.getName() + "_color_table"; |
94 |
colorTable = legendManager.createColorTable(colorTableName, colorTableClasses, true);
|
95 |
|
96 |
colorInterpretation.setPalette(colorTable); |
97 |
colorInterpretation.setPaletteBand(0);
|
98 |
|
99 |
} |
100 |
|
101 |
return colorInterpretation;
|
102 |
} |
103 |
|
104 |
@Override
|
105 |
public Object clone() throws CloneNotSupportedException { |
106 |
return super.clone(); |
107 |
} |
108 |
} |