From 92b4429e2c6c644c9aceb49c5ac8f93b1206ccaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 6 Jul 2012 01:47:21 +0200 Subject: [PATCH] New class ImageWrapper. --- src/BufferedImage.h | 3 +- src/Image.h | 4 +- src/ImageWrapper.h | 120 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+), 3 deletions(-) create mode 100644 src/ImageWrapper.h diff --git a/src/BufferedImage.h b/src/BufferedImage.h index 74cd8b408..c8efa5181 100644 --- a/src/BufferedImage.h +++ b/src/BufferedImage.h @@ -34,7 +34,8 @@ namespace Magnum { @brief %Buffered image 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. +stores image data in client memory, ImageWrapper, or for example with +Trade::ImageData. @requires_gl */ template class BufferedImage: public AbstractImage { diff --git a/src/Image.h b/src/Image.h index ae467d30c..61dcf30a0 100644 --- a/src/Image.h +++ b/src/Image.h @@ -32,8 +32,8 @@ namespace Magnum { @brief %Image Class for storing image data on client memory. Can be replaced with -BufferedImage, which stores image data in GPU memory, or for example with -Trade::ImageData. +ImageWrapper, BufferedImage, which stores image data in GPU memory, or for +example with Trade::ImageData. */ template class Image: public AbstractImage { public: diff --git a/src/ImageWrapper.h b/src/ImageWrapper.h new file mode 100644 index 000000000..fef9a13c0 --- /dev/null +++ b/src/ImageWrapper.h @@ -0,0 +1,120 @@ +#ifndef Magnum_ImageWrapper_h +#define Magnum_ImageWrapper_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::ImageWrapper + */ + +#include "AbstractImage.h" +#include "TypeTraits.h" + +namespace Magnum { + +/** @addtogroup textures + * @{ + */ + +/** +@brief %Image wrapper + +Adds information about dimensions, color components and component type to some +data in memory. + +Unlike Image, this class doesn't delete the data on destruction, so it is +targeted for wrapping data which are either stored in stack/constant memory +(and shouldn't be deleted) or they are managed by someone else and have the +same properties for each frame, such as video stream. Thus it is not possible +to change image properties, only data pointer. + +See also Image, BufferedImage and Trade::ImageData. +*/ +template class ImageWrapper: public AbstractImage { + public: + const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */ + + /** + * @brief Constructor + * @param dimensions %Image dimensions + * @param components Color components. Data type is detected + * from passed data array. + * @param data %Image data with proper size + * + * Note that the image data are not copied on construction, but they + * are deleted on class destruction. + */ + template inline ImageWrapper(const Math::Vector& dimensions, Components components, T* data): AbstractImage(components, TypeTraits::imageType()), _dimensions(dimensions), _data(data) {} + + /** + * @brief Constructor + * @param dimensions %Image dimensions + * @param components Color components + * @param type Data type + * @param data %Image data + * + * Note that the image data are not copied on construction, but they + * are deleted on class destruction. + */ + inline ImageWrapper(const Math::Vector& dimensions, Components components, ComponentType type, GLvoid* data): AbstractImage(components, type), _dimensions(dimensions), _data(reinterpret_cast(data)) {} + + /** + * @brief Constructor + * @param dimensions %Image dimensions + * @param components Color components + * @param type Data type + * + * Dimensions and data pointer are set to zero, call setData() to fill + * the image with data. + */ + inline ImageWrapper(const Math::Vector& dimensions, Components components, ComponentType type): AbstractImage(components, type), _dimensions(dimensions), _data(nullptr) {} + + /** @brief %Image dimensions */ + inline constexpr const Math::Vector& dimensions() const { return _dimensions; } + + /** @brief Pointer to raw data */ + inline constexpr const void* data() const { return _data; } + + /** + * @brief Set image data + * @param data %Image data + * + * Dimensions, color compnents and data type remains the same as + * passed in constructor. The data are not copied nor deleted on + * destruction. + */ + void setData(GLvoid* data) { + _data = reinterpret_cast(data); + } + + protected: + Math::Vector _dimensions; /**< @brief %Image dimensions */ + char* _data; /**< @brief %Image data */ +}; + +/** @brief One-dimensional image wrapper */ +typedef ImageWrapper<1> ImageWrapper1D; + +/** @brief Two-dimensional image wrapper */ +typedef ImageWrapper<2> ImageWrapper2D; + +/** @brief Three-dimensional image wrapper */ +typedef ImageWrapper<3> ImageWrapper3D; + +/*@}*/ + +} + +#endif