Blame ImathTest/testColor.cpp

Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
//
Packit 8dc392
// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
Packit 8dc392
// Digital Ltd. LLC
Packit 8dc392
// 
Packit 8dc392
// All rights reserved.
Packit 8dc392
// 
Packit 8dc392
// Redistribution and use in source and binary forms, with or without
Packit 8dc392
// modification, are permitted provided that the following conditions are
Packit 8dc392
// met:
Packit 8dc392
// *       Redistributions of source code must retain the above copyright
Packit 8dc392
// notice, this list of conditions and the following disclaimer.
Packit 8dc392
// *       Redistributions in binary form must reproduce the above
Packit 8dc392
// copyright notice, this list of conditions and the following disclaimer
Packit 8dc392
// in the documentation and/or other materials provided with the
Packit 8dc392
// distribution.
Packit 8dc392
// *       Neither the name of Industrial Light & Magic nor the names of
Packit 8dc392
// its contributors may be used to endorse or promote products derived
Packit 8dc392
// from this software without specific prior written permission. 
Packit 8dc392
// 
Packit 8dc392
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 8dc392
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 8dc392
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 8dc392
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 8dc392
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 8dc392
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 8dc392
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 8dc392
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 8dc392
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 8dc392
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 8dc392
//
Packit 8dc392
///////////////////////////////////////////////////////////////////////////
Packit 8dc392
Packit 8dc392
Packit 8dc392
Packit 8dc392
#include <testColor.h>
Packit 8dc392
#include "ImathColor.h"
Packit 8dc392
#include "ImathColorAlgo.h"
Packit 8dc392
#include "ImathLimits.h"
Packit 8dc392
#include "ImathMath.h"
Packit 8dc392
#include <iostream>
Packit 8dc392
#include <assert.h>
Packit 8dc392
Packit 8dc392
Packit 8dc392
using namespace std;
Packit 8dc392
Packit 8dc392
Packit 8dc392
void
Packit 8dc392
testColor ()
Packit 8dc392
{
Packit 8dc392
    cout << "Testing functions in ImathColor.h & ImathColorAlgo.h" << endl;
Packit 8dc392
Packit 8dc392
    cout << "rgb2packed -> packed2rgb" << endl;
Packit 8dc392
Packit 8dc392
    const float         epsilon = IMATH_INTERNAL_NAMESPACE::limits< float >::epsilon();
Packit 8dc392
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::PackedColor	packed;
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C3c	    	in3( 52, 128, 254 );
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C3c	    	out3;
Packit 8dc392
    
Packit 8dc392
    packed = IMATH_INTERNAL_NAMESPACE::rgb2packed( in3 );
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::packed2rgb( packed, out3 );
Packit 8dc392
Packit 8dc392
    assert ( in3 == out3 );
Packit 8dc392
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C4c	    	testConstructor1;
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C4c	    	testConstructor2( testConstructor1 );
Packit 8dc392
Packit 8dc392
    testConstructor1 = testConstructor2; // use these so the compiler doesn't emit a warning
Packit 8dc392
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C4c	    	testConstructor3( 52, 128, 254, 127 );
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C4c	    	A( testConstructor3 );
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C4c	    	B;
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::C4f	    	X, Y, tmp;
Packit 8dc392
Packit 8dc392
    packed = IMATH_INTERNAL_NAMESPACE::rgb2packed( A );
Packit 8dc392
    IMATH_INTERNAL_NAMESPACE::packed2rgb( packed, B );
Packit 8dc392
Packit 8dc392
    assert ( A == B );
Packit 8dc392
Packit 8dc392
    cout << "Imath::Color4 * f" << endl;
Packit 8dc392
Packit 8dc392
    assert ( ( IMATH_INTERNAL_NAMESPACE::C4f( 0.330f, 0.710f, 0.010f, 0.999f ) * 0.999f ) ==
Packit 8dc392
    	     IMATH_INTERNAL_NAMESPACE::C4f( 0.330f * 0.999f,
Packit 8dc392
	     	    	 0.710f * 0.999f,
Packit 8dc392
			 0.010f * 0.999f,
Packit 8dc392
			 0.999f * 0.999f ) );
Packit 8dc392
Packit 8dc392
    cout << "Imath::Color4 / f" << endl;
Packit 8dc392
Packit 8dc392
    assert ( ( IMATH_INTERNAL_NAMESPACE::C4f( 0.330f, 0.710f, 0.010f, 0.999f ) / 0.999f ) ==
Packit 8dc392
    	     IMATH_INTERNAL_NAMESPACE::C4f( 0.330f / 0.999f,
Packit 8dc392
	     	    	 0.710f / 0.999f,
Packit 8dc392
			 0.010f / 0.999f,
Packit 8dc392
			 0.999f / 0.999f ) );
Packit 8dc392
Packit 8dc392
    cout << "Assignment and comparison" << endl;
Packit 8dc392
Packit 8dc392
    B = A;
Packit 8dc392
    assert( B == A );
Packit 8dc392
    assert( !( B != A ) );
Packit 8dc392
Packit 8dc392
    X = Y = IMATH_INTERNAL_NAMESPACE::C4f( 0.123f, -0.420f, 0.501f, 0.998f );
Packit 8dc392
    
Packit 8dc392
    X *= 0.001f;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.r * 0.001f ) - X.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.g * 0.001f ) - X.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.b * 0.001f ) - X.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.a * 0.001f ) - X.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    X = Y = IMATH_INTERNAL_NAMESPACE::C4f( 0.123f, -0.420f, 0.501f, 0.998f );
