ImfMisc.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2002, 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_MISC_H
  35. #define INCLUDED_IMF_MISC_H
  36. //-----------------------------------------------------------------------------
  37. //
  38. // Miscellaneous helper functions for OpenEXR image file I/O
  39. //
  40. //-----------------------------------------------------------------------------
  41. #include "ImfPixelType.h"
  42. #include "ImfCompressor.h"
  43. #include "ImfArray.h"
  44. #include "ImfNamespace.h"
  45. #include "ImfExport.h"
  46. #include "ImfForward.h"
  47. #include <cstddef>
  48. #include <vector>
  49. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
  50. //
  51. // Return the size of a single value of the indicated type,
  52. // in the machine's native format.
  53. //
  54. IMF_EXPORT
  55. int pixelTypeSize (PixelType type);
  56. //
  57. // Return the number of samples a channel with subsampling rate
  58. // s has in the interval [a, b]. For example, a channel with
  59. // subsampling rate 2 (and samples at 0, 2, 4, 6, 8, etc.) has
  60. // 2 samples in the interval [1, 5] and three samples in the
  61. // interval [2, 6].
  62. //
  63. IMF_EXPORT
  64. int numSamples (int s, int a, int b);
  65. //
  66. // Build a table that lists, for each scanline in a file's
  67. // data window, how many bytes are required to store all
  68. // pixels in all channels in that scanline (assuming that
  69. // the pixel data are tightly packed).
  70. //
  71. IMF_EXPORT
  72. size_t bytesPerLineTable (const Header &header,
  73. std::vector<size_t> &bytesPerLine);
  74. //
  75. // Get the sample count for pixel (x, y) using the array base
  76. // pointer, xStride and yStride.
  77. //
  78. inline
  79. int&
  80. sampleCount(char* base, int xStride, int yStride, int x, int y)
  81. {
  82. char* ptr = base + y * yStride + x * xStride;
  83. int* intPtr = (int*) ptr;
  84. return *intPtr;
  85. }
  86. inline
  87. const int&
  88. sampleCount(const char* base, int xStride, int yStride, int x, int y)
  89. {
  90. const char* ptr = base + y * yStride + x * xStride;
  91. int* intPtr = (int*) ptr;
  92. return *intPtr;
  93. }
  94. //
  95. // Build a table that lists, for each scanline in a DEEP file's
  96. // data window, how many bytes are required to store all
  97. // pixels in all channels in scanlines ranged in [minY, maxY]
  98. // (assuming that the pixel data are tightly packed).
  99. //
  100. IMF_EXPORT
  101. size_t bytesPerDeepLineTable (const Header &header,
  102. int minY, int maxY,
  103. const char* base,
  104. int xStride,
  105. int yStride,
  106. std::vector<size_t> &bytesPerLine);
  107. //
  108. // Build a table that lists, for each scanline in a DEEP file's
  109. // data window, how many bytes are required to store all
  110. // pixels in all channels in every scanline (assuming that
  111. // the pixel data are tightly packed).
  112. //
  113. IMF_EXPORT
  114. size_t bytesPerDeepLineTable (const Header &header,
  115. char* base,
  116. int xStride,
  117. int yStride,
  118. std::vector<size_t> &bytesPerLine);
  119. //
  120. // For scanline-based files, pixels are read or written in
  121. // in multi-scanline blocks. Internally, class OutputFile
  122. // and class ScanLineInputFile store a block of scan lines
  123. // in a "line buffer". Function offsetInLineBufferTable()
  124. // builds a table that lists, scanlines within range
  125. // [scanline1, scanline2], the location of the pixel data
  126. // for the scanline relative to the beginning of the line buffer,
  127. // where scanline1 = 0 represents the first line in the DATA WINDOW.
  128. // The one without specifying the range will make scanline1 = 0
  129. // and scanline2 = bytesPerLine.size().
  130. //
  131. IMF_EXPORT
  132. void offsetInLineBufferTable (const std::vector<size_t> &bytesPerLine,
  133. int scanline1, int scanline2,
  134. int linesInLineBuffer,
  135. std::vector<size_t> &offsetInLineBuffer);
  136. IMF_EXPORT
  137. void offsetInLineBufferTable (const std::vector<size_t> &bytesPerLine,
  138. int linesInLineBuffer,
  139. std::vector<size_t> &offsetInLineBuffer);
  140. //
  141. // For a scanline-based file, compute the range of scanlines
  142. // that occupy the same line buffer as a given scanline, y.
  143. // (minY is the minimum y coordinate of the file's data window.)
  144. //
  145. IMF_EXPORT int lineBufferMinY (int y, int minY, int linesInLineBuffer);
  146. IMF_EXPORT int lineBufferMaxY (int y, int minY, int linesInLineBuffer);
  147. //
  148. // Return a compressor's data format (Compressor::NATIVE or Compressor::XDR).
  149. // If compressor is 0, return Compressor::XDR.
  150. //
  151. IMF_EXPORT
  152. Compressor::Format defaultFormat (Compressor *compressor);
  153. //
  154. // Return the number of scan lines a compressor wants to compress
  155. // or uncompress at once. If compressor is 0, return 1.
  156. //
  157. IMF_EXPORT
  158. int numLinesInBuffer (Compressor *compressor);
  159. //
  160. // Copy a single channel of a horizontal row of pixels from an
  161. // input file's internal line buffer or tile buffer into a
  162. // frame buffer slice. If necessary, perform on-the-fly data
  163. // type conversion.
  164. //
  165. // readPtr initially points to the beginning of the
  166. // data in the line or tile buffer. readPtr
  167. // is advanced as the pixel data are copied;
  168. // when copyIntoFrameBuffer() returns,
  169. // readPtr points just past the end of the
  170. // copied data.
  171. //
  172. // writePtr, endPtr point to the lefmost and rightmost pixels
  173. // in the frame buffer slice
  174. //
  175. // xStride the xStride for the frame buffer slice
  176. //
  177. // format indicates if the line or tile buffer is
  178. // in NATIVE or XDR format.
  179. //
  180. // typeInFrameBuffer the pixel data type of the frame buffer slice
  181. //
  182. // typeInFile the pixel data type in the input file's channel
  183. //
  184. IMF_EXPORT
  185. void copyIntoFrameBuffer (const char *&readPtr,
  186. char *writePtr,
  187. char *endPtr,
  188. size_t xStride,
  189. bool fill,
  190. double fillValue,
  191. Compressor::Format format,
  192. PixelType typeInFrameBuffer,
  193. PixelType typeInFile);
  194. //
  195. // Copy a single channel of a horizontal row of pixels from an
  196. // input file's internal line buffer or tile buffer into a
  197. // frame buffer slice. If necessary, perform on-the-fly data
  198. // type conversion.
  199. //
  200. // readPtr initially points to the beginning of the
  201. // data in the line or tile buffer. readPtr
  202. // is advanced as the pixel data are copied;
  203. // when copyIntoFrameBuffer() returns,
  204. // readPtr points just past the end of the
  205. // copied data.
  206. //
  207. // base point to each pixel in the framebuffer
  208. //
  209. // sampleCountBase, provide the number of samples in each pixel
  210. // sampleCountXStride,
  211. // sampleCountYStride
  212. //
  213. // y the scanline to copy. The coordinate is
  214. // relative to the datawindow.min.y.
  215. //
  216. // minX, maxX used to indicate which pixels in the scanline
  217. // will be copied.
  218. //
  219. // xOffsetForSampleCount, used to offset the sample count array
  220. // yOffsetForSampleCount, and the base array.
  221. // xOffsetForData,
  222. // yOffsetForData
  223. //
  224. // xStride the xStride for the frame buffer slice
  225. //
  226. // format indicates if the line or tile buffer is
  227. // in NATIVE or XDR format.
  228. //
  229. // typeInFrameBuffer the pixel data type of the frame buffer slice
  230. //
  231. // typeInFile the pixel data type in the input file's channel
  232. //
  233. IMF_EXPORT
  234. void copyIntoDeepFrameBuffer (const char *& readPtr,
  235. char * base,
  236. const char* sampleCountBase,
  237. ptrdiff_t sampleCountXStride,
  238. ptrdiff_t sampleCountYStride,
  239. int y, int minX, int maxX,
  240. int xOffsetForSampleCount,
  241. int yOffsetForSampleCount,
  242. int xOffsetForData,
  243. int yOffsetForData,
  244. ptrdiff_t xStride,
  245. ptrdiff_t xPointerStride,
  246. ptrdiff_t yPointerStride,
  247. bool fill,
  248. double fillValue,
  249. Compressor::Format format,
  250. PixelType typeInFrameBuffer,
  251. PixelType typeInFile);
  252. //
  253. // Given a pointer into a an input file's line buffer or tile buffer,
  254. // skip over the data for xSize pixels of type typeInFile.
  255. // readPtr initially points to the beginning of the data to be skipped;
  256. // when skipChannel() returns, readPtr points just past the end of the
  257. // skipped data.
  258. //
  259. IMF_EXPORT
  260. void skipChannel (const char *&readPtr,
  261. PixelType typeInFile,
  262. size_t xSize);
  263. //
  264. // Convert an array of pixel data from the machine's native
  265. // representation to XDR format.
  266. //
  267. // toPtr, fromPtr initially point to the beginning of the input
  268. // and output pixel data arrays; when convertInPlace()
  269. // returns, toPtr and fromPtr point just past the
  270. // end of the input and output arrays.
  271. // If the native representation of the data has the
  272. // same size as the XDR data, then the conversion
  273. // can take in place, without an intermediate
  274. // temporary buffer (toPtr and fromPtr can point
  275. // to the same location).
  276. //
  277. // type the pixel data type
  278. //
  279. // numPixels number of pixels in the input and output arrays
  280. //
  281. IMF_EXPORT
  282. void convertInPlace (char *&toPtr,
  283. const char *&fromPtr,
  284. PixelType type,
  285. size_t numPixels);
  286. //
  287. // Copy a single channel of a horizontal row of pixels from a
  288. // a frame buffer into an output file's internal line buffer or
  289. // tile buffer.
  290. //
  291. // writePtr initially points to the beginning of the
  292. // data in the line or tile buffer. writePtr
  293. // is advanced as the pixel data are copied;
  294. // when copyFromFrameBuffer() returns,
  295. // writePtr points just past the end of the
  296. // copied data.
  297. //
  298. // readPtr, endPtr point to the lefmost and rightmost pixels
  299. // in the frame buffer slice
  300. //
  301. // xStride the xStride for the frame buffer slice
  302. //
  303. // format indicates if the line or tile buffer is
  304. // in NATIVE or XDR format.
  305. //
  306. // type the pixel data type in the frame buffer
  307. // and in the output file's channel (function
  308. // copyFromFrameBuffer() doesn't do on-the-fly
  309. // data type conversion)
  310. //
  311. IMF_EXPORT
  312. void copyFromFrameBuffer (char *&writePtr,
  313. const char *&readPtr,
  314. const char *endPtr,
  315. size_t xStride,
  316. Compressor::Format format,
  317. PixelType type);
  318. //
  319. // Copy a single channel of a horizontal row of pixels from a
  320. // a frame buffer in a deep data file into an output file's
  321. // internal line buffer or tile buffer.
  322. //
  323. // writePtr initially points to the beginning of the
  324. // data in the line or tile buffer. writePtr
  325. // is advanced as the pixel data are copied;
  326. // when copyFromDeepFrameBuffer() returns,
  327. // writePtr points just past the end of the
  328. // copied data.
  329. //
  330. // base the start pointer of each pixel in this channel.
  331. // It points to the real data in FrameBuffer.
  332. // It is different for different channels.
  333. // dataWindowMinX and dataWindowMinY are involved in
  334. // locating for base.
  335. //
  336. // sampleCountBase, used to locate the position to get
  337. // sampleCountXStride, the number of samples for each pixel.
  338. // sampleCountYStride Used to determine how far we should
  339. // read based on the pointer provided by base.
  340. //
  341. // y the scanline to copy. If we are dealing
  342. // with a tiled deep file, then probably a portion
  343. // of the scanline is copied.
  344. //
  345. // xMin, xMax used to indicate which pixels in the scanline
  346. // will be copied.
  347. //
  348. // xOffsetForSampleCount, used to offset the sample count array
  349. // yOffsetForSampleCount, and the base array.
  350. // xOffsetForData,
  351. // yOffsetForData
  352. //
  353. // xStride the xStride for the frame buffer slice
  354. //
  355. // format indicates if the line or tile buffer is
  356. // in NATIVE or XDR format.
  357. //
  358. // type the pixel data type in the frame buffer
  359. // and in the output file's channel (function
  360. // copyFromFrameBuffer() doesn't do on-the-fly
  361. // data type conversion)
  362. //
  363. IMF_EXPORT
  364. void copyFromDeepFrameBuffer (char *& writePtr,
  365. const char * base,
  366. char* sampleCountBase,
  367. ptrdiff_t sampleCountXStride,
  368. ptrdiff_t sampleCountYStride,
  369. int y, int xMin, int xMax,
  370. int xOffsetForSampleCount,
  371. int yOffsetForSampleCount,
  372. int xOffsetForData,
  373. int yOffsetForData,
  374. ptrdiff_t sampleStride,
  375. ptrdiff_t xStrideForData,
  376. ptrdiff_t yStrideForData,
  377. Compressor::Format format,
  378. PixelType type);
  379. //
  380. // Fill part of an output file's line buffer or tile buffer with
  381. // zeroes. This routine is called when an output file contains
  382. // a channel for which the frame buffer contains no corresponding
  383. // slice.
  384. //
  385. // writePtr initially points to the beginning of the
  386. // data in the line or tile buffer. When
  387. // fillChannelWithZeroes() returns, writePtr
  388. // points just past the end of the zeroed
  389. // data.
  390. //
  391. // format indicates if the line or tile buffer is
  392. // in NATIVE or XDR format.
  393. //
  394. // type the pixel data type in the line or frame buffer.
  395. //
  396. // xSize number of pixels to be filled with zeroes.
  397. //
  398. IMF_EXPORT
  399. void fillChannelWithZeroes (char *&writePtr,
  400. Compressor::Format format,
  401. PixelType type,
  402. size_t xSize);
  403. IMF_EXPORT
  404. bool usesLongNames (const Header &header);
  405. //
  406. // compute size of chunk offset table - if ignore_attribute set to true
  407. // will compute from the image size and layout, rather than the attribute
  408. // The default behaviour is to read the attribute
  409. //
  410. IMF_EXPORT
  411. int getChunkOffsetTableSize(const Header& header,bool ignore_attribute=false);
  412. OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
  413. #endif