Browse Source

Trade: make AbstractImageConverter API non-const.

In the particular case of AnyImageConverter the function needs to access
manager and load/instantiate plugin using it, which is non-const
operation. More generally, it puts unnecessary restrictions on what the
plugin can and cannot do.
pull/107/head
Vladimír Vondruš 11 years ago
parent
commit
21fc790632
  1. 24
      src/Magnum/Trade/AbstractImageConverter.cpp
  2. 60
      src/Magnum/Trade/AbstractImageConverter.h
  3. 2
      src/Magnum/Trade/Test/AbstractImageConverterTest.cpp
  4. 2
      src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp
  5. 2
      src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h

24
src/Magnum/Trade/AbstractImageConverter.cpp

@ -39,62 +39,62 @@ AbstractImageConverter::AbstractImageConverter(PluginManager::Manager<AbstractIm
AbstractImageConverter::AbstractImageConverter(PluginManager::AbstractManager& manager, std::string plugin): PluginManager::AbstractManagingPlugin<AbstractImageConverter>{manager, std::move(plugin)} {} AbstractImageConverter::AbstractImageConverter(PluginManager::AbstractManager& manager, std::string plugin): PluginManager::AbstractManagingPlugin<AbstractImageConverter>{manager, std::move(plugin)} {}
std::optional<Image2D> AbstractImageConverter::exportToImage(const ImageView2D& image) const { std::optional<Image2D> AbstractImageConverter::exportToImage(const ImageView2D& image) {
CORRADE_ASSERT(features() & Feature::ConvertImage, CORRADE_ASSERT(features() & Feature::ConvertImage,
"Trade::AbstractImageConverter::exportToImage(): feature not supported", {}); "Trade::AbstractImageConverter::exportToImage(): feature not supported", {});
return doExportToImage(image); return doExportToImage(image);
} }
std::optional<Image2D> AbstractImageConverter::doExportToImage(const ImageView2D&) const { std::optional<Image2D> AbstractImageConverter::doExportToImage(const ImageView2D&) {
CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToImage(): feature advertised but not implemented", {}); CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToImage(): feature advertised but not implemented", {});
return std::nullopt; return std::nullopt;
} }
std::optional<CompressedImage2D> AbstractImageConverter::exportToCompressedImage(const ImageView2D& image) const { std::optional<CompressedImage2D> AbstractImageConverter::exportToCompressedImage(const ImageView2D& image) {
CORRADE_ASSERT(features() & Feature::ConvertCompressedImage, CORRADE_ASSERT(features() & Feature::ConvertCompressedImage,
"Trade::AbstractImageConverter::exportToCompressedImage(): feature not supported", {}); "Trade::AbstractImageConverter::exportToCompressedImage(): feature not supported", {});
return doExportToCompressedImage(image); return doExportToCompressedImage(image);
} }
std::optional<CompressedImage2D> AbstractImageConverter::doExportToCompressedImage(const ImageView2D&) const { std::optional<CompressedImage2D> AbstractImageConverter::doExportToCompressedImage(const ImageView2D&) {
CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToCompressedImage(): feature advertised but not implemented", {}); CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToCompressedImage(): feature advertised but not implemented", {});
return std::nullopt; return std::nullopt;
} }
Containers::Array<char> AbstractImageConverter::exportToData(const ImageView2D& image) const { Containers::Array<char> AbstractImageConverter::exportToData(const ImageView2D& image) {
CORRADE_ASSERT(features() & Feature::ConvertData, CORRADE_ASSERT(features() & Feature::ConvertData,
"Trade::AbstractImageConverter::exportToData(): feature not supported", nullptr); "Trade::AbstractImageConverter::exportToData(): feature not supported", nullptr);
return doExportToData(image); return doExportToData(image);
} }
Containers::Array<char> AbstractImageConverter::doExportToData(const ImageView2D&) const { Containers::Array<char> AbstractImageConverter::doExportToData(const ImageView2D&) {
CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToData(): feature advertised but not implemented", nullptr); CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToData(): feature advertised but not implemented", nullptr);
return nullptr; return nullptr;
} }
Containers::Array<char> AbstractImageConverter::exportToData(const CompressedImageView2D& image) const { Containers::Array<char> AbstractImageConverter::exportToData(const CompressedImageView2D& image) {
CORRADE_ASSERT(features() & Feature::ConvertCompressedData, CORRADE_ASSERT(features() & Feature::ConvertCompressedData,
"Trade::AbstractImageConverter::exportToData(): feature not supported", nullptr); "Trade::AbstractImageConverter::exportToData(): feature not supported", nullptr);
return doExportToData(image); return doExportToData(image);
} }
Containers::Array<char> AbstractImageConverter::doExportToData(const CompressedImageView2D&) const { Containers::Array<char> AbstractImageConverter::doExportToData(const CompressedImageView2D&) {
CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToData(): feature advertised but not implemented", nullptr); CORRADE_ASSERT(false, "Trade::AbstractImageConverter::exportToData(): feature advertised but not implemented", nullptr);
return nullptr; return nullptr;
} }
bool AbstractImageConverter::exportToFile(const ImageView2D& image, const std::string& filename) const { bool AbstractImageConverter::exportToFile(const ImageView2D& image, const std::string& filename) {
CORRADE_ASSERT(features() & Feature::ConvertFile, CORRADE_ASSERT(features() & Feature::ConvertFile,
"Trade::AbstractImageConverter::exportToFile(): feature not supported", {}); "Trade::AbstractImageConverter::exportToFile(): feature not supported", {});
return doExportToFile(image, filename); return doExportToFile(image, filename);
} }
bool AbstractImageConverter::doExportToFile(const ImageView2D& image, const std::string& filename) const { bool AbstractImageConverter::doExportToFile(const ImageView2D& image, const std::string& filename) {
CORRADE_ASSERT(features() & Feature::ConvertData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false); CORRADE_ASSERT(features() & Feature::ConvertData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false);
const auto data = doExportToData(image); const auto data = doExportToData(image);
@ -109,14 +109,14 @@ bool AbstractImageConverter::doExportToFile(const ImageView2D& image, const std:
return true; return true;
} }
bool AbstractImageConverter::exportToFile(const CompressedImageView2D& image, const std::string& filename) const { bool AbstractImageConverter::exportToFile(const CompressedImageView2D& image, const std::string& filename) {
CORRADE_ASSERT(features() & Feature::ConvertCompressedFile, CORRADE_ASSERT(features() & Feature::ConvertCompressedFile,
"Trade::AbstractImageConverter::exportToFile(): feature not supported", {}); "Trade::AbstractImageConverter::exportToFile(): feature not supported", {});
return doExportToFile(image, filename); return doExportToFile(image, filename);
} }
bool AbstractImageConverter::doExportToFile(const CompressedImageView2D& image, const std::string& filename) const { bool AbstractImageConverter::doExportToFile(const CompressedImageView2D& image, const std::string& filename) {
CORRADE_ASSERT(features() & Feature::ConvertCompressedData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false); CORRADE_ASSERT(features() & Feature::ConvertCompressedData, "Trade::AbstractImageConverter::exportToFile(): not implemented", false);
const auto data = doExportToData(image); const auto data = doExportToData(image);

60
src/Magnum/Trade/AbstractImageConverter.h

@ -57,10 +57,10 @@ checked by the implementation:
is supported. is supported.
- Function @ref doExportToCompressedImage() is called only if - Function @ref doExportToCompressedImage() is called only if
@ref Feature::ConvertCompressedImage is supported. @ref Feature::ConvertCompressedImage is supported.
- Function @ref doExportToData(const ImageView2D&) const is called only if - Function @ref doExportToData(const ImageView2D&) is called only if
@ref Feature::ConvertData is supported. @ref Feature::ConvertData is supported.
- Function @ref doExportToData(const CompressedImageView2D&) const is called - Function @ref doExportToData(const CompressedImageView2D&) is called only
only if @ref Feature::ConvertCompressedData is supported. if @ref Feature::ConvertCompressedData is supported.
Plugin interface string is `"cz.mosra.magnum.Trade.AbstractImageConverter/0.2.1"`. Plugin interface string is `"cz.mosra.magnum.Trade.AbstractImageConverter/0.2.1"`.
*/ */
@ -80,21 +80,21 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
/** Conversion to compressed image with @ref exportToCompressedImage() */ /** Conversion to compressed image with @ref exportToCompressedImage() */
ConvertCompressedImage = 1 << 1, ConvertCompressedImage = 1 << 1,
/** Exporting to file with @ref exportToFile(const ImageView2D&, const std::string&) const */ /** Exporting to file with @ref exportToFile(const ImageView2D&, const std::string&) */
ConvertFile = 1 << 2, ConvertFile = 1 << 2,
/** Exporting to file with @ref exportToFile(const CompressedImageView2D&, const std::string&) const */ /** Exporting to file with @ref exportToFile(const CompressedImageView2D&, const std::string&) */
ConvertCompressedFile = 1 << 3, ConvertCompressedFile = 1 << 3,
/** /**
* Exporting to raw data with @ref exportToData(const ImageView2D&) const. * Exporting to raw data with @ref exportToData(const ImageView2D&).
* Implies @ref Feature::ConvertFile. * Implies @ref Feature::ConvertFile.
*/ */
ConvertData = ConvertFile|(1 << 4), ConvertData = ConvertFile|(1 << 4),
/** /**
* Exporting compressed image to raw data with * Exporting compressed image to raw data with
* @ref exportToData(const CompressedImageView2D&) const. Implies * @ref exportToData(const CompressedImageView2D&). Implies
* @ref Feature::ConvertCompressedFile. * @ref Feature::ConvertCompressedFile.
*/ */
ConvertCompressedData = ConvertCompressedFile|(1 << 4) ConvertCompressedData = ConvertCompressedFile|(1 << 4)
@ -126,7 +126,7 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
* converted image on success, `std::nullopt` otherwise. * converted image on success, `std::nullopt` otherwise.
* @see @ref features(), @ref exportToData(), @ref exportToFile() * @see @ref features(), @ref exportToData(), @ref exportToFile()
*/ */
std::optional<Image2D> exportToImage(const ImageView2D& image) const; std::optional<Image2D> exportToImage(const ImageView2D& image);
/** /**
* @brief Convert image to compressed format * @brief Convert image to compressed format
@ -135,7 +135,7 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
* Returns converted image on success, `std::nullopt` otherwise. * Returns converted image on success, `std::nullopt` otherwise.
* @see @ref features(), @ref exportToData(), @ref exportToFile() * @see @ref features(), @ref exportToData(), @ref exportToFile()
*/ */
std::optional<CompressedImage2D> exportToCompressedImage(const ImageView2D& image) const; std::optional<CompressedImage2D> exportToCompressedImage(const ImageView2D& image);
/** /**
* @brief Export image to raw data * @brief Export image to raw data
@ -143,9 +143,9 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
* Available only if @ref Feature::ConvertData is supported. Returns * Available only if @ref Feature::ConvertData is supported. Returns
* data on success, zero-sized array otherwise. * data on success, zero-sized array otherwise.
* @see @ref features(), @ref exportToImage(), * @see @ref features(), @ref exportToImage(),
* @ref exportToFile(const ImageView2D&, const std::string&) const * @ref exportToFile(const ImageView2D&, const std::string&)
*/ */
Containers::Array<char> exportToData(const ImageView2D& image) const; Containers::Array<char> exportToData(const ImageView2D& image);
/** /**
* @brief Export compressed image to raw data * @brief Export compressed image to raw data
@ -153,9 +153,9 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
* Available only if @ref Feature::ConvertCompressedData is supported. * Available only if @ref Feature::ConvertCompressedData is supported.
* Returns data on success, zero-sized array otherwise. * Returns data on success, zero-sized array otherwise.
* @see @ref features(), @ref exportToCompressedImage(), * @see @ref features(), @ref exportToCompressedImage(),
* @ref exportToFile(const CompressedImageView2D&, const std::string&) const * @ref exportToFile(const CompressedImageView2D&, const std::string&)
*/ */
Containers::Array<char> exportToData(const CompressedImageView2D& image) const; Containers::Array<char> exportToData(const CompressedImageView2D& image);
/** /**
* @brief Export image to file * @brief Export image to file
@ -164,9 +164,9 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
* @ref Feature::ConvertData is supported. Returns `true` on success, * @ref Feature::ConvertData is supported. Returns `true` on success,
* `false` otherwise. * `false` otherwise.
* @see @ref features(), @ref exportToImage(), * @see @ref features(), @ref exportToImage(),
* @ref exportToData(const ImageView2D&) const * @ref exportToData(const ImageView2D&)
*/ */
bool exportToFile(const ImageView2D& image, const std::string& filename) const; bool exportToFile(const ImageView2D& image, const std::string& filename);
/** /**
* @brief Export compressed image to file * @brief Export compressed image to file
@ -175,9 +175,9 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
* @ref Feature::ConvertCompressedData is supported. Returns `true` on * @ref Feature::ConvertCompressedData is supported. Returns `true` on
* success, `false` otherwise. * success, `false` otherwise.
* @see @ref features(), @ref exportToCompressedImage(), * @see @ref features(), @ref exportToCompressedImage(),
* @ref exportToData(const CompressedImageView2D&) const * @ref exportToData(const CompressedImageView2D&)
*/ */
bool exportToFile(const CompressedImageView2D& image, const std::string& filename) const; bool exportToFile(const CompressedImageView2D& image, const std::string& filename);
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
private: private:
@ -188,34 +188,34 @@ class MAGNUM_EXPORT AbstractImageConverter: public PluginManager::AbstractManagi
virtual Features doFeatures() const = 0; virtual Features doFeatures() const = 0;
/** @brief Implementation of @ref exportToImage() */ /** @brief Implementation of @ref exportToImage() */
virtual std::optional<Image2D> doExportToImage(const ImageView2D& image) const; virtual std::optional<Image2D> doExportToImage(const ImageView2D& image);
/** @brief Implementation of @ref exportToCompressedImage() */ /** @brief Implementation of @ref exportToCompressedImage() */
virtual std::optional<CompressedImage2D> doExportToCompressedImage(const ImageView2D& image) const; virtual std::optional<CompressedImage2D> doExportToCompressedImage(const ImageView2D& image);
/** @brief Implementation of @ref exportToData(const ImageView2D&) const */ /** @brief Implementation of @ref exportToData(const ImageView2D&) */
virtual Containers::Array<char> doExportToData(const ImageView2D& image) const; virtual Containers::Array<char> doExportToData(const ImageView2D& image);
/** @brief Implementation of @ref exportToData(const CompressedImageView2D&) const */ /** @brief Implementation of @ref exportToData(const CompressedImageView2D&) */
virtual Containers::Array<char> doExportToData(const CompressedImageView2D& image) const; virtual Containers::Array<char> doExportToData(const CompressedImageView2D& image);
/** /**
* @brief Implementation of @ref exportToFile(const ImageView2D&, const std::string&) const * @brief Implementation of @ref exportToFile(const ImageView2D&, const std::string&)
* *
* If @ref Feature::ConvertData is supported, default implementation * If @ref Feature::ConvertData is supported, default implementation
* calls @ref doExportToData(const ImageView2D&) const and saves the * calls @ref doExportToData(const ImageView2D&) and saves the result
* result to given file. * to given file.
*/ */
virtual bool doExportToFile(const ImageView2D& image, const std::string& filename) const; virtual bool doExportToFile(const ImageView2D& image, const std::string& filename);
/** /**
* @brief Implementation of @ref exportToFile(const CompressedImageView2D&, const std::string&) const * @brief Implementation of @ref exportToFile(const CompressedImageView2D&, const std::string&)
* *
* If @ref Feature::ConvertCompressedData is supported, default * If @ref Feature::ConvertCompressedData is supported, default
* implementation calls @ref doExportToData(const CompressedImageView2D&) const * implementation calls @ref doExportToData(const CompressedImageView2D&)
* and saves the result to given file. * and saves the result to given file.
*/ */
virtual bool doExportToFile(const CompressedImageView2D& image, const std::string& filename) const; virtual bool doExportToFile(const CompressedImageView2D& image, const std::string& filename);
}; };
CORRADE_ENUMSET_OPERATORS(AbstractImageConverter::Features) CORRADE_ENUMSET_OPERATORS(AbstractImageConverter::Features)

2
src/Magnum/Trade/Test/AbstractImageConverterTest.cpp

@ -52,7 +52,7 @@ void AbstractImageConverterTest::exportToFile() {
private: private:
Features doFeatures() const override { return Feature::ConvertData; } Features doFeatures() const override { return Feature::ConvertData; }
Containers::Array<char> doExportToData(const ImageView2D& image) const override { Containers::Array<char> doExportToData(const ImageView2D& image) override {
return Containers::Array<char>::from(char(image.size().x()), char(image.size().y())); return Containers::Array<char>::from(char(image.size().x()), char(image.size().y()));
}; };
}; };

