1 /*
2  * This file is a part of Luminance HDR package
3  * ----------------------------------------------------------------------
4  * Copyright (C) 2013 Davide Anastasia
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * ----------------------------------------------------------------------
20  */
21 
22 #include <gtest/gtest.h>
23 #include <boost/assign/std/vector.hpp> // for 'operator+=()'
24 #include <boost/assert.hpp>
25 
26 #include <Libpfs/array2d.h>
27 #include <Libpfs/colorspace/colorspace.h>
28 
29 #include "PrintArray2D.h"
30 
31 typedef std::vector<float> ColorSpaceSamples;
32 
33 using namespace std;
34 using namespace boost::assign;  // bring 'operator+=()' into scope
35 
TEST(TestSRGB2Y,TestSRGB2XYZ)36 TEST(TestSRGB2Y, TestSRGB2XYZ)
37 {
38     ColorSpaceSamples redInput;
39     ColorSpaceSamples greenInput;
40     ColorSpaceSamples blueInput;
41     ColorSpaceSamples xOutput;
42     ColorSpaceSamples yOutput;
43     ColorSpaceSamples zOutput;
44 
45     redInput    += 0.f, 1.f,        0.f,        0.f,        0.2f,       1.f,        1.2f,       1.2f,       2.f;
46     greenInput  += 0.f, 0.f,        1.f,        0.f,        0.3f,       1.f,        0.f,        0.0f,       1.2f;
47     blueInput   += 0.f, 0.f,        0.f,        1.f,        0.4f,       1.f,        0.f,        1.4f,       2.f;
48     xOutput     += 0.f, 0.412456f,  0.357576f,  0.180437f,  0.261939f,  0.950470f,  0.494948f,  0.747560f,  1.614879f;
49     yOutput     += 0.f, 0.212673f,  0.715152f,  0.072175f,  0.285950f,  1.f,        0.255207f,  0.356252f,  1.427878f;
50     zOutput     += 0.f, 0.019334f,  0.119192f,  0.950304f,  0.419746f,  1.088830f,  0.023201f,  1.353626f,  2.082306f;
51 
52     ASSERT_TRUE( static_cast<bool>(yOutput.size()) );
53     ASSERT_EQ( zOutput.size(), xOutput.size() );
54     ASSERT_EQ( xOutput.size(), yOutput.size() );
55     ASSERT_EQ( yOutput.size(), blueInput.size() );
56     ASSERT_EQ( redInput.size(), blueInput.size() );
57     ASSERT_EQ( greenInput.size(), blueInput.size() );
58 
59     pfs::Array2Df A2DRed(redInput.size(), 1);
60     pfs::Array2Df A2DGreen(greenInput.size(), 1);
61     pfs::Array2Df A2DBlue(blueInput.size(), 1);
62 
63     std::copy(redInput.begin(), redInput.end(), A2DRed.begin());
64     std::copy(greenInput.begin(), greenInput.end(), A2DGreen.begin());
65     std::copy(blueInput.begin(), blueInput.end(), A2DBlue.begin());
66 
67     pfs::Array2Df A2DX(xOutput.size(), 1);
68     pfs::Array2Df A2DY(yOutput.size(), 1);
69     pfs::Array2Df A2DZ(zOutput.size(), 1);
70 
71     // function under unit test!
72     pfs::transformRGB2XYZ(&A2DRed, &A2DGreen, &A2DBlue, &A2DX, &A2DY, &A2DZ);
73 
74 //    print(A2DX);
75 //    print(A2DY);
76 //    print(A2DZ);
77 
78     for (size_t idx = 0; idx < A2DY.size(); ++idx)
79     {
80         EXPECT_NEAR(xOutput[idx], A2DX(idx), 10e-6);
81         EXPECT_NEAR(yOutput[idx], A2DY(idx), 10e-6);
82         EXPECT_NEAR(zOutput[idx], A2DZ(idx), 10e-6);
83     }
84 }
85 
TEST(TestSRGB2Y,TestSRGB2Y)86 TEST(TestSRGB2Y, TestSRGB2Y)
87 {
88     ColorSpaceSamples redInput;
89     ColorSpaceSamples greenInput;
90     ColorSpaceSamples blueInput;
91     ColorSpaceSamples yOutput;
92 
93     redInput    += 0.f, 1.f,        0.f,        0.f,        0.2f,       1.f, 1.2f;
94     greenInput  += 0.f, 0.f,        1.f,        0.f,        0.3f,       1.f, 0.5f;
95     blueInput   += 0.f, 0.f,        0.f,        1.f,        0.4f,       1.f, 0.2f;
96     yOutput     += 0.f, 0.212673f,  0.715152f,  0.072175f,  0.285950f,  1.f, 0.627218f;
97 
98     ASSERT_EQ( yOutput.size(), redInput.size() );
99     ASSERT_EQ( redInput.size(), blueInput.size() );
100     ASSERT_EQ( greenInput.size(), blueInput.size() );
101 
102     pfs::Array2Df A2DRed(redInput.size(), 1);
103     pfs::Array2Df A2DGreen(greenInput.size(), 1);
104     pfs::Array2Df A2DBlue(blueInput.size(), 1);
105     pfs::Array2Df A2DY(redInput.size(), 1);
106 
107     std::copy(redInput.begin(), redInput.end(), A2DRed.begin());
108     std::copy(greenInput.begin(), greenInput.end(), A2DGreen.begin());
109     std::copy(blueInput.begin(), blueInput.end(), A2DBlue.begin());
110 
111     // function under unit test!
112     pfs::transformRGB2Y(&A2DRed, &A2DGreen, &A2DBlue, &A2DY);
113 
114     for (size_t idx = 0; idx < A2DY.size(); ++idx)
115     {
116         EXPECT_NEAR(A2DY(idx),
117                     yOutput[idx],
118                     10e-6);
119     }
120 }
121