svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster.legend / org.gvsig.raster.legend.png / src / main / java / org / gvsig / fmap / dal / file / png / legend / PngGetColorInterpretation.java @ 43803
History | View | Annotate | Download (4.01 KB)
1 |
package org.gvsig.fmap.dal.file.png.legend; |
---|---|
2 |
|
3 |
import java.awt.Color; |
4 |
import java.awt.image.BufferedImage; |
5 |
import java.awt.image.ColorModel; |
6 |
import java.awt.image.ComponentColorModel; |
7 |
import java.awt.image.IndexColorModel; |
8 |
import java.awt.image.WritableRaster; |
9 |
import java.util.ArrayList; |
10 |
import java.util.List; |
11 |
|
12 |
import org.gvsig.fmap.dal.file.png.PngStoreProvider; |
13 |
import org.gvsig.fmap.dal.raster.api.RasterStore; |
14 |
import org.gvsig.raster.lib.legend.api.RasterLegendLocator; |
15 |
import org.gvsig.raster.lib.legend.api.RasterLegendManager; |
16 |
import org.gvsig.raster.lib.legend.api.colorinterpretation.ColorInterpretation; |
17 |
import org.gvsig.raster.lib.legend.api.colortable.ColorTable; |
18 |
import org.gvsig.raster.lib.legend.api.colortable.colortableclass.ColorTableClass; |
19 |
import org.gvsig.tools.ToolsLocator; |
20 |
import org.gvsig.tools.dynobject.DynClass; |
21 |
import org.gvsig.tools.dynobject.DynMethod; |
22 |
import org.gvsig.tools.dynobject.DynObject; |
23 |
import org.gvsig.tools.dynobject.exception.DynMethodException; |
24 |
import org.gvsig.tools.dynobject.exception.DynMethodNotSupportedException; |
25 |
|
26 |
/**
|
27 |
* @author dmartinezizquierdo
|
28 |
*
|
29 |
*/
|
30 |
public class PngGetColorInterpretation implements DynMethod { |
31 |
|
32 |
private static Integer code = null; |
33 |
|
34 |
static void register(DynClass storeClass) { |
35 |
if (code != null) { |
36 |
return;
|
37 |
} |
38 |
code = ToolsLocator.getDynObjectManager() |
39 |
.registerDynMethod(storeClass, new PngGetColorInterpretation());
|
40 |
|
41 |
} |
42 |
|
43 |
@Override
|
44 |
public int getCode() throws DynMethodNotSupportedException { |
45 |
return code;
|
46 |
} |
47 |
|
48 |
@Override
|
49 |
public String getDescription() { |
50 |
return "PNG Color Interpretation"; |
51 |
} |
52 |
|
53 |
@Override
|
54 |
public String getName() { |
55 |
return RasterStore.DYNMETHOD_GETCOLORINTERPRETATION_NAME;
|
56 |
} |
57 |
|
58 |
@Override
|
59 |
public Object invoke(DynObject self, Object[] args) throws DynMethodException { |
60 |
ColorInterpretation colorInterpretation = null;
|
61 |
// try {
|
62 |
PngStoreProvider provider = (PngStoreProvider) self; |
63 |
|
64 |
RasterLegendManager legendManager = RasterLegendLocator.getRasterLegendManager(); |
65 |
BufferedImage image = provider.getImage();
|
66 |
ColorModel colorModel = image.getColorModel();
|
67 |
|
68 |
if (colorModel instanceof ComponentColorModel) { //DirectColorModel) { |
69 |
WritableRaster raster = image.getRaster();
|
70 |
if (raster.getNumBands() == 3) { |
71 |
colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.RGB); |
72 |
} else if (raster.getNumBands() == 4) { |
73 |
colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.ARGB); |
74 |
} |
75 |
} else if (colorModel instanceof IndexColorModel) { |
76 |
colorInterpretation = legendManager.createColorInterpretation(ColorInterpretation.PALETTE); |
77 |
|
78 |
ColorTable colorTable = null;
|
79 |
IndexColorModel indexColorModel = (IndexColorModel) colorModel; |
80 |
int[] rgbs = new int[indexColorModel.getMapSize()]; |
81 |
boolean hasalpha = indexColorModel.hasAlpha();
|
82 |
indexColorModel.getRGBs(rgbs); |
83 |
|
84 |
List<ColorTableClass> colorTableClasses = new ArrayList<ColorTableClass>(); |
85 |
|
86 |
for (int i = 0; i < rgbs.length; i++) { |
87 |
String className = i + ""; |
88 |
double value = ((byte) i); |
89 |
double interpolation = 50.0; |
90 |
Color color = new Color(rgbs[i], hasalpha); |
91 |
ColorTableClass colorTableClass = |
92 |
legendManager.createColorTableClass(className, value, interpolation, color); |
93 |
colorTableClasses.add(colorTableClass); |
94 |
} |
95 |
String colorTableName = provider.getName() + "_color_table"; |
96 |
colorTable = legendManager.createColorTable(colorTableName, colorTableClasses, true);
|
97 |
|
98 |
colorInterpretation.setPalette(colorTable); |
99 |
colorInterpretation.setPaletteBand(0);
|
100 |
|
101 |
} |
102 |
|
103 |
return colorInterpretation;
|
104 |
} |
105 |
|
106 |
@Override
|
107 |
public Object clone() throws CloneNotSupportedException { |
108 |
return super.clone(); |
109 |
} |
110 |
} |