Something went wrong. Try again.
Zig bindings to Google's WUFFS image decoders for PNG, JPEG, GIF and Netpbm
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141// SPDX-FileCopyrightText: © 2024 Mitchell Hashimoto, Ghostty contributors// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");const Allocator = std.mem.Allocator;const c = @import("wuffs_c");const Error = @import("error.zig").Error;const check = @import("error.zig").check;
/// A decoded image. `data` is interleaved RGBA, premultiplied, four bytes/// per pixel, and is owned by the caller.pub const Image = struct { width: u32, height: u32, data: []const u8,};
/// Decode an image with one of WUFFS' decoders.////// `format` names the WUFFS module -- "png", "jpeg", "gif", "bmp",/// "netpbm" -- and is what that module's generated symbols are prefixed/// with, so the whole decode is the same code for every one of them. The/// module has to be enabled in build.zig's `defines` or its symbols will not/// exist.pub fn decode( comptime format: []const u8, comptime log: type, alloc: Allocator, data: []const u8,) Error!Image { const sizeof = @field(c, "sizeof__wuffs_" ++ format ++ "__decoder"); const Decoder = @field(c, "wuffs_" ++ format ++ "__decoder"); const initialize = @field(c, "wuffs_" ++ format ++ "__decoder__initialize"); const decode_image_config = @field(c, "wuffs_" ++ format ++ "__decoder__decode_image_config"); const decode_frame_config = @field(c, "wuffs_" ++ format ++ "__decoder__decode_frame_config"); const decode_frame = @field(c, "wuffs_" ++ format ++ "__decoder__decode_frame"); const workbuf_len = @field(c, "wuffs_" ++ format ++ "__decoder__workbuf_len");
// Work around some weirdness in WUFFS/Zig, there are some structs that // are defined as "extern" by the Zig compiler which means that Zig won't // allocate them on the stack at compile time. WUFFS has functions for // dynamically allocating these structs but they use the C malloc/free. This // gets around that by using the Zig allocator to allocate enough memory for // the struct and then casts it to the appropriate pointer.
const decoder_buf = try alloc.alloc(u8, sizeof()); defer alloc.free(decoder_buf);
const decoder: ?*Decoder = @ptrCast(decoder_buf); { const status = initialize(decoder, sizeof(), c.WUFFS_VERSION, 0); try check(log, &status); }
var source_buffer: c.wuffs_base__io_buffer = .{ .data = .{ .ptr = @ptrCast(@constCast(data.ptr)), .len = data.len }, .meta = .{ .wi = data.len, .ri = 0, .pos = 0, .closed = true, }, };
var image_config: c.wuffs_base__image_config = undefined; { const status = decode_image_config(decoder, &image_config, &source_buffer); try check(log, &status); }
const width = c.wuffs_base__pixel_config__width(&image_config.pixcfg); const height = c.wuffs_base__pixel_config__height(&image_config.pixcfg);
c.wuffs_base__pixel_config__set( &image_config.pixcfg, c.WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL, c.WUFFS_BASE__PIXEL_SUBSAMPLING__NONE, width, height, );
const destination = try alloc.alloc( u8, width * height * @sizeOf(c.wuffs_base__color_u32_argb_premul), ); errdefer alloc.free(destination);
// temporary buffer for intermediate processing of image const work_buffer = try alloc.alloc( u8,
// The type of this is a u64 on all systems but our allocator // uses a usize which is a u32 on 32-bit systems. std.math.cast( usize, workbuf_len(decoder).max_incl, ) orelse return error.OutOfMemory, ); defer alloc.free(work_buffer);
const work_slice = c.wuffs_base__make_slice_u8( work_buffer.ptr, work_buffer.len, );
var pixel_buffer: c.wuffs_base__pixel_buffer = undefined; { const status = c.wuffs_base__pixel_buffer__set_from_slice( &pixel_buffer, &image_config.pixcfg, c.wuffs_base__make_slice_u8(destination.ptr, destination.len), ); try check(log, &status); }
var frame_config: c.wuffs_base__frame_config = undefined; { const status = decode_frame_config(decoder, &frame_config, &source_buffer); try check(log, &status); }
{ const status = decode_frame( decoder, &pixel_buffer, &source_buffer, c.WUFFS_BASE__PIXEL_BLEND__SRC, work_slice, null, ); try check(log, &status); }
return .{ .width = width, .height = height, .data = destination, };}