Statistics
| Revision:

root / org.gvsig.toolbox / trunk / org.gvsig.toolbox / org.gvsig.toolbox.algorithm / src / main / java / es / unex / sextante / imageAnalysis / rgb2his / RGB2HISAlgorithm.java @ 59

History | View | Annotate | Download (4.65 KB)

1
package es.unex.sextante.imageAnalysis.rgb2his;
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 RGB2HISAlgorithm
13
         extends
14
            GeoAlgorithm {
15

    
16
   public static final String LAYERR = "LAYERR";
17
   public static final String LAYERB = "LAYERB";
18
   public static final String LAYERG = "LAYERG";
19
   public static final String BANDR  = "BANDR";
20
   public static final String BANDG  = "BANDG";
21
   public static final String BANDB  = "BANDB";
22
   public static final String H      = "H";
23
   public static final String I      = "I";
24
   public static final String S      = "S";
25

    
26

    
27
   @Override
28
   public void defineCharacteristics() {
29

    
30
      setName("RGB -> HIS");
31
      setGroup(Sextante.getText("Image_processing"));
32
      setUserCanDefineAnalysisExtent(true);
33

    
34
      try {
35
         m_Parameters.addInputRasterLayer(LAYERR, Sextante.getText("R_layer"), true);
36
         m_Parameters.addBand(BANDR, Sextante.getText("R_band"), LAYERR);
37
         m_Parameters.addInputRasterLayer("LAYERG", Sextante.getText("G_layer"), true);
38
         m_Parameters.addBand(BANDG, Sextante.getText("G_band"), LAYERG);
39
         m_Parameters.addInputRasterLayer(LAYERB, Sextante.getText("B_layer"), true);
40
         m_Parameters.addBand(BANDB, Sextante.getText("B_band"), LAYERB);
41
         addOutputRasterLayer(H, "H");
42
         addOutputRasterLayer(I, "I");
43
         addOutputRasterLayer(S, "S");
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;
64
      double h, s;
65
      IRasterLayer hLayer, iLayer, sLayer;
66

    
67
      final IRasterLayer rLayer = m_Parameters.getParameterValueAsRasterLayer(LAYERR);
68
      final IRasterLayer gLayer = m_Parameters.getParameterValueAsRasterLayer(LAYERG);
69
      final IRasterLayer bLayer = m_Parameters.getParameterValueAsRasterLayer(LAYERB);
70
      final int rBand = m_Parameters.getParameterValueAsInt(BANDR);
71
      final int gBand = m_Parameters.getParameterValueAsInt(BANDG);
72
      final int bBand = m_Parameters.getParameterValueAsInt(BANDB);
73

    
74
      hLayer = getNewRasterLayer(H, "H", IRasterLayer.RASTER_DATA_TYPE_FLOAT);
75
      iLayer = getNewRasterLayer(I, "I", IRasterLayer.RASTER_DATA_TYPE_BYTE);
76
      sLayer = getNewRasterLayer(S, "S", IRasterLayer.RASTER_DATA_TYPE_FLOAT);
77

    
78
      final AnalysisExtent gridExtent = hLayer.getWindowGridExtent();
79

    
80
      rLayer.setWindowExtent(gridExtent);
81
      gLayer.setWindowExtent(gridExtent);
82
      bLayer.setWindowExtent(gridExtent);
83

    
84
      iNX = gridExtent.getNX();
85
      iNY = gridExtent.getNY();
86

    
87
      for (y = 0; (y < iNY) && setProgress(y, iNY); y++) {
88
         for (x = 0; x < iNX; x++) {
89
            r = rLayer.getCellValueAsByte(x, y, rBand);
90
            g = gLayer.getCellValueAsByte(x, y, gBand);
91
            b = bLayer.getCellValueAsByte(x, y, bBand);
92

    
93
            if (rLayer.isNoDataValue(r) || gLayer.isNoDataValue(g) || bLayer.isNoDataValue(b)) {
94
               hLayer.setNoData(x, y);
95
               sLayer.setNoData(x, y);
96
               iLayer.setNoData(x, y);
97
            }
98
            else {
99
               h = (((r - g) + (r - b)) * .5) / Math.sqrt((r - g) * (r - g) + (r - b) * (g - b) + 0.0000000001);
100
               h = Math.acos(h);
101
               h = h / Math.PI * 180.;
102
               if (Double.isNaN(h)) {
103
                  h = 0;
104
               }
105
               if (b > g) {
106
                  h = 360 - h;
107
               }
108
               i = (byte) ((r + g + b) / Math.sqrt(3.));
109
               if ((r == g) && (g == b)) {
110
                  s = 0;
111
               }
112
               else {
113
                  s = (float) (1. - Math.sqrt(3) / (i) * Math.min(Math.min(r, g), b));
114
               }
115

    
116
               hLayer.setCellValue(x, y, h);
117
               iLayer.setCellValue(x, y, i);
118
               sLayer.setCellValue(x, y, s);
119
            }
120
         }
121
      }
122

    
123
      return !m_Task.isCanceled();
124

    
125
   }
126

    
127

    
128
}