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