ImfRgbaFile.h 12 KB

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