#pragma once #include #include #include #include "color.h" #include "coord.h" template class Image : public std::array, Y> { public: constexpr const Color& GetPixel(const Coord& 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); std::string ToPng(); }; template constexpr const Color& Image::GetPixel(const Coord& coord) const { return this->at(coord.y).at(coord.x); } template void Image::SetPixel(const Coord& coord, const Color& color) { this->at(coord.y).at(coord.x) = color; } template void Image::DrawXLine(const Coord& coord, const Color& color, uint32_t length) { auto& row = this->at(coord.y); for (uint32_t x = coord.x; x < std::min(X, coord.x + length); ++x) { row.at(x) = color; } } template void Image::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 void Image::DrawRectangle(const Coord& start, const Color& color, uint32_t x_length, uint32_t y_length) { DrawXLine(start, color, x_length); DrawXLine({start.x, start.y + y_length}, color, x_length); DrawYLine(start, color, y_length); DrawYLine({start.x + x_length, start.y}, color, y_length); } template void Image::DrawSquare(const Coord& start, const Color& color, uint32_t length) { DrawRectangle(start, color, length, length); } static 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() { 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) { std::array out_row; for (uint32_t x = 0; x < X; ++x) { out_row[x * 3 + 0] = htons(static_cast(row[x].r)); out_row[x * 3 + 1] = htons(static_cast(row[x].g)); out_row[x * 3 + 2] = htons(static_cast(row[x].b)); } 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; }