diff options
Diffstat (limited to 'libraries')
-rw-r--r-- | libraries/daguerre/include/daguerre.hpp | 100 |
1 files changed, 62 insertions, 38 deletions
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 <algorithm> #include <stdint.h> #include <cstring> #include <cstdio> @@ -14,6 +15,11 @@ namespace daguerre { uint8_t b; }; + template <class color_t> + 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<color_t> &other) = delete; + template <class other_color_t, + void overlay(color_t &, const other_color_t &) = default_overlay> + image(const image<other_color_t> &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_color_t, overlay>(other, 0, 0, 0, 0, width, height); + } image(image<color_t> &&other) : delete_buffer_on_destruct(other.delete_buffer_on_destruct), @@ -62,7 +75,19 @@ namespace daguerre { other.buffer = 0; } - image<color_t> &operator =(const image<color_t> &other) = delete; + template <class other_color_t, + void overlay(color_t &, const other_color_t &) = default_overlay> + image<color_t> &operator =(const image<other_color_t> &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_color_t, overlay>(other, 0, 0, 0, 0, width, height); + return *this; + } image<color_t> &operator =(image<color_t> &&other) { if (delete_buffer_on_destruct && buffer) @@ -84,46 +109,45 @@ namespace daguerre { buffer[y * pitch + x] = c; } - }; + void copy_from( + const image<color_t> &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 <class color_t> - void copy_region( - image<color_t> &to, unsigned to_x, unsigned to_y, - const image<color_t> &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 <class other_color_t, + void overlay(color_t &, const other_color_t &) = default_overlay> + void overlay_from( + const image<other_color_t> &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_color_t> &to, unsigned to_x, unsigned to_y, - const image<from_color_t> &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 <class color_t> + void swap(image<color_t> &a, image<color_t> &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<hilbert_color> get_hilbert_framebuffer(); @@ -159,8 +183,8 @@ namespace daguerre { if (ch == 0) return; if (ch < 128) { - overlay_region<target_color_t, color_t, overlay>( - target, x, y, glyphs[ch], 0, 0, width, height); + target.template overlay_from<color_t, overlay>( + glyphs[ch], x, y, 0, 0, width, height); x += width; } ++text; |