ImfRgbaFile.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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_RGBA_FILE_H
  35. #define INCLUDED_IMF_RGBA_FILE_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Simplified RGBA image I/O
  39. //
  40. // class RgbaOutputFile
  41. // class RgbaInputFile
  42. //
  43. //-----------------------------------------------------------------------------
  44. #include "ImfHeader.h"
  45. #include "ImfFrameBuffer.h"
  46. #include "ImfRgba.h"
  47. #include "ImathVec.h"
  48. #include "ImathBox.h"
  49. #include "half.h"
  50. #include "ImfThreading.h"
  51. #include <string>
  52. #include "ImfNamespace.h"
  53. #include "ImfForward.h"
  54. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  55. //
  56. // RGBA output file.
  57. //
  58. class RgbaOutputFile
  59. {
  60. public:
  61. //---------------------------------------------------
  62. // Constructor -- header is constructed by the caller
  63. //---------------------------------------------------
  64. IMF_EXPORT
  65. RgbaOutputFile (const char name[],
  66. const Header &header,
  67. RgbaChannels rgbaChannels = WRITE_RGBA,
  68. int numThreads = globalThreadCount());
  69. //----------------------------------------------------
  70. // Constructor -- header is constructed by the caller,
  71. // file is opened by the caller, destructor will not
  72. // automatically close the file.
  73. //----------------------------------------------------
  74. IMF_EXPORT
  75. RgbaOutputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os,
  76. const Header &header,
  77. RgbaChannels rgbaChannels = WRITE_RGBA,
  78. int numThreads = globalThreadCount());
  79. //----------------------------------------------------------------
  80. // Constructor -- header data are explicitly specified as function
  81. // call arguments (empty dataWindow means "same as displayWindow")
  82. //----------------------------------------------------------------
  83. IMF_EXPORT
  84. RgbaOutputFile (const char name[],
  85. const IMATH_NAMESPACE::Box2i &displayWindow,
  86. const IMATH_NAMESPACE::Box2i &dataWindow = IMATH_NAMESPACE::Box2i(),
  87. RgbaChannels rgbaChannels = WRITE_RGBA,
  88. float pixelAspectRatio = 1,
  89. const IMATH_NAMESPACE::V2f screenWindowCenter = IMATH_NAMESPACE::V2f (0, 0),
  90. float screenWindowWidth = 1,
  91. LineOrder lineOrder = INCREASING_Y,
  92. Compression compression = PIZ_COMPRESSION,
  93. int numThreads = globalThreadCount());
  94. //-----------------------------------------------
  95. // Constructor -- like the previous one, but both
  96. // the display window and the data window are
  97. // Box2i (V2i (0, 0), V2i (width - 1, height -1))
  98. //-----------------------------------------------
  99. IMF_EXPORT
  100. RgbaOutputFile (const char name[],
  101. int width,
  102. int height,
  103. RgbaChannels rgbaChannels = WRITE_RGBA,
  104. float pixelAspectRatio = 1,
  105. const IMATH_NAMESPACE::V2f screenWindowCenter = IMATH_NAMESPACE::V2f (0, 0),
  106. float screenWindowWidth = 1,
  107. LineOrder lineOrder = INCREASING_Y,
  108. Compression compression = PIZ_COMPRESSION,
  109. int numThreads = globalThreadCount());
  110. //-----------
  111. // Destructor
  112. //-----------
  113. IMF_EXPORT
  114. virtual ~RgbaOutputFile ();
  115. //------------------------------------------------
  116. // Define a frame buffer as the pixel data source:
  117. // Pixel (x, y) is at address
  118. //
  119. // base + x * xStride + y * yStride
  120. //
  121. //------------------------------------------------
  122. IMF_EXPORT
  123. void setFrameBuffer (const Rgba *base,
  124. size_t xStride,
  125. size_t yStride);
  126. //---------------------------------------------
  127. // Write pixel data (see class Imf::OutputFile)
  128. //---------------------------------------------
  129. IMF_EXPORT
  130. void writePixels (int numScanLines = 1);
  131. IMF_EXPORT
  132. int currentScanLine () const;
  133. //--------------------------
  134. // Access to the file header
  135. //--------------------------
  136. IMF_EXPORT
  137. const Header & header () const;
  138. IMF_EXPORT
  139. const FrameBuffer & frameBuffer () const;
  140. IMF_EXPORT
  141. const IMATH_NAMESPACE::Box2i & displayWindow () const;
  142. IMF_EXPORT
  143. const IMATH_NAMESPACE::Box2i & dataWindow () const;
  144. IMF_EXPORT
  145. float pixelAspectRatio () const;
  146. IMF_EXPORT
  147. const IMATH_NAMESPACE::V2f screenWindowCenter () const;
  148. IMF_EXPORT
  149. float screenWindowWidth () const;
  150. IMF_EXPORT
  151. LineOrder lineOrder () const;
  152. IMF_EXPORT
  153. Compression compression () const;
  154. IMF_EXPORT
  155. RgbaChannels channels () const;
  156. // --------------------------------------------------------------------
  157. // Update the preview image (see Imf::OutputFile::updatePreviewImage())
  158. // --------------------------------------------------------------------
  159. IMF_EXPORT
  160. void updatePreviewImage (const PreviewRgba[]);
  161. //-----------------------------------------------------------------------
  162. // Rounding control for luminance/chroma images:
  163. //
  164. // If the output file contains luminance and chroma channels (WRITE_YC
  165. // or WRITE_YCA), then the the significands of the luminance and
  166. // chroma values are rounded to roundY and roundC bits respectively (see
  167. // function half::round()). Rounding improves compression with minimal
  168. // image degradation, usually much less than the degradation caused by
  169. // chroma subsampling. By default, roundY is 7, and roundC is 5.
  170. //
  171. // If the output file contains RGB channels or a luminance channel,
  172. // without chroma, then no rounding is performed.
  173. //-----------------------------------------------------------------------
  174. IMF_EXPORT
  175. void setYCRounding (unsigned int roundY,
  176. unsigned int roundC);
  177. //----------------------------------------------------
  178. // Break a scan line -- for testing and debugging only
  179. // (see Imf::OutputFile::updatePreviewImage()
  180. //
  181. // Warning: Calling this function usually results in a
  182. // broken image file. The file or parts of it may not
  183. // be readable, or the file may contain bad data.
  184. //
  185. //----------------------------------------------------
  186. IMF_EXPORT
  187. void breakScanLine (int y,
  188. int offset,
  189. int length,
  190. char c);
  191. private:
  192. RgbaOutputFile (const RgbaOutputFile &); // not implemented
  193. RgbaOutputFile & operator = (const RgbaOutputFile &); // not implemented
  194. class ToYca;
  195. OutputFile * _outputFile;
  196. ToYca * _toYca;
  197. };
  198. //
  199. // RGBA input file
  200. //
  201. class RgbaInputFile
  202. {
  203. public:
  204. //-------------------------------------------------------
  205. // Constructor -- opens the file with the specified name,
  206. // destructor will automatically close the file.
  207. //-------------------------------------------------------
  208. IMF_EXPORT
  209. RgbaInputFile (const char name[], int numThreads = globalThreadCount());
  210. //-----------------------------------------------------------
  211. // Constructor -- attaches the new RgbaInputFile object to a
  212. // file that has already been opened by the caller.
  213. // Destroying the RgbaInputFile object will not automatically
  214. // close the file.
  215. //-----------------------------------------------------------
  216. IMF_EXPORT
  217. RgbaInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is, int numThreads = globalThreadCount());
  218. //--------------------------------------------------------------
  219. // Constructors -- the same as the previous two, but the names
  220. // of the red, green, blue, alpha, luminance and chroma channels
  221. // are expected to be layerName.R, layerName.G, etc.
  222. //--------------------------------------------------------------
  223. IMF_EXPORT
  224. RgbaInputFile (const char name[],
  225. const std::string &layerName,
  226. int numThreads = globalThreadCount());
  227. IMF_EXPORT
  228. RgbaInputFile (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
  229. const std::string &layerName,
  230. int numThreads = globalThreadCount());
  231. //-----------
  232. // Destructor
  233. //-----------
  234. IMF_EXPORT
  235. virtual ~RgbaInputFile ();
  236. //-----------------------------------------------------
  237. // Define a frame buffer as the pixel data destination:
  238. // Pixel (x, y) is at address
  239. //
  240. // base + x * xStride + y * yStride
  241. //
  242. //-----------------------------------------------------
  243. IMF_EXPORT
  244. void setFrameBuffer (Rgba *base,
  245. size_t xStride,
  246. size_t yStride);
  247. //----------------------------------------------------------------
  248. // Switch to a different layer -- subsequent calls to readPixels()
  249. // will read channels layerName.R, layerName.G, etc.
  250. // After each call to setLayerName(), setFrameBuffer() must be
  251. // called at least once before the next call to readPixels().
  252. //----------------------------------------------------------------
  253. IMF_EXPORT
  254. void setLayerName (const std::string &layerName);
  255. //-------------------------------------------
  256. // Read pixel data (see class Imf::InputFile)
  257. //-------------------------------------------
  258. IMF_EXPORT
  259. void readPixels (int scanLine1, int scanLine2);
  260. IMF_EXPORT
  261. void readPixels (int scanLine);
  262. //--------------------------
  263. // Access to the file header
  264. //--------------------------
  265. IMF_EXPORT
  266. const Header & header () const;
  267. IMF_EXPORT
  268. const FrameBuffer & frameBuffer () const;
  269. IMF_EXPORT
  270. const IMATH_NAMESPACE::Box2i & displayWindow () const;
  271. IMF_EXPORT
  272. const IMATH_NAMESPACE::Box2i & dataWindow () const;
  273. IMF_EXPORT
  274. float pixelAspectRatio () const;
  275. IMF_EXPORT
  276. const IMATH_NAMESPACE::V2f screenWindowCenter () const;
  277. IMF_EXPORT
  278. float screenWindowWidth () const;
  279. IMF_EXPORT
  280. LineOrder lineOrder () const;
  281. IMF_EXPORT
  282. Compression compression () const;
  283. IMF_EXPORT
  284. RgbaChannels channels () const;
  285. IMF_EXPORT
  286. const char * fileName () const;
  287. IMF_EXPORT
  288. bool isComplete () const;
  289. //----------------------------------
  290. // Access to the file format version
  291. //----------------------------------
  292. IMF_EXPORT
  293. int version () const;
  294. private:
  295. RgbaInputFile (const RgbaInputFile &); // not implemented
  296. RgbaInputFile & operator = (const RgbaInputFile &); // not implemented
  297. class FromYca;
  298. InputFile * _inputFile;
  299. FromYca * _fromYca;
  300. std::string _channelNamePrefix;
  301. };
  302. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  303. #endif