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

svn_grass at osgeo.org svn_grass at osgeo.org
Mon Sep 12 02:30:44 PDT 2016


Author: nikosa
Date: 2016-09-12 02:30:44 -0700 (Mon, 12 Sep 2016)
New Revision: 69452

Added:
   sandbox/alexandris/i.rgb.his/
   sandbox/alexandris/i.rgb.his/Makefile
   sandbox/alexandris/i.rgb.his/closefiles.c
   sandbox/alexandris/i.rgb.his/globals.h
   sandbox/alexandris/i.rgb.his/i.rgb.his.html
   sandbox/alexandris/i.rgb.his/main.c
   sandbox/alexandris/i.rgb.his/openfiles.c
   sandbox/alexandris/i.rgb.his/rgb2his.c
Modified:
   sandbox/alexandris/i.hsl.rgb/close_files.c
   sandbox/alexandris/i.hsl.rgb/globals.h
   sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c
   sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.html
   sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.md
   sandbox/alexandris/i.hsl.rgb/main.c
   sandbox/alexandris/i.hsl.rgb/open_files.c
   sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c
Log:
i.rgb.his (sandbox): support for user defined bits per image

Modified: sandbox/alexandris/i.hsl.rgb/close_files.c
===================================================================
--- sandbox/alexandris/i.hsl.rgb/close_files.c	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/close_files.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -7,14 +7,14 @@
    use a less than perfect way of setting the color maps for the output
    to grey scale.  */
 
-/* 
+/*
  * free allocated memory (row buffers),
  * close raster maps
  * set color table for output maps to grey scale.
  */
 
 int close_files(char *red, char *green, char *blue,
-	       int fd_output[3], DCELL * rowbuffer[3])
+                int fd_output[3], DCELL * rowbuffer[3])
 {
     int row;
     struct Colors colors;
@@ -25,8 +25,8 @@
 
     /* free allocated memory, close raster maps */
     for (row = 0; row < 3; row++) {
-        G_free(rowbuffer[row]);
-        Rast_close(fd_output[row]);
+         G_free(rowbuffer[row]);
+         Rast_close(fd_output[row]);
     }
 
     mapset = G_mapset();

Modified: sandbox/alexandris/i.hsl.rgb/globals.h
===================================================================
--- sandbox/alexandris/i.hsl.rgb/globals.h	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/globals.h	2016-09-12 09:30:44 UTC (rev 69452)
@@ -11,21 +11,26 @@
  * DCELL * rowbuf[3]
  */
 void open_files(char *, char *, char *,
-               char *, char *, char *,
-               int[3], int[3],
-               DCELL *[3]);
+                char *, char *, char *,
+                int[3], int[3],
+                DCELL *[3]);
 
-/* hsl_to_rgb.c */
-void hsl_to_rgb(DCELL *[3], int, double);
+/*
+ * hsl_to_rgb.c
+ * rowbuffer
+ * columns
+ * maximum number of colors
+ */
+void hsl_to_rgb(DCELL *[3], unsigned int, unsigned int);
 
-/* 
+/*
  * close_files.c
  * free allocated memory (row buffers),
  * close raster maps
  * set color table for output maps to grey scale.
  */
 int close_files(char *, char *, char *,
-               int[3],
-               DCELL *[3]);
+                int[3],
+                DCELL *[3]);
 
 #endif /* __GLOBALS_H__ */

Modified: sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c
===================================================================
--- sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/hsl_to_rgb.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -1,26 +1,26 @@
 /*
  * PURPOSE      Convert hue, saturation and lightness properties of the HSL
- *                color space model to red, green and blue of the RGB color space
- *               model
+ *              color space model to red, green and blue of the RGB color space
+ *              model
  */
 
 #include <grass/gis.h>
 #include "globals.h"
 #include <math.h>
 
