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
/mnt/nas/libepaper/include/epaper/io/image_io.hpp
// Load and display image
auto result = ImageIO::load_image("logo.png", 3); // Force RGB
if (result) {
auto [w, h, ch, data] = *result;
// Convert RGB to framebuffer color mode
for (size_t y = 0; y < h; ++y) {
for (size_t x = 0; x < w; ++x) {
size_t idx = (y * w + x) * 3;
RGB rgb{data[idx], data[idx+1], data[idx+2]};
Color c = ColorManager::to_color(rgb, DisplayMode::BlackWhite);
framebuffer.set_pixel(x, y, c, Orientation::Portrait0);
}
}
}
// Save framebuffer as PNG
auto rgb = ImageIO::framebuffer_to_rgb(framebuffer);
ImageIO::save_png("output.png", fb.width(), fb.height(), 3, rgb);
See also
ColorManager, FramebufferLike, stb_image (third_party)
#pragma once
#include <cstddef>
#include <expected>
#include <span>
#include <string_view>
#include <vector>
namespace epaper {
class ImageIO {
public:
struct ImageResult {
std::size_t width;
std::size_t height;
std::size_t channels;
std::vector<std::uint8_t> data; // Row-major, interleaved channels
};
[[nodiscard]] static auto load_image(std::string_view path, int desired_channels = 0)
-> std::expected<ImageResult, Error>;
[[nodiscard]] 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>;
template <FramebufferLike FB> [[nodiscard]] static auto framebuffer_to_rgb(const FB &fb) -> std::vector<std::uint8_t>;
};
// ========== Template Implementation ==========
template <FramebufferLike FB> auto ImageIO::framebuffer_to_rgb(const FB &fb) -> std::vector<std::uint8_t> {
std::size_t w = fb.width();
std::size_t h = fb.height();
std::vector<std::uint8_t> rgb;
rgb.reserve(w * h * 3);
for (std::size_t y = 0; y < h; ++y) {
for (std::size_t x = 0; x < w; ++x) {
Color c = fb.get_pixel(x, y, Orientation::Portrait0);
auto rgb_val = ColorManager::to_rgb(c);
rgb.push_back(rgb_val.r);
rgb.push_back(rgb_val.g);
rgb.push_back(rgb_val.b);
}
}
return rgb;
}
} // namespace epaper
static constexpr auto to_rgb(Color color) noexcept -> RGB
Convert Color enum to RGB.
Definition color_manager.hpp:99
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
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