From e5306ac3cc6885014f788b38f88ad0b57f6ef7b2 Mon Sep 17 00:00:00 2001 From: MichalG315 Date: Thu, 7 Aug 2025 20:31:22 +0200 Subject: [PATCH] comp and decomp works with algo --- homework/grayscale-image/compression.cpp | 53 ++++++++++++++++++++++++ homework/grayscale-image/compression.hpp | 11 +++++ homework/grayscale-image/main.cpp | 4 +- homework/grayscale-image/test.cpp | 2 +- 4 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 homework/grayscale-image/compression.cpp create mode 100644 homework/grayscale-image/compression.hpp diff --git a/homework/grayscale-image/compression.cpp b/homework/grayscale-image/compression.cpp new file mode 100644 index 000000000..6708c9c60 --- /dev/null +++ b/homework/grayscale-image/compression.cpp @@ -0,0 +1,53 @@ +#include "compression.hpp" +#include +#include +#include +#include +#include + +std::vector> compressGrayscale(std::array, height>& array) { + uint8_t val = 0; + uint8_t counter = 0; + std::vector> output; + + for (auto& row : array) { + uint8_t current = row[0]; + uint8_t counter = 1; + + std::accumulate(std::next(begin(row)), + end(row), + 0, + [&](int, auto val) { + if (val == current) { + counter++; + } else { + output.emplace_back(current, counter); + current = val; + counter = 1; + } + return 0; + }); + output.emplace_back(current, counter); + } + + return output; +}; + +std::array, height> decompressGrayscale(std::vector>& compressed) { + std::array, height> array; + auto row = 0; + auto column = 0; + + std::for_each(compressed.begin(), compressed.end(), [&](const std::pair& pair) { + for (auto i = 0; i < pair.second; ++i) { + array[row][column] = pair.first; + column++; + } + if (column == width) { + column = 0; + row++; + } + }); + + return array; +}; \ No newline at end of file diff --git a/homework/grayscale-image/compression.hpp b/homework/grayscale-image/compression.hpp new file mode 100644 index 000000000..1d31f952d --- /dev/null +++ b/homework/grayscale-image/compression.hpp @@ -0,0 +1,11 @@ +#pragma once +#include +#include +#include + +constexpr std::size_t width = 32U; +constexpr std::size_t height = 32U; + +std::vector> compressGrayscale(std::array, height>& array); + +std::array, height> decompressGrayscale(std::vector>& vec); \ No newline at end of file diff --git a/homework/grayscale-image/main.cpp b/homework/grayscale-image/main.cpp index b9dc994f0..ed8cb87c2 100644 --- a/homework/grayscale-image/main.cpp +++ b/homework/grayscale-image/main.cpp @@ -1,8 +1,8 @@ #include +#include "compression.hpp" +#include #include -// TODO: include - std::array, 32> generateNinja() { return { std::array{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 36, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, diff --git a/homework/grayscale-image/test.cpp b/homework/grayscale-image/test.cpp index 3b2f1bca6..223eeb095 100644 --- a/homework/grayscale-image/test.cpp +++ b/homework/grayscale-image/test.cpp @@ -3,7 +3,7 @@ #include // for std::pair<> #include -// TODO: include +#include "compression.hpp" #include "gtest/gtest.h" void expectBitmap(const std::vector>& bitmap, size_t fraction) {