main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /****************************************************************************
  2. *
  3. * MODULE: r.out.ppm
  4. * AUTHOR(S): Bill Brown, USA-CERL (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Glynn Clements <glynn gclements.plus.com>,
  8. * Jachym Cepicky <jachym les-ejk.cz>,
  9. * Jan-Oliver Wagner <jan intevation.de>
  10. * PURPOSE: converts a GRASS raster map into a PPM image (obeying REGION)
  11. * COPYRIGHT: (C) 1999-2007 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <grass/gis.h>
  21. #include <grass/raster.h>
  22. #include <grass/glocale.h>
  23. #define DEF_RED 255
  24. #define DEF_GRN 255
  25. #define DEF_BLU 255
  26. typedef int FILEDESC;
  27. int main(int argc, char *argv[])
  28. {
  29. struct GModule *module;
  30. struct Option *rast, *ppm_file;
  31. struct Flag *gscale, *header;
  32. char *map, *p, ofile[1000];
  33. unsigned char *set, *ored, *ogrn, *oblu;
  34. CELL *cell_buf;
  35. FCELL *fcell_buf;
  36. DCELL *dcell_buf;
  37. void *voidc;
  38. int rtype, row, col, do_stdout = 0;
  39. struct Cell_head w;
  40. FILEDESC cellfile = 0;
  41. FILE *fp;
  42. G_gisinit(argv[0]);
  43. module = G_define_module();
  44. G_add_keyword(_("raster"));
  45. G_add_keyword(_("export"));
  46. module->description = _("Converts a GRASS raster map to a PPM image file.");
  47. rast = G_define_standard_option(G_OPT_R_INPUT);
  48. ppm_file = G_define_standard_option(G_OPT_F_OUTPUT);
  49. ppm_file->required = NO;
  50. ppm_file->answer = "<rasterfilename>.ppm";
  51. ppm_file->description = _("Name for new PPM file (use '-' for stdout)");
  52. gscale = G_define_flag();
  53. gscale->key = 'g';
  54. gscale->description = _("Output greyscale instead of color");
  55. header = G_define_flag();
  56. header->key = 'h';
  57. header->description = _("Suppress printing of PPM header");
  58. if (G_parser(argc, argv))
  59. exit(EXIT_FAILURE);
  60. /* kludge to work with r.out.mpeg */
  61. if (rast->answer[0] == '/')
  62. rast->answer++;
  63. if (strcmp(ppm_file->answer, "<rasterfilename>.ppm")) {
  64. if (strcmp(ppm_file->answer, "-"))
  65. strcpy(ofile, ppm_file->answer);
  66. else
  67. do_stdout = 1;
  68. }
  69. else {
  70. map = p = rast->answer;
  71. /* knock off any GRASS location suffix */
  72. if ((char *)NULL != (p = strrchr(map, '@'))) {
  73. if (p != map)
  74. *p = '\0';
  75. }
  76. strcpy(ofile, map);
  77. strcat(ofile, ".ppm");
  78. }
  79. /*G_get_set_window (&w); *//* 10/99 MN: check for current region */
  80. G_get_window(&w);
  81. G_message(_("rows = %d, cols = %d"), w.rows, w.cols);
  82. /* open raster map for reading */
  83. cellfile = Rast_open_old(rast->answer, "");
  84. cell_buf = Rast_allocate_c_buf();
  85. fcell_buf = Rast_allocate_f_buf();
  86. dcell_buf = Rast_allocate_d_buf();
  87. ored = G_malloc(w.cols);
  88. ogrn = G_malloc(w.cols);
  89. oblu = G_malloc(w.cols);
  90. set = G_malloc(w.cols);
  91. /* open ppm file for writing */
  92. {
  93. if (do_stdout)
  94. fp = stdout;
  95. else if (NULL == (fp = fopen(ofile, "w"))) {
  96. G_fatal_error(_("Unable to open file <%s> for write"), ofile);
  97. }
  98. }
  99. /* write header info */
  100. if (!header->answer) {
  101. if (!gscale->answer)
  102. fprintf(fp, "P6\n");
  103. /* Magic number meaning rawbits, 24bit color to ppm format */
  104. else
  105. fprintf(fp, "P5\n");
  106. /* Magic number meaning rawbits, 8bit greyscale to ppm format */
  107. if (!do_stdout) {
  108. fprintf(fp, "# CREATOR: %s from GRASS raster map \"%s\"\n",
  109. G_program_name(), rast->answer);
  110. fprintf(fp, "# east-west resolution: %f\n", w.ew_res);
  111. fprintf(fp, "# north-south resolution: %f\n", w.ns_res);
  112. fprintf(fp, "# South edge: %f\n", w.south);
  113. fprintf(fp, "# West edge: %f\n", w.west);
  114. /* comments */
  115. }
  116. fprintf(fp, "%d %d\n", w.cols, w.rows);
  117. /* width & height */
  118. fprintf(fp, "255\n");
  119. /* max intensity val */
  120. }
  121. G_important_message(_("Converting..."));
  122. {
  123. struct Colors colors;
  124. Rast_read_colors(rast->answer, "", &colors);
  125. rtype = Rast_get_map_type(cellfile);
  126. if (rtype == CELL_TYPE)
  127. voidc = (CELL *) cell_buf;
  128. else if (rtype == FCELL_TYPE)
  129. voidc = (FCELL *) fcell_buf;
  130. else if (rtype == DCELL_TYPE)
  131. voidc = (DCELL *) dcell_buf;
  132. else
  133. exit(1);
  134. if (!gscale->answer) { /* 24BIT COLOR IMAGE */
  135. for (row = 0; row < w.rows; row++) {
  136. G_percent(row, w.rows, 5);
  137. Rast_get_row(cellfile, (void *)voidc, row, rtype);
  138. Rast_lookup_colors((void *)voidc, ored, ogrn, oblu, set,
  139. w.cols, &colors, rtype);
  140. for (col = 0; col < w.cols; col++) {
  141. if (set[col]) {
  142. putc(ored[col], fp);
  143. putc(ogrn[col], fp);
  144. putc(oblu[col], fp);
  145. }
  146. else {
  147. putc(DEF_RED, fp);
  148. putc(DEF_GRN, fp);
  149. putc(DEF_BLU, fp);
  150. }
  151. }
  152. }
  153. }
  154. else { /* GREYSCALE IMAGE */
  155. for (row = 0; row < w.rows; row++) {
  156. G_percent(row, w.rows, 5);
  157. Rast_get_row(cellfile, (void *)voidc, row, rtype);
  158. Rast_lookup_colors((void *)voidc, ored, ogrn, oblu, set,
  159. w.cols, &colors, rtype);
  160. for (col = 0; col < w.cols; col++) {
  161. #ifdef XV_STYLE
  162. /*.33R+ .5G+ .17B */
  163. putc((((ored[col]) * 11 + (ogrn[col]) * 16
  164. + (oblu[col]) * 5) >> 5), fp);
  165. #else
  166. /*NTSC Y equation: .30R+ .59G+ .11B */
  167. putc((((ored[col]) * 19 + (ogrn[col]) * 38
  168. + (oblu[col]) * 7) >> 6), fp);
  169. #endif
  170. }
  171. }
  172. }
  173. Rast_free_colors(&colors);
  174. }
  175. G_free(cell_buf);
  176. G_free(fcell_buf);
  177. G_free(dcell_buf);
  178. G_free(ored);
  179. G_free(ogrn);
  180. G_free(oblu);
  181. G_free(set);
  182. Rast_close(cellfile);
  183. /*
  184. if(!do_stdout)
  185. */
  186. fclose(fp);
  187. if (do_stdout)
  188. G_done_msg("");
  189. else
  190. G_done_msg(_("File <%s> created"), ofile);
  191. return (EXIT_SUCCESS);
  192. }