Merge pull request #3 from alexelex/alexelex-libtiff

review
This commit is contained in:
Latysheva Alexandra 2020-10-28 02:16:40 +06:00 committed by GitHub
commit 225b57aed3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 139 additions and 182 deletions

View File

@ -18,6 +18,8 @@
#include <cstring> #include <cstring>
#include "../sandboxed.h" // NOLINT(build/include) #include "../sandboxed.h" // NOLINT(build/include)
#include "absl/algorithm/container.h"
#include "absl/strings/str_join.h"
#include "sandboxed_api/sandbox2/util/fileops.h" #include "sandboxed_api/sandbox2/util/fileops.h"
#include "sandboxed_api/sandbox2/util/path.h" #include "sandboxed_api/sandbox2/util/path.h"
#include "sandboxed_api/vars.h" #include "sandboxed_api/vars.h"
@ -56,45 +58,46 @@ constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
constexpr absl::string_view kClusterErrorFormatStr = constexpr absl::string_view kClusterErrorFormatStr =
"Cluster %d did not match expected results.\n" "Cluster %d did not match expected results.\n"
"Expect:\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n" "Expect:\t%s\n"
"Got:\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n"; "Got:\t%s";
constexpr absl::string_view kRgbPixelErrorFormatStr = constexpr absl::string_view kRgbPixelErrorFormatStr =
"Pixel %d did not match expected results.\n" "Pixel %d did not match expected results.\n"
"Got R=%d (expected %d..%d), G=%d (expected %d..%d), " "Got R=%d (expected %d..%d), G=%d (expected %d..%d), "
"B=%d (expected %d..%d)\n"; "B=%d (expected %d..%d)";
constexpr absl::string_view kRgbaPixelErrorFormatStr = constexpr absl::string_view kRgbaPixelErrorFormatStr =
"Pixel %d did not match expected results.\n" "Pixel %d did not match expected results.\n"
"Got R=%d (expected %d..%d), G=%d (expected %d..%d), " "Got R=%d (expected %d..%d), G=%d (expected %d..%d), "
"B=%d (expected %d..%d), A=%d (expected %d..%d)\n"; "B=%d (expected %d..%d), A=%d (expected %d..%d)";
absl::Status CheckCluster(uint32_t cluster, absl::Status CheckCluster(uint32_t cluster,
const sapi::v::Array<uint8_t>& buffer, const sapi::v::Array<uint8_t>& buffer,
const ClusterData& expected_cluster) { const ClusterData& expected_cluster) {
if (buffer.GetSize() <= cluster * kClusterSize) { if (buffer.GetSize() < (cluster + 1) * kClusterSize) {
return absl::InternalError("Buffer overrun\n"); return absl::InternalError("Buffer overrun");
} }
auto* target = buffer.GetData() + cluster * kClusterSize;
if (!std::memcmp(target, expected_cluster.data(), kClusterSize)) { std::vector<uint8_t> target(buffer.GetData() + cluster * kClusterSize,
buffer.GetData() + (cluster + 1) * kClusterSize);
if (absl::c_equal(absl::MakeSpan(target), expected_cluster)) {
return absl::OkStatus(); return absl::OkStatus();
} }
// the image is split on 6-bit clusters because it has YCbCr color format // the image is split on 6-bit clusters because it has YCbCr color format
return absl::InternalError(absl::StrFormat( return absl::InternalError(absl::StrFormat(
kClusterErrorFormatStr, cluster, expected_cluster[0], expected_cluster[1], kClusterErrorFormatStr, cluster, absl::StrJoin(expected_cluster, "\t"),
expected_cluster[2], expected_cluster[3], expected_cluster[4], absl::StrJoin(target, "\t")));
expected_cluster[5], target[0], target[1], target[2], target[3],
target[4], target[5]));
} }
absl::Status CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits, absl::Status CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits,
const sapi::v::Array<uint8_t>& buffer) { const sapi::v::Array<uint8_t>& buffer) {
if (buffer.GetSize() <= pixel * kChannelsInPixel) { if (buffer.GetSize() < (pixel + 1) * kChannelsInPixel) {
return absl::InternalError("Buffer overrun\n"); return absl::InternalError("Buffer overrun");
} }
auto* rgb = buffer.GetData() + kChannelsInPixel * pixel;
uint8_t* rgb = buffer.GetData() + pixel * kChannelsInPixel;
if (rgb[0] >= limits.min_red && rgb[0] <= limits.max_red && if (rgb[0] >= limits.min_red && rgb[0] <= limits.max_red &&
rgb[1] >= limits.min_green && rgb[1] <= limits.max_green && rgb[1] >= limits.min_green && rgb[1] <= limits.max_green &&
@ -114,10 +117,10 @@ absl::Status CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
uint32_t adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128; uint32_t adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
if (buffer.GetSize() <= adjusted_pixel) { if (buffer.GetSize() <= adjusted_pixel) {
return absl::InternalError("Buffer overrun\n"); return absl::InternalError("Buffer overrun");
} }
auto rgba = buffer[adjusted_pixel]; uint32_t rgba = buffer[adjusted_pixel];
if (TIFFGetR(rgba) >= static_cast<unsigned>(limits.min_red) && if (TIFFGetR(rgba) >= static_cast<unsigned>(limits.min_red) &&
TIFFGetR(rgba) <= static_cast<unsigned>(limits.max_red) && TIFFGetR(rgba) <= static_cast<unsigned>(limits.max_red) &&
TIFFGetG(rgba) >= static_cast<unsigned>(limits.min_green) && TIFFGetG(rgba) >= static_cast<unsigned>(limits.min_green) &&
@ -138,11 +141,12 @@ absl::Status CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
} // namespace } // namespace
std::string GetFilePath(const std::string& dir, const std::string& filename) { std::string GetFilePath(const absl::string_view dir,
const absl::string_view filename) {
return sandbox2::file::JoinPath(dir, "test", "images", filename); return sandbox2::file::JoinPath(dir, "test", "images", filename);
} }
std::string GetFilePath(const std::string filename) { std::string GetFilePath(const absl::string_view filename) {
std::string cwd = sandbox2::file_util::fileops::GetCWD(); std::string cwd = sandbox2::file_util::fileops::GetCWD();
auto find = cwd.rfind("build"); auto find = cwd.rfind("build");
@ -161,7 +165,7 @@ std::string GetFilePath(const std::string filename) {
return sandbox2::file::JoinPath(project_path, "test", "images", filename); return sandbox2::file::JoinPath(project_path, "test", "images", filename);
} }
absl::Status LibTIFFMain(const std::string& srcfile) { absl::Status LibTIFFMain(const absl::string_view srcfile) {
// to use dir and file inside sapi-libtiff, use // to use dir and file inside sapi-libtiff, use
// sandbox(file) file only -- or // sandbox(file) file only -- or
// sandbox(file, dir) -- file and dir -- or // sandbox(file, dir) -- file and dir -- or
@ -171,24 +175,15 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
TiffSapiSandbox sandbox(srcfile); TiffSapiSandbox sandbox(srcfile);
bool pixel_status_ok = true;
bool cluster_status_ok = true;
// initialize sapi vars after constructing TiffSapiSandbox // initialize sapi vars after constructing TiffSapiSandbox
sapi::v::UShort h;
sapi::v::UShort v;
absl::StatusOr<TIFF*> status_or_tif;
absl::StatusOr<int> status_or_int;
absl::StatusOr<tmsize_t> status_or_long;
absl::Status status;
status = sandbox.Init();
SAPI_RETURN_IF_ERROR(sandbox.Init()); SAPI_RETURN_IF_ERROR(sandbox.Init());
TiffApi api(&sandbox); TiffApi api(&sandbox);
sapi::v::ConstCStr srcfile_var(srcfile.c_str()); sapi::v::ConstCStr srcfile_var(srcfile.data());
sapi::v::ConstCStr r_var("r"); sapi::v::ConstCStr r_var("r");
absl::StatusOr<TIFF*> status_or_tif;
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(
status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore())); status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()));
@ -197,9 +192,11 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
return absl::InternalError(absl::StrCat("Could not open ", srcfile)); return absl::InternalError(absl::StrCat("Could not open ", srcfile));
} }
SAPI_ASSIGN_OR_RETURN(auto return_value, sapi::v::UShort h;
sapi::v::UShort v;
SAPI_ASSIGN_OR_RETURN(int return_value,
api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING,
h.PtrBoth(), v.PtrBoth())); h.PtrAfter(), v.PtrAfter()));
if (return_value == 0 || h.GetValue() != 2 || v.GetValue() != 2) { if (return_value == 0 || h.GetValue() != 2 || v.GetValue() != 2) {
return absl::InternalError("Could not retrieve subsampling tag"); return absl::InternalError("Could not retrieve subsampling tag");
} }
@ -208,23 +205,25 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
if (sz != kClusterSize * kClusterImageSize) { if (sz != kClusterSize * kClusterImageSize) {
return absl::InternalError( return absl::InternalError(
absl::StrCat("Unexpected TileSize ", sz, ". Expected ", absl::StrCat("Unexpected TileSize ", sz, ". Expected ",
kClusterSize * kClusterImageSize, " bytes\n")); kClusterSize * kClusterImageSize, " bytes"));
} }
sapi::v::Array<uint8_t> buffer_(sz); sapi::v::Array<uint8_t> buffer(sz);
// Read a tile in decompressed form, but still YCbCr subsampled // Read a tile in decompressed form, but still YCbCr subsampled
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(
tsize_t new_sz, tsize_t new_sz,
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer_.PtrBoth(), sz)); api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer.PtrAfter(), sz));
if (new_sz != sz) { if (new_sz != sz) {
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrCat(
"Did not get expected result code from TIFFReadEncodedTile(): ", "Did not get expected result code from TIFFReadEncodedTile(): ", new_sz,
status_or_long.value(), " instead of ", sz)); " instead of ", sz));
} }
absl::Status status;
bool cluster_status_ok = true;
for (const auto& [id, data] : kClusters) { for (const auto& [id, data] : kClusters) {
if (status = CheckCluster(id, buffer_, data); !status.ok()) { if (status = CheckCluster(id, buffer, data); !status.ok()) {
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString(); LOG(ERROR) << "CheckCluster failed:\n" << status.ToString() << '\n';
} }
cluster_status_ok &= status.ok(); cluster_status_ok &= status.ok();
} }
@ -244,23 +243,23 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
if (sz != kChannelsInPixel * kImageSize) { if (sz != kChannelsInPixel * kImageSize) {
return absl::InternalError( return absl::InternalError(
absl::StrCat("Unexpected TileSize ", sz, ". Expected ", absl::StrCat("Unexpected TileSize ", sz, ". Expected ",
kChannelsInPixel * kImageSize, " bytes\n")); kChannelsInPixel * kImageSize, " bytes"));
} }
sapi::v::Array<uint8_t> buffer2_(sz); sapi::v::Array<uint8_t> buffer2(sz);
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(
new_sz, new_sz,
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2_.PtrBoth(), sz)); api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2.PtrAfter(), sz));
if (new_sz != sz) { if (new_sz != sz) {
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrCat(
"Did not get expected result code from TIFFReadEncodedTile(): ", new_sz, "Did not get expected result code from TIFFReadEncodedTile(): ", new_sz,
" instead of ", sz)); " instead of ", sz));
} }
bool pixel_status_ok = true;
for (const auto& [id, data] : kLimits) { for (const auto& [id, data] : kLimits) {
if (status = CheckRgbPixel(id, data, buffer2_); !status.ok()) { if (status = CheckRgbPixel(id, data, buffer2); !status.ok()) {
LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString(); LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString() << '\n';
} }
pixel_status_ok &= status.ok(); pixel_status_ok &= status.ok();
} }
@ -275,20 +274,20 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
return absl::InternalError(absl::StrCat("Could not reopen ", srcfile)); return absl::InternalError(absl::StrCat("Could not reopen ", srcfile));
} }
sapi::v::Array<uint32_t> rgba_buffer_(kImageSize); sapi::v::Array<uint32_t> rgba_buffer(kImageSize);
// read as rgba // read as rgba
SAPI_ASSIGN_OR_RETURN( SAPI_ASSIGN_OR_RETURN(
return_value, return_value,
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth())); api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer.PtrAfter()));
if (return_value == 0) { if (return_value == 0) {
return absl::InternalError("TIFFReadRGBATile() returned failure code"); return absl::InternalError("TIFFReadRGBATile() returned failure code");
} }
// Checking specific pixels from the test data, 0th, 64th and 512th // Checking specific pixels from the test data, 0th, 64th and 512th
for (const auto& [id, data] : kLimits) { for (const auto& [id, data] : kLimits) {
if (status = CheckRgbaPixel(id, data, rgba_buffer_); !status.ok()) { if (status = CheckRgbaPixel(id, data, rgba_buffer); !status.ok()) {
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString(); LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString() << '\n';
} }
pixel_status_ok &= status.ok(); pixel_status_ok &= status.ok();
} }
@ -314,7 +313,7 @@ int main(int argc, char** argv) {
srcfile = GetFilePath(argv[1], srcfilerel); srcfile = GetFilePath(argv[1], srcfilerel);
} }
auto status = LibTIFFMain(srcfile); absl::Status status = LibTIFFMain(srcfile);
if (!status.ok()) { if (!status.ok()) {
LOG(ERROR) << "LibTIFFMain failed with error:\n" LOG(ERROR) << "LibTIFFMain failed with error:\n"
<< status.ToString() << '\n'; << status.ToString() << '\n';

View File

@ -24,8 +24,8 @@ namespace {
class TiffSapiSandbox : public TiffSandbox { class TiffSapiSandbox : public TiffSandbox {
public: public:
TiffSapiSandbox(std::optional<std::string> file = std::nullopt, TiffSapiSandbox(std::optional<absl::string_view> file = std::nullopt,
std::optional<std::string> dir = std::nullopt) std::optional<absl::string_view> dir = std::nullopt)
: file_(std::move(file)), dir_(std::move(dir)) {} : file_(std::move(file)), dir_(std::move(dir)) {}
private: private:
@ -61,8 +61,8 @@ class TiffSapiSandbox : public TiffSandbox {
return builder.get()->BuildOrDie(); return builder.get()->BuildOrDie();
} }
std::optional<std::string> file_; std::optional<absl::string_view> file_;
std::optional<std::string> dir_; std::optional<absl::string_view> dir_;
}; };
} // namespace } // namespace

View File

@ -20,7 +20,7 @@ using ::testing::IsTrue;
void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field,
const uint16_t value) { const uint16_t value) {
sapi::v::UShort tmp(123); sapi::v::UShort tmp(value + 1);
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
status_or_int = api.TIFFGetField1(&tif, field, tmp.PtrBoth()); status_or_int = api.TIFFGetField1(&tif, field, tmp.PtrBoth());
@ -34,8 +34,8 @@ void CheckShortField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field,
void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif, void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif,
const ttag_t field, const ttag_t field,
const std::array<uint16_t, 2>& values) { const std::array<uint16_t, 2>& values) {
sapi::v::UShort tmp0(123); sapi::v::UShort tmp0(values[0] + 1);
sapi::v::UShort tmp1(456); sapi::v::UShort tmp1(values[1] + 1);
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
status_or_int = status_or_int =
@ -51,7 +51,7 @@ void CheckShortPairedField(TiffApi& api, sapi::v::RemotePtr& tif,
void CheckLongField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field, void CheckLongField(TiffApi& api, sapi::v::RemotePtr& tif, const ttag_t field,
const uint32_t value) { const uint32_t value) {
sapi::v::UInt tmp(123); sapi::v::UInt tmp(value + 1);
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
status_or_int = api.TIFFGetField1(&tif, field, tmp.PtrBoth()); status_or_int = api.TIFFGetField1(&tif, field, tmp.PtrBoth());

View File

@ -37,10 +37,7 @@ void TestWriting(const char* mode, int tiled, int height) {
std::string srcfile = sandbox2::file::JoinPath( std::string srcfile = sandbox2::file::JoinPath(
sandbox2::file_util::fileops::GetCWD(), status_or_path.value()); sandbox2::file_util::fileops::GetCWD(), status_or_path.value());
absl::StatusOr<int> status_or_int;
absl::StatusOr<signed long> status_or_long; absl::StatusOr<signed long> status_or_long;
absl::StatusOr<TIFF*> status_or_tif;
TiffSapiSandbox sandbox(srcfile); TiffSapiSandbox sandbox(srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
@ -49,14 +46,15 @@ void TestWriting(const char* mode, int tiled, int height) {
sapi::v::ConstCStr srcfile_var(srcfile.c_str()); sapi::v::ConstCStr srcfile_var(srcfile.c_str());
sapi::v::ConstCStr mode_var(mode); sapi::v::ConstCStr mode_var(mode);
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), mode_var.PtrBefore()); absl::StatusOr<TIFF*> status_or_tif =
api.TIFFOpen(srcfile_var.PtrBefore(), mode_var.PtrBefore());
ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile; ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile;
sapi::v::RemotePtr tif(status_or_tif.value()); sapi::v::RemotePtr tif(status_or_tif.value());
ASSERT_THAT(tif.GetValue(), NotNull()) ASSERT_THAT(tif.GetValue(), NotNull())
<< "Can't create test TIFF file " << srcfile; << "Can't create test TIFF file " << srcfile;
status_or_int = absl::StatusOr<int> status_or_int =
api.TIFFSetFieldU1(&tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE); api.TIFFSetFieldU1(&tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) EXPECT_THAT(status_or_int.value(), IsTrue())
@ -214,10 +212,11 @@ void TestWriting(const char* mode, int tiled, int height) {
for (int i = 0; i < (height + 15) / 16; ++i) { for (int i = 0; i < (height + 15) / 16; ++i) {
std::array<uint8_t, kTileBufferSize> tilebuffer; std::array<uint8_t, kTileBufferSize> tilebuffer;
tilebuffer.fill(i); tilebuffer.fill(i);
sapi::v::Array<uint8_t> tilebuffer_(tilebuffer.data(), tilebuffer.size()); sapi::v::Array<uint8_t> tilebuffer_sapi(tilebuffer.data(),
tilebuffer.size());
status_or_int = api.TIFFWriteEncodedTile(&tif, i, tilebuffer_.PtrBoth(), status_or_int = api.TIFFWriteEncodedTile(
kTileBufferSize); &tif, i, tilebuffer_sapi.PtrBefore(), kTileBufferSize);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteEncodedTile fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteEncodedTile fatal error";
EXPECT_THAT(status_or_int.value(), Eq(kTileBufferSize)) EXPECT_THAT(status_or_int.value(), Eq(kTileBufferSize))
<< "line " << i << ": expected " << kTileBufferSize << ", got " << "line " << i << ": expected " << kTileBufferSize << ", got "
@ -226,7 +225,7 @@ void TestWriting(const char* mode, int tiled, int height) {
} else { } else {
for (int i = 0; i < height; ++i) { for (int i = 0; i < height; ++i) {
sapi::v::UChar c(i); sapi::v::UChar c(i);
status_or_long = api.TIFFWriteEncodedStrip(&tif, i, c.PtrBoth(), 1); status_or_long = api.TIFFWriteEncodedStrip(&tif, i, c.PtrBefore(), 1);
ASSERT_THAT(status_or_long, IsOk()) ASSERT_THAT(status_or_long, IsOk())
<< "TIFFWriteEncodedStrip fatal error"; << "TIFFWriteEncodedStrip fatal error";
EXPECT_THAT(status_or_int.value(), Eq(1)) EXPECT_THAT(status_or_int.value(), Eq(1))
@ -254,11 +253,11 @@ void TestWriting(const char* mode, int tiled, int height) {
uint8_t expected_c = static_cast<uint8_t>(i); uint8_t expected_c = static_cast<uint8_t>(i);
tilebuffer.fill(0); tilebuffer.fill(0);
sapi::v::Array<uint8_t> tilebuffer_(tilebuffer.data(), sapi::v::Array<uint8_t> tilebuffer_sapi(tilebuffer.data(),
tilebuffer.size()); tilebuffer.size());
status_or_long = api.TIFFReadEncodedTile( status_or_long = api.TIFFReadEncodedTile(
&tif2, i, tilebuffer_.PtrBoth(), kTileBufferSize); &tif2, i, tilebuffer_sapi.PtrBoth(), kTileBufferSize);
ASSERT_THAT(status_or_long, IsOk()) ASSERT_THAT(status_or_long, IsOk())
<< "TIFFReadEncodedTile fatal error"; << "TIFFReadEncodedTile fatal error";
EXPECT_THAT(status_or_long.value(), Eq(kTileBufferSize)) EXPECT_THAT(status_or_long.value(), Eq(kTileBufferSize))

View File

@ -50,24 +50,20 @@ TEST(SandboxTest, LongTag) {
TiffSapiSandbox sandbox(srcfile); TiffSapiSandbox sandbox(srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), buffer.size());
absl::StatusOr<int> status_or_int;
absl::StatusOr<TIFF*> status_or_tif;
TiffApi api(&sandbox); TiffApi api(&sandbox);
sapi::v::ConstCStr srcfile_var(srcfile.c_str()); sapi::v::ConstCStr srcfile_var(srcfile.c_str());
sapi::v::ConstCStr w_var("w"); sapi::v::ConstCStr w_var("w");
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), w_var.PtrBefore()); absl::StatusOr<TIFF*> status_or_tif =
api.TIFFOpen(srcfile_var.PtrBefore(), w_var.PtrBefore());
ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile; ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile;
sapi::v::RemotePtr tif(status_or_tif.value()); sapi::v::RemotePtr tif(status_or_tif.value());
ASSERT_THAT(tif.GetValue(), NotNull()) ASSERT_THAT(tif.GetValue(), NotNull())
<< "Can't create test TIFF file " << srcfile; << "Can't create test TIFF file " << srcfile;
status_or_int = api.TIFFSetFieldU1(&tif, TIFFTAG_IMAGEWIDTH, kWidth); absl::StatusOr<int> status_or_int =
api.TIFFSetFieldU1(&tif, TIFFTAG_IMAGEWIDTH, kWidth);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set ImageWidth tag"; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set ImageWidth tag";
@ -107,7 +103,10 @@ TEST(SandboxTest, LongTag) {
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag;
} }
status_or_int = api.TIFFWriteScanline(&tif, buffer_.PtrBoth(), 0, 0); std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_sapi(buffer.data(), buffer.size());
status_or_int = api.TIFFWriteScanline(&tif, buffer_sapi.PtrBefore(), 0, 0);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteScanline fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteScanline fatal error";
ASSERT_THAT(status_or_int.value(), Ne(-1)) << "Can't write image data"; ASSERT_THAT(status_or_int.value(), Ne(-1)) << "Can't write image data";

View File

@ -15,15 +15,21 @@
#include <array> #include <array>
#include <cstring> #include <cstring>
#include "absl/algorithm/container.h"
#include "absl/strings/str_join.h"
#include "helper.h" // NOLINT(build/include) #include "helper.h" // NOLINT(build/include)
#include "tiffio.h" // NOLINT(build/include) #include "tiffio.h" // NOLINT(build/include)
namespace { namespace {
using ::sapi::IsOk; using ::sapi::IsOk;
using ::testing::ContainerEq;
using ::testing::Eq; using ::testing::Eq;
using ::testing::Ge;
using ::testing::Gt;
using ::testing::IsFalse; using ::testing::IsFalse;
using ::testing::IsTrue; using ::testing::IsTrue;
using ::testing::Le;
using ::testing::NotNull; using ::testing::NotNull;
struct ChannelLimits { struct ChannelLimits {
@ -55,141 +61,98 @@ constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
{64, {0, 0, 0, 0, 0, 2, 255, 255}}, {64, {0, 0, 0, 0, 0, 2, 255, 255}},
{512, {5, 6, 34, 36, 182, 196, 255, 255}}}}; {512, {5, 6, 34, 36, 182, 196, 255, 255}}}};
bool CheckCluster(uint32_t cluster, const sapi::v::Array<uint8_t>& buffer, void CheckCluster(uint32_t cluster, const sapi::v::Array<uint8_t>& buffer,
const ClusterData& expected_cluster) { const ClusterData& expected_cluster) {
bool is_overrun = (buffer.GetSize() <= cluster * kClusterSize); ASSERT_THAT(buffer.GetSize(), Ge((cluster + 1) * kClusterSize)) << "Overrun";
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
if (is_overrun) {
return true;
}
auto* target = buffer.GetData() + cluster * kClusterSize;
bool comp =
!(std::memcmp(target, expected_cluster.data(), kClusterSize) == 0);
// the image is split on 6-bit clusters because it has YCbCr color format // the image is split on 6-bit clusters because it has YCbCr color format
EXPECT_THAT(comp, IsFalse()) EXPECT_THAT(
<< "Cluster " << cluster << " did not match expected results.\n" absl::MakeSpan(buffer.GetData() + cluster * kClusterSize, kClusterSize),
<< "Expect: " << expected_cluster[0] << "\t" << expected_cluster[1] ContainerEq(absl::MakeSpan(expected_cluster)))
<< "\t" << expected_cluster[2] << "\t" << expected_cluster[3] << "\t" << "Cluster " << cluster << " did not match expected results.";
<< expected_cluster[4] << "\t" << expected_cluster[5] << "\n"
<< "Got: " << target[0] << "\t" << target[1] << "\t" << target[2] << "\t"
<< target[3] << "\t" << target[4] << "\t" << target[5];
return comp;
} }
bool CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits, void CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits,
const sapi::v::Array<uint8_t>& buffer) { const sapi::v::Array<uint8_t>& buffer) {
bool is_overrun = (buffer.GetSize() <= pixel * kChannelsInPixel); ASSERT_THAT(buffer.GetSize(), Ge((pixel + 1) * kChannelsInPixel))
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun"; << "Overrun";
if (is_overrun) { uint8_t* rgb = buffer.GetData() + pixel * kChannelsInPixel;
return true; EXPECT_THAT(rgb[0], Ge(limits.min_red));
} EXPECT_THAT(rgb[0], Le(limits.max_red));
EXPECT_THAT(rgb[1], Ge(limits.min_green));
auto* rgb = buffer.GetData() + pixel * kChannelsInPixel; EXPECT_THAT(rgb[1], Le(limits.max_green));
bool comp = !(rgb[0] >= limits.min_red && rgb[0] <= limits.max_red && EXPECT_THAT(rgb[2], Ge(limits.min_blue));
rgb[1] >= limits.min_green && rgb[1] <= limits.max_green && EXPECT_THAT(rgb[2], Le(limits.max_blue));
rgb[2] >= limits.min_blue && rgb[2] <= limits.max_blue);
EXPECT_THAT(comp, IsFalse())
<< "Pixel " << pixel << " did not match expected results.\n"
<< "Got R=" << rgb[0] << " (expected " << limits.min_red
<< "..=" << limits.max_red << "), G=" << rgb[1] << " (expected "
<< limits.min_green << "..=" << limits.max_green << "), B=" << rgb[2]
<< " (expected " << limits.min_blue << "..=" << limits.max_blue << ")";
return comp;
} }
bool CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits, void CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
const sapi::v::Array<uint32_t>& buffer) { const sapi::v::Array<uint32_t>& buffer) {
// RGBA images are upside down - adjust for normal ordering // RGBA images are upside down - adjust for normal ordering
uint32_t adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128; uint32_t adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
bool is_overrun = (buffer.GetSize() <= adjusted_pixel); ASSERT_THAT(buffer.GetSize(), Gt(adjusted_pixel)) << "Overrun";
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
if (is_overrun) { uint32_t rgba = buffer[adjusted_pixel];
return true; EXPECT_THAT(TIFFGetR(rgba), Ge(limits.min_red));
} EXPECT_THAT(TIFFGetR(rgba), Le(limits.max_red));
EXPECT_THAT(TIFFGetG(rgba), Ge(limits.min_green));
auto rgba = buffer[adjusted_pixel]; EXPECT_THAT(TIFFGetG(rgba), Le(limits.max_green));
bool comp = !(TIFFGetR(rgba) >= static_cast<unsigned>(limits.min_red) && EXPECT_THAT(TIFFGetB(rgba), Ge(limits.min_blue));
TIFFGetR(rgba) <= static_cast<unsigned>(limits.max_red) && EXPECT_THAT(TIFFGetB(rgba), Le(limits.max_blue));
TIFFGetG(rgba) >= static_cast<unsigned>(limits.min_green) && EXPECT_THAT(TIFFGetA(rgba), Ge(limits.min_alpha));
TIFFGetG(rgba) <= static_cast<unsigned>(limits.max_green) && EXPECT_THAT(TIFFGetA(rgba), Le(limits.max_alpha));
TIFFGetB(rgba) >= static_cast<unsigned>(limits.min_blue) &&
TIFFGetB(rgba) <= static_cast<unsigned>(limits.max_blue) &&
TIFFGetA(rgba) >= static_cast<unsigned>(limits.min_alpha) &&
TIFFGetA(rgba) <= static_cast<unsigned>(limits.max_alpha));
EXPECT_THAT(comp, IsFalse())
<< "Pixel " << pixel << " did not match expected results.\n"
<< "Got R=" << TIFFGetR(rgba) << " (expected " << limits.min_red
<< "..=" << limits.max_red << "), G=" << TIFFGetG(rgba) << " (expected "
<< limits.min_green << "..=" << limits.max_green
<< "), B=" << TIFFGetB(rgba) << " (expected " << limits.min_blue
<< "..=" << limits.max_blue << "), A=" << TIFFGetA(rgba) << " (expected "
<< limits.min_alpha << "..=" << limits.max_alpha << ")";
return comp;
} }
TEST(SandboxTest, RawDecode) { TEST(SandboxTest, RawDecode) {
tsize_t sz;
bool pixel_status_ok = false;
bool cluster_status_ok = false;
std::string srcfile = GetFilePath("quad-tile.jpg.tiff"); std::string srcfile = GetFilePath("quad-tile.jpg.tiff");
TiffSapiSandbox sandbox(srcfile); TiffSapiSandbox sandbox(srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
sapi::v::UShort h;
sapi::v::UShort v;
absl::StatusOr<TIFF*> status_or_tif;
absl::StatusOr<int> status_or_int;
absl::StatusOr<tmsize_t> status_or_long;
TiffApi api(&sandbox); TiffApi api(&sandbox);
sapi::v::ConstCStr srcfile_var(srcfile.c_str()); sapi::v::ConstCStr srcfile_var(srcfile.c_str());
sapi::v::ConstCStr r_var("r"); sapi::v::ConstCStr r_var("r");
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()); absl::StatusOr<TIFF*> status_or_tif =
api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore());
ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile; ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile;
sapi::v::RemotePtr tif(status_or_tif.value()); sapi::v::RemotePtr tif(status_or_tif.value());
ASSERT_THAT(tif.GetValue(), NotNull()) ASSERT_THAT(tif.GetValue(), NotNull())
<< "Could not open " << srcfile << ", TIFFOpen return NULL"; << "Could not open " << srcfile << ", TIFFOpen return NULL";
status_or_int = api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(), sapi::v::UShort h;
v.PtrBoth()); sapi::v::UShort v;
absl::StatusOr<int> status_or_int = api.TIFFGetField2(
&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrAfter(), v.PtrAfter());
ASSERT_THAT(status_or_int, IsOk()) << "TIFFGetField2 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFGetField2 fatal error";
EXPECT_THAT( EXPECT_THAT(
status_or_int.value() == 0 || h.GetValue() != 2 || v.GetValue() != 2, status_or_int.value() == 0 || h.GetValue() != 2 || v.GetValue() != 2,
IsFalse()) IsFalse())
<< "Could not retrieve subsampling tag"; << "Could not retrieve subsampling tag";
status_or_long = api.TIFFTileSize(&tif); tsize_t sz;
absl::StatusOr<tmsize_t> status_or_long = api.TIFFTileSize(&tif);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFTileSize fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFTileSize fatal error";
EXPECT_THAT(status_or_long.value(), Eq(kClusterImageSize * kClusterSize)) EXPECT_THAT(status_or_long.value(), Eq(kClusterImageSize * kClusterSize))
<< "Unexpected TileSize " << status_or_long.value() << ". Expected " << "Unexpected TileSize " << status_or_long.value() << ". Expected "
<< kClusterImageSize * kClusterSize << " bytes\n"; << kClusterImageSize * kClusterSize << " bytes";
sz = status_or_long.value(); sz = status_or_long.value();
sapi::v::Array<uint8_t> buffer_(sz); sapi::v::Array<uint8_t> buffer(sz);
// Read a tile in decompressed form, but still YCbCr subsampled // Read a tile in decompressed form, but still YCbCr subsampled
status_or_long = status_or_long =
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer_.PtrBoth(), sz); api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer.PtrAfter(), sz);
ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error"; ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
EXPECT_THAT(status_or_long.value(), Eq(sz)) EXPECT_THAT(status_or_long.value(), Eq(sz))
<< "Did not get expected result code from TIFFReadEncodedTile()(" << "Did not get expected result code from TIFFReadEncodedTile()("
<< (int)status_or_long.value() << " instead of " << (int)sz << ")"; << static_cast<int>(status_or_long.value()) << " instead of "
<< static_cast<int>(sz) << ")";
for (const auto& [id, data] : kClusters) { for (const auto& [id, data] : kClusters) {
cluster_status_ok |= CheckCluster(id, buffer_, data); CheckCluster(id, buffer, data);
} }
ASSERT_FALSE(cluster_status_ok) << "Clusters did not match expected results";
status_or_int = status_or_int =
api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB); api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
@ -201,19 +164,19 @@ TEST(SandboxTest, RawDecode) {
ASSERT_THAT(status_or_long, IsOk()) << "TIFFTileSize fatal error"; ASSERT_THAT(status_or_long, IsOk()) << "TIFFTileSize fatal error";
EXPECT_THAT(status_or_long.value(), Eq(kImageSize * kChannelsInPixel)) EXPECT_THAT(status_or_long.value(), Eq(kImageSize * kChannelsInPixel))
<< "Unexpected TileSize " << status_or_long.value() << ". Expected " << "Unexpected TileSize " << status_or_long.value() << ". Expected "
<< kImageSize * kChannelsInPixel << " bytes\n"; << kImageSize * kChannelsInPixel << " bytes";
sz = status_or_long.value(); sz = status_or_long.value();
sapi::v::Array<uint8_t> buffer2_(sz); sapi::v::Array<uint8_t> buffer2(sz);
status_or_long = status_or_long =
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2_.PtrBoth(), sz); api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2.PtrAfter(), sz);
ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error"; ASSERT_THAT(status_or_long, IsOk()) << "TIFFReadEncodedTile fatal error";
EXPECT_THAT(status_or_long.value(), Eq(sz)) EXPECT_THAT(status_or_long.value(), Eq(sz))
<< "Did not get expected result code from TIFFReadEncodedTile()(" << "Did not get expected result code from TIFFReadEncodedTile()("
<< status_or_long.value() << " instead of " << sz; << status_or_long.value() << " instead of " << sz;
for (const auto& [id, data] : kLimits) { for (const auto& [id, data] : kLimits) {
pixel_status_ok |= CheckRgbPixel(id, data, buffer2_); CheckRgbPixel(id, data, buffer2);
} }
ASSERT_THAT(api.TIFFClose(&tif), IsOk()) << "TIFFClose fatal error"; ASSERT_THAT(api.TIFFClose(&tif), IsOk()) << "TIFFClose fatal error";
@ -225,20 +188,18 @@ TEST(SandboxTest, RawDecode) {
ASSERT_THAT(tif2.GetValue(), NotNull()) ASSERT_THAT(tif2.GetValue(), NotNull())
<< "Could not open " << srcfile << ", TIFFOpen return NULL"; << "Could not open " << srcfile << ", TIFFOpen return NULL";
sapi::v::Array<uint32_t> rgba_buffer_(kImageSize); sapi::v::Array<uint32_t> rgba_buffer(kImageSize);
status_or_int = status_or_int =
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth()); api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer.PtrAfter());
ASSERT_THAT(status_or_int, IsOk()) << "TIFFReadRGBATile fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFReadRGBATile fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) EXPECT_THAT(status_or_int.value(), IsTrue())
<< "TIFFReadRGBATile() returned failure code"; << "TIFFReadRGBATile() returned failure code";
for (const auto& [id, data] : kLimits) { for (const auto& [id, data] : kLimits) {
pixel_status_ok |= CheckRgbaPixel(id, data, rgba_buffer_); CheckRgbaPixel(id, data, rgba_buffer);
} }
EXPECT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error"; EXPECT_THAT(api.TIFFClose(&tif2), IsOk()) << "TIFFClose fatal error";
EXPECT_THAT(pixel_status_ok, IsFalse()) << "wrong encoding";
} }
} // namespace } // namespace

