do_masking.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Function: do_masking
  2. **
  3. ** Author: Paul W. Carlson May 1992
  4. */
  5. #include <stdlib.h>
  6. #include <unistd.h>
  7. #include <grass/gis.h>
  8. #include <grass/glocale.h>
  9. #include "ps_info.h"
  10. extern char *ps_mask_file;
  11. int do_masking(void)
  12. {
  13. FILE *ps_mask_fp;
  14. int rows, cols;
  15. double factor, width;
  16. char buf[128];
  17. /* open the temporary mask file */
  18. if ((ps_mask_fp = fopen(ps_mask_file, "r")) == NULL)
  19. G_fatal_error(_("Can't open temporary PostScript mask file."));
  20. /* adjust columns to multiple of 8 */
  21. rows = Rast_window_rows();
  22. cols = Rast_window_cols();
  23. while (cols % 8)
  24. cols++;
  25. factor = (double)cols / (double)Rast_window_cols();
  26. width = factor * PS.map_pix_wide;
  27. /* write mask to PostScript file, using "no data" color */
  28. fprintf(PS.fp, "gsave\n");
  29. fprintf(PS.fp, "/imgstrg %d string def\n", cols / 8);
  30. fprintf(PS.fp, "/cw %d def /ch %d def\n", cols, rows);
  31. fprintf(PS.fp, "%.2f %.2f TR\n", PS.map_left, PS.map_bot);
  32. fprintf(PS.fp, "%d %d scale\n",
  33. (int)(width + 0.5), (int)(PS.map_pix_high + 0.5));
  34. if (PS.mask_color == 1) {
  35. fprintf(PS.fp, "%.3f %.3f %.3f C\n", PS.mask_r, PS.mask_g, PS.mask_b);
  36. }
  37. else {
  38. fprintf(PS.fp, "%.3f %.3f %.3f C\n", PS.r0, PS.g0, PS.b0);
  39. }
  40. fprintf(PS.fp, "cw ch true\n");
  41. fprintf(PS.fp, "[cw 0 0 ch neg 0 ch]\n");
  42. fprintf(PS.fp, "{currentfile imgstrg readhexstring pop}\n");
  43. fprintf(PS.fp, "imagemask\n");
  44. while (fgets(buf, 128, ps_mask_fp) != NULL)
  45. fprintf(PS.fp, "%s", buf);
  46. fprintf(PS.fp, "grestore\n");
  47. /* close and remove temporary mask file */
  48. fclose(ps_mask_fp);
  49. unlink(ps_mask_file);
  50. return 0;
  51. }