Tatooine
write_png.h
Go to the documentation of this file.
1#ifndef TATOOINE_WRITE_PNG_H
2#define TATOOINE_WRITE_PNG_H
3//==============================================================================
4#include <tatooine/concepts.h>
5
7#include <tatooine/png.h>
8//==============================================================================
9namespace tatooine {
10//==============================================================================
11#if TATOOINE_PNG_AVAILABLE
12template <arithmetic T>
13void write_png(filesystem::path const& path,
14 std::vector<T> const& data, std::size_t width, std::size_t height) {
15 auto image = png::image<png::rgb_pixel>{static_cast<png::uint_32>(width),
16 static_cast<png::uint_32>(height)};
17 for (png::uint_32 y = 0; y < image.get_height(); ++y) {
18 for (png::uint_32 x = 0; x < image.get_width(); ++x) {
19 auto idx = x + static_cast<png::uint_32>(width) * y;
20 auto d = data[idx];
21 if (std::isnan(d)) {
22 d = 0;
23 } else {
24 d = std::clamp(d, T(0), T(1));
25 }
26 image[image.get_height() - 1 - y][x].red =
27 image[image.get_height() - 1 - y][x].green =
28 image[image.get_height() - 1 - y][x].blue = d * 255;
29 }
30 }
31 image.write(path.string());
32}
33#endif
34//==============================================================================
35} // namespace tatooine
36//==============================================================================
37#endif
Definition: algorithm.h:6
void write_png(filesystem::path const &path, std::vector< T > const &data, std::size_t width, std::size_t height)
Definition: write_png.h:13