/src/FreeImage/Source/OpenEXR/IlmImf/ImfTiledRgbaFile.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 479 lines · 179 code · 97 blank · 203 comment · 0 complexity · a7703bce53b869e0bcd5ab9710326a25 MD5 · raw file

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