main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /****************************************************************************
  2. *
  3. * MODULE: r.out.ppm3
  4. * AUTHOR(S): Glynn Clements <glynn gclements.plus.com> (original contributor)
  5. * Jachym Cepicky <jachym les-ejk.cz>, Markus Neteler <neteler itc.it>
  6. * PURPOSE: Use to convert 3 grass raster layers (R,G,B) to PPM
  7. * uses currently selected region
  8. * COPYRIGHT: (C) 2001-2006 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 <string.h>
  16. #include <stdlib.h>
  17. #include <grass/gis.h>
  18. #include <grass/raster.h>
  19. #include <grass/glocale.h>
  20. #define DEF_RED 255
  21. #define DEF_GRN 255
  22. #define DEF_BLU 255
  23. struct band
  24. {
  25. struct Option *opt;
  26. int file;
  27. int type;
  28. void *array;
  29. struct Colors colors;
  30. unsigned char *buf;
  31. unsigned char *mask;
  32. };
  33. static char *const color_names[3] = { "red", "green", "blue" };
  34. int main(int argc, char **argv)
  35. {
  36. struct band B[3];
  37. struct GModule *module;
  38. struct Option *ppm_file;
  39. struct Flag *comment;
  40. struct Cell_head w;
  41. FILE *fp;
  42. unsigned char *dummy;
  43. int row, col;
  44. int i;
  45. char *tmpstr1, *tmpstr2;
  46. G_gisinit(argv[0]);
  47. module = G_define_module();
  48. G_add_keyword(_("raster"));
  49. G_add_keyword(_("export"));
  50. module->description = _("Converts 3 GRASS raster layers (R,G,B) to a PPM image file.");
  51. for (i = 0; i < 3; i++) {
  52. char buff[80];
  53. sprintf(buff, _("Name of raster map to be used for <%s>"),
  54. color_names[i]);
  55. B[i].opt = G_define_option();
  56. B[i].opt->key = G_store(color_names[i]);
  57. B[i].opt->type = TYPE_STRING;
  58. B[i].opt->answer = NULL;
  59. B[i].opt->required = YES;
  60. B[i].opt->multiple = NO;
  61. B[i].opt->gisprompt = "old,cell,raster";
  62. B[i].opt->description = G_store(buff);
  63. }
  64. ppm_file = G_define_option();
  65. ppm_file->key = "output";
  66. ppm_file->type = TYPE_STRING;
  67. ppm_file->required = YES;
  68. ppm_file->multiple = NO;
  69. ppm_file->answer = NULL;
  70. ppm_file->description =
  71. _("Name for new PPM file. (use '-' for stdout)");
  72. comment = G_define_flag();
  73. comment->key = 'c';
  74. comment->description = _("Add comments to describe the region");
  75. if (G_parser(argc, argv))
  76. exit(EXIT_FAILURE);
  77. G_get_window(&w);
  78. G_asprintf(&tmpstr1, _n("row = %d", "rows = %d", w.rows), w.rows);
  79. /* GTC Raster columns */
  80. G_asprintf(&tmpstr2, _n("column = %d", "columns = %d", w.cols), w.cols);
  81. G_message("%s, %s", tmpstr1, tmpstr2);
  82. G_free(tmpstr1);
  83. G_free(tmpstr2);
  84. /* open raster map for reading */
  85. for (i = 0; i < 3; i++) {
  86. /* Get name of layer */
  87. char *name = B[i].opt->answer;
  88. /* Open raster map */
  89. B[i].file = Rast_open_old(name, "");
  90. /* Get map type (CELL/FCELL/DCELL) */
  91. B[i].type = Rast_get_map_type(B[i].file);
  92. /* Get color table */
  93. if (Rast_read_colors(name, "", &B[i].colors) == -1)
  94. G_fatal_error(_("Color file for <%s> not available"), name);
  95. /* Allocate input buffer */
  96. B[i].array = Rast_allocate_buf(B[i].type);
  97. /* Allocate output buffers */
  98. B[i].buf = (unsigned char *)G_malloc(w.cols);
  99. B[i].mask = (unsigned char *)G_malloc(w.cols);
  100. }
  101. dummy = (unsigned char *)G_malloc(w.cols);
  102. /* open PPM file for writing */
  103. if (strcmp(ppm_file->answer, "-") == 0)
  104. fp = stdout;
  105. else {
  106. fp = fopen(ppm_file->answer, "w");
  107. if (!fp)
  108. G_fatal_error(_("Unable to open file <%s>"), ppm_file->answer);
  109. }
  110. /* write header info */
  111. /* Magic number meaning rawbits, 24bit color to PPM format */
  112. fprintf(fp, "P6\n");
  113. /* comments */
  114. if (comment->answer) {
  115. fprintf(fp, "# CREATOR: r.out.ppm3 (from GRASS)\n");
  116. fprintf(fp, "# Red: %s\n", B[0].opt->answer);
  117. fprintf(fp, "# Green: %s\n", B[1].opt->answer);
  118. fprintf(fp, "# Blue: %s\n", B[2].opt->answer);
  119. fprintf(fp, "# Projection: %s (Zone: %d)\n",
  120. G_database_projection_name(), G_zone());
  121. fprintf(fp, "# N=%f, S=%f, E=%f, W=%f\n",
  122. w.north, w.south, w.east, w.west);
  123. fprintf(fp, "# N/S Res: %f, E/W Res: %f\n", w.ns_res, w.ew_res);
  124. }
  125. /* width & height */
  126. fprintf(fp, "%d %d\n", w.cols, w.rows);
  127. /* max intensity val */
  128. fprintf(fp, "255\n");
  129. G_message(_("Converting ... "));
  130. for (row = 0; row < w.rows; row++) {
  131. G_percent(row, w.rows, 5);
  132. for (i = 0; i < 3; i++) {
  133. Rast_get_row(B[i].file, B[i].array, row, B[i].type);
  134. Rast_lookup_colors(B[i].array,
  135. (i == 0) ? B[i].buf : dummy,
  136. (i == 1) ? B[i].buf : dummy,
  137. (i == 2) ? B[i].buf : dummy,
  138. B[i].mask,
  139. w.cols, &B[i].colors, B[i].type);
  140. }
  141. for (col = 0; col < w.cols; col++) {
  142. if (B[0].mask && B[1].mask && B[2].mask) {
  143. putc(B[0].buf[col], fp);
  144. putc(B[1].buf[col], fp);
  145. putc(B[2].buf[col], fp);
  146. }
  147. else {
  148. putc(DEF_RED, fp);
  149. putc(DEF_GRN, fp);
  150. putc(DEF_BLU, fp);
  151. }
  152. }
  153. }
  154. fclose(fp);
  155. for (i = 0; i < 3; i++) {
  156. Rast_free_colors(&B[i].colors);
  157. G_free(B[i].array);
  158. G_free(B[i].buf);
  159. G_free(B[i].mask);
  160. Rast_close(B[i].file);
  161. }
  162. exit(EXIT_SUCCESS);
  163. }