ImfDeepTiledInputFile.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2011, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. #ifndef INCLUDED_IMF_DEEP_TILED_INPUT_FILE_H
  35. #define INCLUDED_IMF_DEEP_TILED_INPUT_FILE_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // class DeepTiledInputFile
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "ImfHeader.h"
  42. #include "ImfFrameBuffer.h"
  43. #include "ImathBox.h"
  44. #include "ImfTileDescription.h"
  45. #include "ImfThreading.h"
  46. #include "ImfGenericInputFile.h"
  47. #include "ImfDeepFrameBuffer.h"
  48. #include "ImfDeepTiledOutputFile.h"
  49. #include "ImfForward.h"
  50. #include "ImfNamespace.h"
  51. #include "ImfExport.h"
  52. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  53. class DeepTiledInputFile : public GenericInputFile
  54. {
  55. public:
  56. //--------------------------------------------------------------------
  57. // A constructor that opens the file with the specified name, and
  58. // reads the file header. The constructor throws an IEX_NAMESPACE::ArgExc
  59. // exception if the file is not tiled.
  60. // The numThreads parameter specifies how many worker threads this
  61. // file will try to keep busy when decompressing individual tiles.
  62. // Destroying TiledInputFile objects constructed with this constructor
  63. // automatically closes the corresponding files.
  64. //--------------------------------------------------------------------
  65. IMF_EXPORT
  66. DeepTiledInputFile (const char fileName[],
  67. int numThreads = globalThreadCount ());
  68. // ----------------------------------------------------------
  69. // A constructor that attaches the new TiledInputFile object
  70. // to a file that has already been opened.
  71. // Destroying TiledInputFile objects constructed with this
  72. // constructor does not automatically close the corresponding
  73. // files.
  74. // ----------------------------------------------------------
  75. IMF_EXPORT
  76. DeepTiledInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int numThreads = globalThreadCount ());
  77. //-----------
  78. // Destructor
  79. //-----------
  80. IMF_EXPORT
  81. virtual ~DeepTiledInputFile ();
  82. //------------------------
  83. // Access to the file name
  84. //------------------------
  85. IMF_EXPORT
  86. const char * fileName () const;
  87. //--------------------------
  88. // Access to the file header
  89. //--------------------------
  90. IMF_EXPORT
  91. const Header & header () const;
  92. //----------------------------------
  93. // Access to the file format version
  94. //----------------------------------
  95. IMF_EXPORT
  96. int version () const;
  97. //-----------------------------------------------------------
  98. // Set the current frame buffer -- copies the FrameBuffer
  99. // object into the TiledInputFile object.
  100. //
  101. // The current frame buffer is the destination for the pixel
  102. // data read from the file. The current frame buffer must be
  103. // set at least once before readTile() is called.
  104. // The current frame buffer can be changed after each call
  105. // to readTile().
  106. //-----------------------------------------------------------
  107. IMF_EXPORT
  108. void setFrameBuffer (const DeepFrameBuffer &frameBuffer);
  109. //-----------------------------------
  110. // Access to the current frame buffer
  111. //-----------------------------------
  112. IMF_EXPORT
  113. const DeepFrameBuffer & frameBuffer () const;
  114. //------------------------------------------------------------
  115. // Check if the file is complete:
  116. //
  117. // isComplete() returns true if all pixels in the data window
  118. // (in all levels) are present in the input file, or false if
  119. // any pixels are missing. (Another program may still be busy
  120. // writing the file, or file writing may have been aborted
  121. // prematurely.)
  122. //------------------------------------------------------------
  123. IMF_EXPORT
  124. bool isComplete () const;
  125. //--------------------------------------------------
  126. // Utility functions:
  127. //--------------------------------------------------
  128. //---------------------------------------------------------
  129. // Multiresolution mode and tile size:
  130. // The following functions return the xSize, ySize and mode
  131. // fields of the file header's TileDescriptionAttribute.
  132. //---------------------------------------------------------
  133. IMF_EXPORT
  134. unsigned int tileXSize () const;
  135. IMF_EXPORT
  136. unsigned int tileYSize () const;
  137. IMF_EXPORT
  138. LevelMode levelMode () const;
  139. IMF_EXPORT
  140. LevelRoundingMode levelRoundingMode () const;
  141. //--------------------------------------------------------------------
  142. // Number of levels:
  143. //
  144. // numXLevels() returns the file's number of levels in x direction.
  145. //
  146. // if levelMode() == ONE_LEVEL:
  147. // return value is: 1
  148. //
  149. // if levelMode() == MIPMAP_LEVELS:
  150. // return value is: rfunc (log (max (w, h)) / log (2)) + 1
  151. //
  152. // if levelMode() == RIPMAP_LEVELS:
  153. // return value is: rfunc (log (w) / log (2)) + 1
  154. //
  155. // where
  156. // w is the width of the image's data window, max.x - min.x + 1,
  157. // y is the height of the image's data window, max.y - min.y + 1,
  158. // and rfunc(x) is either floor(x), or ceil(x), depending on
  159. // whether levelRoundingMode() returns ROUND_DOWN or ROUND_UP.
  160. //
  161. // numYLevels() returns the file's number of levels in y direction.
  162. //
  163. // if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
  164. // return value is the same as for numXLevels()
  165. //
  166. // if levelMode() == RIPMAP_LEVELS:
  167. // return value is: rfunc (log (h) / log (2)) + 1
  168. //
  169. //
  170. // numLevels() is a convenience function for use with
  171. // MIPMAP_LEVELS files.
  172. //
  173. // if levelMode() == ONE_LEVEL or levelMode() == MIPMAP_LEVELS:
  174. // return value is the same as for numXLevels()
  175. //
  176. // if levelMode() == RIPMAP_LEVELS:
  177. // an IEX_NAMESPACE::LogicExc exception is thrown
  178. //
  179. // isValidLevel(lx, ly) returns true if the file contains
  180. // a level with level number (lx, ly), false if not.
  181. //
  182. // totalTiles() returns the total number of tiles in the image
  183. //
  184. //--------------------------------------------------------------------
  185. IMF_EXPORT
  186. int numLevels () const;
  187. IMF_EXPORT
  188. int numXLevels () const;
  189. IMF_EXPORT
  190. int numYLevels () const;
  191. IMF_EXPORT
  192. bool isValidLevel (int lx, int ly) const;
  193. IMF_EXPORT
  194. size_t totalTiles() const;
  195. //----------------------------------------------------------
  196. // Dimensions of a level:
  197. //
  198. // levelWidth(lx) returns the width of a level with level
  199. // number (lx, *), where * is any number.
  200. //
  201. // return value is:
  202. // max (1, rfunc (w / pow (2, lx)))
  203. //
  204. //
  205. // levelHeight(ly) returns the height of a level with level
  206. // number (*, ly), where * is any number.
  207. //
  208. // return value is:
  209. // max (1, rfunc (h / pow (2, ly)))
  210. //
  211. //----------------------------------------------------------
  212. IMF_EXPORT
  213. int levelWidth (int lx) const;
  214. IMF_EXPORT
  215. int levelHeight (int ly) const;
  216. //--------------------------------------------------------------
  217. // Number of tiles:
  218. //
  219. // numXTiles(lx) returns the number of tiles in x direction
  220. // that cover a level with level number (lx, *), where * is
  221. // any number.
  222. //
  223. // return value is:
  224. // (levelWidth(lx) + tileXSize() - 1) / tileXSize()
  225. //
  226. //
  227. // numYTiles(ly) returns the number of tiles in y direction
  228. // that cover a level with level number (*, ly), where * is
  229. // any number.
  230. //
  231. // return value is:
  232. // (levelHeight(ly) + tileXSize() - 1) / tileXSize()
  233. //
  234. //--------------------------------------------------------------
  235. IMF_EXPORT
  236. int numXTiles (int lx = 0) const;
  237. IMF_EXPORT
  238. int numYTiles (int ly = 0) const;
  239. //---------------------------------------------------------------
  240. // Level pixel ranges:
  241. //
  242. // dataWindowForLevel(lx, ly) returns a 2-dimensional region of
  243. // valid pixel coordinates for a level with level number (lx, ly)
  244. //
  245. // return value is a Box2i with min value:
  246. // (dataWindow.min.x, dataWindow.min.y)
  247. //
  248. // and max value:
  249. // (dataWindow.min.x + levelWidth(lx) - 1,
  250. // dataWindow.min.y + levelHeight(ly) - 1)
  251. //
  252. // dataWindowForLevel(level) is a convenience function used
  253. // for ONE_LEVEL and MIPMAP_LEVELS files. It returns
  254. // dataWindowForLevel(level, level).
  255. //
  256. //---------------------------------------------------------------
  257. IMF_EXPORT
  258. IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
  259. IMF_EXPORT
  260. IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
  261. //-------------------------------------------------------------------
  262. // Tile pixel ranges:
  263. //
  264. // dataWindowForTile(dx, dy, lx, ly) returns a 2-dimensional
  265. // region of valid pixel coordinates for a tile with tile coordinates
  266. // (dx,dy) and level number (lx, ly).
  267. //
  268. // return value is a Box2i with min value:
  269. // (dataWindow.min.x + dx * tileXSize(),
  270. // dataWindow.min.y + dy * tileYSize())
  271. //
  272. // and max value:
  273. // (dataWindow.min.x + (dx + 1) * tileXSize() - 1,
  274. // dataWindow.min.y + (dy + 1) * tileYSize() - 1)
  275. //
  276. // dataWindowForTile(dx, dy, level) is a convenience function
  277. // used for ONE_LEVEL and MIPMAP_LEVELS files. It returns
  278. // dataWindowForTile(dx, dy, level, level).
  279. //
  280. //-------------------------------------------------------------------
  281. IMF_EXPORT
  282. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy, int l = 0) const;
  283. IMF_EXPORT
  284. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  285. int lx, int ly) const;
  286. //------------------------------------------------------------
  287. // Read pixel data:
  288. //
  289. // readTile(dx, dy, lx, ly) reads the tile with tile
  290. // coordinates (dx, dy), and level number (lx, ly),
  291. // and stores it in the current frame buffer.
  292. //
  293. // dx must lie in the interval [0, numXTiles(lx)-1]
  294. // dy must lie in the interval [0, numYTiles(ly)-1]
  295. //
  296. // lx must lie in the interval [0, numXLevels()-1]
  297. // ly must lie in the inverval [0, numYLevels()-1]
  298. //
  299. // readTile(dx, dy, level) is a convenience function used
  300. // for ONE_LEVEL and MIPMAP_LEVELS files. It calls
  301. // readTile(dx, dy, level, level).
  302. //
  303. // The two readTiles(dx1, dx2, dy1, dy2, ...) functions allow
  304. // reading multiple tiles at once. If multi-threading is used
  305. // the multiple tiles are read concurrently.
  306. //
  307. // Pixels that are outside the pixel coordinate range for the
  308. // tile's level, are never accessed by readTile().
  309. //
  310. // Attempting to access a tile that is not present in the file
  311. // throws an InputExc exception.
  312. //
  313. //------------------------------------------------------------
  314. IMF_EXPORT
  315. void readTile (int dx, int dy, int l = 0);
  316. IMF_EXPORT
  317. void readTile (int dx, int dy, int lx, int ly);
  318. IMF_EXPORT
  319. void readTiles (int dx1, int dx2, int dy1, int dy2,
  320. int lx, int ly);
  321. IMF_EXPORT
  322. void readTiles (int dx1, int dx2, int dy1, int dy2,
  323. int l = 0);
  324. //--------------------------------------------------
  325. // Read a tile of raw pixel data from the file,
  326. // without uncompressing it (this function is
  327. // used to implement TiledOutputFile::copyPixels()).
  328. //--------------------------------------------------
  329. IMF_EXPORT
  330. void rawTileData (int &dx, int &dy,
  331. int &lx, int &ly,
  332. char *pixelData,
  333. Int64 &dataSize) const;
  334. //------------------------------------------------------------------
  335. // Read pixel sample counts into a slice in the frame buffer.
  336. //
  337. // readPixelSampleCount(dx, dy, lx, ly) reads the sample counts
  338. // for tile (dx, dy) in level (lx, ly).
  339. //
  340. // readPixelSampleCount(dx, dy, l) calls
  341. // readPixelSampleCount(dx, dy, lx = l, ly = l)
  342. //
  343. // dx must lie in the interval [0, numXTiles(lx)-1]
  344. // dy must lie in the interval [0, numYTiles(ly)-1]
  345. //
  346. // lx must lie in the interval [0, numXLevels()-1]
  347. // ly must lie in the inverval [0, numYLevels()-1]
  348. //
  349. // readPixelSampleCounts(dx1, dx2, dy1, dy2, lx, ly) reads all
  350. // the sample counts for tiles within range
  351. // [(min(dx1, dx2), min(dy1, dy2))...(max(dx1, dx2), max(dy1, dy2)],
  352. // and on level (lx, ly)
  353. //
  354. // readPixelSampleCounts(dx1, dx2, dy1, dy2, l) calls
  355. // readPixelSampleCounts(dx1, dx2, dy1, dy2, lx = l, ly = l).
  356. //------------------------------------------------------------------
  357. IMF_EXPORT
  358. void readPixelSampleCount (int dx, int dy, int l = 0);
  359. IMF_EXPORT
  360. void readPixelSampleCount (int dx, int dy, int lx, int ly);
  361. IMF_EXPORT
  362. void readPixelSampleCounts (int dx1, int dx2,
  363. int dy1, int dy2,
  364. int lx, int ly);
  365. IMF_EXPORT
  366. void readPixelSampleCounts (int dx1, int dx2,
  367. int dy1, int dy2,
  368. int l = 0);
  369. struct Data;
  370. private:
  371. friend class InputFile;
  372. friend class MultiPartInputFile;
  373. DeepTiledInputFile (InputPartData* part);
  374. DeepTiledInputFile (const DeepTiledInputFile &); // not implemented
  375. DeepTiledInputFile & operator = (const DeepTiledInputFile &); // not implemented
  376. DeepTiledInputFile (const Header &header, OPENEXR_IMF_INTERNAL_NAMESPACE::IStream *is, int version,
  377. int numThreads);
  378. void initialize ();
  379. void multiPartInitialize(InputPartData* part);
  380. void compatibilityInitialize(OPENEXR_IMF_INTERNAL_NAMESPACE::IStream& is);
  381. bool isValidTile (int dx, int dy,
  382. int lx, int ly) const;
  383. size_t bytesPerLineForTile (int dx, int dy,
  384. int lx, int ly) const;
  385. void getTileOrder(int dx[],int dy[],int lx[],int ly[]) const;
  386. Data * _data;
  387. // needed for copyPixels
  388. friend void DeepTiledOutputFile::copyPixels(DeepTiledInputFile &);
  389. };
  390. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  391. #endif