[GRASS-SVN] r69458 - in sandbox/alexandris: i.hsl.rgb i.rgb.hsl

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 12 06:25:21 PDT 2016


Author: nikosa
Date: 2016-09-12 06:25:20 -0700 (Mon, 12 Sep 2016)
New Revision: 69458

Modified:
   sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c
   sandbox/alexandris/i.hsl.rgb/main.c
   sandbox/alexandris/i.rgb.hsl/i.rgb.hsl.html
   sandbox/alexandris/i.rgb.hsl/main.c
   sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c
Log:
i.[rgb|hsl].[hsl|rgb] (sandbox): various, clean, comment, format

Modified: sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c
===================================================================
--- sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c	2016-09-12 13:19:02 UTC (rev 69457)
+++ sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c	2016-09-12 13:25:20 UTC (rev 69458)
@@ -11,107 +11,108 @@
 void hsl_to_rgb(DCELL *rowbuffer[3], unsigned int columns, unsigned int max_colors)
 {
     unsigned long column;   // column indicator
-    double red;             // the red band output
-    double green;           // the green band output
-    double blue;            // the blue band output
+    double red;             // red output image
+    double green;           // green output image
+    double blue;            // blue output image
     double chroma;          // value used for determining RGB
     double intermediate;    // value used for determining RGB
     double baseline_rgb;    // value used for determining RGB
     double lightness;       // lightness value
     double saturation;      // saturation value
     double hue;             // hue
-    double hue60;           // hue
+    double hue60;           // hue in degrees
 
     /* loop over columns and appropriately set NULLs */
-    for (column = 0; column < columns; column++) {
-    if (Rast_is_d_null_value(&rowbuffer[0][column]) ||
-        Rast_is_d_null_value(&rowbuffer[1][column]) ||
-        Rast_is_d_null_value(&rowbuffer[2][column]))
+    for (column = 0; column < columns; column++)
     {
-        Rast_set_d_null_value(&rowbuffer[0][column], 1);
-        Rast_set_d_null_value(&rowbuffer[1][column], 1);
-        Rast_set_d_null_value(&rowbuffer[2][column], 1);
-        continue;
-    }
+        if (Rast_is_d_null_value(&rowbuffer[0][column]) ||
+                Rast_is_d_null_value(&rowbuffer[1][column]) ||
+                Rast_is_d_null_value(&rowbuffer[2][column]))
+        {
+            Rast_set_d_null_value(&rowbuffer[0][column], 1);
+            Rast_set_d_null_value(&rowbuffer[1][column], 1);
+            Rast_set_d_null_value(&rowbuffer[2][column], 1);
+            continue;
+        }
 
-    /* input HSL color-space properties */
-    hue = rowbuffer[0][column];
-    saturation = rowbuffer[1][column];
-    lightness = rowbuffer[2][column];
-    G_debug(2, "Input Hue, Saturation, Lightness: %f, %f, %f",
-            hue, saturation, lightness);
+        /* input HSL color-space properties */
+        hue = rowbuffer[0][column];
+        saturation = rowbuffer[1][column];
+        lightness = rowbuffer[2][column];
+        G_debug(2, "Input Hue, Saturation, Lightness: %f, %f, %f",
+                hue, saturation, lightness);
 
-    /* chroma, hue/60, intermediate `x`, baseline RGB */
-    chroma = (1.0 - fabs(2.0*lightness - 1.0)) * saturation;
-    G_debug(2, "Chroma: %f", chroma);
+        /* chroma, hue/60, intermediate `x`, baseline RGB */
+        chroma = (1.0 - fabs(2.0*lightness - 1.0)) * saturation;
+        G_debug(2, "Chroma: %f", chroma);
 
-    hue60 = hue / 60.0;
-    G_debug(2, "Hue/60: %f", hue60);
+        hue60 = hue / 60.0;
+        G_debug(2, "Hue/60: %f", hue60);
 
-    intermediate = chroma * (1.0 - fabs( fmod(hue60, 2) - 1.0));
-    G_debug(2, "Intermediate: %f", intermediate);
+        intermediate = chroma * (1.0 - fabs( fmod(hue60, 2) - 1.0));
+        G_debug(2, "Intermediate: %f", intermediate);
 
-    baseline_rgb = lightness - 0.5 * chroma;
-    G_debug(2, "Baseline RGB value: %f", baseline_rgb);
+        baseline_rgb = lightness - 0.5 * chroma;
+        G_debug(2, "Baseline RGB value: %f", baseline_rgb);
 
-    /* conversions */
-    if (hue >= 0.0 && hue < 60.0)
-    {
-        red = chroma;
-        green = intermediate;
-        blue = 0.0;
-    }
-    else if (hue >= 60.0 && hue < 120.0)
-    {
-        red = intermediate;
-        green = chroma;
-        blue = 0.0;
-    }
-    else if (hue >= 120.0 && hue < 180.0)
-    {
-        red = 0.0;
-        green = chroma;
-        blue = intermediate;
-    }
-    else if (hue >= 180.0 && hue < 240.0)
-    {
-        red = 0.0;
-        green = intermediate;
-        blue = chroma;
-    }
-    else if (hue >= 240.0 && hue < 300.0)
-    {
-        red = intermediate;
-        green = 0.0;
-        blue = chroma;
-    }
-    else if (hue >= 300.0 && hue < 360.0)
-    {
-        red = chroma;
-        green = 0.0;
-        blue = intermediate;
-    }
-    else
-    {
-        red = green = blue = 0.0;
-    }
+        /* conversions */
+        if (hue >= 0.0 && hue < 60.0)
+        {
+            red = chroma;
+            green = intermediate;
+            blue = 0.0;
+        }
+        else if (hue >= 60.0 && hue < 120.0)
+        {
+            red = intermediate;
+            green = chroma;
+            blue = 0.0;
+        }
+        else if (hue >= 120.0 && hue < 180.0)
+        {
+            red = 0.0;
+            green = chroma;
+            blue = intermediate;
+        }
+        else if (hue >= 180.0 && hue < 240.0)
+        {
+            red = 0.0;
+            green = intermediate;
+            blue = chroma;
+        }
+        else if (hue >= 240.0 && hue < 300.0)
+        {
+            red = intermediate;
+            green = 0.0;
+            blue = chroma;
+        }
+        else if (hue >= 300.0 && hue < 360.0)
+        {
+            red = chroma;
+            green = 0.0;
+            blue = intermediate;
+        }
+        else
+        {
+            red = green = blue = 0.0;
+        }
 
-    /* add baseline RGB value */
-    red += baseline_rgb;
-    green += baseline_rgb;
-    blue += baseline_rgb;
-    G_debug(2, "Red, Green, Blue: %f, %f, %f", red, green, blue);
+        /* add baseline RGB value */
+        red += baseline_rgb;
+        green += baseline_rgb;
+        blue += baseline_rgb;
+        G_debug(2, "Red, Green, Blue: %f, %f, %f", red, green, blue);
 
-    /* scale up to requested bitness */
-    red *= max_colors;
-    green *= max_colors;
-    blue *= max_colors;
-    G_debug(2, "Red, Green, Blue [scaled up to]: %f, %f, %f, [%.d]",
-            red, green, blue, max_colors);
+        /* scale up to requested bitness */
+        red *= max_colors;
+        green *= max_colors;
+        blue *= max_colors;
+        G_debug(2, "Red, Green, Blue [scaled up to]: %f, %f, %f, [%.d]",
+                red, green, blue, max_colors);
 
-    /* place output row into corresponding buffer */
-    rowbuffer[0][column] = (DCELL) red;
-    rowbuffer[1][column] = (DCELL) green;
-    rowbuffer[2][column] = (DCELL) blue;
+        /* write value into corresponding row, col */
+        rowbuffer[0][column] = (DCELL) red;
+        rowbuffer[1][column] = (DCELL) green;
+        rowbuffer[2][column] = (DCELL) blue;
     }
 }

