ImfTiledRgbaFile.h 16 KB

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