recode.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_command_history(&hist);
  55. Rast_write_history(result, &hist);
  56. return 0;
  57. }
  58. static void process_row_ii(int row)
  59. {
  60. if (no_mask)
  61. Rast_get_c_row_nomask(in_fd, (CELL *) in_rast, row);
  62. else
  63. Rast_get_c_row(in_fd, (CELL *) in_rast, row);
  64. Rast_fpreclass_perform_ii(&rcl_struct, (CELL *) in_rast, (CELL *) out_rast,
  65. ncols);
  66. Rast_put_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  67. }
  68. static void process_row_if(int row)
  69. {
  70. if (no_mask)
  71. Rast_get_c_row_nomask(in_fd, (CELL *) in_rast, row);
  72. else
  73. Rast_get_c_row(in_fd, (CELL *) in_rast, row);
  74. Rast_fpreclass_perform_if(&rcl_struct, (CELL *) in_rast, (FCELL *) out_rast,
  75. ncols);
  76. Rast_put_f_row(out_fd, (FCELL *) out_rast);
  77. }
  78. static void process_row_id(int row)
  79. {
  80. if (no_mask)
  81. Rast_get_c_row_nomask(in_fd, (CELL *) in_rast, row);
  82. else
  83. Rast_get_c_row(in_fd, (CELL *) in_rast, row);
  84. Rast_fpreclass_perform_id(&rcl_struct, (CELL *) in_rast, (DCELL *) out_rast,
  85. ncols);
  86. Rast_put_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  87. }
  88. static void process_row_fi(int row)
  89. {
  90. if (no_mask)
  91. Rast_get_f_row_nomask(in_fd, (FCELL *) in_rast, row);
  92. else
  93. Rast_get_f_row(in_fd, (FCELL *) in_rast, row);
  94. Rast_fpreclass_perform_fi(&rcl_struct, (FCELL *) in_rast, (CELL *) out_rast,
  95. ncols);
  96. Rast_put_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  97. }
  98. static void process_row_ff(int row)
  99. {
  100. if (no_mask)
  101. Rast_get_f_row_nomask(in_fd, (FCELL *) in_rast, row);
  102. else
  103. Rast_get_f_row(in_fd, (FCELL *) in_rast, row);
  104. Rast_fpreclass_perform_ff(&rcl_struct, (FCELL *) in_rast, (FCELL *) out_rast,
  105. ncols);
  106. Rast_put_f_row(out_fd, (FCELL *) out_rast);
  107. }
  108. static void process_row_fd(int row)
  109. {
  110. if (no_mask)
  111. Rast_get_f_row_nomask(in_fd, (FCELL *) in_rast, row);
  112. else
  113. Rast_get_f_row(in_fd, (FCELL *) in_rast, row);
  114. Rast_fpreclass_perform_fd(&rcl_struct, (FCELL *) in_rast, (DCELL *) out_rast,
  115. ncols);
  116. Rast_put_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  117. }
  118. static void process_row_di(int row)
  119. {
  120. if (no_mask)
  121. Rast_get_d_row_nomask(in_fd, (DCELL *) in_rast, row);
  122. else
  123. Rast_get_d_row(in_fd, (DCELL *) in_rast, row);
  124. Rast_fpreclass_perform_di(&rcl_struct, (DCELL *) in_rast, (CELL *) out_rast,
  125. ncols);
  126. Rast_put_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  127. }
  128. static void process_row_df(int row)
  129. {
  130. if (no_mask)
  131. Rast_get_d_row_nomask(in_fd, (DCELL *) in_rast, row);
  132. else
  133. Rast_get_d_row(in_fd, (DCELL *) in_rast, row);
  134. Rast_fpreclass_perform_df(&rcl_struct, (DCELL *) in_rast, (FCELL *) out_rast,
  135. ncols);
  136. Rast_put_f_row(out_fd, (FCELL *) out_rast);
  137. }
  138. static void process_row_dd(int row)
  139. {
  140. if (no_mask)
  141. Rast_get_d_row_nomask(in_fd, (DCELL *) in_rast, row);
  142. else
  143. Rast_get_d_row(in_fd, (DCELL *) in_rast, row);
  144. Rast_fpreclass_perform_dd(&rcl_struct, (DCELL *) in_rast, (DCELL *) out_rast,
  145. ncols);
  146. Rast_put_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  147. }