From 8e1d7c4e484ef43dddc5074055727694ceb0e37b Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 14 May 2016 16:28:50 +0200 Subject: Extract stream management into `ppm_pixel_stream`, provide Cartesian coordinates --- src/imgen.cc | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'src/imgen.cc') diff --git a/src/imgen.cc b/src/imgen.cc index d574672..25731e1 100644 --- a/src/imgen.cc +++ b/src/imgen.cc @@ -1,34 +1,50 @@ #include "imgen.h" -#include - namespace { inline void write_color(std::ofstream& out, const imgen::color& color) { - out << std::get<0>(color); - out << std::get<1>(color); - out << std::get<2>(color); + out << std::get<0>(color) + << std::get<1>(color) + << std::get<2>(color); } } namespace imgen { +ppm_pixel_stream::ppm_pixel_stream( + const std::string& path, + const std::size_t width, + const std::size_t height +): stream_() { + this->stream_.open(path, std::ios::binary | std::ios::out); + this->stream_.sync_with_stdio(false); + + this->stream_ << "P6\n" << width << " " << height << "\n255\n"; +} + +ppm_pixel_stream& ppm_pixel_stream::operator<<(const color& value) { + write_color(this->stream_, value); + + return *this; +} + void write_ppm( - const std::string& path, - const std::size_t sizeX, - const std::size_t sizeY, - std::function generator + const std::string& path, + const std::size_t width, + const std::size_t height, + std::function generator ) { - std::ofstream out; - out.open(path, std::ios::binary | std::ios::out); - out.sync_with_stdio(false); + ppm_pixel_stream file(path, width, height); - out << "P6\n" << sizeX << " " << sizeY << "\n255\n"; + const std::ptrdiff_t min_y = height / 2 * -1; + const std::ptrdiff_t max_y = height / 2; + const std::ptrdiff_t min_x = width / 2 * -1; + const std::ptrdiff_t max_x = width / 2; - for ( std::size_t posY = 0; posY < sizeY; ++posY ) { - for ( std::size_t posX = 0; posX < sizeX; ++posX ) { - write_color(out, generator(posX, posY)); + for ( std::ptrdiff_t posY = min_y; posY < max_y; ++posY ) { + for ( std::ptrdiff_t posX = min_x; posX < max_x; ++posX ) { + file << generator(posX, posY); } } } -- cgit v1.2.3