ImfDeepTiledOutputFile.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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_OUTPUT_FILE_H
  35. #define INCLUDED_IMF_DEEP_TILED_OUTPUT_FILE_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // class DeepTiledOutputFile
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "ImfHeader.h"
  42. #include "ImfFrameBuffer.h"
  43. #include "ImathBox.h"
  44. #include "ImfThreading.h"
  45. #include "ImfGenericOutputFile.h"
  46. #include "ImfNamespace.h"
  47. #include "ImfForward.h"
  48. #include "ImfExport.h"
  49. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  50. class DeepTiledOutputFile : public GenericOutputFile
  51. {
  52. public:
  53. //-------------------------------------------------------------------
  54. // A constructor that opens the file with the specified name, and
  55. // writes the file header. The file header is also copied into the
  56. // TiledOutputFile object, and can later be accessed via the header()
  57. // method.
  58. //
  59. // Destroying TiledOutputFile constructed with this constructor
  60. // automatically closes the corresponding files.
  61. //
  62. // The header must contain a TileDescriptionAttribute called "tiles".
  63. //
  64. // The x and y subsampling factors for all image channels must be 1;
  65. // subsampling is not supported.
  66. //
  67. // Tiles can be written to the file in arbitrary order. The line
  68. // order attribute can be used to cause the tiles to be sorted in
  69. // the file. When the file is read later, reading the tiles in the
  70. // same order as they are in the file tends to be significantly
  71. // faster than reading the tiles in random order (see writeTile,
  72. // below).
  73. //-------------------------------------------------------------------
  74. IMF_EXPORT
  75. DeepTiledOutputFile (const char fileName[],
  76. const Header &header,
  77. int numThreads = globalThreadCount ());
  78. // ----------------------------------------------------------------
  79. // A constructor that attaches the new TiledOutputFile object to
  80. // a file that has already been opened. Destroying TiledOutputFile
  81. // objects constructed with this constructor does not automatically
  82. // close the corresponding files.
  83. // ----------------------------------------------------------------
  84. IMF_EXPORT
  85. DeepTiledOutputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  86. const Header &header,
  87. int numThreads = globalThreadCount ());
  88. //-----------------------------------------------------
  89. // Destructor
  90. //
  91. // Destroying a TiledOutputFile object before all tiles
  92. // have been written results in an incomplete file.
  93. //-----------------------------------------------------
  94. IMF_EXPORT
  95. virtual ~DeepTiledOutputFile ();
  96. //------------------------
  97. // Access to the file name
  98. //------------------------
  99. IMF_EXPORT
  100. const char * fileName () const;
  101. //--------------------------
  102. // Access to the file header
  103. //--------------------------
  104. IMF_EXPORT
  105. const Header & header () const;
  106. //-------------------------------------------------------
  107. // Set the current frame buffer -- copies the FrameBuffer
  108. // object into the TiledOutputFile object.
  109. //
  110. // The current frame buffer is the source of the pixel
  111. // data written to the file. The current frame buffer
  112. // must be set at least once before writeTile() is
  113. // called. The current frame buffer can be changed
  114. // after each call to writeTile().
  115. //-------------------------------------------------------
  116. IMF_EXPORT
  117. void setFrameBuffer (const DeepFrameBuffer &frameBuffer);
  118. //-----------------------------------
  119. // Access to the current frame buffer
  120. //-----------------------------------
  121. IMF_EXPORT
  122. const DeepFrameBuffer & frameBuffer () 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 MIPMAP_LEVELS
  169. // 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
  237. // region of valid pixel coordinates for a level with
  238. // level number (lx, ly)
  239. //
  240. // return value is a Box2i with min value:
  241. // (dataWindow.min.x, dataWindow.min.y)
  242. //
  243. // and max value:
  244. // (dataWindow.min.x + levelWidth(lx) - 1,
  245. // dataWindow.min.y + levelHeight(ly) - 1)
  246. //
  247. // dataWindowForLevel(level) is a convenience function used
  248. // for ONE_LEVEL and MIPMAP_LEVELS files. It returns
  249. // dataWindowForLevel(level, level).
  250. //
  251. //---------------------------------------------------------
  252. IMF_EXPORT
  253. IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
  254. IMF_EXPORT
  255. IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
  256. //-------------------------------------------------------------------
  257. // Tile pixel ranges:
  258. //
  259. // dataWindowForTile(dx, dy, lx, ly) returns a 2-dimensional
  260. // region of valid pixel coordinates for a tile with tile coordinates
  261. // (dx,dy) and level number (lx, ly).
  262. //
  263. // return value is a Box2i with min value:
  264. // (dataWindow.min.x + dx * tileXSize(),
  265. // dataWindow.min.y + dy * tileYSize())
  266. //
  267. // and max value:
  268. // (dataWindow.min.x + (dx + 1) * tileXSize() - 1,
  269. // dataWindow.min.y + (dy + 1) * tileYSize() - 1)
  270. //
  271. // dataWindowForTile(dx, dy, level) is a convenience function
  272. // used for ONE_LEVEL and MIPMAP_LEVELS files. It returns
  273. // dataWindowForTile(dx, dy, level, level).
  274. //
  275. //-------------------------------------------------------------------
  276. IMF_EXPORT
  277. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  278. int l = 0) const;
  279. IMF_EXPORT
  280. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  281. int lx, int ly) const;
  282. //------------------------------------------------------------------
  283. // Write pixel data:
  284. //
  285. // writeTile(dx, dy, lx, ly) writes the tile with tile
  286. // coordinates (dx, dy), and level number (lx, ly) to
  287. // the file.
  288. //
  289. // dx must lie in the interval [0, numXTiles(lx) - 1]
  290. // dy must lie in the interval [0, numYTiles(ly) - 1]
  291. //
  292. // lx must lie in the interval [0, numXLevels() - 1]
  293. // ly must lie in the inverval [0, numYLevels() - 1]
  294. //
  295. // writeTile(dx, dy, level) is a convenience function
  296. // used for ONE_LEVEL and MIPMAP_LEVEL files. It calls
  297. // writeTile(dx, dy, level, level).
  298. //
  299. // The two writeTiles(dx1, dx2, dy1, dy2, ...) functions allow
  300. // writing multiple tiles at once. If multi-threading is used
  301. // multiple tiles are written concurrently. The tile coordinates,
  302. // dx1, dx2 and dy1, dy2, specify inclusive ranges of tile
  303. // coordinates. It is valid for dx1 < dx2 or dy1 < dy2; the
  304. // tiles are always written in the order specified by the line
  305. // order attribute. Hence, it is not possible to specify an
  306. // "invalid" or empty tile range.
  307. //
  308. // Pixels that are outside the pixel coordinate range for the tile's
  309. // level, are never accessed by writeTile().
  310. //
  311. // Each tile in the file must be written exactly once.
  312. //
  313. // The file's line order attribute determines the order of the tiles
  314. // in the file:
  315. //
  316. // INCREASING_Y In the file, the tiles for each level are stored
  317. // in a contiguous block. The levels are ordered
  318. // like this:
  319. //
  320. // (0, 0) (1, 0) ... (nx-1, 0)
  321. // (0, 1) (1, 1) ... (nx-1, 1)
  322. // ...
  323. // (0,ny-1) (1,ny-1) ... (nx-1,ny-1)
  324. //
  325. // where nx = numXLevels(), and ny = numYLevels().
  326. // In an individual level, (lx, ly), the tiles
  327. // are stored in the following order:
  328. //
  329. // (0, 0) (1, 0) ... (tx-1, 0)
  330. // (0, 1) (1, 1) ... (tx-1, 1)
  331. // ...
  332. // (0,ty-1) (1,ty-1) ... (tx-1,ty-1)
  333. //
  334. // where tx = numXTiles(lx),
  335. // and ty = numYTiles(ly).
  336. //
  337. // DECREASING_Y As for INCREASING_Y, the tiles for each level
  338. // are stored in a contiguous block. The levels
  339. // are ordered the same way as for INCREASING_Y,
  340. // but within an individual level, the tiles
  341. // are stored in this order:
  342. //
  343. // (0,ty-1) (1,ty-1) ... (tx-1,ty-1)
  344. // ...
  345. // (0, 1) (1, 1) ... (tx-1, 1)
  346. // (0, 0) (1, 0) ... (tx-1, 0)
  347. //
  348. //
  349. // RANDOM_Y The order of the calls to writeTile() determines
  350. // the order of the tiles in the file.
  351. //
  352. //------------------------------------------------------------------
  353. IMF_EXPORT
  354. void writeTile (int dx, int dy, int l = 0);
  355. IMF_EXPORT
  356. void writeTile (int dx, int dy, int lx, int ly);
  357. IMF_EXPORT
  358. void writeTiles (int dx1, int dx2, int dy1, int dy2,
  359. int lx, int ly);
  360. IMF_EXPORT
  361. void writeTiles (int dx1, int dx2, int dy1, int dy2,
  362. int l = 0);
  363. //------------------------------------------------------------------
  364. // Shortcut to copy all pixels from a TiledInputFile into this file,
  365. // without uncompressing and then recompressing the pixel data.
  366. // This file's header must be compatible with the TiledInputFile's
  367. // header: The two header's "dataWindow", "compression",
  368. // "lineOrder", "channels", and "tiles" attributes must be the same.
  369. //------------------------------------------------------------------
  370. IMF_EXPORT
  371. void copyPixels (DeepTiledInputFile &in);
  372. IMF_EXPORT
  373. void copyPixels (DeepTiledInputPart &in);
  374. //--------------------------------------------------------------
  375. // Updating the preview image:
  376. //
  377. // updatePreviewImage() supplies a new set of pixels for the
  378. // preview image attribute in the file's header. If the header
  379. // does not contain a preview image, updatePreviewImage() throws
  380. // an IEX_NAMESPACE::LogicExc.
  381. //
  382. // Note: updatePreviewImage() is necessary because images are
  383. // often stored in a file incrementally, a few tiles at a time,
  384. // while the image is being generated. Since the preview image
  385. // is an attribute in the file's header, it gets stored in the
  386. // file as soon as the file is opened, but we may not know what
  387. // the preview image should look like until we have written the
  388. // last tile of the main image.
  389. //
  390. //--------------------------------------------------------------
  391. IMF_EXPORT
  392. void updatePreviewImage (const PreviewRgba newPixels[]);
  393. //-------------------------------------------------------------
  394. // Break a tile -- for testing and debugging only:
  395. //
  396. // breakTile(dx,dy,lx,ly,p,n,c) introduces an error into the
  397. // output file by writing n copies of character c, starting
  398. // p bytes from the beginning of the tile with tile coordinates
  399. // (dx, dy) and level number (lx, ly).
  400. //
  401. // Warning: Calling this function usually results in a broken
  402. // image file. The file or parts of it may not be readable,
  403. // or the file may contain bad data.
  404. //
  405. //-------------------------------------------------------------
  406. IMF_EXPORT
  407. void breakTile (int dx, int dy,
  408. int lx, int ly,
  409. int offset,
  410. int length,
  411. char c);
  412. struct Data;
  413. private:
  414. // ----------------------------------------------------------------
  415. // A constructor attaches the OutputStreamMutex to the
  416. // given one from MultiPartOutputFile. Set the previewPosition
  417. // and lineOffsetsPosition which have been acquired from
  418. // the constructor of MultiPartOutputFile as well.
  419. // ----------------------------------------------------------------
  420. DeepTiledOutputFile (const OutputPartData* part);
  421. DeepTiledOutputFile (const DeepTiledOutputFile &); // not implemented
  422. DeepTiledOutputFile & operator = (const DeepTiledOutputFile &); // not implemented
  423. void initialize (const Header &header);
  424. bool isValidTile (int dx, int dy,
  425. int lx, int ly) const;
  426. size_t bytesPerLineForTile (int dx, int dy,
  427. int lx, int ly) const;
  428. Data * _data;
  429. friend class MultiPartOutputFile;
  430. };
  431. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  432. #endif