diff options
author | Benji Dial <benji@benjidial.net> | 2024-05-18 21:53:38 -0400 |
---|---|---|
committer | Benji Dial <benji@benjidial.net> | 2024-05-18 21:53:38 -0400 |
commit | b1a912a8a6ff472a49b2e0a09cfd433adfc2cb24 (patch) | |
tree | 5009d4415ba13e4baa37f3d0271852528130fd3b /libraries/daguerre/source/daguerre.cpp | |
parent | a8a80d326de9550b2a25b1255a2093ab43219ede (diff) | |
download | hilbert-os-b1a912a8a6ff472a49b2e0a09cfd433adfc2cb24.tar.gz |
reorganization, cross compiler
Diffstat (limited to 'libraries/daguerre/source/daguerre.cpp')
-rw-r--r-- | libraries/daguerre/source/daguerre.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
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; + + } + +} |