From 914bb670dd46a8a8aa56a1b25066ec89b8a363f2 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Thu, 20 Jan 2022 15:42:43 +0100 Subject: [PATCH] Made ASCII map static --- include/bild.hpp | 4 ++++ src/bild.cpp | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/include/bild.hpp b/include/bild.hpp index 37abd52..764559a 100644 --- a/include/bild.hpp +++ b/include/bild.hpp @@ -13,6 +13,10 @@ */ class Bild { +public: + // ASCII characters used when printing the image to stdcout + static const std::string asciiBrightnessMap; + public: /** * @brief Creates a new empty picture. diff --git a/src/bild.cpp b/src/bild.cpp index 709f795..ec8b6ba 100644 --- a/src/bild.cpp +++ b/src/bild.cpp @@ -3,6 +3,8 @@ #include #include +const std::string Bild::asciiBrightnessMap = ".'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"; + std::ifstream& operator>>(std::ifstream& file, Bild& image) { std::string lineContent; @@ -85,7 +87,7 @@ std::ofstream& operator<<(std::ofstream& file, const Bild& image) std::ostream& operator<<(std::ostream& os, const Bild& image) { - std::string asciiBrightnessMap(".'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$"); + for(uint16_t y = 0; y < image.height; y++) { @@ -95,8 +97,8 @@ std::ostream& operator<<(std::ostream& os, const Bild& image) uint8_t brightness = image.pixels[y * image.width + x]; // Map the pixel brightness (which is in the interval [0, 255]) to the ascii chart length - uint8_t asciiMapIndex = static_cast(brightness * (asciiBrightnessMap.length() - 1) / 255); - lineBuffer << asciiBrightnessMap[asciiMapIndex]; + uint8_t asciiMapIndex = static_cast(brightness * (Bild::asciiBrightnessMap.length() - 1) / 255); + lineBuffer << Bild::asciiBrightnessMap[asciiMapIndex]; } std::cout << lineBuffer.str() << std::endl;