Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions homework/grayscale-image/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG main # release-1.10.0
GIT_TAG main # release-1.10.0
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# Now simply link against gtest or gtest_main as needed. Eg

project(grayscaleImages)
enable_testing()

Expand Down
71 changes: 71 additions & 0 deletions homework/grayscale-image/compression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "compression.hpp"
#include <algorithm>
#include <cstring>
#include <iostream>
#include <numeric>

auto compressGrayscale(std::array<std::array<uint8_t, width>, height>& bitmap) -> std::vector<std::pair<uint8_t, uint8_t>> {
std::vector<std::pair<uint8_t, uint8_t>> compression;
compression.reserve(height * width);
for (const auto& row : bitmap) {
compression.emplace_back(std::make_pair(row[0], 0));
for (const auto& pixel : row) {
if (pixel == compression.back().first) {
++compression.back().second;
} else {
compression.emplace_back(std::make_pair(pixel, 1));
}
}
}

return compression;
}

auto decompressGrayscale(std::vector<std::pair<uint8_t, uint8_t>>& compressed_bitmap) -> std::array<std::array<uint8_t, width>, height> {
if (std::accumulate(
compressed_bitmap.begin(),
compressed_bitmap.end(),
0,
[](size_t sum, const std::pair<uint8_t, uint8_t>& entry) { return sum + entry.second; }) != height * width) {
throw std::invalid_argument("decompressGrayscale: invalid pixels count in 'compressed_bitmap'.");
}
std::array<std::array<uint8_t, width>, height>
decompression{0};
auto it = decompression[0].data();
for (const auto& [value, count] : compressed_bitmap) {
std::memset(it, value, count);
it += count;
}

return decompression;
}

auto printMap(const std::array<std::array<uint8_t, width>, height>& bitmap) -> void {
for (const auto& row : bitmap) {
for (const auto pixel : row) {
std::cout << pixel;
}
std::cout << "\n";
}
}

auto gen_bitmap(const size_t fraction) -> std::array<std::array<uint8_t, width>, height> {
if (fraction == 0) {
throw std::invalid_argument("gen_bitmap: expected positive 'fraction' num.");
}
std::array<std::array<uint8_t, width>, height> bitmap{0};

std::vector<int> values(fraction);
int curr_val = 0;
for (auto& value : values) {
value = curr_val++;
}

for (auto& row : bitmap) {
auto it = row.begin();
for (const auto val : values) {
it = std::fill_n(it, width / values.size() + (width % values.size() != 0), val);
}
}
return bitmap;
}
16 changes: 16 additions & 0 deletions homework/grayscale-image/compression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#pragma once
#include <array>
#include <cstdint>
#include <utility>
#include <vector>

constexpr size_t width = 64;
constexpr size_t height = 64;

auto compressGrayscale(std::array<std::array<uint8_t, width>, height>& bitmap) -> std::vector<std::pair<uint8_t, uint8_t>>;

auto decompressGrayscale(std::vector<std::pair<uint8_t, uint8_t>>& compressed_bitmap) -> std::array<std::array<uint8_t, width>, height>;

auto printMap(const std::array<std::array<uint8_t, width>, height>& bitmap) -> void;

auto gen_bitmap(const size_t fraction) -> std::array<std::array<uint8_t, width>, height>;
7 changes: 4 additions & 3 deletions homework/grayscale-image/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <array>
#include <forward_list>
#include <iostream>

// TODO: include
#include "compression.hpp"

