From a41fbf75aff54f4d3bd88793cdf2bf281ea9ee01 Mon Sep 17 00:00:00 2001 From: Keuin Date: Thu, 14 Apr 2022 12:54:06 +0800 Subject: Add pixel and bitmap color depth conversion. --- bitmap.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 4 deletions(-) (limited to 'bitmap.h') diff --git a/bitmap.h b/bitmap.h index 5b07914..b35bf81 100644 --- a/bitmap.h +++ b/bitmap.h @@ -12,6 +12,7 @@ #include #include "bitfont.h" +#include "vec.h" #define COLORMIX_OVERFLOW_CHECK @@ -29,6 +30,11 @@ template struct pixel { T r, g, b; + static constexpr auto mod = (1ULL << (sizeof(T) * 8U)) - 1ULL; // FIXME + + bool operator==(const pixel &other) const { + return r == other.r && g == other.g && b == other.b; + } /** * Create a pixel of given depth, from normalized color values. @@ -36,7 +42,6 @@ struct pixel { * For example: Set color depth to 8bit, for normalized color (1, 0.5, 0.25), we get: (255, 127, 63). */ static inline pixel from_normalized(double r, double g, double b) { - const auto mod = (1ULL << (sizeof(T) * 8U)) - 1ULL; return pixel{.r = (T) (mod * r), .g = (T) (mod * g), .b = (T) (mod * b)}; } @@ -45,9 +50,27 @@ struct pixel { return from_normalized(v3d.x / 2.0 + 0.5, v3d.y / 2.0 + 0.5, v3d.z / 2.0 + 0.5); } + // Convert pixels between different color depths. + template + static inline pixel from(const pixel &orig) { + return from_normalized( + 1.0 * orig.r / pixel::max_value(), + 1.0 * orig.g / pixel::max_value(), + 1.0 * orig.b / pixel::max_value() + ); + } + static inline pixel black() { return pixel{(T) 0, (T) 0, (T) 0}; } + + static inline pixel white() { + return pixel{(T) mod, (T) mod, (T) mod}; // FIXME float-point values + } + + static inline T max_value() { + return mod; // FIXME + } }; template< @@ -106,9 +129,11 @@ class bitmap { public: bitmap() = delete; - bitmap(unsigned int width, unsigned int height) : width(width), height(height) { - content.resize(width * height, pixel{}); - } + bitmap(unsigned int width, unsigned int height) : + width(width), height(height), content{width * height, pixel{}} {} + + bitmap(unsigned int width, unsigned int height, std::vector> &&data) : + width(width), height(height), content{data} {} static bitmap average(const std::vector> &images) { assert(!images.empty()); @@ -138,12 +163,45 @@ public: return image(x, y); } + const pixel &operator[](size_t loc) const { + assert(loc < width * height); + return content[loc]; + } + + pixel &operator[](size_t loc) { + assert(loc < width * height); + return content[loc]; + } + // Do not use float-point pixels, or this routine will break. void write_plain_ppm(std::ostream &out) const; // Draw text on the image. Supports limited visual ASCII characters. void print(const std::string &s, const pixel &color, unsigned x, unsigned y, unsigned scale = 1, double alpha = 1.0); + + bool normalized() const { + return false; + // TODO return true for float-point pixels + } + + std::pair shape() const { + return std::pair{width, height}; + } + + // U: original color depth, T: desired color depth + template + static bitmap from(const bitmap &src) { + const auto shape = src.shape(); + const size_t sz = shape.first * shape.second; + bitmap out{shape.first, shape.second}; + for (size_t i = 0; i < sz; ++i) { + out[i] = pixel::from(src[i]); + std::cerr << (int) out[i].r << ' ' << (int) out[i].g << ' ' << (int) out[i].b << std::endl; + } + return out; + } + }; template -- cgit v1.2.3