GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
tileread.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <unistd.h>
5 #include <rpc/types.h>
6 #include <rpc/xdr.h>
7 #include "G3d_intern.h"
8 
9 static int
10 G3d_xdrTile2tile(G3D_Map * map, void *tile, int rows, int cols, int depths,
11  int xRedundant, int yRedundant, int zRedundant, int nofNum,
12  int type)
13 {
14  int y, z, xLength, yLength, length;
15 
16  if (!G3d_initCopyFromXdr(map, type)) {
17  G3d_error("G3d_xdrTile2tile: error in G3d_initCopyFromXdr");
18  return 0;
19  }
20 
21  if (nofNum == map->tileSize) {
22  if (!G3d_copyFromXdr(map->tileSize, tile)) {
23  G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
24  return 0;
25  }
26  return 1;
27  }
28 
29  length = G3d_length(type);
30  xLength = xRedundant * length;
31  yLength = map->tileX * yRedundant * length;
32 
33  if (xRedundant) {
34  for (z = 0; z < depths; z++) {
35  for (y = 0; y < rows; y++) {
36  if (!G3d_copyFromXdr(cols, tile)) {
37  G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
38  return 0;
39  }
40  tile = G_incr_void_ptr(tile, cols * length);
41  G3d_setNullValue(tile, xRedundant, type);
42  tile = G_incr_void_ptr(tile, xLength);
43  }
44  if (yRedundant) {
45  G3d_setNullValue(tile, map->tileX * yRedundant, type);
46  tile = G_incr_void_ptr(tile, yLength);
47  }
48  }
49  if (!zRedundant)
50  return 1;
51 
52  G3d_setNullValue(tile, map->tileXY * zRedundant, type);
53  return 1;
54  }
55 
56  if (yRedundant) {
57  for (z = 0; z < depths; z++) {
58  if (!G3d_copyFromXdr(map->tileX * rows, tile)) {
59  G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
60  return 0;
61  }
62  tile = G_incr_void_ptr(tile, map->tileX * rows * length);
63  G3d_setNullValue(tile, map->tileX * yRedundant, type);
64  tile = G_incr_void_ptr(tile, yLength);
65  }
66  if (!zRedundant)
67  return 1;
68 
69  G3d_setNullValue(tile, map->tileXY * zRedundant, type);
70  return 1;
71  }
72 
73  if (!G3d_copyFromXdr(map->tileXY * depths, tile)) {
74  G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
75  return 0;
76  }
77 
78  if (!zRedundant)
79  return 1;
80 
81  tile = G_incr_void_ptr(tile, map->tileXY * depths * length);
82  G3d_setNullValue(tile, map->tileXY * zRedundant, type);
83 
84  return 1;
85 }
86 
87 /*---------------------------------------------------------------------------*/
88 
89 static int G3d_readTileUncompressed(G3D_Map * map, int tileIndex, int nofNum)
90 {
91  int nofBytes;
92 
93  nofBytes = nofNum * map->numLengthExtern;
94  nofBytes = G3D_MIN(nofBytes, map->fileEndPtr - map->index[tileIndex]);
95 
96  if (read(map->data_fd, xdr, nofBytes) != nofBytes) {
97  G3d_error("G3d_readTileUncompressed: can't read file");
98  return 0;
99  }
100 
101  return 1;
102 }
103 
104 /*---------------------------------------------------------------------------*/
105 
106 static int G3d_readTileCompressed(G3D_Map * map, int tileIndex, int nofNum)
107 {
108  if (!G_fpcompress_readXdrNums(map->data_fd, xdr, nofNum,
109  map->tileLength[tileIndex],
110  map->precision, tmpCompress,
111  map->type == FCELL_TYPE)) {
112  G3d_error
113  ("G3d_readTileCompressed: error in G_fpcompress_readXdrNums");
114  return 0;
115  }
116 
117  return 1;
118 }
119 
120 /*---------------------------------------------------------------------------*/
121 
122 /*---------------------------------------------------------------------------*/
123  /* EXPORTED FUNCTIONS */
124 
125 /*---------------------------------------------------------------------------*/
126 
145 int G3d_readTile(G3D_Map * map, int tileIndex, void *tile, int type)
146 {
147  int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
148 
149  if ((tileIndex >= map->nTiles) || (tileIndex < 0))
150  G3d_fatalError("G3d_readTile: tile index out of range");
151 
152  if (map->index[tileIndex] == -1) {
153  G3d_setNullTileType(map, tile, type);
154  return 1;
155  }
156 
157  nofNum = G3d_computeClippedTileDimensions(map, tileIndex,
158  &rows, &cols, &depths,
159  &xRedundant, &yRedundant,
160  &zRedundant);
161 
162  if (lseek(map->data_fd, map->index[tileIndex], SEEK_SET) == -1) {
163  G3d_error("G3d_readTile: can't position file");
164  return 0;
165  }
166 
167  if (map->compression == G3D_NO_COMPRESSION) {
168  if (!G3d_readTileUncompressed(map, tileIndex, nofNum)) {
169  G3d_error("G3d_readTile: error in G3d_readTileUncompressed");
170  return 0;
171  }
172  }
173  else if (!G3d_readTileCompressed(map, tileIndex, nofNum)) {
174  G3d_error("G3d_readTile: error in G3d_readTileCompressed");
175  return 0;
176  }
177 
178  if (!G3d_xdrTile2tile(map, tile, rows, cols, depths,
179  xRedundant, yRedundant, zRedundant, nofNum, type)) {
180  G3d_error("G3d_readTile: error in G3d_xdrTile2tile");
181  return 0;
182  }
183 
184  if (G3d_maskIsOff(map))
185  return 1;
186 
187  G3d_maskTile(map, tileIndex, tile, type);
188  return 1;
189 }
190 
191 /*---------------------------------------------------------------------------*/
192 
193 
205 int G3d_readTileFloat(G3D_Map * map, int tileIndex, void *tile)
206 {
207  if (!G3d_readTile(map, tileIndex, tile, FCELL_TYPE)) {
208  G3d_error("G3d_readTileFloat: error in G3d_readTile");
209  return 0;
210  }
211 
212  return 1;
213 }
214 
215 /*---------------------------------------------------------------------------*/
216 
217 
229 int G3d_readTileDouble(G3D_Map * map, int tileIndex, void *tile)
230 {
231  if (!G3d_readTile(map, tileIndex, tile, DCELL_TYPE)) {
232  G3d_error("G3d_readTileDouble: error in G3d_readTile");
233  return 0;
234  }
235 
236  return 1;
237 }
238 
239 /*---------------------------------------------------------------------------*/
240 
241  /* CACHE-MODE-ONLY FUNCTIONS */
242 
243 /*---------------------------------------------------------------------------*/
244 
245 
259 int G3d_lockTile(G3D_Map * map, int tileIndex)
260 {
261  if (!map->useCache)
262  G3d_fatalError("G3d_lockTile: function invalid in non-cache mode");
263 
264  if (!G3d_cache_lock(map->cache, tileIndex)) {
265  G3d_error("G3d_lockTile: error in G3d_cache_lock");
266  return 0;
267  }
268 
269  return 1;
270 }
271 
272 /*---------------------------------------------------------------------------*/
273 
274 
286 int G3d_unlockTile(G3D_Map * map, int tileIndex)
287 {
288  if (!map->useCache)
289  G3d_fatalError("G3d_unlockTile: function invalid in non-cache mode");
290 
291  if (!G3d_cache_unlock(map->cache, tileIndex)) {
292  G3d_error("G3d_unlockTile: error in G3d_cache_unlock");
293  return 0;
294  }
295 
296  return 1;
297 }
298 
299 /*---------------------------------------------------------------------------*/
300 
301 
312 int G3d_unlockAll(G3D_Map * map)
313 {
314  if (!map->useCache)
315  G3d_fatalError("G3d_unlockAll: function invalid in non-cache mode");
316 
317  if (!G3d_cache_unlock_all(map->cache)) {
318  G3d_error("G3d_unlockAll: error in G3d_cache_unlock_all");
319  return 0;
320  }
321 
322  return 1;
323 }
324 
325 /*---------------------------------------------------------------------------*/
326 
327 
337 void G3d_autolockOn(G3D_Map * map)
338 {
339  if (!map->useCache)
340  G3d_fatalError("G3d_autoLockOn: function invalid in non-cache mode");
341 
342  G3d_cache_autolock_on(map->cache);
343 }
344 
345 /*---------------------------------------------------------------------------*/
346 
347 
357 void G3d_autolockOff(G3D_Map * map)
358 {
359  if (!map->useCache)
360  G3d_fatalError("G3d_autoLockOff: function invalid in non-cache mode");
361 
362  G3d_cache_autolock_off(map->cache);
363 }
364 
365 /*---------------------------------------------------------------------------*/
366 
367 
386 void G3d_minUnlocked(G3D_Map * map, int minUnlocked)
387 {
388  if (!map->useCache)
389  G3d_fatalError("G3d_autoLockOff: function invalid in non-cache mode");
390 
391  G3d_cache_set_minUnlock(map->cache,
392  G3d__computeCacheSize(map, minUnlocked));
393 }
394 
395 /*---------------------------------------------------------------------------*/
396 
397 
408 int G3d_beginCycle(G3D_Map * map)
409 {
410  if (!G3d_unlockAll(map)) {
411  G3d_fatalError("G3d_beginCycle: error in G3d_unlockAll");
412  return 0;
413  }
414 
415  G3d_autolockOn(map);
416  return 1;
417 }
418 
419 /*---------------------------------------------------------------------------*/
420 
421 
432 int G3d_endCycle(G3D_Map * map)
433 {
434  G3d_autolockOff(map);
435  return 1;
436 }
int G_fpcompress_readXdrNums(int fd, char *dst, int nofNum, int fileBytes, int precision, char *compressBuf, int isFloat)
Definition: fpcompress.c:827
int G3d_readTileFloat(G3D_Map *map, int tileIndex, void *tile)
Is equivalent to G3d_readTile (map, tileIndex, tile, FCELL_TYPE).
Definition: tileread.c:205
void G3d_setNullValue(void *c, int nofElts, int type)
Fills the vector pointed to by c with nofElts NULL-values of type.
Definition: g3dnull.c:32
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
void G3d_autolockOff(G3D_Map *map)
Turns autolock mode Off.
Definition: tileread.c:357
#define G3D_MIN(a, b)
Definition: G3d_intern.h:25
int G3d_cache_unlock(G3D_cache *c, int name)
Definition: cache.c:303
void G3d_cache_autolock_off(G3D_cache *c)
Definition: cache.c:360
int y
Definition: plot.c:34
void * G_incr_void_ptr(const void *ptr, const size_t size)
Advance void pointer.
Definition: gis/raster.c:33
void * tmpCompress
Definition: g3d/header.c:12
void G3d_cache_set_minUnlock(G3D_cache *c, int nofMinUnLocked)
Definition: cache.c:367
void G3d_cache_autolock_on(G3D_cache *c)
Definition: cache.c:353
int G3d_copyFromXdr(int nofNum, void *dst)
Definition: g3dfpxdr.c:222
int G3d_initCopyFromXdr(G3D_Map *map, int dType)
Definition: g3dfpxdr.c:193
int G3d_readTileDouble(G3D_Map *map, int tileIndex, void *tile)
Is equivalent to G3d_readTile (map, tileIndex, tile, DCELL_TYPE).
Definition: tileread.c:229
int G3d_maskIsOff(G3D_Map *map)
Returns 1 if the mask for map is turned off. Returns 0 otherwise.
Definition: g3dmask.c:383
int G3d_cache_lock(G3D_cache *c, int name)
Definition: cache.c:267
int G3d_cache_unlock_all(G3D_cache *c)
Definition: cache.c:324
int G3d_endCycle(G3D_Map *map)
Ends a cycle.
Definition: tileread.c:432
int G3d_unlockAll(G3D_Map *map)
Unlocks every tile in cache of map.
Definition: tileread.c:312
int G3d_computeClippedTileDimensions(G3D_Map *map, int tileIndex, int *rows, int *cols, int *depths, int *xRedundant, int *yRedundant, int *zRedundant)
Computes the dimensions of the tile when clipped to fit the region of map. The clipped dimensions are...
Definition: tilemath.c:267
tuple cols
int G3d_beginCycle(G3D_Map *map)
Starts a new cycle.
Definition: tileread.c:408
int G3d_length(int t)
Definition: g3dmisc.c:77
void G3d_minUnlocked(G3D_Map *map, int minUnlocked)
Sets the minimum number of unlocked tiles to minUnlocked. This function should be used in combination...
Definition: tileread.c:386
void G3d_setNullTileType(G3D_Map *map, void *tile, int type)
Assumes that tile is a tile with the same dimensions as the tiles of map. Fills tile with NULL-values...
Definition: tilenull.c:23
int G3d__computeCacheSize(G3D_Map *map, int cacheCode)
Definition: g3d/header.c:239
int G3d_unlockTile(G3D_Map *map, int tileIndex)
Unlocks tile with tileIndex.
Definition: tileread.c:286
void * xdr
Definition: g3d/header.c:14
void G3d_autolockOn(G3D_Map *map)
Turns autolock mode on.
Definition: tileread.c:337
int G3d_readTile(G3D_Map *map, int tileIndex, void *tile, int type)
Reads tile with index tileIndex into the tile buffer. The cells are stored with type type which must ...
Definition: tileread.c:145
void G3d_fatalError(const char *,...)
This function prints the error message msg, and terminates the program with an error status...
Definition: g3derror.c:58
int G3d_lockTile(G3D_Map *map, int tileIndex)
Locks tile with tileIndex in cache. If after locking fewer than the minimum number of unlocked tiles ...
Definition: tileread.c:259
void G3d_maskTile(G3D_Map *map, int tileIndex, void *tile, int type)
Replaces the values stored in tile (with tileIndex) for which G3d_isMasked returns 1 with NULL-values...
Definition: g3dmask.c:274