ImfZip.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. #include "ImfZip.h"
  35. #include "ImfCheckedArithmetic.h"
  36. #include "ImfNamespace.h"
  37. #include "ImfSimd.h"
  38. #include "Iex.h"
  39. #include <math.h>
  40. #include <zlib.h>
  41. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  42. Zip::Zip(size_t maxRawSize):
  43. _maxRawSize(maxRawSize),
  44. _tmpBuffer(0)
  45. {
  46. _tmpBuffer = new char[_maxRawSize];
  47. }
  48. Zip::Zip(size_t maxScanLineSize, size_t numScanLines):
  49. _maxRawSize(0),
  50. _tmpBuffer(0)
  51. {
  52. _maxRawSize = uiMult (maxScanLineSize, numScanLines);
  53. _tmpBuffer = new char[_maxRawSize];
  54. }
  55. Zip::~Zip()
  56. {
  57. if (_tmpBuffer) delete[] _tmpBuffer;
  58. }
  59. size_t
  60. Zip::maxRawSize()
  61. {
  62. return _maxRawSize;
  63. }
  64. size_t
  65. Zip::maxCompressedSize()
  66. {
  67. return uiAdd (uiAdd (_maxRawSize,
  68. size_t (ceil (_maxRawSize * 0.01))),
  69. size_t (100));
  70. }
  71. int
  72. Zip::compress(const char *raw, int rawSize, char *compressed)
  73. {
  74. //
  75. // Reorder the pixel data.
  76. //
  77. {
  78. char *t1 = _tmpBuffer;
  79. char *t2 = _tmpBuffer + (rawSize + 1) / 2;
  80. const char *stop = raw + rawSize;
  81. while (true)
  82. {
  83. if (raw < stop)
  84. *(t1++) = *(raw++);
  85. else
  86. break;
  87. if (raw < stop)
  88. *(t2++) = *(raw++);
  89. else
  90. break;
  91. }
  92. }
  93. //
  94. // Predictor.
  95. //
  96. {
  97. unsigned char *t = (unsigned char *) _tmpBuffer + 1;
  98. unsigned char *stop = (unsigned char *) _tmpBuffer + rawSize;
  99. int p = t[-1];
  100. while (t < stop)
  101. {
  102. int d = int (t[0]) - p + (128 + 256);
  103. p = t[0];
  104. t[0] = d;
  105. ++t;
  106. }
  107. }
  108. //
  109. // Compress the data using zlib
  110. //
  111. uLongf outSize = int(ceil(rawSize * 1.01)) + 100;
  112. if (Z_OK != ::compress ((Bytef *)compressed, &outSize,
  113. (const Bytef *) _tmpBuffer, rawSize))
  114. {
  115. throw IEX_NAMESPACE::BaseExc ("Data compression (zlib) failed.");
  116. }
  117. return outSize;
  118. }
  119. #ifdef IMF_HAVE_SSE4_1
  120. static void
  121. reconstruct_sse41(char *buf, size_t outSize)
  122. {
  123. static const size_t bytesPerChunk = sizeof(__m128i);
  124. const size_t vOutSize = outSize / bytesPerChunk;
  125. const __m128i c = _mm_set1_epi8(-128);
  126. const __m128i shuffleMask = _mm_set1_epi8(15);
  127. // The first element doesn't have its high bit flipped during compression,
  128. // so it must not be flipped here. To make the SIMD loop nice and
  129. // uniform, we pre-flip the bit so that the loop will unflip it again.
  130. buf[0] += -128;
  131. __m128i *vBuf = reinterpret_cast<__m128i *>(buf);
  132. __m128i vPrev = _mm_setzero_si128();
  133. for (size_t i=0; i<vOutSize; ++i)
  134. {
  135. __m128i d = _mm_add_epi8(_mm_loadu_si128(vBuf), c);
  136. // Compute the prefix sum of elements.
  137. d = _mm_add_epi8(d, _mm_slli_si128(d, 1));
  138. d = _mm_add_epi8(d, _mm_slli_si128(d, 2));
  139. d = _mm_add_epi8(d, _mm_slli_si128(d, 4));
  140. d = _mm_add_epi8(d, _mm_slli_si128(d, 8));
  141. d = _mm_add_epi8(d, vPrev);
  142. _mm_storeu_si128(vBuf++, d);
  143. // Broadcast the high byte in our result to all lanes of the prev
  144. // value for the next iteration.
  145. vPrev = _mm_shuffle_epi8(d, shuffleMask);
  146. }
  147. unsigned char prev = _mm_extract_epi8(vPrev, 15);
  148. for (size_t i=vOutSize*bytesPerChunk; i<outSize; ++i)
  149. {
  150. unsigned char d = prev + buf[i] - 128;
  151. buf[i] = d;
  152. prev = d;
  153. }
  154. }
  155. #else
  156. static void
  157. reconstruct_scalar(char *buf, size_t outSize)
  158. {
  159. unsigned char *t = (unsigned char *) buf + 1;
  160. unsigned char *stop = (unsigned char *) buf + outSize;
  161. while (t < stop)
  162. {
  163. int d = int (t[-1]) + int (t[0]) - 128;
  164. t[0] = d;
  165. ++t;
  166. }
  167. }
  168. #endif
  169. #ifdef IMF_HAVE_SSE2
  170. static void
  171. interleave_sse2(const char *source, size_t outSize, char *out)
  172. {
  173. static const size_t bytesPerChunk = 2*sizeof(__m128i);
  174. const size_t vOutSize = outSize / bytesPerChunk;
  175. const __m128i *v1 = reinterpret_cast<const __m128i *>(source);
  176. const __m128i *v2 = reinterpret_cast<const __m128i *>(source + (outSize + 1) / 2);
  177. __m128i *vOut = reinterpret_cast<__m128i *>(out);
  178. for (size_t i=0; i<vOutSize; ++i) {
  179. __m128i a = _mm_loadu_si128(v1++);
  180. __m128i b = _mm_loadu_si128(v2++);
  181. __m128i lo = _mm_unpacklo_epi8(a, b);
  182. __m128i hi = _mm_unpackhi_epi8(a, b);
  183. _mm_storeu_si128(vOut++, lo);
  184. _mm_storeu_si128(vOut++, hi);
  185. }
  186. const char *t1 = reinterpret_cast<const char *>(v1);
  187. const char *t2 = reinterpret_cast<const char *>(v2);
  188. char *sOut = reinterpret_cast<char *>(vOut);
  189. for (size_t i=vOutSize*bytesPerChunk; i<outSize; ++i)
  190. {
  191. *(sOut++) = (i%2==0) ? *(t1++) : *(t2++);
  192. }
  193. }
  194. #else
  195. static void
  196. interleave_scalar(const char *source, size_t outSize, char *out)
  197. {
  198. const char *t1 = source;
  199. const char *t2 = source + (outSize + 1) / 2;
  200. char *s = out;
  201. char *const stop = s + outSize;
  202. while (true)
  203. {
  204. if (s < stop)
  205. *(s++) = *(t1++);
  206. else
  207. break;
  208. if (s < stop)
  209. *(s++) = *(t2++);
  210. else
  211. break;
  212. }
  213. }
  214. #endif
  215. int
  216. Zip::uncompress(const char *compressed, int compressedSize,
  217. char *raw)
  218. {
  219. //
  220. // Decompress the data using zlib
  221. //
  222. uLongf outSize = _maxRawSize;
  223. if (Z_OK != ::uncompress ((Bytef *)_tmpBuffer, &outSize,
  224. (const Bytef *) compressed, compressedSize))
  225. {
  226. throw IEX_NAMESPACE::InputExc ("Data decompression (zlib) failed.");
  227. }
  228. if (outSize == 0)
  229. {
  230. return outSize;
  231. }
  232. //
  233. // Predictor.
  234. //
  235. #ifdef IMF_HAVE_SSE4_1
  236. reconstruct_sse41(_tmpBuffer, outSize);
  237. #else
  238. reconstruct_scalar(_tmpBuffer, outSize);
  239. #endif
  240. //
  241. // Reorder the pixel data.
  242. //
  243. #ifdef IMF_HAVE_SSE2
  244. interleave_sse2(_tmpBuffer, outSize, raw);
  245. #else
  246. interleave_scalar(_tmpBuffer, outSize, raw);
  247. #endif
  248. return outSize;
  249. }
  250. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT