main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /****************************************************************************
  2. *
  3. * MODULE: d.his
  4. * AUTHOR(S): James Westervelt, CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Huidae Cho <grass4u gmail.com>,
  8. * Glynn Clements <glynn gclements.plus.com>,
  9. * Jan-Oliver Wagner <jan intevation.de>,
  10. * Hamish Bowman (brightness option)
  11. * PURPOSE: produces a raster map layer using hue, intensity, and
  12. * saturation values from two or three user-specified raster
  13. * map layers
  14. * COPYRIGHT: (C) 1999-2007 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General Public
  17. * License (>=v2). Read the file COPYING that comes with GRASS
  18. * for details.
  19. *
  20. *****************************************************************************/
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <grass/gis.h>
  25. #include <grass/raster.h>
  26. #include <grass/display.h>
  27. #include <grass/glocale.h>
  28. #include "his.h"
  29. int main(int argc, char **argv)
  30. {
  31. unsigned char *hue_n, *hue_r, *hue_g, *hue_b;
  32. unsigned char *int_n, *int_r;
  33. unsigned char *sat_n, *sat_r;
  34. unsigned char *dummy;
  35. CELL *r_array, *g_array, *b_array;
  36. char *name_h, *name_i, *name_s;
  37. int intensity;
  38. int saturation;
  39. int atrow, atcol;
  40. int next_row;
  41. int hue_file;
  42. int int_file = 0;
  43. int int_used;
  44. int sat_file = 0;
  45. int sat_used;
  46. struct Cell_head window;
  47. struct Colors hue_colors;
  48. struct Colors int_colors;
  49. struct Colors sat_colors;
  50. struct Colors gray_colors;
  51. struct GModule *module;
  52. struct Option *opt_h, *opt_i, *opt_s, *brighten;
  53. struct Flag *nulldraw;
  54. double bright_mult;
  55. G_gisinit(argv[0]);
  56. module = G_define_module();
  57. G_add_keyword(_("display"));
  58. module->description =
  59. _("Displays the result obtained by combining "
  60. "hue, intensity, and saturation (his) values "
  61. "from user-specified input raster map layers.");
  62. opt_h = G_define_option();
  63. opt_h->key = "h_map";
  64. opt_h->type = TYPE_STRING;
  65. opt_h->required = YES;
  66. opt_h->gisprompt = "old,cell,raster";
  67. opt_h->description = _("Name of layer to be used for HUE");
  68. opt_i = G_define_option();
  69. opt_i->key = "i_map";
  70. opt_i->type = TYPE_STRING;
  71. opt_i->required = NO;
  72. opt_i->gisprompt = "old,cell,raster";
  73. opt_i->description = _("Name of layer to be used for INTENSITY");
  74. opt_s = G_define_option();
  75. opt_s->key = "s_map";
  76. opt_s->type = TYPE_STRING;
  77. opt_s->required = NO;
  78. opt_s->gisprompt = "old,cell,raster";
  79. opt_s->description = _("Name of layer to be used for SATURATION");
  80. brighten = G_define_option();
  81. brighten->key = "brighten";
  82. brighten->type = TYPE_INTEGER;
  83. brighten->description = _("Percent to brighten intensity channel");
  84. brighten->options = "-99-99";
  85. brighten->answer = "0";
  86. nulldraw = G_define_flag();
  87. nulldraw->key = 'n';
  88. nulldraw->description = _("Respect NULL values while drawing");
  89. if (G_parser(argc, argv))
  90. exit(EXIT_FAILURE);
  91. /* it's not truly the percentage to brighten,
  92. but saying that makes the option easy to use */
  93. bright_mult = 1 + 0.01 * atoi(brighten->answer);
  94. /* read in current window */
  95. G_get_window(&window);
  96. /* Do screen initializing stuff */
  97. if (D_open_driver() != 0)
  98. G_fatal_error(_("No graphics device selected"));
  99. /* Prepare the raster cell drawing functions */
  100. D_setup(0);
  101. D_set_overlay_mode(nulldraw->answer ? 1 : 0);
  102. /* Get name of layer to be used for hue */
  103. name_h = opt_h->answer;
  104. /* Make sure map is available */
  105. if ((hue_file = Rast_open_old(name_h, "")) == -1)
  106. G_fatal_error(_("Unable to open raster map <%s>"), name_h);
  107. hue_r = G_malloc(window.cols);
  108. hue_g = G_malloc(window.cols);
  109. hue_b = G_malloc(window.cols);
  110. hue_n = G_malloc(window.cols);
  111. dummy = G_malloc(window.cols);
  112. /* Reading color lookup table */
  113. if (Rast_read_colors(name_h, "", &hue_colors) == -1)
  114. G_fatal_error(_("Color file for <%s> not available"), name_h);
  115. int_used = 0;
  116. if (opt_i->answer != NULL) {
  117. /* Get name of layer to be used for intensity */
  118. name_i = opt_i->answer;
  119. int_used = 1;
  120. /* Make sure map is available */
  121. if ((int_file = Rast_open_old(name_i, "")) == -1)
  122. G_fatal_error(_("Unable to open raster map <%s>"), name_i);
  123. int_r = G_malloc(window.cols);
  124. int_n = G_malloc(window.cols);
  125. /* Reading color lookup table */
  126. if (Rast_read_colors(name_i, "", &int_colors) == -1)
  127. G_fatal_error(_("Color file for <%s> not available"), name_i);
  128. }
  129. sat_used = 0;
  130. if (opt_s->answer != NULL) {
  131. /* Get name of layer to be used for saturation */
  132. name_s = opt_s->answer;
  133. sat_used = 1;
  134. /* Make sure map is available */
  135. if ((sat_file = Rast_open_old(name_s, "")) == -1)
  136. G_fatal_error("Unable to open raster map <%s>", name_s);
  137. sat_r = G_malloc(window.cols);
  138. sat_n = G_malloc(window.cols);
  139. /* Reading color lookup table */
  140. if (Rast_read_colors(name_s, "", &sat_colors) == -1)
  141. G_fatal_error(_("Color file for <%s> not available"), name_s);
  142. }
  143. r_array = Rast_allocate_c_buf();
  144. g_array = Rast_allocate_c_buf();
  145. b_array = Rast_allocate_c_buf();
  146. /* Make color table */
  147. make_gray_scale(&gray_colors);
  148. /* Now do the work */
  149. intensity = 255; /* default is to not change intensity */
  150. saturation = 255; /* default is to not change saturation */
  151. D_cell_draw_begin();
  152. next_row = 0;
  153. for (atrow = 0; atrow < window.rows;) {
  154. G_percent(atrow, window.rows, 2);
  155. if (Rast_get_row_colors
  156. (hue_file, atrow, &hue_colors, hue_r, hue_g, hue_b, hue_n) < 0)
  157. G_fatal_error(_("Error reading hue data"));
  158. if (int_used &&
  159. (Rast_get_row_colors
  160. (int_file, atrow, &int_colors, int_r, dummy, dummy, int_n) < 0))
  161. G_fatal_error(_("Error reading intensity data"));
  162. if (sat_used &&
  163. (Rast_get_row_colors
  164. (sat_file, atrow, &sat_colors, sat_r, dummy, dummy, sat_n) < 0))
  165. G_fatal_error(_("Error reading saturation data"));
  166. for (atcol = 0; atcol < window.cols; atcol++) {
  167. if (nulldraw->answer) {
  168. if (hue_n[atcol]
  169. || (int_used && int_n[atcol])
  170. || (sat_used && sat_n[atcol])) {
  171. Rast_set_c_null_value(&r_array[atcol], 1);
  172. Rast_set_c_null_value(&g_array[atcol], 1);
  173. Rast_set_c_null_value(&b_array[atcol], 1);
  174. continue;
  175. }
  176. }
  177. if (int_used)
  178. intensity = (int)(int_r[atcol] * bright_mult);
  179. if (sat_used)
  180. saturation = sat_r[atcol];
  181. HIS_to_RGB(hue_r[atcol], hue_g[atcol], hue_b[atcol],
  182. intensity, saturation,
  183. &r_array[atcol], &g_array[atcol], &b_array[atcol]);
  184. }
  185. if (atrow == next_row)
  186. next_row = D_draw_raster_RGB(next_row,
  187. r_array, g_array, b_array,
  188. &gray_colors, &gray_colors,
  189. &gray_colors, CELL_TYPE, CELL_TYPE,
  190. CELL_TYPE);
  191. if (next_row > 0)
  192. atrow = next_row;
  193. else
  194. break;
  195. }
  196. G_percent(window.rows, window.rows, 5);
  197. D_cell_draw_end();
  198. /* Close down connection to display driver */
  199. D_close_driver();
  200. /* Close the raster maps */
  201. Rast_close(hue_file);
  202. if (int_used)
  203. Rast_close(int_file);
  204. if (sat_used)
  205. Rast_close(sat_file);
  206. exit(EXIT_SUCCESS);
  207. }