Blame IlmImfTest/compareDwa.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 <math.h>
Packit 0d464f
#include <assert.h>
Packit 0d464f
Packit 0d464f
#include "half.h"
Packit 0d464f
#include "compareDwa.h"
Packit 0d464f
Packit 0d464f
using namespace OPENEXR_IMF_NAMESPACE;
Packit 0d464f
using namespace std;
Packit 0d464f
Packit 0d464f
//
Packit 0d464f
// Convert from linear to a nonlinear representation,
Packit 0d464f
//
Packit 0d464f
Packit 0d464f
half
Packit 0d464f
toNonlinear(half linear)
Packit 0d464f
{
Packit 0d464f
    float sign    = 1;
Packit 0d464f
    float logBase = pow(2.7182818, 2.2);
Packit 0d464f
Packit 0d464f
    if ((float)linear < 0) {
Packit 0d464f
        sign = -1;
Packit 0d464f
    } 
Packit 0d464f
Packit 0d464f
    if ((linear.bits() & 0x7c00) == 0x7c00) {
Packit 0d464f
        return (half)0.0;                   
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    if ( fabs( (float)linear ) <= 1.0f) {
Packit 0d464f
        return (half)(sign * pow(fabs((float)linear), 1.f/2.2f));
Packit 0d464f
    } else {
Packit 0d464f
        return (half)(sign * ( log(fabs((float)linear)) / log(logBase) + 1.0f) );
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
    return (half)0.0f;
Packit 0d464f
}
Packit 0d464f
Packit 0d464f
Packit 0d464f
void
Packit 0d464f
compareDwa(int width, 
Packit 0d464f
           int height,
Packit 0d464f
           const Array2D<Rgba> &src,
Packit 0d464f
           const Array2D<Rgba> &test,
Packit 0d464f
           RgbaChannels channels)
Packit 0d464f
{
Packit 0d464f
    half  srcNonlin, testNonlin;
Packit 0d464f
    float relError;
Packit 0d464f
Packit 0d464f
    for (int y=0; y
Packit 0d464f
        for (int x=0; x
Packit 0d464f
Packit 0d464f
            for (int comp=0; comp<3; ++comp) {
Packit 0d464f
                switch (comp) {
Packit 0d464f
                    case 0:
Packit 0d464f
                        if (!(channels & WRITE_R)) continue;
Packit 0d464f
Packit 0d464f
                        srcNonlin  = toNonlinear(src[y][x].r);
Packit 0d464f
                        testNonlin = toNonlinear(test[y][x].r);
Packit 0d464f
                        break;
Packit 0d464f
                    case 1:
Packit 0d464f
                        if (!(channels & WRITE_G)) continue;
Packit 0d464f
Packit 0d464f
                        srcNonlin  = toNonlinear(src[y][x].g);
Packit 0d464f
                        testNonlin = toNonlinear(test[y][x].g);
Packit 0d464f
                        break;
Packit 0d464f
                    case 2:
Packit 0d464f
                        if (!(channels & WRITE_B)) continue;
Packit 0d464f
Packit 0d464f
                        srcNonlin  = toNonlinear(src[y][x].b);
Packit 0d464f
                        testNonlin = toNonlinear(test[y][x].b);
Packit 0d464f
                        break;
Packit 0d464f
                }
Packit 0d464f
Packit 0d464f
                //
Packit 0d464f
                // Try to compare with relative error. This breaks down
Packit 0d464f
                // for small numbers, which could be quantiezed to 0 
Packit 0d464f
                // giving 100% error. 
Packit 0d464f
                //
Packit 0d464f
                if (srcNonlin.bits() != 0x00) {
Packit 0d464f
Packit 0d464f
                    relError = fabs( (float)srcNonlin - (float)testNonlin ) /
Packit 0d464f
                                    fabs((float)srcNonlin);
Packit 0d464f
Packit 0d464f
Packit 0d464f
                    if (fabs(srcNonlin) < .1) continue;
Packit 0d464f
 
Packit 0d464f
                    if (fabs(srcNonlin) < .25) {
Packit 0d464f
                        assert( relError < .25);
Packit 0d464f
                    } else {
Packit 0d464f
                        assert( relError < .1);
Packit 0d464f
                    }
Packit 0d464f
Packit 0d464f
                } else {
Packit 0d464f
                    assert( srcNonlin != testNonlin );
Packit 0d464f
                }
Packit 0d464f
            }
Packit 0d464f
Packit 0d464f
        }
Packit 0d464f
    }
Packit 0d464f
Packit 0d464f
Packit 0d464f
    //
Packit 0d464f
    // Test alpha, if necessary
Packit 0d464f
    //
Packit 0d464f
    if (channels & WRITE_A) {
Packit 0d464f
        for (int y=0; y
Packit 0d464f
            for (int x=0; x
Packit 0d464f
                assert( src[y][x].a == test[y][x].a );
Packit 0d464f
            }
Packit 0d464f
        }
Packit 0d464f
    }
Packit 0d464f
}