1
///////////////////////////////////////////////////////////////////////////
3
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
6
// All rights reserved.
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are
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
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.
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.
33
///////////////////////////////////////////////////////////////////////////
37
//-----------------------------------------------------------------------------
39
// class RleCompressor
41
//-----------------------------------------------------------------------------
43
#include <ImfRleCompressor.h>
44
#include <ImfCheckedArithmetic.h>
50
const int MIN_RUN_LENGTH = 3;
51
const int MAX_RUN_LENGTH = 127;
55
// Compress an array of bytes, using run-length encoding,
56
// and return the length of the compressed data.
60
rleCompress (int inLength, const char in[], signed char out[])
62
const char *inEnd = in + inLength;
63
const char *runStart = in;
64
const char *runEnd = in + 1;
65
signed char *outWrite = out;
67
while (runStart < inEnd)
69
while (runEnd < inEnd &&
70
*runStart == *runEnd &&
71
runEnd - runStart - 1 < MAX_RUN_LENGTH)
76
if (runEnd - runStart >= MIN_RUN_LENGTH)
82
*outWrite++ = (runEnd - runStart) - 1;
83
*outWrite++ = *(signed char *) runStart;
92
while (runEnd < inEnd &&
93
((runEnd + 1 >= inEnd ||
94
*runEnd != *(runEnd + 1)) ||
95
(runEnd + 2 >= inEnd ||
96
*(runEnd + 1) != *(runEnd + 2))) &&
97
runEnd - runStart < MAX_RUN_LENGTH)
102
*outWrite++ = runStart - runEnd;
104
while (runStart < runEnd)
106
*outWrite++ = *(signed char *) (runStart++);
113
return outWrite - out;
118
// Uncompress an array of bytes compressed with rleCompress().
119
// Returns the length of the oncompressed data, or 0 if the
120
// length of the uncompressed data would be more than maxLength.
124
rleUncompress (int inLength, int maxLength, const signed char in[], char out[])
126
char *outStart = out;
132
int count = -((int)*in++);
133
inLength -= count + 1;
135
if (0 > (maxLength -= count))
139
*out++ = *(char *) (in++);
146
if (0 > (maxLength -= count + 1))
150
*out++ = *(char *) in;
156
return out - outStart;
162
RleCompressor::RleCompressor (const Header &hdr, size_t maxScanLineSize):
164
_maxScanLineSize (maxScanLineSize),
168
_tmpBuffer = new char [maxScanLineSize];
169
_outBuffer = new char [uiMult (maxScanLineSize, size_t (3)) / 2];
173
RleCompressor::~RleCompressor ()
175
delete [] _tmpBuffer;
176
delete [] _outBuffer;
181
RleCompressor::numScanLines () const
184
// This compressor compresses individual scan lines.
192
RleCompressor::compress (const char *inPtr,
198
// Special case �- empty input buffer
208
// Reorder the pixel data.
212
char *t1 = _tmpBuffer;
213
char *t2 = _tmpBuffer + (inSize + 1) / 2;
214
const char *stop = inPtr + inSize;
219
*(t1++) = *(inPtr++);
224
*(t2++) = *(inPtr++);
235
unsigned char *t = (unsigned char *) _tmpBuffer + 1;
236
unsigned char *stop = (unsigned char *) _tmpBuffer + inSize;
241
int d = int (t[0]) - p + (128 + 256);
249
// Run-length encode the data.
253
return rleCompress (inSize, _tmpBuffer, (signed char *) _outBuffer);
258
RleCompressor::uncompress (const char *inPtr,
264
// Special case �- empty input buffer
274
// Decode the run-length encoded data
279
if (0 == (outSize = rleUncompress (inSize, _maxScanLineSize,
280
(const signed char *) inPtr,
283
throw Iex::InputExc ("Data decoding (rle) failed.");
291
unsigned char *t = (unsigned char *) _tmpBuffer + 1;
292
unsigned char *stop = (unsigned char *) _tmpBuffer + outSize;
296
int d = int (t[-1]) + int (t[0]) - 128;
303
// Reorder the pixel data.
307
const char *t1 = _tmpBuffer;
308
const char *t2 = _tmpBuffer + (outSize + 1) / 2;
309
char *s = _outBuffer;
310
char *stop = s + outSize;