From e0470d6b5859a983405692926eb8a168a94486ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 2 Apr 2012 01:58:46 +0200 Subject: [PATCH] Non-templated base for Image, BufferedImage and Trade::ImageData. Also moved ColorFormat enum from AbstractTexture there, because it is not publicly used in AbstractTexture anymore. --- src/AbstractImage.cpp | 38 ++++++++++++++++++++ src/AbstractImage.h | 79 +++++++++++++++++++++++++++++++++++++++++ src/AbstractTexture.cpp | 18 ---------- src/AbstractTexture.h | 18 ---------- src/BufferedImage.h | 27 ++++---------- src/CMakeLists.txt | 1 + src/Image.h | 27 ++++---------- src/Trade/ImageData.h | 19 ++-------- 8 files changed, 134 insertions(+), 93 deletions(-) create mode 100644 src/AbstractImage.cpp create mode 100644 src/AbstractImage.h diff --git a/src/AbstractImage.cpp b/src/AbstractImage.cpp new file mode 100644 index 000000000..3406b3315 --- /dev/null +++ b/src/AbstractImage.cpp @@ -0,0 +1,38 @@ +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum 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 + GNU Lesser General Public License version 3 for more details. +*/ + +#include "AbstractImage.h" + +namespace Magnum { + +size_t AbstractImage::pixelSize(ColorFormat format, Type type) { + size_t size = TypeInfo::sizeOf(type); + switch(format) { + case ColorFormat::Red: + return 1*size; + case ColorFormat::RedGreen: + return 2*size; + case ColorFormat::RGB: + case ColorFormat::BGR: + return 3*size; + case ColorFormat::RGBA: + case ColorFormat::BGRA: + return 4*size; + default: + return 0; + } +} + +} diff --git a/src/AbstractImage.h b/src/AbstractImage.h new file mode 100644 index 000000000..c43604174 --- /dev/null +++ b/src/AbstractImage.h @@ -0,0 +1,79 @@ +#ifndef Magnum_AbstractImage_h +#define Magnum_AbstractImage_h +/* + Copyright © 2010, 2011, 2012 Vladimír Vondruš + + This file is part of Magnum. + + Magnum is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License version 3 + only, as published by the Free Software Foundation. + + Magnum 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 + GNU Lesser General Public License version 3 for more details. +*/ + +/** @file + * @brief Class Magnum::AbstractImage + */ + +#include "Magnum.h" +#include "TypeTraits.h" + +namespace Magnum { + +/** +@brief Non-templated base for one-, two- or three-dimensional images + +See Image, BufferedImage, Trade::ImageData documentation for more information. +*/ +class AbstractImage { + AbstractImage(const AbstractImage& other) = delete; + AbstractImage(AbstractImage&& other) = delete; + AbstractImage& operator=(const AbstractImage& other) = delete; + AbstractImage& operator=(AbstractImage&& other) = delete; + + public: + /** @brief Color format */ + enum class ColorFormat: GLenum { + Red = GL_RED, /**< One-component (red channel) */ + RedGreen = GL_RG, /**< Two-component (red and green channel) */ + RGB = GL_RGB, /**< Three-component (RGB) */ + RGBA = GL_RGBA, /**< Four-component (RGBA) */ + BGR = GL_BGR, /**< Three-component (BGR) */ + BGRA = GL_BGRA /**< Four-component (BGRA) */ + }; + + /** + * @brief Pixel size (in bytes) + * @param format Color format + * @param type Data type per color channel + */ + static size_t pixelSize(ColorFormat format, Type type); + + /** + * @brief Constructor + * @param colorFormat Color format of passed data + * @param type %Image data type + */ + inline AbstractImage(ColorFormat colorFormat, Type type): _colorFormat(colorFormat), _type(type) {} + + /** @brief Destructor */ + inline virtual ~AbstractImage() {} + + /** @brief Color format */ + inline ColorFormat colorFormat() const { return _colorFormat; } + + /** @brief Data type */ + inline Type type() const { return _type; } + + protected: + ColorFormat _colorFormat; /**< @brief Color format */ + Type _type; /**< @brief Data type per color channel */ +}; + +} + +#endif diff --git a/src/AbstractTexture.cpp b/src/AbstractTexture.cpp index 57ee1c9bc..dce557eeb 100644 --- a/src/AbstractTexture.cpp +++ b/src/AbstractTexture.cpp @@ -46,24 +46,6 @@ static_assert((filter_or(NearestNeighbor, BaseLevel) == GL_NEAREST) && #undef filter_or #endif -size_t AbstractTexture::pixelSize(ColorFormat format, Type type) { - size_t size = TypeInfo::sizeOf(type); - switch(format) { - case ColorFormat::Red: - return 1*size; - case ColorFormat::RedGreen: - return 2*size; - case ColorFormat::RGB: - case ColorFormat::BGR: - return 3*size; - case ColorFormat::RGBA: - case ColorFormat::BGRA: - return 4*size; - default: - return 0; - } -} - void AbstractTexture::setMinificationFilter(Filter filter, Mipmap mipmap) { /* Only base mip level is supported on rectangle textures */ if(target == GL_TEXTURE_RECTANGLE) mipmap = Mipmap::BaseLevel; diff --git a/src/AbstractTexture.h b/src/AbstractTexture.h index 0ad26b57d..536dc940b 100644 --- a/src/AbstractTexture.h +++ b/src/AbstractTexture.h @@ -20,7 +20,6 @@ */ #include "Magnum.h" -#include "TypeTraits.h" namespace Magnum { @@ -316,23 +315,6 @@ class MAGNUM_EXPORT AbstractTexture { /*@}*/ - /** @brief Color format */ - enum class ColorFormat: GLenum { - Red = GL_RED, /**< One-component (red channel) */ - RedGreen = GL_RG, /**< Two-component (red and green channel) */ - RGB = GL_RGB, /**< Three-component (RGB) */ - RGBA = GL_RGBA, /**< Four-component (RGBA) */ - BGR = GL_BGR, /**< Three-component (BGR) */ - BGRA = GL_BGRA /**< Four-component (BGRA) */ - }; - - /** - * @brief Pixel size (in bytes) - * @param format Color format - * @param type Data type per color channel - */ - static size_t pixelSize(ColorFormat format, Type type); - /** * @brief Constructor * @param layer %Texture layer (number between 0 and 31) diff --git a/src/BufferedImage.h b/src/BufferedImage.h index a1f94f95b..586441d51 100644 --- a/src/BufferedImage.h +++ b/src/BufferedImage.h @@ -19,7 +19,7 @@ * @brief Class Magnum::BufferedImage */ -#include "AbstractTexture.h" +#include "AbstractImage.h" #include "Buffer.h" namespace Magnum { @@ -31,12 +31,7 @@ Class for storing image data in GPU memory. Can be replaced with Image, which stores image data in client memory, or for example with Trade::ImageData. See also BufferedImage2D, which has additional data updating functions. */ -template class BufferedImage { - BufferedImage(const BufferedImage& other) = delete; - BufferedImage(BufferedImage&& other) = delete; - BufferedImage& operator=(const BufferedImage& other) = delete; - BufferedImage& operator=(BufferedImage&& other) = delete; - +template class BufferedImage: public AbstractImage { public: const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */ @@ -45,9 +40,7 @@ template class BufferedImage { * @param colorFormat Color format of the data. * @param type Data type per color channel */ - BufferedImage(AbstractTexture::ColorFormat colorFormat, Type type): _colorFormat(colorFormat), _type(type), _buffer(Buffer::Target::PixelPack) {} - - inline virtual ~BufferedImage() {} + BufferedImage(ColorFormat colorFormat, Type type): AbstractImage(colorFormat, type), _buffer(Buffer::Target::PixelPack) {} /** @brief %Image dimensions */ inline Math::Vector dimensions() const { return _dimensions; } @@ -62,15 +55,9 @@ template class BufferedImage { */ void setDimensions(const Math::Vector& dimensions, Buffer::Usage usage) { _dimensions = dimensions; - _buffer.setData(Buffer::Target::PixelPack, AbstractTexture::pixelSize(_colorFormat, _type)*dimensions.product(), nullptr, usage); + _buffer.setData(Buffer::Target::PixelPack, pixelSize(_colorFormat, _type)*dimensions.product(), nullptr, usage); } - /** @brief Color format */ - inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; } - - /** @brief Data type */ - inline Type type() const { return _type; } - /** * @brief Data * @@ -98,12 +85,10 @@ template class BufferedImage { return; } - _buffer.setSubData(Buffer::Target::PixelPack, 0, AbstractTexture::pixelSize(_colorFormat, _type)*_dimensions.product(), data); + _buffer.setSubData(Buffer::Target::PixelPack, 0, pixelSize(_colorFormat, _type)*_dimensions.product(), data); } protected: - AbstractTexture::ColorFormat _colorFormat; /**< @brief Color format */ - Type _type; /**< @brief Data type per color channel */ Math::Vector _dimensions; /**< @brief %Image dimensions */ Buffer _buffer; /**< @brief %Image buffer */ }; @@ -120,7 +105,7 @@ class MAGNUM_EXPORT BufferedImage2D: public BufferedImage<2> { * @param type Data type per color channel */ /* doxygen: @copydoc BufferedImage::BufferedImage doesn't work */ - inline BufferedImage2D(AbstractTexture::ColorFormat colorFormat, Type type): BufferedImage(colorFormat, type) {} + inline BufferedImage2D(ColorFormat colorFormat, Type type): BufferedImage(colorFormat, type) {} /** * @brief Set image data from current framebuffer diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5eaaa6119..23a8e54ed 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,6 +9,7 @@ add_subdirectory(Shaders) set(Magnum_SRCS Object.cpp + AbstractImage.cpp AbstractTexture.cpp AbstractShaderProgram.cpp BufferedImage.cpp diff --git a/src/Image.h b/src/Image.h index be2f3984e..172c854bb 100644 --- a/src/Image.h +++ b/src/Image.h @@ -19,7 +19,7 @@ * @brief Class Magnum::Image */ -#include "AbstractTexture.h" +#include "AbstractImage.h" namespace Magnum { @@ -31,12 +31,7 @@ BufferedImage, which stores image data in GPU memory, or for example with Trade::ImageData. See also Image2D, which has additional data updating functions. */ -template class Image { - Image(const Image& other) = delete; - Image(Image&& other) = delete; - Image& operator=(const Image& other) = delete; - Image& operator=(Image&& other) = delete; - +template class Image: public AbstractImage { public: const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */ @@ -50,20 +45,20 @@ template class Image { * Note that the image data are not copied on construction, but they * are deleted on class destruction. */ - template inline Image(AbstractTexture::ColorFormat colorFormat, const Math::Vector& dimensions, T* data): _colorFormat(colorFormat), _type(TypeTraits::TextureType>::glType()), _dimensions(dimensions), _data(data) {} + template inline Image(ColorFormat colorFormat, const Math::Vector& dimensions, T* data): AbstractImage(colorFormat, TypeTraits::TextureType>::glType()), _dimensions(dimensions), _data(data) {} /** * @brief Constructor * @param colorFormat Color format of passed data * @param type Data type per color channel * - * Dimensions and data pointer are are set to zero, call + * Dimensions and data pointer are set to zero, call * setDimensions() and setData() to fill the image with data. */ - inline Image(AbstractTexture::ColorFormat colorFormat, Type type): _colorFormat(colorFormat), _type(type), _data(nullptr) {} + inline Image(ColorFormat colorFormat, Type type): AbstractImage(colorFormat, type), _data(nullptr) {} /** @brief Destructor */ - inline virtual ~Image() { delete[] _data; } + inline ~Image() { delete[] _data; } /** @brief %Image dimensions */ inline const Math::Vector& dimensions() const { return _dimensions; } @@ -80,12 +75,6 @@ template class Image { _data = 0; } - /** @brief Color format */ - inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; } - - /** @brief Data type */ - inline Type type() const { return _type; } - /** @brief Pointer to raw data */ inline const void* data() const { return _data; } @@ -109,8 +98,6 @@ template class Image { } protected: - AbstractTexture::ColorFormat _colorFormat; /**< @brief Color format */ - Type _type; /**< @brief Data type per color channel */ Math::Vector _dimensions; /**< @brief %Image dimensions */ char* _data; /**< @brief %Image data */ }; @@ -130,7 +117,7 @@ class MAGNUM_EXPORT Image2D: public Image<2> { * setDimensions() and setData() to fill the image with data. */ /* doxygen: @copydoc Image::Image doesn't work */ - inline Image2D(AbstractTexture::ColorFormat colorFormat, Type type): Image(colorFormat, type) {} + inline Image2D(ColorFormat colorFormat, Type type): Image(colorFormat, type) {} /** * @brief Set image data from current framebuffer diff --git a/src/Trade/ImageData.h b/src/Trade/ImageData.h index 831e57fec..2f32a9a50 100644 --- a/src/Trade/ImageData.h +++ b/src/Trade/ImageData.h @@ -19,7 +19,7 @@ * @brief Class Magnum::Trade::ImageData */ -#include "AbstractTexture.h" +#include "AbstractImage.h" namespace Magnum { namespace Trade { @@ -29,12 +29,7 @@ namespace Magnum { namespace Trade { Provides access to image data and additional information about data type and dimensions. Can be used in the same situations as Image and BufferedImage. */ -template class ImageData { - ImageData(const ImageData& other) = delete; - ImageData(ImageData&& other) = delete; - ImageData& operator=(const ImageData& other) = delete; - ImageData& operator=(ImageData&& other) = delete; - +template class ImageData: public AbstractImage { public: const static size_t Dimensions = imageDimensions; /**< @brief %Image dimension count */ @@ -48,7 +43,7 @@ template class ImageData { * @attention Note that the image data are not copied on construction, * but they are deleted on class destruction. */ - template inline ImageData(AbstractTexture::ColorFormat colorFormat, const Math::Vector& dimensions, const T* data): _colorFormat(colorFormat), _type(TypeTraits::TextureType>::glType()), _dimensions(dimensions), _data(reinterpret_cast(data)) {} + template inline ImageData(ColorFormat colorFormat, const Math::Vector& dimensions, const T* data): AbstractImage(colorFormat, TypeTraits::TextureType>::glType()), _dimensions(dimensions), _data(reinterpret_cast(data)) {} /** @brief Destructor */ inline virtual ~ImageData() { delete[] _data; } @@ -56,18 +51,10 @@ template class ImageData { /** @brief %Image dimensions */ inline const Math::Vector& dimensions() const { return _dimensions; } - /** @brief Color format */ - inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; } - - /** @brief Data type */ - inline Type type() const { return _type; } - /** @brief Pointer to raw data */ inline const void* data() const { return _data; } private: - AbstractTexture::ColorFormat _colorFormat; - Type _type; Math::Vector _dimensions; const char* _data; };