main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /****************************************************************************
  2. *
  3. * MODULE: d.rgb
  4. * AUTHOR(S): Glynn Clements <glynn gclements.plus.com> (original contributor)
  5. * Roberto Flor <flor itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Hamish Bowman <hamish_b yahoo.com>,
  8. * Markus Neteler <neteler itc.it>,
  9. * Radim Blazek <radim.blazek gmail.com>
  10. * Martin Landa <landa.martin gmail.com> (nulls opaque)
  11. * PURPOSE: Combine three rasters to form a colour image using red, green,
  12. * and blue display channels
  13. * COPYRIGHT: (C) 2001-2007, 2010 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. *****************************************************************************/
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <grass/gis.h>
  24. #include <grass/raster.h>
  25. #include <grass/display.h>
  26. #include <grass/glocale.h>
  27. struct band
  28. {
  29. struct Option *opt;
  30. int file;
  31. int type;
  32. void *array;
  33. struct Colors colors;
  34. };
  35. static char *const color_names[3] = { "red", "green", "blue" };
  36. int main(int argc, char **argv)
  37. {
  38. struct band B[3];
  39. int row;
  40. int next_row;
  41. int overlay;
  42. struct Cell_head window;
  43. struct GModule *module;
  44. struct Flag *flag_n;
  45. int i;
  46. G_gisinit(argv[0]);
  47. module = G_define_module();
  48. G_add_keyword(_("display"));
  49. G_add_keyword(_("graphics"));
  50. G_add_keyword(_("raster"));
  51. G_add_keyword("RGB");
  52. module->description =
  53. _("Displays three user-specified raster maps "
  54. "as red, green, and blue overlays in the active graphics frame.");
  55. flag_n = G_define_flag();
  56. flag_n->key = 'n';
  57. flag_n->description = _("Make null cells opaque");
  58. flag_n->guisection = _("Null cells");
  59. for (i = 0; i < 3; i++) {
  60. char buff[80];
  61. sprintf(buff, _("Name of raster map to be used for <%s>"),
  62. color_names[i]);
  63. B[i].opt = G_define_standard_option(G_OPT_R_MAP);
  64. B[i].opt->key = G_store(color_names[i]);
  65. B[i].opt->description = G_store(buff);
  66. }
  67. if (G_parser(argc, argv))
  68. exit(EXIT_FAILURE);
  69. /* Do screen initializing stuff */
  70. D_open_driver();
  71. overlay = !flag_n->answer;
  72. D_setup(0);
  73. D_set_overlay_mode(overlay);
  74. for (i = 0; i < 3; i++) {
  75. /* Get name of layer to be used */
  76. char *name = B[i].opt->answer;
  77. /* Make sure map is available */
  78. B[i].file = Rast_open_old(name, "");
  79. B[i].type = Rast_get_map_type(B[i].file);
  80. /* Reading color lookup table */
  81. if (Rast_read_colors(name, "", &B[i].colors) == -1)
  82. G_fatal_error(_("Color file for <%s> not available"), name);
  83. B[i].array = Rast_allocate_buf(B[i].type);
  84. }
  85. /* read in current window */
  86. G_get_window(&window);
  87. D_raster_draw_begin();
  88. next_row = 0;
  89. for (row = 0; row < window.rows;) {
  90. G_percent(row, window.rows, 5);
  91. for (i = 0; i < 3; i++)
  92. Rast_get_row(B[i].file, B[i].array, row, B[i].type);
  93. if (row == next_row)
  94. next_row = D_draw_raster_RGB(next_row,
  95. B[0].array, B[1].array, B[2].array,
  96. &B[0].colors, &B[1].colors,
  97. &B[2].colors, B[0].type, B[1].type,
  98. B[2].type);
  99. else if (next_row > 0)
  100. row = next_row;
  101. else
  102. break;
  103. }
  104. G_percent(window.rows, window.rows, 5);
  105. D_raster_draw_end();
  106. D_save_command(G_recreate_command());
  107. D_close_driver();
  108. /* Close the raster maps */
  109. for (i = 0; i < 3; i++)
  110. Rast_close(B[i].file);
  111. exit(EXIT_SUCCESS);
  112. }