diff --git a/libs/gfx/include/psemek/gfx/pixmap.hpp b/libs/gfx/include/psemek/gfx/pixmap.hpp index 7ae7fffa..c0624175 100644 --- a/libs/gfx/include/psemek/gfx/pixmap.hpp +++ b/libs/gfx/include/psemek/gfx/pixmap.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -8,50 +9,7 @@ namespace psemek::gfx { template - struct basic_pixmap - { - using pixel_type = Pixel; - - basic_pixmap() = default; - basic_pixmap(std::size_t width, std::size_t height); - basic_pixmap(std::size_t width, std::size_t height, Pixel value); - basic_pixmap(std::size_t width, std::size_t height, std::unique_ptr data); - basic_pixmap(basic_pixmap &&); - - basic_pixmap(basic_pixmap const &) = delete; - - basic_pixmap & operator = (basic_pixmap &&); - basic_pixmap & operator = (basic_pixmap const &) = delete; - - std::size_t width() const { return width_; } - std::size_t height() const { return height_; } - - void resize(std::size_t width, std::size_t height); - void resize(std::size_t width, std::size_t height, Pixel value); - - Pixel * operator[](std::size_t row); - Pixel const * operator[](std::size_t row) const; - - Pixel * data() { return data_.get(); } - Pixel const * data() const { return data_.get(); } - - Pixel & operator()(std::size_t column, std::size_t row); - Pixel const & operator()(std::size_t column, std::size_t row) const; - - std::unique_ptr release(); - void reset(std::size_t width, std::size_t height, std::unique_ptr data); - - bool empty() const; - - void clear(); - - void fill(Pixel value); - - private: - std::size_t width_ = 0; - std::size_t height_ = 0; - std::unique_ptr data_; - }; + using basic_pixmap = util::array; using pixmap_monochrome = basic_pixmap; using pixmap_rgb = basic_pixmap; @@ -65,139 +23,4 @@ namespace psemek::gfx void write_pgm(pixmap_monochrome const & p, std::ostream & os); void write_ppm(pixmap_rgb const & p, std::ostream & os); - template - basic_pixmap::basic_pixmap(std::size_t width, std::size_t height) - : width_(width) - , height_(height) - { - data_.reset(new Pixel [width_ * height_]); - } - - template - basic_pixmap::basic_pixmap(std::size_t width, std::size_t height, Pixel value) - : width_(width) - , height_(height) - { - data_.reset(new Pixel [width_ * height_]); - fill(value); - } - - template - basic_pixmap::basic_pixmap(std::size_t width, std::size_t height, std::unique_ptr data) - : width_(width) - , height_(height) - , data_(std::move(data)) - {} - - template - basic_pixmap::basic_pixmap(basic_pixmap && other) - : width_(other.width_) - , height_(other.height_) - , data_(other.release()) - {} - - template - basic_pixmap & basic_pixmap::operator = (basic_pixmap && other) - { - if (this == &other) return *this; - - width_ = other.width_; - height_ = other.height_; - data_ = other.release(); - - return *this; - } - - template - void basic_pixmap::resize(std::size_t width, std::size_t height) - { - data_.reset(new Pixel[width * height]); - width_ = width; - height_ = height; - } - - template - void basic_pixmap::resize(std::size_t width, std::size_t height, Pixel value) - { - data_.reset(new Pixel[width * height]); - width_ = width; - height_ = height; - fill(value); - } - - template - Pixel * basic_pixmap::operator[](std::size_t row) - { - return data() + row * width(); - } - - template - Pixel const * basic_pixmap::operator[](std::size_t row) const - { - return data() + row * width(); - } - - template - Pixel & basic_pixmap::operator()(std::size_t column, std::size_t row) - { - return (*this)[row][column]; - } - - template - Pixel const & basic_pixmap::operator()(std::size_t column, std::size_t row) const - { - return (*this)[row][column]; - } - - template - std::unique_ptr basic_pixmap::release() - { - auto data = std::move(data_); - width_ = 0; - height_ = 0; - return data; - } - - template - void basic_pixmap::reset(std::size_t width, std::size_t height, std::unique_ptr data) - { - width_ = width; - height_ = height; - data_ = std::move(data); - } - - template - bool basic_pixmap::empty() const - { - return width() == 0 || height() == 0; - } - - template - void basic_pixmap::clear() - { - release(); - } - - template - void basic_pixmap::fill(Pixel value) - { - std::fill(data_.get(), data_.get() + width_ * height_, value); - } - - template - void mirror_x(basic_pixmap & p) - { - for (std::size_t y = 0; y < p.height(); ++y) - for (std::size_t x = 0; x < p.width() / 2; ++x) - std::swap(p(x, y), p(p.width() - x - 1, y)); - } - - template - void mirror_y(basic_pixmap & p) - { - for (std::size_t y = 0; y < p.height() / 2; ++y) - for (std::size_t x = 0; x < p.width(); ++x) - std::swap(p(x, y), p(x, p.height() - y - 1)); - } - } diff --git a/libs/gfx/source/pixmap.cpp b/libs/gfx/source/pixmap.cpp index a6e5cf91..47b5fc81 100644 --- a/libs/gfx/source/pixmap.cpp +++ b/libs/gfx/source/pixmap.cpp @@ -41,7 +41,7 @@ namespace psemek::gfx sline >> width >> height; - pixmap.resize(width, height); + pixmap.resize({width, height}); std::size_t bytes = width * height; if ((bytes % 8) == 0) @@ -104,7 +104,7 @@ namespace psemek::gfx if (max != 255) fail("max value " + std::to_string(max) + " is not supported"); - pixmap.resize(width, height); + pixmap.resize({width, height}); if (binary) is.read(reinterpret_cast(pixmap.data()), width * height * sizeof(pixmap.data()[0])); @@ -156,7 +156,7 @@ namespace psemek::gfx if (max != 255) fail("max value " + std::to_string(max) + " is not supported"); - pixmap.resize(width, height); + pixmap.resize({width, height}); if (binary) is.read(reinterpret_cast(pixmap.data()), width * height * sizeof(pixmap.data()[0]));