recode.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. {
  24. G_get_window (&window);
  25. if (G_get_cellhd (name, mapset, &cellhd) >= 0)
  26. {
  27. G_align_window (&window, &cellhd);
  28. G_set_window (&window);
  29. }
  30. }
  31. G_get_set_window (&window);
  32. nrows = G_window_rows();
  33. ncols = G_window_cols();
  34. /* open the input file for reading */
  35. in_fd = G_open_cell_old (name, mapset);
  36. if (in_fd < 0) G_fatal_error("Can't open input map");
  37. out_fd = G_open_raster_new (result, out_type);
  38. out_rast = G_allocate_raster_buf(out_type);
  39. in_rast = G_allocate_raster_buf(in_type);
  40. for (row = 0; row < nrows; row++)
  41. {
  42. G_percent (row, nrows, 2);
  43. PROCESS_ROW(row);
  44. }
  45. G_percent (row, nrows, 2);
  46. G_close_cell (in_fd);
  47. G_close_cell (out_fd);
  48. /* writing history file */
  49. G_short_history(result, "raster", &hist);
  50. sprintf(hist.edhist[0], "recode of raster map %s", name);
  51. /* if there are more rules than history lines allocated, write only
  52. MAXEDLINES-1 rules , and "...." as a last rule */
  53. for(i=0; (i< nrules) && (i<MAXEDLINES-2);i++)
  54. sprintf(hist.edhist[i+1], "%s", rules[i]);
  55. if(nrules > MAXEDLINES-1)
  56. {
  57. sprintf(hist.edhist[MAXEDLINES-1], "...");
  58. hist.edlinecnt = MAXEDLINES;
  59. }
  60. else
  61. hist.edlinecnt = nrules + 1;
  62. sprintf(hist.datsrc_1,"raster map %s", name);
  63. G_write_history (result, &hist);
  64. return 0;
  65. }
  66. static void process_row_ii(int row)
  67. {
  68. if(no_mask)
  69. G_get_c_raster_row_nomask (in_fd, (CELL *) in_rast, row);
  70. else
  71. G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
  72. G_fpreclass_perform_ii(&rcl_struct, (CELL *) in_rast, (CELL *) out_rast, ncols);
  73. G_put_raster_row (out_fd, (CELL *) out_rast, CELL_TYPE);
  74. }
  75. static void process_row_if (int row)
  76. {
  77. if(no_mask)
  78. G_get_c_raster_row_nomask (in_fd, (CELL *) in_rast, row);
  79. else
  80. G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
  81. G_fpreclass_perform_if(&rcl_struct, (CELL *) in_rast, (FCELL *) out_rast, ncols);
  82. G_put_f_raster_row (out_fd, (FCELL *) out_rast);
  83. }
  84. static void process_row_id(int row)
  85. {
  86. if(no_mask)
  87. G_get_c_raster_row_nomask (in_fd, (CELL *) in_rast, row);
  88. else
  89. G_get_c_raster_row(in_fd, (CELL *) in_rast, row);
  90. G_fpreclass_perform_id(&rcl_struct, (CELL *) in_rast, (DCELL *) out_rast, 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, ncols);
  100. G_put_raster_row (out_fd, (CELL *) out_rast, CELL_TYPE);
  101. }
  102. static void process_row_ff(int row)
  103. {
  104. if(no_mask)
  105. G_get_f_raster_row_nomask (in_fd, (FCELL *) in_rast, row);
  106. else
  107. G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
  108. G_fpreclass_perform_ff(&rcl_struct, (FCELL *) in_rast, (FCELL *) out_rast, ncols);
  109. G_put_f_raster_row (out_fd, (FCELL *) out_rast);
  110. }
  111. static void process_row_fd(int row)
  112. {
  113. if(no_mask)
  114. G_get_f_raster_row_nomask (in_fd, (FCELL *) in_rast, row);
  115. else
  116. G_get_f_raster_row(in_fd, (FCELL *) in_rast, row);
  117. G_fpreclass_perform_fd(&rcl_struct, (FCELL *) in_rast, (DCELL *) out_rast, ncols);
  118. G_put_raster_row (out_fd, (DCELL *) out_rast,DCELL_TYPE);
  119. }
  120. static void process_row_di(int row)
  121. {
  122. if(no_mask)
  123. G_get_d_raster_row_nomask (in_fd, (DCELL *) in_rast, row);
  124. else
  125. G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
  126. G_fpreclass_perform_di(&rcl_struct, (DCELL *) in_rast, (CELL *) out_rast, ncols);
  127. G_put_raster_row (out_fd, (CELL *) out_rast, CELL_TYPE);
  128. }
  129. static void process_row_df(int row)
  130. {
  131. if(no_mask)
  132. G_get_d_raster_row_nomask (in_fd, (DCELL *) in_rast, row);
  133. else
  134. G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
  135. G_fpreclass_perform_df(&rcl_struct, (DCELL *) in_rast, (FCELL *) out_rast, ncols);
  136. G_put_f_raster_row (out_fd, (FCELL *) out_rast);
  137. }
  138. static void process_row_dd(int row)
  139. {
  140. if(no_mask)
  141. G_get_d_raster_row_nomask (in_fd, (DCELL *) in_rast, row);
  142. else
  143. G_get_d_raster_row(in_fd, (DCELL *) in_rast, row);
  144. G_fpreclass_perform_dd(&rcl_struct, (DCELL *) in_rast, (DCELL *) out_rast, ncols);
  145. G_put_raster_row (out_fd, (DCELL *) out_rast,DCELL_TYPE);
  146. }