recode.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #include <stdio.h>
  2. #include <grass/raster.h>
  3. #include "global.h"
  4. #define F2I(map_type) \
  5. (map_type == CELL_TYPE ? 0 : (map_type == FCELL_TYPE ? 1 : 2))
  6. static void process_row_ii(int), process_row_if(int), process_row_id(int);
  7. static void process_row_fi(int), process_row_ff(int), process_row_fd(int);
  8. static void process_row_di(int), process_row_df(int), process_row_dd(int);
  9. static void (*process_row_FtypeOtype[3][3]) () = { {
  10. process_row_ii, process_row_if, process_row_id}, {
  11. process_row_fi, process_row_ff, process_row_fd}, {
  12. process_row_di, process_row_df, process_row_dd}};
  13. #define PROCESS_ROW \
  14. (process_row_FtypeOtype [F2I (in_type)] [F2I (out_type)])
  15. static void *in_rast, *out_rast;
  16. static int nrows, ncols;
  17. int do_recode(void)
  18. {
  19. struct Cell_head window, cellhd;
  20. int row, i;
  21. struct History hist;
  22. /* set the window from the header for the input file */
  23. if (align_wind) {
  24. G_get_window(&window);
  25. Rast_get_cellhd(name, "", &cellhd);
  26. Rast_align_window(&window, &cellhd);
  27. Rast_set_window(&window);
  28. }
  29. G_get_set_window(&window);
  30. nrows = Rast_window_rows();
  31. ncols = Rast_window_cols();
  32. /* open the input file for reading */
  33. in_fd = Rast_open_old(name, "");
  34. out_fd = Rast_open_new(result, out_type);
  35. out_rast = Rast_allocate_buf(out_type);
  36. in_rast = Rast_allocate_buf(in_type);
  37. for (row = 0; row < nrows; row++) {
  38. G_percent(row, nrows, 2);
  39. PROCESS_ROW(row);
  40. }
  41. G_percent(row, nrows, 2);
  42. Rast_close(in_fd);
  43. Rast_close(out_fd);
  44. /* writing history file */
  45. Rast_short_history(result, "raster", &hist);
  46. Rast_append_format_history(&hist, "recode of raster map %s", name);
  47. /* if there are more rules than history lines allocated, write only
  48. MAXEDLINES-1 rules , and "...." as a last rule */
  49. for (i = 0; i < nrules && i < 50; i++)
  50. Rast_append_history(&hist, rules[i]);
  51. if (nrules > 50)
  52. Rast_append_history(&hist, "...");
  53. Rast_format_history(&hist, HIST_DATSRC_1, "raster map %s", name);
  54. Rast_write_history(result, &hist);
  55. return 0;
  56. }
  57. static void process_row_ii(int row)
  58. {
  59. if (no_mask)
  60. Rast_get_c_row_nomask(in_fd, (CELL *) in_rast, row);
  61. else
  62. Rast_get_c_row(in_fd, (CELL *) in_rast, row);
  63. Rast_fpreclass_perform_ii(&rcl_struct, (CELL *) in_rast, (CELL *) out_rast,
  64. ncols);
  65. Rast_put_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  66. }
  67. static void process_row_if(int row)
  68. {
  69. if (no_mask)
  70. Rast_get_c_row_nomask(in_fd, (CELL *) in_rast, row);
  71. else
  72. Rast_get_c_row(in_fd, (CELL *) in_rast, row);
  73. Rast_fpreclass_perform_if(&rcl_struct, (CELL *) in_rast, (FCELL *) out_rast,
  74. ncols);
  75. Rast_put_f_row(out_fd, (FCELL *) out_rast);
  76. }
  77. static void process_row_id(int row)
  78. {
  79. if (no_mask)
  80. Rast_get_c_row_nomask(in_fd, (CELL *) in_rast, row);
  81. else
  82. Rast_get_c_row(in_fd, (CELL *) in_rast, row);
  83. Rast_fpreclass_perform_id(&rcl_struct, (CELL *) in_rast, (DCELL *) out_rast,
  84. ncols);
  85. Rast_put_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  86. }
  87. static void process_row_fi(int row)
  88. {
  89. if (no_mask)
  90. Rast_get_f_row_nomask(in_fd, (FCELL *) in_rast, row);
  91. else
  92. Rast_get_f_row(in_fd, (FCELL *) in_rast, row);
  93. Rast_fpreclass_perform_fi(&rcl_struct, (FCELL *) in_rast, (CELL *) out_rast,
  94. ncols);
  95. Rast_put_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  96. }
  97. static void process_row_ff(int row)
  98. {
  99. if (no_mask)
  100. Rast_get_f_row_nomask(in_fd, (FCELL *) in_rast, row);
  101. else
  102. Rast_get_f_row(in_fd, (FCELL *) in_rast, row);
  103. Rast_fpreclass_perform_ff(&rcl_struct, (FCELL *) in_rast, (FCELL *) out_rast,
  104. ncols);
  105. Rast_put_f_row(out_fd, (FCELL *) out_rast);
  106. }
  107. static void process_row_fd(int row)
  108. {
  109. if (no_mask)
  110. Rast_get_f_row_nomask(in_fd, (FCELL *) in_rast, row);
  111. else
  112. Rast_get_f_row(in_fd, (FCELL *) in_rast, row);
  113. Rast_fpreclass_perform_fd(&rcl_struct, (FCELL *) in_rast, (DCELL *) out_rast,
  114. ncols);
  115. Rast_put_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  116. }
  117. static void process_row_di(int row)
  118. {
  119. if (no_mask)
  120. Rast_get_d_row_nomask(in_fd, (DCELL *) in_rast, row);
  121. else
  122. Rast_get_d_row(in_fd, (DCELL *) in_rast, row);
  123. Rast_fpreclass_perform_di(&rcl_struct, (DCELL *) in_rast, (CELL *) out_rast,
  124. ncols);
  125. Rast_put_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  126. }
  127. static void process_row_df(int row)
  128. {
  129. if (no_mask)
  130. Rast_get_d_row_nomask(in_fd, (DCELL *) in_rast, row);
  131. else
  132. Rast_get_d_row(in_fd, (DCELL *) in_rast, row);
  133. Rast_fpreclass_perform_df(&rcl_struct, (DCELL *) in_rast, (FCELL *) out_rast,
  134. ncols);
  135. Rast_put_f_row(out_fd, (FCELL *) out_rast);
  136. }
  137. static void process_row_dd(int row)
  138. {
  139. if (no_mask)
  140. Rast_get_d_row_nomask(in_fd, (DCELL *) in_rast, row);
  141. else
  142. Rast_get_d_row(in_fd, (DCELL *) in_rast, row);
  143. Rast_fpreclass_perform_dd(&rcl_struct, (DCELL *) in_rast, (DCELL *) out_rast,
  144. ncols);
  145. Rast_put_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  146. }