/////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas // Digital Ltd. LLC // // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Industrial Light & Magic nor the names of // its contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // /////////////////////////////////////////////////////////////////////////// #include "ImfZip.h" #include "ImfCheckedArithmetic.h" #include "ImfNamespace.h" #include "ImfSimd.h" #include "Iex.h" #include #include OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER Zip::Zip(size_t maxRawSize): _maxRawSize(maxRawSize), _tmpBuffer(0) { _tmpBuffer = new char[_maxRawSize]; } Zip::Zip(size_t maxScanLineSize, size_t numScanLines): _maxRawSize(0), _tmpBuffer(0) { _maxRawSize = uiMult (maxScanLineSize, numScanLines); _tmpBuffer = new char[_maxRawSize]; } Zip::~Zip() { if (_tmpBuffer) delete[] _tmpBuffer; } size_t Zip::maxRawSize() { return _maxRawSize; } size_t Zip::maxCompressedSize() { return uiAdd (uiAdd (_maxRawSize, size_t (ceil (_maxRawSize * 0.01))), size_t (100)); } int Zip::compress(const char *raw, int rawSize, char *compressed) { // // Reorder the pixel data. // { char *t1 = _tmpBuffer; char *t2 = _tmpBuffer + (rawSize + 1) / 2; const char *stop = raw + rawSize; while (true) { if (raw < stop) *(t1++) = *(raw++); else break; if (raw < stop) *(t2++) = *(raw++); else break; } } // // Predictor. // { unsigned char *t = (unsigned char *) _tmpBuffer + 1; unsigned char *stop = (unsigned char *) _tmpBuffer + rawSize; int p = t[-1]; while (t < stop) { int d = int (t[0]) - p + (128 + 256); p = t[0]; t[0] = d; ++t; } } // // Compress the data using zlib // uLongf outSize = int(ceil(rawSize * 1.01)) + 100; if (Z_OK != ::compress ((Bytef *)compressed, &outSize, (const Bytef *) _tmpBuffer, rawSize)) { throw IEX_NAMESPACE::BaseExc ("Data compression (zlib) failed."); } return outSize; } #ifdef IMF_HAVE_SSE4_1 static void reconstruct_sse41(char *buf, size_t outSize) { static const size_t bytesPerChunk = sizeof(__m128i); const size_t vOutSize = outSize / bytesPerChunk; const __m128i c = _mm_set1_epi8(-128); const __m128i shuffleMask = _mm_set1_epi8(15); // The first element doesn't have its high bit flipped during compression, // so it must not be flipped here. To make the SIMD loop nice and // uniform, we pre-flip the bit so that the loop will unflip it again. buf[0] += -128; __m128i *vBuf = reinterpret_cast<__m128i *>(buf); __m128i vPrev = _mm_setzero_si128(); for (size_t i=0; i(source); const __m128i *v2 = reinterpret_cast(source + (outSize + 1) / 2); __m128i *vOut = reinterpret_cast<__m128i *>(out); for (size_t i=0; i(v1); const char *t2 = reinterpret_cast(v2); char *sOut = reinterpret_cast(vOut); for (size_t i=vOutSize*bytesPerChunk; i