set_window.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * \file set_window.c
  3. *
  4. * \brief Set window
  5. *
  6. * (C) 2001-2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author GRASS GIS Development Team
  12. *
  13. * \date 1999-2007
  14. */
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include "G.h"
  18. /**
  19. * \brief Get the current working window.
  20. *
  21. * The current working window values are returned in the structure
  22. * 'window'.
  23. *
  24. * \param[out] window window structure to be set
  25. * \return 1
  26. */
  27. int G_get_set_window(struct Cell_head *window)
  28. {
  29. G__init_window();
  30. G_copy((char *)window, (char *)&G__.window, sizeof(*window));
  31. return 1;
  32. }
  33. /**
  34. * \brief Establishes 'window' as the current working window.
  35. *
  36. * Any opened cell files has its file-to-window mapping reworked.
  37. *
  38. * \param[in] window window to become operative window
  39. *
  40. * \return -1 on error
  41. * \return 1 on success
  42. */
  43. int G_set_window(struct Cell_head *window)
  44. {
  45. int i;
  46. int maskfd;
  47. char *err;
  48. /* adjust window, check for valid window */
  49. /* adjust the real one, not a copy
  50. G_copy (&twindow, window, sizeof(struct Cell_head));
  51. window = &twindow;
  52. */
  53. if ((err = G_adjust_Cell_head(window, 0, 0))) {
  54. G_warning("G_set_window(): %s", err);
  55. return -1;
  56. }
  57. /* except for MASK, cell files open for read must have same projection
  58. * and zone as new window
  59. */
  60. maskfd = G__.auto_mask > 0 ? G__.mask_fd : -1;
  61. for (i = 0; i < G__.fileinfo_count; i++) {
  62. if (G__.fileinfo[i].open_mode == OPEN_OLD) {
  63. if (G__.fileinfo[i].cellhd.zone == window->zone &&
  64. G__.fileinfo[i].cellhd.proj == window->proj)
  65. continue;
  66. if (i != maskfd) {
  67. G_warning(_("G_set_window(): projection/zone differs from that of "
  68. "currently open raster maps"));
  69. return -1;
  70. }
  71. }
  72. }
  73. /* close the mask */
  74. if (G__.auto_mask > 0) {
  75. G_close_cell(maskfd);
  76. /* G_free (G__.mask_buf); */
  77. G__.mask_fd = -1;
  78. G__.auto_mask = -1; /* turn off masking */
  79. }
  80. /* copy the window to the current window */
  81. G_copy((char *)&G__.window, (char *)window, sizeof(*window));
  82. G__.window_set = 1;
  83. /* now for each possible open cell file, recreate the window mapping */
  84. /*
  85. * also the memory for reading and writing must be reallocated for all opened
  86. * cell files
  87. */
  88. for (i = 0; i < G__.fileinfo_count; i++) {
  89. if (G__.fileinfo[i].open_mode != OPEN_OLD &&
  90. G__.fileinfo[i].open_mode != OPEN_NEW_UNCOMPRESSED &&
  91. G__.fileinfo[i].open_mode != OPEN_NEW_COMPRESSED &&
  92. G__.fileinfo[i].open_mode != OPEN_NEW_RANDOM)
  93. continue;
  94. if (G__.fileinfo[i].open_mode == OPEN_OLD)
  95. G__create_window_mapping(i);
  96. /* code commented 10/1999 due to problems */
  97. /* else */
  98. /* opened for writing */
  99. /* {
  100. G_free (G__.fileinfo[i].data);
  101. G__.fileinfo[i].data = (unsigned char *) G_calloc (G__.window.cols,
  102. G_raster_size(G__.fileinfo[i].map_type));
  103. }
  104. */
  105. /* allocate null bitstream buffers for reading/writing null rows */
  106. /* for (j=0;j< NULL_ROWS_INMEM; j++)
  107. {
  108. G_free (G__.fileinfo[i].NULL_ROWS[j]);
  109. G__.fileinfo[i].NULL_ROWS[j] = G__allocate_null_bits(G__.window.cols);
  110. }
  111. */
  112. /* initialize : no NULL rows in memory */
  113. /* G__.fileinfo[i].min_null_row = (-1) * NULL_ROWS_INMEM;
  114. if(G__.fileinfo[i].null_cur_row > 0)
  115. {
  116. G_warning(
  117. "Calling G_set_window() in the middle of writing map %s",
  118. G__.fileinfo[i].name);
  119. G__.fileinfo[i].null_cur_row = 0;
  120. }
  121. */
  122. }
  123. /* turn masking (back) on if necessary */
  124. G__check_for_auto_masking();
  125. /* reallocate/enlarge the G__. buffers for reading raster maps */
  126. G__reallocate_null_buf();
  127. G__reallocate_mask_buf();
  128. G__reallocate_temp_buf();
  129. G__reallocate_work_buf(sizeof(DCELL));
  130. G__reallocate_work_buf(XDR_DOUBLE_NBYTES);
  131. /* we want the number of bytes per cell to be maximum
  132. so that there is enough memory for reading and writing rows */
  133. return 1;
  134. }