std::array<std::array<uint8_t, 32>, 32> generateNinja() {
return {
Expand Down Expand Up @@ -43,8 +44,8 @@ std::array<std::array<uint8_t, 32>, 32> generateNinja() {
int main() {
auto ninja = generateNinja();
// printMap(ninja);
auto compressed = compressGrayscale(ninja);
auto decompressed = decompressGrayscale(compressed);
// auto compressed = compressGrayscale(ninja);
// auto decompressed = decompressGrayscale(compressed);
// printMap(decompressed);

return 0;
Expand Down
71 changes: 29 additions & 42 deletions homework/grayscale-image/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <utility> // for std::pair<>
#include <vector>

// TODO: include
#include "compression.hpp"
#include "gtest/gtest.h"

void expectBitmap(const std::vector<std::pair<uint8_t, uint8_t>>& bitmap, size_t fraction) {
Expand All @@ -28,73 +28,60 @@ std::vector<std::pair<uint8_t, uint8_t>> getBitmap(size_t fraction) {
}

TEST(compressionTests, ShouldCompressWholeLines) {
std::array<std::array<uint8_t, width>, height> arr;
for (int i = 0; i < height; ++i)
for (int j = 0; j < width; ++j)
arr[i][j] = 0;
auto arr = gen_bitmap(1);

auto bitmap = compressGrayscale(arr);
ASSERT_EQ(bitmap.size(), height);
expectBitmap(bitmap, 1);
}

TEST(compressionTests, ShouldCompressHalfLines) {
std::array<std::array<uint8_t, width>, height> arr;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width / 2; ++j)
arr[i][j] = 0;
for (int j = width / 2; j < width; ++j)
arr[i][j] = 1;
}
auto arr = gen_bitmap(2);

auto bitmap = compressGrayscale(arr);
expectBitmap(bitmap, 2);
}

TEST(compressionTests, ShouldCompressQuaterLines) {
std::array<std::array<uint8_t, width>, height> arr;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width / 4; ++j)
arr[i][j] = 0;
for (int j = width / 4; j < width / 2; ++j)
arr[i][j] = 1;
for (int j = width / 2; j < width / (4.0 / 3.0); ++j)
arr[i][j] = 2;
for (int j = width / (4.0 / 3.0); j < width; ++j)
arr[i][j] = 3;
}
auto arr = gen_bitmap(4);

auto bitmap = compressGrayscale(arr);
ASSERT_EQ(bitmap.size(), height * 4);
expectBitmap(bitmap, 4);
}

TEST(compressionTests, ShouldCompressOneEighthLines) {
std::array<std::array<uint8_t, width>, height> arr;
for (int i = 0; i < height; ++i) {
for (int j = 0; j < width / 8; ++j)
arr[i][j] = 0;
for (int j = width / 8; j < width / 4; ++j)
arr[i][j] = 1;
for (int j = width / 4; j < width / (8.0 / 3.0); ++j)
arr[i][j] = 2;
for (int j = width / (8.0 / 3.0); j < width / 2; ++j)
arr[i][j] = 3;
for (int j = width / 2; j < width / (8.0 / 5.0); ++j)
arr[i][j] = 4;
for (int j = width / (8.0 / 5.0); j < width / (4.0 / 3.0); ++j)
arr[i][j] = 5;
for (int j = width / (4.0 / 3.0); j < width / (8.0 / 7.0); ++j)
arr[i][j] = 6;
for (int j = width / (8.0 / 7.0); j < width; ++j)
arr[i][j] = 7;
}
auto arr = gen_bitmap(8);

auto bitmap = compressGrayscale(arr);
ASSERT_EQ(bitmap.size(), height * 8);
expectBitmap(bitmap, 8);
}

TEST(compressionTests, ShouldCompressOneSixteenthLines) {
auto arr = gen_bitmap(16);

auto bitmap = compressGrayscale(arr);
ASSERT_EQ(bitmap.size(), height * 16);
expectBitmap(bitmap, 16);
}

TEST(compressionTests, ShouldCompressOneThirtySecondLines) {
auto arr = gen_bitmap(32);

auto bitmap = compressGrayscale(arr);
ASSERT_EQ(bitmap.size(), height * 32);
expectBitmap(bitmap, 32);
}

TEST(compressionTests, ShouldCompressOneSixtyFourthLines) {
auto arr = gen_bitmap(64);

auto bitmap = compressGrayscale(arr);
ASSERT_EQ(bitmap.size(), height * 64);
expectBitmap(bitmap, 64);
}

TEST(compressionTests, ShouldDecompressWholeLines) {
constexpr size_t fraction = 1;
auto bitmap = getBitmap(fraction);
Expand Down