outl_io.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include <stdlib.h>
  2. #include <grass/gis.h>
  3. #include <grass/raster.h>
  4. #include <grass/glocale.h>
  5. #include "local_proto.h"
  6. static int blank_line();
  7. static char *error_prefix;
  8. static int first_read, last_read;
  9. static char cell_name[256];
  10. static int in_file_d;
  11. static int raster_size, row_length, row_count, n_rows;
  12. static RASTER_MAP_TYPE map_type;
  13. int o_io_init(void)
  14. {
  15. error_prefix = "ps.map";
  16. n_rows = PS.w.rows;
  17. row_length = PS.w.cols;
  18. return 0;
  19. }
  20. int o_read_row(void *buf)
  21. {
  22. void *ptr;
  23. ptr = buf;
  24. if (last_read)
  25. return (0);
  26. if (first_read) {
  27. blank_line(buf);
  28. first_read = 0;
  29. }
  30. else {
  31. if (row_count >= n_rows) {
  32. last_read = 1;
  33. blank_line(buf);
  34. }
  35. else {
  36. Rast_set_null_value(ptr, 1, map_type);
  37. ptr = G_incr_void_ptr(ptr, raster_size);
  38. Rast_get_row(in_file_d, ptr, row_count++, map_type);
  39. ptr = G_incr_void_ptr(ptr, raster_size * (row_length + 1));
  40. Rast_set_null_value(ptr, 1, map_type);
  41. }
  42. }
  43. return (row_length + 2);
  44. }
  45. static int blank_line(void *buf)
  46. {
  47. Rast_set_null_value(buf, row_length + 2, map_type);
  48. return 0;
  49. }
  50. RASTER_MAP_TYPE o_open_file(char *cell)
  51. {
  52. /* open raster map */
  53. sscanf(cell, "%s", cell_name);
  54. in_file_d = Rast_open_old(cell_name, "");
  55. map_type = Rast_get_map_type(in_file_d);
  56. raster_size = Rast_cell_size(map_type);
  57. first_read = 1;
  58. last_read = 0;
  59. row_count = 0;
  60. o_alloc_bufs(row_length + 2, raster_size);
  61. return (map_type);
  62. }
  63. int o_close_file(void)
  64. {
  65. Rast_close(in_file_d);
  66. return 0;
  67. }
  68. #ifdef DEBUG
  69. char *xmalloc(int size, char *label)
  70. {
  71. char *addr, *G_malloc();
  72. addr = G_malloc(size);
  73. fprintf(stdout, "MALLOC: %8d %7d %s\n", addr, size, label);
  74. return (addr);
  75. }
  76. int xfree(char *addr, char *label)
  77. {
  78. fprintf(stdout, "FREE: %8d %s\n", addr, label);
  79. G_free(addr);
  80. return 0;
  81. }
  82. char *xrealloc(char *addr, int size, char *label)
  83. {
  84. char *addr2, *G_realloc();
  85. addr2 = G_realloc(addr, size);
  86. fprintf(stdout, "REALLOC: %8d %7d (%8d) %s\n", addr2, size, addr,
  87. label);
  88. return (addr2);
  89. }
  90. #endif