mirror of
https://github.com/hyprwm/hyprgraphics.git
synced 2025-12-27 22:40:06 +01:00
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
This commit is contained in:
parent
9431db625c
commit
ee86d75cd7
8 changed files with 21 additions and 12 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "AsyncResource.hpp"
|
||||
#include "../../color/Color.hpp"
|
||||
#include "hyprgraphics/image/Image.hpp"
|
||||
|
||||
#include <optional>
|
||||
|
||||
|
|
@ -17,13 +18,17 @@ namespace Hyprgraphics {
|
|||
};
|
||||
|
||||
CImageResource(const std::string& path);
|
||||
CImageResource(const std::span<const uint8_t> data, eImageFormat format);
|
||||
CImageResource(const std::string& svg, const Hyprutils::Math::Vector2D& size);
|
||||
virtual ~CImageResource() = default;
|
||||
|
||||
virtual void render();
|
||||
|
||||
private:
|
||||
std::string m_path;
|
||||
Hyprutils::Math::Vector2D m_svgSize;
|
||||
std::string m_path;
|
||||
Hyprutils::Math::Vector2D m_svgSize;
|
||||
|
||||
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::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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
|
|
@ -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>);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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>);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -13,13 +13,17 @@ CImageResource::CImageResource(const std::string& path) : m_path(path) {
|
|||
;
|
||||
}
|
||||
|
||||
CImageResource::CImageResource(const std::span<const uint8_t> data, eImageFormat format) : m_data(data), m_format(format) {
|
||||
;
|
||||
}
|
||||
|
||||
CImageResource::CImageResource(const std::string& svg, const Hyprutils::Math::Vector2D& size) : m_path(svg), m_svgSize(size) {
|
||||
;
|
||||
}
|
||||
|
||||
void CImageResource::render() {
|
||||
auto image = CImage(m_path, m_svgSize);
|
||||
auto image = m_data.size() > 0 ? CImage(m_data, m_format) : CImage(m_path, m_svgSize);
|
||||
|
||||
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