main.c 7.1 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. G_add_keyword(_("graphics"));
  59. G_add_keyword(_("color transformation"));
  60. G_add_keyword("RGB");
  61. G_add_keyword("HIS");
  62. G_add_keyword("IHS");
  63. module->description =
  64. _("Displays the result obtained by combining "
  65. "hue, intensity, and saturation (his) values "
  66. "from user-specified input raster map layers.");
  67. opt_h = G_define_option();
  68. opt_h->key = "h_map";
  69. opt_h->type = TYPE_STRING;
  70. opt_h->required = YES;
  71. opt_h->gisprompt = "old,cell,raster";
  72. opt_h->description = _("Name of layer to be used for HUE");
  73. opt_i = G_define_option();
  74. opt_i->key = "i_map";
  75. opt_i->type = TYPE_STRING;
  76. opt_i->required = NO;
  77. opt_i->gisprompt = "old,cell,raster";
  78. opt_i->description = _("Name of layer to be used for INTENSITY");
  79. opt_s = G_define_option();
  80. opt_s->key = "s_map";
  81. opt_s->type = TYPE_STRING;
  82. opt_s->required = NO;
  83. opt_s->gisprompt = "old,cell,raster";
  84. opt_s->description = _("Name of layer to be used for SATURATION");
  85. brighten = G_define_option();
  86. brighten->key = "brighten";
  87. brighten->type = TYPE_INTEGER;
  88. brighten->description = _("Percent to brighten intensity channel");
  89. brighten->options = "-99-99";
  90. brighten->answer = "0";
  91. nulldraw = G_define_flag();
  92. nulldraw->key = 'n';
  93. nulldraw->description = _("Respect NULL values while drawing");
  94. if (G_parser(argc, argv))
  95. exit(EXIT_FAILURE);
  96. /* it's not truly the percentage to brighten,
  97. but saying that makes the option easy to use */
  98. bright_mult = 1 + 0.01 * atoi(brighten->answer);
  99. /* read in current window */
  100. G_get_window(&window);
  101. /* Do screen initializing stuff */
  102. if (D_open_driver() != 0)
  103. G_fatal_error(_("No graphics device selected. "
  104. "Use d.mon to select graphics device."));
  105. /* Prepare the raster cell drawing functions */
  106. D_setup(0);
  107. D_set_overlay_mode(nulldraw->answer ? 1 : 0);
  108. /* Get name of layer to be used for hue */
  109. name_h = opt_h->answer;
  110. /* Make sure map is available */
  111. hue_file = Rast_open_old(name_h, "");
  112. hue_r = G_malloc(window.cols);
  113. hue_g = G_malloc(window.cols);
  114. hue_b = G_malloc(window.cols);
  115. hue_n = G_malloc(window.cols);
  116. dummy = G_malloc(window.cols);
  117. /* Reading color lookup table */
  118. if (Rast_read_colors(name_h, "", &hue_colors) == -1)
  119. G_fatal_error(_("Color file for <%s> not available"), name_h);
  120. int_used = 0;
  121. if (opt_i->answer != NULL) {
  122. /* Get name of layer to be used for intensity */
  123. name_i = opt_i->answer;
  124. int_used = 1;
  125. /* Make sure map is available */
  126. int_file = Rast_open_old(name_i, "");
  127. int_r = G_malloc(window.cols);
  128. int_n = G_malloc(window.cols);
  129. /* Reading color lookup table */
  130. if (Rast_read_colors(name_i, "", &int_colors) == -1)
  131. G_fatal_error(_("Color file for <%s> not available"), name_i);
  132. }
  133. sat_used = 0;
  134. if (opt_s->answer != NULL) {
  135. /* Get name of layer to be used for saturation */
  136. name_s = opt_s->answer;
  137. sat_used = 1;
  138. /* Make sure map is available */
  139. sat_file = Rast_open_old(name_s, "");
  140. sat_r = G_malloc(window.cols);
  141. sat_n = G_malloc(window.cols);
  142. /* Reading color lookup table */
  143. if (Rast_read_colors(name_s, "", &sat_colors) == -1)
  144. G_fatal_error(_("Color file for <%s> not available"), name_s);
  145. }
  146. r_array = Rast_allocate_c_buf();
  147. g_array = Rast_allocate_c_buf();
  148. b_array = Rast_allocate_c_buf();
  149. /* Make color table */
  150. make_gray_scale(&gray_colors);
  151. /* Now do the work */
  152. intensity = 255; /* default is to not change intensity */
  153. saturation = 255; /* default is to not change saturation */
  154. D_cell_draw_begin();
  155. next_row = 0;
  156. for (atrow = 0; atrow < window.rows;) {
  157. G_percent(atrow, window.rows, 2);
  158. Rast_get_row_colors
  159. (hue_file, atrow, &hue_colors, hue_r, hue_g, hue_b, hue_n);
  160. if (int_used)
  161. Rast_get_row_colors(int_file, atrow, &int_colors, int_r, dummy, dummy, int_n);
  162. if (sat_used)
  163. Rast_get_row_colors(sat_file, atrow, &sat_colors, sat_r, dummy, dummy, sat_n);
  164. for (atcol = 0; atcol < window.cols; atcol++) {
  165. if (nulldraw->answer) {
  166. if (hue_n[atcol]
  167. || (int_used && int_n[atcol])
  168. || (sat_used && sat_n[atcol])) {
  169. Rast_set_c_null_value(&r_array[atcol], 1);
  170. Rast_set_c_null_value(&g_array[atcol], 1);
  171. Rast_set_c_null_value(&b_array[atcol], 1);
  172. continue;
  173. }
  174. }
  175. if (int_used)
  176. intensity = (int)(int_r[atcol] * bright_mult);
  177. if (sat_used)
  178. saturation = sat_r[atcol];
  179. HIS_to_RGB(hue_r[atcol], hue_g[atcol], hue_b[atcol],
  180. intensity, saturation,
  181. &r_array[atcol], &g_array[atcol], &b_array[atcol]);
  182. }
  183. if (atrow == next_row)
  184. next_row = D_draw_raster_RGB(next_row,
  185. r_array, g_array, b_array,
  186. &gray_colors, &gray_colors,
  187. &gray_colors, CELL_TYPE, CELL_TYPE,
  188. CELL_TYPE);
  189. if (next_row > 0)
  190. atrow = next_row;
  191. else
  192. break;
  193. }
  194. G_percent(window.rows, window.rows, 5);
  195. D_cell_draw_end();
  196. D_save_command(G_recreate_command());
  197. /* Close down connection to display driver */
  198. D_close_driver();
  199. /* Close the raster maps */
  200. Rast_close(hue_file);
  201. if (int_used)
  202. Rast_close(int_file);
  203. if (sat_used)
  204. Rast_close(sat_file);
  205. exit(EXIT_SUCCESS);
  206. }