mirror of https://github.com/mosra/magnum.git
Browse Source
These classes are meant to be used in the same texture updating functions as Trade::ImageData due to static polymorphism. In addition to Trade::ImageData, which is read-only, these classes support updating the dimensions and data. Image2D and ImageBuffer2D can update the data also from framebuffer.vectorfields
9 changed files with 401 additions and 30 deletions
@ -0,0 +1,25 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "BufferedImage.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
void BufferedImage2D::setDataFromFramebuffer(const Math::Vector2<GLint>& offset) { |
||||
_buffer.bind(Buffer::Target::PixelPack); |
||||
glReadPixels(offset.at(0), offset.at(1), _dimensions.at(0), _dimensions.at(1), static_cast<GLenum>(_colorFormat), static_cast<GLenum>(_type), nullptr); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,136 @@
|
||||
#ifndef Magnum_BufferedImage_h |
||||
#define Magnum_BufferedImage_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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::BufferedImage |
||||
*/ |
||||
|
||||
#include "AbstractTexture.h" |
||||
#include "Buffer.h" |
||||
|
||||
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. See |
||||
also BufferedImage2D, which has additional data updating functions. |
||||
*/ |
||||
template<size_t imageDimensions> class BufferedImage { |
||||
BufferedImage(const BufferedImage<imageDimensions>& other) = delete; |
||||
BufferedImage(BufferedImage<imageDimensions>&& other) = delete; |
||||
BufferedImage& operator=(const BufferedImage<imageDimensions>& other) = delete; |
||||
BufferedImage& operator=(BufferedImage<imageDimensions>&& other) = delete; |
||||
|
||||
public: |
||||
const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */ |
||||
|
||||
/**
|
||||
* @brief Constructor |
||||
* @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() {} |
||||
|
||||
/** @brief %Image dimensions */ |
||||
inline Math::Vector<GLsizei, Dimensions> dimensions() const { return _dimensions; } |
||||
|
||||
/**
|
||||
* @brief Set image dimensions |
||||
* @param dimensions %Image dimensions |
||||
* @param usage %Image buffer usage |
||||
* |
||||
* Saves the dimensions and resizes the buffer to be able to hold |
||||
* given pixmap size. |
||||
*/ |
||||
void setDimensions(const Math::Vector<GLsizei, Dimensions>& dimensions, Buffer::Usage usage) { |
||||
_dimensions = dimensions; |
||||
size_t textureSize = AbstractTexture::pixelSize(AbstractTexture::ColorFormat::RGB, Type::UnsignedByte)*dimensions.product(); |
||||
_buffer.setData(Buffer::Target::PixelPack, textureSize, nullptr, usage); |
||||
} |
||||
|
||||
/** @brief Color format */ |
||||
inline AbstractTexture::ColorFormat colorFormat() const { return _colorFormat; } |
||||
|
||||
/** @brief Data type */ |
||||
inline Type type() const { return _type; } |
||||
|
||||
/**
|
||||
* @brief Data |
||||
* |
||||
* Binds the buffer to @ref Buffer::Target::PixelUnpack "pixel unpack |
||||
* target" and returns nullptr, so it can be used for texture updating |
||||
* functions the same way as Image::data(). |
||||
*/ |
||||
void* data() { |
||||
_buffer.bind(Buffer::Target::PixelUnpack); |
||||
return nullptr; |
||||
} |
||||
|
||||
/**
|
||||
* @brief Set image data |
||||
* @param data %Image data |
||||
* |
||||
* Updates the image buffer with given data. The data are not deleted |
||||
* after filling the buffer. Note that the data must have the right |
||||
* size and type of passed data must be the same as data type passed |
||||
* in constructor. |
||||
*/ |
||||
template<class T> void setData(const T* data) { |
||||
if(TypeTraits<typename TypeTraits<T>::TextureType>::glType() != _type) { |
||||
Corrade::Utility::Error() << "BufferedImage: Passed data have type" << TypeTraits<typename TypeTraits<T>::TextureType>::glType() << "but it should be" << _type; |
||||
return; |
||||
} |
||||
|
||||
size_t textureSize = AbstractTexture::pixelSize(AbstractTexture::ColorFormat::RGB, Type::UnsignedByte)*dimensions.product(); |
||||
_buffer.setSubData(Buffer::Target::PixelPack, 0, textureSize, data); |
||||
} |
||||
|
||||
protected: |
||||
AbstractTexture::ColorFormat _colorFormat; /**< @brief Color format */ |
||||
Type _type; /**< @brief Data type per color channel */ |
||||
Math::Vector<GLsizei, Dimensions> _dimensions; /**< @brief %Image dimensions */ |
||||
Buffer _buffer; /**< @brief %Image buffer */ |
||||
}; |
||||
|
||||
/** @brief One-dimensional buffered image */ |
||||
typedef BufferedImage<1> BufferedImage1D; |
||||
|
||||
/** @brief Two-dimensional buffered image */ |
||||
class BufferedImage2D: public BufferedImage<2> { |
||||
public: |
||||
/** @copydoc BufferedImage::BufferedImage */ |
||||
inline BufferedImage2D(AbstractTexture::ColorFormat colorFormat, Type type): BufferedImage(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<GLint>& offset); |
||||
}; |
||||
|
||||
/** @brief Three-dimensional buffered image */ |
||||
typedef BufferedImage<3> BufferedImage3D; |
||||
|
||||
} |
||||
|
||||
#endif |
||||
@ -0,0 +1,24 @@
|
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "Image.h" |
||||
|
||||
namespace Magnum { |
||||
|
||||
void Image2D::setDataFromFramebuffer(const Math::Vector2<GLint>& offset) { |
||||
glReadPixels(offset.at(0), offset.at(1), _dimensions.at(0), _dimensions.at(1), static_cast<GLenum>(_colorFormat), static_cast<GLenum>(_type), _data); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,141 @@
|
||||
#ifndef Magnum_Image_h |
||||
#define Magnum_Image_h |
||||
/*
|
||||
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||
|
||||
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 "AbstractTexture.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<size_t imageDimensions> class Image { |
||||
Image<imageDimensions>(const Image<imageDimensions>& other) = delete; |
||||
Image<imageDimensions>(Image<imageDimensions>&& other) = delete; |
||||
Image<imageDimensions>& operator=(const Image<imageDimensions>& other) = delete; |
||||
Image<imageDimensions>& operator=(Image<imageDimensions>&& other) = delete; |
||||
|
||||
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<class T> inline Image(AbstractTexture::ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, T* data): _colorFormat(colorFormat), _type(TypeTraits<typename TypeTraits<T>::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 |
||||
* setDimensions() and setData() to fill the image with data. |
||||
*/ |
||||
inline Image(AbstractTexture::ColorFormat colorFormat, Type type): _colorFormat(colorFormat), _type(type), _data(nullptr) {} |
||||
|
||||
/** @brief Destructor */ |
||||
inline virtual ~Image() { delete[] _data; } |
||||
|
||||
/** @brief %Image dimensions */ |
||||
inline const Math::Vector<GLsizei, Dimensions>& 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<GLsizei>& dimensions) { |
||||
_dimensions = dimensions; |
||||
delete _data; |
||||
_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; } |
||||
|
||||
/**
|
||||
* @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<class T> void setData(const T* data) { |
||||
if(TypeTraits<typename TypeTraits<T>::TextureType>::glType() != _type) { |
||||
Corrade::Utility::Error() << "Image: Passed data have type" << TypeTraits<typename TypeTraits<T>::TextureType>::glType() << "but it should be" << _type; |
||||
return; |
||||
} |
||||
|
||||
delete _data; |
||||
_data = reinterpret_cast<const char*>(data); |
||||
} |
||||
|
||||
protected: |
||||
AbstractTexture::ColorFormat _colorFormat; /**< @brief Color format */ |
||||
Type _type; /**< @brief Data type per color channel */ |
||||
Math::Vector<GLsizei, Dimensions> _dimensions; /**< @brief %Image dimensions */ |
||||
char* _data; /**< @brief %Image data */ |
||||
}; |
||||
|
||||
/** @brief One-dimensional image */ |
||||
typedef Image<1> Image1D; |
||||
|
||||
/** @brief Two-dimensional image */ |
||||
class Image2D: public Image<2> { |
||||
public: |
||||
/** @copydoc Image::Image */ |
||||
inline Image2D(AbstractTexture::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<GLint>& offset); |
||||
}; |
||||
|
||||
/** @brief Three-dimensional image */ |
||||
typedef Image<3> Image3D; |
||||
|
||||
} |
||||
|
||||
#endif |
||||
Loading…
Reference in new issue