|
Packit |
0d464f |
///////////////////////////////////////////////////////////////////////////
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
|
|
Packit |
0d464f |
// Digital Ltd. LLC
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// All rights reserved.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
0d464f |
// modification, are permitted provided that the following conditions are
|
|
Packit |
0d464f |
// met:
|
|
Packit |
0d464f |
// * Redistributions of source code must retain the above copyright
|
|
Packit |
0d464f |
// notice, this list of conditions and the following disclaimer.
|
|
Packit |
0d464f |
// * Redistributions in binary form must reproduce the above
|
|
Packit |
0d464f |
// copyright notice, this list of conditions and the following disclaimer
|
|
Packit |
0d464f |
// in the documentation and/or other materials provided with the
|
|
Packit |
0d464f |
// distribution.
|
|
Packit |
0d464f |
// * Neither the name of Industrial Light & Magic nor the names of
|
|
Packit |
0d464f |
// its contributors may be used to endorse or promote products derived
|
|
Packit |
0d464f |
// from this software without specific prior written permission.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
Packit |
0d464f |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
Packit |
0d464f |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
Packit |
0d464f |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
Packit |
0d464f |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
Packit |
0d464f |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
Packit |
0d464f |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
Packit |
0d464f |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
Packit |
0d464f |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
Packit |
0d464f |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
Packit |
0d464f |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
///////////////////////////////////////////////////////////////////////////
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
#include "ImfZip.h"
|
|
Packit |
0d464f |
#include "ImfCheckedArithmetic.h"
|
|
Packit |
0d464f |
#include "ImfNamespace.h"
|
|
Packit |
0d464f |
#include "Iex.h"
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
#include <math.h>
|
|
Packit |
0d464f |
#include <zlib.h>
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
Imf::Zip::Zip(size_t maxRawSize):
|
|
Packit |
0d464f |
_maxRawSize(maxRawSize),
|
|
Packit |
0d464f |
_tmpBuffer(0)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
_tmpBuffer = new char[_maxRawSize];
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
Imf::Zip::Zip(size_t maxScanLineSize, size_t numScanLines):
|
|
Packit |
0d464f |
_maxRawSize(0),
|
|
Packit |
0d464f |
_tmpBuffer(0)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
_maxRawSize = uiMult (maxScanLineSize, numScanLines);
|
|
Packit |
0d464f |
_tmpBuffer = new char[_maxRawSize];
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
Imf::Zip::~Zip()
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
if (_tmpBuffer) delete[] _tmpBuffer;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
size_t
|
|
Packit |
0d464f |
Imf::Zip::maxRawSize()
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return _maxRawSize;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
size_t
|
|
Packit |
0d464f |
Imf::Zip::maxCompressedSize()
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
return uiAdd (uiAdd (_maxRawSize,
|
|
Packit |
0d464f |
size_t (ceil (_maxRawSize * 0.01))),
|
|
Packit |
0d464f |
size_t (100));
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
int
|
|
Packit |
0d464f |
Imf::Zip::compress(const char *raw, int rawSize, char *compressed)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Reorder the pixel data.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
char *t1 = _tmpBuffer;
|
|
Packit |
0d464f |
char *t2 = _tmpBuffer + (rawSize + 1) / 2;
|
|
Packit |
0d464f |
const char *stop = raw + rawSize;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (true)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
if (raw < stop)
|
|
Packit |
0d464f |
*(t1++) = *(raw++);
|
|
Packit |
0d464f |
else
|
|
Packit |
0d464f |
break;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (raw < stop)
|
|
Packit |
0d464f |
*(t2++) = *(raw++);
|
|
Packit |
0d464f |
else
|
|
Packit |
0d464f |
break;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Predictor.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
unsigned char *t = (unsigned char *) _tmpBuffer + 1;
|
|
Packit |
0d464f |
unsigned char *stop = (unsigned char *) _tmpBuffer + rawSize;
|
|
Packit |
0d464f |
int p = t[-1];
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (t < stop)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
int d = int (t[0]) - p + (128 + 256);
|
|
Packit |
0d464f |
p = t[0];
|
|
Packit |
0d464f |
t[0] = d;
|
|
Packit |
0d464f |
++t;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Compress the data using zlib
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
uLongf outSize = int(ceil(rawSize * 1.01)) + 100;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (Z_OK != ::compress ((Bytef *)compressed, &outSize,
|
|
Packit |
0d464f |
(const Bytef *) _tmpBuffer, rawSize))
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
throw Iex::BaseExc ("Data compression (zlib) failed.");
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
return outSize;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
int
|
|
Packit |
0d464f |
Imf::Zip::uncompress(const char *compressed, int compressedSize,
|
|
Packit |
0d464f |
char *raw)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Decompress the data using zlib
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
uLongf outSize = _maxRawSize;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (Z_OK != ::uncompress ((Bytef *)_tmpBuffer, &outSize,
|
|
Packit |
0d464f |
(const Bytef *) compressed, compressedSize))
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
throw Iex::InputExc ("Data decompression (zlib) failed.");
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Predictor.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
unsigned char *t = (unsigned char *) _tmpBuffer + 1;
|
|
Packit |
0d464f |
unsigned char *stop = (unsigned char *) _tmpBuffer + outSize;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (t < stop)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
int d = int (t[-1]) + int (t[0]) - 128;
|
|
Packit |
0d464f |
t[0] = d;
|
|
Packit |
0d464f |
++t;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Reorder the pixel data.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
const char *t1 = _tmpBuffer;
|
|
Packit |
0d464f |
const char *t2 = _tmpBuffer + (outSize + 1) / 2;
|
|
Packit |
0d464f |
char *s = raw;
|
|
Packit |
0d464f |
char *stop = s + outSize;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (true)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
if (s < stop)
|
|
Packit |
0d464f |
*(s++) = *(t1++);
|
|
Packit |
0d464f |
else
|
|
Packit |
0d464f |
break;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (s < stop)
|
|
Packit |
0d464f |
*(s++) = *(t2++);
|
|
Packit |
0d464f |
else
|
|
Packit |
0d464f |
break;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
return outSize;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
|