Skip to content

Commit f2d30e9

Browse files
authored
Add Ppm output (#1)
1 parent 8385717 commit f2d30e9

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

tray/src/main.cpp

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,45 @@
1-
int main() {}
1+
#include <array>
2+
#include <cassert>
3+
#include <iostream>
4+
#include <ostream>
5+
#include <span>
6+
7+
namespace {
8+
struct Rgb {
9+
std::array<std::uint8_t, 3> values{};
10+
};
11+
12+
struct ImageData {
13+
std::span<Rgb const> data{};
14+
std::array<std::uint32_t, 2> extent{};
15+
};
16+
17+
std::ostream& operator<<(std::ostream& out, ImageData const& image) {
18+
assert(image.data.size() == image.extent[0] * image.extent[1]);
19+
// write header
20+
out << "P3\n" << image.extent[0] << ' ' << image.extent[1] << "\n255\n";
21+
// write each row
22+
for (std::uint32_t row = 0; row < image.extent[1]; ++row) {
23+
// write each column
24+
for (std::uint32_t col = 0; col < image.extent[0]; ++col) {
25+
// compute index
26+
auto const index = row * image.extent[0] + col;
27+
// obtain corresponding Rgb
28+
auto const& rgb = image.data[index];
29+
// write out each channel
30+
for (auto const channel : rgb.values) { out << static_cast<int>(channel) << ' '; }
31+
}
32+
out << '\n';
33+
}
34+
return out;
35+
}
36+
} // namespace
37+
38+
int main() {
39+
static constexpr auto extent = std::array{40U, 30U};
40+
static constexpr auto white_v = Rgb{.values = {0xff, 0xff, 0xff}};
41+
auto buffer = std::array<Rgb, extent[0] * extent[1]>{};
42+
std::fill_n(buffer.data(), buffer.size(), white_v);
43+
auto const image_data = ImageData{.data = buffer, .extent = extent};
44+
std::cout << image_data;
45+
}

0 commit comments

Comments
 (0)