libepaper 2.0.0
A C++23 library for controlling Waveshare e-paper displays on Raspberry Pi, featuring transparent sleep/wake management and a fluent builder API.
Loading...
Searching...
No Matches
image_io.hpp
Go to the documentation of this file.
1#pragma once
2
80#include "epaper/core/types.hpp"
81#include <cstddef>
82#include <expected>
83#include <span>
84#include <string_view>
85#include <vector>
86
87namespace epaper {
88
102class ImageIO {
103public:
144 struct ImageResult {
145 std::size_t width;
146 std::size_t height;
147 std::size_t channels;
148 std::vector<std::uint8_t> data; // Row-major, interleaved channels
149 };
150
203 [[nodiscard]] static auto load_image(std::string_view path, int desired_channels = 0)
204 -> std::expected<ImageResult, Error>;
205
264 [[nodiscard]] static auto save_png(std::string_view path, std::size_t width, std::size_t height, int channels,
265 std::span<const std::uint8_t> data) -> std::expected<void, Error>;
266
323 template <FramebufferLike FB> [[nodiscard]] static auto framebuffer_to_rgb(const FB &fb) -> std::vector<std::uint8_t>;
324};
325
326// ========== Template Implementation ==========
327
328template <FramebufferLike FB> auto ImageIO::framebuffer_to_rgb(const FB &fb) -> std::vector<std::uint8_t> {
329 std::size_t w = fb.width();
330 std::size_t h = fb.height();
331 std::vector<std::uint8_t> rgb;
332 rgb.reserve(w * h * 3);
333
334 for (std::size_t y = 0; y < h; ++y) {
335 for (std::size_t x = 0; x < w; ++x) {
336 Color c = fb.get_pixel(x, y, Orientation::Portrait0);
337 auto rgb_val = ColorManager::to_rgb(c);
338 rgb.push_back(rgb_val.r);
339 rgb.push_back(rgb_val.g);
340 rgb.push_back(rgb_val.b);
341 }
342 }
343 return rgb;
344}
345
346} // namespace epaper
static constexpr auto to_rgb(Color color) noexcept -> RGB
Convert Color enum to RGB.
Definition color_manager.hpp:99
Utilities for loading/saving images and converting formats.
Definition image_io.hpp:102
static auto load_image(std::string_view path, int desired_channels=0) -> std::expected< ImageResult, Error >
Definition image_io.cpp:36
static auto save_png(std::string_view path, std::size_t width, std::size_t height, int channels, std::span< const std::uint8_t > data) -> std::expected< void, Error >
Definition image_io.cpp:82
static auto framebuffer_to_rgb(const FB &fb) -> std::vector< std::uint8_t >
Definition image_io.hpp:328
Definition color.hpp:5
@ Portrait0
Default portrait orientation (0° rotation)
Color
Definition types.hpp:32
Definition image_io.hpp:144
std::size_t channels
Definition image_io.hpp:147
std::size_t height
Definition image_io.hpp:146
std::vector< std::uint8_t > data
Definition image_io.hpp:148
std::size_t width
Definition image_io.hpp:145