#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2021 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License // If the library is compiled as a DLL, this ensures symbols are exported #define NUCLEX_PIXELS_SOURCE 1 #include "../../../Source/Storage/WebP/WebPBitmapCodec.h" #if defined(NUCLEX_PIXELS_HAVE_LIBWEBP) #include #include "Nuclex/Pixels/Storage/VirtualFile.h" #include "Nuclex/Pixels/Errors/FileFormatError.h" #include "ExampleWebPs.h" #include // for std::copy_n() namespace { // ------------------------------------------------------------------------------------------- // /// Simple virtual file implementation that accesses an in-memory buffer class InMemoryFile : public Nuclex::Pixels::Storage::VirtualFile { /// Initializes a new memory buffer based file /// Memory buffer the virtual file will access /// Size of the memory buffer in bytes public: InMemoryFile(const std::uint8_t *data, std::uint64_t length) : data(data), length(length) {} /// Frees all memory used by the instance public: ~InMemoryFile() override = default; /// Determines the current size of the file in bytes /// The size of the file in bytes public: std::uint64_t GetSize() const override { return this->length; } /// Reads data from the file /// Offset in the file at which to begin reading /// Number of bytes that will be read /// Buffer into which the data will be read public: void ReadAt( std::uint64_t start, std::size_t byteCount, std::uint8_t *buffer ) const override { std::copy_n(this->data + start, byteCount, buffer); } /// Writes data into the file /// Offset at which writing will begin in the file /// Number of bytes that should be written /// Buffer holding the data that should be written public: void WriteAt( std::uint64_t start, std::size_t byteCount, const std::uint8_t *buffer ) override { (void)start; (void)byteCount; (void)buffer; assert(!u8"Write method of unit test dummy file is never called"); } /// Memory buffer the virtual file implementation is serving data from private: const std::uint8_t *data; /// Length of the memory buffer in bytes private: std::uint64_t length; }; // ------------------------------------------------------------------------------------------- // } // anonymous namespace namespace Nuclex { namespace Pixels { namespace Storage { namespace WebP { // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, HasDefaultConstructor) { EXPECT_NO_THROW( WebPBitmapCodec codec; ); } // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, ProvidesName) { WebPBitmapCodec codec; std::string codecName = codec.GetName(); EXPECT_GT(codecName.length(), 0U); } // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, FileExtensionsIncludeWebP) { WebPBitmapCodec codec; const std::vector &extensions = codec.GetFileExtensions(); bool webPFound = false; for(std::size_t index = 0; index < extensions.size(); ++index) { if((extensions[index] == "webp") || (extensions[index] == ".webp")) { webPFound = true; } } EXPECT_TRUE(webPFound); } /* // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, CanLoadMethodRecognizesPngs) { WebPBitmapCodec codec; { std::uint8_t dummyData[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }; const InMemoryFile dummyFile(dummyData, sizeof(dummyData)); EXPECT_FALSE(codec.CanLoad(dummyFile)); } { const InMemoryFile pngFile(testPng, sizeof(testPng)); EXPECT_TRUE(codec.CanLoad(pngFile)); } } // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, TryReadInfoReturnsEmptyOnWrongFileType) { WebPBitmapCodec codec; { std::uint8_t dummyData[16] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5 }; const InMemoryFile dummyFile(dummyData, sizeof(dummyData)); std::optional dummyBitmapInfo = codec.TryReadInfo(dummyFile); EXPECT_FALSE(dummyBitmapInfo.has_value()); } } // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, TryReadInfoThrowsOnCorruptedFile) { WebPBitmapCodec codec; { const InMemoryFile corruptPngFile(corruptPng, sizeof(corruptPng)); EXPECT_THROW( { codec.TryReadInfo(corruptPngFile); }, Errors::FileFormatError ); } } // ------------------------------------------------------------------------------------------- // TEST(WebPBitmapCodecTest, TryReadInfoSucceedsForPngs) { WebPBitmapCodec codec; { const InMemoryFile testPngFile(testPng, sizeof(testPng)); std::optional testPngBitmapInfo = codec.TryReadInfo(testPngFile); EXPECT_TRUE(testPngBitmapInfo.has_value()); } } // ------------------------------------------------------------------------------------------- // */ }}}} // namespace Nuclex::Pixels::Storage::WebP #endif // defined(NUCLEX_PIXELS_HAVE_LIBWEBP)