|
Packit |
0d464f |
///////////////////////////////////////////////////////////////////////////
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Copyright (c) 2002, 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 <string.h>
|
|
Packit |
0d464f |
#include "ImfRle.h"
|
|
Packit |
0d464f |
#include "ImfNamespace.h"
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
namespace {
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
const int MIN_RUN_LENGTH = 3;
|
|
Packit |
0d464f |
const int MAX_RUN_LENGTH = 127;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Compress an array of bytes, using run-length encoding,
|
|
Packit |
0d464f |
// and return the length of the compressed data.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
int
|
|
Packit |
0d464f |
rleCompress (int inLength, const char in[], signed char out[])
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
const char *inEnd = in + inLength;
|
|
Packit |
0d464f |
const char *runStart = in;
|
|
Packit |
0d464f |
const char *runEnd = in + 1;
|
|
Packit |
0d464f |
signed char *outWrite = out;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (runStart < inEnd)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
while (runEnd < inEnd &&
|
|
Packit |
0d464f |
*runStart == *runEnd &&
|
|
Packit |
0d464f |
runEnd - runStart - 1 < MAX_RUN_LENGTH)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
++runEnd;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (runEnd - runStart >= MIN_RUN_LENGTH)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Compressable run
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
*outWrite++ = (runEnd - runStart) - 1;
|
|
Packit |
0d464f |
*outWrite++ = *(signed char *) runStart;
|
|
Packit |
0d464f |
runStart = runEnd;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
else
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Uncompressable run
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (runEnd < inEnd &&
|
|
Packit |
0d464f |
((runEnd + 1 >= inEnd ||
|
|
Packit |
0d464f |
*runEnd != *(runEnd + 1)) ||
|
|
Packit |
0d464f |
(runEnd + 2 >= inEnd ||
|
|
Packit |
0d464f |
*(runEnd + 1) != *(runEnd + 2))) &&
|
|
Packit |
0d464f |
runEnd - runStart < MAX_RUN_LENGTH)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
++runEnd;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
*outWrite++ = runStart - runEnd;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (runStart < runEnd)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
*outWrite++ = *(signed char *) (runStart++);
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
++runEnd;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
return outWrite - out;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
// Uncompress an array of bytes compressed with rleCompress().
|
|
Packit |
0d464f |
// Returns the length of the oncompressed data, or 0 if the
|
|
Packit |
0d464f |
// length of the uncompressed data would be more than maxLength.
|
|
Packit |
0d464f |
//
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
int
|
|
Packit |
0d464f |
rleUncompress (int inLength, int maxLength, const signed char in[], char out[])
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
char *outStart = out;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
while (inLength > 0)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
if (*in < 0)
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
int count = -((int)*in++);
|
|
Packit |
0d464f |
inLength -= count + 1;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (0 > (maxLength -= count))
|
|
Packit |
0d464f |
return 0;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
memcpy(out, in, count);
|
|
Packit |
0d464f |
out += count;
|
|
Packit |
0d464f |
in += count;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
else
|
|
Packit |
0d464f |
{
|
|
Packit |
0d464f |
int count = *in++;
|
|
Packit |
0d464f |
inLength -= 2;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
if (0 > (maxLength -= count + 1))
|
|
Packit |
0d464f |
return 0;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
memset(out, *(char*)in, count+1);
|
|
Packit |
0d464f |
out += count+1;
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
in++;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
return out - outStart;
|
|
Packit |
0d464f |
}
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
|
|
Packit |
0d464f |
OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT
|