From ee3833732a9ec2e08c929e2abaf2cfd6b3c40b73 Mon Sep 17 00:00:00 2001 From: Sameer Sheorey Date: Tue, 23 Apr 2024 18:26:36 -0700 Subject: [PATCH] Update unit tests and png reading --- cpp/open3d/t/io/file_format/FileJPG.cpp | 2 +- cpp/open3d/t/io/file_format/FilePNG.cpp | 27 ++++++++-------- cpp/tests/t/io/ImageIO.cpp | 42 ++++++++++++++----------- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/cpp/open3d/t/io/file_format/FileJPG.cpp b/cpp/open3d/t/io/file_format/FileJPG.cpp index e7c29bcda90..1b1f5247a03 100644 --- a/cpp/open3d/t/io/file_format/FileJPG.cpp +++ b/cpp/open3d/t/io/file_format/FileJPG.cpp @@ -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 || diff --git a/cpp/open3d/t/io/file_format/FilePNG.cpp b/cpp/open3d/t/io/file_format/FilePNG.cpp index 2497e2a71c8..a87a52c47bd 100644 --- a/cpp/open3d/t/io/file_format/FilePNG.cpp +++ b/cpp/open3d/t/io/file_format/FilePNG.cpp @@ -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; } @@ -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; @@ -73,11 +72,11 @@ 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 @@ -85,7 +84,7 @@ bool WriteImageToPNG(const std::string &filename, 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; @@ -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; @@ -107,11 +106,11 @@ bool WriteImageToPNGInMemory(std::vector &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 @@ -119,7 +118,7 @@ bool WriteImageToPNGInMemory(std::vector &buffer, 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; @@ -133,7 +132,7 @@ bool WriteImageToPNGInMemory(std::vector &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; @@ -141,7 +140,7 @@ bool WriteImageToPNGInMemory(std::vector &buffer, 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; diff --git a/cpp/tests/t/io/ImageIO.cpp b/cpp/tests/t/io/ImageIO.cpp index 12fb7dd511c..0f149a3576c 100644 --- a/cpp/tests/t/io/ImageIO.cpp +++ b/cpp/tests/t/io/ImageIO.cpp @@ -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))); @@ -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))); } @@ -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