ImfTiledInputFile.h 13 KB

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