Browse Source

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.
vectorfields
Vladimír Vondruš 14 years ago
parent
commit
e0470d6b58
  1. 38
      src/AbstractImage.cpp
  2. 79
      src/AbstractImage.h
  3. 18
      src/AbstractTexture.cpp
  4. 18
      src/AbstractTexture.h
  5. 27
      src/BufferedImage.h
  6. 1
      src/CMakeLists.txt
  7. 27
      src/Image.h
  8. 19
      src/Trade/ImageData.h

38
src/AbstractImage.cpp

@ -0,0 +1,38 @@
/*
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 "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;
}
}
}

79
src/AbstractImage.h

@ -0,0 +1,79 @@
#ifndef Magnum_AbstractImage_h
#define Magnum_AbstractImage_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::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

18
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;

18
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)

27
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<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;
template<size_t imageDimensions> class BufferedImage: public AbstractImage {
public:
const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */
@ -45,9 +40,7 @@ template<size_t imageDimensions> 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<GLsizei, Dimensions> dimensions() const { return _dimensions; }
@ -62,15 +55,9 @@ template<size_t imageDimensions> class BufferedImage {
*/
void setDimensions(const Math::Vector<GLsizei, Dimensions>& 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<size_t imageDimensions> 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<GLsizei, Dimensions> _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

1
src/CMakeLists.txt

@ -9,6 +9,7 @@ add_subdirectory(Shaders)
set(Magnum_SRCS
Object.cpp
AbstractImage.cpp
AbstractTexture.cpp
AbstractShaderProgram.cpp
BufferedImage.cpp

27
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<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;
template<size_t imageDimensions> class Image: public AbstractImage {
public:
const static size_t Dimensions = imageDimensions; /**< @brief Image dimension count */
@ -50,20 +45,20 @@ template<size_t imageDimensions> class Image {
* 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) {}
template<class T> inline Image(ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, T* data): AbstractImage(colorFormat, 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
* 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<GLsizei, Dimensions>& dimensions() const { return _dimensions; }
@ -80,12 +75,6 @@ template<size_t imageDimensions> 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<size_t imageDimensions> class Image {
}
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 */
};
@ -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

19
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<size_t imageDimensions> class ImageData {
ImageData<imageDimensions>(const ImageData<imageDimensions>& other) = delete;
ImageData<imageDimensions>(ImageData<imageDimensions>&& other) = delete;
ImageData<imageDimensions>& operator=(const ImageData<imageDimensions>& other) = delete;
ImageData<imageDimensions>& operator=(ImageData<imageDimensions>&& other) = delete;
template<size_t imageDimensions> class ImageData: public AbstractImage {
public:
const static size_t Dimensions = imageDimensions; /**< @brief %Image dimension count */
@ -48,7 +43,7 @@ template<size_t imageDimensions> class ImageData {
* @attention Note that the image data are not copied on construction,
* but they are deleted on class destruction.
*/
template<class T> inline ImageData(AbstractTexture::ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, const T* data): _colorFormat(colorFormat), _type(TypeTraits<typename TypeTraits<T>::TextureType>::glType()), _dimensions(dimensions), _data(reinterpret_cast<const char*>(data)) {}
template<class T> inline ImageData(ColorFormat colorFormat, const Math::Vector<GLsizei, Dimensions>& dimensions, const T* data): AbstractImage(colorFormat, TypeTraits<typename TypeTraits<T>::TextureType>::glType()), _dimensions(dimensions), _data(reinterpret_cast<const char*>(data)) {}
/** @brief Destructor */
inline virtual ~ImageData() { delete[] _data; }
@ -56,18 +51,10 @@ template<size_t imageDimensions> class ImageData {
/** @brief %Image dimensions */
inline const Math::Vector<GLsizei, Dimensions>& 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<GLsizei, Dimensions> _dimensions;
const char* _data;
};

Loading…
Cancel
Save