recode.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #include <stdio.h>
  2. #include "global.h"
  3. #define F2I(map_type) \
  4. (map_type == CELL_TYPE ? 0 : (map_type == FCELL_TYPE ? 1 : 2))
  5. static void process_row_ii(int), process_row_if(int), process_row_id(int);
  6. static void process_row_fi(int), process_row_ff(int), process_row_fd(int);
  7. static void process_row_di(int), process_row_df(int), process_row_dd(int);
  8. static void (*process_row_FtypeOtype[3][3]) () = { {
  9. process_row_ii, process_row_if, process_row_id}, {
  10. process_row_fi, process_row_ff, process_row_fd}, {
  11. process_row_di, process_row_df, process_row_dd}};
  12. #define PROCESS_ROW \
  13. (process_row_FtypeOtype [F2I (in_type)] [F2I (out_type)])
  14. static void *in_rast, *out_rast;
  15. static int nrows, ncols;
  16. int do_recode(void)
  17. {
  18. struct Cell_head window, cellhd;
  19. int row, i;
  20. struct History hist;
  21. /* set the window from the header for the input file */
  22. if (align_wind) {
  23. G_get_window(&window);
  24. if (G_get_cellhd(name, "", &cellhd) >= 0) {
  25. G_align_window(&window, &cellhd);
  26. G_set_window(&window);
  27. }
  28. }
  29. G_get_set_window(&window);
  30. nrows = G_window_rows();
  31. ncols = G_window_cols();
  32. /* open the input file for reading */
  33. in_fd = G_open_cell_old(name, "");
  34. if (in_fd < 0)
  35. G_fatal_error("Can't open input map");
  36. out_fd = G_open_raster_new(result, out_type);
  37. out_rast = G_allocate_raster_buf(out_type);
  38. in_rast = G_allocate_raster_buf(in_type);
  39. for (row = 0; row < nrows; row++) {
  40. G_percent(row, nrows, 2);
  41. PROCESS_ROW(row);
  42. }
  43. G_percent(row, nrows, 2);
  44. G_close_cell(in_fd);
  45. G_close_cell(out_fd);
  46. /* writing history file */
  47. G_short_history(result, "raster", &hist);
  48. sprintf(hist.edhist[0], "recode of raster map %s", name);
  49. /* if there are more rules than history lines allocated, write only
  50. MAXEDLINES-1 rules , and "...." as a last rule */
  51. for (i = 0; (i < nrules) && (i < MAXEDLINES - 2); i++)
  52. sprintf(hist.edhist[i + 1], "%s", rules[i]);
  53. if (nrules > MAXEDLINES - 1) {
  54. sprintf(hist.edhist[MAXEDLINES - 1], "...");
  55. hist.edlinecnt = MAXEDLINES;
  56. }
  57. else
  58. hist.edlinecnt = nrules + 1;
  59. sprintf(hist.datsrc_1, "raster map %s", name);
  60. G_write_history(result, &hist);
  61. return 0;
  62. }
  63. static void process_row_ii(int row)
  64. {
  65. if (no_mask)
  66. G_get_c_raster_row_nomask(in_fd, (CELL *) in_rast, row);
  67. else
  68. G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
  69. G_fpreclass_perform_ii(&rcl_struct, (CELL *) in_rast, (CELL *) out_rast,
  70. ncols);
  71. G_put_raster_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  72. }
  73. static void process_row_if(int row)
  74. {
  75. if (no_mask)
  76. G_get_c_raster_row_nomask(in_fd, (CELL *) in_rast, row);
  77. else
  78. G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
  79. G_fpreclass_perform_if(&rcl_struct, (CELL *) in_rast, (FCELL *) out_rast,
  80. ncols);
  81. G_put_f_raster_row(out_fd, (FCELL *) out_rast);
  82. }
  83. static void process_row_id(int row)
  84. {
  85. if (no_mask)
  86. G_get_c_raster_row_nomask(in_fd, (CELL *) in_rast, row);
  87. else
  88. G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
  89. G_fpreclass_perform_id(&rcl_struct, (CELL *) in_rast, (DCELL *) out_rast,
  90. ncols);
  91. G_put_raster_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  92. }
  93. static void process_row_fi(int row)
  94. {
  95. if (no_mask)
  96. G_get_f_raster_row_nomask(in_fd, (FCELL *) in_rast, row);
  97. else
  98. G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
  99. G_fpreclass_perform_fi(&rcl_struct, (FCELL *) in_rast, (CELL *) out_rast,
  100. ncols);
  101. G_put_raster_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  102. }
  103. static void process_row_ff(int row)
  104. {
  105. if (no_mask)
  106. G_get_f_raster_row_nomask(in_fd, (FCELL *) in_rast, row);
  107. else
  108. G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
  109. G_fpreclass_perform_ff(&rcl_struct, (FCELL *) in_rast, (FCELL *) out_rast,
  110. ncols);
  111. G_put_f_raster_row(out_fd, (FCELL *) out_rast);
  112. }
  113. static void process_row_fd(int row)
  114. {
  115. if (no_mask)
  116. G_get_f_raster_row_nomask(in_fd, (FCELL *) in_rast, row);
  117. else
  118. G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
  119. G_fpreclass_perform_fd(&rcl_struct, (FCELL *) in_rast, (DCELL *) out_rast,
  120. ncols);
  121. G_put_raster_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  122. }
  123. static void process_row_di(int row)
  124. {
  125. if (no_mask)
  126. G_get_d_raster_row_nomask(in_fd, (DCELL *) in_rast, row);
  127. else
  128. G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
  129. G_fpreclass_perform_di(&rcl_struct, (DCELL *) in_rast, (CELL *) out_rast,
  130. ncols);
  131. G_put_raster_row(out_fd, (CELL *) out_rast, CELL_TYPE);
  132. }
  133. static void process_row_df(int row)
  134. {
  135. if (no_mask)
  136. G_get_d_raster_row_nomask(in_fd, (DCELL *) in_rast, row);
  137. else
  138. G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
  139. G_fpreclass_perform_df(&rcl_struct, (DCELL *) in_rast, (FCELL *) out_rast,
  140. ncols);
  141. G_put_f_raster_row(out_fd, (FCELL *) out_rast);
  142. }
  143. static void process_row_dd(int row)
  144. {
  145. if (no_mask)
  146. G_get_d_raster_row_nomask(in_fd, (DCELL *) in_rast, row);
  147. else
  148. G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
  149. G_fpreclass_perform_dd(&rcl_struct, (DCELL *) in_rast, (DCELL *) out_rast,
  150. ncols);
  151. G_put_raster_row(out_fd, (DCELL *) out_rast, DCELL_TYPE);
  152. }