#pragma once #include #include #include #include "array.h" #include "color.h" #include "coord.h" class ImageBase {}; template struct ImageColorBase : public ImageBase { ImageColorBase() = default; ImageColorBase(const ImageColorBase&) = default; virtual ~ImageColorBase() = default; virtual void ForEach(std::function callback) const = 0; }; template class Image : public Array, Y>, ImageColorBase { public: constexpr const C& GetPixel(const Coord<2>& coord) const; void ForEach(std::function callback) const override; void SetPixel(const Coord<2>& coord, const C& color); void DrawXLine(const Coord<2>& start, const C& color, int32_t length); void DrawYLine(const Coord<2>& start, const C& color, int32_t length); void DrawRectangle(const Coord<2>& start, const C& color, int32_t x_length, int32_t y_length); void DrawSquare(const Coord<2>& start, const C& color, int32_t length); std::string ToPng(); }; template constexpr const C& Image::GetPixel(const Coord<2>& coord) const { return this->at(coord.at(1)).at(coord.at(0)); } template void Image::ForEach(std::function callback) const { for (int32_t y = 0; y < Y; ++y) { const auto& row = this->at(y); for (int32_t x = 0; x < X; ++x) { callback(row.at(x)); } } } template void Image::SetPixel(const Coord<2>& coord, const C& color) { if (coord.at(0) >= X || coord.at(1) >= Y) { return; } this->at(coord.at(1)).at(coord.at(0)) = color; } template void Image::DrawXLine(const Coord<2>& coord, const C& color, int32_t length) { for (int32_t x = coord.at(0); x <= coord.at(0) + length; ++x) { SetPixel({{{{x, coord.at(1)}}}}, color); } } template void Image::DrawYLine(const Coord<2>& coord, const C& color, int32_t length) { for (int32_t y = coord.at(1); y <= coord.at(1) + length; ++y) { SetPixel({{{{coord.at(0), y}}}}, color); } } template void Image::DrawRectangle(const Coord<2>& start, const C& color, int32_t x_length, int32_t y_length) { DrawXLine(start, color, x_length); DrawXLine({{{{start.at(0), start.at(1) + y_length}}}}, color, x_length); DrawYLine(start, color, y_length); DrawYLine({{{{start.at(0) + x_length, start.at(1)}}}}, color, y_length); } template void Image::DrawSquare(const Coord<2>& start, const C& color, int32_t length) { DrawRectangle(start, color, length, length); } static inline void WriteCallback(png_structp png_ptr, png_bytep data, png_size_t length) { auto dest = static_cast(png_get_io_ptr(png_ptr)); dest->append(reinterpret_cast(data), length); } template std::string Image::ToPng() { // TODO: specialize this to RgbColor std::string ret; auto png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); assert(png_ptr); auto info_ptr = png_create_info_struct(png_ptr); assert(info_ptr); png_set_write_fn(png_ptr, &ret, &WriteCallback, nullptr); png_set_IHDR(png_ptr, info_ptr, X, Y, 16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); png_write_info(png_ptr, info_ptr); for (auto& row : *this) { Array out_row; for (int32_t x = 0; x < X; ++x) { out_row.at(x * 3 + 0) = htons(static_cast(row.at(x).at(0))); out_row.at(x * 3 + 1) = htons(static_cast(row.at(x).at(1))); out_row.at(x * 3 + 2) = htons(static_cast(row.at(x).at(2))); } png_write_row(png_ptr, reinterpret_cast(out_row.data())); } png_write_end(png_ptr, nullptr); png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1); png_destroy_write_struct(&png_ptr, &info_ptr); return ret; }