From 64880ba1019f8c81470d697143b7bf48143b2fc8 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sat, 14 May 2016 23:06:20 +0200 Subject: Separate into multiple compilation units, add color functions * fix cartesian coordinate determination --- src/color.cc | 19 +++++++++++++++++++ src/color.h | 24 ++++++++++++++++++++++++ src/imgen.cc | 29 ++--------------------------- src/imgen.h | 21 +++++---------------- src/ppm_pixel_stream.cc | 32 ++++++++++++++++++++++++++++++++ src/ppm_pixel_stream.h | 23 +++++++++++++++++++++++ 6 files changed, 105 insertions(+), 43 deletions(-) create mode 100644 src/color.cc create mode 100644 src/color.h create mode 100644 src/ppm_pixel_stream.cc create mode 100644 src/ppm_pixel_stream.h (limited to 'src') diff --git a/src/color.cc b/src/color.cc new file mode 100644 index 0000000..9af409a --- /dev/null +++ b/src/color.cc @@ -0,0 +1,19 @@ +#include "color.h" + +namespace imgen { + +color red() { return color{255, 0, 0}; } +color lime() { return color{ 0, 255, 0}; } +color blue() { return color{ 0, 0, 255}; } +color yellow() { return color{255, 255, 0}; } +color cyan() { return color{ 0, 255, 255}; } +color magenta() { return color{255, 0, 255}; } +color silver() { return color{192, 192, 192}; } +color maroon() { return color{128, 0, 0}; } +color olive() { return color{128, 128, 0}; } +color green() { return color{ 0, 128, 0}; } +color purple() { return color{128, 0, 128}; } +color teal() { return color{ 0, 128, 128}; } +color navy() { return color{ 0, 0, 128}; } + +} diff --git a/src/color.h b/src/color.h new file mode 100644 index 0000000..2c35465 --- /dev/null +++ b/src/color.h @@ -0,0 +1,24 @@ +#pragma once + +#include +#include + +namespace imgen { + +using color = std::tuple; + +color red(); +color lime(); +color blue(); +color yellow(); +color cyan(); +color magenta(); +color silver(); +color maroon(); +color olive(); +color green(); +color purple(); +color teal(); +color navy(); + +} diff --git a/src/imgen.cc b/src/imgen.cc index 25731e1..027e425 100644 --- a/src/imgen.cc +++ b/src/imgen.cc @@ -1,34 +1,9 @@ #include "imgen.h" -namespace { - -inline void write_color(std::ofstream& out, const imgen::color& color) { - out << std::get<0>(color) - << std::get<1>(color) - << std::get<2>(color); -} - -} +#include "ppm_pixel_stream.h" 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 width, @@ -42,7 +17,7 @@ void write_ppm( const std::ptrdiff_t min_x = width / 2 * -1; const std::ptrdiff_t max_x = width / 2; - for ( std::ptrdiff_t posY = min_y; posY < max_y; ++posY ) { + for ( std::ptrdiff_t posY = max_y; posY > min_y; --posY ) { for ( std::ptrdiff_t posX = min_x; posX < max_x; ++posX ) { file << generator(posX, posY); } diff --git a/src/imgen.h b/src/imgen.h index 35b798d..6954239 100644 --- a/src/imgen.h +++ b/src/imgen.h @@ -1,24 +1,13 @@ +#pragma once + #include -#include -#include #include -namespace imgen { - -using color = std::tuple; +#include "color.h" -class ppm_pixel_stream { - public: - ppm_pixel_stream(const std::string& path, - const std::size_t width, - const std::size_t height); - - ppm_pixel_stream& operator<<(const color& value); - - private: - std::ofstream stream_; +namespace imgen { -}; +using colored_vector = std::tuple; void write_ppm( const std::string& path, diff --git a/src/ppm_pixel_stream.cc b/src/ppm_pixel_stream.cc new file mode 100644 index 0000000..465584b --- /dev/null +++ b/src/ppm_pixel_stream.cc @@ -0,0 +1,32 @@ +#include "ppm_pixel_stream.h" + +namespace { + +inline void write_color(std::ofstream& out, const imgen::color& 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; +} + +} diff --git a/src/ppm_pixel_stream.h b/src/ppm_pixel_stream.h new file mode 100644 index 0000000..9d8f5a8 --- /dev/null +++ b/src/ppm_pixel_stream.h @@ -0,0 +1,23 @@ +#pragma once + +#include +#include + +#include "color.h" + +namespace imgen { + +class ppm_pixel_stream { + public: + ppm_pixel_stream(const std::string& path, + const std::size_t width, + const std::size_t height); + + ppm_pixel_stream& operator<<(const color& value); + + private: + std::ofstream stream_; + +}; + +} -- cgit v1.2.3