ImfTiledRgbaFile.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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_RGBA_FILE_H
  35. #define INCLUDED_IMF_TILED_RGBA_FILE_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Simplified RGBA image I/O for tiled files
  39. //
  40. // class TiledRgbaOutputFile
  41. // class TiledRgbaInputFile
  42. //
  43. //-----------------------------------------------------------------------------
  44. #include "ImfHeader.h"
  45. #include "ImfFrameBuffer.h"
  46. #include "ImathVec.h"
  47. #include "ImathBox.h"
  48. #include "half.h"
  49. #include "ImfTileDescription.h"
  50. #include "ImfRgba.h"
  51. #include "ImfThreading.h"
  52. #include <string>
  53. #include "ImfNamespace.h"
  54. #include "ImfForward.h"
  55. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  56. //
  57. // Tiled RGBA output file.
  58. //
  59. class TiledRgbaOutputFile
  60. {
  61. public:
  62. //---------------------------------------------------
  63. // Constructor -- rgbaChannels, tileXSize, tileYSize,
  64. // levelMode, and levelRoundingMode overwrite the
  65. // channel list and tile description attribute in the
  66. // header that is passed as an argument to the
  67. // constructor.
  68. //---------------------------------------------------
  69. IMF_EXPORT
  70. TiledRgbaOutputFile (const char name[],
  71. const Header &header,
  72. RgbaChannels rgbaChannels,
  73. int tileXSize,
  74. int tileYSize,
  75. LevelMode mode,
  76. LevelRoundingMode rmode = ROUND_DOWN,
  77. int numThreads = globalThreadCount ());
  78. //---------------------------------------------------
  79. // Constructor -- like the previous one, but the new
  80. // TiledRgbaOutputFile is attached to a file that has
  81. // already been opened by the caller. Destroying
  82. // TiledRgbaOutputFileObjects constructed with this
  83. // constructor does not automatically close the
  84. // corresponding files.
  85. //---------------------------------------------------
  86. IMF_EXPORT
  87. TiledRgbaOutputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  88. const Header &header,
  89. RgbaChannels rgbaChannels,
  90. int tileXSize,
  91. int tileYSize,
  92. LevelMode mode,
  93. LevelRoundingMode rmode = ROUND_DOWN,
  94. int numThreads = globalThreadCount ());
  95. //------------------------------------------------------
  96. // Constructor -- header data are explicitly specified
  97. // as function call arguments (an empty dataWindow means
  98. // "same as displayWindow")
  99. //------------------------------------------------------
  100. IMF_EXPORT
  101. TiledRgbaOutputFile (const char name[],
  102. int tileXSize,
  103. int tileYSize,
  104. LevelMode mode,
  105. LevelRoundingMode rmode,
  106. const IMATH_NAMESPACE::Box2i &displayWindow,
  107. const IMATH_NAMESPACE::Box2i &dataWindow = IMATH_NAMESPACE::Box2i(),
  108. RgbaChannels rgbaChannels = WRITE_RGBA,
  109. float pixelAspectRatio = 1,
  110. const IMATH_NAMESPACE::V2f screenWindowCenter =
  111. IMATH_NAMESPACE::V2f (0, 0),
  112. float screenWindowWidth = 1,
  113. LineOrder lineOrder = INCREASING_Y,
  114. Compression compression = ZIP_COMPRESSION,
  115. int numThreads = globalThreadCount ());
  116. //-----------------------------------------------
  117. // Constructor -- like the previous one, but both
  118. // the display window and the data window are
  119. // Box2i (V2i (0, 0), V2i (width - 1, height -1))
  120. //-----------------------------------------------
  121. IMF_EXPORT
  122. TiledRgbaOutputFile (const char name[],
  123. int width,
  124. int height,
  125. int tileXSize,
  126. int tileYSize,
  127. LevelMode mode,
  128. LevelRoundingMode rmode = ROUND_DOWN,
  129. RgbaChannels rgbaChannels = WRITE_RGBA,
  130. float pixelAspectRatio = 1,
  131. const IMATH_NAMESPACE::V2f screenWindowCenter =
  132. IMATH_NAMESPACE::V2f (0, 0),
  133. float screenWindowWidth = 1,
  134. LineOrder lineOrder = INCREASING_Y,
  135. Compression compression = ZIP_COMPRESSION,
  136. int numThreads = globalThreadCount ());
  137. IMF_EXPORT
  138. virtual ~TiledRgbaOutputFile ();
  139. //------------------------------------------------
  140. // Define a frame buffer as the pixel data source:
  141. // Pixel (x, y) is at address
  142. //
  143. // base + x * xStride + y * yStride
  144. //
  145. //------------------------------------------------
  146. IMF_EXPORT
  147. void setFrameBuffer (const Rgba *base,
  148. size_t xStride,
  149. size_t yStride);
  150. //--------------------------
  151. // Access to the file header
  152. //--------------------------
  153. IMF_EXPORT
  154. const Header & header () const;
  155. IMF_EXPORT
  156. const FrameBuffer & frameBuffer () const;
  157. IMF_EXPORT
  158. const IMATH_NAMESPACE::Box2i & displayWindow () const;
  159. IMF_EXPORT
  160. const IMATH_NAMESPACE::Box2i & dataWindow () const;
  161. IMF_EXPORT
  162. float pixelAspectRatio () const;
  163. IMF_EXPORT
  164. const IMATH_NAMESPACE::V2f screenWindowCenter () const;
  165. IMF_EXPORT
  166. float screenWindowWidth () const;
  167. IMF_EXPORT
  168. LineOrder lineOrder () const;
  169. IMF_EXPORT
  170. Compression compression () const;
  171. IMF_EXPORT
  172. RgbaChannels channels () const;
  173. //----------------------------------------------------
  174. // Utility functions (same as in Imf::TiledOutputFile)
  175. //----------------------------------------------------
  176. IMF_EXPORT
  177. unsigned int tileXSize () const;
  178. IMF_EXPORT
  179. unsigned int tileYSize () const;
  180. IMF_EXPORT
  181. LevelMode levelMode () const;
  182. IMF_EXPORT
  183. LevelRoundingMode levelRoundingMode () const;
  184. IMF_EXPORT
  185. int numLevels () const;
  186. IMF_EXPORT
  187. int numXLevels () const;
  188. IMF_EXPORT
  189. int numYLevels () const;
  190. IMF_EXPORT
  191. bool isValidLevel (int lx, int ly) const;
  192. IMF_EXPORT
  193. int levelWidth (int lx) const;
  194. IMF_EXPORT
  195. int levelHeight (int ly) const;
  196. IMF_EXPORT
  197. int numXTiles (int lx = 0) const;
  198. IMF_EXPORT
  199. int numYTiles (int ly = 0) const;
  200. IMF_EXPORT
  201. IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
  202. IMF_EXPORT
  203. IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
  204. IMF_EXPORT
  205. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  206. int l = 0) const;
  207. IMF_EXPORT
  208. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  209. int lx, int ly) const;
  210. //------------------------------------------------------------------
  211. // Write pixel data:
  212. //
  213. // writeTile(dx, dy, lx, ly) writes the tile with tile
  214. // coordinates (dx, dy), and level number (lx, ly) to
  215. // the file.
  216. //
  217. // dx must lie in the interval [0, numXTiles(lx)-1]
  218. // dy must lie in the interval [0, numYTiles(ly)-1]
  219. //
  220. // lx must lie in the interval [0, numXLevels()-1]
  221. // ly must lie in the inverval [0, numYLevels()-1]
  222. //
  223. // writeTile(dx, dy, level) is a convenience function
  224. // used for ONE_LEVEL and MIPMAP_LEVEL files. It calls
  225. // writeTile(dx, dy, level, level).
  226. //
  227. // The two writeTiles(dx1, dx2, dy1, dy2, ...) functions allow
  228. // writing multiple tiles at once. If multi-threading is used
  229. // multiple tiles are written concurrently.
  230. //
  231. // Pixels that are outside the pixel coordinate range for the tile's
  232. // level, are never accessed by writeTile().
  233. //
  234. // Each tile in the file must be written exactly once.
  235. //
  236. //------------------------------------------------------------------
  237. IMF_EXPORT
  238. void writeTile (int dx, int dy, int l = 0);
  239. IMF_EXPORT
  240. void writeTile (int dx, int dy, int lx, int ly);
  241. IMF_EXPORT
  242. void writeTiles (int dxMin, int dxMax, int dyMin, int dyMax,
  243. int lx, int ly);
  244. IMF_EXPORT
  245. void writeTiles (int dxMin, int dxMax, int dyMin, int dyMax,
  246. int l = 0);
  247. // -------------------------------------------------------------------------
  248. // Update the preview image (see Imf::TiledOutputFile::updatePreviewImage())
  249. // -------------------------------------------------------------------------
  250. IMF_EXPORT
  251. void updatePreviewImage (const PreviewRgba[]);
  252. //------------------------------------------------
  253. // Break a tile -- for testing and debugging only
  254. // (see Imf::TiledOutputFile::breakTile())
  255. //
  256. // Warning: Calling this function usually results
  257. // in a broken image file. The file or parts of
  258. // it may not be readable, or the file may contain
  259. // bad data.
  260. //
  261. //------------------------------------------------
  262. IMF_EXPORT
  263. void breakTile (int dx, int dy,
  264. int lx, int ly,
  265. int offset,
  266. int length,
  267. char c);
  268. private:
  269. //
  270. // Copy constructor and assignment are not implemented
  271. //
  272. TiledRgbaOutputFile (const TiledRgbaOutputFile &);
  273. TiledRgbaOutputFile & operator = (const TiledRgbaOutputFile &);
  274. class ToYa;
  275. TiledOutputFile * _outputFile;
  276. ToYa * _toYa;
  277. };
  278. //
  279. // Tiled RGBA input file
  280. //
  281. class TiledRgbaInputFile
  282. {
  283. public:
  284. //--------------------------------------------------------
  285. // Constructor -- opens the file with the specified name.
  286. // Destroying TiledRgbaInputFile objects constructed with
  287. // this constructor automatically closes the corresponding
  288. // files.
  289. //--------------------------------------------------------
  290. IMF_EXPORT
  291. TiledRgbaInputFile (const char name[],
  292. int numThreads = globalThreadCount ());
  293. //-------------------------------------------------------
  294. // Constructor -- attaches the new TiledRgbaInputFile
  295. // object to a file that has already been opened by the
  296. // caller.
  297. // Destroying TiledRgbaInputFile objects constructed with
  298. // this constructor does not automatically close the
  299. // corresponding files.
  300. //-------------------------------------------------------
  301. IMF_EXPORT
  302. TiledRgbaInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int numThreads = globalThreadCount ());
  303. //------------------------------------------------------------
  304. // Constructors -- the same as the previous two, but the names
  305. // of the red, green, blue, alpha, and luminance channels are
  306. // expected to be layerName.R, layerName.G, etc.
  307. //------------------------------------------------------------
  308. IMF_EXPORT
  309. TiledRgbaInputFile (const char name[],
  310. const std::string &layerName,
  311. int numThreads = globalThreadCount());
  312. IMF_EXPORT
  313. TiledRgbaInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
  314. const std::string &layerName,
  315. int numThreads = globalThreadCount());
  316. //-----------
  317. // Destructor
  318. //-----------
  319. IMF_EXPORT
  320. virtual ~TiledRgbaInputFile ();
  321. //-----------------------------------------------------
  322. // Define a frame buffer as the pixel data destination:
  323. // Pixel (x, y) is at address
  324. //
  325. // base + x * xStride + y * yStride
  326. //
  327. //-----------------------------------------------------
  328. IMF_EXPORT
  329. void setFrameBuffer (Rgba *base,
  330. size_t xStride,
  331. size_t yStride);
  332. //-------------------------------------------------------------------
  333. // Switch to a different layer -- subsequent calls to readTile()
  334. // and readTiles() will read channels layerName.R, layerName.G, etc.
  335. // After each call to setLayerName(), setFrameBuffer() must be called
  336. // at least once before the next call to readTile() or readTiles().
  337. //-------------------------------------------------------------------
  338. IMF_EXPORT
  339. void setLayerName (const std::string &layerName);
  340. //--------------------------
  341. // Access to the file header
  342. //--------------------------
  343. IMF_EXPORT
  344. const Header & header () const;
  345. IMF_EXPORT
  346. const FrameBuffer & frameBuffer () const;
  347. IMF_EXPORT
  348. const IMATH_NAMESPACE::Box2i & displayWindow () const;
  349. IMF_EXPORT
  350. const IMATH_NAMESPACE::Box2i & dataWindow () const;
  351. IMF_EXPORT
  352. float pixelAspectRatio () const;
  353. IMF_EXPORT
  354. const IMATH_NAMESPACE::V2f screenWindowCenter () const;
  355. IMF_EXPORT
  356. float screenWindowWidth () const;
  357. IMF_EXPORT
  358. LineOrder lineOrder () const;
  359. IMF_EXPORT
  360. Compression compression () const;
  361. IMF_EXPORT
  362. RgbaChannels channels () const;
  363. IMF_EXPORT
  364. const char * fileName () const;
  365. IMF_EXPORT
  366. bool isComplete () const;
  367. //----------------------------------
  368. // Access to the file format version
  369. //----------------------------------
  370. IMF_EXPORT
  371. int version () const;
  372. //---------------------------------------------------
  373. // Utility functions (same as in Imf::TiledInputFile)
  374. //---------------------------------------------------
  375. IMF_EXPORT
  376. unsigned int tileXSize () const;
  377. IMF_EXPORT
  378. unsigned int tileYSize () const;
  379. IMF_EXPORT
  380. LevelMode levelMode () const;
  381. IMF_EXPORT
  382. LevelRoundingMode levelRoundingMode () const;
  383. IMF_EXPORT
  384. int numLevels () const;
  385. IMF_EXPORT
  386. int numXLevels () const;
  387. IMF_EXPORT
  388. int numYLevels () const;
  389. IMF_EXPORT
  390. bool isValidLevel (int lx, int ly) const;
  391. IMF_EXPORT
  392. int levelWidth (int lx) const;
  393. IMF_EXPORT
  394. int levelHeight (int ly) const;
  395. IMF_EXPORT
  396. int numXTiles (int lx = 0) const;
  397. IMF_EXPORT
  398. int numYTiles (int ly = 0) const;
  399. IMF_EXPORT
  400. IMATH_NAMESPACE::Box2i dataWindowForLevel (int l = 0) const;
  401. IMF_EXPORT
  402. IMATH_NAMESPACE::Box2i dataWindowForLevel (int lx, int ly) const;
  403. IMF_EXPORT
  404. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  405. int l = 0) const;
  406. IMF_EXPORT
  407. IMATH_NAMESPACE::Box2i dataWindowForTile (int dx, int dy,
  408. int lx, int ly) const;
  409. //----------------------------------------------------------------
  410. // Read pixel data:
  411. //
  412. // readTile(dx, dy, lx, ly) reads the tile with tile
  413. // coordinates (dx, dy), and level number (lx, ly),
  414. // and stores it in the current frame buffer.
  415. //
  416. // dx must lie in the interval [0, numXTiles(lx)-1]
  417. // dy must lie in the interval [0, numYTiles(ly)-1]
  418. //
  419. // lx must lie in the interval [0, numXLevels()-1]
  420. // ly must lie in the inverval [0, numYLevels()-1]
  421. //
  422. // readTile(dx, dy, level) is a convenience function used
  423. // for ONE_LEVEL and MIPMAP_LEVELS files. It calls
  424. // readTile(dx, dy, level, level).
  425. //
  426. // The two readTiles(dx1, dx2, dy1, dy2, ...) functions allow
  427. // reading multiple tiles at once. If multi-threading is used
  428. // multiple tiles are read concurrently.
  429. //
  430. // Pixels that are outside the pixel coordinate range for the
  431. // tile's level, are never accessed by readTile().
  432. //
  433. // Attempting to access a tile that is not present in the file
  434. // throws an InputExc exception.
  435. //
  436. //----------------------------------------------------------------
  437. IMF_EXPORT
  438. void readTile (int dx, int dy, int l = 0);
  439. IMF_EXPORT
  440. void readTile (int dx, int dy, int lx, int ly);
  441. IMF_EXPORT
  442. void readTiles (int dxMin, int dxMax,
  443. int dyMin, int dyMax, int lx, int ly);
  444. IMF_EXPORT
  445. void readTiles (int dxMin, int dxMax,
  446. int dyMin, int dyMax, int l = 0);
  447. private:
  448. //
  449. // Copy constructor and assignment are not implemented
  450. //
  451. TiledRgbaInputFile (const TiledRgbaInputFile &);
  452. TiledRgbaInputFile & operator = (const TiledRgbaInputFile &);
  453. class FromYa;
  454. TiledInputFile * _inputFile;
  455. FromYa * _fromYa;
  456. std::string _channelNamePrefix;
  457. };
  458. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  459. #endif