diff options
author | Adrian Kummerlaender | 2016-05-08 19:50:45 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2016-05-08 19:50:45 +0200 |
commit | 21423a44d6e3091addc00c1b0769336e1f154d4e (patch) | |
tree | be468b159f478e70d6d2049d988dbc29c31dcc76 /src | |
download | voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.tar voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.tar.gz voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.tar.bz2 voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.tar.lz voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.tar.xz voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.tar.zst voronoi-21423a44d6e3091addc00c1b0769336e1f154d4e.zip |
Implement basic PPM writing function
`write_ppm` generates a PPM image using a given generation function.
Output is written using `std::ofstream`.
The library is intended to become a straight forward way to test out various visualizations.
Diffstat (limited to 'src')
-rw-r--r-- | src/imgen.cc | 36 | ||||
-rw-r--r-- | src/imgen.h | 16 |
2 files changed, 52 insertions, 0 deletions
diff --git a/src/imgen.cc b/src/imgen.cc new file mode 100644 index 0000000..d574672 --- /dev/null +++ b/src/imgen.cc @@ -0,0 +1,36 @@ +#include "imgen.h" + +#include <fstream> + +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); +} + +} + +namespace imgen { + +void write_ppm( + const std::string& path, + const std::size_t sizeX, + const std::size_t sizeY, + std::function<color(std::size_t, std::size_t)> generator +) { + std::ofstream out; + out.open(path, std::ios::binary | std::ios::out); + out.sync_with_stdio(false); + + out << "P6\n" << sizeX << " " << sizeY << "\n255\n"; + + for ( std::size_t posY = 0; posY < sizeY; ++posY ) { + for ( std::size_t posX = 0; posX < sizeX; ++posX ) { + write_color(out, generator(posX, posY)); + } + } +} + +} diff --git a/src/imgen.h b/src/imgen.h new file mode 100644 index 0000000..5ddbaf6 --- /dev/null +++ b/src/imgen.h @@ -0,0 +1,16 @@ +#include <string> +#include <utility> +#include <functional> + +namespace imgen { + +using color = std::tuple<std::uint8_t, std::uint8_t, std::uint8_t>; + +void write_ppm( + const std::string& path, + const std::size_t sizeX, + const std::size_t sizeY, + std::function<color(std::size_t, std::size_t)> generator +); + +} |