2
src/MagnumPlugins/TgaImageConverter/TgaImageConverter.cpp

@ -45,7 +45,7 @@ TgaImageConverter::TgaImageConverter(PluginManager::AbstractManager& manager, st
auto TgaImageConverter::doFeatures() const -> Features { return Feature::ConvertData; } auto TgaImageConverter::doFeatures() const -> Features { return Feature::ConvertData; }
Containers::Array<char> TgaImageConverter::doExportToData(const ImageView2D& image) const { Containers::Array<char> TgaImageConverter::doExportToData(const ImageView2D& image) {
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
if(image.storage().swapBytes()) { if(image.storage().swapBytes()) {
Error() << "Trade::TgaImageConverter::exportToData(): pixel byte swap is not supported"; Error() << "Trade::TgaImageConverter::exportToData(): pixel byte swap is not supported";

2
src/MagnumPlugins/TgaImageConverter/TgaImageConverter.h

@ -74,7 +74,7 @@ class MAGNUM_TGAIMAGECONVERTER_EXPORT TgaImageConverter: public AbstractImageCon
private: private:
Features MAGNUM_TGAIMAGECONVERTER_LOCAL doFeatures() const override; Features MAGNUM_TGAIMAGECONVERTER_LOCAL doFeatures() const override;
Containers::Array<char> MAGNUM_TGAIMAGECONVERTER_LOCAL doExportToData(const ImageView2D& image) const override; Containers::Array<char> MAGNUM_TGAIMAGECONVERTER_LOCAL doExportToData(const ImageView2D& image) override;
}; };
}} }}

Loading…
Cancel
Save