ImfTiledInputFile.h 14 KB

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