#ifndef Magnum_Image_h #define Magnum_Image_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::Image */ #include "AbstractImage.h" 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. See also Image2D, which has additional data updating functions. */ template class Image: public AbstractImage { public: const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */ /** * @brief Constructor * @param colorFormat Color format of passed data. Data size * per color channel is detected from format of passed data array. * @param dimensions %Image dimensions * @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 Image(ColorFormat colorFormat, const Math::Vector& dimensions, T* data): AbstractImage(colorFormat, TypeTraits::imageType()), _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 set to zero, call * setDimensions() and setData() to fill the image with data. */ inline Image(ColorFormat colorFormat, Type type): AbstractImage(colorFormat, type), _data(nullptr) {} /** @brief Destructor */ inline ~Image() { delete[] _data; } /** @brief %Image dimensions */ inline const Math::Vector& dimensions() const { return _dimensions; } /** * @brief Set image dimensions * @param dimensions %Image dimensions * * Saves the dimensions and deletes the internal data array. */ inline void setDimensions(const Math::Vector2& dimensions) { _dimensions = dimensions; delete _data; _data = 0; } /** @brief Pointer to raw data */ inline const void* data() const { return _data; } /** * @brief Set image data * @param data %Image data * * Deletes previous data and replaces them with new. Note that the * data are not copied, but they are deleted on destruction. Also the * type of passed data must be the same as data type passed in * constructor. */ template void setData(const T* data) { if(TypeTraits::imageType() != _type) { Corrade::Utility::Error() << "Image: Passed data have wrong type"; return; } delete _data; _data = reinterpret_cast(data); } protected: Math::Vector _dimensions; /**< @brief %Image dimensions */ char* _data; /**< @brief %Image data */ }; /** @brief One-dimensional image */ typedef Image<1> Image1D; /** @brief Two-dimensional image */ class MAGNUM_EXPORT Image2D: public Image<2> { public: /** * @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 * setDimensions() and setData() to fill the image with data. */ /* doxygen: @copydoc Image::Image doesn't work */ inline Image2D(ColorFormat colorFormat, Type type): Image(colorFormat, type) {} /** * @brief Set image data from current framebuffer * @param offset Offset of the pixamp to read * * Reads pixmap from given offset with already set dimensions. */ void setDataFromFramebuffer(const Math::Vector2& offset); }; /** @brief Three-dimensional image */ typedef Image<3> Image3D; } #endif