root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / imageAnalysis / his2rgb / HIS2RGBAlgorithm.java @ 59
History | View | Annotate | Download (4.78 KB)
1 |
package es.unex.sextante.imageAnalysis.his2rgb; |
---|---|
2 |
|
3 |
import es.unex.sextante.core.GeoAlgorithm; |
4 |
import es.unex.sextante.core.AnalysisExtent; |
5 |
import es.unex.sextante.core.Sextante; |
6 |
import es.unex.sextante.dataObjects.IRasterLayer; |
7 |
import es.unex.sextante.exceptions.GeoAlgorithmExecutionException; |
8 |
import es.unex.sextante.exceptions.OptionalParentParameterException; |
9 |
import es.unex.sextante.exceptions.RepeatedParameterNameException; |
10 |
import es.unex.sextante.exceptions.UndefinedParentParameterNameException; |
11 |
|
12 |
public class HIS2RGBAlgorithm |
13 |
extends
|
14 |
GeoAlgorithm { |
15 |
|
16 |
public static final String LAYERH = "LAYERH"; |
17 |
public static final String LAYERI = "LAYERI"; |
18 |
public static final String LAYERS = "LAYERS"; |
19 |
public static final String BANDH = "BANDH"; |
20 |
public static final String BANDI = "BANDI"; |
21 |
public static final String BANDS = "BANDS"; |
22 |
public static final String R = "R"; |
23 |
public static final String G = "G"; |
24 |
public static final String B = "B"; |
25 |
|
26 |
|
27 |
@Override
|
28 |
public void defineCharacteristics() { |
29 |
|
30 |
setName("HIS -> RGB");
|
31 |
setGroup(Sextante.getText("Image_processing"));
|
32 |
setUserCanDefineAnalysisExtent(true);
|
33 |
|
34 |
try {
|
35 |
m_Parameters.addInputRasterLayer(LAYERH, Sextante.getText("H_layer"), true); |
36 |
m_Parameters.addBand(BANDH, Sextante.getText("H_band"), LAYERH);
|
37 |
m_Parameters.addInputRasterLayer("LAYERI", Sextante.getText("I_layer"), true); |
38 |
m_Parameters.addBand(BANDI, Sextante.getText("I_band"), LAYERI);
|
39 |
m_Parameters.addInputRasterLayer("LAYERS", Sextante.getText("S_layer"), true); |
40 |
m_Parameters.addBand(BANDS, Sextante.getText("S_band"), LAYERS);
|
41 |
addOutputRasterLayer(R, "R");
|
42 |
addOutputRasterLayer(G, "G");
|
43 |
addOutputRasterLayer(B, "B");
|
44 |
} |
45 |
catch (final RepeatedParameterNameException e) { |
46 |
Sextante.addErrorToLog(e); |
47 |
} |
48 |
catch (final UndefinedParentParameterNameException e) { |
49 |
Sextante.addErrorToLog(e); |
50 |
} |
51 |
catch (final OptionalParentParameterException e) { |
52 |
Sextante.addErrorToLog(e); |
53 |
} |
54 |
|
55 |
} |
56 |
|
57 |
|
58 |
@Override
|
59 |
public boolean processAlgorithm() throws GeoAlgorithmExecutionException { |
60 |
|
61 |
int x, y;
|
62 |
int iNX, iNY;
|
63 |
byte r, g, b, i, s;
|
64 |
int h;
|
65 |
IRasterLayer rLayer, gLayer, bLayer; |
66 |
|
67 |
final IRasterLayer hLayer = m_Parameters.getParameterValueAsRasterLayer(LAYERH);
|
68 |
final IRasterLayer iLayer = m_Parameters.getParameterValueAsRasterLayer(LAYERI);
|
69 |
final IRasterLayer sLayer = m_Parameters.getParameterValueAsRasterLayer(LAYERS);
|
70 |
final int hBand = m_Parameters.getParameterValueAsInt(BANDH); |
71 |
final int iBand = m_Parameters.getParameterValueAsInt(BANDI); |
72 |
final int sBand = m_Parameters.getParameterValueAsInt(BANDS); |
73 |
|
74 |
rLayer = getNewRasterLayer(R, "R", IRasterLayer.RASTER_DATA_TYPE_BYTE);
|
75 |
gLayer = getNewRasterLayer(G, "G", IRasterLayer.RASTER_DATA_TYPE_BYTE);
|
76 |
bLayer = getNewRasterLayer(B, "B", IRasterLayer.RASTER_DATA_TYPE_BYTE);
|
77 |
|
78 |
final AnalysisExtent extent = rLayer.getWindowGridExtent();
|
79 |
|
80 |
hLayer.setWindowExtent(extent); |
81 |
sLayer.setWindowExtent(extent); |
82 |
iLayer.setWindowExtent(extent); |
83 |
|
84 |
iNX = extent.getNX(); |
85 |
iNY = extent.getNY(); |
86 |
|
87 |
for (y = 0; (y < iNY) && setProgress(y, iNY); y++) { |
88 |
for (x = 0; x < iNX; x++) { |
89 |
h = hLayer.getCellValueAsInt(x, y, hBand); |
90 |
i = iLayer.getCellValueAsByte(x, y, iBand); |
91 |
s = sLayer.getCellValueAsByte(x, y, sBand); |
92 |
|
93 |
if (hLayer.isNoDataValue(h) || iLayer.isNoDataValue(i) || sLayer.isNoDataValue(s)) {
|
94 |
rLayer.setNoData(x, y); |
95 |
gLayer.setNoData(x, y); |
96 |
bLayer.setNoData(x, y); |
97 |
} |
98 |
else {
|
99 |
if ((h <= 0) && (h < 120)) { |
100 |
r = (byte) (1 + ((s * Math.cos(h)) / (Math.cos(Math.toRadians(60 - h)))) * i); |
101 |
b = (byte) (i * (1 - s)); |
102 |
g = (byte) (1 - r - b); |
103 |
} |
104 |
else if (h < 240) { |
105 |
h = h - 120;
|
106 |
g = (byte) (1 + ((s * Math.cos(h)) / (Math.cos(Math.toRadians(60 - h)))) * i); |
107 |
r = (byte) (i * (1 - s)); |
108 |
b = (byte) (1 - r - g); |
109 |
} |
110 |
else {
|
111 |
h = h - 240;
|
112 |
b = (byte) (1 + ((s * Math.cos(h)) / (Math.cos(Math.toRadians(60 - h)))) * i); |
113 |
g = (byte) (i * (1 - s)); |
114 |
r = (byte) (1 - g - b); |
115 |
} |
116 |
|
117 |
rLayer.setCellValue(x, y, r); |
118 |
gLayer.setCellValue(x, y, g); |
119 |
bLayer.setCellValue(x, y, b); |
120 |
} |
121 |
|
122 |
} |
123 |
} |
124 |
|
125 |
return !m_Task.isCanceled();
|
126 |
|
127 |
} |
128 |
|
129 |
|
130 |
} |