opencv/3rdparty/openexr/IlmImf/ImfPizCompressor.cpp

667 lines
15 KiB
C++
Raw Normal View History

2012-08-24 22:31:49 +02:00
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
// Digital Ltd. LLC
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// All rights reserved.
2012-10-17 09:12:04 +02:00
//
2012-08-24 22:31:49 +02:00
// 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
2012-10-17 09:12:04 +02:00
// from this software without specific prior written permission.
//
2012-08-24 22:31:49 +02:00
// 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.
//
///////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
//
// class PizCompressor
//
//-----------------------------------------------------------------------------
#include <ImfPizCompressor.h>
#include <ImfHeader.h>
#include <ImfChannelList.h>
#include <ImfHuf.h>
#include <ImfWav.h>
#include <ImfMisc.h>
#include <ImfCheckedArithmetic.h>
#include <ImathFun.h>
#include <ImathBox.h>
#include <Iex.h>
#include <ImfIO.h>
#include <ImfXdr.h>
#include <ImfAutoArray.h>
#include <string.h>
#include <assert.h>
namespace Imf {
using Imath::divp;
using Imath::modp;
using Imath::Box2i;
using Imath::V2i;
using Iex::InputExc;
namespace {
//
// Functions to compress the range of values in the pixel data
//
const int USHORT_RANGE = (1 << 16);
const int BITMAP_SIZE = (USHORT_RANGE >> 3);
void
bitmapFromData (const unsigned short data[/*nData*/],
2012-10-17 09:12:04 +02:00
int nData,
unsigned char bitmap[BITMAP_SIZE],
unsigned short &minNonZero,
unsigned short &maxNonZero)
2012-08-24 22:31:49 +02:00
{
for (int i = 0; i < BITMAP_SIZE; ++i)
2012-10-17 09:12:04 +02:00
bitmap[i] = 0;
2012-08-24 22:31:49 +02:00
for (int i = 0; i < nData; ++i)
2012-10-17 09:12:04 +02:00
bitmap[data[i] >> 3] |= (1 << (data[i] & 7));
2012-08-24 22:31:49 +02:00
bitmap[0] &= ~1; // zero is not explicitly stored in
2012-10-17 09:12:04 +02:00
// the bitmap; we assume that the
// data always contain zeroes
2012-08-24 22:31:49 +02:00
minNonZero = BITMAP_SIZE - 1;
maxNonZero = 0;
for (int i = 0; i < BITMAP_SIZE; ++i)
{
2012-10-17 09:12:04 +02:00
if (bitmap[i])
{
if (minNonZero > i)
minNonZero = i;
if (maxNonZero < i)
maxNonZero = i;
}
2012-08-24 22:31:49 +02:00
}
}
unsigned short
forwardLutFromBitmap (const unsigned char bitmap[BITMAP_SIZE],
2012-10-17 09:12:04 +02:00
unsigned short lut[USHORT_RANGE])
2012-08-24 22:31:49 +02:00
{
int k = 0;
for (int i = 0; i < USHORT_RANGE; ++i)
{
2012-10-17 09:12:04 +02:00
if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
lut[i] = k++;
else
lut[i] = 0;
2012-08-24 22:31:49 +02:00
}
return k - 1; // maximum value stored in lut[],
} // i.e. number of ones in bitmap minus 1
unsigned short
reverseLutFromBitmap (const unsigned char bitmap[BITMAP_SIZE],
2012-10-17 09:12:04 +02:00
unsigned short lut[USHORT_RANGE])
2012-08-24 22:31:49 +02:00
{
int k = 0;
for (int i = 0; i < USHORT_RANGE; ++i)
{
2012-10-17 09:12:04 +02:00
if ((i == 0) || (bitmap[i >> 3] & (1 << (i & 7))))
lut[k++] = i;
2012-08-24 22:31:49 +02:00
}
int n = k - 1;
while (k < USHORT_RANGE)
2012-10-17 09:12:04 +02:00
lut[k++] = 0;
2012-08-24 22:31:49 +02:00
return n; // maximum k where lut[k] is non-zero,
} // i.e. number of ones in bitmap minus 1
void
applyLut (const unsigned short lut[USHORT_RANGE],
2012-10-17 09:12:04 +02:00
unsigned short data[/*nData*/],
int nData)
2012-08-24 22:31:49 +02:00
{
for (int i = 0; i < nData; ++i)
2012-10-17 09:12:04 +02:00
data[i] = lut[data[i]];
2012-08-24 22:31:49 +02:00
}
} // namespace
struct PizCompressor::ChannelData
{
unsigned short * start;
unsigned short * end;
int nx;
int ny;
int ys;
int size;
};
PizCompressor::PizCompressor
(const Header &hdr,
size_t maxScanLineSize,
size_t numScanLines)
:
Compressor (hdr),
_maxScanLineSize (maxScanLineSize),
_format (XDR),
_numScanLines (numScanLines),
_tmpBuffer (0),
_outBuffer (0),
_numChans (0),
_channels (hdr.channels()),
_channelData (0)
{
size_t tmpBufferSize =
uiMult (maxScanLineSize, numScanLines) / 2;
size_t outBufferSize =
uiAdd (uiMult (maxScanLineSize, numScanLines),
size_t (65536 + 8192));
_tmpBuffer = new unsigned short
[checkArraySize (tmpBufferSize, sizeof (unsigned short))];
_outBuffer = new char [outBufferSize];
const ChannelList &channels = header().channels();
bool onlyHalfChannels = true;
for (ChannelList::ConstIterator c = channels.begin();
2012-10-17 09:12:04 +02:00
c != channels.end();
++c)
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
_numChans++;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
assert (pixelTypeSize (c.channel().type) % pixelTypeSize (HALF) == 0);
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
if (c.channel().type != HALF)
onlyHalfChannels = false;
2012-08-24 22:31:49 +02:00
}
_channelData = new ChannelData[_numChans];
const Box2i &dataWindow = hdr.dataWindow();
_minX = dataWindow.min.x;
_maxX = dataWindow.max.x;
_maxY = dataWindow.max.y;
//
// We can support uncompressed data in the machine's native format
// if all image channels are of type HALF, and if the Xdr and the
// native represenations of a half have the same size.
//
if (onlyHalfChannels && (sizeof (half) == pixelTypeSize (HALF)))
2012-10-17 09:12:04 +02:00
_format = NATIVE;
2012-08-24 22:31:49 +02:00
}
PizCompressor::~PizCompressor ()
{
delete [] _tmpBuffer;
delete [] _outBuffer;
delete [] _channelData;
}
int
PizCompressor::numScanLines () const
{
return _numScanLines;
}
Compressor::Format
PizCompressor::format () const
{
return _format;
}
int
PizCompressor::compress (const char *inPtr,
2012-10-17 09:12:04 +02:00
int inSize,
int minY,
const char *&outPtr)
2012-08-24 22:31:49 +02:00
{
return compress (inPtr,
2012-10-17 09:12:04 +02:00
inSize,
Box2i (V2i (_minX, minY),
V2i (_maxX, minY + numScanLines() - 1)),
outPtr);
2012-08-24 22:31:49 +02:00
}
int
PizCompressor::compressTile (const char *inPtr,
2012-10-17 09:12:04 +02:00
int inSize,
Imath::Box2i range,
const char *&outPtr)
2012-08-24 22:31:49 +02:00
{
return compress (inPtr, inSize, range, outPtr);
}
int
PizCompressor::uncompress (const char *inPtr,
2012-10-17 09:12:04 +02:00
int inSize,
int minY,
const char *&outPtr)
2012-08-24 22:31:49 +02:00
{
return uncompress (inPtr,
2012-10-17 09:12:04 +02:00
inSize,
Box2i (V2i (_minX, minY),
V2i (_maxX, minY + numScanLines() - 1)),
outPtr);
2012-08-24 22:31:49 +02:00
}
int
PizCompressor::uncompressTile (const char *inPtr,
2012-10-17 09:12:04 +02:00
int inSize,
Imath::Box2i range,
const char *&outPtr)
2012-08-24 22:31:49 +02:00
{
return uncompress (inPtr, inSize, range, outPtr);
}
int
PizCompressor::compress (const char *inPtr,
2012-10-17 09:12:04 +02:00
int inSize,
Imath::Box2i range,
const char *&outPtr)
2012-08-24 22:31:49 +02:00
{
//
// This is the compress function which is used by both the tiled and
// scanline compression routines.
//
//
// Special case <20>- empty input buffer
//
if (inSize == 0)
{
2012-10-17 09:12:04 +02:00
outPtr = _outBuffer;
return 0;
2012-08-24 22:31:49 +02:00
}
//
// Rearrange the pixel data so that the wavelet
// and Huffman encoders can process them easily.
//
// The wavelet and Huffman encoders both handle only
// 16-bit data, so 32-bit data must be split into smaller
// pieces. We treat each 32-bit channel (UINT, FLOAT) as
// two interleaved 16-bit channels.
//
int minX = range.min.x;
int maxX = range.max.x;
int minY = range.min.y;
int maxY = range.max.y;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (maxY > _maxY)
maxY = _maxY;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (maxX > _maxX)
maxX = _maxX;
unsigned short *tmpBufferEnd = _tmpBuffer;
int i = 0;
for (ChannelList::ConstIterator c = _channels.begin();
2012-10-17 09:12:04 +02:00
c != _channels.end();
++c, ++i)
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
ChannelData &cd = _channelData[i];
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
cd.start = tmpBufferEnd;
cd.end = cd.start;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
cd.nx = numSamples (c.channel().xSampling, minX, maxX);
cd.ny = numSamples (c.channel().ySampling, minY, maxY);
cd.ys = c.channel().ySampling;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
cd.size = pixelTypeSize (c.channel().type) / pixelTypeSize (HALF);
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
tmpBufferEnd += cd.nx * cd.ny * cd.size;
2012-08-24 22:31:49 +02:00
}
if (_format == XDR)
{
2012-10-17 09:12:04 +02:00
//
// Machine-independent (Xdr) data format
//
for (int y = minY; y <= maxY; ++y)
{
for (int i = 0; i < _numChans; ++i)
{
ChannelData &cd = _channelData[i];
if (modp (y, cd.ys) != 0)
continue;
for (int x = cd.nx * cd.size; x > 0; --x)
{
Xdr::read <CharPtrIO> (inPtr, *cd.end);
++cd.end;
}
}
}
2012-08-24 22:31:49 +02:00
}
else
{
2012-10-17 09:12:04 +02:00
//
// Native, machine-dependent data format
//
for (int y = minY; y <= maxY; ++y)
{
for (int i = 0; i < _numChans; ++i)
{
ChannelData &cd = _channelData[i];
if (modp (y, cd.ys) != 0)
continue;
int n = cd.nx * cd.size;
memcpy (cd.end, inPtr, n * sizeof (unsigned short));
inPtr += n * sizeof (unsigned short);
cd.end += n;
}
}
2012-08-24 22:31:49 +02:00
}
#if defined (DEBUG)
2012-10-17 09:12:04 +02:00
for (int i = 1; i < _numChans; ++i)
assert (_channelData[i-1].end == _channelData[i].start);
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
assert (_channelData[_numChans-1].end == tmpBufferEnd);
2012-08-24 22:31:49 +02:00
#endif
//
// Compress the range of the pixel data
//
AutoArray <unsigned char, BITMAP_SIZE> bitmap;
unsigned short minNonZero;
unsigned short maxNonZero;
bitmapFromData (_tmpBuffer,
2012-10-17 09:12:04 +02:00
tmpBufferEnd - _tmpBuffer,
bitmap,
minNonZero, maxNonZero);
2012-08-24 22:31:49 +02:00
AutoArray <unsigned short, USHORT_RANGE> lut;
unsigned short maxValue = forwardLutFromBitmap (bitmap, lut);
applyLut (lut, _tmpBuffer, tmpBufferEnd - _tmpBuffer);
//
// Store range compression info in _outBuffer
//
char *buf = _outBuffer;
Xdr::write <CharPtrIO> (buf, minNonZero);
Xdr::write <CharPtrIO> (buf, maxNonZero);
if (minNonZero <= maxNonZero)
{
2012-10-17 09:12:04 +02:00
Xdr::write <CharPtrIO> (buf, (char *) &bitmap[0] + minNonZero,
maxNonZero - minNonZero + 1);
2012-08-24 22:31:49 +02:00
}
//
// Apply wavelet encoding
//
for (int i = 0; i < _numChans; ++i)
{
2012-10-17 09:12:04 +02:00
ChannelData &cd = _channelData[i];
for (int j = 0; j < cd.size; ++j)
{
wav2Encode (cd.start + j,
cd.nx, cd.size,
cd.ny, cd.nx * cd.size,
maxValue);
}
2012-08-24 22:31:49 +02:00
}
//
// Apply Huffman encoding; append the result to _outBuffer
//
char *lengthPtr = buf;
Xdr::write <CharPtrIO> (buf, int(0));
int length = hufCompress (_tmpBuffer, tmpBufferEnd - _tmpBuffer, buf);
Xdr::write <CharPtrIO> (lengthPtr, length);
outPtr = _outBuffer;
return buf - _outBuffer + length;
}
int
PizCompressor::uncompress (const char *inPtr,
2012-10-17 09:12:04 +02:00
int inSize,
Imath::Box2i range,
const char *&outPtr)
2012-08-24 22:31:49 +02:00
{
//
// This is the cunompress function which is used by both the tiled and
// scanline decompression routines.
//
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Special case - empty input buffer
//
if (inSize == 0)
{
2012-10-17 09:12:04 +02:00
outPtr = _outBuffer;
return 0;
2012-08-24 22:31:49 +02:00
}
//
// Determine the layout of the compressed pixel data
//
int minX = range.min.x;
int maxX = range.max.x;
int minY = range.min.y;
int maxY = range.max.y;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (maxY > _maxY)
maxY = _maxY;
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
if (maxX > _maxX)
maxX = _maxX;
unsigned short *tmpBufferEnd = _tmpBuffer;
int i = 0;
for (ChannelList::ConstIterator c = _channels.begin();
2012-10-17 09:12:04 +02:00
c != _channels.end();
++c, ++i)
2012-08-24 22:31:49 +02:00
{
2012-10-17 09:12:04 +02:00
ChannelData &cd = _channelData[i];
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
cd.start = tmpBufferEnd;
cd.end = cd.start;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
cd.nx = numSamples (c.channel().xSampling, minX, maxX);
cd.ny = numSamples (c.channel().ySampling, minY, maxY);
cd.ys = c.channel().ySampling;
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
cd.size = pixelTypeSize (c.channel().type) / pixelTypeSize (HALF);
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
tmpBufferEnd += cd.nx * cd.ny * cd.size;
2012-08-24 22:31:49 +02:00
}
//
// Read range compression data
//
unsigned short minNonZero;
unsigned short maxNonZero;
AutoArray <unsigned char, BITMAP_SIZE> bitmap;
memset (bitmap, 0, sizeof (unsigned char) * BITMAP_SIZE);
Xdr::read <CharPtrIO> (inPtr, minNonZero);
Xdr::read <CharPtrIO> (inPtr, maxNonZero);
if (maxNonZero >= BITMAP_SIZE)
{
2012-10-17 09:12:04 +02:00
throw InputExc ("Error in header for PIZ-compressed data "
"(invalid bitmap size).");
2012-08-24 22:31:49 +02:00
}
if (minNonZero <= maxNonZero)
{
2012-10-17 09:12:04 +02:00
Xdr::read <CharPtrIO> (inPtr, (char *) &bitmap[0] + minNonZero,
maxNonZero - minNonZero + 1);
2012-08-24 22:31:49 +02:00
}
AutoArray <unsigned short, USHORT_RANGE> lut;
unsigned short maxValue = reverseLutFromBitmap (bitmap, lut);
//
// Huffman decoding
//
int length;
Xdr::read <CharPtrIO> (inPtr, length);
hufUncompress (inPtr, length, _tmpBuffer, tmpBufferEnd - _tmpBuffer);
//
// Wavelet decoding
//
for (int i = 0; i < _numChans; ++i)
{
2012-10-17 09:12:04 +02:00
ChannelData &cd = _channelData[i];
for (int j = 0; j < cd.size; ++j)
{
wav2Decode (cd.start + j,
cd.nx, cd.size,
cd.ny, cd.nx * cd.size,
maxValue);
}
2012-08-24 22:31:49 +02:00
}
//
// Expand the pixel data to their original range
//
applyLut (lut, _tmpBuffer, tmpBufferEnd - _tmpBuffer);
2012-10-17 09:12:04 +02:00
2012-08-24 22:31:49 +02:00
//
// Rearrange the pixel data into the format expected by the caller.
//
char *outEnd = _outBuffer;
if (_format == XDR)
{
2012-10-17 09:12:04 +02:00
//
// Machine-independent (Xdr) data format
//
for (int y = minY; y <= maxY; ++y)
{
for (int i = 0; i < _numChans; ++i)
{
ChannelData &cd = _channelData[i];
if (modp (y, cd.ys) != 0)
continue;
for (int x = cd.nx * cd.size; x > 0; --x)
{
Xdr::write <CharPtrIO> (outEnd, *cd.end);
++cd.end;
}
}
}
2012-08-24 22:31:49 +02:00
}
else
{
2012-10-17 09:12:04 +02:00
//
// Native, machine-dependent data format
//
for (int y = minY; y <= maxY; ++y)
{
for (int i = 0; i < _numChans; ++i)
{
ChannelData &cd = _channelData[i];
if (modp (y, cd.ys) != 0)
continue;
int n = cd.nx * cd.size;
memcpy (outEnd, cd.end, n * sizeof (unsigned short));
outEnd += n * sizeof (unsigned short);
cd.end += n;
}
}
2012-08-24 22:31:49 +02:00
}
#if defined (DEBUG)
2012-10-17 09:12:04 +02:00
for (int i = 1; i < _numChans; ++i)
assert (_channelData[i-1].end == _channelData[i].start);
2012-08-24 22:31:49 +02:00
2012-10-17 09:12:04 +02:00
assert (_channelData[_numChans-1].end == tmpBufferEnd);
2012-08-24 22:31:49 +02:00
#endif
outPtr = _outBuffer;
return outEnd - _outBuffer;
}
} // namespace Imf