// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Writes each test picture through the encoder, for `tools/oracle.py check` //! to read back with libsixel: `NAME.six`, and beside it `NAME.rgba`, the //! picture it was written from, with straight alpha. //! //! Run as part of `zig build oracle`, which passes the directory to write to. const std = @import("std"); const Io = std.Io; const sixel = @import("sixel"); const z2d = @import("z2d"); const pictures = @import("pictures"); pub fn main(init: std.process.Init) !void { const io = init.io; const gpa = init.gpa; const args = try init.minimal.args.toSlice(init.arena.allocator()); if (args.len != 2) return error.Usage; var dir = try Io.Dir.cwd().createDirPathOpen(io, args[1], .{}); defer dir.close(io); const list = try pictures.all(io, gpa); defer pictures.free(gpa, list); for (list) |pic| { if (!pic.oracle) continue; const name = try std.fmt.allocPrint(gpa, "{s}.six", .{pic.name}); defer gpa.free(name); const bytes = try sixel.encodeAlloc(gpa, &pic.surface, pic.options); defer gpa.free(bytes); try dir.writeFile(io, .{ .sub_path = name, .data = bytes }); const w: u32 = @intCast(pic.surface.getWidth()); const h: u32 = @intCast(pic.surface.getHeight()); var raw: std.ArrayList(u8) = .empty; defer raw.deinit(gpa); try raw.appendSlice(gpa, std.mem.asBytes(&std.mem.nativeToLittle(u32, w))); try raw.appendSlice(gpa, std.mem.asBytes(&std.mem.nativeToLittle(u32, h))); for (0..h) |y| { for (0..w) |x| { const px = z2d.pixel.RGBA.fromPixel(pic.surface.getPixel(@intCast(x), @intCast(y)).?).demultiply(); try raw.appendSlice(gpa, &.{ px.r, px.g, px.b, px.a }); } } const raw_name = try std.fmt.allocPrint(gpa, "{s}.rgba", .{pic.name}); defer gpa.free(raw_name); try dir.writeFile(io, .{ .sub_path = raw_name, .data = raw.items }); } }