-void hsl_to_rgb(DCELL *rowbuffer[3], int columns, double max_colors)
+void hsl_to_rgb(DCELL *rowbuffer[3], unsigned int columns, unsigned int max_colors)
 {
-    long column;		/* column indicator                          */
-    double red;			/* the red band output                       */
-    double green;		/* the green band output                     */
-    double blue;		/* the blue band output                      */
-    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                                       */
+    unsigned long column;   // column indicator
+    double red;             // the red band output
+    double green;           // the green band output
+    double blue;            // the blue band output
+    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
 
     /* loop over columns and appropriately set NULLs */
     for (column = 0; column < columns; column++) {
@@ -38,7 +38,8 @@
     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);
+    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;
@@ -95,7 +96,7 @@
         red = green = blue = 0.0;
     }
 
-    /* add baseline RGB value*/
+    /* add baseline RGB value */
     red += baseline_rgb;
     green += baseline_rgb;
     blue += baseline_rgb;
@@ -105,7 +106,8 @@
     red *= max_colors;
     green *= max_colors;
     blue *= max_colors;
-    G_debug(2, "Red, Green, Blue [scaled up to]: %f, %f, %f, [%.0f]", red, green, 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;

Modified: sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.html
===================================================================
--- sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.html	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.html	2016-09-12 09:30:44 UTC (rev 69452)
@@ -1,12 +1,13 @@
 <h2 id="description">DESCRIPTION</h2>
 <p><em>i.hsl.rgb</em> converts hue, saturation and lightness input images, being dimensions of the HSL color space, into red, green and blue values in the RGB color space. 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>
+<p>Read examples in the manual of <a href="i.rgb.hsl.html">i.rgb.hsl</a></p>
 <h2 id="notes">NOTES</h2>
 <ul>
-<li><p>The <em>bits</em> option refers to the bit (or color) depth of the input images, as in bits per color channel. Not to be confused with bits-per-pixel, which refers to the sum of all three color channels. For example, an 8-bit image feaures 256 number of colors. Expecting all input images to be of the same color depth, in example 8 bits, then, an RGB composite would feature 24 bits per pixel.</p></li>
+<li><p>The <em>bits</em> option refers to the bit (or color) depth of the input images, as in <em>bits per color channel</em>. Not to confuse with <em>bits-per-pixel</em>, which refers to the sum of all three color channels. For example, an 8-bit image feaures 256 number of colors. Expecting all input images to be of the same color depth, in this case 8 bits, then, an RGB composite would feature 24 bits per pixel.</p></li>
 <li><p>It is possible to process three images with <em>i.hsl.rgb</em> and then recover the original images with <em>i.rgb.hsl</em>.</p></li>
 </ul>
 <h2 id="see-also">SEE ALSO</h2>
-<p><em><a href="i.rgb.his.html">i.rgb.his</a>, <a href="i.his.rgb.html">i.rgb.his</a>, <a href="r.colors.html">r.colors</a></em></p>
+<p><em><a href="i.rgb.hsl.html">i.rgb.hsl</a>, <a href="i.rgb.his.html">i.rgb.his</a>, <a href="i.his.rgb.html">i.rgb.his</a>, <a href="r.colors.html">r.colors</a></em></p>
 <h2 id="author">AUTHOR</h2>
 <p>Nikos Alexandris</p>
-<p><em>Last changed: $Date: 2016-09-09 13:10:00 +0100 (Fri, 9 Sep 2016) $</em></p>

Modified: sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.md
===================================================================
--- sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.md	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/i.hsl.rgb.md	2016-09-12 09:30:44 UTC (rev 69452)
@@ -7,14 +7,19 @@
 color table. The current geographic region and mask settings are
 respected.
 
+EXAMPLES
+--------
+
+Read examples in the manual of [i.rgb.hsl](i.rgb.hsl.html)
+
 NOTES
 -----
 
-- The *bits* option refers to the bit (or color) depth of the input
-images, as in bits per color channel. Not to be confused with bits-per-pixel,
+- The <em>bits</em> option refers to the bit (or color) depth of the input
+images, as in *bits per color channel*.  Not to confuse with *bits-per-pixel*,
 which refers to the sum of all three color channels.  For example, an 8-bit
 image feaures 256 number of colors.  Expecting all input images to be of the
-same color depth, in example 8 bits, then, an RGB composite would feature 24
+same color depth, in this case 8 bits, then, an RGB composite would feature 24
 bits per pixel.
 
 - It is possible to process three images with *i.hsl.rgb* and then recover
@@ -30,5 +35,3 @@
 ------
 
 Nikos Alexandris
-
-*Last changed: \$Date: 2016-09-09 13:10:00 +0100 (Fri, 9 Sep 2016) \$*

Modified: sandbox/alexandris/i.hsl.rgb/main.c
===================================================================
--- sandbox/alexandris/i.hsl.rgb/main.c	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/main.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -33,8 +33,8 @@
 int main(int argc, char **argv)
 {
 
-    long row;
-    int rows, cols;
+    unsigned long row;
+    unsigned int rows, cols;
     DCELL *rowbuffer[3];
     struct Option *opt_hue;
     struct Option *opt_saturation;
@@ -42,12 +42,12 @@
     struct Option *opt_red;
     struct Option *opt_green;
     struct Option *opt_blue;
-    struct Option *opt_bits;    /* bits per input image, as in bits per channel */
+    struct Option *opt_bits;    // bits per input image, as in bits per channel
     struct GModule *module;
     int fd_input[3];
     int fd_output[3];
-    int bits;
-    double max_colors;          /* maximum number of colors */
+    unsigned int bits;
+    unsigned int max_colors;          // maximum number of colors
 
     G_gisinit(argv[0]);
 
@@ -106,8 +106,8 @@
         G_fatal_error(_("Invalid bit depth definition!"));
 
     /* open half ended range for maximum number of colors */
-    max_colors = pow(2, bits) - 1.0;
-    G_debug(1, "%d-bit data ranging in [0,%.0f)", bits, max_colors);
+    max_colors = pow(2, bits) - 1;
+    G_debug(1, "%d-bit data ranging in [0,%.0d)", bits, max_colors);
 
     /* get image dimensions */
     rows = Rast_window_rows();
@@ -115,24 +115,23 @@
 
     /* open input and output files */
     open_files(opt_hue->answer,  opt_saturation->answer, opt_lightness->answer,
-              opt_red->answer, opt_green->answer, opt_blue->answer,
-              fd_input, fd_output, rowbuffer);
+               opt_red->answer, opt_green->answer, opt_blue->answer,
+               fd_input, fd_output, rowbuffer);
 
     /* loop over hue, saturation and lightness color space properties */
     for (row = 0; row < rows; row++) {
-        int property;
+         int property;
 
-
         /* read in row from each cell map */
         for (property = 0; property < 3; property++)
-            Rast_get_d_row(fd_input[property], rowbuffer[property], row);
+             Rast_get_d_row(fd_input[property], rowbuffer[property], row);
 
             /* process row of image */
             hsl_to_rgb(rowbuffer, cols, max_colors);
 
         /* write out the new row for each cell map */
         for (property = 0; property < 3; property++)
-            Rast_put_row(fd_output[property], rowbuffer[property], DCELL_TYPE);
+             Rast_put_row(fd_output[property], rowbuffer[property], DCELL_TYPE);
     }
 
     /* progress */
@@ -140,7 +139,7 @@
 
     /* close output files */
     close_files(opt_red->answer, opt_green->answer, opt_blue->answer,
-               fd_output, rowbuffer);
+                fd_output, rowbuffer);
 
     exit(EXIT_SUCCESS);
 }

