mirror of
https://github.com/ghostty-org/ghostty.git
synced 2025-07-15 00:06:09 +03:00
some tweaks for wuffs
This commit is contained in:
@ -1,11 +1,12 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
const c = @import("c.zig").c;
|
const c = @import("c.zig").c;
|
||||||
const Error = @import("error.zig").Error;
|
const Error = @import("error.zig").Error;
|
||||||
|
|
||||||
const log = std.log.scoped(.wuffs_png);
|
const log = std.log.scoped(.wuffs_png);
|
||||||
|
|
||||||
pub fn decode(alloc: std.mem.Allocator, data: []const u8) Error!struct {
|
/// Decode a PNG image.
|
||||||
|
pub fn decode(alloc: Allocator, data: []const u8) Error!struct {
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
data: []const u8,
|
data: []const u8,
|
||||||
@ -30,18 +31,20 @@ pub fn decode(alloc: std.mem.Allocator, data: []const u8) Error!struct {
|
|||||||
);
|
);
|
||||||
if (!c.wuffs_base__status__is_ok(&status)) {
|
if (!c.wuffs_base__status__is_ok(&status)) {
|
||||||
const e = c.wuffs_base__status__message(&status);
|
const e = c.wuffs_base__status__message(&status);
|
||||||
log.warn("{s}", .{e});
|
log.warn("decode err={s}", .{e});
|
||||||
return error.WuffsError;
|
return error.WuffsError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var source_buffer: c.wuffs_base__io_buffer = undefined;
|
var source_buffer: c.wuffs_base__io_buffer = .{
|
||||||
source_buffer.data.ptr = @constCast(@ptrCast(data.ptr));
|
.data = .{ .ptr = @constCast(@ptrCast(data.ptr)), .len = data.len },
|
||||||
source_buffer.data.len = data.len;
|
.meta = .{
|
||||||
source_buffer.meta.wi = data.len;
|
.wi = data.len,
|
||||||
source_buffer.meta.ri = 0;
|
.ri = 0,
|
||||||
source_buffer.meta.pos = 0;
|
.pos = 0,
|
||||||
source_buffer.meta.closed = true;
|
.closed = true,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
var image_config: c.wuffs_base__image_config = undefined;
|
var image_config: c.wuffs_base__image_config = undefined;
|
||||||
{
|
{
|
||||||
@ -52,7 +55,7 @@ pub fn decode(alloc: std.mem.Allocator, data: []const u8) Error!struct {
|
|||||||
);
|
);
|
||||||
if (!c.wuffs_base__status__is_ok(&status)) {
|
if (!c.wuffs_base__status__is_ok(&status)) {
|
||||||
const e = c.wuffs_base__status__message(&status);
|
const e = c.wuffs_base__status__message(&status);
|
||||||
log.warn("{s}", .{e});
|
log.warn("decode err={s}", .{e});
|
||||||
return error.WuffsError;
|
return error.WuffsError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -95,7 +98,7 @@ pub fn decode(alloc: std.mem.Allocator, data: []const u8) Error!struct {
|
|||||||
);
|
);
|
||||||
if (!c.wuffs_base__status__is_ok(&status)) {
|
if (!c.wuffs_base__status__is_ok(&status)) {
|
||||||
const e = c.wuffs_base__status__message(&status);
|
const e = c.wuffs_base__status__message(&status);
|
||||||
log.warn("{s}", .{e});
|
log.warn("decode err={s}", .{e});
|
||||||
return error.WuffsError;
|
return error.WuffsError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +112,7 @@ pub fn decode(alloc: std.mem.Allocator, data: []const u8) Error!struct {
|
|||||||
);
|
);
|
||||||
if (!c.wuffs_base__status__is_ok(&status)) {
|
if (!c.wuffs_base__status__is_ok(&status)) {
|
||||||
const e = c.wuffs_base__status__message(&status);
|
const e = c.wuffs_base__status__message(&status);
|
||||||
log.warn("{s}", .{e});
|
log.warn("decode err={s}", .{e});
|
||||||
return error.WuffsError;
|
return error.WuffsError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,7 +128,7 @@ pub fn decode(alloc: std.mem.Allocator, data: []const u8) Error!struct {
|
|||||||
);
|
);
|
||||||
if (!c.wuffs_base__status__is_ok(&status)) {
|
if (!c.wuffs_base__status__is_ok(&status)) {
|
||||||
const e = c.wuffs_base__status__message(&status);
|
const e = c.wuffs_base__status__message(&status);
|
||||||
log.warn("{s}", .{e});
|
log.warn("decode err={s}", .{e});
|
||||||
return error.WuffsError;
|
return error.WuffsError;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const Allocator = std.mem.Allocator;
|
||||||
|
const assert = std.debug.assert;
|
||||||
const c = @import("c.zig").c;
|
const c = @import("c.zig").c;
|
||||||
const Error = @import("error.zig").Error;
|
const Error = @import("error.zig").Error;
|
||||||
|
|
||||||
const log = std.log.scoped(.wuffs_swizzler);
|
const log = std.log.scoped(.wuffs_swizzler);
|
||||||
|
|
||||||
pub fn gToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 {
|
pub fn gToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
||||||
return swizzle(
|
return swizzle(
|
||||||
alloc,
|
alloc,
|
||||||
src,
|
src,
|
||||||
@ -14,7 +15,7 @@ pub fn gToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn gaToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 {
|
pub fn gaToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
||||||
return swizzle(
|
return swizzle(
|
||||||
alloc,
|
alloc,
|
||||||
src,
|
src,
|
||||||
@ -23,7 +24,7 @@ pub fn gaToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rgbToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 {
|
pub fn rgbToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
||||||
return swizzle(
|
return swizzle(
|
||||||
alloc,
|
alloc,
|
||||||
src,
|
src,
|
||||||
@ -33,7 +34,7 @@ pub fn rgbToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn swizzle(
|
fn swizzle(
|
||||||
alloc: std.mem.Allocator,
|
alloc: Allocator,
|
||||||
src: []const u8,
|
src: []const u8,
|
||||||
comptime src_pixel_format: u32,
|
comptime src_pixel_format: u32,
|
||||||
comptime dst_pixel_format: u32,
|
comptime dst_pixel_format: u32,
|
||||||
@ -47,9 +48,9 @@ fn swizzle(
|
|||||||
dst_pixel_format,
|
dst_pixel_format,
|
||||||
);
|
);
|
||||||
|
|
||||||
std.debug.assert(c.wuffs_base__pixel_format__is_direct(&dst_fmt));
|
assert(c.wuffs_base__pixel_format__is_direct(&dst_fmt));
|
||||||
std.debug.assert(c.wuffs_base__pixel_format__is_interleaved(&dst_fmt));
|
assert(c.wuffs_base__pixel_format__is_interleaved(&dst_fmt));
|
||||||
std.debug.assert(c.wuffs_base__pixel_format__bits_per_pixel(&dst_fmt) % 8 == 0);
|
assert(c.wuffs_base__pixel_format__bits_per_pixel(&dst_fmt) % 8 == 0);
|
||||||
|
|
||||||
const dst_size = c.wuffs_base__pixel_format__bits_per_pixel(&dst_fmt) / 8;
|
const dst_size = c.wuffs_base__pixel_format__bits_per_pixel(&dst_fmt) / 8;
|
||||||
|
|
||||||
@ -57,13 +58,13 @@ fn swizzle(
|
|||||||
src_pixel_format,
|
src_pixel_format,
|
||||||
);
|
);
|
||||||
|
|
||||||
std.debug.assert(c.wuffs_base__pixel_format__is_direct(&src_fmt));
|
assert(c.wuffs_base__pixel_format__is_direct(&src_fmt));
|
||||||
std.debug.assert(c.wuffs_base__pixel_format__is_interleaved(&src_fmt));
|
assert(c.wuffs_base__pixel_format__is_interleaved(&src_fmt));
|
||||||
std.debug.assert(c.wuffs_base__pixel_format__bits_per_pixel(&src_fmt) % 8 == 0);
|
assert(c.wuffs_base__pixel_format__bits_per_pixel(&src_fmt) % 8 == 0);
|
||||||
|
|
||||||
const src_size = c.wuffs_base__pixel_format__bits_per_pixel(&src_fmt) / 8;
|
const src_size = c.wuffs_base__pixel_format__bits_per_pixel(&src_fmt) / 8;
|
||||||
|
|
||||||
std.debug.assert(src.len % src_size == 0);
|
assert(src.len % src_size == 0);
|
||||||
|
|
||||||
const dst = try alloc.alloc(u8, src.len * dst_size / src_size);
|
const dst = try alloc.alloc(u8, src.len * dst_size / src_size);
|
||||||
errdefer alloc.free(dst);
|
errdefer alloc.free(dst);
|
||||||
@ -74,7 +75,6 @@ fn swizzle(
|
|||||||
);
|
);
|
||||||
|
|
||||||
var swizzler: c.wuffs_base__pixel_swizzler = undefined;
|
var swizzler: c.wuffs_base__pixel_swizzler = undefined;
|
||||||
|
|
||||||
{
|
{
|
||||||
const status = c.wuffs_base__pixel_swizzler__prepare(
|
const status = c.wuffs_base__pixel_swizzler__prepare(
|
||||||
&swizzler,
|
&swizzler,
|
||||||
|
@ -412,9 +412,12 @@ pub const LoadingImage = struct {
|
|||||||
fn decodePng(self: *LoadingImage, alloc: Allocator) !void {
|
fn decodePng(self: *LoadingImage, alloc: Allocator) !void {
|
||||||
assert(self.image.format == .png);
|
assert(self.image.format == .png);
|
||||||
|
|
||||||
const result = wuffs.png.decode(alloc, self.data.items) catch |err| switch (err) {
|
const result = wuffs.png.decode(
|
||||||
|
alloc,
|
||||||
|
self.data.items,
|
||||||
|
) catch |err| switch (err) {
|
||||||
error.WuffsError => return error.InvalidData,
|
error.WuffsError => return error.InvalidData,
|
||||||
else => |e| return e,
|
error.OutOfMemory => return error.OutOfMemory,
|
||||||
};
|
};
|
||||||
defer alloc.free(result.data);
|
defer alloc.free(result.data);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user