Allow input from stdin

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

View File

@ -50,8 +50,8 @@ int main(int argc, char* argv[]) {
} }
} }
if (optind == argc) { if (optind == argc) {
print_help(); // no file arg => read from stdin
return EXIT_FAILURE; write_image(std::cout);
} }
for (int i = optind; i < argc; ++i) { for (int i = optind; i < argc; ++i) {
try { try {

View File

@ -8,13 +8,12 @@ static inline int t2p(int val) {
return .5 + 5 * val / 255.0; 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"); char* tc = getenv("TRUECOLOR");
std::string truecolor {tc ? tc : ""}; std::string truecolor {tc ? tc : ""};
bool has_truecolor = truecolor.size() > 0; bool has_truecolor = truecolor.size() > 0;
std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close};
struct pam info; 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*)> row1 {pnm_allocpamrow(&info), pm_freerow};
std::unique_ptr<tuple, void(*)(void*)> row2 {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) { 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'; 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());
}

View File

@ -3,6 +3,7 @@
#include <iostream> #include <iostream>
void write_image(std::ostream& os);
void write_image(std::ostream& os, const std::string& path); void write_image(std::ostream& os, const std::string& path);
#endif // PIXELS_H_ #endif // PIXELS_H_