tileread.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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 <grass/raster.h>
  8. #include "raster3d_intern.h"
  9. static int
  10. Rast3d_xdrTile2tile(RASTER3D_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. if (!Rast3d_init_copy_from_xdr(map, type)) {
  16. Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_init_copy_from_xdr");
  17. return 0;
  18. }
  19. if (nofNum == map->tileSize) {
  20. if (!Rast3d_copy_from_xdr(map->tileSize, tile)) {
  21. Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
  22. return 0;
  23. }
  24. return 1;
  25. }
  26. length = Rast3d_length(type);
  27. xLength = xRedundant * length;
  28. yLength = map->tileX * yRedundant * length;
  29. if (xRedundant) {
  30. for (z = 0; z < depths; z++) {
  31. for (y = 0; y < rows; y++) {
  32. if (!Rast3d_copy_from_xdr(cols, tile)) {
  33. Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
  34. return 0;
  35. }
  36. tile = G_incr_void_ptr(tile, cols * length);
  37. Rast3d_set_null_value(tile, xRedundant, type);
  38. tile = G_incr_void_ptr(tile, xLength);
  39. }
  40. if (yRedundant) {
  41. Rast3d_set_null_value(tile, map->tileX * yRedundant, type);
  42. tile = G_incr_void_ptr(tile, yLength);
  43. }
  44. }
  45. if (!zRedundant)
  46. return 1;
  47. Rast3d_set_null_value(tile, map->tileXY * zRedundant, type);
  48. return 1;
  49. }
  50. if (yRedundant) {
  51. for (z = 0; z < depths; z++) {
  52. if (!Rast3d_copy_from_xdr(map->tileX * rows, tile)) {
  53. Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
  54. return 0;
  55. }
  56. tile = G_incr_void_ptr(tile, map->tileX * rows * length);
  57. Rast3d_set_null_value(tile, map->tileX * yRedundant, type);
  58. tile = G_incr_void_ptr(tile, yLength);
  59. }
  60. if (!zRedundant)
  61. return 1;
  62. Rast3d_set_null_value(tile, map->tileXY * zRedundant, type);
  63. return 1;
  64. }
  65. if (!Rast3d_copy_from_xdr(map->tileXY * depths, tile)) {
  66. Rast3d_error("Rast3d_xdrTile2tile: error in Rast3d_copy_from_xdr");
  67. return 0;
  68. }
  69. if (!zRedundant)
  70. return 1;
  71. tile = G_incr_void_ptr(tile, map->tileXY * depths * length);
  72. Rast3d_set_null_value(tile, map->tileXY * zRedundant, type);
  73. return 1;
  74. }
  75. /*---------------------------------------------------------------------------*/
  76. static int Rast3d_readTileUncompressed(RASTER3D_Map * map, int tileIndex, int nofNum)
  77. {
  78. int nofBytes;
  79. nofBytes = nofNum * map->numLengthExtern;
  80. nofBytes = RASTER3D_MIN(nofBytes, map->fileEndPtr - map->index[tileIndex]);
  81. if (read(map->data_fd, xdr, nofBytes) != nofBytes) {
  82. Rast3d_error("Rast3d_readTileUncompressed: can't read file");
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. /*---------------------------------------------------------------------------*/
  88. static int Rast3d_readTileCompressed(RASTER3D_Map * map, int tileIndex, int nofNum)
  89. {
  90. if (!Rast3d_fpcompress_read_xdr_nums(map->data_fd, xdr, nofNum,
  91. map->tileLength[tileIndex],
  92. map->precision, tmpCompress,
  93. map->type == FCELL_TYPE)) {
  94. Rast3d_error
  95. ("Rast3d_readTileCompressed: error in Rast3d_fpcompress_read_xdr_nums");
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. /*---------------------------------------------------------------------------*/
  101. /*---------------------------------------------------------------------------*/
  102. /* EXPORTED FUNCTIONS */
  103. /*---------------------------------------------------------------------------*/
  104. /*!
  105. * \brief
  106. *
  107. *
  108. * Reads tile with index <em>tileIndex</em> into the <em>tile</em> buffer. The cells
  109. * are stored with type <em>type</em> which must be one of FCELL_TYPE and
  110. * DCELL_TYPE. If the tile with <em>tileIndex</em> is not stored on the file
  111. * corresponding to <em>map</em>, and <em>tileIndex</em> is a valid index <em>tile</em>
  112. * is filled with NULL-values.
  113. *
  114. * \param map
  115. * \param tileIndex
  116. * \param tile
  117. * \param type
  118. * \return 1 ... if successful,
  119. * 0 ... otherwise.
  120. */
  121. int Rast3d_read_tile(RASTER3D_Map * map, int tileIndex, void *tile, int type)
  122. {
  123. int nofNum, rows, cols, depths, xRedundant, yRedundant, zRedundant;
  124. if ((tileIndex >= map->nTiles) || (tileIndex < 0))
  125. Rast3d_fatal_error("Rast3d_read_tile: tile index out of range");
  126. if (map->index[tileIndex] == -1) {
  127. Rast3d_set_null_tile_type(map, tile, type);
  128. return 1;
  129. }
  130. nofNum = Rast3d_compute_clipped_tile_dimensions(map, tileIndex,
  131. &rows, &cols, &depths,
  132. &xRedundant, &yRedundant,
  133. &zRedundant);
  134. if (lseek(map->data_fd, map->index[tileIndex], SEEK_SET) == -1) {
  135. Rast3d_error("Rast3d_read_tile: can't position file");
  136. return 0;
  137. }
  138. if (map->compression == RASTER3D_NO_COMPRESSION) {
  139. if (!Rast3d_readTileUncompressed(map, tileIndex, nofNum)) {
  140. Rast3d_error("Rast3d_read_tile: error in Rast3d_readTileUncompressed");
  141. return 0;
  142. }
  143. }
  144. else if (!Rast3d_readTileCompressed(map, tileIndex, nofNum)) {
  145. Rast3d_error("Rast3d_read_tile: error in Rast3d_readTileCompressed");
  146. return 0;
  147. }
  148. if (!Rast3d_xdrTile2tile(map, tile, rows, cols, depths,
  149. xRedundant, yRedundant, zRedundant, nofNum, type)) {
  150. Rast3d_error("Rast3d_read_tile: error in Rast3d_xdrTile2tile");
  151. return 0;
  152. }
  153. if (Rast3d_mask_is_off(map))
  154. return 1;
  155. Rast3d_mask_tile(map, tileIndex, tile, type);
  156. return 1;
  157. }
  158. /*---------------------------------------------------------------------------*/
  159. /*!
  160. * \brief
  161. *
  162. * Is equivalent to Rast3d_read_tile (map, tileIndex, tile, FCELL_TYPE).
  163. *
  164. * \param map
  165. * \param tileIndex
  166. * \param tile
  167. * \return int
  168. */
  169. int Rast3d_read_tile_float(RASTER3D_Map * map, int tileIndex, void *tile)
  170. {
  171. if (!Rast3d_read_tile(map, tileIndex, tile, FCELL_TYPE)) {
  172. Rast3d_error("Rast3d_read_tile_float: error in Rast3d_read_tile");
  173. return 0;
  174. }
  175. return 1;
  176. }
  177. /*---------------------------------------------------------------------------*/
  178. /*!
  179. * \brief
  180. *
  181. * Is equivalent to Rast3d_read_tile (map, tileIndex, tile, DCELL_TYPE).
  182. *
  183. * \param map
  184. * \param tileIndex
  185. * \param tile
  186. * \return int
  187. */
  188. int Rast3d_read_tile_double(RASTER3D_Map * map, int tileIndex, void *tile)
  189. {
  190. if (!Rast3d_read_tile(map, tileIndex, tile, DCELL_TYPE)) {
  191. Rast3d_error("Rast3d_read_tile_double: error in Rast3d_read_tile");
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. /*---------------------------------------------------------------------------*/
  197. /* CACHE-MODE-ONLY FUNCTIONS */
  198. /*---------------------------------------------------------------------------*/
  199. /*!
  200. * \brief
  201. *
  202. * Locks tile with <em>tileIndex</em> in cache. If after locking fewer than the minimum number of
  203. * unlocked tiles are unlocked, the lock request is ignored.
  204. *
  205. * \param map
  206. * \param tileIndex
  207. * \return 1 ... if successful,
  208. * -1 ... if request is ignored,
  209. * 0 ... otherwise.
  210. */
  211. int Rast3d_lock_tile(RASTER3D_Map * map, int tileIndex)
  212. {
  213. if (!map->useCache)
  214. Rast3d_fatal_error("Rast3d_lock_tile: function invalid in non-cache mode");
  215. if (!Rast3d_cache_lock(map->cache, tileIndex)) {
  216. Rast3d_error("Rast3d_lock_tile: error in Rast3d_cache_lock");
  217. return 0;
  218. }
  219. return 1;
  220. }
  221. /*---------------------------------------------------------------------------*/
  222. /*!
  223. * \brief
  224. *
  225. * Unlocks tile with <em>tileIndex</em>.
  226. *
  227. * \param map
  228. * \param tileIndex
  229. * \return 1 ... if successful,
  230. * 0 ... otherwise.
  231. */
  232. int Rast3d_unlock_tile(RASTER3D_Map * map, int tileIndex)
  233. {
  234. if (!map->useCache)
  235. Rast3d_fatal_error("Rast3d_unlock_tile: function invalid in non-cache mode");
  236. if (!Rast3d_cache_unlock(map->cache, tileIndex)) {
  237. Rast3d_error("Rast3d_unlock_tile: error in Rast3d_cache_unlock");
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. /*---------------------------------------------------------------------------*/
  243. /*!
  244. * \brief
  245. *
  246. * Unlocks every tile in cache of <em>map</em>.
  247. *
  248. * \param map
  249. * \return 1 ... if successful,
  250. * 0 ... otherwise.
  251. */
  252. int Rast3d_unlock_all(RASTER3D_Map * map)
  253. {
  254. if (!map->useCache)
  255. Rast3d_fatal_error("Rast3d_unlock_all: function invalid in non-cache mode");
  256. if (!Rast3d_cache_unlock_all(map->cache)) {
  257. Rast3d_error("Rast3d_unlock_all: error in Rast3d_cache_unlock_all");
  258. return 0;
  259. }
  260. return 1;
  261. }
  262. /*---------------------------------------------------------------------------*/
  263. /*!
  264. * \brief
  265. *
  266. * Turns autolock mode on.
  267. *
  268. * \param map
  269. * \return void
  270. */
  271. void Rast3d_autolock_on(RASTER3D_Map * map)
  272. {
  273. if (!map->useCache)
  274. Rast3d_fatal_error("Rast3d_autoLockOn: function invalid in non-cache mode");
  275. Rast3d_cache_autolock_on(map->cache);
  276. }
  277. /*---------------------------------------------------------------------------*/
  278. /*!
  279. * \brief
  280. *
  281. * Turns autolock mode Off.
  282. *
  283. * \param map
  284. * \return void
  285. */
  286. void Rast3d_autolock_off(RASTER3D_Map * map)
  287. {
  288. if (!map->useCache)
  289. Rast3d_fatal_error("Rast3d_autoLockOff: function invalid in non-cache mode");
  290. Rast3d_cache_autolock_off(map->cache);
  291. }
  292. /*---------------------------------------------------------------------------*/
  293. /*!
  294. * \brief
  295. *
  296. * Sets the minimum
  297. * number of unlocked tiles to <em>minUnlocked</em>. This function should be used
  298. * in combination with <tt>Rast3d_unlock_all ()</tt> in order to avoid situations where the
  299. * new minimum is larger than the actual number of unlocked tiles.
  300. * <em>minUnlocked</em> must be one of RASTER3D_USE_CACHE_X, RASTER3D_USE_CACHE_Y,
  301. * RASTER3D_USE_CACHE_Z, RASTER3D_USE_CACHE_XY, RASTER3D_USE_CACHE_XZ,
  302. * RASTER3D_USE_CACHE_YZ, RASTER3D_USE_CACHE_XYZ, the result of Rast3d_cache_size_encode()
  303. * (cf.{g3d:G3d.cacheSizeEncode}), or any positive integer
  304. * which explicitly specifies the number of tiles.
  305. *
  306. * \param map
  307. * \param minUnlocked
  308. * \return void
  309. */
  310. void Rast3d_min_unlocked(RASTER3D_Map * map, int minUnlocked)
  311. {
  312. if (!map->useCache)
  313. Rast3d_fatal_error("Rast3d_autoLockOff: function invalid in non-cache mode");
  314. Rast3d_cache_set_min_unlock(map->cache,
  315. Rast3d__compute_cache_size(map, minUnlocked));
  316. }
  317. /*---------------------------------------------------------------------------*/
  318. /*!
  319. * \brief
  320. *
  321. * Starts a new cycle.
  322. *
  323. * \param map
  324. * \return 1 ... if successful,
  325. * 0 ... otherwise.
  326. */
  327. int Rast3d_begin_cycle(RASTER3D_Map * map)
  328. {
  329. if (!Rast3d_unlock_all(map)) {
  330. Rast3d_fatal_error("Rast3d_begin_cycle: error in Rast3d_unlock_all");
  331. return 0;
  332. }
  333. Rast3d_autolock_on(map);
  334. return 1;
  335. }
  336. /*---------------------------------------------------------------------------*/
  337. /*!
  338. * \brief
  339. *
  340. * Ends a cycle.
  341. *
  342. * \param map
  343. * \return 1 ... if successful,
  344. * 0 ... otherwise.
  345. */
  346. int Rast3d_end_cycle(RASTER3D_Map * map)
  347. {
  348. Rast3d_autolock_off(map);
  349. return 1;
  350. }