ImfDeepTiledInputFile.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. //
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. // Copyright (c) Contributors to the OpenEXR Project.
  4. //
  5. #ifndef INCLUDED_IMF_DEEP_TILED_INPUT_FILE_H
  6. #define INCLUDED_IMF_DEEP_TILED_INPUT_FILE_H
  7. //-----------------------------------------------------------------------------
  8. //
  9. // class DeepTiledInputFile
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "ImfForward.h"
  13. #include "ImfThreading.h"
  14. #include "ImfGenericInputFile.h"
  15. #include "ImfTileDescription.h"
  16. #include <cstdint>
  17. #include <ImathBox.h>
  18. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  19. class IMF_EXPORT_TYPE DeepTiledInputFile : public GenericInputFile
  20. {
  21. public:
  22. //--------------------------------------------------------------------
  23. // A constructor that opens the file with the specified name, and
  24. // reads the file header. The constructor throws an IEX_NAMESPACE::ArgExc
  25. // exception if the file is not tiled.
  26. // The numThreads parameter specifies how many worker threads this
  27. // file will try to keep busy when decompressing individual tiles.
  28. // Destroying TiledInputFile objects constructed with this constructor
  29. // automatically closes the corresponding files.
  30. //--------------------------------------------------------------------
  31. IMF_EXPORT
  32. DeepTiledInputFile (const char fileName[],
  33. int numThreads = globalThreadCount ());
  34. // ----------------------------------------------------------
  35. // A constructor that attaches the new TiledInputFile object
  36. // to a file that has already been opened.
  37. // Destroying TiledInputFile objects constructed with this
  38. // constructor does not automatically close the corresponding
  39. // files.
  40. // ----------------------------------------------------------
  41. IMF_EXPORT
  42. DeepTiledInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int numThreads = globalThreadCount ());
  43. //-----------
  44. // Destructor
  45. //-----------
  46. IMF_EXPORT
  47. virtual ~DeepTiledInputFile ();
  48. //------------------------
  49. // Access to the file name
  50. //------------------------
  51. IMF_EXPORT
  52. const char * fileName () const;
  53. //--------------------------
  54. // Access to the file header
  55. //--------------------------
  56. IMF_EXPORT
  57. const Header & header () const;
  58. //----------------------------------
  59. // Access to the file format version
  60. //----------------------------------
  61. IMF_EXPORT
  62. int version () const;
  63. //-----------------------------------------------------------
  64. // Set the current frame buffer -- copies the FrameBuffer
  65. // object into the TiledInputFile object.
  66. //
  67. // The current frame buffer is the destination for the pixel
  68. // data read from the file. The current frame buffer must be
  69. // set at least once before readTile() is called.
  70. // The current frame buffer can be changed after each call
  71. // to readTile().
  72. //-----------------------------------------------------------
  73. IMF_EXPORT
  74. void setFrameBuffer (const DeepFrameBuffer &frameBuffer);
  75. //-----------------------------------
  76. // Access to the current frame buffer
  77. //-----------------------------------
  78. IMF_EXPORT
  79. const DeepFrameBuffer & frameBuffer () const;
  80. //------------------------------------------------------------
  81. // Check if the file is complete:
  82. //
  83. // isComplete() returns true if all pixels in the data window
  84. // (in all levels) are present in the input file, or false if
  85. // any pixels are missing. (Another program may still be busy
  86. // writing the file, or file writing may have been aborted
  87. // prematurely.)
  88. //------------------------------------------------------------
  89. IMF_EXPORT
  90. bool isComplete () const;
  91. //--------------------------------------------------
  92. // Utility functions:
  93. //--------------------------------------------------
  94. //---------------------------------------------------------
  95. // Multiresolution mode and tile size:
  96. // The following functions return the xSize, ySize and mode
  97. // fields of the file header's TileDescriptionAttribute.
  98. //---------------------------------------------------------
  99. IMF_EXPORT
  100. unsigned int tileXSize () const;
  101. IMF_EXPORT
  102. unsigned int tileYSize () const;
  103. IMF_EXPORT
  104. LevelMode levelMode () const;
  105. IMF_EXPORT
  106. LevelRoundingMode levelRoundingMode () const;
  107. //--------------------------------------------------------------------
  108. // Number of levels:
  109. //
  110. // numXLevels() returns the file's number of levels in x direction.
  111. //
  112. // if levelMode() == ONE_LEVEL:
  113. // return value is: 1
  114. //
  115. // if levelMode() == MIPMAP_LEVELS:
  116. // return value is: rfunc (log (max (w, h)) / log (2)) + 1
  117. //
  118. // if levelMode() == RIPMAP_LEVELS:
  119. // return value is: rfunc (log (w) / log (2)) + 1
  120. //
  121. // where
  122. // w is the width of the image's data window, max.x - min.x + 1,
  123. // y is the height of the image's data window, max.y - min.y + 1,
  124. // and rfunc(x) is either floor(x), or ceil(x), depending on
  125. // whether levelRoundingMode() returns ROUND_DOWN or ROUND_UP.
  126. //
  127. // numYLevels() returns the file's number of levels in y direction.
  128. //
  129. // if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
  130. // return value is the same as for numXLevels()
  131. //
  132. // if levelMode() == RIPMAP_LEVELS:
  133. // return value is: rfunc (log (h) / log (2)) + 1
  134. //
  135. //
  136. // numLevels() is a convenience function for use with
  137. // MIPMAP_LEVELS files.
  138. //
  139. // if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
  140. // return value is the same as for numXLevels()
  141. //
  142. // if levelMode() == RIPMAP_LEVELS:
  143. // an IEX_NAMESPACE::LogicExc exception is thrown
  144. //
  145. // isValidLevel(lx, ly) returns true if the file contains
  146. // a level with level number (lx, ly), false if not.
  147. //
  148. // totalTiles() returns the total number of tiles in the image
  149. //
  150. //--------------------------------------------------------------------
  151. IMF_EXPORT
  152. int numLevels () const;
  153. IMF_EXPORT
  154. int numXLevels () const;
  155. IMF_EXPORT
  156. int numYLevels () const;
  157. IMF_EXPORT
  158. bool isValidLevel (int lx, int ly) const;
  159. IMF_EXPORT
  160. size_t totalTiles() const;
  161. //----------------------------------------------------------
  162. // Dimensions of a level:
  163. //
  164. // levelWidth(lx) returns the width of a level with level
  165. // number (lx, *), where * is any number.
  166. //
  167. // return value is:
  168. // max (1, rfunc (w / pow (2, lx)))
  169. //
  170. //
  171. // levelHeight(ly) returns the height of a level with level
  172. // number (*, ly), where * is any number.
  173. //
  174. // return value is:
  175. // max (1, rfunc (h / pow (2, ly)))
  176. //
  177. //----------------------------------------------------------
  178. IMF_EXPORT
  179. int levelWidth (int lx) const;
  180. IMF_EXPORT
  181. int levelHeight (int ly) const;
  182. //--------------------------------------------------------------
  183. // Number of tiles:
  184. //
  185. // numXTiles(lx) returns the number of tiles in x direction
  186. // that cover a level with level number (lx, *), where * is
  187. // any number.
  188. //
  189. // return value is:
  190. // (levelWidth(lx) + tileXSize() - 1) / tileXSize()
  191. //
  192. //
  193. // numYTiles(ly) returns the number of tiles in y direction
  194. // that cover a level with level number (*, ly), where * is
  195. // any number.
  196. //
  197. // return value is:
  198. // (levelHeight(ly) + tileXSize() - 1) / tileXSize()
  199. //
  200. //--------------------------------------------------------------
  201. IMF_EXPORT
  202. int numXTiles (int lx = 0) const;
  203. IMF_EXPORT
  204. int numYTiles (int ly = 0) const;
  205. //---------------------------------------------------------------
  206. // Level pixel ranges:
  207. //
  208. // dataWindowForLevel(lx, ly) returns a 2-dimensional region of
  209. // valid pixel coordinates for a level with level number (lx, ly)
  210. //
  211. // return value is a Box2i with min value:
  212. // (dataWindow.min.x, dataWindow.min.y)
  213. //
  214. // and max value:
  215. // (dataWindow.min.x + levelWidth(lx) - 1,
  216. // dataWindow.min.y + levelHeight(ly) - 1)
  217. //
  218. // dataWindowForLevel(level) is a convenience function used
  219. // for ONE_LEVEL and MIPMAP_LEVELS files. It returns
  220. // dataWindowForLevel(level, level).
  221. //
  222. //---------------------------------------------------------------
  223. IMF_EXPORT
  224. IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
  225. IMF_EXPORT
  226. IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
  227. //-------------------------------------------------------------------
  228. // Tile pixel ranges:
  229. //
  230. // dataWindowForTile(dx, dy, lx, ly) returns a 2-dimensional
  231. // region of valid pixel coordinates for a tile with tile coordinates
  232. // (dx,dy) and level number (lx, ly).
  233. //
  234. // return value is a Box2i with min value:
  235. // (dataWindow.min.x + dx * tileXSize(),
  236. // dataWindow.min.y + dy * tileYSize())
  237. //
  238. // and max value:
  239. // (dataWindow.min.x + (dx + 1) * tileXSize() - 1,
  240. // dataWindow.min.y + (dy + 1) * tileYSize() - 1)
  241. //
  242. // dataWindowForTile(dx, dy, level) is a convenience function
  243. // used for ONE_LEVEL and MIPMAP_LEVELS files. It returns
  244. // dataWindowForTile(dx, dy, level, level).
  245. //
  246. //-------------------------------------------------------------------
  247. IMF_EXPORT
  248. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy, int l = 0) const;
  249. IMF_EXPORT
  250. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  251. int lx, int ly) const;
  252. //------------------------------------------------------------
  253. // Read pixel data:
  254. //
  255. // readTile(dx, dy, lx, ly) reads the tile with tile
  256. // coordinates (dx, dy), and level number (lx, ly),
  257. // and stores it in the current frame buffer.
  258. //
  259. // dx must lie in the interval [0, numXTiles(lx)-1]
  260. // dy must lie in the interval [0, numYTiles(ly)-1]
  261. //
  262. // lx must lie in the interval [0, numXLevels()-1]
  263. // ly must lie in the interval [0, numYLevels()-1]
  264. //
  265. // readTile(dx, dy, level) is a convenience function used
  266. // for ONE_LEVEL and MIPMAP_LEVELS files. It calls
  267. // readTile(dx, dy, level, level).
  268. //
  269. // The two readTiles(dx1, dx2, dy1, dy2, ...) functions allow
  270. // reading multiple tiles at once. If multi-threading is used
  271. // the multiple tiles are read concurrently.
  272. //
  273. // Pixels that are outside the pixel coordinate range for the
  274. // tile's level, are never accessed by readTile().
  275. //
  276. // Attempting to access a tile that is not present in the file
  277. // throws an InputExc exception.
  278. //
  279. //------------------------------------------------------------
  280. IMF_EXPORT
  281. void readTile (int dx, int dy, int l = 0);
  282. IMF_EXPORT
  283. void readTile (int dx, int dy, int lx, int ly);
  284. IMF_EXPORT
  285. void readTiles (int dx1, int dx2, int dy1, int dy2,
  286. int lx, int ly);
  287. IMF_EXPORT
  288. void readTiles (int dx1, int dx2, int dy1, int dy2,
  289. int l = 0);
  290. //--------------------------------------------------
  291. // Read a tile of raw pixel data from the file,
  292. // without uncompressing it (this function is
  293. // used to implement TiledOutputFile::copyPixels()).
  294. //--------------------------------------------------
  295. IMF_EXPORT
  296. void rawTileData (int &dx, int &dy,
  297. int &lx, int &ly,
  298. char *pixelData,
  299. uint64_t &dataSize) const;
  300. //------------------------------------------------------------------
  301. // Read pixel sample counts into a slice in the frame buffer.
  302. //
  303. // readPixelSampleCount(dx, dy, lx, ly) reads the sample counts
  304. // for tile (dx, dy) in level (lx, ly).
  305. //
  306. // readPixelSampleCount(dx, dy, l) calls
  307. // readPixelSampleCount(dx, dy, lx = l, ly = l)
  308. //
  309. // dx must lie in the interval [0, numXTiles(lx)-1]
  310. // dy must lie in the interval [0, numYTiles(ly)-1]
  311. //
  312. // lx must lie in the interval [0, numXLevels()-1]
  313. // ly must lie in the interval [0, numYLevels()-1]
  314. //
  315. // readPixelSampleCounts(dx1, dx2, dy1, dy2, lx, ly) reads all
  316. // the sample counts for tiles within range
  317. // [(min(dx1, dx2), min(dy1, dy2))...(max(dx1, dx2), max(dy1, dy2)],
  318. // and on level (lx, ly)
  319. //
  320. // readPixelSampleCounts(dx1, dx2, dy1, dy2, l) calls
  321. // readPixelSampleCounts(dx1, dx2, dy1, dy2, lx = l, ly = l).
  322. //------------------------------------------------------------------
  323. IMF_EXPORT
  324. void readPixelSampleCount (int dx, int dy, int l = 0);
  325. IMF_EXPORT
  326. void readPixelSampleCount (int dx, int dy, int lx, int ly);
  327. IMF_EXPORT
  328. void readPixelSampleCounts (int dx1, int dx2,
  329. int dy1, int dy2,
  330. int lx, int ly);
  331. IMF_EXPORT
  332. void readPixelSampleCounts (int dx1, int dx2,
  333. int dy1, int dy2,
  334. int l = 0);
  335. struct Data;
  336. private:
  337. friend class InputFile;
  338. friend class MultiPartInputFile;
  339. DeepTiledInputFile (InputPartData* part);
  340. DeepTiledInputFile (const DeepTiledInputFile &) = delete;
  341. DeepTiledInputFile & operator = (const DeepTiledInputFile &) = delete;
  342. DeepTiledInputFile (DeepTiledInputFile &&) = delete;
  343. DeepTiledInputFile & operator = (DeepTiledInputFile &&) = delete;
  344. DeepTiledInputFile (const Header &header, OPENEXR_IMF_INTERNAL_NAMESPACE::IStream *is, int version,
  345. int numThreads);
  346. void initialize ();
  347. void multiPartInitialize(InputPartData* part);
  348. void compatibilityInitialize(OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is);
  349. bool isValidTile (int dx, int dy,
  350. int lx, int ly) const;
  351. size_t bytesPerLineForTile (int dx, int dy,
  352. int lx, int ly) const;
  353. void getTileOrder(int dx[],int dy[],int lx[],int ly[]) const;
  354. Data * _data;
  355. // needed for copyPixels
  356. friend class DeepTiledOutputFile;
  357. };
  358. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  359. #endif