#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 #ifndef NUCLEX_PIXELS_STORAGE_BITMAPCODEC_H #define NUCLEX_PIXELS_STORAGE_BITMAPCODEC_H #include "Nuclex/Pixels/Config.h" #include "Nuclex/Pixels/BitmapInfo.h" #include "Nuclex/Pixels/Bitmap.h" #include // for std::string #include // for std::vector #include // for std::optional namespace Nuclex { namespace Pixels { namespace Storage { // ------------------------------------------------------------------------------------------- // class VirtualFile; // ------------------------------------------------------------------------------------------- // /// Codec that loads and saves bitmaps in a predefined file format class NUCLEX_PIXELS_TYPE BitmapCodec { /// Frees all resources owned by the instance public: virtual ~BitmapCodec() = default; /// Gives the name of the file format implemented by this codec /// The name of the file format this codec implements public: virtual const std::string &GetName() const = 0; /// Provides commonly used file extensions for this codec /// The commonly used file extensions in order of preference public: virtual const std::vector &GetFileExtensions() const = 0; /// Tries to read informations for a bitmap /// Source data from which the informations should be extracted /// Optional file extension the loaded data had /// Informations about the bitmap, if the codec is able to load it public: virtual std::optional TryReadInfo( const VirtualFile &source, const std::string &extensionHint = std::string() ) const = 0; /// Checks if the codec is able to load the specified file /// Source data that will be checked for loadbility /// Optional file extension the loaded data had /// True if the codec is able to load the specified file public: virtual bool CanLoad( const VirtualFile &source, const std::string &extensionHint = std::string() ) const = 0; /// Checks if the codec is able to save bitmaps to storage /// True if the codec supports saving bitmaps public: virtual bool CanSave() const = 0; /// Tries to load the specified file as a bitmap /// Source data the bitmap will be loaded from /// Optional file extension the loaded data had /// /// The bitmap loaded from the specified file data or an empty value if the file format /// is not supported by the codec /// /// /// /// This method should, like CanLoad(), check if the provided data source contains /// an image file of the file format implemented by the codec. If the file format /// doesn't match up, it should return an empty OptionalBitmap instance (that's /// the 'try' part). /// /// /// On any other error (i.e. exception from the data source, corrupted image data, /// unsupported version of the file format, etc.), the codec must throw an exception /// rather than return an empty std::optional<Bitmap> instance. /// /// public: virtual std::optional TryLoad( const VirtualFile &source, const std::string &extensionHint = std::string() ) const = 0; /// Tries to load the specified file into an exciting bitmap /// /// Bitmap matching the exact dimensions of the file to be loaded /// /// Source data the bitmap will be loaded from /// Optional file extension the loaded data had /// True if the codec was able to load the bitmap, false otherwise /// /// /// The behavior of this method should be identical to the TryLoad() method, namely /// it should only return false if the data source seems to contain a different file /// format than is implemented by the codec. If the image is broken or unloadable /// for any other reason, an exception must be thrown. /// /// /// This variant of the load function is useful for game engines that want to load /// images directly into memory provided by their 3D API (they can construct a Bitmap /// that accesses foreign memory, then load into that). This reduces the overhead of /// having to load an image into freshly allocated memory only to copy it over into /// a texture. /// /// public: virtual bool TryReload( Bitmap &exactlyFittingBitmap, const VirtualFile &source, const std::string &extensionHint = std::string() ) const = 0; /// Saves the specified bitmap into a file /// Bitmap that will be saved into a file /// File into which the bitmap will be saved /// /// How much effort (CPU time) should be put into reducing the size of the image. /// /// /// How much image quality should be prioritized over achieving small file sizes. /// public: virtual void Save( const Bitmap &bitmap, VirtualFile &target, float compressionEffortHint = 0.75f, float outputQualityHint = 0.95f ) const = 0; }; // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Pixels::Storage #endif // NUCLEX_PIXELS_STORAGE_BITMAPCODEC_H