mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
using absl
This commit is contained in:
parent
1350cfb3b6
commit
418bc3f356
|
@ -18,6 +18,8 @@
|
|||
#include <cstring>
|
||||
|
||||
#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/path.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 =
|
||||
"Cluster %d did not match expected results.\n"
|
||||
"Expect:\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n"
|
||||
"Got:\t%3d\t%3d\t%3d\t%3d\t%3d\t%3d\n";
|
||||
"Expect:\t%s\n"
|
||||
"Got:\t%s";
|
||||
|
||||
constexpr absl::string_view kRgbPixelErrorFormatStr =
|
||||
"Pixel %d did not match expected results.\n"
|
||||
"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 =
|
||||
"Pixel %d did not match expected results.\n"
|
||||
"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,
|
||||
const sapi::v::Array<uint8_t>& buffer,
|
||||
const ClusterData& expected_cluster) {
|
||||
if (buffer.GetSize() <= cluster * kClusterSize) {
|
||||
return absl::InternalError("Buffer overrun\n");
|
||||
if (buffer.GetSize() < (cluster + 1) * kClusterSize) {
|
||||
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();
|
||||
}
|
||||
|
||||
// the image is split on 6-bit clusters because it has YCbCr color format
|
||||
return absl::InternalError(absl::StrFormat(
|
||||
kClusterErrorFormatStr, cluster, expected_cluster[0], expected_cluster[1],
|
||||
expected_cluster[2], expected_cluster[3], expected_cluster[4],
|
||||
expected_cluster[5], target[0], target[1], target[2], target[3],
|
||||
target[4], target[5]));
|
||||
kClusterErrorFormatStr, cluster, absl::StrJoin(expected_cluster, "\t"),
|
||||
absl::StrJoin(target, "\t")));
|
||||
}
|
||||
|
||||
absl::Status CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits,
|
||||
const sapi::v::Array<uint8_t>& buffer) {
|
||||
if (buffer.GetSize() <= pixel * kChannelsInPixel) {
|
||||
return absl::InternalError("Buffer overrun\n");
|
||||
if (buffer.GetSize() < (pixel + 1) * kChannelsInPixel) {
|
||||
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 &&
|
||||
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;
|
||||
|
||||
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) &&
|
||||
TIFFGetR(rgba) <= static_cast<unsigned>(limits.max_red) &&
|
||||
TIFFGetG(rgba) >= static_cast<unsigned>(limits.min_green) &&
|
||||
|
@ -138,11 +141,12 @@ absl::Status CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
|
|||
|
||||
} // 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);
|
||||
}
|
||||
|
||||
std::string GetFilePath(const std::string filename) {
|
||||
std::string GetFilePath(const absl::string_view filename) {
|
||||
std::string cwd = sandbox2::file_util::fileops::GetCWD();
|
||||
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);
|
||||
}
|
||||
|
||||
absl::Status LibTIFFMain(const std::string& srcfile) {
|
||||
absl::Status LibTIFFMain(const absl::string_view srcfile) {
|
||||
// to use dir and file inside sapi-libtiff, use
|
||||
// sandbox(file) – file only -- or
|
||||
// sandbox(file, dir) -- file and dir -- or
|
||||
|
@ -171,24 +175,15 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
|
|||
|
||||
TiffSapiSandbox sandbox(srcfile);
|
||||
|
||||
bool pixel_status_ok = true;
|
||||
bool cluster_status_ok = true;
|
||||
// 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());
|
||||
|
||||
TiffApi api(&sandbox);
|
||||
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
|
||||
sapi::v::ConstCStr srcfile_var(srcfile.data());
|
||||
sapi::v::ConstCStr r_var("r");
|
||||
|
||||
absl::StatusOr<TIFF*> status_or_tif;
|
||||
SAPI_ASSIGN_OR_RETURN(
|
||||
status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()));
|
||||
|
||||
|
@ -197,7 +192,9 @@ absl::Status LibTIFFMain(const std::string& 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,
|
||||
h.PtrBoth(), v.PtrBoth()));
|
||||
if (return_value == 0 || h.GetValue() != 2 || v.GetValue() != 2) {
|
||||
|
@ -208,23 +205,25 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
|
|||
if (sz != kClusterSize * kClusterImageSize) {
|
||||
return absl::InternalError(
|
||||
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
|
||||
SAPI_ASSIGN_OR_RETURN(
|
||||
tsize_t new_sz,
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer_.PtrBoth(), sz));
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer.PtrBoth(), sz));
|
||||
if (new_sz != sz) {
|
||||
return absl::InternalError(absl::StrCat(
|
||||
"Did not get expected result code from TIFFReadEncodedTile(): ",
|
||||
status_or_long.value(), " instead of ", sz));
|
||||
"Did not get expected result code from TIFFReadEncodedTile(): ", new_sz,
|
||||
" instead of ", sz));
|
||||
}
|
||||
|
||||
absl::Status status;
|
||||
bool cluster_status_ok = true;
|
||||
for (const auto& [id, data] : kClusters) {
|
||||
if (status = CheckCluster(id, buffer_, data); !status.ok()) {
|
||||
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString();
|
||||
if (status = CheckCluster(id, buffer, data); !status.ok()) {
|
||||
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString() << '\n';
|
||||
}
|
||||
cluster_status_ok &= status.ok();
|
||||
}
|
||||
|
@ -244,11 +243,10 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
|
|||
if (sz != kChannelsInPixel * kImageSize) {
|
||||
return absl::InternalError(
|
||||
absl::StrCat("Unexpected TileSize ", sz, ". Expected ",
|
||||
kChannelsInPixel * kImageSize, " bytes\n"));
|
||||
kChannelsInPixel * kImageSize, " bytes"));
|
||||
}
|
||||
|
||||
sapi::v::Array<uint8_t> buffer2_(sz);
|
||||
|
||||
SAPI_ASSIGN_OR_RETURN(
|
||||
new_sz,
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2_.PtrBoth(), sz));
|
||||
|
@ -258,9 +256,10 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
|
|||
" instead of ", sz));
|
||||
}
|
||||
|
||||
bool pixel_status_ok = true;
|
||||
for (const auto& [id, data] : kLimits) {
|
||||
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();
|
||||
}
|
||||
|
@ -275,20 +274,20 @@ absl::Status LibTIFFMain(const std::string& 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
|
||||
SAPI_ASSIGN_OR_RETURN(
|
||||
return_value,
|
||||
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth()));
|
||||
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer.PtrBoth()));
|
||||
if (return_value == 0) {
|
||||
return absl::InternalError("TIFFReadRGBATile() returned failure code");
|
||||
}
|
||||
|
||||
// Checking specific pixels from the test data, 0th, 64th and 512th
|
||||
for (const auto& [id, data] : kLimits) {
|
||||
if (status = CheckRgbaPixel(id, data, rgba_buffer_); !status.ok()) {
|
||||
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString();
|
||||
if (status = CheckRgbaPixel(id, data, rgba_buffer); !status.ok()) {
|
||||
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString() << '\n';
|
||||
}
|
||||
pixel_status_ok &= status.ok();
|
||||
}
|
||||
|
@ -314,7 +313,7 @@ int main(int argc, char** argv) {
|
|||
srcfile = GetFilePath(argv[1], srcfilerel);
|
||||
}
|
||||
|
||||
auto status = LibTIFFMain(srcfile);
|
||||
absl::Status status = LibTIFFMain(srcfile);
|
||||
if (!status.ok()) {
|
||||
LOG(ERROR) << "LibTIFFMain failed with error:\n"
|
||||
<< status.ToString() << '\n';
|
||||
|
|
|
@ -24,8 +24,8 @@ namespace {
|
|||
|
||||
class TiffSapiSandbox : public TiffSandbox {
|
||||
public:
|
||||
TiffSapiSandbox(std::optional<std::string> file = std::nullopt,
|
||||
std::optional<std::string> dir = std::nullopt)
|
||||
TiffSapiSandbox(std::optional<absl::string_view> file = std::nullopt,
|
||||
std::optional<absl::string_view> dir = std::nullopt)
|
||||
: file_(std::move(file)), dir_(std::move(dir)) {}
|
||||
|
||||
private:
|
||||
|
@ -61,8 +61,8 @@ class TiffSapiSandbox : public TiffSandbox {
|
|||
return builder.get()->BuildOrDie();
|
||||
}
|
||||
|
||||
std::optional<std::string> file_;
|
||||
std::optional<std::string> dir_;
|
||||
std::optional<absl::string_view> file_;
|
||||
std::optional<absl::string_view> dir_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -37,10 +37,7 @@ void TestWriting(const char* mode, int tiled, int height) {
|
|||
|
||||
std::string srcfile = sandbox2::file::JoinPath(
|
||||
sandbox2::file_util::fileops::GetCWD(), status_or_path.value());
|
||||
|
||||
absl::StatusOr<int> status_or_int;
|
||||
absl::StatusOr<signed long> status_or_long;
|
||||
absl::StatusOr<TIFF*> status_or_tif;
|
||||
|
||||
TiffSapiSandbox sandbox(srcfile);
|
||||
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 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;
|
||||
|
||||
sapi::v::RemotePtr tif(status_or_tif.value());
|
||||
ASSERT_THAT(tif.GetValue(), NotNull())
|
||||
<< "Can't create test TIFF file " << srcfile;
|
||||
|
||||
status_or_int =
|
||||
absl::StatusOr<int> status_or_int =
|
||||
api.TIFFSetFieldU1(&tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
|
||||
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
|
||||
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) {
|
||||
std::array<uint8_t, kTileBufferSize> tilebuffer;
|
||||
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(),
|
||||
kTileBufferSize);
|
||||
status_or_int = api.TIFFWriteEncodedTile(
|
||||
&tif, i, tilebuffer_sapi.PtrBoth(), kTileBufferSize);
|
||||
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteEncodedTile fatal error";
|
||||
EXPECT_THAT(status_or_int.value(), Eq(kTileBufferSize))
|
||||
<< "line " << i << ": expected " << kTileBufferSize << ", got "
|
||||
|
@ -254,11 +253,11 @@ void TestWriting(const char* mode, int tiled, int height) {
|
|||
uint8_t expected_c = static_cast<uint8_t>(i);
|
||||
tilebuffer.fill(0);
|
||||
|
||||
sapi::v::Array<uint8_t> tilebuffer_(tilebuffer.data(),
|
||||
tilebuffer.size());
|
||||
sapi::v::Array<uint8_t> tilebuffer_sapi(tilebuffer.data(),
|
||||
tilebuffer.size());
|
||||
|
||||
status_or_long = api.TIFFReadEncodedTile(
|
||||
&tif2, i, tilebuffer_.PtrBoth(), kTileBufferSize);
|
||||
&tif2, i, tilebuffer_sapi.PtrBoth(), kTileBufferSize);
|
||||
ASSERT_THAT(status_or_long, IsOk())
|
||||
<< "TIFFReadEncodedTile fatal error";
|
||||
EXPECT_THAT(status_or_long.value(), Eq(kTileBufferSize))
|
||||
|
|
|
@ -50,24 +50,20 @@ TEST(SandboxTest, LongTag) {
|
|||
TiffSapiSandbox sandbox(srcfile);
|
||||
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);
|
||||
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
|
||||
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;
|
||||
|
||||
sapi::v::RemotePtr tif(status_or_tif.value());
|
||||
ASSERT_THAT(tif.GetValue(), NotNull())
|
||||
<< "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";
|
||||
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;
|
||||
}
|
||||
|
||||
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.PtrBoth(), 0, 0);
|
||||
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteScanline fatal error";
|
||||
ASSERT_THAT(status_or_int.value(), Ne(-1)) << "Can't write image data";
|
||||
|
||||
|
|
|
@ -15,15 +15,21 @@
|
|||
#include <array>
|
||||
#include <cstring>
|
||||
|
||||
#include "absl/algorithm/container.h"
|
||||
#include "absl/strings/str_join.h"
|
||||
#include "helper.h" // NOLINT(build/include)
|
||||
#include "tiffio.h" // NOLINT(build/include)
|
||||
|
||||
namespace {
|
||||
|
||||
using ::sapi::IsOk;
|
||||
using ::testing::ContainerEq;
|
||||
using ::testing::Eq;
|
||||
using ::testing::Ge;
|
||||
using ::testing::Gt;
|
||||
using ::testing::IsFalse;
|
||||
using ::testing::IsTrue;
|
||||
using ::testing::Le;
|
||||
using ::testing::NotNull;
|
||||
|
||||
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}},
|
||||
{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) {
|
||||
bool is_overrun = (buffer.GetSize() <= cluster * kClusterSize);
|
||||
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);
|
||||
ASSERT_THAT(buffer.GetSize(), Ge((cluster + 1) * kClusterSize)) << "Overrun";
|
||||
|
||||
// the image is split on 6-bit clusters because it has YCbCr color format
|
||||
EXPECT_THAT(comp, IsFalse())
|
||||
<< "Cluster " << cluster << " did not match expected results.\n"
|
||||
<< "Expect: " << expected_cluster[0] << "\t" << expected_cluster[1]
|
||||
<< "\t" << expected_cluster[2] << "\t" << expected_cluster[3] << "\t"
|
||||
<< 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;
|
||||
EXPECT_THAT(
|
||||
absl::MakeSpan(buffer.GetData() + cluster * kClusterSize, kClusterSize),
|
||||
ContainerEq(absl::MakeSpan(expected_cluster)))
|
||||
<< "Cluster " << cluster << " did not match expected results.";
|
||||
}
|
||||
|
||||
bool CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits,
|
||||
void CheckRgbPixel(uint32_t pixel, const ChannelLimits& limits,
|
||||
const sapi::v::Array<uint8_t>& buffer) {
|
||||
bool is_overrun = (buffer.GetSize() <= pixel * kChannelsInPixel);
|
||||
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
|
||||
ASSERT_THAT(buffer.GetSize(), Ge((pixel + 1) * kChannelsInPixel))
|
||||
<< "Overrun";
|
||||
|
||||
if (is_overrun) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto* rgb = buffer.GetData() + pixel * kChannelsInPixel;
|
||||
bool comp = !(rgb[0] >= limits.min_red && rgb[0] <= limits.max_red &&
|
||||
rgb[1] >= limits.min_green && rgb[1] <= limits.max_green &&
|
||||
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;
|
||||
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));
|
||||
}
|
||||
|
||||
bool CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
|
||||
void CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
|
||||
const sapi::v::Array<uint32_t>& buffer) {
|
||||
// RGBA images are upside down - adjust for normal ordering
|
||||
uint32_t adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
|
||||
|
||||
bool is_overrun = (buffer.GetSize() <= adjusted_pixel);
|
||||
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
|
||||
ASSERT_THAT(buffer.GetSize(), Gt(adjusted_pixel)) << "Overrun";
|
||||
|
||||
if (is_overrun) {
|
||||
return true;
|
||||
}
|
||||
|
||||
auto rgba = buffer[adjusted_pixel];
|
||||
bool comp = !(TIFFGetR(rgba) >= static_cast<unsigned>(limits.min_red) &&
|
||||
TIFFGetR(rgba) <= static_cast<unsigned>(limits.max_red) &&
|
||||
TIFFGetG(rgba) >= static_cast<unsigned>(limits.min_green) &&
|
||||
TIFFGetG(rgba) <= static_cast<unsigned>(limits.max_green) &&
|
||||
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;
|
||||
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));
|
||||
}
|
||||
|
||||
TEST(SandboxTest, RawDecode) {
|
||||
tsize_t sz;
|
||||
bool pixel_status_ok = false;
|
||||
bool cluster_status_ok = false;
|
||||
std::string srcfile = GetFilePath("quad-tile.jpg.tiff");
|
||||
|
||||
TiffSapiSandbox sandbox(srcfile);
|
||||
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);
|
||||
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
|
||||
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;
|
||||
|
||||
sapi::v::RemotePtr tif(status_or_tif.value());
|
||||
ASSERT_THAT(tif.GetValue(), NotNull())
|
||||
<< "Could not open " << srcfile << ", TIFFOpen return NULL";
|
||||
|
||||
status_or_int = api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(),
|
||||
v.PtrBoth());
|
||||
sapi::v::UShort h;
|
||||
sapi::v::UShort v;
|
||||
absl::StatusOr<int> status_or_int = api.TIFFGetField2(
|
||||
&tif, TIFFTAG_YCBCRSUBSAMPLING, h.PtrBoth(), v.PtrBoth());
|
||||
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";
|
||||
|
||||
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";
|
||||
EXPECT_THAT(status_or_long.value(), Eq(kClusterImageSize * kClusterSize))
|
||||
<< "Unexpected TileSize " << status_or_long.value() << ". Expected "
|
||||
<< kClusterImageSize * kClusterSize << " bytes\n";
|
||||
<< kClusterImageSize * kClusterSize << " bytes";
|
||||
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
|
||||
status_or_long =
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer_.PtrBoth(), sz);
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer.PtrBoth(), sz);
|
||||
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()("
|
||||
<< (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) {
|
||||
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 =
|
||||
api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
|
||||
|
@ -201,19 +164,19 @@ TEST(SandboxTest, RawDecode) {
|
|||
ASSERT_THAT(status_or_long, IsOk()) << "TIFFTileSize fatal error";
|
||||
EXPECT_THAT(status_or_long.value(), Eq(kImageSize * kChannelsInPixel))
|
||||
<< "Unexpected TileSize " << status_or_long.value() << ". Expected "
|
||||
<< kImageSize * kChannelsInPixel << " bytes\n";
|
||||
<< kImageSize * kChannelsInPixel << " bytes";
|
||||
sz = status_or_long.value();
|
||||
|
||||
sapi::v::Array<uint8_t> buffer2_(sz);
|
||||
sapi::v::Array<uint8_t> buffer2(sz);
|
||||
status_or_long =
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2_.PtrBoth(), sz);
|
||||
api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2.PtrBoth(), sz);
|
||||
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;
|
||||
|
||||
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";
|
||||
|
@ -225,20 +188,18 @@ TEST(SandboxTest, RawDecode) {
|
|||
ASSERT_THAT(tif2.GetValue(), NotNull())
|
||||
<< "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 =
|
||||
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth());
|
||||
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer.PtrBoth());
|
||||
ASSERT_THAT(status_or_int, IsOk()) << "TIFFReadRGBATile fatal error";
|
||||
EXPECT_THAT(status_or_int.value(), IsTrue())
|
||||
<< "TIFFReadRGBATile() returned failure code";
|
||||
|
||||
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(pixel_status_ok, IsFalse()) << "wrong encoding";
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
|
|
@ -71,24 +71,20 @@ TEST(SandboxTest, ShortTag) {
|
|||
TiffSapiSandbox sandbox(srcfile);
|
||||
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);
|
||||
sapi::v::ConstCStr srcfile_var(srcfile.c_str());
|
||||
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;
|
||||
|
||||
sapi::v::RemotePtr tif(status_or_tif.value());
|
||||
ASSERT_THAT(tif.GetValue(), NotNull())
|
||||
<< "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";
|
||||
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;
|
||||
}
|
||||
|
||||
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.PtrBoth(), 0, 0);
|
||||
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteScanline fatal error";
|
||||
ASSERT_THAT(status_or_int.value(), Ne(-1)) << "Can't write image data";
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user