From 2869fba479db50131e53112c274ed89c80da1b0f Mon Sep 17 00:00:00 2001 From: Alexandra Latysheva Date: Thu, 1 Oct 2020 16:49:59 +0000 Subject: [PATCH] example --- .../libtiff/example/main_sandboxed.cc | 81 +++++++++++-------- .../libtiff/test/defer_strile_writing.cc | 22 ++--- oss-internship-2020/libtiff/test/long_tag.cc | 8 +- .../libtiff/test/raw_decode.cc | 16 ++-- oss-internship-2020/libtiff/test/short_tag.cc | 12 +-- 5 files changed, 78 insertions(+), 61 deletions(-) diff --git a/oss-internship-2020/libtiff/example/main_sandboxed.cc b/oss-internship-2020/libtiff/example/main_sandboxed.cc index 25e2e0c..472431b 100644 --- a/oss-internship-2020/libtiff/example/main_sandboxed.cc +++ b/oss-internship-2020/libtiff/example/main_sandboxed.cc @@ -27,26 +27,37 @@ constexpr std::array kCluster0 = {0, 0, 2, 0, 138, 139}; constexpr std::array kCluster64 = {0, 0, 9, 6, 134, 119}; constexpr std::array kCluster128 = {44, 40, 63, 59, 230, 95}; +constexpr unsigned kRawTileNumber = 9; + namespace { absl::Status CheckCluster(int cluster, const sapi::v::Array& buffer, const std::array& 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& 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& 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 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 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 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) { diff --git a/oss-internship-2020/libtiff/test/defer_strile_writing.cc b/oss-internship-2020/libtiff/test/defer_strile_writing.cc index 507e3fc..02957b4 100644 --- a/oss-internship-2020/libtiff/test/defer_strile_writing.cc +++ b/oss-internship-2020/libtiff/test/defer_strile_writing.cc @@ -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 tilebuffer; + std::array tilebuffer; tilebuffer.fill(i); - sapi::v::Array tilebuffer_(tilebuffer.data(), TBS); + sapi::v::Array 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 tilebuffer; + std::array tilebuffer; unsigned char expected_c = (unsigned char)i; tilebuffer.fill(0); - sapi::v::Array tilebuffer_(tilebuffer.data(), TBS); + sapi::v::Array 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; diff --git a/oss-internship-2020/libtiff/test/long_tag.cc b/oss-internship-2020/libtiff/test/long_tag.cc index 964826f..396add3 100644 --- a/oss-internship-2020/libtiff/test/long_tag.cc +++ b/oss-internship-2020/libtiff/test/long_tag.cc @@ -28,7 +28,7 @@ struct LongTag { constexpr std::array 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 buffer = {0, 127, 255}; - sapi::v::Array buffer_(buffer.data(), SPP); + std::array buffer = {0, 127, 255}; + sapi::v::Array buffer_(buffer.data(), kSamplePerPixel); sapi::StatusOr status_or_int; sapi::StatusOr 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"; diff --git a/oss-internship-2020/libtiff/test/raw_decode.cc b/oss-internship-2020/libtiff/test/raw_decode.cc index b288a4e..20e655d 100644 --- a/oss-internship-2020/libtiff/test/raw_decode.cc +++ b/oss-internship-2020/libtiff/test/raw_decode.cc @@ -47,7 +47,7 @@ bool CheckCluster(int cluster, const sapi::v::Array& 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; } diff --git a/oss-internship-2020/libtiff/test/short_tag.cc b/oss-internship-2020/libtiff/test/short_tag.cc index 48555c7..5451cd1 100644 --- a/oss-internship-2020/libtiff/test/short_tag.cc +++ b/oss-internship-2020/libtiff/test/short_tag.cc @@ -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 kShortSingleTags = { {TIFFTAG_MINSAMPLEVALUE, 23}, {TIFFTAG_MAXSAMPLEVALUE, 241}, {TIFFTAG_INKSET, INKSET_MULTIINK}, - {TIFFTAG_NUMBEROFINKS, SPP}, + {TIFFTAG_NUMBEROFINKS, kSamplePerPixel}, {TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT}}}; constexpr std::array kShortPairedTags = { @@ -66,8 +66,8 @@ TEST(SandboxTest, ShortTag) { TiffSapiSandbox sandbox(srcfile); ASSERT_THAT(sandbox.Init(), IsOk()) << "Couldn't initialize Sandboxed API"; - std::array buffer = {0, 127, 255}; - sapi::v::Array buffer_(buffer.data(), SPP); + std::array buffer = {0, 127, 255}; + sapi::v::Array buffer_(buffer.data(), kSamplePerPixel); sapi::StatusOr status_or_int; sapi::StatusOr 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);