Add processing of ppm image into color output

This commit is contained in:
Bob Polis 2024-01-19 12:05:14 +01:00
parent 52ad6f619d
commit a45ebc04dc
2 changed files with 34 additions and 0 deletions

26
src/pixels.cpp Normal file
View File

@ -0,0 +1,26 @@
#include <memory>
#include <libscterm.hpp>
#include <netpbm/pam.h>
#include "pixels.hpp"
void write_image(std::ostream& os, const std::string& path) {
std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close};
struct pam info;
pnm_readpaminit(infile.get(), &info, PAM_STRUCT_SIZE(tuple_type));
std::unique_ptr<tuple, void(*)(void*)> row1 {pnm_allocpamrow(&info), pm_freerow};
std::unique_ptr<tuple, void(*)(void*)> row2 {pnm_allocpamrow(&info), pm_freerow};
for (int y = 0; y < info.height; y += 2) {
pnm_readpamrow(&info, row1.get());
pnm_readpamrow(&info, row2.get());
for (int x = 0; x < info.width; ++x) {
if (info.depth == 3) { // assume RGB, 1 byte per sample
tuple* t1 {row1.get()};
os << sc::io::truecolorb(t1[x][0], t1[x][1], t1[x][2]);
tuple* t2 {row2.get()};
os << sc::io::truecolorf(t2[x][0], t2[x][1], t2[x][2]);
os << u8"\u2584"; // unicode lower half block
}
}
os << sc::io::reset << '\n';
}
}

8
src/pixels.hpp Normal file
View File

@ -0,0 +1,8 @@
#ifndef PIXELS_H_
#define PIXELS_H_
#include <iostream>
void write_image(std::ostream& os, const std::string& path);
#endif // PIXELS_H_