GRASS Programmer's Manual  6.4.2(2012)
tileread.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <sys/types.h>
00004 #include <unistd.h>
00005 #include <rpc/types.h>
00006 #include <rpc/xdr.h>
00007 #include "G3d_intern.h"
00008 
00009 static int
00010 G3d_xdrTile2tile(G3D_Map * map, void *tile, int rows, int cols, int depths,
00011                  int xRedundant, int yRedundant, int zRedundant, int nofNum,
00012                  int type)
00013 {
00014     int y, z, xLength, yLength, length;
00015 
00016     if (!G3d_initCopyFromXdr(map, type)) {
00017         G3d_error("G3d_xdrTile2tile: error in G3d_initCopyFromXdr");
00018         return 0;
00019     }
00020 
00021     if (nofNum == map->tileSize) {
00022         if (!G3d_copyFromXdr(map->tileSize, tile)) {
00023             G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
00024             return 0;
00025         }
00026         return 1;
00027     }
00028 
00029     length = G3d_length(type);
00030     xLength = xRedundant * length;
00031     yLength = map->tileX * yRedundant * length;
00032 
00033     if (xRedundant) {
00034         for (z = 0; z < depths; z++) {
00035             for (y = 0; y < rows; y++) {
00036                 if (!G3d_copyFromXdr(cols, tile)) {
00037                     G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
00038                     return 0;
00039                 }
00040                 tile = G_incr_void_ptr(tile, cols * length);
00041                 G3d_setNullValue(tile, xRedundant, type);
00042                 tile = G_incr_void_ptr(tile, xLength);
00043             }
00044             if (yRedundant) {
00045                 G3d_setNullValue(tile, map->tileX * yRedundant, type);
00046                 tile = G_incr_void_ptr(tile, yLength);
00047             }
00048         }
00049         if (!zRedundant)
00050             return 1;
00051 
00052         G3d_setNullValue(tile, map->tileXY * zRedundant, type);
00053         return 1;
00054     }
00055 
00056     if (yRedundant) {
00057         for (z = 0; z < depths; z++) {
00058             if (!G3d_copyFromXdr(map->tileX * rows, tile)) {
00059                 G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
00060                 return 0;
00061             }
00062             tile = G_incr_void_ptr(tile, map->tileX * rows * length);
00063             G3d_setNullValue(tile, map->tileX * yRedundant, type);
00064             tile = G_incr_void_ptr(tile, yLength);
00065         }
00066         if (!zRedundant)
00067             return 1;
00068 
00069         G3d_setNullValue(tile, map->tileXY * zRedundant, type);
00070         return 1;
00071     }
00072 
00073     if (!G3d_copyFromXdr(map->tileXY * depths, tile)) {
00074         G3d_error("G3d_xdrTile2tile: error in G3d_copyFromXdr");
00075         return 0;
00076     }
00077 
00078     if (!zRedundant)
00079         return 1;
00080 
00081     tile = G_incr_void_ptr(tile, map->tileXY * depths * length);
00082     G3d_setNullValue(tile, map->tileXY * zRedundant, type);
00083 
00084     return 1;
00085 }
00086 
00087 /*---------------------------------------------------------------------------*/
00088 
00089 static int G3d_readTileUncompressed(G3D_Map * map, int tileIndex, int nofNum)
00090 {
00091     int nofBytes;
00092 
00093     nofBytes = nofNum * map->numLengthExtern;
00094     nofBytes = G3D_MIN(nofBytes, map->fileEndPtr - map->index[tileIndex]);
00095 
00096     if (read(map->data_fd, xdr, nofBytes) != nofBytes) {
00097         G3d_error("G3d_readTileUncompressed: can't read file");
00098         return 0;
00099     }
00100 
00101     return 1;
00102 }
00103 
00104 /*---------------------------------------------------------------------------*/
00105 
00106 static int G3d_readTileCompressed(G3D_Map * map, int tileIndex, int nofNum)
00107 {
00108     if (!G_fpcompress_readXdrNums(map->data_fd, xdr, nofNum,
00109                                   map->tileLength[tileIndex],
00110                                   map->precision, tmpCompress,
00111                                   map->type == FCELL_TYPE)) {
00112         G3d_error
00113             ("G3d_readTileCompressed: error in G_fpcompress_readXdrNums");
00114         return 0;
00115     }
00116 
00117     return 1;
00118 }
00119 
00120 /*---------------------------------------------------------------------------*/
00121 
00122 /*---------------------------------------------------------------------------*/
00123                        /* EXPORTED FUNCTIONS */
00124 
00125 /*---------------------------------------------------------------------------*/
00126 
00145 int G3d_readTile(G3D_Map * map, int tileIndex, void *tile, int type)
00146 {
00147     int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
00148 
00149     if ((tileIndex >= map->nTiles) || (tileIndex < 0))
00150         G3d_fatalError("G3d_readTile: tile index out of range");
00151 
00152     if (map->index[tileIndex] == -1) {
00153         G3d_setNullTileType(map, tile, type);
00154         return 1;
00155     }
00156 
00157     nofNum = G3d_computeClippedTileDimensions(map, tileIndex,
00158                                               &rows, &cols, &depths,
00159                                               &xRedundant, &yRedundant,
00160                                               &zRedundant);
00161 
00162     if (lseek(map->data_fd, map->index[tileIndex], SEEK_SET) == -1) {
00163         G3d_error("G3d_readTile: can't position file");
00164         return 0;
00165     }
00166 
00167     if (map->compression == G3D_NO_COMPRESSION) {
00168         if (!G3d_readTileUncompressed(map, tileIndex, nofNum)) {
00169             G3d_error("G3d_readTile: error in G3d_readTileUncompressed");
00170             return 0;
00171         }
00172     }
00173     else if (!G3d_readTileCompressed(map, tileIndex, nofNum)) {
00174         G3d_error("G3d_readTile: error in G3d_readTileCompressed");
00175         return 0;
00176     }
00177 
00178     if (!G3d_xdrTile2tile(map, tile, rows, cols, depths,
00179                           xRedundant, yRedundant, zRedundant, nofNum, type)) {
00180         G3d_error("G3d_readTile: error in G3d_xdrTile2tile");
00181         return 0;
00182     }
00183 
00184     if (G3d_maskIsOff(map))
00185         return 1;
00186 
00187     G3d_maskTile(map, tileIndex, tile, type);
00188     return 1;
00189 }
00190 
00191 /*---------------------------------------------------------------------------*/
00192 
00193 
00205 int G3d_readTileFloat(G3D_Map * map, int tileIndex, void *tile)
00206 {
00207     if (!G3d_readTile(map, tileIndex, tile, FCELL_TYPE)) {
00208         G3d_error("G3d_readTileFloat: error in G3d_readTile");
00209         return 0;
00210     }
00211 
00212     return 1;
00213 }
00214 
00215 /*---------------------------------------------------------------------------*/
00216 
00217 
00229 int G3d_readTileDouble(G3D_Map * map, int tileIndex, void *tile)
00230 {
00231     if (!G3d_readTile(map, tileIndex, tile, DCELL_TYPE)) {
00232         G3d_error("G3d_readTileDouble: error in G3d_readTile");
00233         return 0;
00234     }
00235 
00236     return 1;
00237 }
00238 
00239 /*---------------------------------------------------------------------------*/
00240 
00241                       /* CACHE-MODE-ONLY FUNCTIONS */
00242 
00243 /*---------------------------------------------------------------------------*/
00244 
00245 
00259 int G3d_lockTile(G3D_Map * map, int tileIndex)
00260 {
00261     if (!map->useCache)
00262         G3d_fatalError("G3d_lockTile: function invalid in non-cache mode");
00263 
00264     if (!G3d_cache_lock(map->cache, tileIndex)) {
00265         G3d_error("G3d_lockTile: error in G3d_cache_lock");
00266         return 0;
00267     }
00268 
00269     return 1;
00270 }
00271 
00272 /*---------------------------------------------------------------------------*/
00273 
00274 
00286 int G3d_unlockTile(G3D_Map * map, int tileIndex)
00287 {
00288     if (!map->useCache)
00289         G3d_fatalError("G3d_unlockTile: function invalid in non-cache mode");
00290 
00291     if (!G3d_cache_unlock(map->cache, tileIndex)) {
00292         G3d_error("G3d_unlockTile: error in G3d_cache_unlock");
00293         return 0;
00294     }
00295 
00296     return 1;
00297 }
00298 
00299 /*---------------------------------------------------------------------------*/
00300 
00301 
00312 int G3d_unlockAll(G3D_Map * map)
00313 {
00314     if (!map->useCache)
00315         G3d_fatalError("G3d_unlockAll: function invalid in non-cache mode");
00316 
00317     if (!G3d_cache_unlock_all(map->cache)) {
00318         G3d_error("G3d_unlockAll: error in G3d_cache_unlock_all");
00319         return 0;
00320     }
00321 
00322     return 1;
00323 }
00324 
00325 /*---------------------------------------------------------------------------*/
00326 
00327 
00337 void G3d_autolockOn(G3D_Map * map)
00338 {
00339     if (!map->useCache)
00340         G3d_fatalError("G3d_autoLockOn: function invalid in non-cache mode");
00341 
00342     G3d_cache_autolock_on(map->cache);
00343 }
00344 
00345 /*---------------------------------------------------------------------------*/
00346 
00347 
00357 void G3d_autolockOff(G3D_Map * map)
00358 {
00359     if (!map->useCache)
00360         G3d_fatalError("G3d_autoLockOff: function invalid in non-cache mode");
00361 
00362     G3d_cache_autolock_off(map->cache);
00363 }
00364 
00365 /*---------------------------------------------------------------------------*/
00366 
00367 
00386 void G3d_minUnlocked(G3D_Map * map, int minUnlocked)
00387 {
00388     if (!map->useCache)
00389         G3d_fatalError("G3d_autoLockOff: function invalid in non-cache mode");
00390 
00391     G3d_cache_set_minUnlock(map->cache,
00392                             G3d__computeCacheSize(map, minUnlocked));
00393 }
00394 
00395 /*---------------------------------------------------------------------------*/
00396 
00397 
00408 int G3d_beginCycle(G3D_Map * map)
00409 {
00410     if (!G3d_unlockAll(map)) {
00411         G3d_fatalError("G3d_beginCycle: error in G3d_unlockAll");
00412         return 0;
00413     }
00414 
00415     G3d_autolockOn(map);
00416     return 1;
00417 }
00418 
00419 /*---------------------------------------------------------------------------*/
00420 
00421 
00432 int G3d_endCycle(G3D_Map * map)
00433 {
00434     G3d_autolockOff(map);
00435     return 1;
00436 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines