From de612934a08cf4d3a7f9b6ab995fae2332c1a6e4 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 2 Sep 2024 20:41:59 -0700 Subject: [PATCH] some tweaks for wuffs --- pkg/wuffs/src/png.zig | 31 +++++++++++++++------------ pkg/wuffs/src/swizzle.zig | 26 +++++++++++----------- src/terminal/kitty/graphics_image.zig | 7 ++++-- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/pkg/wuffs/src/png.zig b/pkg/wuffs/src/png.zig index 65258224e..ad47074bd 100644 --- a/pkg/wuffs/src/png.zig +++ b/pkg/wuffs/src/png.zig @@ -1,11 +1,12 @@ const std = @import("std"); - +const Allocator = std.mem.Allocator; const c = @import("c.zig").c; const Error = @import("error.zig").Error; 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, height: u32, 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)) { const e = c.wuffs_base__status__message(&status); - log.warn("{s}", .{e}); + log.warn("decode err={s}", .{e}); return error.WuffsError; } } - var source_buffer: c.wuffs_base__io_buffer = undefined; - source_buffer.data.ptr = @constCast(@ptrCast(data.ptr)); - source_buffer.data.len = data.len; - source_buffer.meta.wi = data.len; - source_buffer.meta.ri = 0; - source_buffer.meta.pos = 0; - source_buffer.meta.closed = true; + var source_buffer: c.wuffs_base__io_buffer = .{ + .data = .{ .ptr = @constCast(@ptrCast(data.ptr)), .len = data.len }, + .meta = .{ + .wi = data.len, + .ri = 0, + .pos = 0, + .closed = true, + }, + }; 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)) { const e = c.wuffs_base__status__message(&status); - log.warn("{s}", .{e}); + log.warn("decode err={s}", .{e}); 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)) { const e = c.wuffs_base__status__message(&status); - log.warn("{s}", .{e}); + log.warn("decode err={s}", .{e}); 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)) { const e = c.wuffs_base__status__message(&status); - log.warn("{s}", .{e}); + log.warn("decode err={s}", .{e}); 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)) { const e = c.wuffs_base__status__message(&status); - log.warn("{s}", .{e}); + log.warn("decode err={s}", .{e}); return error.WuffsError; } } diff --git a/pkg/wuffs/src/swizzle.zig b/pkg/wuffs/src/swizzle.zig index 8a691c19d..8a0e4e82b 100644 --- a/pkg/wuffs/src/swizzle.zig +++ b/pkg/wuffs/src/swizzle.zig @@ -1,11 +1,12 @@ const std = @import("std"); - +const Allocator = std.mem.Allocator; +const assert = std.debug.assert; const c = @import("c.zig").c; const Error = @import("error.zig").Error; 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( alloc, 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( alloc, 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( alloc, src, @@ -33,7 +34,7 @@ pub fn rgbToRgba(alloc: std.mem.Allocator, src: []const u8) Error![]u8 { } fn swizzle( - alloc: std.mem.Allocator, + alloc: Allocator, src: []const u8, comptime src_pixel_format: u32, comptime dst_pixel_format: u32, @@ -47,9 +48,9 @@ fn swizzle( dst_pixel_format, ); - std.debug.assert(c.wuffs_base__pixel_format__is_direct(&dst_fmt)); - std.debug.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__is_direct(&dst_fmt)); + assert(c.wuffs_base__pixel_format__is_interleaved(&dst_fmt)); + 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; @@ -57,13 +58,13 @@ fn swizzle( src_pixel_format, ); - std.debug.assert(c.wuffs_base__pixel_format__is_direct(&src_fmt)); - std.debug.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__is_direct(&src_fmt)); + assert(c.wuffs_base__pixel_format__is_interleaved(&src_fmt)); + 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; - 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); errdefer alloc.free(dst); @@ -74,7 +75,6 @@ fn swizzle( ); var swizzler: c.wuffs_base__pixel_swizzler = undefined; - { const status = c.wuffs_base__pixel_swizzler__prepare( &swizzler, diff --git a/src/terminal/kitty/graphics_image.zig b/src/terminal/kitty/graphics_image.zig index d1a20f160..5adac9128 100644 --- a/src/terminal/kitty/graphics_image.zig +++ b/src/terminal/kitty/graphics_image.zig @@ -412,9 +412,12 @@ pub const LoadingImage = struct { fn decodePng(self: *LoadingImage, alloc: Allocator) !void { 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, - else => |e| return e, + error.OutOfMemory => return error.OutOfMemory, }; defer alloc.free(result.data);