View File

@ -71,24 +71,20 @@ TEST(SandboxTest, ShortTag) {
TiffSapiSandbox sandbox(srcfile); TiffSapiSandbox sandbox(srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), buffer.size());
absl::StatusOr<int> status_or_int;
absl::StatusOr<TIFF*> status_or_tif;
TiffApi api(&sandbox); TiffApi api(&sandbox);
sapi::v::ConstCStr srcfile_var(srcfile.c_str()); sapi::v::ConstCStr srcfile_var(srcfile.c_str());
sapi::v::ConstCStr w_var("w"); sapi::v::ConstCStr w_var("w");
status_or_tif = api.TIFFOpen(srcfile_var.PtrBefore(), w_var.PtrBefore()); absl::StatusOr<TIFF*> status_or_tif =
api.TIFFOpen(srcfile_var.PtrBefore(), w_var.PtrBefore());
ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile; ASSERT_THAT(status_or_tif, IsOk()) << "Could not open " << srcfile;
sapi::v::RemotePtr tif(status_or_tif.value()); sapi::v::RemotePtr tif(status_or_tif.value());
ASSERT_THAT(tif.GetValue(), NotNull()) ASSERT_THAT(tif.GetValue(), NotNull())
<< "Can't create test TIFF file " << srcfile; << "Can't create test TIFF file " << srcfile;
status_or_int = api.TIFFSetFieldUShort1(&tif, TIFFTAG_IMAGEWIDTH, kWidth); absl::StatusOr<int> status_or_int =
api.TIFFSetFieldUShort1(&tif, TIFFTAG_IMAGEWIDTH, kWidth);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set ImagekWidth tag"; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set ImagekWidth tag";
@ -136,7 +132,10 @@ TEST(SandboxTest, ShortTag) {
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag; EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set tag " << tag.tag;
} }
status_or_int = api.TIFFWriteScanline(&tif, buffer_.PtrBoth(), 0, 0); std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_sapi(buffer.data(), buffer.size());
status_or_int = api.TIFFWriteScanline(&tif, buffer_sapi.PtrBefore(), 0, 0);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteScanline fatal error"; ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteScanline fatal error";
ASSERT_THAT(status_or_int.value(), Ne(-1)) << "Can't write image data"; ASSERT_THAT(status_or_int.value(), Ne(-1)) << "Can't write image data";