xref: /aosp_15_r20/external/sandboxed-api/contrib/libraw/test/libraw_test.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2022 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #include "contrib/libraw/sandboxed.h"
16*ec63e07aSXin Li #include "contrib/libraw/utils/utils_libraw.h"
17*ec63e07aSXin Li #include "sandboxed_api/util/path.h"
18*ec63e07aSXin Li #include "sandboxed_api/util/status_matchers.h"
19*ec63e07aSXin Li #include "sandboxed_api/util/temp_file.h"
20*ec63e07aSXin Li 
21*ec63e07aSXin Li namespace {
22*ec63e07aSXin Li 
23*ec63e07aSXin Li using ::sapi::IsOk;
24*ec63e07aSXin Li 
25*ec63e07aSXin Li const struct TestVariant {
26*ec63e07aSXin Li   std::string filename;
27*ec63e07aSXin Li   ushort raw_height;
28*ec63e07aSXin Li   ushort raw_width;
29*ec63e07aSXin Li   int COLOR[4][4];
30*ec63e07aSXin Li   int color_values[4][4];
31*ec63e07aSXin Li } kTestData[] = {{.filename = "img.raw",
32*ec63e07aSXin Li                   .raw_height = 540,
33*ec63e07aSXin Li                   .raw_width = 960,
34*ec63e07aSXin Li                   .COLOR =
35*ec63e07aSXin Li                       {
36*ec63e07aSXin Li                           {0, 1, 0, 1},
37*ec63e07aSXin Li                           {3, 2, 3, 2},
38*ec63e07aSXin Li                           {0, 1, 0, 1},
39*ec63e07aSXin Li                           {3, 2, 3, 2},
40*ec63e07aSXin Li                       },
41*ec63e07aSXin Li                   .color_values = {
42*ec63e07aSXin Li                       {548, 1285, 554, 1253},
43*ec63e07aSXin Li                       {1290, 789, 1279, 788},
44*ec63e07aSXin Li                       {551, 1303, 549, 1253},
45*ec63e07aSXin Li                       {1265, 809, 1257, 779},
46*ec63e07aSXin Li                   }}};
47*ec63e07aSXin Li 
48*ec63e07aSXin Li class LibRawBase : public testing::Test {
49*ec63e07aSXin Li  protected:
GetTestFilePath(const std::string & filename)50*ec63e07aSXin Li   std::string GetTestFilePath(const std::string& filename) {
51*ec63e07aSXin Li     return sapi::file::JoinPath(test_dir_, filename);
52*ec63e07aSXin Li   }
53*ec63e07aSXin Li 
54*ec63e07aSXin Li   void SetUp() override;
55*ec63e07aSXin Li 
56*ec63e07aSXin Li   const char* test_dir_;
57*ec63e07aSXin Li };
58*ec63e07aSXin Li 
59*ec63e07aSXin Li class LibRawTestFiles : public LibRawBase,
60*ec63e07aSXin Li                         public testing::WithParamInterface<TestVariant> {};
61*ec63e07aSXin Li 
SetUp()62*ec63e07aSXin Li void LibRawBase::SetUp() {
63*ec63e07aSXin Li   test_dir_ = getenv("TEST_FILES_DIR");
64*ec63e07aSXin Li   ASSERT_NE(test_dir_, nullptr);
65*ec63e07aSXin Li }
66*ec63e07aSXin Li 
TEST_P(LibRawTestFiles,TestOpen)67*ec63e07aSXin Li TEST_P(LibRawTestFiles, TestOpen) {
68*ec63e07aSXin Li   const TestVariant& tv = GetParam();
69*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
70*ec63e07aSXin Li 
71*ec63e07aSXin Li   LibRawSapiSandbox sandbox(test_file_path);
72*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
73*ec63e07aSXin Li 
74*ec63e07aSXin Li   LibRaw lr(&sandbox, test_file_path);
75*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.CheckIsInit());
76*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.OpenFile());
77*ec63e07aSXin Li }
78*ec63e07aSXin Li 
TEST_P(LibRawTestFiles,TestUnpack)79*ec63e07aSXin Li TEST_P(LibRawTestFiles, TestUnpack) {
80*ec63e07aSXin Li   const TestVariant& tv = GetParam();
81*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
82*ec63e07aSXin Li 
83*ec63e07aSXin Li   LibRawSapiSandbox sandbox(test_file_path);
84*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
85*ec63e07aSXin Li 
86*ec63e07aSXin Li   LibRaw lr(&sandbox, test_file_path);
87*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.CheckIsInit());
88*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.OpenFile());
89*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.Unpack());
90*ec63e07aSXin Li }
91*ec63e07aSXin Li 
TEST_P(LibRawTestFiles,TestSize)92*ec63e07aSXin Li TEST_P(LibRawTestFiles, TestSize) {
93*ec63e07aSXin Li   const TestVariant& tv = GetParam();
94*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
95*ec63e07aSXin Li 
96*ec63e07aSXin Li   LibRawSapiSandbox sandbox(test_file_path);
97*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
98*ec63e07aSXin Li 
99*ec63e07aSXin Li   LibRaw lr(&sandbox, test_file_path);
100*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.CheckIsInit());
101*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.OpenFile());
102*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.Unpack());
103*ec63e07aSXin Li 
104*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(ushort raw_height, lr.GetRawHeight());
105*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(ushort raw_width, lr.GetRawWidth());
106*ec63e07aSXin Li 
107*ec63e07aSXin Li   EXPECT_EQ(raw_height, tv.raw_height);
108*ec63e07aSXin Li   EXPECT_EQ(raw_width, tv.raw_width);
109*ec63e07aSXin Li }
110*ec63e07aSXin Li 
TEST_P(LibRawTestFiles,TestCameraList)111*ec63e07aSXin Li TEST_P(LibRawTestFiles, TestCameraList) {
112*ec63e07aSXin Li   const TestVariant& tv = GetParam();
113*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
114*ec63e07aSXin Li 
115*ec63e07aSXin Li   LibRawSapiSandbox sandbox(test_file_path);
116*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
117*ec63e07aSXin Li 
118*ec63e07aSXin Li   LibRaw lr(&sandbox, test_file_path);
119*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.CheckIsInit());
120*ec63e07aSXin Li 
121*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(std::vector<char*> camera_list, lr.GetCameraList());
122*ec63e07aSXin Li 
123*ec63e07aSXin Li   EXPECT_FALSE(camera_list.empty());
124*ec63e07aSXin Li }
125*ec63e07aSXin Li 
TEST_P(LibRawTestFiles,TestColor)126*ec63e07aSXin Li TEST_P(LibRawTestFiles, TestColor) {
127*ec63e07aSXin Li   const TestVariant& tv = GetParam();
128*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
129*ec63e07aSXin Li 
130*ec63e07aSXin Li   LibRawSapiSandbox sandbox(test_file_path);
131*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
132*ec63e07aSXin Li 
133*ec63e07aSXin Li   LibRaw lr(&sandbox, test_file_path);
134*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.CheckIsInit());
135*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.OpenFile());
136*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.Unpack());
137*ec63e07aSXin Li 
138*ec63e07aSXin Li   for (int row = 0; row < 4; ++row) {
139*ec63e07aSXin Li     for (int col = 0; col < 4; ++col) {
140*ec63e07aSXin Li       SAPI_ASSERT_OK_AND_ASSIGN(int color, lr.COLOR(row, col));
141*ec63e07aSXin Li       EXPECT_EQ(color, tv.COLOR[row][col]);
142*ec63e07aSXin Li     }
143*ec63e07aSXin Li   }
144*ec63e07aSXin Li }
145*ec63e07aSXin Li 
TEST_P(LibRawTestFiles,TestSubtractBlack)146*ec63e07aSXin Li TEST_P(LibRawTestFiles, TestSubtractBlack) {
147*ec63e07aSXin Li   const TestVariant& tv = GetParam();
148*ec63e07aSXin Li   std::string test_file_path = GetTestFilePath(tv.filename);
149*ec63e07aSXin Li 
150*ec63e07aSXin Li   LibRawSapiSandbox sandbox(test_file_path);
151*ec63e07aSXin Li   SAPI_ASSERT_OK(sandbox.Init());
152*ec63e07aSXin Li 
153*ec63e07aSXin Li   LibRaw lr(&sandbox, test_file_path);
154*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.CheckIsInit());
155*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.OpenFile());
156*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.Unpack());
157*ec63e07aSXin Li   SAPI_ASSERT_OK(lr.SubtractBlack());
158*ec63e07aSXin Li 
159*ec63e07aSXin Li   libraw_data_t lr_data = lr.GetImgData();
160*ec63e07aSXin Li 
161*ec63e07aSXin Li   SAPI_ASSERT_OK_AND_ASSIGN(std::vector<uint16_t> rawdata, lr.RawData());
162*ec63e07aSXin Li 
163*ec63e07aSXin Li   for (int row = 0; row < 4; ++row) {
164*ec63e07aSXin Li     unsigned rcolors[48];
165*ec63e07aSXin Li     if (lr_data.idata.colors > 1) {
166*ec63e07aSXin Li       for (int c = 0; c < 48; c++) {
167*ec63e07aSXin Li         SAPI_ASSERT_OK_AND_ASSIGN(int color, lr.COLOR(row, c));
168*ec63e07aSXin Li         rcolors[c] = color;
169*ec63e07aSXin Li       }
170*ec63e07aSXin Li     } else {
171*ec63e07aSXin Li       memset(rcolors, 0, sizeof(rcolors));
172*ec63e07aSXin Li     }
173*ec63e07aSXin Li 
174*ec63e07aSXin Li     for (int col = 0; col < 4; col++) {
175*ec63e07aSXin Li       int raw_idx = row * lr.GetImgData().sizes.raw_pitch / 2 + col;
176*ec63e07aSXin Li       unsigned black_level = lr_data.color.cblack[rcolors[col % 48]];
177*ec63e07aSXin Li       int color_value =
178*ec63e07aSXin Li           rawdata[raw_idx] > black_level ? rawdata[raw_idx] - black_level : 0;
179*ec63e07aSXin Li       EXPECT_EQ(color_value, tv.color_values[row][col]);
180*ec63e07aSXin Li     }
181*ec63e07aSXin Li   }
182*ec63e07aSXin Li }
183*ec63e07aSXin Li 
184*ec63e07aSXin Li INSTANTIATE_TEST_SUITE_P(LibRawBase, LibRawTestFiles,
185*ec63e07aSXin Li                          testing::ValuesIn(kTestData));
186*ec63e07aSXin Li 
187*ec63e07aSXin Li }  // namespace
188