From fbfc078e9f44c1c1e95c9c484f1d5650bcf631b7 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 27 Jul 2024 16:57:39 -0400 Subject: lots and lots of userspace stuff --- libraries/daguerre/source/daguerre.cpp | 110 ------------------------------ libraries/daguerre/source/framebuffer.cpp | 12 ++++ libraries/daguerre/source/ppm.cpp | 60 ++++++++++++++++ libraries/daguerre/source/psf.cpp | 51 ++++++++++++++ 4 files changed, 123 insertions(+), 110 deletions(-) delete mode 100644 libraries/daguerre/source/daguerre.cpp create mode 100644 libraries/daguerre/source/framebuffer.cpp create mode 100644 libraries/daguerre/source/ppm.cpp create mode 100644 libraries/daguerre/source/psf.cpp (limited to 'libraries/daguerre/source') diff --git a/libraries/daguerre/source/daguerre.cpp b/libraries/daguerre/source/daguerre.cpp deleted file mode 100644 index 2f7fe4d..0000000 --- a/libraries/daguerre/source/daguerre.cpp +++ /dev/null @@ -1,110 +0,0 @@ -#include - -namespace daguerre { - - image get_hilbert_framebuffer() { - uint32_t width, height, pitch; - hilbert_color *ptr = __euler_get_framebuffer(width, height, pitch); - return image(ptr, width, height, pitch, false); - } - - //TODO: make this more robust - unsigned read_text_int(std::FILE *input) { - unsigned n = 0; - char ch; - std::fread(&ch, 1, 1, input); - if (ch == '#') { - do - std::fread(&ch, 1, 1, input); - while (ch != '\n'); - std::fread(&ch, 1, 1, input); - } - do { - n = n * 10 + ch - '0'; - std::fread(&ch, 1, 1, input); - } while (ch >= '0' && ch <= '9'); - return n; - } - - //only supports p6 format - bool try_load_ppm(std::FILE *input, image &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(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; - - } - - //assumes the font is in psf2 format, and has a unicode table - bool try_load_psf(std::FILE *input, fixed_bitmap_font &into) { - - uint32_t header[8]; - if (std::fread(header, 4, 8, input) != 8) - return false; - - const uint32_t glyphs_start = header[2]; - const uint32_t glyph_count = header[4]; - const uint32_t glyph_length = header[5]; - into.height = header[6]; - into.width = header[7]; - - const uint32_t unicode_start = glyphs_start + glyph_count * glyph_length; - std::fseek(input, unicode_start, SEEK_SET); - - uint32_t indices[128]; - - for (uint32_t index = 0; index < glyph_count; ++index) { - uint8_t ch; - std::fread(&ch, 1, 1, input); - if (ch < 128) - indices[ch] = index; - do - std::fread(&ch, 1, 1, input); - while (ch != 0xff); - } - - for (uint8_t ch = 0; ch < 128; ++ch) { - std::fseek(input, glyphs_start + glyph_length * indices[ch], SEEK_SET); - into.glyphs[ch] = image(into.width, into.height); - for (unsigned h = 0; h < into.height; ++h) - for (unsigned wb = 0; wb < into.width; wb += 8) { - uint8_t byte; - std::fread(&byte, 1, 1, input); - for (unsigned x = 0; x < 8 && wb + x < into.width; ++x) - into.glyphs[ch].set(wb + x, h, (byte >> (7 - x)) & 1); - } - } - - return true; - - } - -} diff --git a/libraries/daguerre/source/framebuffer.cpp b/libraries/daguerre/source/framebuffer.cpp new file mode 100644 index 0000000..d4282ad --- /dev/null +++ b/libraries/daguerre/source/framebuffer.cpp @@ -0,0 +1,12 @@ +#include + +namespace daguerre { + + image get_hilbert_framebuffer() { + hilbert_color *ptr; + uint32_t width, height, pitch; + euler::syscall::get_framebuffer(ptr, width, height, pitch); + return image(width, height, ptr, pitch, false); + } + +} diff --git a/libraries/daguerre/source/ppm.cpp b/libraries/daguerre/source/ppm.cpp new file mode 100644 index 0000000..a4a7e27 --- /dev/null +++ b/libraries/daguerre/source/ppm.cpp @@ -0,0 +1,60 @@ +#include + +namespace daguerre { + + //TODO: make this more robust + static unsigned read_text_int(FILE *input) { + unsigned n = 0; + char ch; + fread(&ch, 1, 1, input); + if (ch == '#') { + do + fread(&ch, 1, 1, input); + while (ch != '\n'); + fread(&ch, 1, 1, input); + } + do { + n = n * 10 + ch - '0'; + fread(&ch, 1, 1, input); + } while (ch >= '0' && ch <= '9'); + return n; + } + + //TODO: this only supports p6 format, and assumes max < 256 + std::optional> try_load_ppm(FILE *input) { + + char header[3]; + if (fread(header, 1, 3, input) != 3) + return {}; + + if (header[0] != 'P' || header[1] != '6' || header[2] != '\n') + return {}; + + unsigned width = read_text_int(input); + unsigned height = read_text_int(input); + unsigned max = read_text_int(input); + + image im(width, height); + + for (unsigned y = 0; y < height; ++y) + for (unsigned x = 0; x < width; ++x) { + if (fread(&im.buffer[y * width + x].r, 1, 1, input) != 1) + return {}; + if (fread(&im.buffer[y * width + x].g, 1, 1, input) != 1) + return {}; + if (fread(&im.buffer[y * width + x].b, 1, 1, input) != 1) + return {}; + } + + if (max != 255) + for (unsigned v = 0; v < width * height; ++v) { + im.buffer[v].r = ((uint16_t)im.buffer[v].r * 255) / max; + im.buffer[v].g = ((uint16_t)im.buffer[v].g * 255) / max; + im.buffer[v].b = ((uint16_t)im.buffer[v].b * 255) / max; + } + + return im; + + } + +} diff --git a/libraries/daguerre/source/psf.cpp b/libraries/daguerre/source/psf.cpp new file mode 100644 index 0000000..81f722a --- /dev/null +++ b/libraries/daguerre/source/psf.cpp @@ -0,0 +1,51 @@ +#include + +namespace daguerre { + + //TODO: this assumes the font is in psf2 format, and has a unicode table + std::optional> try_load_psf(FILE *input) { + + uint32_t header[8]; + if (fread(header, 4, 8, input) != 8) + return {}; + + const uint32_t glyphs_start = header[2]; + const uint32_t glyph_count = header[4]; + const uint32_t glyph_length = header[5]; + const uint32_t height = header[6]; + const uint32_t width = header[7]; + + fixed_font font(width, height); + + const uint32_t unicode_start = glyphs_start + glyph_count * glyph_length; + fseek(input, unicode_start, SEEK_SET); + + uint32_t indices[128]; + + for (uint32_t index = 0; index < glyph_count; ++index) { + uint8_t ch; + fread(&ch, 1, 1, input); + if (ch < 128) + indices[ch] = index; + do + if (fread(&ch, 1, 1, input) != 1) + return {}; + while (ch != 0xff); + } + + for (uint8_t ch = 0; ch < 128; ++ch) { + fseek(input, glyphs_start + glyph_length * indices[ch], SEEK_SET); + for (unsigned h = 0; h < height; ++h) + for (unsigned wb = 0; wb < width; wb += 8) { + uint8_t byte; + fread(&byte, 1, 1, input); + for (unsigned x = 0; x < 8 && wb + x < width; ++x) + font.glyphs[ch].at(wb + x, h) = (byte >> (7 - x)) & 1; + } + } + + return font; + + } + +} -- cgit v1.2.3