diff --git a/library/phx/resources/types/image.cpp b/library/phx/resources/types/image.cpp index f4da0845b2f216c908b3172ebc1bf92c935027c9..48f4fa2910e834a97858abadd19c89179825ec60 100644 --- a/library/phx/resources/types/image.cpp +++ b/library/phx/resources/types/image.cpp @@ -25,6 +25,8 @@ #include <string> #include <utility> +#include "phx/core/logger.hpp" + namespace phx { std::once_flag Image::once_flag_; @@ -46,7 +48,20 @@ Image::Image(const std::string& filepath, const std::int32_t native_flags) { format = FreeImage_GetFIFFromFilename(filepath.c_str()); native_ = FreeImage_Load(format, filepath.c_str(), native_flags); - if (!native_) throw std::runtime_error("FreeImage_Load failed."); + if (!native_) { + error( + "FreeImage_Load failed for path {}. A default 1x1 fuchsia image is " + "used instead.", + filepath); + native_ = FreeImage_AllocateT(FIT_BITMAP, static_cast<std::int32_t>(1), + static_cast<std::int32_t>(1), + static_cast<std::int32_t>(32), 0, 0, 0); + + std::array<std::uint8_t, 4> fuchsia_pixel = {{255u, 0u, 255u, 255u}}; + + FreeImage_SetPixelColor(native_, 0, 0, + reinterpret_cast<RGBQUAD*>(fuchsia_pixel.data())); + } SwapRedBlue(native_); } Image::Image(const Image& that) diff --git a/tests/src/test_image.cpp b/tests/src/test_image.cpp index bc6f702a54d3096148e61d1c9c2a6aeb7d9a5a9a..8c52dd7d58e520cb444cd8d407a21ea8e25ec248 100644 --- a/tests/src/test_image.cpp +++ b/tests/src/test_image.cpp @@ -173,3 +173,22 @@ SCENARIO("Images can be loaded from and saved to disk.", "[phx][phx::Image]") { } } } + +SCENARIO( + "When the filepath of an image does not exist, a default image is " + "generated.", + "[phx][phx::Image]") { + WHEN("We load an Image from an invalid filepath.") { + auto invalid_image = std::make_unique<phx::Image>("This is nonsense"); + + THEN("The image is a 1x1 fuchsia image.") { + auto dimensions = invalid_image->GetDimensions(); + auto pixel = invalid_image->GetPixelColor({{0, 0}}); + std::array<std::uint8_t, 4> fuchsia_pixel = {{255u, 0u, 255u, 255u}}; + + REQUIRE(dimensions[0] == 1); + REQUIRE(dimensions[1] == 1); + REQUIRE(pixel == fuchsia_pixel); + } + } +} diff --git a/tests/test_utilities/opengl_buffer_data_comparison.hpp b/tests/test_utilities/opengl_buffer_data_comparison.hpp index 5f86bb1bb79253277cd60ca51606447cfd5997d4..47c28f0cce855ac18f8f8fee696fe79518424ee6 100644 --- a/tests/test_utilities/opengl_buffer_data_comparison.hpp +++ b/tests/test_utilities/opengl_buffer_data_comparison.hpp @@ -35,11 +35,15 @@ #include <tuple> #include <utility> +#include "boost/filesystem/operations.hpp" +#include "boost/filesystem/path.hpp" + #include "catch/catch.hpp" +#include "phx/core/logger.hpp" #include "phx/rendering/backend/opengl_image_buffer_data.hpp" -#include "phx/resources/types/image.hpp" #include "phx/resources/resource_manager.hpp" +#include "phx/resources/types/image.hpp" #include "test_utilities/reference_image_path.hpp" @@ -217,9 +221,14 @@ void OpenGLBufferComparison::REQUIRE_REFERENCE_IMAGE_SIMILARITY( phx::ResourceUtils::DeclarationFromFile( filename_with_path, {{"bit_format", bit_format}}, true)); - try { - image.Load(); - } catch (const std::exception&) { + if (boost::filesystem::exists(boost::filesystem::path(filename_with_path))) { + try { + image.Load(); + } catch (const std::exception&) { + phx::error( + "Unexpected exception while loading the reference image. File exists " + "but may be corrupted."); + } } similarity = ComputeSimilarity(buffer_test, image.Get());