diff --git a/src/main.cpp b/src/main.cpp index 0323c42..d7acf3f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,8 +50,8 @@ int main(int argc, char* argv[]) { } } if (optind == argc) { - print_help(); - return EXIT_FAILURE; + // no file arg => read from stdin + write_image(std::cout); } for (int i = optind; i < argc; ++i) { try { diff --git a/src/pixels.cpp b/src/pixels.cpp index 715cc6b..b0d7cb0 100644 --- a/src/pixels.cpp +++ b/src/pixels.cpp @@ -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 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 row1 {pnm_allocpamrow(&info), pm_freerow}; std::unique_ptr 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 infile {pm_openr(path.c_str()), pm_close}; + write(os, infile.get()); +} diff --git a/src/pixels.hpp b/src/pixels.hpp index 48e9338..c16bec7 100644 --- a/src/pixels.hpp +++ b/src/pixels.hpp @@ -3,6 +3,7 @@ #include +void write_image(std::ostream& os); void write_image(std::ostream& os, const std::string& path); #endif // PIXELS_H_