Porytiles
Loading...
Searching...
No Matches
png_rgba_image_loader.cpp
Go to the documentation of this file.
2
3#include <filesystem>
4#include <memory>
5
6#include "png++/png.hpp"
7
10
11namespace porytiles {
12
13ChainableResult<std::unique_ptr<Image<Rgba32>>, ImageLoadError>
14PngRgbaImageLoader::load_from_file(const std::filesystem::path &path) const
15{
16 if (!exists(path)) {
17 return ImageLoadError::file_not_found(path.string());
18 }
19
20 png::image<png::rgba_pixel> png{};
21 try {
22 png.read(path.string());
23 }
24 catch (const std::exception &e) {
25 return ImageLoadError::other_load_error(path.string(), e.what());
26 }
27
28 const auto width = static_cast<std::size_t>(png.get_width());
29 const auto height = static_cast<std::size_t>(png.get_height());
30
31 Image<Rgba32> image{width, height};
32
33 for (std::size_t row = 0; row < height; ++row) {
34 for (std::size_t col = 0; col < width; ++col) {
35 const auto &pixel = png[row][col];
36 image.set(row, col, Rgba32{pixel.red, pixel.green, pixel.blue, pixel.alpha});
37 }
38 }
39
40 return std::make_unique<Image<Rgba32>>(std::move(image));
41}
42
43} // namespace porytiles
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
void set(std::size_t i, PixelType pixel)
Sets the pixel value at a given one-dimensional pixel index.
Definition image.hpp:88
ChainableResult< std::unique_ptr< Image< Rgba32 > >, ImageLoadError > load_from_file(const std::filesystem::path &path) const
Represents a 32-bit RGBA color.
Definition rgba32.hpp:23
std::uint8_t red() const
Definition rgba32.hpp:81