diff --git a/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.cpp b/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.cpp index 7ec7249c7c..3448eb04cd 100644 --- a/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.cpp +++ b/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.cpp @@ -8,44 +8,18 @@ #include #include -#include -#include -#include namespace iglu::textureloader::stb::hdr { +TextureLoaderFactory::TextureLoaderFactory() noexcept : image::TextureLoaderFactory(true) {} + uint32_t TextureLoaderFactory::headerLength() const noexcept { return kHeaderLength; } -bool TextureLoaderFactory::isFloatFormat() const noexcept { - return true; -} - -igl::TextureFormat TextureLoaderFactory::format() const noexcept { - return igl::TextureFormat::RGBA_F32; -} - -bool TextureLoaderFactory::canCreateInternal(DataReader headerReader, - igl::Result* IGL_NULLABLE outResult) const noexcept { - if (headerReader.data() == nullptr) { - igl::Result::setResult( - outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr."); - return false; - } - if (headerReader.length() < kHeaderLength) { - igl::Result::setResult( - outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header."); - return false; - } - +bool TextureLoaderFactory::isIdentifierValid(DataReader headerReader) const noexcept { const Header* header = headerReader.as
(); - if (!header->tagIsValid()) { - igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier."); - return false; - } - - return true; + return header->tagIsValid(); } } // namespace iglu::textureloader::stb::hdr diff --git a/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h b/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h index 7fc3bb6e06..94ff013a65 100644 --- a/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h +++ b/IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h @@ -19,16 +19,12 @@ namespace iglu::textureloader::stb::hdr { class TextureLoaderFactory final : public image::TextureLoaderFactory { public: - explicit TextureLoaderFactory() noexcept = default; + TextureLoaderFactory() noexcept; [[nodiscard]] uint32_t headerLength() const noexcept final; private: - [[nodiscard]] bool isFloatFormat() const noexcept final; - [[nodiscard]] igl::TextureFormat format() const noexcept final; - - [[nodiscard]] bool canCreateInternal(DataReader headerReader, - igl::Result* IGL_NULLABLE outResult) const noexcept final; + [[nodiscard]] bool isIdentifierValid(DataReader headerReader) const noexcept final; }; } // namespace iglu::textureloader::stb::hdr diff --git a/IGLU/texture_loader/stb_image/TextureLoaderFactory.cpp b/IGLU/texture_loader/stb_image/TextureLoaderFactory.cpp index 38199ca3cb..efbe26f7f9 100644 --- a/IGLU/texture_loader/stb_image/TextureLoaderFactory.cpp +++ b/IGLU/texture_loader/stb_image/TextureLoaderFactory.cpp @@ -51,11 +51,7 @@ class TextureLoader : public ITextureLoader { using Super = ITextureLoader; public: - explicit TextureLoader(DataReader reader, - int width, - int height, - igl::TextureFormat format, - bool isFloatFormat) noexcept; + explicit TextureLoader(DataReader reader, int width, int height, bool isFloatFormat) noexcept; [[nodiscard]] bool canUploadSourceData() const noexcept final; [[nodiscard]] bool shouldGenerateMipmaps() const noexcept final; @@ -66,14 +62,11 @@ class TextureLoader : public ITextureLoader { bool isFloatFormat_; }; -TextureLoader::TextureLoader(DataReader reader, - int width, - int height, - igl::TextureFormat format, - bool isFloatFormat) noexcept : +TextureLoader::TextureLoader(DataReader reader, int width, int height, bool isFloatFormat) noexcept + : Super(reader), isFloatFormat_(isFloatFormat) { auto& desc = mutableDescriptor(); - desc.format = format; + desc.format = isFloatFormat ? igl::TextureFormat::RGBA_F32 : igl::TextureFormat::RGBA_UNorm8; desc.numLayers = 1; desc.width = static_cast(width); desc.height = static_cast(height); @@ -116,6 +109,30 @@ std::unique_ptr TextureLoader::loadInternal( } } // namespace +TextureLoaderFactory::TextureLoaderFactory(bool isFloatFormat) noexcept : + isFloatFormat_(isFloatFormat) {} + +bool TextureLoaderFactory::canCreateInternal(DataReader headerReader, + igl::Result* IGL_NULLABLE outResult) const noexcept { + if (headerReader.data() == nullptr) { + igl::Result::setResult( + outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr."); + return false; + } + if (headerReader.length() < headerLength()) { + igl::Result::setResult( + outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header."); + return false; + } + + if (!isIdentifierValid(headerReader)) { + igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier."); + return false; + } + + return true; +} + std::unique_ptr TextureLoaderFactory::tryCreateInternal( DataReader reader, igl::Result* IGL_NULLABLE outResult) const noexcept { @@ -140,7 +157,7 @@ std::unique_ptr TextureLoaderFactory::tryCreateInternal( return nullptr; } - return std::make_unique(reader, x, y, format(), isFloatFormat()); + return std::make_unique(reader, x, y, isFloatFormat_); } } // namespace iglu::textureloader::stb::image diff --git a/IGLU/texture_loader/stb_image/TextureLoaderFactory.h b/IGLU/texture_loader/stb_image/TextureLoaderFactory.h index b6f47a140d..63e3f12005 100644 --- a/IGLU/texture_loader/stb_image/TextureLoaderFactory.h +++ b/IGLU/texture_loader/stb_image/TextureLoaderFactory.h @@ -12,17 +12,20 @@ namespace iglu::textureloader::stb::image { class TextureLoaderFactory : public ITextureLoaderFactory { - public: - explicit TextureLoaderFactory() noexcept = default; - protected: - [[nodiscard]] virtual bool isFloatFormat() const noexcept = 0; - [[nodiscard]] virtual igl::TextureFormat format() const noexcept = 0; + explicit TextureLoaderFactory(bool isFloatFormat = false) noexcept; + + [[nodiscard]] virtual bool isIdentifierValid(DataReader headerReader) const noexcept = 0; private: + [[nodiscard]] bool canCreateInternal(DataReader headerReader, + igl::Result* IGL_NULLABLE outResult) const noexcept final; + [[nodiscard]] std::unique_ptr tryCreateInternal( DataReader reader, igl::Result* IGL_NULLABLE outResult) const noexcept final; + + bool isFloatFormat_; }; } // namespace iglu::textureloader::stb::image diff --git a/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.cpp b/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.cpp index 92aca5f76f..a01f5117ce 100644 --- a/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.cpp +++ b/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.cpp @@ -8,9 +8,6 @@ #include #include -#include -#include -#include namespace iglu::textureloader::stb::jpeg { @@ -18,34 +15,9 @@ uint32_t TextureLoaderFactory::headerLength() const noexcept { return kHeaderLength; } -bool TextureLoaderFactory::isFloatFormat() const noexcept { - return false; -} - -igl::TextureFormat TextureLoaderFactory::format() const noexcept { - return igl::TextureFormat::RGBA_SRGB; -} - -bool TextureLoaderFactory::canCreateInternal(DataReader headerReader, - igl::Result* IGL_NULLABLE outResult) const noexcept { - if (headerReader.data() == nullptr) { - igl::Result::setResult( - outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr."); - return false; - } - if (headerReader.length() < kHeaderLength) { - igl::Result::setResult( - outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header."); - return false; - } - +bool TextureLoaderFactory::isIdentifierValid(DataReader headerReader) const noexcept { const Header* header = headerReader.as
(); - if (!header->tagIsValid()) { - igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier."); - return false; - } - - return true; + return header->tagIsValid(); } } // namespace iglu::textureloader::stb::jpeg diff --git a/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h b/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h index 6da66ee86c..a6939e5acb 100644 --- a/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h +++ b/IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h @@ -17,16 +17,12 @@ namespace iglu::textureloader::stb::jpeg { class TextureLoaderFactory final : public image::TextureLoaderFactory { public: - explicit TextureLoaderFactory() noexcept = default; + TextureLoaderFactory() noexcept = default; [[nodiscard]] uint32_t headerLength() const noexcept final; private: - [[nodiscard]] bool isFloatFormat() const noexcept final; - [[nodiscard]] igl::TextureFormat format() const noexcept final; - - [[nodiscard]] bool canCreateInternal(DataReader headerReader, - igl::Result* IGL_NULLABLE outResult) const noexcept final; + [[nodiscard]] bool isIdentifierValid(DataReader headerReader) const noexcept final; }; } // namespace iglu::textureloader::stb::jpeg diff --git a/IGLU/texture_loader/stb_png/TextureLoaderFactory.cpp b/IGLU/texture_loader/stb_png/TextureLoaderFactory.cpp index 5d7af9ffa7..9ebbf2c00a 100644 --- a/IGLU/texture_loader/stb_png/TextureLoaderFactory.cpp +++ b/IGLU/texture_loader/stb_png/TextureLoaderFactory.cpp @@ -8,9 +8,6 @@ #include #include -#include -#include -#include namespace iglu::textureloader::stb::png { @@ -18,34 +15,9 @@ uint32_t TextureLoaderFactory::headerLength() const noexcept { return kHeaderLength; } -bool TextureLoaderFactory::isFloatFormat() const noexcept { - return false; -} - -igl::TextureFormat TextureLoaderFactory::format() const noexcept { - return igl::TextureFormat::RGBA_SRGB; -} - -bool TextureLoaderFactory::canCreateInternal(DataReader headerReader, - igl::Result* IGL_NULLABLE outResult) const noexcept { - if (headerReader.data() == nullptr) { - igl::Result::setResult( - outResult, igl::Result::Code::ArgumentInvalid, "Reader's data is nullptr."); - return false; - } - if (headerReader.length() < kHeaderLength) { - igl::Result::setResult( - outResult, igl::Result::Code::ArgumentOutOfRange, "Not enough data for header."); - return false; - } - +bool TextureLoaderFactory::isIdentifierValid(DataReader headerReader) const noexcept { const Header* header = headerReader.as
(); - if (!header->tagIsValid()) { - igl::Result::setResult(outResult, igl::Result::Code::InvalidOperation, "Incorrect identifier."); - return false; - } - - return true; + return header->tagIsValid(); } } // namespace iglu::textureloader::stb::png diff --git a/IGLU/texture_loader/stb_png/TextureLoaderFactory.h b/IGLU/texture_loader/stb_png/TextureLoaderFactory.h index f0d0cd03a4..0161d15f8b 100644 --- a/IGLU/texture_loader/stb_png/TextureLoaderFactory.h +++ b/IGLU/texture_loader/stb_png/TextureLoaderFactory.h @@ -17,16 +17,12 @@ namespace iglu::textureloader::stb::png { class TextureLoaderFactory final : public image::TextureLoaderFactory { public: - explicit TextureLoaderFactory() noexcept = default; + TextureLoaderFactory() noexcept = default; [[nodiscard]] uint32_t headerLength() const noexcept final; private: - [[nodiscard]] bool isFloatFormat() const noexcept final; - [[nodiscard]] igl::TextureFormat format() const noexcept final; - - [[nodiscard]] bool canCreateInternal(DataReader headerReader, - igl::Result* IGL_NULLABLE outResult) const noexcept final; + [[nodiscard]] bool isIdentifierValid(DataReader headerReader) const noexcept final; }; } // namespace iglu::textureloader::stb::png