My omnium-gatherom of scripts and source code.
1/* ========================================================================
2 *
3 * Filename: main.cpp
4 * Description: data file renderer
5 * Author: Diego Estrada
6 * Version: 0.0.1
7 *
8 * ======================================================================== */
9#include <functional>
10#include <ranges>
11#include <algorithm>
12#include <array>
13#include <cstdint>
14#include <fstream>
15#include <iostream>
16#include <iterator>
17#include <numeric>
18#include <optional>
19#include <vector>
20
21namespace ra = std::ranges;
22namespace vi = ra::views;
23
24template <typename T> using ref = T&;
25
26template <typename T> using ptr = T*;
27
28struct Bytes {
29 std::vector<uint8_t> bytes;
30
31 auto read_file(ref<const std::string> file_path)
32 {
33 auto input = std::ifstream(file_path, std::ios::binary);
34
35 bytes = std::vector<uint8_t>(
36 (std::istreambuf_iterator<char>(input)),
37 (std::istreambuf_iterator<char>()));
38
39 input.close();
40 }
41};
42
43template<size_t _rows, size_t _cols>
44struct Map {
45 std::vector<char> map;
46
47 Map()
48 {
49 for (auto& e : map) e = ' ';
50 }
51
52 auto operator () (size_t x, size_t y, char c)
53 {
54 std::cout << (x & 0xFF) << ' ' << y << '\n';
55 if (x < _rows && y < _cols)
56 map[x + (y * _cols)] = c;
57 }
58
59 auto rows() const -> size_t
60 {
61 return _rows;
62 }
63
64 auto cols() const -> size_t
65 {
66 return _cols;
67 }
68};
69
70struct Graph {
71 Map<0xFF, 0xFF> mp;
72 Graph(ref<const Bytes> b) : mp()
73 {
74 auto view = vi::slide(b.bytes, 2);
75
76 auto const mapper = [this](ra::viewable_range auto&& r) {
77 std::cout << r.size() << std::endl;
78 [[maybe_unused]] auto const i = r[0];
79 [[maybe_unused]] auto const j = r[1];
80 mp(i & 0xFF, j & 0xFF, '.');
81 };
82
83 ra::for_each(view, mapper);
84 }
85
86 auto show() const -> void
87 {
88 for(size_t i = 0; i < mp.map.size(); ++i) {
89 if (i % mp.cols() == 0)
90 std::cout << std::endl;
91 std::cout << mp.map.at(i);
92 }
93 }
94};
95
96auto main([[maybe_unused]] int argc, [[maybe_unused]] char** argv) -> int
97{
98
99 auto k = Bytes();
100
101 // k.read_file("Makefile");
102 k.read_file("/home/Michorron/Media/Pictures/Wallpapers/totoro.jpeg");
103
104 auto g = Graph(k);
105
106 g.show();
107
108 return 0;
109}