Modified: sandbox/alexandris/i.hsl.rgb/open_files.c
===================================================================
--- sandbox/alexandris/i.hsl.rgb/open_files.c	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.hsl.rgb/open_files.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -11,10 +11,10 @@
  * (input/output?) rowbuffers
  */
 
-void open_files(char *hue, char *saturation, char *lightness, 
-               char *red, char *green, char *blue,
-               int fd_input[3], int fd_output[3],
-               DCELL * rowbuffer[3])
+void open_files(char *hue, char *saturation, char *lightness,
+                char *red, char *green, char *blue,
+                int fd_input[3], int fd_output[3],
+                DCELL * rowbuffer[3])
 {
     /* open output files */
     fd_output[0] = Rast_open_fp_new(red);

Added: sandbox/alexandris/i.rgb.his/Makefile
===================================================================
--- sandbox/alexandris/i.rgb.his/Makefile	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/Makefile	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = i.rgb.his
+
+LIBES = $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd

Added: sandbox/alexandris/i.rgb.his/closefiles.c
===================================================================
--- sandbox/alexandris/i.rgb.his/closefiles.c	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/closefiles.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include "globals.h"
+
+/* This routine closes up the cell maps, frees up the row buffers and
+   uses a less than perfect way of setting the color maps for the output
+   to grey scale.  */
+
+int closefiles(char *hue, char *intensity, char *saturation,
+	       int fd_output[3], DCELL * rowbuffer[3])
+{
+    unsigned int property;  // color properties: hue, intensity, saturation
+    struct Colors colors;
+    struct FPRange range;
+    struct History history;
+    DCELL min, max;
+    const char *mapset;
+
+    for (property = 0; property < 3; property++) {
+        Rast_close(fd_output[property]);
+        G_free(rowbuffer[property]);
+    }
+
+    mapset = G_mapset();
+
+    /* write colors */
+    Rast_read_fp_range(hue, mapset, &range);
+    Rast_get_fp_range_min_max(&range, &min, &max);
+    Rast_make_grey_scale_colors(&colors, min, max); // set to [0,max] instead ?
+    Rast_write_colors(hue, mapset, &colors);
+
+    Rast_read_fp_range(intensity, mapset, &range);
+    Rast_get_fp_range_min_max(&range, &min, &max);
+    Rast_make_grey_scale_colors(&colors, min, max);
+    Rast_write_colors(intensity, mapset, &colors);
+
+    Rast_read_fp_range(saturation, mapset, &range);
+    Rast_get_fp_range_min_max(&range, &min, &max);
+    Rast_make_grey_scale_colors(&colors, min, max);
+    Rast_write_colors(saturation, mapset, &colors);
+
+    /* write metadata */
+    Rast_short_history(hue, "raster", &history);
+    Rast_command_history(&history);
+    Rast_write_history(hue, &history);
+    Rast_put_cell_title(hue, "Image hue");
+
+    Rast_short_history(intensity, "raster", &history);
+    Rast_command_history(&history);
+    Rast_write_history(intensity, &history);
+    Rast_put_cell_title(intensity, "Image intensity");
+
+    Rast_short_history(saturation, "raster", &history);
+    Rast_command_history(&history);
+    Rast_write_history(saturation, &history);
+    Rast_put_cell_title(saturation, "Image saturation");
+
+    return 0;
+}

Added: sandbox/alexandris/i.rgb.his/globals.h
===================================================================
--- sandbox/alexandris/i.rgb.his/globals.h	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/globals.h	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,16 @@
+#ifndef __GLOBALS_H__
+#define __GLOBALS_H__
+
+#include <grass/raster.h>
+
+/* closefiles.c */
+int closefiles(char *, char *, char *, int[3], DCELL *[3]);
+
+/* openfiles.c */
+void openfiles(char *, char *, char *, char *, char *, char *, int[3], int[3],
+        DCELL *[3]);
+
+/* rgb2his.c */
+void rgb2his(DCELL *[3], unsigned int, double);
+
+#endif /* __GLOBALS_H__ */

Added: sandbox/alexandris/i.rgb.his/i.rgb.his.html
===================================================================
--- sandbox/alexandris/i.rgb.his/i.rgb.his.html	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/i.rgb.his.html	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,31 @@
+<h2>DESCRIPTION</h2>
+
+<em>i.rgb.his</em> is an image processing program that
+processes three input images as red, green, and
+blue components and produces three output images
+representing the hue, intensity, and saturation color properties.
+
+The output images base upon a standard red-green-blue (rgb) to
+hue-intensity-saturation (his) color space transformation.  Hue ranges in degrees [0,
+360] while intensity and saturation range in [0, 1].
+
+<h2>NOTES</h2>
+
+The module supports various bits per image (as in bits per color channel, bitness ranging in [2,16]).
+
+Each output raster map layer is given a linear gray scale color table.  The current geographic region definition and mask settings are
+respected.
+
+<h2>SEE ALSO</h2>
+
+<em><a href="i.his.rgb.html">i.his.rgb</a></em>
+
+<h2>AUTHOR</h2>
+
+David Satnik, GIS Laboratory, 
+Central Washington University, 
+<br>
+with acknowledgements to Ali Vali, Space Research
+Center, for the core routine. 
+
+<p><i>Last changed: $Date: 2011-11-08 23:24:20 +0200 (Tue, 08 Nov 2011) $</i>

Added: sandbox/alexandris/i.rgb.his/main.c
===================================================================
--- sandbox/alexandris/i.rgb.his/main.c	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/main.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,142 @@
+
+/****************************************************************************
+ *
+ * MODULE:      i.rgb.his
+ *
+ * AUTHOR(S):   David Satnik, GIS Laboratory, Central Washington University
+ *              Nikos Alexandris, support various bit depths
+ *
+ *              with acknowledgements to Ali Vali,
+ *              Univ. of Texas Space Research Center, for the core routine.
+ *
+ * PURPOSE:     Red-green-blue (rgb) to hue-intensity-saturation (his)
+ *              raster map color transformation function
+ *
+ * COPYRIGHT:   (C) 2007-2016 by the GRASS Development Team
+ *
+ *              This program is free software under the GNU General Public
+ *              License (>=v2). Read the file COPYING that comes with GRASS
+ *              for details.
+ *
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+#include "globals.h"
+
+int main(int argc, char **argv)
+{
+    unsigned int band;
+    unsigned long row;
+    unsigned int rows;
+    unsigned int cols;
+    DCELL *rowbuffer[3];
+    struct Option *opt_red;
+    struct Option *opt_green;
+    struct Option *opt_blue;
+    struct Option *opt_hue;
+    struct Option *opt_intensity;
+    struct Option *opt_saturation;
+    struct Option *opt_bits;
+    struct GModule *module;
+    int fd_input[3];
+    int fd_output[3];
+    int bits;   // bitness of input raster maps
+    double max_colors;  // maximum level based on input bitness
+
+    G_gisinit(argv[0]);  // initialize GIS engine
+
+    /* Set description */
+    module = G_define_module();
+    G_add_keyword(_("imagery"));
+    G_add_keyword(_("color transformation"));
+    G_add_keyword("RGB");
+    G_add_keyword("HIS");
+    G_add_keyword("IHS");
+    module->description =
+        _("Transforms raster maps from RGB (Red-Green-Blue) color space to "
+                "HIS (Hue-Intensity-Saturation) color space.");
+
+    /* Define the different options */
+    opt_red = G_define_standard_option(G_OPT_R_INPUT);
+    opt_red->key = "red";
+    opt_red->description = _("Name of input raster map (red)");
+
+    opt_green = G_define_standard_option(G_OPT_R_INPUT);
+    opt_green->key = "green";
+    opt_green->description = _("Name of input raster map (green)");
+
+    opt_blue = G_define_standard_option(G_OPT_R_INPUT);
+    opt_blue->key = "blue";
+    opt_blue->description = _("Name of input raster map (blue)");
+
+    opt_hue = G_define_standard_option(G_OPT_R_OUTPUT);
+    opt_hue->key = "hue";
+    opt_hue->description = _("Name for output raster map (hue)");
+
+    opt_intensity = G_define_standard_option(G_OPT_R_OUTPUT);
+    opt_intensity->key = "intensity";
+    opt_intensity->description = _("Name for output raster map (intensity)");
+
+    opt_saturation = G_define_standard_option(G_OPT_R_OUTPUT);
+    opt_saturation->key = "saturation";
+    opt_saturation->description = _("Name for output raster map (saturation)");
+
+    opt_bits = G_define_option();
+    opt_bits->key = "bits";
+    opt_bits->type = TYPE_INTEGER;
+    opt_bits->required = NO;
+    opt_bits->answer = "8";
+    opt_bits->options = "2-16";
+    opt_bits->description = _("Bits per input image");
+
+    if (G_parser(argc, argv))
+        exit(EXIT_FAILURE);
+
+    /* bit depth, should be > 0 */
+    bits = atoi(opt_bits->answer);
+    if (bits <= 0)
+        G_fatal_error(_("Invalid bit depth definition!"));
+
+    /* open half ended range for maximum level */
+    max_colors = pow(2, bits) - 1.0;
+    G_debug(1, "%d-bit data ranging in [0,%.0f)", bits, max_colors);
+
+    /* get dimension of the image */
+    rows = Rast_window_rows();
+    cols = Rast_window_cols();
+
+    openfiles(opt_red->answer, opt_green->answer, opt_blue->answer,
+            opt_hue->answer, opt_intensity->answer, opt_saturation->answer,
+            fd_input, fd_output, rowbuffer);
+
+    for (row = 0; row < rows; row++) {
+
+        /* read in a row from each cell map */
+        G_percent(row, rows, 2);
+
+        for (band = 0; band < 3; band++)
+            Rast_get_d_row(fd_input[band], rowbuffer[band], row);
+
+        /* process this row of the map */
+        rgb2his(rowbuffer, cols, max_colors);
+
+        /* write out the new row for each cell map */
+        for (band = 0; band < 3; band++)
+            Rast_put_row(fd_output[band], rowbuffer[band], DCELL_TYPE);
+    }
+
+    /* progress */
+    G_percent(row, rows, 2);
+
+    /* close output files */
+    closefiles(opt_hue->answer, opt_intensity->answer, opt_saturation->answer,
+            fd_output, rowbuffer);
+
+    exit(EXIT_SUCCESS);
+}

