From b1a912a8a6ff472a49b2e0a09cfd433adfc2cb24 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 18 May 2024 21:53:38 -0400 Subject: reorganization, cross compiler --- libraries/daguerre/include/daguerre.hpp | 131 ++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100644 libraries/daguerre/include/daguerre.hpp (limited to 'libraries/daguerre/include/daguerre.hpp') diff --git a/libraries/daguerre/include/daguerre.hpp b/libraries/daguerre/include/daguerre.hpp new file mode 100644 index 0000000..274e257 --- /dev/null +++ b/libraries/daguerre/include/daguerre.hpp @@ -0,0 +1,131 @@ +#pragma once + +#include +#include + +namespace daguerre { + + typedef uint32_t hilbert_color; + + struct rgb24 { + uint8_t r; + uint8_t g; + uint8_t b; + }; + + template + to_type convert_color(const from_type &from); + + template <> + inline hilbert_color convert_color(const rgb24 &from) { + return __euler_encode_color(from.r, from.g, from.b); + } + + template + class image { + + public: + bool delete_buffer_on_destruct; + color_t *buffer; + unsigned width; + unsigned height; + unsigned pitch;//in sizeof(color_t) + + image() + : delete_buffer_on_destruct(false), buffer(0), width(0), height(0), + pitch(0) {} + + image(unsigned width, unsigned height) + : delete_buffer_on_destruct(true), buffer(new color_t[width * height]), + width(width), height(height), pitch(width) {} + + image( + color_t *buffer, unsigned width, unsigned height, unsigned pitch, + bool delete_buffer_on_destruct) + : delete_buffer_on_destruct(delete_buffer_on_destruct), buffer(buffer), + width(width), height(height), pitch(pitch) {} + + ~image() { + if (delete_buffer_on_destruct && buffer) + delete[] buffer; + } + + image(const image &other) = delete; + + image(image &&other) + : delete_buffer_on_destruct(other.delete_buffer_on_destruct), + buffer(other.buffer), width(other.width), height(other.height), + pitch(other.pitch) { + other.buffer = 0; + } + + image &operator =(const image &other) = delete; + + image &operator =(image &&other) { + if (delete_buffer_on_destruct && buffer) + delete[] buffer; + delete_buffer_on_destruct = other.delete_buffer_on_destruct; + buffer = other.buffer; + width = other.width; + height = other.height; + pitch = other.pitch; + other.buffer = 0; + return *this; + } + + color_t get(unsigned x, unsigned y) const { + return buffer[y * pitch + x]; + } + + void set(unsigned x, unsigned y, const color_t &c) { + buffer[y * pitch + x] = c; + } + + }; + + //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, const image &from, unsigned from_x, + unsigned from_y, unsigned to_x, unsigned to_y, unsigned width, + unsigned height) { + + 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) + memcpy( + to_start + to.pitch * y, from_start + from.pitch * y, + width * sizeof(color_t)); + + } + + //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, + to_color_t converter(const from_color_t &) = + convert_color> + void copy_region( + image &to, const image &from, unsigned from_x, + unsigned from_y, unsigned to_x, unsigned to_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) + to_start[to.pitch * y + x] = converter(from_start[from.pitch * y + x]); + + } + + image get_hilbert_framebuffer(); + + bool try_load_ppm(std::FILE *input, image &into); + +} -- cgit v1.2.3