iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-Pixel.C
/*! @file
@author Unknown
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <nrt/config.h>
#include <boost/mpl/list.hpp>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE PixelTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE(PixelPromotionTest)
{
//nrt::PixRGBD<nrt::byte, uint16_t> rgbd2 = rgbd + rgbd;
// Type and promotion checking:
nrt::PixRGB<nrt::byte> pix(1, 2, 3);
// Promote the pixel using nrt::promote. We store the result into an auto so that we can then test for what the type
// of that result is (as opposed to storing it, with possible automatic conversion, into a fixed type):
// #################### Pixel promoted by a POD type:
// byte * float = float
auto pixf = nrt::promote<nrt::PixRGB<nrt::byte>, float>::type(pix);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixf).name()), "nrt::PixRGB<float>");
// byte * double = double
auto pixd = nrt::promote<nrt::PixRGB<nrt::byte>, double>::type(pix);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixd).name()), "nrt::PixRGB<double>");
// byte * void = byte (compiler calls that unsigned char)
auto pixv = nrt::promote<nrt::PixRGB<nrt::byte>, void>::type(pix);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixv).name()), "nrt::PixRGB<unsigned char>");
// byte * byte = int
auto pixb = nrt::promote<nrt::PixRGB<nrt::byte>, nrt::byte>::type(pix);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixb).name()), "nrt::PixRGB<int>");
// float * float = float
auto pixff = nrt::promote<nrt::PixRGB<float>, float>::type(pixf);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixff).name()), "nrt::PixRGB<float>");
// float * double = double
auto pixfd = nrt::promote<nrt::PixRGB<float>, double>::type(pixf);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixfd).name()), "nrt::PixRGB<double>");
// float * byte = float
auto pixfb = nrt::promote<nrt::PixRGB<float>, nrt::byte>::type(pixf);
BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixfb).name()), "nrt::PixRGB<float>");
// #################### Pixel promoted by a another pixel:
// byte * float = float
BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixgf).name()), "nrt::PixRGB<float>");
// byte * float = float
BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixrf).name()), "nrt::PixRGB<float>");
// byte * byte = int
BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixgb).name()), "nrt::PixRGB<int>");
// byte * byte = int
BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixrb).name()), "nrt::PixRGB<int>");
}
typedef boost::mpl::list<unsigned long int> unsigned_types;
typedef boost::mpl::list<char, short int, int, long> signed_types;
typedef boost::mpl::list<float, double> floating_types;
BOOST_AUTO_TEST_CASE_TEMPLATE(SignedPixelOpsTest, T, signed_types)
{
// Value, clamping and operator checking:
nrt::PixRGB<T> x(T(10), T(20), T(30));
nrt::PixRGB<T> y(T(1), T(2), T(3));
nrt::PixGray<T> z(T(2));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+y), nrt::PixRGB<T>(T(11), T(22), T(33)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-y), nrt::PixRGB<T>(T(9), T(18), T(27)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*y), nrt::PixRGB<T>(T(10), T(40), T(90)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/y), nrt::PixRGB<T>(T(10), T(10), T(10)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+2), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-2), nrt::PixRGB<T>(T(8), T(18), T(28)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2-x), nrt::PixRGB<T>(T(-8), T(-18), T(-28)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*2), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/2), nrt::PixRGB<T>(T(5), T(10), T(15)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+z), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-z), nrt::PixRGB<T>(T(8), T(18), T(28)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z-x), nrt::PixRGB<T>(T(-8), T(-18), T(-28)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*z), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/z), nrt::PixRGB<T>(T(5), T(10), T(15)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
a = x; a+=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(11), T(22), T(33)));
a = x; a-=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(9), T(18), T(27)));
a = x; a*=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(40), T(90)));
a = x; a/=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(10), T(10)));
a = x; a+=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
a = x; a-=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
a = x; a*=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
a = x; a/=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
a = x; a+=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
a = x; a-=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
a = x; a*=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
a = x; a/=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(UnsignedPixelOpsTest, T, unsigned_types)
{
// Value, clamping and operator checking:
nrt::PixRGB<T> x(T(10), T(20), T(30));
nrt::PixRGB<T> y(T(1), T(2), T(3));
nrt::PixGray<T> z(T(2));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+y), nrt::PixRGB<T>(T(11), T(22), T(33)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-y), nrt::PixRGB<T>(T(9), T(18), T(27)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*y), nrt::PixRGB<T>(T(10), T(40), T(90)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/y), nrt::PixRGB<T>(T(10), T(10), T(10)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+2), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-2), nrt::PixRGB<T>(T(8), T(18), T(28)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2-x), nrt::PixRGB<T>(T(0), T(0), T(0)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*2), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/2), nrt::PixRGB<T>(T(5), T(10), T(15)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+z), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-z), nrt::PixRGB<T>(T(8), T(18), T(28)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z-x), nrt::PixRGB<T>(T(0), T(0), T(0)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*z), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/z), nrt::PixRGB<T>(T(5), T(10), T(15)));
BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
a = x; a+=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(11), T(22), T(33)));
a = x; a-=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(9), T(18), T(27)));
a = x; a*=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(40), T(90)));
a = x; a/=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(10), T(10)));
a = x; a+=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
a = x; a-=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
a = x; a*=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
a = x; a/=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
a = x; a+=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
a = x; a-=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
a = x; a*=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
a = x; a/=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
}
BOOST_AUTO_TEST_CASE_TEMPLATE(FloatingPixelOpsTest, T, floating_types)
{
// Value, clamping and operator checking:
nrt::PixRGB<T> x(T(10), T(20), T(30));
nrt::PixRGB<T> y(T(1), T(2), T(3));
nrt::PixGray<T> z(T(2));
// Set the tolerance to machine precision
const T tol(boost::numeric::bounds<T>::smallest());
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x+y) - nrt::PixRGB<T>(T(11), T(22), T(33))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x-y) - nrt::PixRGB<T>(T(9), T(18), T(27))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x*y) - nrt::PixRGB<T>(T(10), T(40), T(90))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x/y) - nrt::PixRGB<T>(T(10), T(10), T(10))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x+2) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2+x) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x-2) - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2-x) - nrt::PixRGB<T>(T(-8), T(-18), T(-28))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x*2) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2*x) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x/2) - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2/x) - nrt::PixRGB<T>(T(.2), T(.1), T(2.0/30.0))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x+z) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z+x) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x-z) - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z-x) - nrt::PixRGB<T>(T(-8), T(-18), T(-28))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x*z) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z*x) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x/z) - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z/x) - nrt::PixRGB<T>(T(.2), T(.1), T(2.0/30.0))).val(), 0, tol);
a = x; a+=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(11), T(22), T(33))).val(), 0, tol);
a = x; a-=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(9), T(18), T(27))).val(), 0, tol);
a = x; a*=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(10), T(40), T(90))).val(), 0, tol);
a = x; a/=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(10), T(10), T(10))).val(), 0, tol);
a = x; a+=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
a = x; a-=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
a = x; a*=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
a = x; a/=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
a = x; a+=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
a = x; a-=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
a = x; a*=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
a = x; a/=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
}