Added: sandbox/alexandris/i.rgb.his/openfiles.c
===================================================================
--- sandbox/alexandris/i.rgb.his/openfiles.c	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/openfiles.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,26 @@
+#include <stdio.h>
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+#include "globals.h"
+
+void openfiles(char *red, char *green, char *blue,
+               char *hue, char *intensity, char *saturation,
+               int fd_input[3], int fd_output[3],
+               DCELL *rowbuffer[3])
+{
+    /* input file descriptors */
+    fd_input[0] = Rast_open_old(red, "");
+    fd_input[1] = Rast_open_old(green, "");
+    fd_input[2] = Rast_open_old(blue, "");
+
+    /* open output files */
+    fd_output[0] = Rast_open_fp_new(hue);
+    fd_output[1] = Rast_open_fp_new(intensity);
+    fd_output[2] = Rast_open_fp_new(saturation);
+
+    /* allocate the cell row buffer */
+    rowbuffer[0] = Rast_allocate_d_buf();
+    rowbuffer[1] = Rast_allocate_d_buf();
+    rowbuffer[2] = Rast_allocate_d_buf();
+}

Added: sandbox/alexandris/i.rgb.his/rgb2his.c
===================================================================
--- sandbox/alexandris/i.rgb.his/rgb2his.c	                        (rev 0)
+++ sandbox/alexandris/i.rgb.his/rgb2his.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -0,0 +1,170 @@
+
+/******************************************************************************
+
+NAME:       RGB2HIS
+ 
+PURPOSE     To process red,green,blue bands to hue,intensity,saturation.
+ 
+ALGORITHM:
+            Get red, green, blue from input buffer
+            Create the HIS bands
+            Write to output buffer
+ 
+ASSUMPTION:
+            The input images are read to the input buffer.
+
+NOTE:       For GRASS one row from each cell map is passed in and each cell in
+            each band is processed and written out.   CWU GIS Lab: DBS 8/90
+
+******************************************************************************/
+
+#include <grass/gis.h>
+#include "globals.h"
+
+void rgb2his(DCELL * rowbuffer[3], unsigned int columns, double max_colors)
+{
+    int column;     // column counter
+    double scaled_red;     // red input band, to be scaled to [0,1]
+    double scaled_green;   // green input band, likewise
+    double scaled_blue;    // blue input output, likewise
+    double red;     // red value used for computing hue
+    double green;   // green value, likewise
+    double blue;    // blue value, likewise
+    double min;     // minimum among red, green, blue
+    double max;     // maximum among red, green, blue
+    double chroma;  // maximum - minimum
+    double hue = 0.0L;
+    double saturation;
+    double intensity;
+
+    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;
+        }
+
+        /* scale red, green, blue to [0.0,1.0] */
+
+        scaled_red = rowbuffer[0][column];
+        scaled_red /= max_colors;
+
+        scaled_green = rowbuffer[1][column];
+        scaled_green /= max_colors;
+
+        scaled_blue = rowbuffer[2][column];
+        scaled_blue /= max_colors;
+
+        /* max of {r,g,b} */
+
+        max = scaled_red;
+        if (scaled_green > max)
+            max = scaled_green;
+
+        if (scaled_blue > max)
+            max = scaled_blue;
+
+        /* min of {r,g,b} */
+
+        min = scaled_red;
+        if (scaled_green < min)
+            min = scaled_green;
+
+        if (scaled_blue < min)
+            min = scaled_blue;
+
+        /* chroma and intensity */
+
+        chroma = max - min;
+        intensity = ((max + min) / 2.0);
+
+        /* if R == G == B, then min == max, which is achromatic */
+
+        if (chroma == 0.0)
+        {
+            saturation = 0.0;
+            hue = -1.0; // undefined hue, set to -1.0
+        }
+
+        /*  else chromatic */
+
+        else if (chroma != 0.0) {
+
+            if (intensity <= 0.5)
+            {
+                saturation = chroma / (max + min);
+                G_debug(2, "Saturation (for I <= 0.5): %f", saturation);
+            }
+
+            else if (intensity > 0.5)
+            {
+                saturation = chroma / (2 - max - min);
+                G_debug(2, "Saturation (for I > 0.5): %f", saturation);
+            }
+
+            /* set red, green, blue for computing hue */
+
+            red = (max - scaled_red) / chroma;
+            green = (max - scaled_green) / chroma;
+            blue = (max - scaled_blue) / chroma;
+
+            /* resulting color between yelmin and magenta */
+
+            if (scaled_red == max)
+            {
+                hue = blue - green;
+                G_debug(2, "Hue (blue - green): %f", hue);
+            }
+
+            /* resulting color between cyan and yelmin */
+
+            else if (scaled_green == max)
+            {
+                hue = 2 + red - blue;
+                G_debug(2, "Hue (red - blue): %f", hue);
+            }
+            /* resulting color between magenta and cyan */
+
+            else if (scaled_blue == max)
+            {
+                hue = 4 + green - red;
+                G_debug(2, "Hue (green - red): %f", hue);
+            }
+            /* convert to degrees */
+
+            hue *= 60.0;
+
+            /* make nonnegative */
+
+            if (hue < 0.0)
+                hue += 360.0;
+
+
+        }
+
+        G_debug(2, "Minimum and Maximum levels among r, g, b: [%f, %f]", min, max);
+        G_debug(2, "HIS: %f, %f, %f", hue, intensity, saturation);
+
+        /* HIS output values */
+
+        /* set hue = -1.0 to NULL */
+
+        if (hue == -1.0)
+            Rast_set_d_null_value(&rowbuffer[0][column], 1);
+
+        else
+            rowbuffer[0][column] = (FCELL)hue;
+
+        rowbuffer[1][column] = (FCELL)intensity;
+        rowbuffer[2][column] = (FCELL)saturation;
+
+        /* for debugging purposes */
+        G_debug(3, "Output rowbuffers 0, 1, 2: %f, %f, %f\n",
+                rowbuffer[0][column], rowbuffer[1][column], rowbuffer[2][column]);
+    }
+}

Modified: sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c
===================================================================
--- sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c	2016-09-12 03:06:33 UTC (rev 69451)
+++ sandbox/alexandris/i.rgb.hsl/rgb_to_hsl.c	2016-09-12 09:30:44 UTC (rev 69452)
@@ -26,6 +26,7 @@
     float chroma;       // chrome, intermediate value
     float lightness;    // lightness
     float saturation;   // saturation
+    int negative_value; // flag to warn if out of range value detected
     float hue = 0.0L;   // hue
 
 for (column = 0; column < columns; column++) {
@@ -130,10 +131,15 @@
     }
 
     /* 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!
+if (negative_value == 1)
+    G_warning("Detected a negative Saturation value. Is the bits setting correct?");
 }



More information about the grass-commit mailing list