Big templatization. Fix for interpolation.
This commit is contained in:
62
image.h
62
image.h
@@ -8,56 +8,56 @@
|
||||
#include "color.h"
|
||||
#include "coord.h"
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
class Image : public std::array<std::array<Color, X>, Y> {
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
class Image : public std::array<std::array<Color<C>, X>, Y> {
|
||||
public:
|
||||
constexpr const Color& GetPixel(const Coord& coord) const;
|
||||
constexpr const Color<C>& GetPixel(const Coord<2>& coord) const;
|
||||
|
||||
void SetPixel(const Coord& coord, const Color& color);
|
||||
void DrawXLine(const Coord& start, const Color& color, uint32_t length);
|
||||
void DrawYLine(const Coord& start, const Color& color, uint32_t length);
|
||||
void DrawRectangle(const Coord& start, const Color& color, uint32_t x_length, uint32_t y_length);
|
||||
void DrawSquare(const Coord& start, const Color& color, uint32_t length);
|
||||
void SetPixel(const Coord<2>& coord, const Color<C>& color);
|
||||
void DrawXLine(const Coord<2>& start, const Color<C>& color, uint32_t length);
|
||||
void DrawYLine(const Coord<2>& start, const Color<C>& color, uint32_t length);
|
||||
void DrawRectangle(const Coord<2>& start, const Color<C>& color, uint32_t x_length, uint32_t y_length);
|
||||
void DrawSquare(const Coord<2>& start, const Color<C>& color, uint32_t length);
|
||||
|
||||
std::string ToPng();
|
||||
};
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
constexpr const Color& Image<X, Y>::GetPixel(const Coord& coord) const {
|
||||
return this->at(coord.y).at(coord.x);
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
constexpr const Color<C>& Image<X, Y, C>::GetPixel(const Coord<2>& coord) const {
|
||||
return this->at(coord.at(1)).at(coord.at(0));
|
||||
}
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
void Image<X, Y>::SetPixel(const Coord& coord, const Color& color) {
|
||||
this->at(coord.y).at(coord.x) = color;
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
void Image<X, Y, C>::SetPixel(const Coord<2>& coord, const Color<C>& color) {
|
||||
this->at(coord.at(1)).at(coord.at(0)) = color;
|
||||
}
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
void Image<X, Y>::DrawXLine(const Coord& coord, const Color& color, uint32_t length) {
|
||||
auto& row = this->at(coord.y);
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
void Image<X, Y, C>::DrawXLine(const Coord<2>& coord, const Color<C>& color, uint32_t length) {
|
||||
auto& row = this->at(coord.at(1));
|
||||
|
||||
for (uint32_t x = coord.x; x < std::min(X, coord.x + length); ++x) {
|
||||
for (uint32_t x = coord.at(0); x < std::min(X, coord.at(0) + length); ++x) {
|
||||
row.at(x) = color;
|
||||
}
|
||||
}
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
void Image<X, Y>::DrawYLine(const Coord& coord, const Color& color, uint32_t length) {
|
||||
for (uint32_t y = coord.y; y <= std::min(Y, coord.y + length); ++y) {
|
||||
SetPixel({coord.x, y}, color);
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
void Image<X, Y, C>::DrawYLine(const Coord<2>& coord, const Color<C>& color, uint32_t length) {
|
||||
for (uint32_t y = coord.at(1); y <= std::min(Y, coord.at(1) + length); ++y) {
|
||||
SetPixel({{{coord.at(0), y}}}, color);
|
||||
}
|
||||
}
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
void Image<X, Y>::DrawRectangle(const Coord& start, const Color& color, uint32_t x_length, uint32_t y_length) {
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
void Image<X, Y, C>::DrawRectangle(const Coord<2>& start, const Color<C>& color, uint32_t x_length, uint32_t y_length) {
|
||||
DrawXLine(start, color, x_length);
|
||||
DrawXLine({start.x, start.y + y_length}, color, x_length);
|
||||
DrawXLine({{{start.at(0), start.at(1) + y_length}}}, color, x_length);
|
||||
DrawYLine(start, color, y_length);
|
||||
DrawYLine({start.x + x_length, start.y}, color, y_length);
|
||||
DrawYLine({{{start.at(0) + x_length, start.at(1)}}}, color, y_length);
|
||||
}
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
void Image<X, Y>::DrawSquare(const Coord& start, const Color& color, uint32_t length) {
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
void Image<X, Y, C>::DrawSquare(const Coord<2>& start, const Color<C>& color, uint32_t length) {
|
||||
DrawRectangle(start, color, length, length);
|
||||
}
|
||||
|
||||
@@ -66,8 +66,10 @@ static inline void WriteCallback(png_structp png_ptr, png_bytep data, png_size_t
|
||||
dest->append(reinterpret_cast<char*>(data), length);
|
||||
}
|
||||
|
||||
template <uint32_t X, uint32_t Y>
|
||||
std::string Image<X, Y>::ToPng() {
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
std::string Image<X, Y, C>::ToPng() {
|
||||
static_assert(C == 3); // PNG only supports RGB
|
||||
|
||||
std::string ret;
|
||||
|
||||
auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
||||
|
||||
Reference in New Issue
Block a user