Modified: sandbox/alexandris/i.hsl.rgb/main.c
===================================================================
--- sandbox/alexandris/i.hsl.rgb/main.c	2016-09-12 13:19:02 UTC (rev 69457)
+++ sandbox/alexandris/i.hsl.rgb/main.c	2016-09-12 13:25:20 UTC (rev 69458)
@@ -44,10 +44,10 @@
     struct Option *opt_blue;
     struct Option *opt_bits;    // bits per input image, as in bits per channel
     struct GModule *module;
+    unsigned int bits;
+    unsigned int max_colors;          // maximum number of colors
     int fd_input[3];
     int fd_output[3];
-    unsigned int bits;
-    unsigned int max_colors;          // maximum number of colors
 
     G_gisinit(argv[0]);
 

Modified: sandbox/alexandris/i.rgb.hsl/i.rgb.hsl.html
===================================================================
--- sandbox/alexandris/i.rgb.hsl/i.rgb.hsl.html	2016-09-12 13:19:02 UTC (rev 69457)
+++ sandbox/alexandris/i.rgb.hsl/i.rgb.hsl.html	2016-09-12 13:25:20 UTC (rev 69458)
@@ -1,5 +1,6 @@
 <h2 id="description">DESCRIPTION</h2>
-<p><em>i.rgb.his</em> is an image processing program that processes three input raster map layers as red, green, and blue components and produces three output raster map layers representing the hue, intensity, and saturation of the data. The output raster map layers are created by a standard red-green-blue (RGB) to hue-intensity-saturation (HIS) color transformation. Each output raster map layer is given a linear gray scale color table. The current geographic region definition and mask settings are respected.</p>
+<p><em>i.rgb.hsl</em> converts three input images red, green and blue, being components in the RGB color space, into hue, saturation and lightness dimensions of the HSL color space.</p>
+<p>Each output image is given a linear gray scale color table. The current geographic region and mask settings are respected.</p>
 <h2 id="examples">EXAMPLES</h2>
 <h2 id="bit">8-bit</h2>
 <p>The digital numbers of the 8-bit Landsat 7 bands red, green and blue, range between 0 and 255:</p>
@@ -27,11 +28,11 @@
 blue: min=42 max=255</code></pre>
 <h2 id="bit-1">11-bit</h2>
 <p>For 11-bit sensors, like QuickBird2 [0], set <code>bits=11</code>. For example:</p>
-<pre><code>i.rgb.his r=Red g=Green bl=Blue h=h s=s l=l bit_depth=11 --o</code></pre>
+<pre><code>i.rgb.hsl r=Red g=Green bl=Blue h=hue s=saturation l=lightness bits=11</code></pre>
 <h2 id="bit-2">16-bit</h2>
 <p>The Landsat 8 instruments OLI and TIRS are capable of 12-bit. Products are, however, delivered as 16-bit images (scaled to 55,000 grey levels). [0]</p>
 <p>Therefore, <em>bitness</em> needs to be set to 16, like:</p>
-<pre><code>i.rgb.his red=B4.hpf green=B3.hpf blue=B2.hpf h=h s=s l=l bit=16</code></pre>
+<pre><code>i.rgb.hsl red=B4 green=B3 blue=B2 h=hue s=saturation l=lightness bits=16</code></pre>
 <h2 id="arbitrary-lower-bit-depths">Arbitrary lower bit depths</h2>
 <p>The module allows for data of less than 8-bit. For example, data from the DMSP Operational Linescan System (OLS) nighttime lights time series, which feature 6-bit sensors [2].</p>
 <p>For example:</p>

Modified: sandbox/alexandris/i.rgb.hsl/main.c
===================================================================
--- sandbox/alexandris/i.rgb.hsl/main.c	2016-09-12 13:19:02 UTC (rev 69457)
+++ sandbox/alexandris/i.rgb.hsl/main.c	2016-09-12 13:25:20 UTC (rev 69458)
@@ -31,8 +31,10 @@
 
 int main(int argc, char **argv)
 {
-    long row;                       // long is 32-/64-bit, ~4 billion/~18 qunitillion
-    int band, rows, cols;           // input band counter, number of rows, columns
+    unsigned long row;      // long is 32-/64-bit, ~4 billion/~18 qunitillion
+    unsigned int bands;     // input band counter
+    unsigned int rows;      // input number of rows
+    unsigned int cols;      // input columns
     DCELL *rowbuffer[3];
     struct Option *opt_red;
     struct Option *opt_green;
@@ -41,13 +43,13 @@
     struct Option *opt_saturation;
     struct Option *opt_lightness;
     struct Option *opt_bits;
-    struct GModule *module;         // GRASS module for parsing arguments
-    int fd_input[3];                // input file descriptors
-    int fd_output[3];               // output file descriptors
-    int bits;                       // bitness of input raster maps
-    double max_colors;              // maximum level based on input bitness
+    struct GModule *module; // GRASS module for parsing arguments
+    int fd_input[3];        // input file descriptors
+    int fd_output[3];       // output file descriptors
+    int bits;               // bitness of input raster maps
+    double max_colors;      // maximum level based on input bitness
 
-    G_gisinit(argv[0]);             // initialize GIS engine
+    G_gisinit(argv[0]);     // initialize GIS engine
 
     /* Set description */
     module = G_define_module();
@@ -57,7 +59,7 @@
     G_add_keyword("red");
     G_add_keyword("green");
     G_add_keyword("blue");
-    G_add_keyword("HSL");           // Lightness, Luminosity or Luminance
+    G_add_keyword("HSL");   // Lightness, Luminosity or Luminance
     G_add_keyword("hue");
     G_add_keyword("saturation");
     G_add_keyword("lightness");

Modified: sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c
===================================================================
--- sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c	2016-09-12 13:19:02 UTC (rev 69457)
+++ sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c	2016-09-12 13:25:20 UTC (rev 69458)
@@ -14,7 +14,7 @@
  * maximum level of range 2^bit_depth
  */
 
-void rgb_to_hsl(DCELL *rowbuffer[3], unsigned int columns, double max_level)
+void rgb_to_hsl(DCELL *rowbuffer[3], unsigned int columns, double max_colors)
 
 {
     unsigned int column;// column indicator
@@ -29,117 +29,118 @@
     int negative_value; // flag to warn if out of range value detected
     float hue = 0.0L;   // hue
 
-for (column = 0; column < columns; column++) {
+    for (column = 0; column < columns; column++) {
 
-    if (Rast_is_d_null_value(&rowbuffer[0][column]) ||
-        Rast_is_d_null_value(&rowbuffer[1][column]) ||
-        Rast_is_d_null_value(&rowbuffer[2][column]))
-    {
-        Rast_set_d_null_value(&rowbuffer[0][column], 1);
-        Rast_set_d_null_value(&rowbuffer[1][column], 1);
-        Rast_set_d_null_value(&rowbuffer[2][column], 1);
-        continue;
-    }
+        if (Rast_is_d_null_value(&rowbuffer[0][column]) ||
+            Rast_is_d_null_value(&rowbuffer[1][column]) ||
+            Rast_is_d_null_value(&rowbuffer[2][column]))
+        {
+            Rast_set_d_null_value(&rowbuffer[0][column], 1);
+            Rast_set_d_null_value(&rowbuffer[1][column], 1);
+            Rast_set_d_null_value(&rowbuffer[2][column], 1);
+            continue;
+        }
 
-    /* scale r, g, b to [0.0,1.0] */
+        /* scale r, g, b to [0.0,1.0] */
 
-    red = rowbuffer[0][column];
-    red /= max_level;
+        red = rowbuffer[0][column];
+        red /= max_colors;
 
-    green = rowbuffer[1][column];
-    green /= max_level;
+        green = rowbuffer[1][column];
+        green /= max_colors;
 
-    blue = rowbuffer[2][column];
-    blue /= max_level;
+        blue = rowbuffer[2][column];
+        blue /= max_colors;
 
-    /* max of {r,g,b} */
+        /* max of {r,g,b} */
 
-    max = red;
-    if (green > max)
-        max = green;
+        max = red;
+        if (green > max)
+            max = green;
 
-    if (blue > max)
-        max = blue;
+        if (blue > max)
+            max = blue;
 
 
-    /* min of {r,g,b} */
+        /* min of {r,g,b} */
 
-    min = red;
-    if (green < min)
-        min = green;
+        min = red;
+        if (green < min)
+            min = green;
 
-    if (blue < min)
-        min = blue;
+        if (blue < min)
+            min = blue;
 
 
-    /* chroma and lightness */
+        /* chroma and lightness */
 
-    chroma = max - min;
-    lightness = ((max + min) / 2.0);
+        chroma = max - min;
+        lightness = ((max + min) / 2.0);
 
 
-    /* if R == G == B, then min == max, which is achromatic */
+        /* if R == G == B, then min == max, which is achromatic */
 
-    if (chroma == 0.0) {
+        if (chroma == 0.0)
+        {
+            saturation = 0.0;
+            hue = -1.0; // undefined hue, set to -1.0
+        }
 
-        saturation = 0.0;
+        /*  else chromatic */
 
-        /* undefined hue, set to -1.0 */
-        hue = -1.0;
+        else if (chroma != 0.0) {
 
-    }
+            saturation = chroma / (1.0 - fabs(2.0 * lightness - 1.0));
 
-    /*  else chromatic */
+            if (red == max)
+                hue = fmod(((green - blue) / chroma), 6.0);
 
-    else if (chroma != 0.0) {
+            else if (green == max)
+                hue = ((blue - red) / chroma) + 2.0;
 
-        saturation = chroma / (1.0 - fabs(2.0 * lightness - 1.0));
+            else if (blue == max)
+                hue = ((red - green) / chroma) + 4.0;
 
-        if (red == max)
-            hue = fmod(((green - blue) / chroma), 6.0);
+            /* convert hue to degrees */
 
-        else if (green == max)
-            hue = ((blue - red) / chroma) + 2.0;
+            hue *= 60.0;
 
-        else if (blue == max)
-            hue = ((red - green) / chroma) + 4.0;
+            /* make nonnegative */
 
-        /* convert hue to degrees */
+            if (hue < 0.0)
+                hue += 360.0;
 
-        hue *= 60.0;
+        }
 
-        /* make nonnegative */
+        G_debug(2, "Minimum and Maximum levels among r, g, b: [%f, %f]", min, max);
+        G_debug(2, "Lightness: %f", lightness);
 
-        if (hue < 0.0)
-            hue += 360.0;
+        /* HSL output values */
 
-    }
+        /* set hue = -1.0 to NULL */
 
-    G_debug(2, "Minimum and Maximum levels among r, g, b: [%f, %f]", min, max);
-    G_debug(2, "Lightness: %f", lightness);
+        if (hue == -1.0)
+            Rast_set_d_null_value(&rowbuffer[0][column], 1);
 
-    /* HSL output values */
+        else
+            rowbuffer[0][column] = (FCELL)hue;
 
-    /* set hue = -1.0 to NULL */
-    if (hue == -1.0)
-    {
-        Rast_set_d_null_value(&rowbuffer[0][column], 1);
+        /* set saturation, lightness */
+
+        if (saturation < 0)
+            negative_value = 1;
+
+        /* write out saturation and lightness */
+
+        rowbuffer[1][column] = (FCELL)saturation;
+        rowbuffer[2][column] = (FCELL)lightness;
+
+        /* for debugging purposes */
+        G_debug(3, "Output rowbuffers 0, 1, 2: %f, %f, %f\n",
+                rowbuffer[0][column], rowbuffer[1][column], rowbuffer[2][column]);
     }
-    else
-    {
-        rowbuffer[0][column] = (FCELL)hue;
-    }
 
-    /* set saturation, lightness */
-    if (saturation < 0)
-        negative_value = 1;
-    rowbuffer[1][column] = (FCELL)saturation;
-    rowbuffer[2][column] = (FCELL)lightness;
-
-    G_debug(3, "Output rowbuffers 0, 1, 2: %f, %f, %f\n",
-            rowbuffer[0][column], rowbuffer[1][column], rowbuffer[2][column]);
-  }
-// move this to main, emmit only once!
+// move the following to main, emmit only once!
 if (negative_value == 1)
     G_warning("Detected a negative Saturation value. Is the bits setting correct?");
 }



More information about the grass-commit mailing list