set_window.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  26. */
  27. void G_get_set_window(struct Cell_head *window)
  28. {
  29. G__init_window();
  30. G_copy(window, &G__.window, sizeof(*window));
  31. }
  32. /**
  33. * \brief Establishes 'window' as the current working window.
  34. *
  35. * Any opened cell files has its file-to-window mapping reworked.
  36. *
  37. * \param[in] window window to become operative window
  38. *
  39. * \return -1 on error
  40. * \return 1 on success
  41. */
  42. int G_set_window(struct Cell_head *window)
  43. {
  44. int i;
  45. int maskfd;
  46. char *err;
  47. /* adjust window, check for valid window */
  48. /* adjust the real one, not a copy
  49. G_copy (&twindow, window, sizeof(struct Cell_head));
  50. window = &twindow;
  51. */
  52. if ((err = G_adjust_Cell_head(window, 0, 0))) {
  53. G_warning("G_set_window(): %s", err);
  54. return -1;
  55. }
  56. /* except for MASK, cell files open for read must have same projection
  57. * and zone as new window
  58. */
  59. maskfd = G__.auto_mask > 0 ? G__.mask_fd : -1;
  60. for (i = 0; i < G__.fileinfo_count; i++) {
  61. struct fileinfo *fcb = &G__.fileinfo[i];
  62. if (fcb->open_mode == OPEN_OLD) {
  63. if (fcb->cellhd.zone == window->zone &&
  64. fcb->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. struct fileinfo *fcb = &G__.fileinfo[i];
  90. if (fcb->open_mode != OPEN_OLD &&
  91. fcb->open_mode != OPEN_NEW_UNCOMPRESSED &&
  92. fcb->open_mode != OPEN_NEW_COMPRESSED)
  93. continue;
  94. if (fcb->open_mode == OPEN_OLD)
  95. G__create_window_mapping(i);
  96. /* code commented 10/1999 due to problems */
  97. #if 0
  98. else
  99. {
  100. /* opened for writing */
  101. G_free (fcb->data);
  102. fcb->data = (unsigned char *) G_calloc (G__.window.cols,
  103. G_raster_size(fcb->map_type));
  104. }
  105. /* allocate null bitstream buffers for reading/writing null rows */
  106. for (j=0;j< NULL_ROWS_INMEM; j++)
  107. {
  108. G_free (fcb->NULL_ROWS[j]);
  109. fcb->NULL_ROWS[j] = G__allocate_null_bits(G__.window.cols);
  110. }
  111. /* initialize : no NULL rows in memory */
  112. fcb->min_null_row = (-1) * NULL_ROWS_INMEM;
  113. if(fcb->null_cur_row > 0)
  114. {
  115. G_warning(
  116. "Calling G_set_window() in the middle of writing map %s",
  117. fcb->name);
  118. fcb->null_cur_row = 0;
  119. }
  120. #endif
  121. }
  122. /* turn masking (back) on if necessary */
  123. G__check_for_auto_masking();
  124. /* we want the number of bytes per cell to be maximum
  125. so that there is enough memory for reading and writing rows */
  126. return 1;
  127. }