From b80282a05a840b4e9882b936dbbf32ec3bbffabb Mon Sep 17 00:00:00 2001 From: emeric Date: Sat, 6 Jan 2024 21:09:20 +0100 Subject: [PATCH] stb: exposed error messages --- src/libs/image/impl/stb/RawImage.cpp | 155 +++++++++++++-------------- src/libs/image/impl/stb/RawImage.hpp | 32 +++--- 2 files changed, 91 insertions(+), 96 deletions(-) diff --git a/src/libs/image/impl/stb/RawImage.cpp b/src/libs/image/impl/stb/RawImage.cpp index ba7a80b9..1f8a5e68 100644 --- a/src/libs/image/impl/stb/RawImage.cpp +++ b/src/libs/image/impl/stb/RawImage.cpp @@ -25,6 +25,8 @@ #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM +#define STBI_FAILURE_USERMSG + #include #include @@ -34,99 +36,92 @@ namespace Image { - std::unique_ptr decodeImage(const std::byte* encodedData, std::size_t encodedDataSize) - { - return std::make_unique(encodedData, encodedDataSize); - } + std::unique_ptr decodeImage(const std::byte* encodedData, std::size_t encodedDataSize) + { + return std::make_unique(encodedData, encodedDataSize); + } - std::unique_ptr decodeImage(const std::filesystem::path& path) - { - return std::make_unique(path); - } + std::unique_ptr decodeImage(const std::filesystem::path& path) + { + return std::make_unique(path); + } - void - init(const std::filesystem::path&) - { - } + void init(const std::filesystem::path&) + { + } } namespace Image::STB { - RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize) - { - int n; - _data = UniquePtrFree {stbi_load_from_memory(reinterpret_cast(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free}; - if (!_data) - throw ImageException {"Cannot load image from memory"}; - } + RawImage::RawImage(const std::byte* encodedData, std::size_t encodedDataSize) + { + int n; + _data = UniquePtrFree{ ::stbi_load_from_memory(reinterpret_cast(encodedData), encodedDataSize, &_width, &_height, &n, 3), std::free }; + if (!_data) + throw ImageException{ "Cannot load image from memory: " + std::string{ ::stbi_failure_reason() } }; + } - RawImage::RawImage(const std::filesystem::path& p) - { - int n; - _data = UniquePtrFree {stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free}; - if (!_data) - throw ImageException {"Cannot load image from memory"}; - } + RawImage::RawImage(const std::filesystem::path& p) + { + int n; + _data = UniquePtrFree{ stbi_load(p.string().c_str(), &_width, &_height, &n, 3), std::free }; + if (!_data) + throw ImageException{ "Cannot load image from file: " + std::string{ ::stbi_failure_reason() } }; + } - void - RawImage::resize(ImageSize width) - { - size_t height; - if (_width == _height) - { - height = width; - } - else if (_width > _height) - { - height = (size_t)((float)width/_width*_height); - } - else - { - height = width; - width = (size_t)((float)height/_height*_width); - } + void RawImage::resize(ImageSize width) + { + size_t height; + if (_width == _height) + { + height = width; + } + else if (_width > _height) + { + height = (size_t)((float)width / _width * _height); + } + else + { + height = width; + width = (size_t)((float)height / _height * _width); + } - UniquePtrFree resizedData {reinterpret_cast(malloc(width*height*3)), std::free}; - if (!resizedData) - throw ImageException {"Cannot allocate memory for resized image!"}; + UniquePtrFree resizedData{ reinterpret_cast(malloc(width * height * 3)), std::free }; + if (!resizedData) + throw ImageException{ "Cannot allocate memory for resized image!" }; - if (stbir_resize_uint8_srgb(reinterpret_cast(_data.get()), _width, _height, 0, - reinterpret_cast(resizedData.get()), width, height, 0, - 3, STBIR_ALPHA_CHANNEL_NONE, 0) == 0) - { - throw ImageException {"Failed to resize image!"}; - } + if (::stbir_resize_uint8_srgb(reinterpret_cast(_data.get()), _width, _height, 0, + reinterpret_cast(resizedData.get()), width, height, 0, + 3, STBIR_ALPHA_CHANNEL_NONE, 0) == 0) + { + throw ImageException{ "Failed to resize image:" + std::string{ ::stbi_failure_reason() } }; + } - _data = std::move(resizedData); - _height = height; - _width = width; - } + _data = std::move(resizedData); + _height = height; + _width = width; + } - std::unique_ptr - RawImage::encodeToJPEG(unsigned quality) const - { - return std::make_unique(*this, quality); - } + std::unique_ptr RawImage::encodeToJPEG(unsigned quality) const + { + return std::make_unique(*this, quality); + } - ImageSize - RawImage::getWidth() const - { - return _width; - } + ImageSize RawImage::getWidth() const + { + return _width; + } - ImageSize - RawImage::getHeight() const - { - return _height; - } + ImageSize RawImage::getHeight() const + { + return _height; + } - const std::byte* - RawImage::getData() const - { - if (!_data) - return nullptr; - - return reinterpret_cast(_data.get()); - } -} + const std::byte* RawImage::getData() const + { + if (!_data) + return nullptr; + return reinterpret_cast(_data.get()); + } +} \ No newline at end of file diff --git a/src/libs/image/impl/stb/RawImage.hpp b/src/libs/image/impl/stb/RawImage.hpp index c7d7921a..60452529 100644 --- a/src/libs/image/impl/stb/RawImage.hpp +++ b/src/libs/image/impl/stb/RawImage.hpp @@ -31,24 +31,24 @@ namespace Image::STB { - class RawImage : public IRawImage - { - public: - RawImage(const std::byte* encodedData, std::size_t encodedDataSize); - RawImage(const std::filesystem::path& path); + class RawImage : public IRawImage + { + public: + RawImage(const std::byte* encodedData, std::size_t encodedDataSize); + RawImage(const std::filesystem::path& path); - void resize(ImageSize width) override; - std::unique_ptr encodeToJPEG(unsigned quality) const override; + void resize(ImageSize width) override; + std::unique_ptr encodeToJPEG(unsigned quality) const override; - ImageSize getWidth() const; - ImageSize getHeight() const; - const std::byte* getData() const; + ImageSize getWidth() const; + ImageSize getHeight() const; + const std::byte* getData() const; - private: - int _width; - int _height; - using UniquePtrFree = std::unique_ptr; - UniquePtrFree _data {nullptr, std::free}; - }; + private: + int _width; + int _height; + using UniquePtrFree = std::unique_ptr; + UniquePtrFree _data{ nullptr, std::free }; + }; }