summaryrefslogtreecommitdiff
path: root/libraries/daguerre
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/daguerre')
-rw-r--r--libraries/daguerre/include/daguerre.hpp131
-rw-r--r--libraries/daguerre/include/daguerre/image.hpp155
-rw-r--r--libraries/daguerre/makefile12
-rw-r--r--libraries/daguerre/ppm.cpp52
-rw-r--r--libraries/daguerre/source/daguerre.cpp58
5 files changed, 201 insertions, 207 deletions
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 <stdint.h>
+#include <cstdio>
+
+namespace daguerre {
+
+ typedef uint32_t hilbert_color;
+
+ struct rgb24 {
+ uint8_t r;
+ uint8_t g;
+ uint8_t b;
+ };
+
+ template <class to_type, class from_type>
+ to_type convert_color(const from_type &from);
+
+ template <>
+ inline hilbert_color convert_color<hilbert_color, rgb24>(const rgb24 &from) {
+ return __euler_encode_color(from.r, from.g, from.b);
+ }
+
+ template <class color_t>
+ 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<color_t> &other) = delete;
+
+ image(image<color_t> &&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<color_t> &operator =(const image<color_t> &other) = delete;
+
+ image<color_t> &operator =(image<color_t> &&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 <class color_t>
+ void copy_region(
+ image<color_t> &to, const image<color_t> &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<to_color_t, from_color_t>>
+ void copy_region(
+ image<to_color_t> &to, const image<from_color_t> &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<hilbert_color> get_hilbert_framebuffer();
+
+ bool try_load_ppm(std::FILE *input, image<rgb24> &into);
+
+}
diff --git a/libraries/daguerre/include/daguerre/image.hpp b/libraries/daguerre/include/daguerre/image.hpp
deleted file mode 100644
index 6129306..0000000
--- a/libraries/daguerre/include/daguerre/image.hpp
+++ /dev/null
@@ -1,155 +0,0 @@
-#pragma once
-
-#include <euler/syscall.hpp>
-#include <cassert>
-#include <cstdint>
-#include <cstdio>
-
-namespace daguerre {
-
- struct color24 {
- uint8_t r;
- uint8_t g;
- uint8_t b;
- };
-
- using hilbert_color = euler::syscall::encoded_color;
-
- static inline hilbert_color to_hilbert_color(const color24 &c) {
- return _syscall_encode_color(c.r, c.g, c.b);
- }
-
- template <class pixel_type = color24>
- class image {
-
- pixel_type *buffer;
- unsigned width;
- unsigned height;
- unsigned pitch; //in pixels
-
- bool buffer_owned;
-
- public:
- image() : buffer(0) {}
-
- image(pixel_type *buffer, unsigned width,
- unsigned height, unsigned pitch, bool buffer_owned
- ) : buffer(buffer), width(width), height(height),
- pitch(pitch), buffer_owned(buffer_owned) {}
-
- image(unsigned width, unsigned height)
- : buffer(new pixel_type[width * height]), width(width),
- height(height), pitch(width), buffer_owned(true) {}
-
- image(const image<pixel_type> &other) = delete;
-
- image(image<pixel_type> &&other)
- : buffer(other.buffer), width(other.width), height(other.height),
- pitch(other.pitch), buffer_owned(other.buffer_owned) {
- other.buffer = 0;
- }
-
- ~image() {
- if (buffer && buffer_owned)
- delete[] buffer;
- }
-
- image &operator =(const image<pixel_type> &other) = delete;
-
- image &operator =(image<pixel_type> &&other) {
- if (buffer && buffer_owned)
- delete[] buffer;
- buffer = other.buffer;
- width = other.width;
- height = other.height;
- pitch = other.pitch;
- buffer_owned = other.buffer_owned;
- other.buffer = 0;
- return *this;
- }
-
- pixel_type &get(unsigned x, unsigned y) {
- return buffer[y * pitch + x];
- }
-
- const pixel_type &get(unsigned x, unsigned y) const {
- return buffer[y * pitch + x];
- }
-
- pixel_type *get_buffer() {
- return buffer;
- }
-
- const pixel_type *get_buffer() const {
- return buffer;
- }
-
- unsigned get_width() const {
- return width;
- }
-
- unsigned get_height() const {
- return height;
- }
-
- unsigned get_pitch() const {
- return pitch;
- }
-
- };
-
- template <class pixel_type>
- void default_converter(const pixel_type &i, pixel_type &o) {
- o = i;
- }
-
- static inline void default_converter(const color24 &i, hilbert_color &o) {
- o = to_hilbert_color(i);
- }
-
- //copies a rectangle of size width x height from source to destination. the
- //rectangle starts in source at (source_x, source_y), and in destination at
- //(destination_x, destination_y). every pixel is passed through the converter
- //template argument.
- template <
- class source_pixel_type, class destination_pixel_type,
- void (*converter)(const source_pixel_type &, destination_pixel_type &) =
- &default_converter>
- void copy_image(const image<source_pixel_type> &source,
- image<destination_pixel_type> &destination, unsigned source_x,
- unsigned source_y, unsigned destination_x, unsigned destination_y,
- unsigned width, unsigned height
- ) {
-
- assert(source_x + width <= source.get_width());
- assert(source_y + height <= source.get_height());
- assert(destination_x + width <= destination.get_width());
- assert(destination_y + height <= destination.get_height());
-
- unsigned source_pitch = source.get_pitch();
- unsigned destination_pitch = destination.get_pitch();
-
- const source_pixel_type *source_buffer =
- &source.get_buffer()[source_y * source_pitch + source_x];
- destination_pixel_type *destination_buffer = &destination.get_buffer()[
- destination_y * destination_pitch + destination_x];
-
- for (unsigned y = 0; y < height; ++y)
- for (unsigned x = 0; x < width; ++x)
- converter(source_buffer[y * source_pitch + x],
- destination_buffer[y * destination_pitch + x]);
-
- }
-
- static inline image<hilbert_color> get_hilbert_framebuffer() {
- hilbert_color *ptr;
- uint32_t width;
- uint32_t height;
- uint32_t pitch;
- _syscall_get_framebuffer(ptr, width, height, pitch);
- return image<hilbert_color>(ptr, width, height, pitch, false);
- }
-
- bool try_load_ppm(std::FILE *from, image<color24> &into);
-
-}
diff --git a/libraries/daguerre/makefile b/libraries/daguerre/makefile
new file mode 100644
index 0000000..3505d35
--- /dev/null
+++ b/libraries/daguerre/makefile
@@ -0,0 +1,12 @@
+SOURCES = \
+ daguerre.cpp
+
+build/%.cpp.o: source/%.cpp
+ @mkdir -p $(@D)
+ $(HILBERT_CC) -c $^ -o $@
+
+build/libdaguerre.a: $(SOURCES:%=build/%.o)
+ $(HILBERT_AR) rcs $@ $^
+
+clean:
+ rm -rf build
diff --git a/libraries/daguerre/ppm.cpp b/libraries/daguerre/ppm.cpp
deleted file mode 100644
index e909503..0000000
--- a/libraries/daguerre/ppm.cpp
+++ /dev/null
@@ -1,52 +0,0 @@
-#include <daguerre/image.hpp>
-#include <cctype>
-
-namespace daguerre {
-
- static unsigned read_int(std::FILE *from) {
- unsigned out = 0;
- int ch;
- do
- ch = std::fgetc(from);
- while (std::isspace(ch));
- while (true) {
- if (ch == EOF)
- return out;
- if (ch < '0' || ch > '9') {
- std::ungetc(ch, from);
- return out;
- }
- out = out * 10 + (ch - '0');
- ch = std::fgetc(from);
- }
- }
-
- bool try_load_ppm(std::FILE *from, image<color24> &into) {
-
- if (std::fgetc(from) != 'P' || std::fgetc(from) != '6' ||
- std::fgetc(from) != '\n')
- return false;
-
- unsigned width = read_int(from);
- unsigned height = read_int(from);
- unsigned max = read_int(from);
- std::fgetc(from);//newline
-
- if (max != 255)
- return false;
-
- into = image<color24>(width, height);
- color24 *buffer = into.get_buffer();
-
- for (unsigned y = 0; y < height; ++y)
- for (unsigned x = 0; x < width; ++x) {
- buffer[y * width + x].r = std::fgetc(from);
- buffer[y * width + x].g = std::fgetc(from);
- buffer[y * width + x].b = std::fgetc(from);
- }
-
- return true;
-
- }
-
-}
diff --git a/libraries/daguerre/source/daguerre.cpp b/libraries/daguerre/source/daguerre.cpp
new file mode 100644
index 0000000..fb3ddc7
--- /dev/null
+++ b/libraries/daguerre/source/daguerre.cpp
@@ -0,0 +1,58 @@
+#include <daguerre.hpp>
+
+namespace daguerre {
+
+ image<hilbert_color> get_hilbert_framebuffer() {
+ uint32_t width, height, pitch;
+ hilbert_color *ptr = __euler_get_framebuffer(width, height, pitch);
+ return image<hilbert_color>(ptr, width, height, pitch, false);
+ }
+
+ unsigned read_text_int(std::FILE *input) {
+ unsigned n = 0;
+ char ch;
+ while (true) {
+ std::fread(&ch, 1, 1, input);
+ if (ch < '0' || ch > '9')
+ return n;
+ n = n * 10 + ch - '0';
+ }
+ }
+
+ bool try_load_ppm(std::FILE *input, image<rgb24> &into) {
+
+ char header[3];
+ if (std::fread(header, 1, 3, input) != 3)
+ return false;
+
+ if (header[0] != 'P' || header[1] != '6' || header[2] != '\n')
+ return false;
+
+ unsigned width = read_text_int(input);
+ unsigned height = read_text_int(input);
+ unsigned max = read_text_int(input);
+
+ into = image<rgb24>(width, height);
+
+ for (unsigned y = 0; y < height; ++y)
+ for (unsigned x = 0; x < width; ++x) {
+ if (std::fread(&into.buffer[y * width + x].r, 1, 1, input) != 1)
+ return false;
+ if (std::fread(&into.buffer[y * width + x].g, 1, 1, input) != 1)
+ return false;
+ if (std::fread(&into.buffer[y * width + x].b, 1, 1, input) != 1)
+ return false;
+ }
+
+ if (max != 255)
+ for (unsigned v = 0; v < width * height; ++v) {
+ into.buffer[v].r = ((uint16_t)into.buffer[v].r * 255) / max;
+ into.buffer[v].g = ((uint16_t)into.buffer[v].g * 255) / max;
+ into.buffer[v].b = ((uint16_t)into.buffer[v].b * 255) / max;
+ }
+
+ return true;
+
+ }
+
+}