From 21423a44d6e3091addc00c1b0769336e1f154d4e Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Sun, 8 May 2016 19:50:45 +0200 Subject: 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. --- CMakeLists.txt | 17 +++++++++++++++++ src/imgen.cc | 36 ++++++++++++++++++++++++++++++++++++ src/imgen.h | 16 ++++++++++++++++ test.cc | 12 ++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 src/imgen.cc create mode 100644 src/imgen.h create mode 100644 test.cc diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..88200b9 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,17 @@ +cmake_minimum_required(VERSION 2.8) +project(imgen) + +set( + CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS} -std=c++14 -W -Wall -Wextra -Winline -pedantic" +) + +include_directories( + src/ +) + +add_executable( + test + test.cc + src/imgen.cc +) 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 + +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 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 +#include +#include + +namespace imgen { + +using color = std::tuple; + +void write_ppm( + const std::string& path, + const std::size_t sizeX, + const std::size_t sizeY, + std::function generator +); + +} diff --git a/test.cc b/test.cc new file mode 100644 index 0000000..36f637d --- /dev/null +++ b/test.cc @@ -0,0 +1,12 @@ +#include + +int main(int, char*[]) { + imgen::write_ppm( + "test.ppm", + 500, + 500, + [](std::size_t x, std::size_t y) -> imgen::color { + return imgen::color(255, x % 255, y % 255); + } + ); +} -- cgit v1.2.3