main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: r.his
  5. * AUTHOR(S): Glynn Clements - glynn.clements@virgin.net
  6. * PURPOSE: Create a color image by composing color, brightness
  7. * and haze maps
  8. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public
  11. * License (>=v2). Read the file COPYING that comes with GRASS
  12. * for details.
  13. *
  14. *****************************************************************************/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <grass/gis.h>
  19. #include <grass/raster.h>
  20. #include <grass/display.h>
  21. #include <grass/colors.h>
  22. #include "his.h"
  23. #include <grass/glocale.h>
  24. int main(int argc, char **argv)
  25. {
  26. unsigned char *hue_n, *hue_r, *hue_g, *hue_b;
  27. unsigned char *int_n, *int_r;
  28. unsigned char *sat_n, *sat_r;
  29. unsigned char *dummy;
  30. CELL *r_array, *g_array, *b_array;
  31. char *name_h, *name_i, *name_s;
  32. int intensity;
  33. int saturation;
  34. int atrow, atcol;
  35. int hue_file;
  36. int int_file = 0;
  37. int int_used;
  38. int sat_file = 0;
  39. int sat_used;
  40. char *name_r, *name_g, *name_b;
  41. int r_file = 0;
  42. int r_used;
  43. int g_file = 0;
  44. int g_used;
  45. int b_file = 0;
  46. int b_used;
  47. int bg_r, bg_g, bg_b;
  48. int bgcolor_state;
  49. int draw_nulls; /* 0 as nulls, 1 draw using bgcolor, 2 draw from table */
  50. struct Cell_head window;
  51. struct Colors hue_colors;
  52. struct Colors int_colors;
  53. struct Colors sat_colors;
  54. struct Colors gray_colors;
  55. struct History history;
  56. struct GModule *module;
  57. struct Option *opt_h, *opt_i, *opt_s;
  58. struct Option *opt_r, *opt_g, *opt_b;
  59. struct Option *bgcolor;
  60. struct Flag *nulldraw;
  61. G_gisinit(argv[0]);
  62. module = G_define_module();
  63. G_add_keyword(_("raster"));
  64. G_add_keyword(_("color transformation"));
  65. G_add_keyword("RGB");
  66. G_add_keyword("HIS");
  67. G_add_keyword("IHS");
  68. module->description =
  69. _("Generates red, green and blue (RGB) raster map layers "
  70. "combining hue, intensity and saturation (HIS) "
  71. "values from user-specified input raster map layers.");
  72. opt_h = G_define_option();
  73. opt_h->key = "hue";
  74. opt_h->type = TYPE_STRING;
  75. opt_h->required = YES;
  76. opt_h->gisprompt = "old,cell,raster";
  77. opt_h->description = _("Name of layer to be used for hue");
  78. opt_i = G_define_option();
  79. opt_i->key = "intensity";
  80. opt_i->type = TYPE_STRING;
  81. opt_i->required = NO;
  82. opt_i->gisprompt = "old,cell,raster";
  83. opt_i->description = _("Name of layer to be used for intensity");
  84. opt_s = G_define_option();
  85. opt_s->key = "saturation";
  86. opt_s->type = TYPE_STRING;
  87. opt_s->required = NO;
  88. opt_s->gisprompt = "old,cell,raster";
  89. opt_s->description = _("Name of layer to be used for saturation");
  90. opt_r = G_define_option();
  91. opt_r->key = "red";
  92. opt_r->type = TYPE_STRING;
  93. opt_r->required = YES;
  94. opt_r->gisprompt = "new,cell,raster";
  95. opt_r->description = _("Name of output layer to be used for red");
  96. opt_g = G_define_option();
  97. opt_g->key = "green";
  98. opt_g->type = TYPE_STRING;
  99. opt_g->required = YES;
  100. opt_g->gisprompt = "new,cell,raster";
  101. opt_g->description = _("Name of output layer to be used for green");
  102. opt_b = G_define_option();
  103. opt_b->key = "blue";
  104. opt_b->type = TYPE_STRING;
  105. opt_b->required = YES;
  106. opt_b->gisprompt = "new,cell,raster";
  107. opt_b->description = _("Name of output layer to be used for blue");
  108. bgcolor = G_define_standard_option(G_OPT_CN);
  109. bgcolor->key = "bgcolor";
  110. bgcolor->label = _("Color to use instead of NULL values");
  111. bgcolor->answer = NULL;
  112. nulldraw = G_define_flag();
  113. nulldraw->key = 'c';
  114. nulldraw->description = _("Use colors from color tables for NULL values");
  115. G_option_exclusive(bgcolor, nulldraw, NULL);
  116. if (G_parser(argc, argv))
  117. exit(EXIT_FAILURE);
  118. draw_nulls = 0;
  119. if (nulldraw->answer) {
  120. draw_nulls = 2;
  121. }
  122. if (bgcolor->answer) {
  123. bgcolor_state = G_str_to_color(bgcolor->answer, &bg_r, &bg_g, &bg_b);
  124. if (bgcolor_state == 1) {
  125. draw_nulls = 1;
  126. } else if (bgcolor_state == 2) {
  127. /* none is the same as not providing the color */
  128. draw_nulls = 0;
  129. } else {
  130. G_fatal_error(_("No such color <%s>"), bgcolor->answer);
  131. }
  132. }
  133. /* read in current window */
  134. G_get_window(&window);
  135. /* Get name of layer to be used for hue */
  136. name_h = opt_h->answer;
  137. /* Make sure map is available */
  138. hue_file = Rast_open_old(name_h, "");
  139. hue_r = G_malloc(window.cols);
  140. hue_g = G_malloc(window.cols);
  141. hue_b = G_malloc(window.cols);
  142. hue_n = G_malloc(window.cols);
  143. dummy = G_malloc(window.cols);
  144. /* Reading color lookup table */
  145. if (Rast_read_colors(name_h, "", &hue_colors) == -1)
  146. G_fatal_error(_("Color file for <%s> not available"), name_h);
  147. int_used = 0;
  148. if (opt_i->answer != NULL) {
  149. /* Get name of layer to be used for intensity */
  150. name_i = opt_i->answer;
  151. int_used = 1;
  152. /* Make sure map is available */
  153. int_file = Rast_open_old(name_i, "");
  154. int_r = G_malloc(window.cols);
  155. int_n = G_malloc(window.cols);
  156. /* Reading color lookup table */
  157. if (Rast_read_colors(name_i, "", &int_colors) == -1)
  158. G_fatal_error(_("Color file for <%s> not available"), name_i);
  159. }
  160. sat_used = 0;
  161. if (opt_s->answer != NULL) {
  162. /* Get name of layer to be used for saturation */
  163. name_s = opt_s->answer;
  164. sat_used = 1;
  165. /* Make sure map is available */
  166. sat_file = Rast_open_old(name_s, "");
  167. sat_r = G_malloc(window.cols);
  168. sat_n = G_malloc(window.cols);
  169. /* Reading color lookup table */
  170. if (Rast_read_colors(name_s, "", &sat_colors) == -1)
  171. G_fatal_error(_("Color file for <%s> not available"), name_s);
  172. }
  173. r_used = 0;
  174. if (opt_r->answer != NULL) {
  175. name_r = opt_r->answer;
  176. r_file = Rast_open_c_new(name_r);
  177. r_used = 1;
  178. }
  179. g_used = 0;
  180. if (opt_g->answer != NULL) {
  181. name_g = opt_g->answer;
  182. g_file = Rast_open_c_new(name_g);
  183. g_used = 1;
  184. }
  185. b_used = 0;
  186. if (opt_b->answer != NULL) {
  187. name_b = opt_b->answer;
  188. b_file = Rast_open_c_new(name_b);
  189. b_used = 1;
  190. }
  191. r_array = Rast_allocate_c_buf();
  192. g_array = Rast_allocate_c_buf();
  193. b_array = Rast_allocate_c_buf();
  194. /* Make color table */
  195. make_gray_scale(&gray_colors);
  196. /* Now do the work */
  197. intensity = 255; /* default is to not change intensity */
  198. saturation = 255; /* default is to not change saturation */
  199. for (atrow = 0; atrow < window.rows; atrow++) {
  200. G_percent(atrow, window.rows, 2);
  201. Rast_get_row_colors(hue_file, atrow, &hue_colors, hue_r, hue_g, hue_b, hue_n);
  202. if (int_used)
  203. Rast_get_row_colors(int_file, atrow, &int_colors, int_r, dummy, dummy, int_n);
  204. if (sat_used)
  205. Rast_get_row_colors(sat_file, atrow, &sat_colors, sat_r, dummy, dummy, sat_n);
  206. for (atcol = 0; atcol < window.cols; atcol++) {
  207. if (hue_n[atcol]
  208. || (int_used && int_n[atcol])
  209. || (sat_used && sat_n[atcol]))
  210. {
  211. if (draw_nulls == 0) {
  212. /* write nulls where nulls are by default */
  213. Rast_set_c_null_value(&r_array[atcol], 1);
  214. Rast_set_c_null_value(&g_array[atcol], 1);
  215. Rast_set_c_null_value(&b_array[atcol], 1);
  216. continue;
  217. } else if (draw_nulls == 1) {
  218. /* if nulls opaque and bgcolor provided use it */
  219. r_array[atcol] = bg_r;
  220. g_array[atcol] = bg_g;
  221. b_array[atcol] = bg_b;
  222. continue;
  223. }
  224. /* else use the color table colors, G6 default */
  225. }
  226. if (int_used)
  227. intensity = int_r[atcol];
  228. if (sat_used)
  229. saturation = sat_r[atcol];
  230. HIS_to_RGB(hue_r[atcol], hue_g[atcol], hue_b[atcol],
  231. intensity, saturation,
  232. &r_array[atcol], &g_array[atcol], &b_array[atcol]);
  233. }
  234. if (r_used)
  235. Rast_put_row(r_file, r_array, CELL_TYPE);
  236. if (g_used)
  237. Rast_put_row(g_file, g_array, CELL_TYPE);
  238. if (b_used)
  239. Rast_put_row(b_file, b_array, CELL_TYPE);
  240. }
  241. G_percent(window.rows, window.rows, 5);
  242. /* Close the cell files */
  243. Rast_close(hue_file);
  244. if (int_used)
  245. Rast_close(int_file);
  246. if (sat_used)
  247. Rast_close(sat_file);
  248. if (r_used) {
  249. Rast_close(r_file);
  250. Rast_write_colors(name_r, G_mapset(), &gray_colors);
  251. Rast_short_history(name_r, "raster", &history);
  252. Rast_command_history(&history);
  253. Rast_write_history(name_r, &history);
  254. Rast_put_cell_title(name_r, "Red extracted from HIS");
  255. }
  256. if (g_used) {
  257. Rast_close(g_file);
  258. Rast_write_colors(name_g, G_mapset(), &gray_colors);
  259. Rast_short_history(name_g, "raster", &history);
  260. Rast_command_history(&history);
  261. Rast_write_history(name_g, &history);
  262. Rast_put_cell_title(name_g, "Green extracted from HIS");
  263. }
  264. if (b_used) {
  265. Rast_close(b_file);
  266. Rast_write_colors(name_b, G_mapset(), &gray_colors);
  267. Rast_short_history(name_b, "raster", &history);
  268. Rast_command_history(&history);
  269. Rast_write_history(name_b, &history);
  270. Rast_put_cell_title(name_b, "Blue extracted from HIS");
  271. }
  272. return EXIT_SUCCESS;
  273. }