iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-Pixel.C
Go to the documentation of this file.
1 /*! @file
2  @author Unknown
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 #include <nrt/config.h>
37 
38 #include <boost/mpl/list.hpp>
39 
40 #define BOOST_TEST_DYN_LINK
41 #define BOOST_TEST_MODULE PixelTest
42 #include <boost/test/unit_test.hpp>
43 
44 
45 BOOST_AUTO_TEST_CASE(PixelPromotionTest)
46 {
47 
50 
51  rgb = nrt::PixRGB<nrt::byte>(rgbd);
53 
54  //nrt::PixRGBD<nrt::byte, uint16_t> rgbd2 = rgbd + rgbd;
55 
56  // Type and promotion checking:
57  nrt::PixRGB<nrt::byte> pix(1, 2, 3);
58 
59  // Promote the pixel using nrt::promote. We store the result into an auto so that we can then test for what the type
60  // of that result is (as opposed to storing it, with possible automatic conversion, into a fixed type):
61 
62  // #################### Pixel promoted by a POD type:
63 
64  // byte * float = float
65  auto pixf = nrt::promote<nrt::PixRGB<nrt::byte>, float>::type(pix);
66  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixf).name()), "nrt::PixRGB<float>");
67 
68  // byte * double = double
69  auto pixd = nrt::promote<nrt::PixRGB<nrt::byte>, double>::type(pix);
70  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixd).name()), "nrt::PixRGB<double>");
71 
72  // byte * void = byte (compiler calls that unsigned char)
73  auto pixv = nrt::promote<nrt::PixRGB<nrt::byte>, void>::type(pix);
74  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixv).name()), "nrt::PixRGB<unsigned char>");
75 
76  // byte * byte = int
77  auto pixb = nrt::promote<nrt::PixRGB<nrt::byte>, nrt::byte>::type(pix);
78  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixb).name()), "nrt::PixRGB<int>");
79 
80  // float * float = float
81  auto pixff = nrt::promote<nrt::PixRGB<float>, float>::type(pixf);
82  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixff).name()), "nrt::PixRGB<float>");
83 
84  // float * double = double
85  auto pixfd = nrt::promote<nrt::PixRGB<float>, double>::type(pixf);
86  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixfd).name()), "nrt::PixRGB<double>");
87 
88  // float * byte = float
89  auto pixfb = nrt::promote<nrt::PixRGB<float>, nrt::byte>::type(pixf);
90  BOOST_CHECK_EQUAL(nrt::demangle(typeid(pixfb).name()), "nrt::PixRGB<float>");
91 
92  // #################### Pixel promoted by a another pixel:
93  // byte * float = float
94  auto ppixgf = nrt::promote<nrt::PixRGB<nrt::byte>, nrt::PixGray<float> >::type(pix);
95  BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixgf).name()), "nrt::PixRGB<float>");
96 
97  // byte * float = float
98  auto ppixrf = nrt::promote<nrt::PixRGB<nrt::byte>, nrt::PixRGB<float> >::type(pix);
99  BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixrf).name()), "nrt::PixRGB<float>");
100 
101  // byte * byte = int
103  BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixgb).name()), "nrt::PixRGB<int>");
104 
105  // byte * byte = int
107  BOOST_CHECK_EQUAL(nrt::demangle(typeid(ppixrb).name()), "nrt::PixRGB<int>");
108 }
109 
110 typedef boost::mpl::list<unsigned long int> unsigned_types;
111 typedef boost::mpl::list<char, short int, int, long> signed_types;
112 typedef boost::mpl::list<float, double> floating_types;
113 
114 BOOST_AUTO_TEST_CASE_TEMPLATE(SignedPixelOpsTest, T, signed_types)
115 {
116  // Value, clamping and operator checking:
117  nrt::PixRGB<T> x(T(10), T(20), T(30));
118  nrt::PixRGB<T> y(T(1), T(2), T(3));
119  nrt::PixGray<T> z(T(2));
120 
121  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+y), nrt::PixRGB<T>(T(11), T(22), T(33)));
122  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-y), nrt::PixRGB<T>(T(9), T(18), T(27)));
123  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*y), nrt::PixRGB<T>(T(10), T(40), T(90)));
124  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/y), nrt::PixRGB<T>(T(10), T(10), T(10)));
125 
126  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+2), nrt::PixRGB<T>(T(12), T(22), T(32)));
127  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
128 
129  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-2), nrt::PixRGB<T>(T(8), T(18), T(28)));
130  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2-x), nrt::PixRGB<T>(T(-8), T(-18), T(-28)));
131 
132  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*2), nrt::PixRGB<T>(T(20), T(40), T(60)));
133  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
134 
135  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/2), nrt::PixRGB<T>(T(5), T(10), T(15)));
136  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
137 
138  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+z), nrt::PixRGB<T>(T(12), T(22), T(32)));
139  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
140 
141  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-z), nrt::PixRGB<T>(T(8), T(18), T(28)));
142  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z-x), nrt::PixRGB<T>(T(-8), T(-18), T(-28)));
143 
144  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*z), nrt::PixRGB<T>(T(20), T(40), T(60)));
145  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
146 
147  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/z), nrt::PixRGB<T>(T(5), T(10), T(15)));
148  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
149 
150  nrt::PixRGB<T> a;
151 
152  a = x; a+=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(11), T(22), T(33)));
153  a = x; a-=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(9), T(18), T(27)));
154  a = x; a*=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(40), T(90)));
155  a = x; a/=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(10), T(10)));
156 
157  a = x; a+=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
158  a = x; a-=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
159  a = x; a*=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
160  a = x; a/=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
161 
162  a = x; a+=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
163  a = x; a-=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
164  a = x; a*=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
165  a = x; a/=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
166 }
167 
168 BOOST_AUTO_TEST_CASE_TEMPLATE(UnsignedPixelOpsTest, T, unsigned_types)
169 {
170  // Value, clamping and operator checking:
171  nrt::PixRGB<T> x(T(10), T(20), T(30));
172  nrt::PixRGB<T> y(T(1), T(2), T(3));
173  nrt::PixGray<T> z(T(2));
174 
175  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+y), nrt::PixRGB<T>(T(11), T(22), T(33)));
176  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-y), nrt::PixRGB<T>(T(9), T(18), T(27)));
177  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*y), nrt::PixRGB<T>(T(10), T(40), T(90)));
178  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/y), nrt::PixRGB<T>(T(10), T(10), T(10)));
179 
180  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+2), nrt::PixRGB<T>(T(12), T(22), T(32)));
181  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
182 
183  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-2), nrt::PixRGB<T>(T(8), T(18), T(28)));
184  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2-x), nrt::PixRGB<T>(T(0), T(0), T(0)));
185 
186  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*2), nrt::PixRGB<T>(T(20), T(40), T(60)));
187  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
188 
189  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/2), nrt::PixRGB<T>(T(5), T(10), T(15)));
190  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(2/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
191 
192  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x+z), nrt::PixRGB<T>(T(12), T(22), T(32)));
193  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z+x), nrt::PixRGB<T>(T(12), T(22), T(32)));
194 
195  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x-z), nrt::PixRGB<T>(T(8), T(18), T(28)));
196  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z-x), nrt::PixRGB<T>(T(0), T(0), T(0)));
197 
198  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x*z), nrt::PixRGB<T>(T(20), T(40), T(60)));
199  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z*x), nrt::PixRGB<T>(T(20), T(40), T(60)));
200 
201  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(x/z), nrt::PixRGB<T>(T(5), T(10), T(15)));
202  BOOST_CHECK_EQUAL(nrt::PixRGB<T>(z/x), nrt::PixRGB<T>(T(0), T(0), T(0)));
203 
204  nrt::PixRGB<T> a;
205 
206  a = x; a+=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(11), T(22), T(33)));
207  a = x; a-=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(9), T(18), T(27)));
208  a = x; a*=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(40), T(90)));
209  a = x; a/=y; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(10), T(10), T(10)));
210 
211  a = x; a+=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
212  a = x; a-=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
213  a = x; a*=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
214  a = x; a/=2; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
215 
216  a = x; a+=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(12), T(22), T(32)));
217  a = x; a-=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(8), T(18), T(28)));
218  a = x; a*=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(20), T(40), T(60)));
219  a = x; a/=z; BOOST_CHECK_EQUAL(a, nrt::PixRGB<T>(T(5), T(10), T(15)));
220 }
221 
222 BOOST_AUTO_TEST_CASE_TEMPLATE(FloatingPixelOpsTest, T, floating_types)
223 {
224  // Value, clamping and operator checking:
225  nrt::PixRGB<T> x(T(10), T(20), T(30));
226  nrt::PixRGB<T> y(T(1), T(2), T(3));
227  nrt::PixGray<T> z(T(2));
228 
229  // Set the tolerance to machine precision
230  const T tol(boost::numeric::bounds<T>::smallest());
231 
232  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x+y) - nrt::PixRGB<T>(T(11), T(22), T(33))).val(), 0, tol);
233  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x-y) - nrt::PixRGB<T>(T(9), T(18), T(27))).val(), 0, tol);
234  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x*y) - nrt::PixRGB<T>(T(10), T(40), T(90))).val(), 0, tol);
235  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x/y) - nrt::PixRGB<T>(T(10), T(10), T(10))).val(), 0, tol);
236 
237  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x+2) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
238  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2+x) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
239 
240  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x-2) - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
241  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2-x) - nrt::PixRGB<T>(T(-8), T(-18), T(-28))).val(), 0, tol);
242 
243  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x*2) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
244  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(2*x) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
245 
246  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x/2) - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
247  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);
248 
249  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x+z) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
250  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z+x) - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
251 
252  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x-z) - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
253  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z-x) - nrt::PixRGB<T>(T(-8), T(-18), T(-28))).val(), 0, tol);
254 
255  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x*z) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
256  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(z*x) - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
257 
258  BOOST_CHECK_CLOSE(nrt::PixGray<T>(nrt::PixRGB<T>(x/z) - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
259  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);
260 
261  nrt::PixRGB<T> a;
262 
263  a = x; a+=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(11), T(22), T(33))).val(), 0, tol);
264  a = x; a-=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(9), T(18), T(27))).val(), 0, tol);
265  a = x; a*=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(10), T(40), T(90))).val(), 0, tol);
266  a = x; a/=y; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(10), T(10), T(10))).val(), 0, tol);
267 
268  a = x; a+=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
269  a = x; a-=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
270  a = x; a*=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
271  a = x; a/=2; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
272 
273  a = x; a+=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(12), T(22), T(32))).val(), 0, tol);
274  a = x; a-=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(8), T(18), T(28))).val(), 0, tol);
275  a = x; a*=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(20), T(40), T(60))).val(), 0, tol);
276  a = x; a/=z; BOOST_CHECK_CLOSE(nrt::PixGray<T>(a - nrt::PixRGB<T>(T(5), T(10), T(15))).val(), 0, tol);
277 }