From 7199e74aa22e592a3b77bdd81f735edca5470596 Mon Sep 17 00:00:00 2001 From: Benji Dial Date: Sat, 20 Jan 2024 17:59:40 -0500 Subject: update --- libraries/daguerre/ppm.cpp | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 libraries/daguerre/ppm.cpp (limited to 'libraries/daguerre/ppm.cpp') diff --git a/libraries/daguerre/ppm.cpp b/libraries/daguerre/ppm.cpp new file mode 100644 index 0000000..e909503 --- /dev/null +++ b/libraries/daguerre/ppm.cpp @@ -0,0 +1,52 @@ +#include +#include + +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 &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(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; + + } + +} -- cgit v1.2.3