Merge pull request #1 from alexelex/alexelex-libtiff

review fixes
This commit is contained in:
Latysheva Alexandra 2020-10-11 17:08:21 +06:00 committed by GitHub
commit e5b8f271b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 205 additions and 170 deletions

View File

@ -36,90 +36,90 @@ set(SAPI_ENABLE_TESTS ${TIFF_SAPI_ENABLE_TESTS} CACHE BOOL "" FORCE)
add_subdirectory(wrapper) add_subdirectory(wrapper)
add_subdirectory( add_subdirectory(
"${SAPI_ROOT}" "${SAPI_ROOT}"
"${CMAKE_BINARY_DIR}/sandboxed-api-build" "${CMAKE_BINARY_DIR}/sandboxed-api-build"
# Omit this to have the full Sandboxed API in IDE # Omit this to have the full Sandboxed API in IDE
EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL
) )
add_sapi_library(tiff_sapi add_sapi_library(tiff_sapi
# List of functions that we want to include in the # List of functions that we want to include in the
# generated sandboxed API class # generated sandboxed API class
FUNCTIONS TIFFOpen FUNCTIONS TIFFOpen
TIFFClose TIFFClose
TIFFGetField1 TIFFGetField1
TIFFGetField2 TIFFGetField2
TIFFGetField3 TIFFGetField3
TIFFSetFieldUChar1 TIFFSetFieldUChar1
TIFFSetFieldUChar2 TIFFSetFieldUChar2
TIFFSetFieldUChar3 TIFFSetFieldUChar3
TIFFSetFieldSChar1 TIFFSetFieldSChar1
TIFFSetFieldSChar2 TIFFSetFieldSChar2
TIFFSetFieldSChar3 TIFFSetFieldSChar3
TIFFSetFieldU1 TIFFSetFieldU1
TIFFSetFieldU2 TIFFSetFieldU2
TIFFSetFieldU3 TIFFSetFieldU3
TIFFSetFieldS1 TIFFSetFieldS1
TIFFSetFieldS2 TIFFSetFieldS2
TIFFSetFieldS3 TIFFSetFieldS3
TIFFSetFieldUShort1 TIFFSetFieldUShort1
TIFFSetFieldUShort2 TIFFSetFieldUShort2
TIFFSetFieldUShort3 TIFFSetFieldUShort3
TIFFSetFieldSShort1 TIFFSetFieldSShort1
TIFFSetFieldSShort2 TIFFSetFieldSShort2
TIFFSetFieldSShort3 TIFFSetFieldSShort3
TIFFSetFieldULLong1 TIFFSetFieldULLong1
TIFFSetFieldULLong2 TIFFSetFieldULLong2
TIFFSetFieldULLong3 TIFFSetFieldULLong3
TIFFSetFieldSLLong1 TIFFSetFieldSLLong1
TIFFSetFieldSLLong2 TIFFSetFieldSLLong2
TIFFSetFieldSLLong3 TIFFSetFieldSLLong3
TIFFSetFieldFloat1 TIFFSetFieldFloat1
TIFFSetFieldFloat2 TIFFSetFieldFloat2
TIFFSetFieldFloat3 TIFFSetFieldFloat3
TIFFSetFieldDouble1 TIFFSetFieldDouble1
TIFFSetFieldDouble2 TIFFSetFieldDouble2
TIFFSetFieldDouble3 TIFFSetFieldDouble3
TIFFReadRGBATile TIFFReadRGBATile
TIFFReadEncodedTile TIFFReadEncodedTile
TIFFReadEncodedStrip TIFFReadEncodedStrip
TIFFReadFromUserBuffer TIFFReadFromUserBuffer
TIFFTileSize TIFFTileSize
TIFFSetDirectory TIFFSetDirectory
TIFFFreeDirectory TIFFFreeDirectory
TIFFCreateDirectory TIFFCreateDirectory
TIFFForceStrileArrayWriting TIFFForceStrileArrayWriting
TIFFDeferStrileArrayWriting TIFFDeferStrileArrayWriting
TIFFWriteCheck TIFFWriteCheck
TIFFWriteScanline TIFFWriteScanline
TIFFWriteDirectory TIFFWriteDirectory
TIFFWriteEncodedTile TIFFWriteEncodedTile
TIFFWriteEncodedStrip TIFFWriteEncodedStrip
TIFFGetStrileOffsetWithErr TIFFGetStrileOffsetWithErr
TIFFGetStrileByteCountWithErr TIFFGetStrileByteCountWithErr
TIFFCreateEXIFDirectory TIFFCreateEXIFDirectory
TIFFWriteCustomDirectory TIFFWriteCustomDirectory
INPUTS wrapper/libtiff/libtiff/tiffio.h INPUTS wrapper/libtiff/libtiff/tiffio.h
wrapper/func.h wrapper/func.h
# Header files or .cc files that should be parsed # Header files or .cc files that should be parsed
LIBRARY wrapped_tiff # Library dependency from the add_library() above LIBRARY wrapped_tiff # Library dependency from the add_library() above
LIBRARY_NAME Tiff # Name prefix for the generated header. Will be LIBRARY_NAME Tiff # Name prefix for the generated header. Will be
# suffixed with "Api" and "Sandbox" as needed. # suffixed with "Api" and "Sandbox" as needed.
NAMESPACE "" # Optional C++ namespace to wrap the generated code NAMESPACE "" # Optional C++ namespace to wrap the generated code
) )
target_include_directories(tiff_sapi INTERFACE target_include_directories(tiff_sapi INTERFACE
"${PROJECT_BINARY_DIR}" # To find the generated SAPI header "${PROJECT_BINARY_DIR}" # To find the generated SAPI header
) )
if (TIFF_SAPI_ENABLE_EXAMPLES) if (TIFF_SAPI_ENABLE_EXAMPLES)

