From 5a54df93c4e9368c36e69d1e9c88cd2904e92308 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sun, 19 May 2024 14:39:06 -0400 Subject: rework daguerre a bit --- libraries/daguerre/include/daguerre.hpp | 100 ++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 38 deletions(-) (limited to 'libraries/daguerre/include') diff --git a/libraries/daguerre/include/daguerre.hpp b/libraries/daguerre/include/daguerre.hpp index c236cb9..9468826 100644 --- a/libraries/daguerre/include/daguerre.hpp +++ b/libraries/daguerre/include/daguerre.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -14,6 +15,11 @@ namespace daguerre { uint8_t b; }; + template + static inline void default_overlay(color_t &dest, const color_t &src) { + dest = src; + } + static inline void default_overlay(hilbert_color &dest, const rgb24 &src) { dest = __euler_encode_color(src.r, src.g, src.b); } @@ -53,7 +59,14 @@ namespace daguerre { delete[] buffer; } - image(const image &other) = delete; + template + image(const image &other) + : delete_buffer_on_destruct(true), + buffer(new color_t[other.width * other.height]), width(other.width), + height(other.height), pitch(other.width) { + overlay_from(other, 0, 0, 0, 0, width, height); + } image(image &&other) : delete_buffer_on_destruct(other.delete_buffer_on_destruct), @@ -62,7 +75,19 @@ namespace daguerre { other.buffer = 0; } - image &operator =(const image &other) = delete; + template + image &operator =(const image &other) { + if (delete_buffer_on_destruct && buffer) + delete[] buffer; + delete_buffer_on_destruct = true; + width = other.width; + height = other.height; + pitch = width; + buffer = new color_t[width * height]; + overlay_from(other, 0, 0, 0, 0, width, height); + return *this; + } image &operator =(image &&other) { if (delete_buffer_on_destruct && buffer) @@ -84,46 +109,45 @@ namespace daguerre { buffer[y * pitch + x] = c; } - }; + void copy_from( + const image &other, unsigned to_x, unsigned to_y, + unsigned from_x, unsigned from_y, unsigned width, unsigned height) { - //it is assumed that the regions do not overlap in memory. - //copies into [to_x, to_x + width) x [to_y, to_y + height) - //from [from_x, from_x + width) x [from_y, from_y + height). - template - void copy_region( - image &to, unsigned to_x, unsigned to_y, - const image &from, unsigned from_x, unsigned from_y, - unsigned width, unsigned height) { + color_t *to_start = buffer + pitch * to_y + to_x; + const color_t *from_start = other.buffer + other.pitch * from_y + from_x; - color_t *to_start = to.buffer + to.pitch * to_y + to_x; - const color_t *from_start = from.buffer + from.pitch * from_y + from_x; + for (unsigned y = 0; y < height; ++y) + std::memcpy( + to_start + pitch * y, from_start + other.pitch * y, + width * sizeof(color_t)); - for (unsigned y = 0; y < height; ++y) - std::memcpy( - to_start + to.pitch * y, from_start + from.pitch * y, - width * sizeof(color_t)); + } - } + template + void overlay_from( + const image &other, unsigned to_x, unsigned to_y, + unsigned from_x, unsigned from_y, unsigned width, unsigned height) { + + color_t *to_start = buffer + pitch * to_y + to_x; + const other_color_t *from_start = + other.buffer + other.pitch * from_y + from_x; - //it is assumed that the regions do not overlap in memory. - //copies into [to_x, to_x + width) x [to_y, to_y + height) - //from [from_x, from_x + width) x [from_y, from_y + height). - template < - class to_color_t, class from_color_t, - void overlay(to_color_t &dest, const from_color_t &src) = default_overlay> - void overlay_region( - image &to, unsigned to_x, unsigned to_y, - const image &from, unsigned from_x, unsigned from_y, - unsigned width, unsigned height) { - - to_color_t *to_start = to.buffer + to.pitch * to_y + to_x; - const from_color_t *from_start = - from.buffer + from.pitch * from_y + from_x; - - for (unsigned y = 0; y < height; ++y) - for (unsigned x = 0; x < width; ++x) - overlay(to_start[to.pitch * y + x], from_start[from.pitch * y + x]); + for (unsigned y = 0; y < height; ++y) + for (unsigned x = 0; x < width; ++x) + overlay(to_start[pitch * y + x], from_start[other.pitch * y + x]); + } + + }; + + template + void swap(image &a, image &b) { + std::swap(a.delete_buffer_on_destruct, b.delete_buffer_on_destruct); + std::swap(a.buffer, b.buffer); + std::swap(a.width, b.width); + std::swap(a.height, b.height); + std::swap(a.pitch, b.pitch); } image get_hilbert_framebuffer(); @@ -159,8 +183,8 @@ namespace daguerre { if (ch == 0) return; if (ch < 128) { - overlay_region( - target, x, y, glyphs[ch], 0, 0, width, height); + target.template overlay_from( + glyphs[ch], x, y, 0, 0, width, height); x += width; } ++text; -- cgit v1.2.3