1 // Copyright 2020 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 <array>
16 #include <cstdint>
17 #include <cstring>
18
19 #include "helper.h" // NOLINT(build/include)
20 #include "gtest/gtest.h"
21 #include "tiffio.h" // NOLINT(build/include)
22
23 using ::sapi::IsOk;
24 using ::testing::Eq;
25 using ::testing::IsFalse;
26 using ::testing::IsTrue;
27 using ::testing::NotNull;
28
29 // sapi functions:
30 // TIFFOpen
31 // TIFFClose
32 // TIFFGetField
33 // TIFFSetField
34 // TIFFTileSize
35 // TIFFReadRGBATile
36 // TIFFReadEncodedTile
37
38 namespace {
39
40 constexpr std::array<uint8_t, 6> kCluster0 = {0, 0, 2, 0, 138, 139};
41 constexpr std::array<uint8_t, 6> kCluster64 = {0, 0, 9, 6, 134, 119};
42 constexpr std::array<uint8_t, 6> kCluster128 = {44, 40, 63, 59, 230, 95};
43
44 template <typename ArrayT>
CheckCluster(int cluster,const sapi::v::Array<uint8_t> & buffer,const ArrayT & expected_cluster)45 int CheckCluster(int cluster, const sapi::v::Array<uint8_t>& buffer,
46 const ArrayT& expected_cluster) {
47 uint8_t* target = buffer.GetData() + cluster * 6;
48
49 bool comp = !(std::memcmp(target, expected_cluster.data(), 6) == 0);
50
51 EXPECT_THAT(comp, IsFalse())
52 << "Cluster " << cluster << " did not match expected results.\n"
53 << "Expect: " << expected_cluster[0] << "\t" << expected_cluster[1]
54 << "\t" << expected_cluster[4] << "\t" << expected_cluster[5] << "\t"
55 << expected_cluster[2] << "\t" << expected_cluster[3] << "\n"
56 << "Got: " << target[0] << "\t" << target[1] << "\t" << target[4] << "\t"
57 << target[5] << "\t" << target[2] << "\t" << target[3];
58
59 return comp;
60 }
61
CheckRgbPixel(int pixel,int min_red,int max_red,int min_green,int max_green,int min_blue,int max_blue,const sapi::v::Array<uint8_t> & buffer)62 int CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
63 int max_green, int min_blue, int max_blue,
64 const sapi::v::Array<uint8_t>& buffer) {
65 uint8_t* rgb = buffer.GetData() + 3 * pixel;
66
67 bool comp =
68 !(rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green &&
69 rgb[1] <= max_green && rgb[2] >= min_blue && rgb[2] <= max_blue);
70
71 EXPECT_THAT(comp, IsFalse())
72 << "Pixel " << pixel << " did not match expected results.\n"
73 << "Got R=" << rgb[0] << " (expected " << min_red << ".." << max_red
74 << "), G=" << rgb[1] << " (expected " << min_green << ".." << max_green
75 << "), B=" << rgb[2] << " (expected " << min_blue << ".." << max_blue
76 << ")";
77 return comp;
78 }
79
CheckRgbaPixel(int pixel,int min_red,int max_red,int min_green,int max_green,int min_blue,int max_blue,int min_alpha,int max_alpha,const sapi::v::Array<uint32_t> & buffer)80 int CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
81 int max_green, int min_blue, int max_blue, int min_alpha,
82 int max_alpha, const sapi::v::Array<uint32_t>& buffer) {
83 // RGBA images are upside down - adjust for normal ordering
84 int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
85 unsigned rgba = buffer[adjusted_pixel];
86
87 bool comp = !(TIFFGetR(rgba) >= static_cast<uint32_t>(min_red) &&
88 TIFFGetR(rgba) <= static_cast<uint32_t>(max_red) &&
89 TIFFGetG(rgba) >= static_cast<uint32_t>(min_green) &&
90 TIFFGetG(rgba) <= static_cast<uint32_t>(max_green) &&
91 TIFFGetB(rgba) >= static_cast<uint32_t>(min_blue) &&
92 TIFFGetB(rgba) <= static_cast<uint32_t>(max_blue) &&
93 TIFFGetA(rgba) >= static_cast<uint32_t>(min_alpha) &&
94 TIFFGetA(rgba) <= static_cast<uint32_t>(max_alpha));
95
96 EXPECT_THAT(comp, IsFalse())
97 << "Pixel " << pixel << " did not match expected results.\n"
98 << "Got R=" << TIFFGetR(rgba) << " (expected " << min_red << ".."
99 << max_red << "), G=" << TIFFGetG(rgba) << " (expected " << min_green
100 << ".." << max_green << "), B=" << TIFFGetB(rgba) << " (expected "
101 << min_blue << ".." << max_blue << "), A=" << TIFFGetA(rgba)
102 << " (expected " << min_alpha << ".." << max_alpha << ")";
103 return comp;
104 }
105
TEST(SandboxTest,RawDecode)106 TEST(SandboxTest, RawDecode) {
107 std::string srcfile = GetFilePath("test/images/quad-tile.jpg.tiff");
108
109 TiffSapiSandbox sandbox("", srcfile);
110 ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
111
112 tsize_t sz;
113 unsigned int pixel_status = 0;
114 sapi::v::UShort h;
115 sapi::v::UShort v;
116
117 TiffApi api(&sandbox);
118 sapi::v::ConstCStr srcfile_var(srcfile.c_str());
119 sapi::v::ConstCStr r_var("r");
120
121 absl::StatusOr<TIFF*> status_or_tif =
122 api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
123 ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile;
124
125 sapi::v::RemotePtr tif(status_or_tif.value());
126 ASSERT_THAT(tif.GetValue(), NotNull())
127 << "Could not open " << srcfile << ", TIFFOpen return NULL";
128
129 absl::StatusOr<int> status_or_int = api.TIFFGetField2(
130 &tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(), v.PtrBoth());
131 ASSERT_THAT(status_or_int, IsOk()) << "TIFFGetField2 fatal error";
132 EXPECT_THAT(
133 status_or_int.value() == 0 || h.GetValue() != 2 || v.GetValue() != 2,
134 IsFalse())
135 << "Could not retrieve subsampling tag";
136
137 absl::StatusOr<tmsize_t> status_or_long = api.TIFFTileSize(&tif);
138 ASSERT_THAT(status_or_int, IsOk()) << "TIFFTileSize fatal error";
139 EXPECT_THAT(status_or_long.value(), Eq(24576))
140 << "tiles are " << status_or_long.value() << " bytes";
141 sz = status_or_long.value();
142
143 sapi::v::Array<uint8_t> buffer_(sz);
144 status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz);
145 ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
146 EXPECT_THAT(status_or_long.value(), Eq(sz))
147 << "Did not get expected result code from TIFFReadEncodedTile()("
148 << static_cast<int>(status_or_long.value()) << " instead of " << (int)sz
149 << ")";
150
151 ASSERT_FALSE(CheckCluster(0, buffer_, kCluster0) ||
152 CheckCluster(64, buffer_, kCluster64) ||
153 CheckCluster(128, buffer_, kCluster128))
154 << "Clusters did not match expected results";
155
156 status_or_int =
157 api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
158 ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
159 EXPECT_THAT(status_or_int.value(), IsTrue())
160 << "TIFFSetFieldU1 not available";
161
162 status_or_long = api.TIFFTileSize(&tif);
163 ASSERT_THAT(status_or_long, IsOk()) << "TIFFTileSize fatal error";
164 EXPECT_THAT(status_or_long.value(), Eq(128 * 128 * 3))
165 << "tiles are " << status_or_long.value() << " bytes";
166 sz = status_or_long.value();
167
168 sapi::v::Array<uint8_t> buffer2_(sz);
169 status_or_long = api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz);
170 ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
171 EXPECT_THAT(status_or_long.value(), Eq(sz))
172 << "Did not get expected result code from TIFFReadEncodedTile()("
173 << status_or_long.value() << " instead of " << sz;
174
175 pixel_status |= CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_);
176 pixel_status |= CheckRgbPixel(64, 0, 0, 0, 0, 0, 2, buffer2_);
177 pixel_status |= CheckRgbPixel(512, 5, 6, 34, 36, 182, 196, buffer2_);
178
179 ASSERT_THAT(api.TIFFClose(&tif), IsOk()) << "TIFFClose fatal error";
180
181 status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
182 ASSERT_THAT(status_or_tif, IsOk()) << "TIFFOpen fatal error";
183
184 sapi::v::RemotePtr tif2(status_or_tif.value());
185 ASSERT_THAT(tif2.GetValue(), NotNull())
186 << "Could not open " << srcfile << ", TIFFOpen return NULL";
187
188 sapi::v::Array<uint32_t> rgba_buffer_(128 * 128);
189
190 status_or_int =
191 api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth());
192 ASSERT_THAT(status_or_int, IsOk()) << "TIFFReadRGBATile fatal error";
193 EXPECT_THAT(status_or_int.value(), IsTrue())
194 << "TIFFReadRGBATile() returned failure code";
195
196 pixel_status |=
197 CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_);
198 pixel_status |= CheckRgbaPixel(64, 0, 0, 0, 0, 0, 2, 255, 255, rgba_buffer_);
199 pixel_status |=
200 CheckRgbaPixel(512, 5, 6, 34, 36, 182, 196, 255, 255, rgba_buffer_);
201
202 EXPECT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error";
203
204 EXPECT_THAT(pixel_status, IsFalse()) << "wrong encoding";
205 }
206
207 } // namespace
208