Blame IlmImfTest/testRle.cpp

Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
//
Packit Service 6754ca
// Copyright (c) 2009-2014 DreamWorks Animation LLC. 
Packit Service 6754ca
//
Packit Service 6754ca
// All rights reserved.
Packit Service 6754ca
//
Packit Service 6754ca
// Redistribution and use in source and binary forms, with or without
Packit Service 6754ca
// modification, are permitted provided that the following conditions are
Packit Service 6754ca
// met:
Packit Service 6754ca
// *       Redistributions of source code must retain the above copyright
Packit Service 6754ca
// notice, this list of conditions and the following disclaimer.
Packit Service 6754ca
// *       Redistributions in binary form must reproduce the above
Packit Service 6754ca
// copyright notice, this list of conditions and the following disclaimer
Packit Service 6754ca
// in the documentation and/or other materials provided with the
Packit Service 6754ca
// distribution.
Packit Service 6754ca
// *       Neither the name of DreamWorks Animation nor the names of
Packit Service 6754ca
// its contributors may be used to endorse or promote products derived
Packit Service 6754ca
// from this software without specific prior written permission.
Packit Service 6754ca
//
Packit Service 6754ca
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 6754ca
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit Service 6754ca
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit Service 6754ca
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit Service 6754ca
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit Service 6754ca
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 6754ca
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 6754ca
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 6754ca
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 6754ca
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 6754ca
//
Packit Service 6754ca
///////////////////////////////////////////////////////////////////////////
Packit Service 6754ca
Packit Service 6754ca
#include <string>
Packit Service 6754ca
#include <iostream>
Packit Service 6754ca
#include <assert.h>
Packit Service 6754ca
#include <ImfRle.h>
Packit Service 6754ca
#include <ImathRandom.h>
Packit Service 6754ca
Packit Service 6754ca
using namespace OPENEXR_IMF_NAMESPACE;
Packit Service 6754ca
using namespace IMATH_NAMESPACE;
Packit Service 6754ca
using namespace std;
Packit Service 6754ca
Packit Service 6754ca
namespace {
Packit Service 6754ca
Packit Service 6754ca
// Generate a random sequence of runs and single values
Packit Service 6754ca
void
Packit Service 6754ca
generateData(char *buffer, int bufferLen)
Packit Service 6754ca
{
Packit Service 6754ca
    char   value = 0;
Packit Service 6754ca
    int    i     = 0;
Packit Service 6754ca
    Rand48 rand48(0);
Packit Service 6754ca
Packit Service 6754ca
    while (i < bufferLen) {
Packit Service 6754ca
        
Packit Service 6754ca
        if (rand48.nextf() < .5) {
Packit Service 6754ca
Packit Service 6754ca
            // Insert a single value 
Packit Service 6754ca
            buffer[i++] = value;
Packit Service 6754ca
Packit Service 6754ca
        } else {
Packit Service 6754ca
Packit Service 6754ca
            // Insert a run
Packit Service 6754ca
            int runLen = (int)rand48.nextf(2.0, 1024.0);
Packit Service 6754ca
Packit Service 6754ca
            for (int j=0; j
Packit Service 6754ca
                buffer[i++] = value;
Packit Service 6754ca
                if (i >= bufferLen) break;
Packit Service 6754ca
            }
Packit Service 6754ca
            
Packit Service 6754ca
        }
Packit Service 6754ca
Packit Service 6754ca
        value = (int)(value+1) & 0xff;
Packit Service 6754ca
    }
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
// Compress, decompress, and compare with the original 
Packit Service 6754ca
void
Packit Service 6754ca
testRoundTrip(int bufferLen)
Packit Service 6754ca
{
Packit Service 6754ca
    char        *src        = new char[bufferLen];
Packit Service 6754ca
    signed char *compressed = new signed char[2*bufferLen];
Packit Service 6754ca
    char        *test       = new char[bufferLen];
Packit Service 6754ca
Packit Service 6754ca
    generateData(src, bufferLen);
Packit Service 6754ca
Packit Service 6754ca
    int compressedLen = rleCompress(bufferLen, src, compressed);
Packit Service 6754ca
    
Packit Service 6754ca
    assert(rleUncompress(compressedLen, bufferLen, compressed, test) > 0);
Packit Service 6754ca
Packit Service 6754ca
    for (int i=0; i
Packit Service 6754ca
        assert( src[i] == test[i] );
Packit Service 6754ca
    }
Packit Service 6754ca
Packit Service 6754ca
    delete[] src;
Packit Service 6754ca
    delete[] compressed;
Packit Service 6754ca
    delete[] test;
Packit Service 6754ca
}
Packit Service 6754ca
Packit Service 6754ca
Packit Service 6754ca
} // namespace
Packit Service 6754ca
Packit Service 6754ca
void 
Packit Service 6754ca
testRle(const string&)   
Packit Service 6754ca
{
Packit Service 6754ca
    cout << "RLE compression:" << endl;
Packit Service 6754ca
Packit Service 6754ca
    try {
Packit Service 6754ca
Packit Service 6754ca
        cout << "   Round tripping buffers " << endl; 
Packit Service 6754ca
Packit Service 6754ca
        const int numIter = 1000;
Packit Service 6754ca
        Rand48    rand48(0);
Packit Service 6754ca
Packit Service 6754ca
        for (int iter=0; iter
Packit Service 6754ca
            testRoundTrip( (int)rand48.nextf(100.0, 1000000.0));
Packit Service 6754ca
        }
Packit Service 6754ca
   
Packit Service 6754ca
    } catch (const exception &e) {
Packit Service 6754ca
        cout << "unexpected exception: " << e.what() << endl;
Packit Service 6754ca
        assert (false);
Packit Service 6754ca
    } catch (...) {
Packit Service 6754ca
        cout << "unexpected exception" << endl;
Packit Service 6754ca
        assert (false);
Packit Service 6754ca
    }
Packit Service 6754ca
Packit Service 6754ca
    cout << "ok\n" << endl;
Packit Service 6754ca
}