Conversion to signed
This commit is contained in:
55
image.h
55
image.h
@@ -5,59 +5,60 @@
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#include "array.h"
|
||||
#include "color.h"
|
||||
#include "coord.h"
|
||||
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
class Image : public std::array<std::array<Color<C>, X>, Y> {
|
||||
template <int32_t X, int32_t Y, int32_t C>
|
||||
class Image : public Array<Array<Color<C>, X>, Y> {
|
||||
public:
|
||||
constexpr const Color<C>& GetPixel(const Coord<2>& coord) const;
|
||||
|
||||
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);
|
||||
void DrawXLine(const Coord<2>& start, const Color<C>& color, int32_t length);
|
||||
void DrawYLine(const Coord<2>& start, const Color<C>& color, int32_t length);
|
||||
void DrawRectangle(const Coord<2>& start, const Color<C>& color, int32_t x_length, int32_t y_length);
|
||||
void DrawSquare(const Coord<2>& start, const Color<C>& color, int32_t length);
|
||||
|
||||
std::string ToPng();
|
||||
};
|
||||
|
||||
template <uint32_t X, uint32_t Y, uint32_t C>
|
||||
template <int32_t X, int32_t Y, int32_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, uint32_t C>
|
||||
template <int32_t X, int32_t Y, int32_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, uint32_t C>
|
||||
void Image<X, Y, C>::DrawXLine(const Coord<2>& coord, const Color<C>& color, uint32_t length) {
|
||||
template <int32_t X, int32_t Y, int32_t C>
|
||||
void Image<X, Y, C>::DrawXLine(const Coord<2>& coord, const Color<C>& color, int32_t length) {
|
||||
auto& row = this->at(coord.at(1));
|
||||
|
||||
for (uint32_t x = coord.at(0); x < std::min(X, coord.at(0) + length); ++x) {
|
||||
for (int32_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, 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 <int32_t X, int32_t Y, int32_t C>
|
||||
void Image<X, Y, C>::DrawYLine(const Coord<2>& coord, const Color<C>& color, int32_t length) {
|
||||
for (int32_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, 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) {
|
||||
template <int32_t X, int32_t Y, int32_t C>
|
||||
void Image<X, Y, C>::DrawRectangle(const Coord<2>& start, const Color<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);
|
||||
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);
|
||||
DrawYLine({{{{start.at(0) + x_length, start.at(1)}}}}, color, y_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) {
|
||||
template <int32_t X, int32_t Y, int32_t C>
|
||||
void Image<X, Y, C>::DrawSquare(const Coord<2>& start, const Color<C>& color, int32_t length) {
|
||||
DrawRectangle(start, color, length, length);
|
||||
}
|
||||
|
||||
@@ -66,7 +67,7 @@ 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, uint32_t C>
|
||||
template <int32_t X, int32_t Y, int32_t C>
|
||||
std::string Image<X, Y, C>::ToPng() {
|
||||
static_assert(C == 3); // PNG only supports RGB
|
||||
|
||||
@@ -84,11 +85,11 @@ std::string Image<X, Y, C>::ToPng() {
|
||||
|
||||
png_write_info(png_ptr, info_ptr);
|
||||
for (auto& row : *this) {
|
||||
std::array<uint16_t, X * 3> out_row;
|
||||
for (uint32_t x = 0; x < X; ++x) {
|
||||
out_row[x * 3 + 0] = htons(static_cast<uint16_t>(row[x].at(0)));
|
||||
out_row[x * 3 + 1] = htons(static_cast<uint16_t>(row[x].at(1)));
|
||||
out_row[x * 3 + 2] = htons(static_cast<uint16_t>(row[x].at(2)));
|
||||
Array<uint16_t, X * 3> out_row;
|
||||
for (int32_t x = 0; x < X; ++x) {
|
||||
out_row.at(x * 3 + 0) = htons(static_cast<uint16_t>(row.at(x).at(0)));
|
||||
out_row.at(x * 3 + 1) = htons(static_cast<uint16_t>(row.at(x).at(1)));
|
||||
out_row.at(x * 3 + 2) = htons(static_cast<uint16_t>(row.at(x).at(2)));
|
||||
}
|
||||
png_write_row(png_ptr, reinterpret_cast<unsigned char*>(out_row.data()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user