diff --git a/netpbm.cpp b/netpbm.cpp index 6756a5c2..b74e5434 100644 --- a/netpbm.cpp +++ b/netpbm.cpp @@ -63,7 +63,7 @@ write_netpbm (const uint8_t *restrict pixels, size_t width, size_t height, size_t stride, - const boost::filesystem::path &path, + std::ostream &output, const char* MAGIC) { CHECK (pixels); CHECK_GT (components, 0); @@ -71,11 +71,6 @@ write_netpbm (const uint8_t *restrict pixels, CHECK_GE (stride, width); CHECK_GT (height, 0); - // Establish an output stream - std::ofstream output (path.string ()); - if (!output.good ()) - throw util::output_error ("Unable to open output file"); - // Write the PPM header. output << MAGIC << "\n" << width << "\n" @@ -83,11 +78,17 @@ write_netpbm (const uint8_t *restrict pixels, << (size_t)std::numeric_limits::max () << "\n"; // Write the data rows - for (size_t y = 0; y < height; ++y) { - for (size_t x = 0; x < width; ++x) - for (size_t c = 0; c < components; ++c) - output << pixels[y * stride + x * components + c]; - } + for (size_t y = 0; y < height; ++y) + output.write (reinterpret_cast (pixels + y * stride), width * components); +} + + +//----------------------------------------------------------------------------- +void +util::pgm::write (const util::image::buffer &src, + std::ostream &dst) +{ + write (src.begin (), src.w, src.h, src.s, dst); } @@ -96,6 +97,7 @@ void util::pgm::write (const util::image::buffer &src, const boost::filesystem::path &path) { + std::ofstream dst (path.c_str ()); write (src.begin (), src.w, src.h, src.s, path); } @@ -106,10 +108,24 @@ util::pgm::write (const uint8_t *restrict pixels, size_t width, size_t height, size_t stride, - const boost::filesystem::path &path) { + const boost::filesystem::path &path) +{ + std::ofstream dst (path.c_str ()); + write (pixels, width, height, stride, dst); +} + + +//----------------------------------------------------------------------------- +void +util::pgm::write (const uint8_t *restrict pixels, + size_t width, + size_t height, + size_t stride, + std::ostream &dst) +{ // TODO: We should switch between P2 (ascii) and P5 (binary) static const char MAGIC[] = "P5"; - write_netpbm (pixels, 1, width, height, stride, path, MAGIC); + write_netpbm (pixels, 1, width, height, stride, dst, MAGIC); } @@ -119,8 +135,22 @@ util::ppm::write (const uint8_t *restrict pixels, size_t width, size_t height, size_t stride, - const boost::filesystem::path &path) { + const boost::filesystem::path &path) +{ + std::ofstream dst (path.c_str ()); + write (pixels, width, height, stride, dst); +} + + +//----------------------------------------------------------------------------- +void +util::ppm::write (const uint8_t *restrict pixels, + size_t width, + size_t height, + size_t stride, + std::ostream &dst) +{ // TODO: We should switch between P3 (ascii) and P6 (binary) static const char MAGIC[] = "P6"; - write_netpbm (pixels, 3, width, height, stride, path, MAGIC); + write_netpbm (pixels, 3, width, height, stride, dst, MAGIC); } diff --git a/netpbm.hpp b/netpbm.hpp index 69b9d6f8..04339c2e 100644 --- a/netpbm.hpp +++ b/netpbm.hpp @@ -21,6 +21,7 @@ #include #include +#include #include namespace util { @@ -29,8 +30,15 @@ namespace util { static image::buffer read (const boost::filesystem::path&); static void write (const image::buffer &src, - const boost::filesystem::path &); + const boost::filesystem::path &dst); + static void write (const image::buffer &src, + std::ostream &dst); + static void write (const uint8_t *restrict pixels, + size_t width, + size_t height, + size_t stride, + std::ostream &dst); static void write (const uint8_t *restrict pixels, size_t width, size_t height, @@ -45,6 +53,11 @@ namespace util { size_t height, size_t stride, const boost::filesystem::path &path); + static void write (const uint8_t *restrict pixels, + size_t width, + size_t height, + size_t stride, + std::ostream &dst); }; }