Skip to content

Commit

Permalink
Update unit tests and png reading
Browse files Browse the repository at this point in the history
  • Loading branch information
ssheorey committed Apr 24, 2024
1 parent 29ba9f5 commit ee38337
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 34 deletions.
2 changes: 1 addition & 1 deletion cpp/open3d/t/io/file_format/FileJPG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ bool WriteImageToJPG(const std::string &filename,
const geometry::Image &image,
int quality /* = kOpen3DImageIODefaultQuality*/) {
if (image.IsEmpty()) {
utility::LogWarning("Write JPG failed: image has no data.");
utility::LogError("Write JPG failed: image has no data.");
return false;
}
if (image.GetDtype() != core::UInt8 ||
Expand Down
27 changes: 13 additions & 14 deletions cpp/open3d/t/io/file_format/FilePNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ bool ReadImageFromPNG(const std::string &filename, geometry::Image &image) {
memset(&pngimage, 0, sizeof(pngimage));
pngimage.version = PNG_IMAGE_VERSION;
if (png_image_begin_read_from_file(&pngimage, filename.c_str()) == 0) {
utility::LogWarning("Read PNG failed: unable to parse header.");
utility::LogError("Read PNG failed: unable to parse header.");
return false;
}

Expand All @@ -61,9 +61,8 @@ bool ReadImageFromPNG(const std::string &filename, geometry::Image &image) {

if (png_image_finish_read(&pngimage, NULL, image.GetDataPtr(), 0, NULL) ==
0) {
utility::LogWarning("Read PNG failed: unable to read file: {}",
filename);
utility::LogWarning("PNG error: {}", pngimage.message);
utility::LogError("Read PNG failed: unable to read file: {}", filename);
utility::LogError("PNG error: {}", pngimage.message);
return false;
}
return true;
Expand All @@ -73,19 +72,19 @@ bool WriteImageToPNG(const std::string &filename,
const geometry::Image &image,
int quality) {
if (image.IsEmpty()) {
utility::LogWarning("Write PNG failed: image has no data.");
utility::LogError("Write PNG failed: image has no data.");
return false;
}
if (image.GetDtype() != core::UInt8 && image.GetDtype() != core::UInt16) {
utility::LogWarning("Write PNG failed: unsupported image data.");
utility::LogError("Write PNG failed: unsupported image data.");
return false;
}
if (quality == kOpen3DImageIODefaultQuality) // Set default quality
{
quality = 6;
}
if (quality < 0 || quality > 9) {
utility::LogWarning(
utility::LogError(
"Write PNG failed: quality ({}) must be in the range [0,9]",
quality);
return false;
Expand All @@ -96,8 +95,8 @@ bool WriteImageToPNG(const std::string &filename,
SetPNGImageFromImage(image, quality, pngimage);
if (png_image_write_to_file(&pngimage, filename.c_str(), 0,
image.GetDataPtr(), 0, NULL) == 0) {
utility::LogWarning("Write PNG failed: unable to write file: {}",
filename);
utility::LogError("Write PNG failed: unable to write file: {}",
filename);
return false;
}
return true;
Expand All @@ -107,19 +106,19 @@ bool WriteImageToPNGInMemory(std::vector<uint8_t> &buffer,
const t::geometry::Image &image,
int quality) {
if (image.IsEmpty()) {
utility::LogWarning("Write PNG failed: image has no data.");
utility::LogError("Write PNG failed: image has no data.");
return false;
}
if (image.GetDtype() != core::UInt8 && image.GetDtype() != core::UInt16) {
utility::LogWarning("Write PNG failed: unsupported image data.");
utility::LogError("Write PNG failed: unsupported image data.");
return false;
}
if (quality == kOpen3DImageIODefaultQuality) // Set default quality
{
quality = 6;
}
if (quality < 0 || quality > 9) {
utility::LogWarning(
utility::LogError(
"Write PNG failed: quality ({}) must be in the range [0,9]",
quality);
return false;
Expand All @@ -133,15 +132,15 @@ bool WriteImageToPNGInMemory(std::vector<uint8_t> &buffer,
size_t mem_bytes = 0;
if (png_image_write_to_memory(&pngimage, nullptr, &mem_bytes, 0,
image.GetDataPtr(), 0, nullptr) == 0) {
utility::LogWarning(
utility::LogError(
"Could not compute bytes needed for encoding to PNG in "
"memory.");
return false;
}
buffer.resize(mem_bytes);
if (png_image_write_to_memory(&pngimage, &buffer[0], &mem_bytes, 0,
image.GetDataPtr(), 0, nullptr) == 0) {
utility::LogWarning("Unable to encode to encode to PNG in memory.");
utility::LogError("Unable to encode to encode to PNG in memory.");
return false;
}
return true;
Expand Down
42 changes: 23 additions & 19 deletions cpp/tests/t/io/ImageIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,22 @@ TEST(ImageIO, DifferentDtype) {
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::UInt8)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::UInt16)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Float32)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Float64)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Int32)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Int64)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 3, core::Bool)));

Expand All @@ -198,19 +198,19 @@ TEST(ImageIO, DifferentDtype) {
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::UInt16)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Float32)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Float64)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Int32)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Int64)));
EXPECT_FALSE(
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.png",
t::geometry::Image(100, 200, 3, core::Bool)));
}
Expand All @@ -220,19 +220,23 @@ TEST(ImageIO, CornerCases) {
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 0, core::UInt8)));
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(0, 200, 3, core::UInt8)));
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(0, 200, 3, core::UInt8)));
EXPECT_TRUE(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jpg",
t::geometry::Image(100, 200, 1, core::UInt8)));

// Wrong extension
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.jg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_FALSE(t::io::WriteImage(tmp_path + "/test_imageio_dtype.pg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.jg",
t::geometry::Image(100, 0, 3, core::UInt8)));
EXPECT_ANY_THROW(
t::io::WriteImage(tmp_path + "/test_imageio_dtype.pg",
t::geometry::Image(100, 0, 3, core::UInt8)));
}

} // namespace tests
Expand Down

0 comments on commit ee38337

Please sign in to comment.