This commit is contained in:
Alexandra Latysheva 2020-10-01 16:49:59 +00:00
parent 477de3b6b7
commit 2869fba479
5 changed files with 78 additions and 61 deletions

View File

@ -27,26 +27,37 @@ constexpr std::array<uint8_t, 6> kCluster0 = {0, 0, 2, 0, 138, 139};
constexpr std::array<uint8_t, 6> kCluster64 = {0, 0, 9, 6, 134, 119};
constexpr std::array<uint8_t, 6> kCluster128 = {44, 40, 63, 59, 230, 95};
constexpr unsigned kRawTileNumber = 9;
namespace {
absl::Status CheckCluster(int cluster, const sapi::v::Array<uint8_t>& buffer,
const std::array<uint8_t, 6>& expected_cluster) {
if (buffer.GetSize() <= cluster * 6) {
return absl::InternalError("Buffer overrun\n");
}
uint8_t* target = buffer.GetData() + cluster * 6;
if (!std::memcmp(target, expected_cluster.data(), 6)) {
return absl::OkStatus();
}
// the image is split on 6-bit clusters because it has YCbCr color format
return absl::InternalError(
absl::StrCat("Cluster ", cluster, " did not match expected results.\n",
"Expect: ", expected_cluster[0], "\t", expected_cluster[1],
"\t", expected_cluster[4], "\t", expected_cluster[5], "\t",
expected_cluster[2], "\t", expected_cluster[3], "\n"));
"\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], "\n"));
}
absl::Status 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) {
if (buffer.GetSize() <= pixel * 3) {
return absl::InternalError("Buffer overrun\n");
}
uint8_t* rgb = buffer.GetData() + 3 * pixel;
if (rgb[0] >= min_red && rgb[0] <= max_red && rgb[1] >= min_green &&
@ -56,9 +67,9 @@ absl::Status CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
return absl::InternalError(absl::StrCat(
"Pixel ", pixel, " did not match expected results.\n", "Got R=", rgb[0],
" (expected ", min_red, "..", max_red, "), G=", rgb[1], " (expected ",
min_green, "..", max_green, "), B=", rgb[2], " (expected ", min_blue,
"..", max_blue, ")\n"));
" (expected ", min_red, "..=", max_red, "), G=", rgb[1], " (expected ",
min_green, "..=", max_green, "), B=", rgb[2], " (expected ", min_blue,
"..=", max_blue, ")\n"));
}
absl::Status CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
@ -67,6 +78,10 @@ absl::Status CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
const sapi::v::Array<unsigned>& buffer) {
// RGBA images are upside down - adjust for normal ordering
int adjusted_pixel = pixel % 128 + (127 - (pixel / 128)) * 128;
if (buffer.GetSize() <= adjusted_pixel) {
return absl::InternalError("Buffer overrun\n");
}
uint32 rgba = buffer[adjusted_pixel];
if (TIFFGetR(rgba) >= (uint32)min_red && TIFFGetR(rgba) <= (uint32)max_red &&
@ -81,10 +96,10 @@ absl::Status CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
return absl::InternalError(absl::StrCat(
"Pixel ", pixel, " did not match expected results.\n",
"Got R=", TIFFGetR(rgba), " (expected ", min_red, "..", max_red,
"), G=", TIFFGetG(rgba), " (expected ", min_green, "..", max_green,
"), B=", TIFFGetB(rgba), " (expected ", min_blue, "..", max_blue, "), A=",
TIFFGetA(rgba), " (expected ", min_alpha, "..", max_alpha, ")\n"));
"Got R=", TIFFGetR(rgba), " (expected ", min_red, "..=", max_red,
"), G=", TIFFGetG(rgba), " (expected ", min_green, "..=", max_green,
"), B=", TIFFGetB(rgba), " (expected ", min_blue, "..=", max_blue, "), A=",
TIFFGetA(rgba), " (expected ", min_alpha, "..=", max_alpha, ")\n"));
}
} // namespace
@ -101,7 +116,8 @@ std::string GetFilePath(const std::string filename) {
if (find == std::string::npos) {
LOG(ERROR) << "Something went wrong: CWD don't contain build dir. "
<< "Please run tests from build dir or send project dir as a "
<< "parameter: ./sandboxed /absolute/path/to/project/dir \n";
<< "parameter: ./sandboxed /absolute/path/to/project/dir .\n"
<< "Falling back to using current working directory as root dir.\n";
project_path = cwd;
} else {
project_path = cwd.substr(0, find);
@ -141,30 +157,30 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
return absl::InternalError(absl::StrCat("Could not open ", srcfile));
}
SAPI_ASSIGN_OR_RETURN(status_or_int,
SAPI_ASSIGN_OR_RETURN(auto return_value,
api.TIFFGetField2(&tif, TIFFTAG_YCBCRSUBSAMPLING,
h.PtrBoth(), v.PtrBoth()));
if (status_or_int.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");
}
SAPI_ASSIGN_OR_RETURN(status_or_long, api.TIFFTileSize(&tif));
if (status_or_long.value() != 24576) {
SAPI_ASSIGN_OR_RETURN(tsize_t sz, api.TIFFTileSize(&tif));
if (sz != 24576) {
return absl::InternalError(
absl::StrCat("tiles are ", status_or_long.value(), " bytes\n"));
absl::StrCat("tiles are ", sz, " bytes\n"));
}
tsize_t sz = status_or_long.value();
sapi::v::Array<uint8_t> buffer_(sz);
// Read a tile in decompressed form, but still YCbCr subsampled
SAPI_ASSIGN_OR_RETURN(
status_or_long, api.TIFFReadEncodedTile(&tif, 9, buffer_.PtrBoth(), sz));
if (status_or_long.value() != sz) {
tsize_t new_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));
}
unsigned pixel_status = 1;
bool pixel_status = true;
if (status = CheckCluster(0, buffer_, kCluster0); !status.ok()) {
LOG(ERROR) << "CheckCluster failed:\n" << status.ToString();
}
@ -187,28 +203,28 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
SAPI_ASSIGN_OR_RETURN(
status_or_int,
api.TIFFSetFieldU1(&tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB));
if (!status_or_int.value()) {
if (return_value == 0) {
return absl::InternalError("TIFFSetFieldU1 not available");
}
SAPI_ASSIGN_OR_RETURN(status_or_long, api.TIFFTileSize(&tif));
if (status_or_long.value() != 128 * 128 * 3) {
SAPI_ASSIGN_OR_RETURN(sz, api.TIFFTileSize(&tif));
if (sz != 128 * 128 * 3) {
return absl::InternalError(
absl::StrCat("tiles are ", status_or_long.value(), " bytes"));
absl::StrCat("tiles are ", sz, " bytes"));
}
sz = status_or_long.value();
sapi::v::Array<uint8_t> buffer2_(sz);
SAPI_ASSIGN_OR_RETURN(
status_or_long, api.TIFFReadEncodedTile(&tif, 9, buffer2_.PtrBoth(), sz));
if (status_or_long.value() != sz) {
new_sz, api.TIFFReadEncodedTile(&tif, kRawTileNumber, buffer2_.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));
new_sz, " instead of ", sz));
}
pixel_status = 1;
pixel_status = true;
// Checking specific pixels from the test data, 0th, 64th and 512th
if (status = CheckRgbPixel(0, 15, 18, 0, 0, 18, 41, buffer2_); !status.ok()) {
LOG(ERROR) << "CheckRgbPixel failed:\n" << status.ToString();
}
@ -231,19 +247,21 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
status_or_tif, api.TIFFOpen(srcfile_var.PtrBefore(), r_var.PtrBefore()));
sapi::v::RemotePtr tif2(status_or_tif.value());
if (!tif2.GetValue()) { // tif is NULL
if (!tif2.GetValue()) {
return absl::InternalError(absl::StrCat("Could not reopen ", srcfile));
}
sapi::v::Array<uint32> rgba_buffer_(128 * 128);
// read as rgba
SAPI_ASSIGN_OR_RETURN(
status_or_int,
return_value,
api.TIFFReadRGBATile(&tif2, 1 * 128, 2 * 128, rgba_buffer_.PtrBoth()));
if (!status_or_int.value()) {
if (return_value == 0) {
return absl::InternalError("TIFFReadRGBATile() returned failure code");
}
// Checking specific pixels from the test data, 0th, 64th and 512th
if (status = CheckRgbaPixel(0, 15, 18, 0, 0, 18, 41, 255, 255, rgba_buffer_);
!status.ok()) {
LOG(ERROR) << "CheckRgbaPixel failed:\n" << status.ToString();
@ -276,7 +294,6 @@ int main(int argc, char** argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
std::string srcfile;
// "test/images/quad-tile.jpg.tiff"
std::string srcfilerel = "quad-tile.jpg.tiff";
if (argc < 2) {

View File

@ -19,7 +19,7 @@
namespace {
#define TBS 256 // kTileBufferSize
constexpr unsigned kTileBufferSize = 256;
constexpr uint16_t kWidth = 1;
constexpr uint16_t kBps = 8;
constexpr uint16_t kRowsPerStrip = 1;
@ -207,15 +207,15 @@ void TestWriting(const char* mode, int tiled, int height) {
if (tiled) {
for (int i = 0; i < (height + 15) / 16; ++i) {
std::array<unsigned char, TBS> tilebuffer;
std::array<unsigned char, kTileBufferSize> tilebuffer;
tilebuffer.fill(i);
sapi::v::Array<unsigned char> tilebuffer_(tilebuffer.data(), TBS);
sapi::v::Array<unsigned char> tilebuffer_(tilebuffer.data(), kTileBufferSize);
status_or_int =
api.TIFFWriteEncodedTile(&tif, i, tilebuffer_.PtrBoth(), TBS);
api.TIFFWriteEncodedTile(&tif, i, tilebuffer_.PtrBoth(), kTileBufferSize);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFWriteEncodedTile fatal error";
EXPECT_THAT(status_or_int.value(), Eq(TBS))
<< "line " << i << ": expected " << TBS << ", got "
EXPECT_THAT(status_or_int.value(), Eq(kTileBufferSize))
<< "line " << i << ": expected " << kTileBufferSize << ", got "
<< status_or_int.value();
}
} else {
@ -245,17 +245,17 @@ void TestWriting(const char* mode, int tiled, int height) {
if (tiled) {
for (int i = 0; i < (height + 15) / 16; ++i) {
for (int retry = 0; retry < 2; ++retry) {
std::array<unsigned char, TBS> tilebuffer;
std::array<unsigned char, kTileBufferSize> tilebuffer;
unsigned char expected_c = (unsigned char)i;
tilebuffer.fill(0);
sapi::v::Array<unsigned char> tilebuffer_(tilebuffer.data(), TBS);
sapi::v::Array<unsigned char> tilebuffer_(tilebuffer.data(), kTileBufferSize);
status_or_long =
api.TIFFReadEncodedTile(&tif2, i, tilebuffer_.PtrBoth(), TBS);
api.TIFFReadEncodedTile(&tif2, i, tilebuffer_.PtrBoth(), kTileBufferSize);
ASSERT_THAT(status_or_long, IsOk())
<< "TIFFReadEncodedTile fatal error";
EXPECT_THAT(status_or_long.value(), Eq(TBS))
<< "line " << i << ": expected " << TBS << ", got "
EXPECT_THAT(status_or_long.value(), Eq(kTileBufferSize))
<< "line " << i << ": expected " << kTileBufferSize << ", got "
<< status_or_long.value();
bool cmp = tilebuffer[0] != expected_c || tilebuffer[255] != expected_c;

View File

@ -28,7 +28,7 @@ struct LongTag {
constexpr std::array<LongTag, 1> kLongTags = {
{TIFFTAG_SUBFILETYPE, 1,
FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}};
#define SPP 3 // kSamplePerPixel
constexpr unsigned kSamplePerPixel = 3;
constexpr unsigned kWidth = 1;
constexpr unsigned kLength = 1;
constexpr unsigned kBps = 8;
@ -45,8 +45,8 @@ TEST(SandboxTest, LongTag) {
TiffSapiSandbox sandbox(srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
std::array<uint8_t, SPP> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), SPP);
std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), kSamplePerPixel);
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<TIFF*> status_or_tif;
@ -74,7 +74,7 @@ TEST(SandboxTest, LongTag) {
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set BitsPerSample tag";
status_or_int = api.TIFFSetFieldU1(&tif, TIFFTAG_SAMPLESPERPIXEL, SPP);
status_or_int = api.TIFFSetFieldU1(&tif, TIFFTAG_SAMPLESPERPIXEL, kSamplePerPixel);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldU1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue())
<< "Can't set SamplesPerPixel tag";

View File

@ -47,7 +47,7 @@ bool CheckCluster(int cluster, const sapi::v::Array<uint8_t>& buffer,
<< "\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[4];
<< target[3] << "\t" << target[4] << "\t" << target[5];
return comp;
}
@ -70,9 +70,9 @@ bool CheckRgbPixel(int pixel, int min_red, int max_red, int min_green,
EXPECT_THAT(comp, IsFalse())
<< "Pixel " << pixel << " did not match expected results.\n"
<< "Got R=" << rgb[0] << " (expected " << min_red << ".." << max_red
<< "), G=" << rgb[1] << " (expected " << min_green << ".." << max_green
<< "), B=" << rgb[2] << " (expected " << min_blue << ".." << max_blue
<< "Got R=" << rgb[0] << " (expected " << min_red << "..=" << max_red
<< "), G=" << rgb[1] << " (expected " << min_green << "..=" << max_green
<< "), B=" << rgb[2] << " (expected " << min_blue << "..=" << max_blue
<< ")";
return comp;
}
@ -103,11 +103,11 @@ bool CheckRgbaPixel(int pixel, int min_red, int max_red, int min_green,
EXPECT_THAT(comp, IsFalse())
<< "Pixel " << pixel << " did not match expected results.\n"
<< "Got R=" << TIFFGetR(rgba) << " (expected " << min_red << ".."
<< "Got R=" << TIFFGetR(rgba) << " (expected " << min_red << "..="
<< max_red << "), G=" << TIFFGetG(rgba) << " (expected " << min_green
<< ".." << max_green << "), B=" << TIFFGetB(rgba) << " (expected "
<< min_blue << ".." << max_blue << "), A=" << TIFFGetA(rgba)
<< " (expected " << min_alpha << ".." << max_alpha << ")";
<< "..=" << max_green << "), B=" << TIFFGetB(rgba) << " (expected "
<< min_blue << "..=" << max_blue << "), A=" << TIFFGetA(rgba)
<< " (expected " << min_alpha << "..=" << max_alpha << ")";
return comp;
}

View File

@ -20,7 +20,7 @@
namespace {
#define SPP 3 // kSamplePerPixel
constexpr unsigned kSamplePerPixel = 3;
constexpr uint16_t kWidth = 1;
constexpr uint16_t kLength = 1;
constexpr uint16_t kBps = 8;
@ -46,7 +46,7 @@ constexpr std::array<SingleTag, 9> kShortSingleTags = {
{TIFFTAG_MINSAMPLEVALUE, 23},
{TIFFTAG_MAXSAMPLEVALUE, 241},
{TIFFTAG_INKSET, INKSET_MULTIINK},
{TIFFTAG_NUMBEROFINKS, SPP},
{TIFFTAG_NUMBEROFINKS, kSamplePerPixel},
{TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT}}};
constexpr std::array<PairedTag, 4> kShortPairedTags = {
@ -66,8 +66,8 @@ TEST(SandboxTest, ShortTag) {
TiffSapiSandbox sandbox(srcfile);
ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API";
std::array<uint8_t, SPP> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), SPP);
std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), kSamplePerPixel);
sapi::StatusOr<int> status_or_int;
sapi::StatusOr<TIFF*> status_or_tif;
@ -95,7 +95,7 @@ TEST(SandboxTest, ShortTag) {
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue()) << "Can't set BitsPerSample tag";
status_or_int = api.TIFFSetFieldUShort1(&tif, TIFFTAG_SAMPLESPERPIXEL, SPP);
status_or_int = api.TIFFSetFieldUShort1(&tif, TIFFTAG_SAMPLESPERPIXEL, kSamplePerPixel);
ASSERT_THAT(status_or_int, IsOk()) << "TIFFSetFieldUShort1 fatal error";
EXPECT_THAT(status_or_int.value(), IsTrue())
<< "Can't set SamplesPerPixel tag";
@ -148,7 +148,7 @@ TEST(SandboxTest, ShortTag) {
CheckLongField(api, tif2, TIFFTAG_IMAGELENGTH, kLength);
CheckShortField(api, tif2, TIFFTAG_BITSPERSAMPLE, kBps);
CheckShortField(api, tif2, TIFFTAG_PHOTOMETRIC, kPhotometric);
CheckShortField(api, tif2, TIFFTAG_SAMPLESPERPIXEL, SPP);
CheckShortField(api, tif2, TIFFTAG_SAMPLESPERPIXEL, kSamplePerPixel);
CheckLongField(api, tif2, TIFFTAG_ROWSPERSTRIP, kRowsPerStrip);
CheckShortField(api, tif2, TIFFTAG_PLANARCONFIG, kPlanarConfig);