ImfTiledOutputFile.h 18 KB

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