[GRASS-SVN] r69457 - in sandbox/alexandris: . i.his.rgb i.rgb.his
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Sep 12 06:19:02 PDT 2016
Author: nikosa
Date: 2016-09-12 06:19:02 -0700 (Mon, 12 Sep 2016)
New Revision: 69457
Added:
sandbox/alexandris/i.his.rgb/
sandbox/alexandris/i.his.rgb/Makefile
sandbox/alexandris/i.his.rgb/closefiles.c
sandbox/alexandris/i.his.rgb/globals.h
sandbox/alexandris/i.his.rgb/his2rgb.c
sandbox/alexandris/i.his.rgb/i.his.rgb.html
sandbox/alexandris/i.his.rgb/main.c
sandbox/alexandris/i.his.rgb/openfiles.c
Modified:
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/rgb2his.c
Log:
i.his.rgb (sandbox): added support for user defined bits per image, plus clean, comment, format for i.rgb.his
Added: sandbox/alexandris/i.his.rgb/Makefile
===================================================================
--- sandbox/alexandris/i.his.rgb/Makefile (rev 0)
+++ sandbox/alexandris/i.his.rgb/Makefile 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,10 @@
+MODULE_TOPDIR = ../..
+
+PGM = i.his.rgb
+
+LIBES = $(RASTERLIB) $(GISLIB)
+DEPENDENCIES = $(RASTERDEP) $(GISDEP)
+
+include $(MODULE_TOPDIR)/include/Make/Module.make
+
+default: cmd
Added: sandbox/alexandris/i.his.rgb/closefiles.c
===================================================================
--- sandbox/alexandris/i.his.rgb/closefiles.c (rev 0)
+++ sandbox/alexandris/i.his.rgb/closefiles.c 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,63 @@
+#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
+ use a less than perfect way of setting the color maps for the output
+ to grey scale. */
+
+int closefiles(char *red, char *green, char *blue,
+ int fd_output[3],
+ DCELL * rowbuf[3])
+
+{
+ unsigned int color;
+ struct Colors colors;
+ struct FPRange range;
+ struct History history;
+ DCELL min, max; // set to [0, max_colors] instead of [min, max] ?
+ const char *mapset;
+
+ for (color = 0; color < 3; color++)
+ {
+ Rast_close(fd_output[color]);
+ G_free(rowbuf[color]);
+ }
+
+ mapset = G_mapset();
+
+ /* write colors */
+ Rast_read_fp_range(red, mapset, &range);
+ Rast_get_fp_range_min_max(&range, &min, &max);
+ Rast_make_grey_scale_colors(&colors, min, max);
+ Rast_write_colors(red, mapset, &colors);
+
+ Rast_read_fp_range(green, mapset, &range);
+ Rast_get_fp_range_min_max(&range, &min, &max);
+ Rast_make_grey_scale_colors(&colors, min, max);
+ Rast_write_colors(green, mapset, &colors);
+
+ Rast_read_fp_range(blue, mapset, &range);
+ Rast_get_fp_range_min_max(&range, &min, &max);
+ Rast_make_grey_scale_colors(&colors, min, max);
+ Rast_write_colors(blue, mapset, &colors);
+
+ /* write metadata */
+ Rast_short_history(red, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(red, &history);
+ Rast_put_cell_title(red, "Image red");
+
+ Rast_short_history(green, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(green, &history);
+ Rast_put_cell_title(green, "Image green");
+
+ Rast_short_history(blue, "raster", &history);
+ Rast_command_history(&history);
+ Rast_write_history(blue, &history);
+ Rast_put_cell_title(blue, "Image blue");
+
+ return 0;
+}
Added: sandbox/alexandris/i.his.rgb/globals.h
===================================================================
--- sandbox/alexandris/i.his.rgb/globals.h (rev 0)
+++ sandbox/alexandris/i.his.rgb/globals.h 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,32 @@
+#ifndef __GLOBALS_H__
+#define __GLOBALS_H__
+
+#include <grass/raster.h>
+
+/*
+ * openfiles.c
+ * hue, saturation, intensity,
+ * red, green, blue,
+ * int fd_input[3], int fd_output[3],
+ * DCELL * rowbuf[3]
+ */
+void openfiles(char *, char *, char *, char *, char *, char *, int[3], int[3],
+ DCELL *[3]);
+
+/*
+ * his2rgb.c
+ * rowbuffer
+ * columns
+ * maximum number of colors
+ */
+void his2rgb(DCELL *[3], unsigned int, unsigned int);
+
+/*
+ * closefiles.c
+ * red, green, blue
+ * output file descriptors
+ * output rowbuffers
+ */
+int closefiles(char *, char *, char *, int[3], DCELL *[3]);
+
+#endif /* __GLOBALS_H__ */
Added: sandbox/alexandris/i.his.rgb/his2rgb.c
===================================================================
--- sandbox/alexandris/i.his.rgb/his2rgb.c (rev 0)
+++ sandbox/alexandris/i.his.rgb/his2rgb.c 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,165 @@
+
+/******************************************************************************
+NAME: HIS to RGB
+
+PURPOSE: To proces hue, intensity, saturation bands to red, green, blue.
+
+ALGORITHM: Get hue, intensity, saturation from input buffer
+ Create the RGB 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 his2rgb(DCELL *rowbuffer[3], unsigned int columns, unsigned int max_colors)
+{
+ long column; // column indicator
+ double red; // red output image
+ double green; // green output image
+ double blue; // blue output image
+ double scaled_red; // ranging in [0,1], used to compute hue
+ double scaled_green;// likewise
+ double scaled_blue; // likewise
+ double m1; // value used to determine RGB
+ double m2; // value used to determine RGB
+ double scaled_intensity; // input image ranging in [0,1]
+ double scaled_saturation; // input image ranging in [0,1]
+ double hue; // input image ranging in [0, 360]
+ double savehue; // save the hue for future processing
+
+ /* 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]))
+ {
+ 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;
+ }
+
+ /* initialize zero red, green, blue */
+ red = green = blue = 0.0;
+
+ /* input intensity (from i.rgb.his) ranges in [0,1] */
+ scaled_intensity = rowbuffer[1][column];
+
+ /* input saturation (from i.rgb.his) ranges in [0,1] */
+ scaled_saturation = rowbuffer[2][column];
+
+ m2 = 0.0;
+
+ if (scaled_intensity <= 0.50)
+ m2 = scaled_intensity * (1.0 + scaled_saturation);
+
+ else if (scaled_intensity > 0.50)
+ m2 = scaled_intensity + scaled_saturation - (scaled_intensity * scaled_saturation);
+
+ m1 = 2.0 * scaled_intensity - m2;
+
+ /* input hue (from i.rgb.his) ranges in [0, 360] */
+ hue = rowbuffer[0][column];
+
+ if (scaled_saturation == 0.0) {
+ if (hue == -1.0) {
+ red = scaled_intensity;
+ green = scaled_intensity;
+ blue = scaled_intensity;
+ }
+ }
+ else {
+ /* calculate the red band */
+ savehue = hue + 120.0;
+ if (savehue > 360.0)
+ savehue -= 360.0;
+ if (savehue < 0.0)
+ savehue += 360.0;
+ if (savehue < 60.0)
+ red = m1 + (m2 - m1) * savehue / 60.0;
+ else if (savehue < 180.0)
+ red = m2;
+ else if (savehue < 240.0)
+ red = m1 + (m2 - m1) * (240.0 - savehue) / 60.0;
+ else
+ red = m1;
+
+ /* calculate the green band */
+ savehue = hue;
+ if (savehue > 360.0)
+ savehue -= 360.0;
+ if (savehue < 0.0)
+ savehue += 360.0;
+ if (savehue < 60.0)
+ green = m1 + (m2 - m1) * savehue / 60.0;
+ else if (savehue < 180.0)
+ green = m2;
+ else if (savehue < 240.0)
+ green = m1 + (m2 - m1) * (240.0 - savehue) / 60.0;
+ else
+ green = m1;
+
+ /* calculate the blue band */
+ savehue = hue - 120.0;
+ if (savehue > 360.0)
+ savehue -= 360.0;
+ if (savehue < 0.0)
+ savehue += 360.0;
+ if (savehue < 60.0)
+ blue = m1 + (m2 - m1) * savehue / 60.0;
+ else if (savehue < 180.0)
+ blue = m2;
+ else if (savehue < 240.0)
+ blue = m1 + (m2 - m1) * (240.0 - savehue) / 60.0;
+ else
+ blue = m1;
+ }
+
+ scaled_red = red * max_colors;
+ scaled_green = green * max_colors;
+ scaled_blue = blue * max_colors;
+
+ if (scaled_red > max_colors)
+ red = max_colors;
+
+ else
+ red = scaled_red;
+
+ if (scaled_green > max_colors)
+ green = max_colors;
+
+ else
+ green = scaled_green;
+
+ if (scaled_blue > max_colors)
+ blue = max_colors;
+
+ else
+ blue = scaled_blue;
+
+ if (red < 0.0)
+ red = 0.0;
+
+ if (green < 0.0)
+ green = 0.0;
+
+ if (blue < 0.0)
+ blue = 0.0;
+
+ G_debug(2, "Red, Green, Blue [scaled up to]: %f, %f, %f, [%.d]",
+ red, green, blue, max_colors);
+
+ /* write value into corresponding row, col */
+ rowbuffer[0][column] = red;
+ rowbuffer[1][column] = green;
+ rowbuffer[2][column] = blue;
+ }
+}
Added: sandbox/alexandris/i.his.rgb/i.his.rgb.html
===================================================================
--- sandbox/alexandris/i.his.rgb/i.his.rgb.html (rev 0)
+++ sandbox/alexandris/i.his.rgb/i.his.rgb.html 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,28 @@
+<h2>DESCRIPTION</h2>
+
+<p><em>i.his.rgb</em> is an image processing program that processes three input
+images as hue, intensity and saturation components and produces three output
+images representing the red, green and blue colors.</p>
+
+<p>Hue values are expected to be degrees, ranging in [0, 360] while intensity and
+saturation should range within [0,1].
+
+The output images base upon a standard hue-intensity-saturation (his) to red-green-blue
+(rgb) color space transformation.</p>
+
+<p>Each output raster map layer is given a linear gray scale color table. The
+current geographic region definition and mask settings are respected.</p>
+
+<h2>SEE ALSO</h2>
+
+<em>
+<a href="i.rgb.his.html">i.rgb.his</a>,
+<a href="r.colors.html">r.colors</a>
+</em>
+
+<h2>AUTHOR</h2>
+
+David Satnik, GIS Laboratory, Central Washington University
+<br>
+with acknowledgements to Ali Vali, Univ. of Texas, Space Research Center, for the core routine.
+Nikos Alexandris
Added: sandbox/alexandris/i.his.rgb/main.c
===================================================================
--- sandbox/alexandris/i.his.rgb/main.c (rev 0)
+++ sandbox/alexandris/i.his.rgb/main.c 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,137 @@
+
+/****************************************************************************
+ *
+ * MODULE: i.his.rgb
+ *
+ * AUTHOR(S): David Satnik, GIS Laboratory, Central Washington University
+ * with acknowledgements to Ali Vali,
+ * Univ. of Texas Space Research Center, for the core routine.
+ *
+ * PURPOSE: Hue-intensity-saturation (his) to red-green-blue (rgb)
+ * raster map color transformation function.
+ *
+ * COPYRIGHT: (C) 2007-2008 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 <stdlib.h>
+#include <stdio.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)
+{
+
+ long i;
+ int rows, cols;
+ DCELL *rowbuffer[3];
+ struct Option *opt_hue;
+ struct Option *opt_int;
+ struct Option *opt_sat;
+ 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 GModule *module;
+ unsigned int bits;
+ unsigned int max_colors; // maximum number of colors
+ int fd_output[3];
+ int fd_input[3];
+
+ G_gisinit(argv[0]);
+
+ /* 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 HIS (Hue-Intensity-Saturation) color space to "
+ "RGB (Red-Green-Blue) color space.");
+
+ /* Define the different options */
+ opt_hue = G_define_standard_option(G_OPT_R_INPUT);
+ opt_hue->key = "hue";
+ opt_hue->description = _("Name of input raster map (hue)");
+
+ opt_int = G_define_standard_option(G_OPT_R_INPUT);
+ opt_int->key = "intensity";
+ opt_int->description = _("Name of input raster map (intensity)");
+
+ opt_sat = G_define_standard_option(G_OPT_R_INPUT);
+ opt_sat->key = "saturation";
+ opt_sat->description = _("Name of input raster map (saturation)");
+
+ opt_red = G_define_standard_option(G_OPT_R_OUTPUT);
+ opt_red->key = "red";
+ opt_red->description = _("Name for output raster map (red)");
+
+ opt_green = G_define_standard_option(G_OPT_R_OUTPUT);
+ opt_green->key = "green";
+ opt_green->description = _("Name for output raster map (green)");
+
+ opt_blue = G_define_standard_option(G_OPT_R_OUTPUT);
+ opt_blue->key = "blue";
+ opt_blue->description = _("Name for output raster map (blue)");
+
+ 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 output image");
+
+ if (G_parser(argc, argv))
+ exit(EXIT_FAILURE);
+
+ /* bits per image, should be > 0 */
+ bits = atoi(opt_bits->answer);
+ if (bits <= 0)
+ G_fatal_error(_("Invalid bit depth definition!"));
+
+ /* open half ended range for maximum number of colors */
+ max_colors = pow(2, bits) - 1;
+ G_debug(1, "%d-bit data ranging in [0,%.0d)", bits, max_colors);
+
+ /* get dimension of the image */
+ rows = Rast_window_rows();
+ cols = Rast_window_cols();
+
+ openfiles(opt_hue->answer, opt_int->answer, opt_sat->answer,
+ opt_red->answer, opt_green->answer, opt_blue->answer,
+ fd_input, fd_output, rowbuffer);
+
+ for (i = 0; i < rows; i++) {
+ int band;
+
+ G_percent(i, rows, 2);
+
+ /* read in a row from each cell map */
+ for (band = 0; band < 3; band++)
+ Rast_get_d_row(fd_input[band], rowbuffer[band], i);
+
+ /* process this row of the map */
+ his2rgb(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);
+ }
+ G_percent(1, 1, 1);
+
+ closefiles(opt_red->answer, opt_green->answer, opt_blue->answer,
+ fd_output, rowbuffer);
+
+ exit(EXIT_SUCCESS);
+}
Added: sandbox/alexandris/i.his.rgb/openfiles.c
===================================================================
--- sandbox/alexandris/i.his.rgb/openfiles.c (rev 0)
+++ sandbox/alexandris/i.his.rgb/openfiles.c 2016-09-12 13:19:02 UTC (rev 69457)
@@ -0,0 +1,32 @@
+#include <stdio.h>
+#include <grass/gis.h>
+#include <grass/raster.h>
+#include <grass/glocale.h>
+#include "globals.h"
+
+/*
+ * hue, saturation, intensity,
+ * red, green, blue,
+ * int fd_input[3], int fd_output[3],
+ * DCELL * rowbuf[3]
+ */
+void openfiles(char *hue, char *intensity, char *saturation,
+ char *red, char *green, char *blue,
+ int fd_input[3], int fd_output[3],
+ DCELL * rowbuf[3])
+{
+ /* open output files */
+ fd_output[0] = Rast_open_fp_new(red);
+ fd_output[1] = Rast_open_fp_new(green);
+ fd_output[2] = Rast_open_fp_new(blue);
+
+ /* allocate the cell row buffer */
+ rowbuf[0] = Rast_allocate_d_buf();
+ rowbuf[1] = Rast_allocate_d_buf();
+ rowbuf[2] = Rast_allocate_d_buf();
+
+ /* open input files (maps can be in different mapsets) */
+ fd_input[0] = Rast_open_old(hue, "");
+ fd_input[1] = Rast_open_old(intensity, "");
+ fd_input[2] = Rast_open_old(saturation, "");
+}
Modified: sandbox/alexandris/i.rgb.his/closefiles.c
===================================================================
--- sandbox/alexandris/i.rgb.his/closefiles.c 2016-09-12 13:15:45 UTC (rev 69456)
+++ sandbox/alexandris/i.rgb.his/closefiles.c 2016-09-12 13:19:02 UTC (rev 69457)
@@ -8,7 +8,9 @@
to grey scale. */
int closefiles(char *hue, char *intensity, char *saturation,
- int fd_output[3], DCELL * rowbuffer[3])
+ int fd_output[3],
+ DCELL * rowbuffer[3])
+
{
unsigned int property; // color properties: hue, intensity, saturation
struct Colors colors;
@@ -17,7 +19,8 @@
DCELL min, max;
const char *mapset;
- for (property = 0; property < 3; property++) {
+ for (property = 0; property < 3; property++)
+ {
Rast_close(fd_output[property]);
G_free(rowbuffer[property]);
}
Modified: sandbox/alexandris/i.rgb.his/globals.h
===================================================================
--- sandbox/alexandris/i.rgb.his/globals.h 2016-09-12 13:15:45 UTC (rev 69456)
+++ sandbox/alexandris/i.rgb.his/globals.h 2016-09-12 13:19:02 UTC (rev 69457)
@@ -3,14 +3,28 @@
#include <grass/raster.h>
-/* closefiles.c */
-int closefiles(char *, char *, char *, int[3], DCELL *[3]);
-
-/* openfiles.c */
+/*
+ * openfiles.c
+ * red, green, blue
+ * hue, saturation, intensity,
+ * input file descriptors, output file descriptors
+ * input rowbuffer
+ */
void openfiles(char *, char *, char *, char *, char *, char *, int[3], int[3],
DCELL *[3]);
-/* rgb2his.c */
+/*
+ * rgb2hsl.c
+ * input rowbuffer, columns, maximum range value
+ */
void rgb2his(DCELL *[3], unsigned int, double);
+/*
+ * closefiles.c
+ * hue, saturation, intensity
+ * output file descriptors
+ * output rowbuffers
+ */
+int closefiles(char *, char *, char *, int[3], DCELL *[3]);
+
#endif /* __GLOBALS_H__ */
Modified: sandbox/alexandris/i.rgb.his/i.rgb.his.html
===================================================================
--- sandbox/alexandris/i.rgb.his/i.rgb.his.html 2016-09-12 13:15:45 UTC (rev 69456)
+++ sandbox/alexandris/i.rgb.his/i.rgb.his.html 2016-09-12 13:19:02 UTC (rev 69457)
@@ -1,31 +1,35 @@
<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.
+<p><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.</p>
-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].
+<p>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].</p>
<h2>NOTES</h2>
-The module supports various bits per image (as in bits per color channel, bitness ranging in [2,16]).
+<p>The module supports various bits per image (as in bits per color channel,
+bitness ranging in [2,16]).</p>
-Each output raster map layer is given a linear gray scale color table. The current geographic region definition and mask settings are
-respected.
+<p>It is possible to process three bands with <em>i.rgb.his</em> and recover the
+original bands, nearly identical, with <em>i.his.rgb</em>.
+Due to rounding, minor loss of precision is expected. Cell values will be
+reproduced exactly after transformation in both directions, within plus or
+minus 1.</p>
+
+<p>Each output raster map layer is given a linear gray scale color table. The
+current geographic region definition and mask settings are respected.</p>
+
<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,
+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>
+with acknowledgements to Ali Vali, Univ. of Texas, Space Research Center, for the core routine.
+Nikos Alexandris, support for user defined bits per image
Modified: sandbox/alexandris/i.rgb.his/rgb2his.c
===================================================================
--- sandbox/alexandris/i.rgb.his/rgb2his.c 2016-09-12 13:15:45 UTC (rev 69456)
+++ sandbox/alexandris/i.rgb.his/rgb2his.c 2016-09-12 13:19:02 UTC (rev 69457)
@@ -1,17 +1,15 @@
/******************************************************************************
-NAME: RGB2HIS
+NAME: RGB to HIS
-PURPOSE To process red,green,blue bands to hue,intensity,saturation.
+PURPOSE To process red, green, blue bands to hue, intensity, saturation.
-ALGORITHM:
- Get red, green, blue from input buffer
+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.
+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
More information about the grass-commit
mailing list