resource/image: add buffer option to image resource (#39)
Some checks failed
Build & Test (Arch) / Arch: Build and Test (gcc) (push) Has been cancelled
Build & Test (Arch) / Arch: Build and Test (clang) (push) Has been cancelled
Build & Test / nix (hyprgraphics) (push) Has been cancelled
Build & Test / nix (hyprgraphics-with-tests) (push) Has been cancelled

* add buffer option to image resource

There were also some changes made to the types of the span. The content
is now always a const uint8_t, as if shouldn't be mutable. We can also
omit the reference to the span, as it is very lightweight and can
therefore be copied

* move buffer to StaticImageResource

* minor fixes

* minor fixes

again
This commit is contained in:
Felix Salcher 2025-10-14 14:37:28 +02:00 committed by GitHub
parent 9431db625c
commit 50fb9f0692
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 62 additions and 13 deletions

View file

@ -15,7 +15,7 @@ namespace Hyprgraphics {
class CImage {
public:
CImage(const std::string& path, const Hyprutils::Math::Vector2D& size = {} /* for SVG */);
CImage(const std::span<uint8_t>&, eImageFormat);
CImage(const std::span<const uint8_t>, eImageFormat);
~CImage();
CImage(const CImage&) = delete;

View file

@ -1,12 +1,12 @@
#pragma once
#include "AsyncResource.hpp"
#include <string>
#include <hyprutils/math/Vector2D.hpp>
#include "./AsyncResource.hpp"
#include "../../color/Color.hpp"
#include <optional>
#include <hyprutils/math/Vector2D.hpp>
namespace Hyprgraphics {
class CImageResource : public IAsyncResource {
public:

View file

@ -0,0 +1,29 @@
#pragma once
#include "./AsyncResource.hpp"
#include "../../color/Color.hpp"
#include "hyprgraphics/image/Image.hpp"
#include <optional>
#include <hyprutils/math/Vector2D.hpp>
namespace Hyprgraphics {
class CStaticImageResource : public IAsyncResource {
public:
enum eTextAlignmentMode : uint8_t {
TEXT_ALIGN_LEFT = 0,
TEXT_ALIGN_CENTER,
TEXT_ALIGN_RIGHT,
};
CStaticImageResource(const std::span<const uint8_t> data, eImageFormat format);
virtual ~CStaticImageResource() = default;
virtual void render();
private:
const std::span<const uint8_t> m_data;
const eImageFormat m_format = eImageFormat::IMAGE_FORMAT_PNG;
};
};

View file

@ -17,7 +17,7 @@ using namespace Hyprgraphics;
using namespace Hyprutils::Memory;
using namespace Hyprutils::Math;
Hyprgraphics::CImage::CImage(const std::span<uint8_t>& data, eImageFormat format) {
Hyprgraphics::CImage::CImage(const std::span<const uint8_t> data, eImageFormat format) {
std::expected<cairo_surface_t*, std::string> CAIROSURFACE;
if (format == eImageFormat::IMAGE_FORMAT_PNG) {
CAIROSURFACE = PNG::createSurfaceFromPNG(data);

View file

@ -78,7 +78,7 @@ std::expected<cairo_surface_t*, std::string> AVIF::createSurfaceFromAvif(const s
return result;
}
std::expected<cairo_surface_t*, std::string> AVIF::createSurfaceFromAvif(const std::span<uint8_t>& buf) {
std::expected<cairo_surface_t*, std::string> AVIF::createSurfaceFromAvif(const std::span<const uint8_t> buf) {
heif_context* ctx = heif_context_alloc();
struct heif_error err = heif_context_read_from_memory(ctx, buf.data(), buf.size(), nullptr);

View file

@ -8,5 +8,5 @@
namespace AVIF {
std::expected<cairo_surface_t*, std::string> createSurfaceFromAvif(const std::string&);
std::expected<cairo_surface_t*, std::string> createSurfaceFromAvif(const std::span<uint8_t>&);
std::expected<cairo_surface_t*, std::string> createSurfaceFromAvif(const std::span<const uint8_t>);
};

View file

@ -38,8 +38,8 @@ std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(co
}
struct SReadState {
const std::span<uint8_t>& data;
size_t offset;
const std::span<const uint8_t> data;
size_t offset;
};
static void customReadFunction(png_structp png, png_bytep data, png_size_t length) {
@ -53,7 +53,7 @@ static void customReadFunction(png_structp png, png_bytep data, png_size_t lengt
state->offset += length;
}
std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const std::span<uint8_t>& data) {
std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const std::span<const uint8_t> data) {
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
png_infop info = png_create_info_struct(png);
if (!png || !info)

View file

@ -9,5 +9,5 @@
namespace PNG {
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const std::string&);
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const std::span<uint8_t>&);
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const std::span<const uint8_t>);
};

View file

@ -1,5 +1,5 @@
#include <hyprgraphics/resource/resources/ImageResource.hpp>
#include <hyprgraphics/image/Image.hpp>
#include <hyprgraphics/resource/resources/ImageResource.hpp>
#include <hyprutils/memory/Atomic.hpp>
#include <hyprutils/memory/Casts.hpp>

View file

@ -0,0 +1,20 @@
#include <hyprgraphics/resource/resources/StaticImageResource.hpp>
#include <hyprutils/memory/Atomic.hpp>
#include <hyprutils/memory/Casts.hpp>
#include <cairo/cairo.h>
#include <pango/pangocairo.h>
using namespace Hyprgraphics;
using namespace Hyprutils::Memory;
CStaticImageResource::CStaticImageResource(const std::span<const uint8_t> data, eImageFormat format) : m_data(data), m_format(format) {
;
}
void CStaticImageResource::render() {
auto image = CImage(m_data, m_format);
m_asset.cairoSurface = image.cairoSurface();
m_asset.pixelSize = m_asset.cairoSurface && m_asset.cairoSurface->cairo() ? m_asset.cairoSurface->size() : Hyprutils::Math::Vector2D{};
}