2020-09-24 04:21:33 +08:00
|
|
|
// Copyright 2020 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
2020-10-28 18:32:55 +08:00
|
|
|
#include "../sandboxed.h" // NOLINT(build/include)
|
2020-10-28 22:04:14 +08:00
|
|
|
#include "../test/data.h" // NOLINT(build/include)
|
2020-10-23 00:19:27 +08:00
|
|
|
#include "absl/algorithm/container.h"
|
|
|
|
#include "absl/strings/str_join.h"
|
2020-10-28 18:32:55 +08:00
|
|
|
#include "gtest/gtest.h"
|
2020-10-11 16:20:40 +08:00
|
|
|
#include "helper.h" // NOLINT(build/include)
|
2020-10-28 18:32:55 +08:00
|
|
|
#include "sandboxed_api/util/status_matchers.h"
|
2020-09-26 19:27:13 +08:00
|
|
|
#include "tiffio.h" // NOLINT(build/include)
|
2020-09-24 04:21:33 +08:00
|
|
|
|
2020-10-11 19:06:18 +08:00
|
|
|
namespace {
|
|
|
|
|
2020-10-11 17:40:47 +08:00
|
|
|
using ::sapi::IsOk;
|
2020-10-23 00:19:27 +08:00
|
|
|
using ::testing::ContainerEq;
|
2020-10-11 17:40:47 +08:00
|
|
|
using ::testing::Eq;
|
2020-10-23 00:19:27 +08:00
|
|
|
using ::testing::Ge;
|
|
|
|
using ::testing::Gt;
|
2020-10-11 17:40:47 +08:00
|
|
|
using ::testing::IsFalse;
|
|
|
|
using ::testing::IsTrue;
|
2020-10-23 00:19:27 +08:00
|
|
|
using ::testing::Le;
|
2020-10-11 17:40:47 +08:00
|
|
|
using ::testing::NotNull;
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
void CheckCluster(uint32_t cluster, const sapi::v::Array<uint8_t>& buffer,
|
2020-10-05 01:38:10 +08:00
|
|
|
const ClusterData& expected_cluster) {
|
2020-10-23 00:19:27 +08:00
|
|
|
ASSERT_THAT(buffer.GetSize(), Ge((cluster + 1) * kClusterSize)) << "Overrun";
|
2020-09-24 04:21:33 +08:00
|
|
|
|
2020-10-01 02:45:48 +08:00
|
|
|
// the image is split on 6-bit clusters because it has YCbCr color format
|
2020-10-23 00:19:27 +08:00
|
|
|
EXPECT_THAT(
|
|
|
|
absl::MakeSpan(buffer.GetData() + cluster * kClusterSize, kClusterSize),
|
|
|
|
ContainerEq(absl::MakeSpan(expected_cluster)))
|
|
|
|
<< "Cluster " << cluster << " did not match expected results.";
|
2020-09-24 04:21:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
void CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits,
|
2020-10-01 02:45:48 +08:00
|
|
|
const sapi::v::Array<uint8_t>& buffer) {
|
2020-10-23 00:19:27 +08:00
|
|
|
ASSERT_THAT(buffer.GetSize(), Ge((pixel + 1) * kChannelsInPixel))
|
|
|
|
<< "Overrun";
|
|
|
|
|
|
|
|
uint8_t* rgb = buffer.GetData() + pixel * kChannelsInPixel;
|
|
|
|
EXPECT_THAT(rgb[0], Ge(limits.min_red));
|
|
|
|
EXPECT_THAT(rgb[0], Le(limits.max_red));
|
|
|
|
EXPECT_THAT(rgb[1], Ge(limits.min_green));
|
|
|
|
EXPECT_THAT(rgb[1], Le(limits.max_green));
|
|
|
|
EXPECT_THAT(rgb[2], Ge(limits.min_blue));
|
|
|
|
EXPECT_THAT(rgb[2], Le(limits.max_blue));
|
2020-09-24 04:21:33 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
void CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
|
2020-10-11 18:01:59 +08:00
|
|
|
const sapi::v::Array<uint32_t>& buffer) {
|
2020-09-24 05:32:45 +08:00
|
|
|
// RGBA images are upside down - adjust for normal ordering
|
2020-10-11 18:01:59 +08:00
|
|
|
uint32_t adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
|
2020-10-01 01:54:47 +08:00
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
ASSERT_THAT(buffer.GetSize(), Gt(adjusted_pixel)) << "Overrun";
|
|
|
|
|
|
|
|
uint32_t rgba = buffer[adjusted_pixel];
|
|
|
|
EXPECT_THAT(TIFFGetR(rgba), Ge(limits.min_red));
|
|
|
|
EXPECT_THAT(TIFFGetR(rgba), Le(limits.max_red));
|
|
|
|
EXPECT_THAT(TIFFGetG(rgba), Ge(limits.min_green));
|
|
|
|
EXPECT_THAT(TIFFGetG(rgba), Le(limits.max_green));
|
|
|
|
EXPECT_THAT(TIFFGetB(rgba), Ge(limits.min_blue));
|
|
|
|
EXPECT_THAT(TIFFGetB(rgba), Le(limits.max_blue));
|
|
|
|
EXPECT_THAT(TIFFGetA(rgba), Ge(limits.min_alpha));
|
|
|
|
EXPECT_THAT(TIFFGetA(rgba), Le(limits.max_alpha));
|
2020-09-24 04:21:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(SandboxTest, RawDecode) {
|
|
|
|
std::string srcfile = GetFilePath("quad-tile.jpg.tiff");
|
|
|
|
|
2020-10-01 02:45:48 +08:00
|
|
|
TiffSapiSandbox sandbox(srcfile);
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
|
|
|
|
|
|
|
|
TiffApi api(&sandbox);
|
|
|
|
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
|
|
|
|
sapi::v::ConstCStr r_var("r");
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
absl::StatusOr<TIFF*> status_or_tif =
|
|
|
|
api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile;
|
|
|
|
|
|
|
|
sapi::v::RemotePtr tif(status_or_tif.value());
|
|
|
|
ASSERT_THAT(tif.GetValue(), NotNull())
|
|
|
|
<< "Could not open " << srcfile << ", TIFFOpen return NULL";
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
sapi::v::UShort h;
|
|
|
|
sapi::v::UShort v;
|
|
|
|
absl::StatusOr<int> status_or_int = api.TIFFGetField2(
|
2020-10-23 14:54:36 +08:00
|
|
|
&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrAfter(), v.PtrAfter());
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(status_or_int, IsOk()) << "TIFFGetField2 fatal error";
|
|
|
|
EXPECT_THAT(
|
|
|
|
status_or_int.value() == 0 || h.GetValue() != 2 || v.GetValue() != 2,
|
|
|
|
IsFalse())
|
|
|
|
<< "Could not retrieve subsampling tag";
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
tsize_t sz;
|
|
|
|
absl::StatusOr<tmsize_t> status_or_long = api.TIFFTileSize(&tif);
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(status_or_int, IsOk()) << "TIFFTileSize fatal error";
|
2020-10-05 01:38:10 +08:00
|
|
|
EXPECT_THAT(status_or_long.value(), Eq(kClusterImageSize * kClusterSize))
|
|
|
|
<< "Unexpected TileSize " << status_or_long.value() << ". Expected "
|
2020-10-23 00:19:27 +08:00
|
|
|
<< kClusterImageSize * kClusterSize << " bytes";
|
2020-09-24 04:21:33 +08:00
|
|
|
sz = status_or_long.value();
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
sapi::v::Array<uint8_t> buffer(sz);
|
2020-10-01 01:54:47 +08:00
|
|
|
// Read a tile in decompressed form, but still YCbCr subsampled
|
2020-10-01 02:45:48 +08:00
|
|
|
status_or_long =
|
2020-10-23 14:54:36 +08:00
|
|
|
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer.PtrAfter(), sz);
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
|
|
|
|
EXPECT_THAT(status_or_long.value(), Eq(sz))
|
|
|
|
<< "Did not get expected result code from TIFFReadEncodedTile()("
|
2020-10-23 00:19:27 +08:00
|
|
|
<< static_cast<int>(status_or_long.value()) << " instead of "
|
|
|
|
<< static_cast<int>(sz) << ")";
|
2020-09-24 04:21:33 +08:00
|
|
|
|
2020-10-05 01:38:10 +08:00
|
|
|
for (const auto& [id, data] : kClusters) {
|
2020-10-23 00:19:27 +08:00
|
|
|
CheckCluster(id, buffer, data);
|
2020-10-05 01:38:10 +08:00
|
|
|
}
|
2020-09-24 04:21:33 +08:00
|
|
|
|
|
|
|
status_or_int =
|
|
|
|
api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
|
|
|
|
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
|
|
|
|
EXPECT_THAT(status_or_int.value(), IsTrue())
|
2020-10-05 01:38:10 +08:00
|
|
|
<< "The JPEGCOLORMODE tag cannot be changed";
|
2020-09-24 04:21:33 +08:00
|
|
|
|
|
|
|
status_or_long = api.TIFFTileSize(&tif);
|
|
|
|
ASSERT_THAT(status_or_long, IsOk()) << "TIFFTileSize fatal error";
|
2020-10-05 01:38:10 +08:00
|
|
|
EXPECT_THAT(status_or_long.value(), Eq(kImageSize * kChannelsInPixel))
|
|
|
|
<< "Unexpected TileSize " << status_or_long.value() << ". Expected "
|
2020-10-23 00:19:27 +08:00
|
|
|
<< kImageSize * kChannelsInPixel << " bytes";
|
2020-09-24 04:21:33 +08:00
|
|
|
sz = status_or_long.value();
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
sapi::v::Array<uint8_t> buffer2(sz);
|
2020-10-01 02:45:48 +08:00
|
|
|
status_or_long =
|
2020-10-23 14:54:36 +08:00
|
|
|
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2.PtrAfter(), sz);
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
|
|
|
|
EXPECT_THAT(status_or_long.value(), Eq(sz))
|
|
|
|
<< "Did not get expected result code from TIFFReadEncodedTile()("
|
|
|
|
<< status_or_long.value() << " instead of " << sz;
|
|
|
|
|
2020-10-05 01:38:10 +08:00
|
|
|
for (const auto& [id, data] : kLimits) {
|
2020-10-23 00:19:27 +08:00
|
|
|
CheckRgbPixel(id, data, buffer2);
|
2020-10-05 01:38:10 +08:00
|
|
|
}
|
2020-09-24 04:21:33 +08:00
|
|
|
|
|
|
|
ASSERT_THAT(api.TIFFClose(&tif), IsOk()) << "TIFFClose fatal error";
|
|
|
|
|
|
|
|
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
|
|
|
|
ASSERT_THAT(status_or_tif, IsOk()) << "TIFFOpen fatal error";
|
|
|
|
|
|
|
|
sapi::v::RemotePtr tif2(status_or_tif.value());
|
|
|
|
ASSERT_THAT(tif2.GetValue(), NotNull())
|
|
|
|
<< "Could not open " << srcfile << ", TIFFOpen return NULL";
|
|
|
|
|
2020-10-23 00:19:27 +08:00
|
|
|
sapi::v::Array<uint32_t> rgba_buffer(kImageSize);
|
2020-09-24 04:21:33 +08:00
|
|
|
status_or_int =
|
2020-10-23 14:54:36 +08:00
|
|
|
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer.PtrAfter());
|
2020-09-24 04:21:33 +08:00
|
|
|
ASSERT_THAT(status_or_int, IsOk()) << "TIFFReadRGBATile fatal error";
|
|
|
|
EXPECT_THAT(status_or_int.value(), IsTrue())
|
|
|
|
<< "TIFFReadRGBATile() returned failure code";
|
|
|
|
|
2020-10-05 01:38:10 +08:00
|
|
|
for (const auto& [id, data] : kLimits) {
|
2020-10-23 00:19:27 +08:00
|
|
|
CheckRgbaPixel(id, data, rgba_buffer);
|
2020-10-05 01:38:10 +08:00
|
|
|
}
|
2020-09-24 04:21:33 +08:00
|
|
|
|
|
|
|
EXPECT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error";
|
2020-09-26 05:21:07 +08:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:27:13 +08:00
|
|
|
} // namespace
|