View File

@ -8,13 +8,17 @@ You should make sure the libtiff submodule is cloned.
## Usage ## Usage
#### build: #### Build:
`mkdir -p build && cd build && cmake .. -DSAPI_ROOT=/path/to/sapi_root -DBUILD_SHARED_LIBS=OFF` ```
mkdir -p build && cd build && \
cmake .. -DSAPI_ROOT=/path/to/sapi_root -DBUILD_SHARED_LIBS=OFF
make -j8
```
`make -j 8` #### Example:
You should add `-DTIFF_SAPI_ENABLE_EXAMPLES=ON` to use the example.\
run: `./example/sandboxed /absolute/path/to/project/dir`
#### to run the sandboxed example: #### Tests:
`./example/sandboxed /absolute/path/to/project/dir` You should add `-DTIFF_SAPI_ENABLE_TESTS=ON` to use tests.\
run: `./test/tests`
#### to run tests:
`./test/tests`

View File

@ -17,7 +17,7 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include "../sandboxed.h" #include "../sandboxed.h" // NOLINT(build/include)
#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"
@ -36,51 +36,65 @@ struct ChannelLimits {
uint8_t max_alpha; uint8_t max_alpha;
}; };
constexpr unsigned kRawTileNumber = 9; constexpr uint32_t kRawTileNumber = 9;
constexpr unsigned kClusterSize = 6; constexpr uint32_t kClusterSize = 6;
constexpr unsigned kChannelsInPixel = 3; constexpr uint32_t kChannelsInPixel = 3;
constexpr unsigned kTestCount = 3; constexpr uint32_t kTestCount = 3;
constexpr unsigned kImageSize = 128 * 128; constexpr uint32_t kImageSize = 128 * 128;
constexpr unsigned kClusterImageSize = 64 * 64; constexpr uint32_t kClusterImageSize = 64 * 64;
using ClusterData = std::array<uint8_t, kClusterSize>; using ClusterData = std::array<uint8_t, kClusterSize>;
constexpr std::array<std::pair<unsigned, ClusterData>, kTestCount> kClusters = { constexpr std::array<std::pair<uint32_t, ClusterData>, kTestCount> kClusters = {
{{0, {0, 0, 2, 0, 138, 139}}, {{0, {0, 0, 2, 0, 138, 139}},
{64, {0, 0, 9, 6, 134, 119}}, {64, {0, 0, 9, 6, 134, 119}},
{128, {44, 40, 63, 59, 230, 95}}}}; {128, {44, 40, 63, 59, 230, 95}}}};
constexpr std::array<std::pair<unsigned, ChannelLimits>, kTestCount> kLimits = { constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
{{0, {15, 18, 0, 0, 18, 41, 255, 255}}, {{0, {15, 18, 0, 0, 18, 41, 255, 255}},
{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}}}};
absl::Status CheckCluster(unsigned cluster, 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";
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";
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";
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 * kClusterSize) {
return absl::InternalError("Buffer overrun\n"); return absl::InternalError("Buffer overrun\n");
} }
auto target = buffer.GetData() + cluster * kClusterSize; auto* target = buffer.GetData() + cluster * kClusterSize;
if (!std::memcmp(target, expected_cluster.data(), kClusterSize)) { if (!std::memcmp(target, expected_cluster.data(), kClusterSize)) {
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::StrCat( return absl::InternalError(absl::StrFormat(
"Cluster ", cluster, " did not match expected results.\n", "Expect: ", kClusterErrorFormatStr, cluster, expected_cluster[0], expected_cluster[1],
expected_cluster[0], "\t", expected_cluster[1], "\t", expected_cluster[2], expected_cluster[2], expected_cluster[3], expected_cluster[4],
"\t", expected_cluster[3], "\t", expected_cluster[4], "\t", expected_cluster[5], target[0], target[1], target[2], target[3],
expected_cluster[5], "\n", "Got: ", target[0], "\t", target[1], "\t", target[4], target[5]));
target[2], "\t", target[3], "\t", target[4], "\t", target[5], "\n"));
} }
absl::Status CheckRgbPixel(unsigned 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 * kChannelsInPixel) {
return absl::InternalError("Buffer overrun\n"); return absl::InternalError("Buffer overrun\n");
} }
auto rgb = buffer.GetData() + kChannelsInPixel * pixel; auto* rgb = buffer.GetData() + kChannelsInPixel * pixel;
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 &&
@ -88,17 +102,16 @@ absl::Status CheckRgbPixel(unsigned pixel, const ChannelLimits& limits,
return absl::OkStatus(); return absl::OkStatus();
} }
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrFormat(
"Pixel ", pixel, " did not match expected results.\n", "Got R=", rgb[0], kRgbPixelErrorFormatStr, pixel, rgb[0], limits.min_red, limits.max_red,
" (expected ", limits.min_red, "..=", limits.max_red, "), G=", rgb[1], rgb[1], limits.min_green, limits.max_green, rgb[2], limits.min_blue,
" (expected ", limits.min_green, "..=", limits.max_green, "), B=", rgb[2], limits.max_blue));
" (expected ", limits.min_blue, "..=", limits.max_blue, ")\n"));
} }
absl::Status CheckRgbaPixel(unsigned pixel, const ChannelLimits& limits, absl::Status CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
const sapi::v::Array<unsigned>& 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
unsigned 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\n");
@ -116,13 +129,11 @@ absl::Status CheckRgbaPixel(unsigned pixel, const ChannelLimits& limits,
return absl::OkStatus(); return absl::OkStatus();
} }
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrFormat(
"Pixel ", pixel, " did not match expected results.\n", "Got R=", kRgbaPixelErrorFormatStr, pixel, TIFFGetR(rgba), limits.min_red,
TIFFGetR(rgba), " (expected ", limits.min_red, "..=", limits.max_red, limits.max_red, TIFFGetG(rgba), limits.min_green, limits.max_green,
"), G=", TIFFGetG(rgba), " (expected ", limits.min_green, TIFFGetB(rgba), limits.min_blue, limits.max_blue, TIFFGetA(rgba),
"..=", limits.max_green, "), B=", TIFFGetB(rgba), " (expected ", limits.min_alpha, limits.max_alpha));
limits.min_blue, "..=", limits.max_blue, "), A=", TIFFGetA(rgba),
" (expected ", limits.min_alpha, "..=", limits.max_alpha, ")\n"));
} }
} // namespace } // namespace
@ -163,7 +174,8 @@ absl::Status LibTIFFMain(const std::string& srcfile) {
bool pixel_status = true; bool pixel_status = true;
bool cluster_status = true; bool cluster_status = true;
// initialize sapi vars after constructing TiffSapiSandbox // initialize sapi vars after constructing TiffSapiSandbox
sapi::v::UShort h, v; sapi::v::UShort h;
sapi::v::UShort v;
absl::StatusOr<TIFF*> status_or_tif; absl::StatusOr<TIFF*> status_or_tif;
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
absl::StatusOr<tmsize_t> status_or_long; absl::StatusOr<tmsize_t> status_or_long;
@ -263,7 +275,7 @@ 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<unsigned> 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(

View File

@ -18,7 +18,7 @@
#include <optional> #include <optional>
#include <utility> #include <utility>
#include "tiff_sapi.sapi.h" #include "tiff_sapi.sapi.h" // NOLINT(build/include)
namespace { namespace {

View File

@ -12,7 +12,11 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "check_tag.h" #include "check_tag.h" // NOLINT(build/include)
using ::sapi::IsOk;
using ::testing::Eq;
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) {
@ -46,7 +50,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 unsigned value) { const uint32_t value) {
sapi::v::UInt tmp(123); sapi::v::UInt tmp(123);
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;

View File

@ -14,7 +14,7 @@
#include <cstring> #include <cstring>
#include "helper.h" #include "helper.h" // NOLINT(build/include)
#include "tiffio.h" // NOLINT(build/include) #include "tiffio.h" // NOLINT(build/include)
void CheckShortField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field, void CheckShortField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field,
@ -23,4 +23,4 @@ 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);
void CheckLongField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field, void CheckLongField(TiffApi&, sapi::v::RemotePtr& tif, const ttag_t field,
const unsigned value); const uint32_t value);

View File

@ -14,12 +14,17 @@
#include <cstdint> #include <cstdint>
#include "helper.h" #include "helper.h" // NOLINT(build/include)
#include "tiffio.h" // NOLINT(build/include) #include "tiffio.h" // NOLINT(build/include)
namespace { namespace {
constexpr unsigned kTileBufferSize = 256; using ::sapi::IsOk;
using ::testing::Eq;
using ::testing::IsTrue;
using ::testing::NotNull;
constexpr uint16_t kTileBufferSize = 256;
constexpr uint16_t kWidth = 1; constexpr uint16_t kWidth = 1;
constexpr uint16_t kBps = 8; constexpr uint16_t kBps = 8;
constexpr uint16_t kRowsPerStrip = 1; constexpr uint16_t kRowsPerStrip = 1;
@ -207,10 +212,9 @@ void TestWriting(const char* mode, int tiled, int height) {
if (tiled) { if (tiled) {
for (int i = 0; i < (height + 15) / 16; ++i) { for (int i = 0; i < (height + 15) / 16; ++i) {
std::array<unsigned char, kTileBufferSize> tilebuffer; std::array<uint8_t, kTileBufferSize> tilebuffer;
tilebuffer.fill(i); tilebuffer.fill(i);
sapi::v::Array<unsigned char> tilebuffer_(tilebuffer.data(), sapi::v::Array<uint8_t> tilebuffer_(tilebuffer.data(), tilebuffer.size());
kTileBufferSize);
status_or_int = api.TIFFWriteEncodedTile(&tif, i, tilebuffer_.PtrBoth(), status_or_int = api.TIFFWriteEncodedTile(&tif, i, tilebuffer_.PtrBoth(),
kTileBufferSize); kTileBufferSize);
@ -246,12 +250,13 @@ void TestWriting(const char* mode, int tiled, int height) {
if (tiled) { if (tiled) {
for (int i = 0; i < (height + 15) / 16; ++i) { for (int i = 0; i < (height + 15) / 16; ++i) {
for (int retry = 0; retry < 2; ++retry) { for (int retry = 0; retry < 2; ++retry) {
std::array<unsigned char, kTileBufferSize> tilebuffer; std::array<uint8_t, kTileBufferSize> tilebuffer;
unsigned char expected_c = (unsigned char)i; uint8_t expected_c = static_cast<uint8_t>(i);
tilebuffer.fill(0); tilebuffer.fill(0);
sapi::v::Array<unsigned char> tilebuffer_(tilebuffer.data(), sapi::v::Array<uint8_t> tilebuffer_(tilebuffer.data(),
kTileBufferSize); tilebuffer.size());
status_or_long = api.TIFFReadEncodedTile( status_or_long = api.TIFFReadEncodedTile(
&tif2, i, tilebuffer_.PtrBoth(), kTileBufferSize); &tif2, i, tilebuffer_.PtrBoth(), kTileBufferSize);
ASSERT_THAT(status_or_long, IsOk()) ASSERT_THAT(status_or_long, IsOk())
@ -278,7 +283,7 @@ void TestWriting(const char* mode, int tiled, int height) {
for (int i = 0; i < height; ++i) { for (int i = 0; i < height; ++i) {
for (int retry = 0; retry < 2; ++retry) { for (int retry = 0; retry < 2; ++retry) {
sapi::v::UChar c(0); sapi::v::UChar c(0);
unsigned char expected_c = (unsigned char)i; uint8_t expected_c = static_cast<uint8_t>(i);
status_or_long = api.TIFFReadEncodedStrip(&tif2, i, c.PtrBoth(), 1); status_or_long = api.TIFFReadEncodedStrip(&tif2, i, c.PtrBoth(), 1);
ASSERT_THAT(status_or_long, IsOk()) ASSERT_THAT(status_or_long, IsOk())

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "helper.h" #include "helper.h" // NOLINT(build/include)
std::string GetImagesFolder() { std::string GetImagesFolder() {
std::string cwd = sandbox2::file_util::fileops::GetCWD(); std::string cwd = sandbox2::file_util::fileops::GetCWD();

View File

@ -15,18 +15,11 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include "../sandboxed.h" #include "../sandboxed.h" // NOLINT(build/include)
#include "gtest/gtest.h" #include "gtest/gtest.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/sandbox2/util/temp_file.h" #include "sandboxed_api/sandbox2/util/temp_file.h"
#include "sandboxed_api/util/status_matchers.h" #include "sandboxed_api/util/status_matchers.h"
using ::sapi::IsOk;
using ::testing::Eq;
using ::testing::IsFalse;
using ::testing::IsTrue;
using ::testing::Ne;
using ::testing::NotNull;
std::string GetFilePath(const std::string& filename); std::string GetFilePath(const std::string& filename);

View File

@ -14,25 +14,30 @@
#include <array> #include <array>
#include "check_tag.h" #include "check_tag.h" // NOLINT(build/include)
#include "tiffio.h" // NOLINT(build/include) #include "tiffio.h" // NOLINT(build/include)
namespace { namespace {
using ::sapi::IsOk;
using ::testing::IsTrue;
using ::testing::Ne;
using ::testing::NotNull;
struct LongTag { struct LongTag {
ttag_t tag; ttag_t tag;
short count; int16_t count;
unsigned value; uint32_t value;
}; };
constexpr std::array<LongTag, 1> kLongTags = { constexpr std::array<LongTag, 1> kLongTags = {
{TIFFTAG_SUBFILETYPE, 1, {TIFFTAG_SUBFILETYPE, 1,
FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}}; FILETYPE_REDUCEDIMAGE | FILETYPE_PAGE | FILETYPE_MASK}};
constexpr unsigned kSamplePerPixel = 3; constexpr uint32_t kSamplePerPixel = 3;
constexpr unsigned kWidth = 1; constexpr uint32_t kWidth = 1;
constexpr unsigned kLength = 1; constexpr uint32_t kLength = 1;
constexpr unsigned kBps = 8; constexpr uint32_t kBps = 8;
constexpr unsigned kRowsPerStrip = 1; constexpr uint32_t kRowsPerStrip = 1;
TEST(SandboxTest, LongTag) { TEST(SandboxTest, LongTag) {
absl::StatusOr<std::string> status_or_path = absl::StatusOr<std::string> status_or_path =
@ -46,7 +51,7 @@ TEST(SandboxTest, LongTag) {
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}; std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), kSamplePerPixel); sapi::v::Array<uint8_t> buffer_(buffer.data(), buffer.size());
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
absl::StatusOr<TIFF*> status_or_tif; absl::StatusOr<TIFF*> status_or_tif;

View File

@ -15,12 +15,17 @@
#include <array> #include <array>
#include <cstring> #include <cstring>
#include "gtest/gtest.h" #include "helper.h" // NOLINT(build/include)
#include "helper.h"
#include "tiffio.h" // NOLINT(build/include) #include "tiffio.h" // NOLINT(build/include)
namespace { namespace {
using ::sapi::IsOk;
using ::testing::Eq;
using ::testing::IsFalse;
using ::testing::IsTrue;
using ::testing::NotNull;
struct ChannelLimits { struct ChannelLimits {
uint8_t min_red; uint8_t min_red;
uint8_t max_red; uint8_t max_red;
@ -32,25 +37,25 @@ struct ChannelLimits {
uint8_t max_alpha; uint8_t max_alpha;
}; };
constexpr unsigned kRawTileNumber = 9; constexpr uint32_t kRawTileNumber = 9;
constexpr unsigned kClusterSize = 6; constexpr uint32_t kClusterSize = 6;
constexpr unsigned kChannelsInPixel = 3; constexpr uint32_t kChannelsInPixel = 3;
constexpr unsigned kTestCount = 3; constexpr uint32_t kTestCount = 3;
constexpr unsigned kImageSize = 128 * 128; constexpr uint32_t kImageSize = 128 * 128;
constexpr unsigned kClusterImageSize = 64 * 64; constexpr uint32_t kClusterImageSize = 64 * 64;
using ClusterData = std::array<uint8_t, kClusterSize>; using ClusterData = std::array<uint8_t, kClusterSize>;
constexpr std::array<std::pair<unsigned, ClusterData>, kTestCount> kClusters = { constexpr std::array<std::pair<uint32_t, ClusterData>, kTestCount> kClusters = {
{{0, {0, 0, 2, 0, 138, 139}}, {{0, {0, 0, 2, 0, 138, 139}},
{64, {0, 0, 9, 6, 134, 119}}, {64, {0, 0, 9, 6, 134, 119}},
{128, {44, 40, 63, 59, 230, 95}}}}; {128, {44, 40, 63, 59, 230, 95}}}};
constexpr std::array<std::pair<unsigned, ChannelLimits>, kTestCount> kLimits = { constexpr std::array<std::pair<uint32_t, ChannelLimits>, kTestCount> kLimits = {
{{0, {15, 18, 0, 0, 18, 41, 255, 255}}, {{0, {15, 18, 0, 0, 18, 41, 255, 255}},
{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(unsigned cluster, const sapi::v::Array<uint8_t>& buffer, bool 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); bool is_overrun = (buffer.GetSize() <= cluster * kClusterSize);
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun"; EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
@ -59,7 +64,7 @@ bool CheckCluster(unsigned cluster, const sapi::v::Array<uint8_t>& buffer,
return true; return true;
} }
auto target = buffer.GetData() + cluster * kClusterSize; auto* target = buffer.GetData() + cluster * kClusterSize;
bool comp = bool comp =
!(std::memcmp(target, expected_cluster.data(), kClusterSize) == 0); !(std::memcmp(target, expected_cluster.data(), kClusterSize) == 0);
@ -75,7 +80,7 @@ bool CheckCluster(unsigned cluster, const sapi::v::Array<uint8_t>& buffer,
return comp; return comp;
} }
bool CheckRgbPixel(unsigned pixel, const ChannelLimits& limits, bool 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); bool is_overrun = (buffer.GetSize() <= pixel * kChannelsInPixel);
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun"; EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
@ -84,7 +89,7 @@ bool CheckRgbPixel(unsigned pixel, const ChannelLimits& limits,
return true; return true;
} }
auto rgb = buffer.GetData() + pixel * kChannelsInPixel; auto* rgb = buffer.GetData() + pixel * kChannelsInPixel;
bool comp = !(rgb[0] >= limits.min_red && rgb[0] <= limits.max_red && bool comp = !(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 &&
rgb[2] >= limits.min_blue && rgb[2] <= limits.max_blue); rgb[2] >= limits.min_blue && rgb[2] <= limits.max_blue);
@ -98,10 +103,10 @@ bool CheckRgbPixel(unsigned pixel, const ChannelLimits& limits,
return comp; return comp;
} }
bool CheckRgbaPixel(unsigned pixel, const ChannelLimits& limits, bool CheckRgbaPixel(uint32_t pixel, const ChannelLimits& limits,
const sapi::v::Array<unsigned>& 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
unsigned 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); bool is_overrun = (buffer.GetSize() <= adjusted_pixel);
EXPECT_THAT(is_overrun, IsFalse()) << "Overrun"; EXPECT_THAT(is_overrun, IsFalse()) << "Overrun";
@ -140,7 +145,8 @@ TEST(SandboxTest, RawDecode) {
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, v; sapi::v::UShort h;
sapi::v::UShort v;
absl::StatusOr<TIFF*> status_or_tif; absl::StatusOr<TIFF*> status_or_tif;
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
absl::StatusOr<tmsize_t> status_or_long; absl::StatusOr<tmsize_t> status_or_long;
@ -219,7 +225,7 @@ 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<unsigned> 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_.PtrBoth());

View File

@ -15,12 +15,17 @@
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include "check_tag.h" #include "check_tag.h" // NOLINT(build/include)
#include "tiffio.h" // NOLINT(build/include) #include "tiffio.h" // NOLINT(build/include)
namespace { namespace {
constexpr unsigned kSamplePerPixel = 3; using ::sapi::IsOk;
using ::testing::IsTrue;
using ::testing::Ne;
using ::testing::NotNull;
constexpr uint16_t kSamplePerPixel = 3;
constexpr uint16_t kWidth = 1; constexpr uint16_t kWidth = 1;
constexpr uint16_t kLength = 1; constexpr uint16_t kLength = 1;
constexpr uint16_t kBps = 8; constexpr uint16_t kBps = 8;
@ -67,7 +72,7 @@ TEST(SandboxTest, ShortTag) {
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}; std::array<uint8_t, kSamplePerPixel> buffer = {0, 127, 255};
sapi::v::Array<uint8_t> buffer_(buffer.data(), kSamplePerPixel); sapi::v::Array<uint8_t> buffer_(buffer.data(), buffer.size());
absl::StatusOr<int> status_or_int; absl::StatusOr<int> status_or_int;
absl::StatusOr<TIFF*> status_or_tif; absl::StatusOr<TIFF*> status_or_tif;

View File

@ -16,6 +16,7 @@ add_library(wrapped_tiff OBJECT
func.h func.h
func.cc func.cc
) )
set_target_properties(wrapped_tiff set_target_properties(wrapped_tiff
PROPERTIES LINKER_LANGUAGE C PROPERTIES LINKER_LANGUAGE C
) )

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "func.h" #include "func.h" // NOLINT(build/include)
int TIFFGetField1(TIFF* tif, unsigned tag, void* param) { int TIFFGetField1(TIFF* tif, unsigned tag, void* param) {
return TIFFGetField(tif, tag, param); return TIFFGetField(tif, tag, param);