ps_raster.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Functions: PS_raster_plot, ps_get_map_row
  2. **
  3. ** Author: Paul W. Carlson 3/92
  4. **
  5. ** ps_get_map_row is substituted by Rast_get_c_row_nomask
  6. ** writing mask file is done separately by function ps_write_mask_row
  7. ** which used code previously in ps_get_map_row. This is done because
  8. ** sometimes the raster map is not drawn, but we still need a mask
  9. ** so temporary mask file is created by new function PS_make_mask()
  10. ** which calls ps_write_mask_row()
  11. ** These changes are made by Olga Waupotitsch 4/94
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/raster.h>
  16. #include <grass/glocale.h>
  17. #include "local_proto.h"
  18. static FILE *ps_mask_fp;
  19. extern char *ps_mask_file;
  20. int PS_make_mask(void)
  21. {
  22. int row;
  23. CELL *maskbuf;
  24. int maskfd, r, g, b;
  25. maskfd = Rast_maskfd();
  26. if (maskfd < 0)
  27. /* there is no mask */
  28. {
  29. if (PS.mask_needed)
  30. PS.mask_needed = 0;
  31. return 0;
  32. }
  33. if (maskfd >= 0)
  34. maskbuf = Rast_allocate_c_buf();
  35. /* if masked, open a file to hold the PostScript mask data */
  36. if (maskfd >= 0 && PS.mask_needed) {
  37. if ((ps_mask_fp = fopen(ps_mask_file, "w")) == NULL)
  38. G_fatal_error(_("Can't create temporary PostScript mask file."));
  39. /* get no data rgb values for mask */
  40. Rast_get_null_value_color(&r, &g, &b, &PS.colors);
  41. PS.r0 = (double)r / 255.0;
  42. PS.g0 = (double)g / 255.0;
  43. PS.b0 = (double)b / 255.0;
  44. for (row = 0; row < PS.w.rows; row++) {
  45. Rast_get_c_row_nomask(maskfd, maskbuf, row);
  46. ps_write_mask_row(maskbuf);
  47. }
  48. fclose(ps_mask_fp);
  49. G_free(maskbuf);
  50. }
  51. return 0;
  52. }
  53. int PS_raster_plot(void)
  54. {
  55. int i, n, r, g, b, rr, gg, bb, row, col, doing_color;
  56. RASTER_MAP_TYPE map_type, grp_map_type[3];
  57. void *cellbuf = NULL, *cbuf[3], *ptr;
  58. if (!PS.do_raster && !grp.do_group)
  59. return 1;
  60. /* are we doing color? */
  61. doing_color = (PS.grey == 0 && PS.level == 2);
  62. /* save graphics state */
  63. fprintf(PS.fp, "gsave\n");
  64. /* make variables for cells_wide and cells_high */
  65. fprintf(PS.fp, "/cw %d def /ch %d def\n", PS.cells_wide, PS.cells_high);
  66. /* set lower left corner of map */
  67. fprintf(PS.fp, "%.2f %.2f TR\n", PS.map_left, PS.map_bot);
  68. /* mapping of image to map_pix_wide x map_pix_high unit rectangle */
  69. fprintf(PS.fp, "%d %d scale\n",
  70. (int)(PS.map_pix_wide + 0.5), (int)(PS.map_pix_high + 0.5));
  71. /* make strings to hold image RGB values */
  72. if (doing_color)
  73. fprintf(PS.fp, "/imgstrg cw 3 mul string def\n");
  74. else
  75. fprintf(PS.fp, "/imgstrg cw string def\n");
  76. fprintf(PS.fp, "cw ch 8\n");
  77. fprintf(PS.fp, "[cw 0 0 ch neg 0 ch]\n");
  78. fprintf(PS.fp, "{currentfile imgstrg readhexstring pop}\n");
  79. if (doing_color)
  80. fprintf(PS.fp, "false 3 colorimage\n");
  81. else
  82. fprintf(PS.fp, "image\n");
  83. /* let user know what's happenning */
  84. if (PS.do_raster)
  85. G_message(_("Reading raster map <%s>..."),
  86. G_fully_qualified_name(PS.cell_name, PS.cell_mapset));
  87. else
  88. G_message(_("Reading raster maps in group <%s>..."),
  89. grp.group_name);
  90. /* build the image RGB string */
  91. if (PS.do_raster) {
  92. map_type = Rast_get_map_type(PS.cell_fd);
  93. cellbuf = Rast_allocate_buf(map_type);
  94. n = 0;
  95. for (row = 0; row < PS.w.rows; row++) {
  96. Rast_get_row(PS.cell_fd, cellbuf, row, map_type);
  97. if ((row % PS.row_delta) == 0) {
  98. ptr = cellbuf;
  99. for (col = 0; col < PS.w.cols; col += PS.col_delta) {
  100. Rast_get_color(ptr, &r, &g, &b, &PS.colors, map_type);
  101. /* if color raster */
  102. if (doing_color) {
  103. fprintf(PS.fp, "%02X%02X%02X", r, g, b);
  104. if (++n == 13) {
  105. n = 0;
  106. fprintf(PS.fp, "\n");
  107. }
  108. }
  109. /* if grey raster */
  110. else {
  111. fprintf(PS.fp, "%02X",
  112. (int)(.3 * (double)r + .59 * (double)g +
  113. .11 * (double)b));
  114. if (++n == 39) {
  115. n = 0;
  116. fprintf(PS.fp, "\n");
  117. }
  118. }
  119. ptr =
  120. G_incr_void_ptr(ptr,
  121. Rast_cell_size(map_type) *
  122. PS.col_delta);
  123. }
  124. }
  125. }
  126. }
  127. else {
  128. void *cptr[3];
  129. for (i = 0; i < 3; i++) {
  130. grp_map_type[i] = Rast_get_map_type(grp.fd[i]);
  131. cbuf[i] = Rast_allocate_buf(grp_map_type[i]);
  132. }
  133. n = 0;
  134. for (row = 0; row < PS.w.rows; row++) {
  135. for (i = 0; i < 3; i++) {
  136. Rast_get_row(grp.fd[i], cbuf[i], row, grp_map_type[i]);
  137. cptr[i] = cbuf[i];
  138. }
  139. if ((row % PS.row_delta) == 0) {
  140. for (col = 0; col < PS.w.cols; col += PS.col_delta) {
  141. for (i = 0; i < 3; i++) {
  142. Rast_get_color(cptr[i], &rr, &gg, &bb,
  143. &(grp.colors[i]), grp_map_type[i]);
  144. if (i == 0)
  145. r = rr;
  146. if (i == 1)
  147. g = gg;
  148. if (i == 2)
  149. b = bb;
  150. cptr[i] = G_incr_void_ptr(cptr[i],
  151. Rast_cell_size(grp_map_type
  152. [0]) *
  153. PS.col_delta);
  154. }
  155. /* if color raster */
  156. if (doing_color) {
  157. fprintf(PS.fp, "%02X%02X%02X", r, g, b);
  158. if (++n == 13) {
  159. n = 0;
  160. fprintf(PS.fp, "\n");
  161. }
  162. }
  163. }
  164. }
  165. }
  166. }
  167. fprintf(PS.fp, "\n");
  168. /* we're done with the cell stuff */
  169. if (PS.do_raster) {
  170. if (!PS.do_colortable)
  171. Rast_free_colors(&PS.colors);
  172. Rast_close(PS.cell_fd);
  173. G_free(cellbuf);
  174. }
  175. else {
  176. for (i = 0; i < 3; i++) {
  177. Rast_free_colors(&(grp.colors[i]));
  178. Rast_close(grp.fd[i]);
  179. G_free(cbuf[i]);
  180. }
  181. I_free_group_ref(&grp.ref);
  182. }
  183. /* restore graphics state */
  184. fprintf(PS.fp, "grestore\n");
  185. return 0;
  186. }
  187. int ps_write_mask_row(register CELL * mask)
  188. {
  189. register int n;
  190. int i, j, byte;
  191. static int bit[] = { 128, 64, 32, 16, 8, 4, 2, 1 };
  192. i = 0;
  193. j = 0;
  194. byte = 0;
  195. n = PS.w.cols;
  196. while (n-- > 0) {
  197. if (*mask++ == 0)
  198. byte |= bit[i];
  199. i++;
  200. if (i == 8) {
  201. i = 0;
  202. j++;
  203. fprintf(ps_mask_fp, "%02X", byte);
  204. if (j == 39) {
  205. fprintf(ps_mask_fp, "\n");
  206. j = 0;
  207. }
  208. byte = 0;
  209. }
  210. }
  211. if (i) {
  212. while (i < 8) {
  213. if (*(mask - 1) == 0)
  214. byte |= bit[i];
  215. i++;
  216. }
  217. fprintf(ps_mask_fp, "%02X", byte);
  218. }
  219. fprintf(ps_mask_fp, "\n");
  220. return 0;
  221. }