mirror of
https://github.com/hyprwm/hyprgraphics.git
synced 2025-12-20 14:30:03 +01:00
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
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:
parent
9431db625c
commit
50fb9f0692
10 changed files with 62 additions and 13 deletions
|
|
@ -15,7 +15,7 @@ namespace Hyprgraphics {
|
||||||
class CImage {
|
class CImage {
|
||||||
public:
|
public:
|
||||||
CImage(const std::string& path, const Hyprutils::Math::Vector2D& size = {} /* for SVG */);
|
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();
|
||||||
|
|
||||||
CImage(const CImage&) = delete;
|
CImage(const CImage&) = delete;
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "AsyncResource.hpp"
|
#include <string>
|
||||||
|
#include <hyprutils/math/Vector2D.hpp>
|
||||||
|
#include "./AsyncResource.hpp"
|
||||||
#include "../../color/Color.hpp"
|
#include "../../color/Color.hpp"
|
||||||
|
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
#include <hyprutils/math/Vector2D.hpp>
|
|
||||||
|
|
||||||
namespace Hyprgraphics {
|
namespace Hyprgraphics {
|
||||||
class CImageResource : public IAsyncResource {
|
class CImageResource : public IAsyncResource {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
@ -17,7 +17,7 @@ using namespace Hyprgraphics;
|
||||||
using namespace Hyprutils::Memory;
|
using namespace Hyprutils::Memory;
|
||||||
using namespace Hyprutils::Math;
|
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;
|
std::expected<cairo_surface_t*, std::string> CAIROSURFACE;
|
||||||
if (format == eImageFormat::IMAGE_FORMAT_PNG) {
|
if (format == eImageFormat::IMAGE_FORMAT_PNG) {
|
||||||
CAIROSURFACE = PNG::createSurfaceFromPNG(data);
|
CAIROSURFACE = PNG::createSurfaceFromPNG(data);
|
||||||
|
|
|
||||||
|
|
@ -78,7 +78,7 @@ std::expected<cairo_surface_t*, std::string> AVIF::createSurfaceFromAvif(const s
|
||||||
|
|
||||||
return result;
|
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();
|
heif_context* ctx = heif_context_alloc();
|
||||||
struct heif_error err = heif_context_read_from_memory(ctx, buf.data(), buf.size(), nullptr);
|
struct heif_error err = heif_context_read_from_memory(ctx, buf.data(), buf.size(), nullptr);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,5 +8,5 @@
|
||||||
|
|
||||||
namespace AVIF {
|
namespace AVIF {
|
||||||
std::expected<cairo_surface_t*, std::string> createSurfaceFromAvif(const std::string&);
|
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>);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(co
|
||||||
}
|
}
|
||||||
|
|
||||||
struct SReadState {
|
struct SReadState {
|
||||||
const std::span<uint8_t>& data;
|
const std::span<const uint8_t> data;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ static void customReadFunction(png_structp png, png_bytep data, png_size_t lengt
|
||||||
state->offset += length;
|
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_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
||||||
png_infop info = png_create_info_struct(png);
|
png_infop info = png_create_info_struct(png);
|
||||||
if (!png || !info)
|
if (!png || !info)
|
||||||
|
|
|
||||||
|
|
@ -9,5 +9,5 @@
|
||||||
|
|
||||||
namespace PNG {
|
namespace PNG {
|
||||||
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const std::string&);
|
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>);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#include <hyprgraphics/resource/resources/ImageResource.hpp>
|
|
||||||
#include <hyprgraphics/image/Image.hpp>
|
#include <hyprgraphics/image/Image.hpp>
|
||||||
|
#include <hyprgraphics/resource/resources/ImageResource.hpp>
|
||||||
#include <hyprutils/memory/Atomic.hpp>
|
#include <hyprutils/memory/Atomic.hpp>
|
||||||
#include <hyprutils/memory/Casts.hpp>
|
#include <hyprutils/memory/Casts.hpp>
|
||||||
|
|
||||||
|
|
|
||||||
20
src/resource/resources/StaticImageResource.cpp
Normal file
20
src/resource/resources/StaticImageResource.cpp
Normal 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{};
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue