io.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* Line thinning program */
  2. /* Input/output and file support functions */
  3. /* Mike Baba */
  4. /* DBA Systems */
  5. /* Fairfax, Va */
  6. /* Jan 1990 */
  7. /* Jean Ezell */
  8. /* US Army Corps of Engineers */
  9. /* Construction Engineering Research Laboratory */
  10. /* Modelling and Simulation Team */
  11. /* Champaign, IL 61820 */
  12. /* January - February 1988 */
  13. /* Entry points: */
  14. /* get_a_row get row from temporary work file */
  15. /* open_file open input raster map and read it into work file */
  16. /* close_file copy work file into new raster map */
  17. /* map_size get size of map and its pad */
  18. /* Global variables: */
  19. /* row_io place to store pointer to row manager stuff */
  20. /* n_rows number of rows in the work file (includes pads) */
  21. /* n_cols number of columns in the work file (includes pads) */
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <math.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <errno.h>
  29. #include <fcntl.h>
  30. #include <grass/config.h>
  31. #include <grass/gis.h>
  32. #include <grass/raster.h>
  33. #include <grass/glocale.h>
  34. #include <grass/rowio.h>
  35. #define PAD 2
  36. #define MAX_ROW 7
  37. /*extern int errno; *//* included #include <errno.h> instead 1/2000 */
  38. extern char *error_prefix;
  39. static int n_rows, n_cols;
  40. static int work_file;
  41. static char *work_file_name;
  42. static ROWIO row_io;
  43. /* function prototypes */
  44. static int write_row(int file, const void *buf, int row, int buf_len);
  45. static int read_row(int file, void *buf, int row, int buf_len);
  46. CELL *get_a_row(int row)
  47. {
  48. if (row < 0 || row >= n_rows)
  49. return (NULL);
  50. return ((CELL *) Rowio_get(&row_io, row));
  51. }
  52. int put_a_row(int row, CELL * buf)
  53. {
  54. /* rowio.h defines this withe 2nd argument as char * */
  55. Rowio_put(&row_io, (char *)buf, row);
  56. return 0;
  57. }
  58. static int read_row(int file, void *buf, int row, int buf_len)
  59. {
  60. lseek(file, ((off_t) row) * buf_len, 0);
  61. return (read(file, buf, buf_len) == buf_len);
  62. }
  63. static int write_row(int file, const void *buf, int row, int buf_len)
  64. {
  65. lseek(file, ((off_t) row) * buf_len, 0);
  66. return (write(file, buf, buf_len) == buf_len);
  67. }
  68. int open_file(char *name)
  69. {
  70. int cell_file, buf_len;
  71. int i, row;
  72. CELL *buf;
  73. /* open raster map */
  74. cell_file = Rast_open_old(name, "");
  75. if (Rast_get_map_type(cell_file) != CELL_TYPE) {
  76. Rast_close(cell_file);
  77. G_fatal_error(_("Input raster must be of type CELL."));
  78. }
  79. n_rows = Rast_window_rows();
  80. n_cols = Rast_window_cols();
  81. G_message(_("File %s -- %d rows X %d columns"), name, n_rows, n_cols);
  82. n_cols += (PAD << 1);
  83. /* copy raster map into our read/write file */
  84. work_file_name = G_tempfile();
  85. /* create the file and then open it for read and write */
  86. close(creat(work_file_name, 0666));
  87. if ((work_file = open(work_file_name, 2)) < 0) {
  88. unlink(work_file_name);
  89. G_fatal_error(_("%s: Unable to create temporary file <%s> -- errno = %d"),
  90. error_prefix, work_file_name, errno);
  91. }
  92. buf_len = n_cols * sizeof(CELL);
  93. buf = (CELL *) G_malloc(buf_len);
  94. Rast_set_c_null_value(buf, n_cols);
  95. for (i = 0; i < PAD; i++) {
  96. if (write(work_file, buf, buf_len) != buf_len) {
  97. unlink(work_file_name);
  98. G_fatal_error(_("%s: Error writing temporary file"),
  99. error_prefix);
  100. }
  101. }
  102. for (row = 0; row < n_rows; row++) {
  103. Rast_get_c_row(cell_file, buf + PAD, row);
  104. if (write(work_file, buf, buf_len) != buf_len) {
  105. unlink(work_file_name);
  106. G_fatal_error(_("%s: Error writing temporary file"),
  107. error_prefix);
  108. }
  109. }
  110. Rast_set_c_null_value(buf, n_cols);
  111. for (i = 0; i < PAD; i++) {
  112. if (write(work_file, buf, buf_len) != buf_len) {
  113. unlink(work_file_name);
  114. G_fatal_error(_("%s: Error writing temporary file"),
  115. error_prefix);
  116. }
  117. }
  118. n_rows += (PAD << 1);
  119. G_free(buf);
  120. Rast_close(cell_file);
  121. Rowio_setup(&row_io, work_file, MAX_ROW, n_cols * sizeof(CELL), read_row,
  122. write_row);
  123. return 0;
  124. }
  125. int close_file(char *name)
  126. {
  127. int cell_file, row, k;
  128. int row_count, col_count;
  129. CELL *buf;
  130. cell_file = Rast_open_c_new(name);
  131. row_count = n_rows - (PAD << 1);
  132. col_count = n_cols - (PAD << 1);
  133. G_message(_("Output file %d rows X %d columns"), row_count, col_count);
  134. G_message(_("Window %d rows X %d columns"), Rast_window_rows(),
  135. Rast_window_cols());
  136. for (row = 0, k = PAD; row < row_count; row++, k++) {
  137. buf = get_a_row(k);
  138. Rast_put_row(cell_file, buf + PAD, CELL_TYPE);
  139. }
  140. Rast_close(cell_file);
  141. Rowio_flush(&row_io);
  142. close(Rowio_fileno(&row_io));
  143. Rowio_release(&row_io);
  144. unlink(work_file_name);
  145. return 0;
  146. }
  147. int map_size(int *r, int *c, int *p)
  148. {
  149. *r = n_rows;
  150. *c = n_cols;
  151. *p = PAD;
  152. return 0;
  153. }