main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /****************************************************************************
  2. *
  3. * MODULE: d.colortable
  4. * AUTHOR(S): James Westervelt, CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Eric G. Miller <egm2 jps.net>,
  8. * Glynn Clements <glynn gclements.plus.com>,
  9. * Hamish Bowman <hamish_b yahoo.com>,
  10. * Jan-Oliver Wagner <jan intevation.de>
  11. * PURPOSE: display the color table associated with a raster map layer in
  12. * the active frame on the graphics monitor
  13. * COPYRIGHT: (C) 1999-2009 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 <stdlib.h>
  21. #include <math.h>
  22. #include <grass/display.h>
  23. #include <grass/gis.h>
  24. #include <grass/raster.h>
  25. #include <grass/glocale.h>
  26. int main(int argc, char **argv)
  27. {
  28. char *map_name;
  29. int color;
  30. int lines;
  31. int cols;
  32. struct FPRange fp_range;
  33. struct Colors colors;
  34. double ratio;
  35. DCELL dmin, dmax, dval;
  36. int cats_num;
  37. int cur_dot_row, cur_dot_col;
  38. int dots_per_line, dots_per_col;
  39. int atcat;
  40. int white, black;
  41. int atcol, atline;
  42. int count, offset;
  43. double t, b, l, r;
  44. int fp, new_colr;
  45. double x_box[5], y_box[5];
  46. struct GModule *module;
  47. struct Option *opt1, *opt2, *opt3, *opt4;
  48. struct Flag *skip_null;
  49. /* Initialize the GIS calls */
  50. G_gisinit(argv[0]);
  51. module = G_define_module();
  52. G_add_keyword(_("display"));
  53. G_add_keyword(_("raster"));
  54. module->description =
  55. _("Displays the color table associated with a raster map layer.");
  56. opt1 = G_define_standard_option(G_OPT_R_MAP);
  57. opt1->description =
  58. _("Name of raster map whose color table is to be displayed");
  59. opt2 = G_define_option();
  60. opt2->key = "color";
  61. opt2->type = TYPE_STRING;
  62. opt2->answer = DEFAULT_BG_COLOR;
  63. opt2->gisprompt = "old_color,color,color";
  64. opt2->description =
  65. _("Color of lines separating the colors of the color table");
  66. opt3 = G_define_option();
  67. opt3->key = "lines";
  68. opt3->type = TYPE_INTEGER;
  69. opt3->options = "1-1000";
  70. opt3->description = _("Number of lines to appear in the color table");
  71. opt4 = G_define_option();
  72. opt4->key = "cols";
  73. opt4->type = TYPE_INTEGER;
  74. opt4->options = "1-1000";
  75. opt4->description = _("Number of columns to appear in the color table");
  76. skip_null = G_define_flag();
  77. skip_null->key = 'n';
  78. skip_null->description =
  79. _("Don't draw a collar showing the NULL color in FP maps");
  80. /* Check command line */
  81. if (G_parser(argc, argv))
  82. exit(EXIT_FAILURE);
  83. map_name = opt1->answer;
  84. fp = Rast_map_is_fp(map_name, "");
  85. if (opt2->answer != NULL) {
  86. new_colr = D_translate_color(opt2->answer);
  87. color = new_colr;
  88. }
  89. if (fp)
  90. lines = 1;
  91. else
  92. lines = 0;
  93. if (opt3->answer != NULL) {
  94. if (fp)
  95. G_warning(_("<%s> is floating-point; "
  96. "ignoring [lines] and drawing continuous color ramp"),
  97. map_name);
  98. else
  99. sscanf(opt3->answer, "%d", &lines);
  100. }
  101. if (fp)
  102. cols = 1;
  103. else
  104. cols = 0;
  105. if (opt4->answer) {
  106. if (fp)
  107. G_warning(_("<%s> is floating-point; "
  108. "ignoring [cols] and drawing continuous color ramp"),
  109. map_name);
  110. else
  111. sscanf(opt4->answer, "%d", &cols);
  112. }
  113. /* Make sure map is available */
  114. if (Rast_read_colors(map_name, "", &colors) == -1)
  115. G_fatal_error(_("Color file for <%s> not available"), map_name);
  116. if (Rast_read_fp_range(map_name, "", &fp_range) == -1)
  117. G_fatal_error(_("Range file for <%s> not available"), map_name);
  118. if (D_open_driver() != 0)
  119. G_fatal_error(_("No graphics device selected. "
  120. "Use d.mon to select graphics device."));
  121. D_setup_unity(0);
  122. D_get_src(&t, &b, &l, &r);
  123. Rast_get_fp_range_min_max(&fp_range, &dmin, &dmax);
  124. if (Rast_is_d_null_value(&dmin) || Rast_is_d_null_value(&dmax))
  125. G_fatal_error(_("Data range is empty"));
  126. cats_num = (int)dmax - (int)dmin + 1;
  127. if (lines <= 0 && cols <= 0) {
  128. double dx, dy;
  129. dy = (double)(b - t);
  130. dx = (double)(r - l);
  131. ratio = dy / dx;
  132. cols = 1 + sqrt((dmax - dmin + 1.) / ratio);
  133. lines = 1 + cats_num / cols;
  134. }
  135. else if (lines > 0 && cols <= 0) {
  136. cols = 1 + cats_num / lines;
  137. }
  138. else if (cols > 0 && lines <= 0) {
  139. lines = 1 + cats_num / cols;
  140. }
  141. /* otherwise, accept without complaint what the user requests
  142. * It is possible that the number of lines and cols is not
  143. * sufficient for the number of categories.
  144. */
  145. dots_per_line = (b - t) / lines;
  146. dots_per_col = (r - l) / cols;
  147. x_box[0] = 0; y_box[0] = 0;
  148. x_box[1] = 0; y_box[1] = (6 - dots_per_line);
  149. x_box[2] = (dots_per_col - 6); y_box[2] = 0;
  150. x_box[3] = 0; y_box[3] = (dots_per_line - 6);
  151. x_box[4] = (6 - dots_per_col); y_box[4] = 0;
  152. white = D_translate_color("white");
  153. black = D_translate_color("black");
  154. Rast_set_c_null_value(&atcat, 1);
  155. if (!fp) {
  156. for (atcol = 0; atcol < cols; atcol++) {
  157. cur_dot_row = t;
  158. cur_dot_col = l + atcol * dots_per_col;
  159. count = 0;
  160. for (atline = 0; atline < lines; atline++) {
  161. cur_dot_row += dots_per_line;
  162. /* Draw outer border box */
  163. D_use_color(color);
  164. D_begin();
  165. D_move_abs(cur_dot_col + 2, (cur_dot_row - 1));
  166. D_cont_rel(0, (2 - dots_per_line));
  167. D_cont_rel((dots_per_col - 2), 0);
  168. D_cont_rel(0, (dots_per_line - 2));
  169. D_cont_rel((2 - dots_per_col), 0);
  170. D_end();
  171. D_stroke();
  172. /* Draw black box */
  173. D_use_color(black);
  174. D_begin();
  175. D_move_abs(cur_dot_col + 3, (cur_dot_row - 2));
  176. D_cont_rel(0, (4 - dots_per_line));
  177. D_cont_rel((dots_per_col - 4), 0);
  178. D_cont_rel(0, (dots_per_line - 4));
  179. D_cont_rel((4 - dots_per_col), 0);
  180. D_end();
  181. D_stroke();
  182. /* Color box */
  183. D_color((CELL) atcat, &colors);
  184. D_pos_abs(cur_dot_col + 4, (cur_dot_row - 3));
  185. D_polygon_rel(x_box, y_box, 5);
  186. count++;
  187. /* first cat number is null value */
  188. if (count == 1)
  189. atcat = (int)dmin;
  190. else if (++atcat > (int)dmax)
  191. break;
  192. }
  193. if (atcat > (int)dmax)
  194. break;
  195. } /* col loop */
  196. } /* int map */
  197. else {
  198. /*** draw continuous color ramp for fp map ***/
  199. cur_dot_row = t + dots_per_line;
  200. cur_dot_col = l;
  201. /* Draw outer border box */
  202. D_use_color(color);
  203. D_begin();
  204. D_move_abs(cur_dot_col + 1, (cur_dot_row - 1));
  205. D_cont_rel(0, (2 - dots_per_line));
  206. D_cont_rel((dots_per_col - 2), 0);
  207. D_cont_rel(0, (dots_per_line - 2));
  208. D_cont_rel((2 - dots_per_col), 0);
  209. D_end();
  210. D_stroke();
  211. /* Draw black box */
  212. D_use_color(black);
  213. D_begin();
  214. D_move_abs(cur_dot_col + 2, (cur_dot_row - 2));
  215. D_cont_rel(0, (4 - dots_per_line));
  216. D_cont_rel((dots_per_col - 4), 0);
  217. D_cont_rel(0, (dots_per_line - 4));
  218. D_cont_rel((4 - dots_per_col), 0);
  219. D_end();
  220. D_stroke();
  221. /* Color ramp box */
  222. /* get separate color for each pixel */
  223. /* fisrt 5 pixels draw null color */
  224. y_box[1] = -1;
  225. y_box[3] = 1;
  226. x_box[2] = (dots_per_col - 6);
  227. x_box[4] = (6 - dots_per_col);
  228. G_debug(1, "dots_per_line: %d dmin=%.2f dmax=%.2f",
  229. dots_per_line, dmin, dmax);
  230. if (skip_null->answer)
  231. offset = 1;
  232. else
  233. offset = 4;
  234. for (r = 0; r < dots_per_line - 6; r++) {
  235. if ((r <= 4) && !skip_null->answer)
  236. Rast_set_d_null_value(&dval, 1);
  237. else
  238. dval =
  239. dmin + r*(dmax - dmin) / (dots_per_line - 6 - offset);
  240. D_d_color(dval, &colors);
  241. D_pos_abs(cur_dot_col + 3, (cur_dot_row - 3) - r);
  242. D_polygon_rel(x_box, y_box, 5);
  243. }
  244. }
  245. D_save_command(G_recreate_command());
  246. D_close_driver();
  247. exit(EXIT_SUCCESS);
  248. }