main.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. char *tmpstr1, *tmpstr2;
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("raster"));
  46. G_add_keyword(_("export"));
  47. module->description = _("Converts a GRASS raster map to a PPM image file.");
  48. rast = G_define_standard_option(G_OPT_R_INPUT);
  49. ppm_file = G_define_standard_option(G_OPT_F_OUTPUT);
  50. ppm_file->required = NO;
  51. ppm_file->answer = "<rasterfilename>.ppm";
  52. ppm_file->description = _("Name for new PPM file (use '-' for stdout)");
  53. gscale = G_define_flag();
  54. gscale->key = 'g';
  55. gscale->description = _("Output greyscale instead of color");
  56. header = G_define_flag();
  57. header->key = 'h';
  58. header->description = _("Suppress printing of PPM header");
  59. if (G_parser(argc, argv))
  60. exit(EXIT_FAILURE);
  61. /* kludge to work with r.out.mpeg */
  62. if (rast->answer[0] == '/')
  63. rast->answer++;
  64. if (strcmp(ppm_file->answer, "<rasterfilename>.ppm")) {
  65. if (strcmp(ppm_file->answer, "-"))
  66. strcpy(ofile, ppm_file->answer);
  67. else
  68. do_stdout = 1;
  69. }
  70. else {
  71. map = p = rast->answer;
  72. /* knock off any GRASS location suffix */
  73. if ((char *)NULL != (p = strrchr(map, '@'))) {
  74. if (p != map)
  75. *p = '\0';
  76. }
  77. strcpy(ofile, map);
  78. strcat(ofile, ".ppm");
  79. }
  80. /*G_get_set_window (&w); *//* 10/99 MN: check for current region */
  81. G_get_window(&w);
  82. G_asprintf(&tmpstr1, _n("row = %d", "rows = %d", w.rows), w.rows);
  83. G_asprintf(&tmpstr2, _n("column = %d", "columns = %d", w.cols), w.cols);
  84. G_message("%s, %s", tmpstr1, tmpstr2);
  85. G_free(tmpstr1);
  86. G_free(tmpstr2);
  87. /* open raster map for reading */
  88. cellfile = Rast_open_old(rast->answer, "");
  89. cell_buf = Rast_allocate_c_buf();
  90. fcell_buf = Rast_allocate_f_buf();
  91. dcell_buf = Rast_allocate_d_buf();
  92. ored = G_malloc(w.cols);
  93. ogrn = G_malloc(w.cols);
  94. oblu = G_malloc(w.cols);
  95. set = G_malloc(w.cols);
  96. /* open ppm file for writing */
  97. {
  98. if (do_stdout)
  99. fp = stdout;
  100. else if (NULL == (fp = fopen(ofile, "w"))) {
  101. G_fatal_error(_("Unable to open file <%s> for write"), ofile);
  102. }
  103. }
  104. /* write header info */
  105. if (!header->answer) {
  106. if (!gscale->answer)
  107. fprintf(fp, "P6\n");
  108. /* Magic number meaning rawbits, 24bit color to ppm format */
  109. else
  110. fprintf(fp, "P5\n");
  111. /* Magic number meaning rawbits, 8bit greyscale to ppm format */
  112. if (!do_stdout) {
  113. fprintf(fp, "# CREATOR: %s from GRASS raster map \"%s\"\n",
  114. G_program_name(), rast->answer);
  115. fprintf(fp, "# east-west resolution: %f\n", w.ew_res);
  116. fprintf(fp, "# north-south resolution: %f\n", w.ns_res);
  117. fprintf(fp, "# South edge: %f\n", w.south);
  118. fprintf(fp, "# West edge: %f\n", w.west);
  119. /* comments */
  120. }
  121. fprintf(fp, "%d %d\n", w.cols, w.rows);
  122. /* width & height */
  123. fprintf(fp, "255\n");
  124. /* max intensity val */
  125. }
  126. G_important_message(_("Converting..."));
  127. {
  128. struct Colors colors;
  129. Rast_read_colors(rast->answer, "", &colors);
  130. rtype = Rast_get_map_type(cellfile);
  131. if (rtype == CELL_TYPE)
  132. voidc = (CELL *) cell_buf;
  133. else if (rtype == FCELL_TYPE)
  134. voidc = (FCELL *) fcell_buf;
  135. else if (rtype == DCELL_TYPE)
  136. voidc = (DCELL *) dcell_buf;
  137. else
  138. exit(1);
  139. if (!gscale->answer) { /* 24BIT COLOR IMAGE */
  140. for (row = 0; row < w.rows; row++) {
  141. G_percent(row, w.rows, 5);
  142. Rast_get_row(cellfile, (void *)voidc, row, rtype);
  143. Rast_lookup_colors((void *)voidc, ored, ogrn, oblu, set,
  144. w.cols, &colors, rtype);
  145. for (col = 0; col < w.cols; col++) {
  146. if (set[col]) {
  147. putc(ored[col], fp);
  148. putc(ogrn[col], fp);
  149. putc(oblu[col], fp);
  150. }
  151. else {
  152. putc(DEF_RED, fp);
  153. putc(DEF_GRN, fp);
  154. putc(DEF_BLU, fp);
  155. }
  156. }
  157. }
  158. }
  159. else { /* GREYSCALE IMAGE */
  160. for (row = 0; row < w.rows; row++) {
  161. G_percent(row, w.rows, 5);
  162. Rast_get_row(cellfile, (void *)voidc, row, rtype);
  163. Rast_lookup_colors((void *)voidc, ored, ogrn, oblu, set,
  164. w.cols, &colors, rtype);
  165. for (col = 0; col < w.cols; col++) {
  166. #ifdef XV_STYLE
  167. /*.33R+ .5G+ .17B */
  168. putc((((ored[col]) * 11 + (ogrn[col]) * 16
  169. + (oblu[col]) * 5) >> 5), fp);
  170. #else
  171. /*NTSC Y equation: .30R+ .59G+ .11B */
  172. putc((((ored[col]) * 19 + (ogrn[col]) * 38
  173. + (oblu[col]) * 7) >> 6), fp);
  174. #endif
  175. }
  176. }
  177. }
  178. Rast_free_colors(&colors);
  179. }
  180. G_free(cell_buf);
  181. G_free(fcell_buf);
  182. G_free(dcell_buf);
  183. G_free(ored);
  184. G_free(ogrn);
  185. G_free(oblu);
  186. G_free(set);
  187. Rast_close(cellfile);
  188. /*
  189. if(!do_stdout)
  190. */
  191. fclose(fp);
  192. if (do_stdout)
  193. G_done_msg("%s", "");
  194. else
  195. G_done_msg(_("File <%s> created"), ofile);
  196. return (EXIT_SUCCESS);
  197. }