Skip to content

Add Ppm output #1

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

Merged
merged 1 commit into from
Sep 13, 2022
Merged
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
46 changes: 45 additions & 1 deletion tray/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1 +1,45 @@
int main() {}
#include <array>
#include <cassert>
#include <iostream>
#include <ostream>
#include <span>

namespace {
struct Rgb {
std::array<std::uint8_t, 3> values{};
};

struct ImageData {
std::span<Rgb const> data{};
std::array<std::uint32_t, 2> extent{};
};

std::ostream& operator<<(std::ostream& out, ImageData const& image) {
assert(image.data.size() == image.extent[0] * image.extent[1]);
// write header
out << "P3\n" << image.extent[0] << ' ' << image.extent[1] << "\n255\n";
// write each row
for (std::uint32_t row = 0; row < image.extent[1]; ++row) {
// write each column
for (std::uint32_t col = 0; col < image.extent[0]; ++col) {
// compute index
auto const index = row * image.extent[0] + col;
// obtain corresponding Rgb
auto const& rgb = image.data[index];
// write out each channel
for (auto const channel : rgb.values) { out << static_cast<int>(channel) << ' '; }
}
out << '\n';
}
return out;
}
} // namespace

int main() {
static constexpr auto extent = std::array{40U, 30U};
static constexpr auto white_v = Rgb{.values = {0xff, 0xff, 0xff}};
auto buffer = std::array<Rgb, extent[0] * extent[1]>{};
std::fill_n(buffer.data(), buffer.size(), white_v);
auto const image_data = ImageData{.data = buffer, .extent = extent};
std::cout << image_data;
}