aboutsummaryrefslogtreecommitdiff
path: root/src/imgen.cc
blob: 25731e1602f3ff50242c4f1739007147552000c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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);
}

}

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,
	const std::size_t                                    height,
	std::function<color(std::ptrdiff_t, std::ptrdiff_t)> generator
) {
	ppm_pixel_stream file(path, width, height);

	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::ptrdiff_t posY = min_y; posY < max_y; ++posY ) {
		for ( std::ptrdiff_t posX = min_x; posX < max_x; ++posX ) {
			file << generator(posX, posY);
		}
	}
}

}