main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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 = "hue";
  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 = "intensity";
  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 = "saturation";
  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. D_open_driver();
  103. /* Prepare the raster cell drawing functions */
  104. D_setup(0);
  105. D_set_overlay_mode(nulldraw->answer ? 1 : 0);
  106. /* Get name of layer to be used for hue */
  107. name_h = opt_h->answer;
  108. /* Make sure map is available */
  109. hue_file = Rast_open_old(name_h, "");
  110. hue_r = G_malloc(window.cols);
  111. hue_g = G_malloc(window.cols);
  112. hue_b = G_malloc(window.cols);
  113. hue_n = G_malloc(window.cols);
  114. dummy = G_malloc(window.cols);
  115. /* Reading color lookup table */
  116. if (Rast_read_colors(name_h, "", &hue_colors) == -1)
  117. G_fatal_error(_("Color file for <%s> not available"), name_h);
  118. int_used = 0;
  119. if (opt_i->answer != NULL) {
  120. /* Get name of layer to be used for intensity */
  121. name_i = opt_i->answer;
  122. int_used = 1;
  123. /* Make sure map is available */
  124. int_file = Rast_open_old(name_i, "");
  125. int_r = G_malloc(window.cols);
  126. int_n = G_malloc(window.cols);
  127. /* Reading color lookup table */
  128. if (Rast_read_colors(name_i, "", &int_colors) == -1)
  129. G_fatal_error(_("Color file for <%s> not available"), name_i);
  130. }
  131. sat_used = 0;
  132. if (opt_s->answer != NULL) {
  133. /* Get name of layer to be used for saturation */
  134. name_s = opt_s->answer;
  135. sat_used = 1;
  136. /* Make sure map is available */
  137. sat_file = Rast_open_old(name_s, "");
  138. sat_r = G_malloc(window.cols);
  139. sat_n = G_malloc(window.cols);
  140. /* Reading color lookup table */
  141. if (Rast_read_colors(name_s, "", &sat_colors) == -1)
  142. G_fatal_error(_("Color file for <%s> not available"), name_s);
  143. }
  144. r_array = Rast_allocate_c_buf();
  145. g_array = Rast_allocate_c_buf();
  146. b_array = Rast_allocate_c_buf();
  147. /* Make color table */
  148. make_gray_scale(&gray_colors);
  149. /* Now do the work */
  150. intensity = 255; /* default is to not change intensity */
  151. saturation = 255; /* default is to not change saturation */
  152. D_raster_draw_begin();
  153. next_row = 0;
  154. for (atrow = 0; atrow < window.rows;) {
  155. G_percent(atrow, window.rows, 2);
  156. Rast_get_row_colors
  157. (hue_file, atrow, &hue_colors, hue_r, hue_g, hue_b, hue_n);
  158. if (int_used)
  159. Rast_get_row_colors(int_file, atrow, &int_colors, int_r, dummy, dummy, int_n);
  160. if (sat_used)
  161. Rast_get_row_colors(sat_file, atrow, &sat_colors, sat_r, dummy, dummy, sat_n);
  162. for (atcol = 0; atcol < window.cols; atcol++) {
  163. if (nulldraw->answer) {
  164. if (hue_n[atcol]
  165. || (int_used && int_n[atcol])
  166. || (sat_used && sat_n[atcol])) {
  167. Rast_set_c_null_value(&r_array[atcol], 1);
  168. Rast_set_c_null_value(&g_array[atcol], 1);
  169. Rast_set_c_null_value(&b_array[atcol], 1);
  170. continue;
  171. }
  172. }
  173. if (int_used)
  174. intensity = (int)(int_r[atcol] * bright_mult);
  175. if (sat_used)
  176. saturation = sat_r[atcol];
  177. HIS_to_RGB(hue_r[atcol], hue_g[atcol], hue_b[atcol],
  178. intensity, saturation,
  179. &r_array[atcol], &g_array[atcol], &b_array[atcol]);
  180. }
  181. if (atrow == next_row)
  182. next_row = D_draw_raster_RGB(next_row,
  183. r_array, g_array, b_array,
  184. &gray_colors, &gray_colors,
  185. &gray_colors, CELL_TYPE, CELL_TYPE,
  186. CELL_TYPE);
  187. if (next_row > 0)
  188. atrow = next_row;
  189. else
  190. break;
  191. }
  192. G_percent(window.rows, window.rows, 5);
  193. D_raster_draw_end();
  194. D_save_command(G_recreate_command());
  195. /* Close down connection to display driver */
  196. D_close_driver();
  197. /* Close the raster maps */
  198. Rast_close(hue_file);
  199. if (int_used)
  200. Rast_close(int_file);
  201. if (sat_used)
  202. Rast_close(sat_file);
  203. exit(EXIT_SUCCESS);
  204. }