Updating Contact email
[BrunelResearch-dirac.git] / unit_tests / frames_test.cpp
blobb9ff43b4c0ecc72613678ee108546428fa7fb0e3
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$ $Name$
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License
8 * Version 1.1 (the "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 * the specific language governing rights and limitations under the License.
16 * The Original Code is Steve Bearcroft's code.
18 * The Initial Developer of the Original Code is Steve Bearcroft.
19 * Portions created by the Initial Developer are Copyright (C) 2004.
20 * All Rights Reserved.
22 * Contributor(s): Steve Bearcroft (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU General Public License Version 2 (the "GPL"), or the GNU Lesser
26 * Public License Version 2.1 (the "LGPL"), in which case the provisions of
27 * the GPL or the LGPL are applicable instead of those above. If you wish to
28 * allow use of your version of this file only under the terms of the either
29 * the GPL or LGPL and not to allow others to use your version of this file
30 * under the MPL, indicate your decision by deleting the provisions above
31 * and replace them with the notice and other provisions required by the GPL
32 * or LGPL. If you do not delete the provisions above, a recipient may use
33 * your version of this file under the terms of any one of the MPL, the GPL
34 * or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
36 #include "core_suite.h"
37 #include "frames_test.h"
38 #include "arrays_test.h"
40 #include <libdirac_common/picture.h>
41 using namespace dirac;
43 #include <memory>
45 //NOTE: ensure that the suite is added to the default registry in
46 //cppunit_testsuite.cpp
47 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION (PicturesTest, coreSuiteName());
49 void PicturesTest::setupPicture (Picture& picture, int start_val)
51 setupPicArray(picture.Data(Y_COMP), start_val);
52 setupPicArray(picture.Data(U_COMP), start_val);
53 setupPicArray(picture.Data(V_COMP), start_val);
56 bool PicturesTest::setupPicArray (PicArray &arr, int start_val)
58 char value =start_val; // use char to limit values to 8 bits
59 int err_count = 0;
60 int i, j;
61 for ( i =arr.FirstY(); i <= arr.LastY(); i++)
63 for ( j =arr.FirstX(); j <= arr.LastX(); j++)
65 arr[i][j] = ++value;
68 value = start_val;
69 for ( i =arr.FirstY(); i <= arr.LastY(); i++)
71 for ( j =arr.FirstX(); j <= arr.LastX(); j++)
73 ++value;
74 if (arr[i][j] != value)
75 err_count++;
78 CPPUNIT_ASSERT_EQUAL (err_count, 0);
79 return true;
82 void PicturesTest::zeroPicture (Picture& picture)
84 zeroPicArray(picture.Data(Y_COMP));
85 zeroPicArray(picture.Data(U_COMP));
86 zeroPicArray(picture.Data(V_COMP));
89 bool PicturesTest::zeroPicArray (PicArray &arr)
91 short value =0;
92 int err_count = 0;
93 int i, j;
94 for ( i =arr.FirstY(); i <= arr.LastY(); i++)
96 for ( j =arr.FirstX(); j <= arr.LastX(); j++)
98 arr[i][j] = value;
101 value = 0;
102 for ( i =arr.FirstY(); i <= arr.LastY(); i++)
104 for ( j =arr.FirstX(); j <= arr.LastX(); j++)
106 if (arr[i][j] != value)
107 err_count++;
110 CPPUNIT_ASSERT_EQUAL (err_count, 0);
111 return true;
115 bool PicturesTest::equalPicArrays (const PicArray &lhs, const PicArray &rhs)
117 CPPUNIT_ASSERT_EQUAL (lhs.CSort(), rhs.CSort());
118 CPPUNIT_ASSERT_EQUAL (lhs.LengthX(), rhs.LengthX());
119 CPPUNIT_ASSERT_EQUAL (lhs.LengthY(), rhs.LengthY());
120 CPPUNIT_ASSERT_EQUAL (lhs.FirstX(), rhs.FirstX());
121 CPPUNIT_ASSERT_EQUAL (lhs.FirstY(), rhs.FirstY());
122 CPPUNIT_ASSERT_EQUAL (lhs.LastX(), rhs.LastX() );
123 CPPUNIT_ASSERT_EQUAL (lhs.LastY(), rhs.LastY() );
125 for (int i =lhs.FirstY(); i <= lhs.LastY(); i++)
127 ValueType * lshRow = lhs[i];
128 ValueType * rshRow = rhs[i];
129 for (int j =lhs.FirstX(); j <= lhs.LastX(); j++)
131 if (!( lshRow[j] == rshRow[j]))
133 return false;
138 return true;
142 bool PicturesTest::almostEqualPicArrays (const PicArray &lhs, const PicArray &rhs, int allowedError)
144 CPPUNIT_ASSERT_EQUAL (lhs.CSort(), rhs.CSort());
145 CPPUNIT_ASSERT_EQUAL (lhs.LengthX(), rhs.LengthX());
146 CPPUNIT_ASSERT_EQUAL (lhs.LengthY(), rhs.LengthY());
147 CPPUNIT_ASSERT_EQUAL (lhs.FirstX(), rhs.FirstX());
148 CPPUNIT_ASSERT_EQUAL (lhs.FirstY(), rhs.FirstY());
149 CPPUNIT_ASSERT_EQUAL (lhs.LastX(), rhs.LastX() );
150 CPPUNIT_ASSERT_EQUAL (lhs.LastY(), rhs.LastY() );
152 for (int i =lhs.FirstY(); i <= lhs.LastY(); i++)
154 ValueType * lshRow = lhs[i];
155 ValueType * rshRow = rhs[i];
156 for (int j =lhs.FirstX(); j <= lhs.LastX(); j++)
158 if ( allowedError < std::abs(lshRow[j] - rshRow[j]))
160 return false;
165 return true;
168 bool PicturesTest::equalPictures (const Picture &lhs, const Picture &rhs)
170 CPPUNIT_ASSERT_EQUAL (lhs.GetPparams().CFormat(), rhs.GetPparams().CFormat() );
171 CPPUNIT_ASSERT (equalPicArrays(lhs.Data(Y_COMP), rhs.Data(Y_COMP)));
172 CPPUNIT_ASSERT (equalPicArrays(lhs.Data(U_COMP), rhs.Data(U_COMP)));
173 CPPUNIT_ASSERT (equalPicArrays(lhs.Data(V_COMP), rhs.Data(V_COMP)));
174 CPPUNIT_ASSERT_EQUAL (lhs.GetPparams().LumaDepth(), rhs.GetPparams().LumaDepth() );
175 CPPUNIT_ASSERT_EQUAL (lhs.GetPparams().ChromaDepth(), rhs.GetPparams().ChromaDepth() );
177 return true;
180 bool PicturesTest::almostEqualPictures (const Picture &lhs, const Picture &rhs, int allowedError)
182 CPPUNIT_ASSERT_EQUAL (lhs.GetPparams().CFormat(), rhs.GetPparams().CFormat() );
183 CPPUNIT_ASSERT (almostEqualPicArrays(lhs.Data(Y_COMP), rhs.Data(Y_COMP), allowedError));
184 CPPUNIT_ASSERT (almostEqualPicArrays(lhs.Data(U_COMP), rhs.Data(U_COMP), allowedError));
185 CPPUNIT_ASSERT (almostEqualPicArrays(lhs.Data(V_COMP), rhs.Data(V_COMP), allowedError));
187 return true;
190 PicturesTest::PicturesTest()
194 PicturesTest::~PicturesTest()
198 void PicturesTest::setUp()
202 void PicturesTest::tearDown()
206 void PicturesTest::testConstructor()
208 PictureParams p_params(format444, 20, 30, 8, 8);
209 Picture picture(p_params);
211 CPPUNIT_ASSERT_EQUAL (20, picture.Data(Y_COMP).LengthX());
212 CPPUNIT_ASSERT_EQUAL (30, picture.Data(Y_COMP).LengthY());
213 CPPUNIT_ASSERT_EQUAL (20, picture.Data(Y_COMP).LastX() - picture.Data(Y_COMP).FirstX() + 1);
214 CPPUNIT_ASSERT_EQUAL (30, picture.Data(Y_COMP).LastY() - picture.Data(Y_COMP).FirstY() + 1);
217 void PicturesTest::testDefaultPictureParams()
219 PictureParams p_params;
220 Picture picture(p_params);
222 CPPUNIT_ASSERT_EQUAL (0, picture.Data(Y_COMP).LengthX());
223 CPPUNIT_ASSERT_EQUAL (0, picture.Data(Y_COMP).LengthY());
224 CPPUNIT_ASSERT_EQUAL (0, picture.Data(Y_COMP).FirstX());
225 CPPUNIT_ASSERT_EQUAL (0, picture.Data(Y_COMP).FirstY());
226 CPPUNIT_ASSERT_EQUAL (-1, picture.Data(Y_COMP).LastX());
227 CPPUNIT_ASSERT_EQUAL (-1, picture.Data(Y_COMP).LastY());
230 void PicturesTest::testCopyConstructor()
232 PictureParams p_params(format444, 20, 30, 8, 8);
233 Picture picture(p_params);
234 setupPicture(picture, 0);
236 Picture picture_copy(picture);
237 CPPUNIT_ASSERT (equalPictures (picture, picture_copy));
240 void PicturesTest::testAssignment()
242 PictureParams p_params(format444, 20, 30, 8, 8);
243 Picture picture(p_params);
244 setupPicture(picture, 0);
246 PictureParams p_params_copy(format444, 10, 10, 8, 8);
247 Picture picture_copy(p_params_copy);
249 picture_copy = picture;
251 CPPUNIT_ASSERT (equalPictures (picture, picture_copy));