Allow input from stdin

This commit is contained in:
2024-01-27 16:58:52 +01:00
parent 17b32097fe
commit d4b0ae591d
3 changed files with 14 additions and 5 deletions

View File

@@ -8,13 +8,12 @@ static inline int t2p(int val) {
return .5 + 5 * val / 255.0;
}
void write_image(std::ostream& os, const std::string& path) {
static void write(std::ostream& os, FILE* in) {
char* tc = getenv("TRUECOLOR");
std::string truecolor {tc ? tc : ""};
bool has_truecolor = truecolor.size() > 0;
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));
pnm_readpaminit(in, &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) {
@@ -40,3 +39,12 @@ void write_image(std::ostream& os, const std::string& path) {
os << sc::io::reset << '\n';
}
}
void write_image(std::ostream& os) {
write(os, stdin);
}
void write_image(std::ostream& os, const std::string& path) {
std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close};
write(os, infile.get());
}