ImfPxr24Compressor.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, Pixar Animation Studios
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are
  9. // met:
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Pixar Animation Studios nor the names of
  17. // its contributors may be used to endorse or promote products derived
  18. // from this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. //
  32. /////////////////////////////////////////////////////////////////////////////
  33. //-----------------------------------------------------------------------------
  34. //
  35. // class Pxr24Compressor
  36. //
  37. // This compressor is based on source code that was contributed to
  38. // OpenEXR by Pixar Animation Studios. The compression method was
  39. // developed by Loren Carpenter.
  40. //
  41. // The compressor preprocesses the pixel data to reduce entropy,
  42. // and then calls zlib.
  43. //
  44. // Compression of HALF and UINT channels is lossless, but compressing
  45. // FLOAT channels is lossy: 32-bit floating-point numbers are converted
  46. // to 24 bits by rounding the significand to 15 bits.
  47. //
  48. // When the compressor is invoked, the caller has already arranged
  49. // the pixel data so that the values for each channel appear in a
  50. // contiguous block of memory. The compressor converts the pixel
  51. // values to unsigned integers: For UINT, this is a no-op. HALF
  52. // values are simply re-interpreted as 16-bit integers. FLOAT
  53. // values are converted to 24 bits, and the resulting bit patterns
  54. // are interpreted as integers. The compressor then replaces each
  55. // value with the difference between the value and its left neighbor.
  56. // This turns flat fields in the image into zeroes, and ramps into
  57. // strings of similar values. Next, each difference is split into
  58. // 2, 3 or 4 bytes, and the bytes are transposed so that all the
  59. // most significant bytes end up in a contiguous block, followed
  60. // by the second most significant bytes, and so on. The resulting
  61. // string of bytes is compressed with zlib.
  62. //
  63. //-----------------------------------------------------------------------------
  64. #include "ImfPxr24Compressor.h"
  65. #include "ImfHeader.h"
  66. #include "ImfChannelList.h"
  67. #include "ImfMisc.h"
  68. #include "ImfCheckedArithmetic.h"
  69. #include "ImfNamespace.h"
  70. #include <ImathFun.h>
  71. #include <Iex.h>
  72. #include <half.h>
  73. #include <zlib.h>
  74. #include <assert.h>
  75. #include <algorithm>
  76. using namespace std;
  77. using namespace IMATH_NAMESPACE;
  78. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  79. namespace {
  80. //
  81. // Conversion from 32-bit to 24-bit floating-point numbers.
  82. // Conversion back to 32 bits is simply an 8-bit shift to the left.
  83. //
  84. inline unsigned int
  85. floatToFloat24 (float f)
  86. {
  87. union
  88. {
  89. float f;
  90. unsigned int i;
  91. } u;
  92. u.f = f;
  93. //
  94. // Disassemble the 32-bit floating point number, f,
  95. // into sign, s, exponent, e, and significand, m.
  96. //
  97. unsigned int s = u.i & 0x80000000;
  98. unsigned int e = u.i & 0x7f800000;
  99. unsigned int m = u.i & 0x007fffff;
  100. unsigned int i;
  101. if (e == 0x7f800000)
  102. {
  103. if (m)
  104. {
  105. //
  106. // F is a NAN; we preserve the sign bit and
  107. // the 15 leftmost bits of the significand,
  108. // with one exception: If the 15 leftmost
  109. // bits are all zero, the NAN would turn
  110. // into an infinity, so we have to set at
  111. // least one bit in the significand.
  112. //
  113. m >>= 8;
  114. i = (e >> 8) | m | (m == 0);
  115. }
  116. else
  117. {
  118. //
  119. // F is an infinity.
  120. //
  121. i = e >> 8;
  122. }
  123. }
  124. else
  125. {
  126. //
  127. // F is finite, round the significand to 15 bits.
  128. //
  129. i = ((e | m) + (m & 0x00000080)) >> 8;
  130. if (i >= 0x7f8000)
  131. {
  132. //
  133. // F was close to FLT_MAX, and the significand was
  134. // rounded up, resulting in an exponent overflow.
  135. // Avoid the overflow by truncating the significand
  136. // instead of rounding it.
  137. //
  138. i = (e | m) >> 8;
  139. }
  140. }
  141. return (s >> 8) | i;
  142. }
  143. void
  144. notEnoughData ()
  145. {
  146. throw IEX_NAMESPACE::InputExc ("Error decompressing data "
  147. "(input data are shorter than expected).");
  148. }
  149. void
  150. tooMuchData ()
  151. {
  152. throw IEX_NAMESPACE::InputExc ("Error decompressing data "
  153. "(input data are longer than expected).");
  154. }
  155. } // namespace
  156. Pxr24Compressor::Pxr24Compressor (const Header &hdr,
  157. size_t maxScanLineSize,
  158. size_t numScanLines)
  159. :
  160. Compressor (hdr),
  161. _maxScanLineSize (maxScanLineSize),
  162. _numScanLines (numScanLines),
  163. _tmpBuffer (0),
  164. _outBuffer (0),
  165. _channels (hdr.channels())
  166. {
  167. size_t maxInBytes =
  168. uiMult (maxScanLineSize, numScanLines);
  169. size_t maxOutBytes =
  170. uiAdd (uiAdd (maxInBytes,
  171. size_t (ceil (maxInBytes * 0.01))),
  172. size_t (100));
  173. _tmpBuffer = new unsigned char [maxInBytes];
  174. _outBuffer = new char [maxOutBytes];
  175. const Box2i &dataWindow = hdr.dataWindow();
  176. _minX = dataWindow.min.x;
  177. _maxX = dataWindow.max.x;
  178. _maxY = dataWindow.max.y;
  179. }
  180. Pxr24Compressor::~Pxr24Compressor ()
  181. {
  182. delete [] _tmpBuffer;
  183. delete [] _outBuffer;
  184. }
  185. int
  186. Pxr24Compressor::numScanLines () const
  187. {
  188. return _numScanLines;
  189. }
  190. Compressor::Format
  191. Pxr24Compressor::format () const
  192. {
  193. return NATIVE;
  194. }
  195. int
  196. Pxr24Compressor::compress (const char *inPtr,
  197. int inSize,
  198. int minY,
  199. const char *&outPtr)
  200. {
  201. return compress (inPtr,
  202. inSize,
  203. Box2i (V2i (_minX, minY),
  204. V2i (_maxX, minY + _numScanLines - 1)),
  205. outPtr);
  206. }
  207. int
  208. Pxr24Compressor::compressTile (const char *inPtr,
  209. int inSize,
  210. Box2i range,
  211. const char *&outPtr)
  212. {
  213. return compress (inPtr, inSize, range, outPtr);
  214. }
  215. int
  216. Pxr24Compressor::uncompress (const char *inPtr,
  217. int inSize,
  218. int minY,
  219. const char *&outPtr)
  220. {
  221. return uncompress (inPtr,
  222. inSize,
  223. Box2i (V2i (_minX, minY),
  224. V2i (_maxX, minY + _numScanLines - 1)),
  225. outPtr);
  226. }
  227. int
  228. Pxr24Compressor::uncompressTile (const char *inPtr,
  229. int inSize,
  230. Box2i range,
  231. const char *&outPtr)
  232. {
  233. return uncompress (inPtr, inSize, range, outPtr);
  234. }
  235. int
  236. Pxr24Compressor::compress (const char *inPtr,
  237. int inSize,
  238. Box2i range,
  239. const char *&outPtr)
  240. {
  241. if (inSize == 0)
  242. {
  243. outPtr = _outBuffer;
  244. return 0;
  245. }
  246. int minX = range.min.x;
  247. int maxX = min (range.max.x, _maxX);
  248. int minY = range.min.y;
  249. int maxY = min (range.max.y, _maxY);
  250. unsigned char *tmpBufferEnd = _tmpBuffer;
  251. for (int y = minY; y <= maxY; ++y)
  252. {
  253. for (ChannelList::ConstIterator i = _channels.begin();
  254. i != _channels.end();
  255. ++i)
  256. {
  257. const Channel &c = i.channel();
  258. if (modp (y, c.ySampling) != 0)
  259. continue;
  260. int n = numSamples (c.xSampling, minX, maxX);
  261. unsigned char *ptr[4];
  262. unsigned int previousPixel = 0;
  263. switch (c.type)
  264. {
  265. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  266. ptr[0] = tmpBufferEnd;
  267. ptr[1] = ptr[0] + n;
  268. ptr[2] = ptr[1] + n;
  269. ptr[3] = ptr[2] + n;
  270. tmpBufferEnd = ptr[3] + n;
  271. for (int j = 0; j < n; ++j)
  272. {
  273. unsigned int pixel;
  274. char *pPtr = (char *) &pixel;
  275. for (size_t k = 0; k < sizeof (pixel); ++k)
  276. *pPtr++ = *inPtr++;
  277. unsigned int diff = pixel - previousPixel;
  278. previousPixel = pixel;
  279. *(ptr[0]++) = diff >> 24;
  280. *(ptr[1]++) = diff >> 16;
  281. *(ptr[2]++) = diff >> 8;
  282. *(ptr[3]++) = diff;
  283. }
  284. break;
  285. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  286. ptr[0] = tmpBufferEnd;
  287. ptr[1] = ptr[0] + n;
  288. tmpBufferEnd = ptr[1] + n;
  289. for (int j = 0; j < n; ++j)
  290. {
  291. half pixel;
  292. pixel = *(const half *) inPtr;
  293. inPtr += sizeof (half);
  294. unsigned int diff = pixel.bits() - previousPixel;
  295. previousPixel = pixel.bits();
  296. *(ptr[0]++) = diff >> 8;
  297. *(ptr[1]++) = diff;
  298. }
  299. break;
  300. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  301. ptr[0] = tmpBufferEnd;
  302. ptr[1] = ptr[0] + n;
  303. ptr[2] = ptr[1] + n;
  304. tmpBufferEnd = ptr[2] + n;
  305. for (int j = 0; j < n; ++j)
  306. {
  307. float pixel;
  308. char *pPtr = (char *) &pixel;
  309. for (size_t k = 0; k < sizeof (pixel); ++k)
  310. *pPtr++ = *inPtr++;
  311. unsigned int pixel24 = floatToFloat24 (pixel);
  312. unsigned int diff = pixel24 - previousPixel;
  313. previousPixel = pixel24;
  314. *(ptr[0]++) = diff >> 16;
  315. *(ptr[1]++) = diff >> 8;
  316. *(ptr[2]++) = diff;
  317. }
  318. break;
  319. default:
  320. assert (false);
  321. }
  322. }
  323. }
  324. uLongf outSize = int (ceil ((tmpBufferEnd - _tmpBuffer) * 1.01)) + 100;
  325. if (Z_OK != ::compress ((Bytef *) _outBuffer,
  326. &outSize,
  327. (const Bytef *) _tmpBuffer,
  328. tmpBufferEnd - _tmpBuffer))
  329. {
  330. throw IEX_NAMESPACE::BaseExc ("Data compression (zlib) failed.");
  331. }
  332. outPtr = _outBuffer;
  333. return outSize;
  334. }
  335. int
  336. Pxr24Compressor::uncompress (const char *inPtr,
  337. int inSize,
  338. Box2i range,
  339. const char *&outPtr)
  340. {
  341. if (inSize == 0)
  342. {
  343. outPtr = _outBuffer;
  344. return 0;
  345. }
  346. uLongf tmpSize = _maxScanLineSize * _numScanLines;
  347. if (Z_OK != ::uncompress ((Bytef *)_tmpBuffer,
  348. &tmpSize,
  349. (const Bytef *) inPtr,
  350. inSize))
  351. {
  352. throw IEX_NAMESPACE::InputExc ("Data decompression (zlib) failed.");
  353. }
  354. int minX = range.min.x;
  355. int maxX = min (range.max.x, _maxX);
  356. int minY = range.min.y;
  357. int maxY = min (range.max.y, _maxY);
  358. const unsigned char *tmpBufferEnd = _tmpBuffer;
  359. char *writePtr = _outBuffer;
  360. for (int y = minY; y <= maxY; ++y)
  361. {
  362. for (ChannelList::ConstIterator i = _channels.begin();
  363. i != _channels.end();
  364. ++i)
  365. {
  366. const Channel &c = i.channel();
  367. if (modp (y, c.ySampling) != 0)
  368. continue;
  369. int n = numSamples (c.xSampling, minX, maxX);
  370. const unsigned char *ptr[4];
  371. unsigned int pixel = 0;
  372. switch (c.type)
  373. {
  374. case OPENEXR_IMF_INTERNAL_NAMESPACE::UINT:
  375. ptr[0] = tmpBufferEnd;
  376. ptr[1] = ptr[0] + n;
  377. ptr[2] = ptr[1] + n;
  378. ptr[3] = ptr[2] + n;
  379. tmpBufferEnd = ptr[3] + n;
  380. if ( (uLongf)(tmpBufferEnd - _tmpBuffer) > tmpSize)
  381. notEnoughData();
  382. for (int j = 0; j < n; ++j)
  383. {
  384. unsigned int diff = (*(ptr[0]++) << 24) |
  385. (*(ptr[1]++) << 16) |
  386. (*(ptr[2]++) << 8) |
  387. *(ptr[3]++);
  388. pixel += diff;
  389. char *pPtr = (char *) &pixel;
  390. for (size_t k = 0; k < sizeof (pixel); ++k)
  391. *writePtr++ = *pPtr++;
  392. }
  393. break;
  394. case OPENEXR_IMF_INTERNAL_NAMESPACE::HALF:
  395. ptr[0] = tmpBufferEnd;
  396. ptr[1] = ptr[0] + n;
  397. tmpBufferEnd = ptr[1] + n;
  398. if ( (uLongf)(tmpBufferEnd - _tmpBuffer) > tmpSize)
  399. notEnoughData();
  400. for (int j = 0; j < n; ++j)
  401. {
  402. unsigned int diff = (*(ptr[0]++) << 8) |
  403. *(ptr[1]++);
  404. pixel += diff;
  405. half * hPtr = (half *) writePtr;
  406. hPtr->setBits ((unsigned short) pixel);
  407. writePtr += sizeof (half);
  408. }
  409. break;
  410. case OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT:
  411. ptr[0] = tmpBufferEnd;
  412. ptr[1] = ptr[0] + n;
  413. ptr[2] = ptr[1] + n;
  414. tmpBufferEnd = ptr[2] + n;
  415. if ( (uLongf) (tmpBufferEnd - _tmpBuffer) > tmpSize)
  416. notEnoughData();
  417. for (int j = 0; j < n; ++j)
  418. {
  419. unsigned int diff = (*(ptr[0]++) << 24) |
  420. (*(ptr[1]++) << 16) |
  421. (*(ptr[2]++) << 8);
  422. pixel += diff;
  423. char *pPtr = (char *) &pixel;
  424. for (size_t k = 0; k < sizeof (pixel); ++k)
  425. *writePtr++ = *pPtr++;
  426. }
  427. break;
  428. default:
  429. assert (false);
  430. }
  431. }
  432. }
  433. if ((uLongf) (tmpBufferEnd - _tmpBuffer) < tmpSize)
  434. tooMuchData();
  435. outPtr = _outBuffer;
  436. return writePtr - _outBuffer;
  437. }
  438. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT