Make gfx::pixmap an alias to 2D util::array
This commit is contained in:
parent
12b4b44011
commit
6844a01a8f
2 changed files with 5 additions and 182 deletions
|
|
@ -1,5 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <psemek/util/array.hpp>
|
||||
#include <psemek/gfx/color.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
|
@ -8,50 +9,7 @@ namespace psemek::gfx
|
|||
{
|
||||
|
||||
template <typename Pixel>
|
||||
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<Pixel[]> 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<Pixel[]> release();
|
||||
void reset(std::size_t width, std::size_t height, std::unique_ptr<Pixel[]> data);
|
||||
|
||||
bool empty() const;
|
||||
|
||||
void clear();
|
||||
|
||||
void fill(Pixel value);
|
||||
|
||||
private:
|
||||
std::size_t width_ = 0;
|
||||
std::size_t height_ = 0;
|
||||
std::unique_ptr<Pixel[]> data_;
|
||||
};
|
||||
using basic_pixmap = util::array<Pixel, 2>;
|
||||
|
||||
using pixmap_monochrome = basic_pixmap<std::uint8_t>;
|
||||
using pixmap_rgb = basic_pixmap<color_rgb>;
|
||||
|
|
@ -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 <typename Pixel>
|
||||
basic_pixmap<Pixel>::basic_pixmap(std::size_t width, std::size_t height)
|
||||
: width_(width)
|
||||
, height_(height)
|
||||
{
|
||||
data_.reset(new Pixel [width_ * height_]);
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
basic_pixmap<Pixel>::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 <typename Pixel>
|
||||
basic_pixmap<Pixel>::basic_pixmap(std::size_t width, std::size_t height, std::unique_ptr<Pixel[]> data)
|
||||
: width_(width)
|
||||
, height_(height)
|
||||
, data_(std::move(data))
|
||||
{}
|
||||
|
||||
template <typename Pixel>
|
||||
basic_pixmap<Pixel>::basic_pixmap(basic_pixmap && other)
|
||||
: width_(other.width_)
|
||||
, height_(other.height_)
|
||||
, data_(other.release())
|
||||
{}
|
||||
|
||||
template <typename Pixel>
|
||||
basic_pixmap<Pixel> & basic_pixmap<Pixel>::operator = (basic_pixmap && other)
|
||||
{
|
||||
if (this == &other) return *this;
|
||||
|
||||
width_ = other.width_;
|
||||
height_ = other.height_;
|
||||
data_ = other.release();
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
void basic_pixmap<Pixel>::resize(std::size_t width, std::size_t height)
|
||||
{
|
||||
data_.reset(new Pixel[width * height]);
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
void basic_pixmap<Pixel>::resize(std::size_t width, std::size_t height, Pixel value)
|
||||
{
|
||||
data_.reset(new Pixel[width * height]);
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
fill(value);
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
Pixel * basic_pixmap<Pixel>::operator[](std::size_t row)
|
||||
{
|
||||
return data() + row * width();
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
Pixel const * basic_pixmap<Pixel>::operator[](std::size_t row) const
|
||||
{
|
||||
return data() + row * width();
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
Pixel & basic_pixmap<Pixel>::operator()(std::size_t column, std::size_t row)
|
||||
{
|
||||
return (*this)[row][column];
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
Pixel const & basic_pixmap<Pixel>::operator()(std::size_t column, std::size_t row) const
|
||||
{
|
||||
return (*this)[row][column];
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
std::unique_ptr<Pixel[]> basic_pixmap<Pixel>::release()
|
||||
{
|
||||
auto data = std::move(data_);
|
||||
width_ = 0;
|
||||
height_ = 0;
|
||||
return data;
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
void basic_pixmap<Pixel>::reset(std::size_t width, std::size_t height, std::unique_ptr<Pixel[]> data)
|
||||
{
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
data_ = std::move(data);
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
bool basic_pixmap<Pixel>::empty() const
|
||||
{
|
||||
return width() == 0 || height() == 0;
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
void basic_pixmap<Pixel>::clear()
|
||||
{
|
||||
release();
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
void basic_pixmap<Pixel>::fill(Pixel value)
|
||||
{
|
||||
std::fill(data_.get(), data_.get() + width_ * height_, value);
|
||||
}
|
||||
|
||||
template <typename Pixel>
|
||||
void mirror_x(basic_pixmap<Pixel> & 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 <typename Pixel>
|
||||
void mirror_y(basic_pixmap<Pixel> & 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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<char *>(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<char *>(pixmap.data()), width * height * sizeof(pixmap.data()[0]));
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue