00001 00017 #include <grass/gis.h> 00018 #include <grass/glocale.h> 00019 #include "G.h" 00020 00030 int G_get_set_window(struct Cell_head *window) 00031 { 00032 G__init_window(); 00033 G_copy((char *)window, (char *)&G__.window, sizeof(*window)); 00034 00035 return 1; 00036 } 00037 00038 00049 int G_set_window(struct Cell_head *window) 00050 { 00051 int i; 00052 int maskfd; 00053 char *err; 00054 00055 /* adjust window, check for valid window */ 00056 /* adjust the real one, not a copy 00057 G_copy (&twindow, window, sizeof(struct Cell_head)); 00058 window = &twindow; 00059 */ 00060 00061 if ((err = G_adjust_Cell_head(window, 0, 0))) { 00062 G_warning("G_set_window(): %s", err); 00063 return -1; 00064 } 00065 00066 /* except for MASK, cell files open for read must have same projection 00067 * and zone as new window 00068 */ 00069 maskfd = G__.auto_mask > 0 ? G__.mask_fd : -1; 00070 for (i = 0; i < G__.fileinfo_count; i++) { 00071 if (G__.fileinfo[i].open_mode == OPEN_OLD) { 00072 if (G__.fileinfo[i].cellhd.zone == window->zone && 00073 G__.fileinfo[i].cellhd.proj == window->proj) 00074 continue; 00075 if (i != maskfd) { 00076 G_warning(_("G_set_window(): projection/zone differs from that of " 00077 "currently open raster maps")); 00078 return -1; 00079 } 00080 } 00081 } 00082 00083 /* close the mask */ 00084 if (G__.auto_mask > 0) { 00085 G_close_cell(maskfd); 00086 /* G_free (G__.mask_buf); */ 00087 G__.mask_fd = -1; 00088 G__.auto_mask = -1; /* turn off masking */ 00089 } 00090 00091 /* copy the window to the current window */ 00092 G_copy((char *)&G__.window, (char *)window, sizeof(*window)); 00093 00094 G__.window_set = 1; 00095 00096 /* now for each possible open cell file, recreate the window mapping */ 00097 /* 00098 * also the memory for reading and writing must be reallocated for all opened 00099 * cell files 00100 */ 00101 for (i = 0; i < G__.fileinfo_count; i++) { 00102 if (G__.fileinfo[i].open_mode != OPEN_OLD && 00103 G__.fileinfo[i].open_mode != OPEN_NEW_UNCOMPRESSED && 00104 G__.fileinfo[i].open_mode != OPEN_NEW_COMPRESSED && 00105 G__.fileinfo[i].open_mode != OPEN_NEW_RANDOM) 00106 continue; 00107 00108 if (G__.fileinfo[i].open_mode == OPEN_OLD) 00109 G__create_window_mapping(i); 00110 /* code commented 10/1999 due to problems */ 00111 /* else */ 00112 /* opened for writing */ 00113 /* { 00114 G_free (G__.fileinfo[i].data); 00115 G__.fileinfo[i].data = (unsigned char *) G_calloc (G__.window.cols, 00116 G_raster_size(G__.fileinfo[i].map_type)); 00117 } 00118 */ 00119 /* allocate null bitstream buffers for reading/writing null rows */ 00120 /* for (j=0;j< NULL_ROWS_INMEM; j++) 00121 { 00122 G_free (G__.fileinfo[i].NULL_ROWS[j]); 00123 G__.fileinfo[i].NULL_ROWS[j] = G__allocate_null_bits(G__.window.cols); 00124 } 00125 */ 00126 00127 /* initialize : no NULL rows in memory */ 00128 /* G__.fileinfo[i].min_null_row = (-1) * NULL_ROWS_INMEM; 00129 if(G__.fileinfo[i].null_cur_row > 0) 00130 { 00131 G_warning( 00132 "Calling G_set_window() in the middle of writing map %s", 00133 G__.fileinfo[i].name); 00134 G__.fileinfo[i].null_cur_row = 0; 00135 } 00136 */ 00137 } 00138 00139 /* turn masking (back) on if necessary */ 00140 G__check_for_auto_masking(); 00141 00142 /* reallocate/enlarge the G__. buffers for reading raster maps */ 00143 G__reallocate_null_buf(); 00144 G__reallocate_mask_buf(); 00145 G__reallocate_temp_buf(); 00146 G__reallocate_work_buf(sizeof(DCELL)); 00147 G__reallocate_work_buf(XDR_DOUBLE_NBYTES); 00148 /* we want the number of bytes per cell to be maximum 00149 so that there is enough memory for reading and writing rows */ 00150 00151 return 1; 00152 }