tileread.c 11 KB

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