Browse Source

Allow copying of ImageWrapper, movement of Image and Trade::ImageData.

pull/278/head
Vladimír Vondruš 13 years ago
parent
commit
8e7156999e
  1. 2
      src/AbstractImage.cpp
  2. 17
      src/AbstractImage.h
  3. 24
      src/Image.h
  4. 9
      src/ImageWrapper.h
  5. 1
      src/Test/CMakeLists.txt
  6. 72
      src/Test/ImageTest.cpp
  7. 24
      src/Trade/ImageData.h
  8. 1
      src/Trade/Test/CMakeLists.txt
  9. 72
      src/Trade/Test/ImageDataTest.cpp

2
src/AbstractImage.cpp

@ -30,8 +30,6 @@
namespace Magnum {
AbstractImage::~AbstractImage() {}
std::size_t AbstractImage::pixelSize(ImageFormat format, ImageType type) {
std::size_t size = 0;
switch(type) {

17
src/AbstractImage.h

@ -46,11 +46,6 @@ functions operating with images). It also possibly needs to be "stackable" to
easily revert the state back.
*/
class MAGNUM_EXPORT AbstractImage {
AbstractImage(const AbstractImage&) = delete;
AbstractImage(AbstractImage&&) = delete;
AbstractImage& operator=(const AbstractImage&) = delete;
AbstractImage& operator=(AbstractImage&&) = delete;
public:
/**
* @brief Pixel size (in bytes)
@ -66,16 +61,13 @@ class MAGNUM_EXPORT AbstractImage {
* @param format Format of pixel data
* @param type Data type of pixel data
*/
explicit AbstractImage(ImageFormat format, ImageType type): _format(format), _type(type) {}
/** @brief Destructor */
virtual ~AbstractImage() = 0;
constexpr explicit AbstractImage(ImageFormat format, ImageType type): _format(format), _type(type) {}
/** @brief Format of pixel data */
ImageFormat format() const { return _format; }
constexpr ImageFormat format() const { return _format; }
/** @brief Data type of pixel data */
ImageType type() const { return _type; }
constexpr ImageType type() const { return _type; }
/**
* @brief Pixel size (in bytes)
@ -84,6 +76,9 @@ class MAGNUM_EXPORT AbstractImage {
*/
std::size_t pixelSize() const { return pixelSize(_format, _type); }
protected:
~AbstractImage() = default;
#ifdef DOXYGEN_GENERATING_OUTPUT
private:
#else

24
src/Image.h

@ -67,6 +67,18 @@ template<UnsignedInt dimensions> class Image: public AbstractImage {
*/
explicit Image(ImageFormat format, ImageType type): AbstractImage(format, type), _data(nullptr) {}
/** @brief Copying is not allowed */
Image(const Image<dimensions>&& other) = delete;
/** @brief Move constructor */
Image(Image<dimensions>&& other) noexcept;
/** @brief Copying is not allowed */
Image<dimensions>& operator=(const Image<dimensions>&& other) = delete;
/** @brief Move assignment */
Image<dimensions>& operator=(Image<dimensions>&& other) noexcept;
/** @brief Destructor */
~Image() { delete[] _data; }
@ -103,6 +115,18 @@ typedef Image<2> Image2D;
/** @brief Three-dimensional image */
typedef Image<3> Image3D;
template<UnsignedInt dimensions> inline Image<dimensions>::Image(Image<dimensions>&& other) noexcept: AbstractImage(std::move(other)), _size(std::move(other._size)), _data(std::move(other._data)) {
other._size = {};
other._data = nullptr;
}
template<UnsignedInt dimensions> inline Image<dimensions>& Image<dimensions>::operator=(Image<dimensions>&& other) noexcept {
AbstractImage::operator=(std::move(other));
std::swap(_size, other._size);
std::swap(_data, other._data);
return *this;
}
}
#endif

9
src/ImageWrapper.h

@ -52,6 +52,7 @@ Interchangeable with Image, BufferImage or Trade::ImageData.
template<UnsignedInt dimensions> class ImageWrapper: public AbstractImage {
public:
const static UnsignedInt Dimensions = dimensions; /**< @brief %Image dimension count */
/**
* @brief Constructor
* @param size %Image size
@ -62,7 +63,7 @@ template<UnsignedInt dimensions> class ImageWrapper: public AbstractImage {
* Note that the image data are not copied on construction, but they
* are deleted on class destruction.
*/
explicit ImageWrapper(const typename DimensionTraits<Dimensions, Int>::VectorType& size, ImageFormat format, ImageType type, void* data): AbstractImage(format, type), _size(size), _data(reinterpret_cast<unsigned char*>(data)) {}
constexpr explicit ImageWrapper(const typename DimensionTraits<Dimensions, Int>::VectorType& size, ImageFormat format, ImageType type, void* data): AbstractImage(format, type), _size(size), _data(reinterpret_cast<unsigned char*>(data)) {}
/**
* @brief Constructor
@ -73,14 +74,14 @@ template<UnsignedInt dimensions> class ImageWrapper: public AbstractImage {
* Data pointer is set to zero, call setData() to fill the image with
* data.
*/
explicit ImageWrapper(const typename DimensionTraits<Dimensions, Int>::VectorType& size, ImageFormat format, ImageType type): AbstractImage(format, type), _size(size), _data(nullptr) {}
constexpr explicit ImageWrapper(const typename DimensionTraits<Dimensions, Int>::VectorType& size, ImageFormat format, ImageType type): AbstractImage(format, type), _size(size), _data(nullptr) {}
/** @brief %Image size */
typename DimensionTraits<Dimensions, Int>::VectorType size() const { return _size; }
constexpr typename DimensionTraits<Dimensions, Int>::VectorType size() const { return _size; }
/** @brief Pointer to raw data */
unsigned char* data() { return _data; }
const unsigned char* data() const { return _data; } /**< @overload */
constexpr const unsigned char* data() const { return _data; } /**< @overload */
/**
* @brief Set image data

1
src/Test/CMakeLists.txt

@ -28,6 +28,7 @@ corrade_add_test(ArrayTest ArrayTest.cpp)
corrade_add_test(ColorTest ColorTest.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(DefaultFramebufferTest DefaultFramebufferTest.cpp LIBRARIES Magnum)
corrade_add_test(FramebufferTest FramebufferTest.cpp LIBRARIES Magnum)
corrade_add_test(ImageTest ImageTest.cpp LIBRARIES Magnum)
corrade_add_test(MeshTest MeshTest.cpp LIBRARIES Magnum)
corrade_add_test(RendererTest RendererTest.cpp LIBRARIES Magnum)
corrade_add_test(ResourceManagerTest ResourceManagerTest.cpp LIBRARIES MagnumTestLib)

72
src/Test/ImageTest.cpp

@ -0,0 +1,72 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <TestSuite/Tester.h>
#include "Image.h"
#include "ImageFormat.h"
namespace Magnum { namespace Test {
class ImageTest: public TestSuite::Tester {
public:
explicit ImageTest();
void moveConstructor();
void moveAssignment();
};
ImageTest::ImageTest() {
addTests({&ImageTest::moveConstructor,
&ImageTest::moveAssignment});
}
void ImageTest::moveConstructor() {
unsigned char* data = new unsigned char[3];
Image2D a({1, 3}, ImageFormat::Red, ImageType::UnsignedByte, data);
Image2D b(std::move(a));
CORRADE_VERIFY(!a.data());
CORRADE_COMPARE(b.format(), ImageFormat::Red);
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte);
CORRADE_COMPARE(b.size(), Vector2i(1, 3));
CORRADE_VERIFY(b.data() == data);
}
void ImageTest::moveAssignment() {
unsigned char* data = new unsigned char[3];
Image2D a({1, 3}, ImageFormat::Red, ImageType::UnsignedByte, data);
Image2D b(ImageFormat::Red, ImageType::UnsignedByte);
b = std::move(a);
CORRADE_VERIFY(!a.data());
CORRADE_COMPARE(b.format(), ImageFormat::Red);
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte);
CORRADE_COMPARE(b.size(), Vector2i(1, 3));
CORRADE_VERIFY(b.data() == data);
}
}}
CORRADE_TEST_MAIN(Magnum::Test::ImageTest)

24
src/Trade/ImageData.h

@ -57,6 +57,18 @@ template<UnsignedInt dimensions> class ImageData: public AbstractImage {
*/
explicit ImageData(const typename DimensionTraits<Dimensions, Int>::VectorType& size, ImageFormat format, ImageType type, void* data): AbstractImage(format, type), _size(size), _data(reinterpret_cast<unsigned char*>(data)) {}
/** @brief Copying is not allowed */
ImageData(const ImageData<dimensions>&& other) = delete;
/** @brief Move constructor */
ImageData(ImageData<dimensions>&& other) noexcept;
/** @brief Copying is not allowed */
ImageData<dimensions>& operator=(const ImageData<dimensions>&& other) = delete;
/** @brief Move assignment */
ImageData<dimensions>& operator=(ImageData<dimensions>&& other) noexcept;
/** @brief Destructor */
~ImageData() { delete[] _data; }
@ -81,6 +93,18 @@ typedef ImageData<2> ImageData2D;
/** @brief Three-dimensional image */
typedef ImageData<3> ImageData3D;
template<UnsignedInt dimensions> inline ImageData<dimensions>::ImageData(ImageData<dimensions>&& other) noexcept: AbstractImage(std::move(other)), _size(std::move(other._size)), _data(std::move(other._data)) {
other._size = {};
other._data = nullptr;
}
template<UnsignedInt dimensions> inline ImageData<dimensions>& ImageData<dimensions>::operator=(ImageData<dimensions>&& other) noexcept {
AbstractImage::operator=(std::move(other));
std::swap(_size, other._size);
std::swap(_data, other._data);
return *this;
}
}}
#endif

1
src/Trade/Test/CMakeLists.txt

@ -29,5 +29,6 @@ include_directories(${CMAKE_CURRENT_BINARY_DIR})
corrade_add_test(TradeAbstractImageConverterTest AbstractImageConverterTest.cpp LIBRARIES Magnum)
corrade_add_test(TradeAbstractImporterTest AbstractImporterTest.cpp LIBRARIES Magnum)
corrade_add_test(TradeImageDataTest ImageDataTest.cpp LIBRARIES Magnum)
corrade_add_test(TradeObjectData2DTest ObjectData2DTest.cpp LIBRARIES Magnum)
corrade_add_test(TradeObjectData3DTest ObjectData3DTest.cpp LIBRARIES Magnum)

72
src/Trade/Test/ImageDataTest.cpp

@ -0,0 +1,72 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013 Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <TestSuite/Tester.h>
#include "ImageFormat.h"
#include "Trade/ImageData.h"
namespace Magnum { namespace Trade { namespace Test {
class ImageDataTest: public TestSuite::Tester {
public:
explicit ImageDataTest();
void moveConstructor();
void moveAssignment();
};
ImageDataTest::ImageDataTest() {
addTests({&ImageDataTest::moveConstructor,
&ImageDataTest::moveAssignment});
}
void ImageDataTest::moveConstructor() {
unsigned char* data = new unsigned char[3];
Trade::ImageData2D a({1, 3}, ImageFormat::Red, ImageType::UnsignedByte, data);
Trade::ImageData2D b(std::move(a));
CORRADE_VERIFY(!a.data());
CORRADE_COMPARE(b.format(), ImageFormat::Red);
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte);
CORRADE_COMPARE(b.size(), Vector2i(1, 3));
CORRADE_VERIFY(b.data() == data);
}
void ImageDataTest::moveAssignment() {
unsigned char* data = new unsigned char[3];
Trade::ImageData2D a({1, 3}, ImageFormat::Red, ImageType::UnsignedByte, data);
Trade::ImageData2D b({}, ImageFormat::Red, ImageType::UnsignedByte, nullptr);
b = std::move(a);
CORRADE_VERIFY(!a.data());
CORRADE_COMPARE(b.format(), ImageFormat::Red);
CORRADE_COMPARE(b.type(), ImageType::UnsignedByte);
CORRADE_COMPARE(b.size(), Vector2i(1, 3));
CORRADE_VERIFY(b.data() == data);
}
}}}
CORRADE_TEST_MAIN(Magnum::Trade::Test::ImageDataTest)
Loading…
Cancel
Save