Blame IlmImfTest/testWav.cpp

Packit 0d464f
///////////////////////////////////////////////////////////////////////////
Packit 0d464f
//
Packit 0d464f
// Copyright (c) 2002-2012, 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
Packit 0d464f
Packit 0d464f
#include <ImfWav.h>
Packit 0d464f
#include "ImathRandom.h"
Packit 0d464f
#include <ImfArray.h>
Packit 0d464f
#include <iostream>
Packit 0d464f
#include <exception>
Packit 0d464f
#include <stdlib.h>
Packit 0d464f
#include <assert.h>
Packit 0d464f
Packit 0d464f
Packit 0d464f
using namespace OPENEXR_IMF_NAMESPACE;
Packit 0d464f
using namespace std;
Packit 0d464f
Packit 0d464f
Packit 0d464f
namespace {
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill1_14bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny,
Packit 0d464f
	     IMATH_NAMESPACE::Rand48 & rand48)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = rand48.nexti() & 0x3fff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill1_16bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny,
Packit 0d464f
	     IMATH_NAMESPACE::Rand48 & rand48)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = rand48.nexti() & 0xffff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill2 (Array2D <unsigned short> &a,
Packit 0d464f
       Array2D <unsigned short> &b,
Packit 0d464f
       int nx,
Packit 0d464f
       int ny,
Packit 0d464f
       unsigned short v)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = v;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill3_14bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = (x & 1)? 0: 0x3fff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill3_16bit (Array2D <unsigned short> &a,
Packit 0d464f
          Array2D <unsigned short> &b,
Packit 0d464f
          int nx,
Packit 0d464f
          int ny)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = (x & 1)? 0: 0xffff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill4_14bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = (y & 1)? 0: 0x3fff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill4_16bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = (y & 1)? 0: 0xffff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill5_14bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = ((x + y) & 1)? 0: 0x3fff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
fill5_16bit (Array2D <unsigned short> &a,
Packit 0d464f
             Array2D <unsigned short> &b,
Packit 0d464f
             int nx,
Packit 0d464f
             int ny)
Packit 0d464f
{
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    a[y][x] = b[y][x] = ((x + y) & 1)? 0: 0xffff;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
unsigned short
Packit 0d464f
maxValue (const Array2D <unsigned short> &a, int nx, int ny)
Packit 0d464f
{
Packit 0d464f
    unsigned short mx = 0;
Packit 0d464f
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	    if (mx < a[y][x])
Packit 0d464f
		mx = a[y][x];
Packit 0d464f
Packit 0d464f
    // cout << "max value = " << mx << endl;
Packit 0d464f
Packit 0d464f
    return mx;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
wavEncodeDecode (Array2D <unsigned short> &a,
Packit 0d464f
		 const Array2D <unsigned short> &b,
Packit 0d464f
		 int nx,
Packit 0d464f
		 int ny)
Packit 0d464f
{
Packit 0d464f
    unsigned short mx = maxValue (a, nx, ny);
Packit 0d464f
Packit 0d464f
    //cout << "encoding " << flush;
Packit 0d464f
Packit 0d464f
    wav2Encode (&a[0][0], nx, 1, ny, nx, mx);
Packit 0d464f
Packit 0d464f
    //cout << "decoding " << flush;
Packit 0d464f
Packit 0d464f
    wav2Decode (&a[0][0], nx, 1, ny, nx, mx);
Packit 0d464f
Packit 0d464f
    //cout << "comparing" << endl;
Packit 0d464f
Packit 0d464f
    for (int y = 0; y < ny; ++y)
Packit 0d464f
    {
Packit 0d464f
	for (int x = 0; x < nx; ++x)
Packit 0d464f
	{
Packit 0d464f
	    //cout << x << ' ' << y << ' ' << a[y][x] << ' ' << b[y][x] << endl;
Packit 0d464f
	    assert (a[y][x] == b[y][x]);
Packit 0d464f
	}
Packit 0d464f
    }
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
test (int nx, int ny)
Packit 0d464f
{
Packit 0d464f
    cout << nx << " x " << ny << endl;
Packit 0d464f
Packit 0d464f
    Array2D<unsigned short> a (ny, nx);
Packit 0d464f
    Array2D<unsigned short> b (ny, nx);
Packit 0d464f
Packit 0d464f
    IMATH_NAMESPACE::Rand48 rand48 (0);
Packit 0d464f
Packit 0d464f
    fill1_14bit (a, b, nx, ny, rand48);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill1_16bit (a, b, nx, ny, rand48);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill2 (a, b, nx, ny, 0);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill2 (a, b, nx, ny, 1);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill2 (a, b, nx, ny, 0x3ffe);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill2 (a, b, nx, ny, 0x3fff);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill2 (a, b, nx, ny, 0xfffe);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill2 (a, b, nx, ny, 0xffff);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill3_14bit (a, b, nx, ny);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill3_16bit (a, b, nx, ny);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill4_14bit (a, b, nx, ny);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill4_16bit (a, b, nx, ny);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill5_14bit (a, b, nx, ny);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
Packit 0d464f
    fill5_16bit (a, b, nx, ny);
Packit 0d464f
    wavEncodeDecode (a, b, nx, ny);
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
} // namespace
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
testWav (const std::string&)
Packit 0d464f
{
Packit 0d464f
    try
Packit 0d464f
    {
Packit 0d464f
	cout << "Testing Wavelet encoder" << endl;
Packit 0d464f
	
Packit 0d464f
	test (1, 1);
Packit 0d464f
	test (2, 2);
Packit 0d464f
	test (32, 32);
Packit 0d464f
	test (1024, 16);
Packit 0d464f
	test (16, 1024);
Packit 0d464f
	test (997, 37);
Packit 0d464f
	test (37, 997);
Packit 0d464f
	test (1024, 1024);
Packit 0d464f
	test (997, 997);
Packit 0d464f
Packit 0d464f
	cout << "ok\n" << endl;
Packit 0d464f
    }
Packit 0d464f
    catch (const std::exception &e)
Packit 0d464f
    {
Packit 0d464f
	cerr << "ERROR -- caught exception: " << e.what() << endl;
Packit 0d464f
	assert (false);
Packit 0d464f
    }
Packit 0d464f
}