Packit 8dc392
    
Packit 8dc392
    X /= -1.001f;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.r / -1.001f ) - X.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.g / -1.001f ) - X.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.b / -1.001f ) - X.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( Y.a / -1.001f ) - X.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    Y = IMATH_INTERNAL_NAMESPACE::C4f( 0.998f, -0.001f,  0.501f, 1.001f );
Packit 8dc392
    X = IMATH_INTERNAL_NAMESPACE::C4f( 0.011f, -0.420f, -0.501f, 0.998f );
Packit 8dc392
Packit 8dc392
    tmp = X + Y;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r + Y.r ) - tmp.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g + Y.g ) - tmp.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b + Y.b ) - tmp.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a + Y.a ) - tmp.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    tmp = X - Y;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r - Y.r ) - tmp.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g - Y.g ) - tmp.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b - Y.b ) - tmp.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a - Y.a ) - tmp.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    tmp = X * Y;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r * Y.r ) - tmp.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g * Y.g ) - tmp.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b * Y.b ) - tmp.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a * Y.a ) - tmp.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    tmp = X / Y;
Packit 8dc392
    
Packit 8dc392
    //
Packit 8dc392
    // epsilon doesn't work here.
Packit 8dc392
    //
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r / Y.r ) - tmp.r ) <= 1e-5f &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g / Y.g ) - tmp.g ) <= 1e-5f &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b / Y.b ) - tmp.b ) <= 1e-5f &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a / Y.a ) - tmp.a ) <= 1e-5f );
Packit 8dc392
Packit 8dc392
    tmp = X;
Packit 8dc392
    tmp += Y;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r + Y.r ) - tmp.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g + Y.g ) - tmp.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b + Y.b ) - tmp.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a + Y.a ) - tmp.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    tmp = X;
Packit 8dc392
    tmp -= Y;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r - Y.r ) - tmp.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g - Y.g ) - tmp.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b - Y.b ) - tmp.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a - Y.a ) - tmp.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    tmp = X;
Packit 8dc392
    tmp *= Y;
Packit 8dc392
    
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r * Y.r ) - tmp.r ) <= epsilon &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g * Y.g ) - tmp.g ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b * Y.b ) - tmp.b ) <= epsilon &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a * Y.a ) - tmp.a ) <= epsilon );
Packit 8dc392
Packit 8dc392
    tmp = X;
Packit 8dc392
    tmp /= Y;
Packit 8dc392
    
Packit 8dc392
    //
Packit 8dc392
    // epsilon doesn't work here.
Packit 8dc392
    //
Packit 8dc392
    assert( IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.r / Y.r ) - tmp.r ) <= 1e-5f &&
Packit 8dc392
    	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.g / Y.g ) - tmp.g ) <= 1e-5f &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.b / Y.b ) - tmp.b ) <= 1e-5f &&
Packit 8dc392
	    IMATH_INTERNAL_NAMESPACE::Math<float>::fabs( ( X.a / Y.a ) - tmp.a ) <= 1e-5f );
Packit 8dc392
Packit 8dc392
    cout << "ok\n" << endl;
Packit 8dc392
}