Skip to content

grayscale-image-algo exercise #857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
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
38 changes: 38 additions & 0 deletions homework/grayscale-image/compression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "compression.hpp"
#include <algorithm>

std::vector<std::pair<uint8_t, uint8_t>> compressGrayscale(std::array<std::array<uint8_t, width>, height>& bitimg) {
std::vector<std::pair<uint8_t, uint8_t>> compressed;

std::for_each(bitimg.begin(), bitimg.end(), [&compressed](auto& column) {
int count = 1;
std::for_each(std::next(column.begin()), column.end(), [&compressed, &count, &column](auto& pixel) {
int row = std::distance(column.begin(), &pixel);
if (pixel == column[row - 1]) {
count++;
} else {
compressed.push_back({column[row - 1], count});
count = 1;
}
if (row == column.size() - 1) {
compressed.push_back({pixel, count});
}
});
});

return compressed;
}

std::array<std::array<uint8_t, width>, height> decompressGrayscale(std::vector<std::pair<uint8_t, uint8_t>>& compressed) {
std::array<std::array<uint8_t, width>, height> decompressed;

int index = 0;
std::for_each(compressed.begin(), compressed.end(), [&decompressed, &index](const std::pair<uint8_t, uint8_t>& c) {
uint8_t value = c.first;
uint8_t count = c.second;
std::generate_n(&decompressed[index / height][index % height], count, [value]() { return value; });
index += count;
});

return decompressed;
}
12 changes: 12 additions & 0 deletions homework/grayscale-image/compression.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once
#include <array>
#include <cstdint>
#include <iterator>
#include <map>
#include <vector>

constexpr std::size_t width = 32;
constexpr std::size_t height = 32;

std::vector<std::pair<uint8_t, uint8_t>> compressGrayscale(std::array<std::array<uint8_t, width>, height>& bitimg);
std::array<std::array<uint8_t, width>, height> decompressGrayscale(std::vector<std::pair<uint8_t, uint8_t>>& bitimg);
5 changes: 4 additions & 1 deletion homework/grayscale-image/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <algorithm>
#include <array>
#include <cstdint>
#include <forward_list>
#include <iterator>

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

std::array<std::array<uint8_t, 32>, 32> generateNinja() {
return {
Expand Down
2 changes: 1 addition & 1 deletion 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 Down