Blame IlmImfTest/testRle.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2009-2014 DreamWorks Animation 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 DreamWorks Animation 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>
Packit 0d464f
#include <iostream>
Packit 0d464f
#include <assert.h>
Packit 0d464f
#include <ImfRle.h>
Packit 0d464f
#include <ImathRandom.h>
Packit 0d464f
Packit 0d464f
using namespace OPENEXR_IMF_NAMESPACE;
Packit 0d464f
using namespace IMATH_NAMESPACE;
Packit 0d464f
using namespace std;
Packit 0d464f
Packit 0d464f
namespace {
Packit 0d464f
Packit 0d464f
// Generate a random sequence of runs and single values
Packit 0d464f
void
Packit 0d464f
generateData(char *buffer, int bufferLen)
Packit 0d464f
{
Packit 0d464f
    char   value = 0;
Packit 0d464f
    int    i     = 0;
Packit 0d464f
    Rand48 rand48(0);
Packit 0d464f
Packit 0d464f
    while (i < bufferLen) {
Packit 0d464f
        
Packit 0d464f
        if (rand48.nextf() < .5) {
Packit 0d464f
Packit 0d464f
            // Insert a single value 
Packit 0d464f
            buffer[i++] = value;
Packit 0d464f
Packit 0d464f
        } else {
Packit 0d464f
Packit 0d464f
            // Insert a run
Packit 0d464f
            int runLen = (int)rand48.nextf(2.0, 1024.0);
Packit 0d464f
Packit 0d464f
            for (int j=0; j
Packit 0d464f
                buffer[i++] = value;
Packit 0d464f
                if (i >= bufferLen) break;
Packit 0d464f
            }
Packit 0d464f
            
Packit 0d464f
        }
Packit 0d464f
Packit 0d464f
        value = (int)(value+1) & 0xff;
Packit 0d464f
    }
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
// Compress, decompress, and compare with the original 
Packit 0d464f
void
Packit 0d464f
testRoundTrip(int bufferLen)
Packit 0d464f
{
Packit 0d464f
    char        *src        = new char[bufferLen];
Packit 0d464f
    signed char *compressed = new signed char[2*bufferLen];
Packit 0d464f
    char        *test       = new char[bufferLen];
Packit 0d464f
Packit 0d464f
    generateData(src, bufferLen);
Packit 0d464f
Packit 0d464f
    int compressedLen = rleCompress(bufferLen, src, compressed);
Packit 0d464f
    
Packit 0d464f
    assert(rleUncompress(compressedLen, bufferLen, compressed, test) > 0);
Packit 0d464f
Packit 0d464f
    for (int i=0; i
Packit 0d464f
        assert( src[i] == test[i] );
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    delete[] src;
Packit 0d464f
    delete[] compressed;
Packit 0d464f
    delete[] test;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
} // namespace
Packit 0d464f
Packit 0d464f
void 
Packit 0d464f
testRle(const string&)   
Packit 0d464f
{
Packit 0d464f
    cout << "RLE compression:" << endl;
Packit 0d464f
Packit 0d464f
    try {
Packit 0d464f
Packit 0d464f
        cout << "   Round tripping buffers " << endl; 
Packit 0d464f
Packit 0d464f
        const int numIter = 1000;
Packit 0d464f
        Rand48    rand48(0);
Packit 0d464f
Packit 0d464f
        for (int iter=0; iter
Packit 0d464f
            testRoundTrip( (int)rand48.nextf(100.0, 1000000.0));
Packit 0d464f
        }
Packit 0d464f
   
Packit 0d464f
    } catch (const exception &e) {
Packit 0d464f
        cout << "unexpected exception: " << e.what() << endl;
Packit 0d464f
        assert (false);
Packit 0d464f
    } catch (...) {
Packit 0d464f
        cout << "unexpected exception" << endl;
Packit 0d464f
        assert (false);
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    cout << "ok\n" << endl;
Packit 0d464f
}