mirror of
https://github.com/hyprwm/hyprgraphics.git
synced 2025-12-25 01:50:03 +01:00
fix stuff
This commit is contained in:
parent
bba4596882
commit
6ba2c4cfb1
4 changed files with 19 additions and 23 deletions
|
|
@ -6,15 +6,14 @@
|
|||
#include <hyprutils/memory/SharedPtr.hpp>
|
||||
|
||||
namespace Hyprgraphics {
|
||||
enum ImageFormat {
|
||||
PNG
|
||||
enum EImageFormat {
|
||||
IMAGE_FORMAT_PNG
|
||||
};
|
||||
|
||||
class CImage {
|
||||
public:
|
||||
// create an image from a provided path.
|
||||
CImage(const std::string& path);
|
||||
CImage(const unsigned char*, size_t, ImageFormat);
|
||||
CImage(const std::span<uint8_t>&, EImageFormat);
|
||||
~CImage();
|
||||
|
||||
CImage(const CImage&) = delete;
|
||||
|
|
|
|||
|
|
@ -12,10 +12,10 @@
|
|||
using namespace Hyprgraphics;
|
||||
using namespace Hyprutils::Memory;
|
||||
|
||||
Hyprgraphics::CImage::CImage(const unsigned char* data, size_t size, ImageFormat format) {
|
||||
Hyprgraphics::CImage::CImage(const std::span<uint8_t>& data, EImageFormat format) {
|
||||
std::expected<cairo_surface_t*, std::string> CAIROSURFACE;
|
||||
if (format == ImageFormat::PNG) {
|
||||
CAIROSURFACE = PNG::createSurfaceFromPNG(data, size);
|
||||
if (format == EImageFormat::IMAGE_FORMAT_PNG) {
|
||||
CAIROSURFACE = PNG::createSurfaceFromPNG(data);
|
||||
mime = "image/png";
|
||||
} else {
|
||||
lastError = "Currently only PNG images are supported for embedding";
|
||||
|
|
|
|||
|
|
@ -11,7 +11,9 @@
|
|||
#include <hyprutils/utils/ScopeGuard.hpp>
|
||||
using namespace Hyprutils::Utils;
|
||||
|
||||
std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const std::string& path) {
|
||||
static std::expected<cairo_surface_t*, std::string> loadPNG(png_structp, png_infop);
|
||||
|
||||
std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const std::string& path) {
|
||||
if (!std::filesystem::exists(path))
|
||||
return std::unexpected("loading png: file doesn't exist");
|
||||
|
||||
|
|
@ -37,24 +39,23 @@ std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const std
|
|||
return loadPNG(png, info);
|
||||
}
|
||||
|
||||
struct MemoryReadState {
|
||||
const unsigned char* data;
|
||||
size_t size;
|
||||
size_t offset;
|
||||
struct ReadState {
|
||||
const std::span<uint8_t>& data;
|
||||
size_t offset;
|
||||
};
|
||||
|
||||
void custom_read_function(png_structp png, png_bytep data, png_size_t length) {
|
||||
MemoryReadState* state = static_cast<MemoryReadState*>(png_get_io_ptr(png));
|
||||
if (state->offset + length > state->size) {
|
||||
ReadState* state = static_cast<ReadState*>(png_get_io_ptr(png));
|
||||
if (state->offset + length > state->data.size()) {
|
||||
png_error(png, "read error");
|
||||
return;
|
||||
}
|
||||
|
||||
memcpy(data, state->data + state->offset, length);
|
||||
memcpy(data, state->data.data() + state->offset, length);
|
||||
state->offset += length;
|
||||
}
|
||||
|
||||
std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const unsigned char* data, size_t size) {
|
||||
std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const std::span<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)
|
||||
|
|
@ -65,10 +66,7 @@ std::expected<cairo_surface_t*, std::string> PNG::createSurfaceFromPNG(const uns
|
|||
if (setjmp(png_jmpbuf(png)))
|
||||
return std::unexpected("loading png: couldn't setjmp");
|
||||
|
||||
MemoryReadState readState;
|
||||
readState.data = reinterpret_cast<const unsigned char*>(data);
|
||||
readState.size = size;
|
||||
readState.offset = 0;
|
||||
ReadState readState = {data, 0};
|
||||
|
||||
png_set_read_fn(png, &readState, custom_read_function);
|
||||
png_set_sig_bytes(png, 0);
|
||||
|
|
|
|||
|
|
@ -4,10 +4,9 @@
|
|||
#include <string>
|
||||
#include <expected>
|
||||
#include <png.h>
|
||||
#include <span>
|
||||
|
||||
namespace PNG {
|
||||
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const std::string&);
|
||||
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const unsigned char*, size_t);
|
||||
std::expected<cairo_surface_t*, std::string> createSurfaceFromPNG(const std::span<u_int8_t>&);
|
||||
};
|
||||
|
||||
static std::expected<cairo_surface_t*, std::string> loadPNG(png_structp, png_infop);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue