mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-07-31 12:49:03 +00:00
This commit represents the majority of the work necessary to upgrade Ghostty to use Zig 0.16.0. Key parts: * In addition to its previous responsibilities, the global state now houses state for global I/O implementations and the process environment. It is now also utilized in the main application along with the C library. Where necessary, global state is isolated from key parts of the implementation (e.g., in libghostty subsystems), and it's expected that this list will grow. * We currently manage our own C translation layer where necessary. In these cases, cImport has been removed in favor of the new external translate-c package. Due to fixes that have needed be made to properly translate the dependencies that were swapped out, as mentioned, we have had to backport fixes from the current translate-c package (and the upstream Arocc dependency). We will host this ourselves until Zig 0.17.0 is released with these fixes. * Where necessary (only a small number of cases), some stdlib code from 0.15.2 (and even from 0.17.0) has been taken, adopted, and vendored in lib/compat. Co-authored-by: Leah Amelia Chen <hi@pluie.me>
122 lines
3.2 KiB
Zig
122 lines
3.2 KiB
Zig
const std = @import("std");
|
|
const Allocator = std.mem.Allocator;
|
|
const assert = std.debug.assert;
|
|
const c = @import("wuffs_c");
|
|
const Error = @import("error.zig").Error;
|
|
|
|
const log = std.log.scoped(.wuffs_swizzler);
|
|
|
|
pub fn gToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
|
return swizzle(
|
|
alloc,
|
|
src,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__Y,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL,
|
|
);
|
|
}
|
|
|
|
pub fn gaToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
|
return swizzle(
|
|
alloc,
|
|
src,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__YA_PREMUL,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL,
|
|
);
|
|
}
|
|
|
|
pub fn rgbToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
|
return swizzle(
|
|
alloc,
|
|
src,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__RGB,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL,
|
|
);
|
|
}
|
|
|
|
pub fn bgrToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
|
return swizzle(
|
|
alloc,
|
|
src,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__BGR,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL,
|
|
);
|
|
}
|
|
|
|
pub fn bgraToRgba(alloc: Allocator, src: []const u8) Error![]u8 {
|
|
return swizzle(
|
|
alloc,
|
|
src,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__BGRA_PREMUL,
|
|
c.WUFFS_BASE__PIXEL_FORMAT__RGBA_PREMUL,
|
|
);
|
|
}
|
|
|
|
fn swizzle(
|
|
alloc: Allocator,
|
|
src: []const u8,
|
|
comptime src_pixel_format: u32,
|
|
comptime dst_pixel_format: u32,
|
|
) Error![]u8 {
|
|
const src_slice = c.wuffs_base__make_slice_u8(
|
|
@constCast(src.ptr),
|
|
src.len,
|
|
);
|
|
|
|
const dst_fmt = c.wuffs_base__make_pixel_format(
|
|
dst_pixel_format,
|
|
);
|
|
|
|
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;
|
|
|
|
const src_fmt = c.wuffs_base__make_pixel_format(
|
|
src_pixel_format,
|
|
);
|
|
|
|
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;
|
|
|
|
assert(src.len % src_size == 0);
|
|
|
|
const dst = try alloc.alloc(u8, src.len * dst_size / src_size);
|
|
errdefer alloc.free(dst);
|
|
|
|
const dst_slice = c.wuffs_base__make_slice_u8(
|
|
dst.ptr,
|
|
dst.len,
|
|
);
|
|
|
|
var swizzler: c.wuffs_base__pixel_swizzler = undefined;
|
|
{
|
|
const status = c.wuffs_base__pixel_swizzler__prepare(
|
|
&swizzler,
|
|
dst_fmt,
|
|
c.wuffs_base__empty_slice_u8(),
|
|
src_fmt,
|
|
c.wuffs_base__empty_slice_u8(),
|
|
c.WUFFS_BASE__PIXEL_BLEND__SRC,
|
|
);
|
|
if (!c.wuffs_base__status__is_ok(&status)) {
|
|
const e = c.wuffs_base__status__message(&status);
|
|
log.warn("{s}", .{e});
|
|
return error.WuffsError;
|
|
}
|
|
}
|
|
{
|
|
_ = c.wuffs_base__pixel_swizzler__swizzle_interleaved_from_slice(
|
|
&swizzler,
|
|
dst_slice,
|
|
c.wuffs_base__empty_slice_u8(),
|
|
src_slice,
|
|
);
|
|
}
|
|
|
|
return dst;
|
|
}
|