raster.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include <math.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include "format.h"
  5. #include "local_proto.h"
  6. struct Cell_head region, page;
  7. static union
  8. {
  9. char **c;
  10. unsigned char **u;
  11. short **s;
  12. CELL **cell;
  13. FCELL **fcell;
  14. DCELL **dcell;
  15. } raster;
  16. static int max_rows;
  17. static int at_row;
  18. static int cat_int;
  19. static double cat_double;
  20. static int cur_x, cur_y;
  21. static int format;
  22. static CELL *cell;
  23. static int (*dot) ();
  24. static int cell_dot(int, int);
  25. static int fcell_dot(int, int);
  26. static int dcell_dot(int, int);
  27. static int uchar_dot(int, int);
  28. static int char_dot(int, int);
  29. static int short_dot(int, int);
  30. static int move(int, int);
  31. static int cont(int, int);
  32. int begin_rasterization(int nrows, int f)
  33. {
  34. int i, size;
  35. int pages;
  36. format = f;
  37. max_rows = nrows;
  38. if (max_rows <= 0)
  39. max_rows = 512;
  40. Rast_get_window(&region);
  41. Rast_get_window(&page);
  42. pages = (region.rows + max_rows - 1) / max_rows;
  43. if (max_rows > region.rows)
  44. max_rows = region.rows;
  45. size = max_rows * region.cols;
  46. switch (format) {
  47. case USE_CHAR:
  48. raster.c = (char **)G_calloc(max_rows, sizeof(char *));
  49. raster.c[0] = (char *)G_calloc(size, sizeof(char));
  50. for (i = 1; i < max_rows; i++)
  51. raster.c[i] = raster.c[i - 1] + region.cols;
  52. dot = char_dot;
  53. break;
  54. case USE_UCHAR:
  55. raster.u =
  56. (unsigned char **)G_calloc(max_rows, sizeof(unsigned char *));
  57. raster.u[0] = (unsigned char *)G_calloc(size, sizeof(unsigned char));
  58. for (i = 1; i < max_rows; i++)
  59. raster.u[i] = raster.u[i - 1] + region.cols;
  60. dot = uchar_dot;
  61. break;
  62. case USE_SHORT:
  63. raster.s = (short **)G_calloc(max_rows, sizeof(short *));
  64. raster.s[0] = (short *)G_calloc(size, sizeof(short));
  65. for (i = 1; i < max_rows; i++)
  66. raster.s[i] = raster.s[i - 1] + region.cols;
  67. dot = short_dot;
  68. break;
  69. case USE_CELL:
  70. raster.cell = (CELL **) G_calloc(max_rows, sizeof(CELL *));
  71. raster.cell[0] = (CELL *) G_calloc(size, sizeof(CELL));
  72. for (i = 1; i < max_rows; i++)
  73. raster.cell[i] = raster.cell[i - 1] + region.cols;
  74. dot = cell_dot;
  75. break;
  76. case USE_FCELL:
  77. raster.fcell = (FCELL **) G_calloc(max_rows, sizeof(FCELL *));
  78. raster.fcell[0] = (FCELL *) G_calloc(size, sizeof(FCELL));
  79. for (i = 1; i < max_rows; i++)
  80. raster.fcell[i] = raster.fcell[i - 1] + region.cols;
  81. dot = fcell_dot;
  82. break;
  83. case USE_DCELL:
  84. raster.dcell = (DCELL **) G_calloc(max_rows, sizeof(DCELL *));
  85. raster.dcell[0] = (DCELL *) G_calloc(size, sizeof(DCELL));
  86. for (i = 1; i < max_rows; i++)
  87. raster.dcell[i] = raster.dcell[i - 1] + region.cols;
  88. dot = dcell_dot;
  89. break;
  90. }
  91. if (format != USE_CELL && format != USE_FCELL && format != USE_DCELL)
  92. cell = Rast_allocate_c_buf();
  93. at_row = 0;
  94. configure_plot();
  95. return pages;
  96. }
  97. #define DONE 1
  98. #define ERROR -1
  99. #define AGAIN 0
  100. int configure_plot(void)
  101. {
  102. int i, j;
  103. int nrows;
  104. int ncols;
  105. nrows = region.rows - at_row;
  106. if (nrows <= 0)
  107. return DONE;
  108. if (nrows > max_rows)
  109. nrows = max_rows;
  110. ncols = region.cols;
  111. /* zero the raster */
  112. switch (format) {
  113. case USE_CHAR:
  114. for (i = 0; i < nrows; i++)
  115. for (j = 0; j < ncols; j++)
  116. raster.c[i][j] = 0;
  117. break;
  118. case USE_UCHAR:
  119. for (i = 0; i < nrows; i++)
  120. for (j = 0; j < ncols; j++)
  121. raster.u[i][j] = 0;
  122. break;
  123. case USE_SHORT:
  124. for (i = 0; i < nrows; i++)
  125. for (j = 0; j < ncols; j++)
  126. raster.s[i][j] = 0;
  127. break;
  128. case USE_CELL:
  129. for (i = 0; i < nrows; i++)
  130. Rast_set_c_null_value(raster.cell[i], ncols);
  131. break;
  132. case USE_FCELL:
  133. for (i = 0; i < nrows; i++)
  134. Rast_set_f_null_value(raster.fcell[i], ncols);
  135. break;
  136. case USE_DCELL:
  137. for (i = 0; i < nrows; i++)
  138. Rast_set_d_null_value(raster.dcell[i], ncols);
  139. break;
  140. }
  141. /* change the region */
  142. page.north = region.north - at_row * region.ns_res;
  143. page.south = page.north - nrows * region.ns_res;
  144. /* Rast_set_[inpu|output]_window not working but G_set_window ??? */
  145. G_set_window(&page);
  146. /* configure the plot routines */
  147. G_setup_plot(-0.5, page.rows - 0.5, -0.5, page.cols - 0.5, move, cont);
  148. return AGAIN;
  149. }
  150. int output_raster(int fd, int *null)
  151. {
  152. int i, j;
  153. FCELL *fcell;
  154. DCELL *dcell;
  155. for (i = 0; i < page.rows; i++, at_row++) {
  156. switch (format) {
  157. case USE_CHAR:
  158. for (j = 0; j < page.cols; j++) {
  159. cell[j] = (CELL) raster.c[i][j];
  160. if (cell[j] == 0)
  161. Rast_set_null_value(&cell[j], 1, CELL_TYPE);
  162. }
  163. break;
  164. case USE_UCHAR:
  165. for (j = 0; j < page.cols; j++) {
  166. cell[j] = (CELL) raster.u[i][j];
  167. if (cell[j] == 0)
  168. Rast_set_null_value(&cell[j], 1, CELL_TYPE);
  169. }
  170. break;
  171. case USE_SHORT:
  172. for (j = 0; j < page.cols; j++) {
  173. cell[j] = (CELL) raster.s[i][j];
  174. if (cell[j] == 0)
  175. Rast_set_null_value(&cell[j], 1, CELL_TYPE);
  176. }
  177. break;
  178. case USE_CELL:
  179. cell = raster.cell[i];
  180. if (!null)
  181. break;
  182. for (j = 0; j < page.cols; j++) {
  183. if (cell[j] == *null)
  184. Rast_set_null_value(&cell[j], 1, CELL_TYPE);
  185. }
  186. break;
  187. case USE_FCELL:
  188. fcell = raster.fcell[i];
  189. if (!null)
  190. break;
  191. for (j = 0; j < page.cols; j++) {
  192. if (fabs(fcell[j] - (FCELL) *null) < 1e-6)
  193. Rast_set_null_value(&fcell[j], 1, FCELL_TYPE);
  194. }
  195. break;
  196. case USE_DCELL:
  197. dcell = raster.dcell[i];
  198. if (!null)
  199. break;
  200. for (j = 0; j < page.cols; j++) {
  201. if (fabs(dcell[j] - (DCELL) *null) < 1e-6)
  202. Rast_set_null_value(&dcell[j], 1, DCELL_TYPE);
  203. }
  204. break;
  205. }
  206. switch (format) {
  207. case USE_CHAR:
  208. case USE_UCHAR:
  209. case USE_SHORT:
  210. case USE_CELL:
  211. Rast_put_row(fd, cell, CELL_TYPE);
  212. break;
  213. case USE_FCELL:
  214. Rast_put_row(fd, fcell, FCELL_TYPE);
  215. break;
  216. case USE_DCELL:
  217. Rast_put_row(fd, dcell, DCELL_TYPE);
  218. break;
  219. }
  220. G_percent(i, page.rows, 2);
  221. }
  222. G_percent(i, page.rows, 2);
  223. return configure_plot();
  224. }
  225. int set_cat_int(int x)
  226. {
  227. cat_int = x;
  228. return 0;
  229. }
  230. int set_cat_double(double x)
  231. {
  232. cat_double = x;
  233. return 0;
  234. }
  235. int raster_dot(int x, int y)
  236. {
  237. dot(x, y);
  238. return 0;
  239. }
  240. static int move(int x, int y)
  241. {
  242. cur_x = x;
  243. cur_y = y;
  244. return 0;
  245. }
  246. static int cont(int x, int y)
  247. {
  248. if (cur_x < 0 && x < 0)
  249. goto set;
  250. if (cur_y < 0 && y < 0)
  251. goto set;
  252. if (cur_x >= page.cols && x >= page.cols)
  253. goto set;
  254. if (cur_y >= page.rows && y >= page.rows)
  255. goto set;
  256. G_bresenham_line(cur_x, cur_y, x, y, dot);
  257. set:
  258. move(x, y);
  259. return 0;
  260. }
  261. static int cell_dot(int x, int y)
  262. {
  263. if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
  264. raster.cell[y][x] = (CELL) cat_int;
  265. return 0;
  266. }
  267. static int fcell_dot(int x, int y)
  268. {
  269. if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
  270. raster.fcell[y][x] = (FCELL) cat_double;
  271. return 0;
  272. }
  273. static int dcell_dot(int x, int y)
  274. {
  275. if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
  276. raster.dcell[y][x] = (DCELL) cat_double;
  277. return 0;
  278. }
  279. static int uchar_dot(int x, int y)
  280. {
  281. if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
  282. raster.u[y][x] = cat_int;
  283. return 0;
  284. }
  285. static int char_dot(int x, int y)
  286. {
  287. if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
  288. raster.c[y][x] = cat_int;
  289. return 0;
  290. }
  291. static int short_dot(int x, int y)
  292. {
  293. if (x >= 0 && x < page.cols && y >= 0 && y < page.rows)
  294. raster.s[y][x] = cat_int;
  295. return 0;
  296. }