a73x

9fae3529

imgdiff: shrink CLI to thin wrapper over src/imgdiff.zig

a73x   2026-04-19 08:47

Commit message
imgdiff: shrink CLI to thin wrapper over src/imgdiff.zig

Removes duplicated pure-math (compare, makeDiffImage, DiffResult,
inline tests) from the CLI. CLI now imports the library module
for both the comparison and the default threshold constants.

No behavior change. test-render still passes against existing
golden PNGs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>

src/tools/imgdiff.zig
Old New
@@ -1,58 +1,6 @@
1 const std = @import("std"); 1 const std = @import("std");
2 const png = @import("png"); 2 const png = @import("png");
3 3 const imgdiff = @import("imgdiff");
4 pub const DiffResult = struct {
5 rmse: f64, // [0, 1]
6 max_pixel: f64, // [0, 1]
7 pixel_count: usize,
8 };
9
10 pub fn compare(a: png.Image, b: png.Image) !DiffResult {
11 if (a.width != b.width or a.height != b.height) return error.DimensionsDiffer;
12 std.debug.assert(a.pixels.len == b.pixels.len);
13
14 const px_count = @as(usize, a.width) * a.height;
15 var sum_sq: f64 = 0;
16 var max_d: f64 = 0;
17
18 var i: usize = 0;
19 while (i < px_count) : (i += 1) {
20 const off = i * 4;
21 const dr = (@as(f64, @floatFromInt(a.pixels[off + 0])) - @as(f64, @floatFromInt(b.pixels[off + 0]))) / 255.0;
22 const dg = (@as(f64, @floatFromInt(a.pixels[off + 1])) - @as(f64, @floatFromInt(b.pixels[off + 1]))) / 255.0;
23 const db = (@as(f64, @floatFromInt(a.pixels[off + 2])) - @as(f64, @floatFromInt(b.pixels[off + 2]))) / 255.0;
24 const d_sq = (dr * dr + dg * dg + db * db) / 3.0;
25 sum_sq += d_sq;
26 const d = @sqrt(d_sq);
27 if (d > max_d) max_d = d;
28 }
29
30 return .{
31 .rmse = @sqrt(sum_sq / @as(f64, @floatFromInt(px_count))),
32 .max_pixel = max_d,
33 .pixel_count = px_count,
34 };
35 }
36
37 test "identical images produce zero RMSE" {
38 var pixels_a = [_]u8{ 10, 20, 30, 255, 40, 50, 60, 255 };
39 var pixels_b = [_]u8{ 10, 20, 30, 255, 40, 50, 60, 255 };
40 const a = png.Image{ .width = 2, .height = 1, .pixels = &pixels_a };
41 const b = png.Image{ .width = 2, .height = 1, .pixels = &pixels_b };
42 const r = try compare(a, b);
43 try std.testing.expectEqual(@as(f64, 0.0), r.rmse);
44 try std.testing.expectEqual(@as(f64, 0.0), r.max_pixel);
45 }
46
47 test "fully saturated difference produces rmse=1.0 and max=1.0" {
48 var pixels_a = [_]u8{ 0, 0, 0, 255 };
49 var pixels_b = [_]u8{ 255, 255, 255, 255 };
50 const a = png.Image{ .width = 1, .height = 1, .pixels = &pixels_a };
51 const b = png.Image{ .width = 1, .height = 1, .pixels = &pixels_b };
52 const r = try compare(a, b);
53 try std.testing.expectApproxEqAbs(@as(f64, 1.0), r.rmse, 1e-9);
54 try std.testing.expectApproxEqAbs(@as(f64, 1.0), r.max_pixel, 1e-9);
55 }
56 4
57 pub fn main() !void { 5 pub fn main() !void {
58 var gpa: std.heap.DebugAllocator(.{}) = .init; 6 var gpa: std.heap.DebugAllocator(.{}) = .init;
@@ -70,8 +18,8 @@ pub fn main() !void {
70 const reference_path = args[2]; 18 const reference_path = args[2];
71 const diff_path: ?[]const u8 = if (args.len >= 4) args[3] else null; 19 const diff_path: ?[]const u8 = if (args.len >= 4) args[3] else null;
72 20
73 const rmse_max = readFloatEnv("WAYSTTY_TEST_RMSE_MAX", 0.005); 21 const rmse_max = readFloatEnv("WAYSTTY_TEST_RMSE_MAX", imgdiff.RMSE_DEFAULT);
74 const pixel_max = readFloatEnv("WAYSTTY_TEST_PIXEL_MAX", 0.125); 22 const pixel_max = readFloatEnv("WAYSTTY_TEST_PIXEL_MAX", imgdiff.PIXEL_MAX_DEFAULT);
75 23
76 const actual_bytes = try std.fs.cwd().readFileAlloc(alloc, actual_path, 64 * 1024 * 1024); 24 const actual_bytes = try std.fs.cwd().readFileAlloc(alloc, actual_path, 64 * 1024 * 1024);
77 defer alloc.free(actual_bytes); 25 defer alloc.free(actual_bytes);
@@ -88,7 +36,7 @@ pub fn main() !void {
88 std.process.exit(3); 36 std.process.exit(3);
89 } 37 }
90 38
91 const r = try compare(actual, reference); 39 const r = try imgdiff.compare(actual, reference);
92 const pass = r.rmse <= rmse_max and r.max_pixel <= pixel_max; 40 const pass = r.rmse <= rmse_max and r.max_pixel <= pixel_max;
93 41
94 if (pass) { 42 if (pass) {
@@ -99,7 +47,7 @@ pub fn main() !void {
99 std.debug.print("FAIL: {s}\n RMSE: {d:.4}% (max {d:.4}%)\n worst pixel: {d:.4}% (max {d:.4}%)\n", .{ reference_path, r.rmse * 100.0, rmse_max * 100.0, r.max_pixel * 100.0, pixel_max * 100.0 }); 47 std.debug.print("FAIL: {s}\n RMSE: {d:.4}% (max {d:.4}%)\n worst pixel: {d:.4}% (max {d:.4}%)\n", .{ reference_path, r.rmse * 100.0, rmse_max * 100.0, r.max_pixel * 100.0, pixel_max * 100.0 });
100 48
101 if (diff_path) |p| { 49 if (diff_path) |p| {
102 const diff_img = try makeDiffImage(alloc, actual, reference); 50 const diff_img = try imgdiff.makeDiffImage(alloc, actual, reference);
103 defer alloc.free(diff_img.pixels); 51 defer alloc.free(diff_img.pixels);
104 52
105 var buf: std.ArrayList(u8) = .empty; 53 var buf: std.ArrayList(u8) = .empty;
@@ -120,32 +68,3 @@ fn readFloatEnv(name: []const u8, default: f64) f64 {
120 const val = std.posix.getenv(name) orelse return default; 68 const val = std.posix.getenv(name) orelse return default;
121 return std.fmt.parseFloat(f64, val) catch default; 69 return std.fmt.parseFloat(f64, val) catch default;
122 } 70 }
123
124 fn makeDiffImage(alloc: std.mem.Allocator, a: png.Image, b: png.Image) !png.Image {
125 // Side-by-side: [actual | reference | delta-heatmap]
126 const w = a.width * 3;
127 const h = a.height;
128 const pixels = try alloc.alloc(u8, w * h * 4);
129 var y: u32 = 0;
130 while (y < h) : (y += 1) {
131 const row_off = @as(usize, y) * w * 4;
132 const a_off = @as(usize, y) * a.width * 4;
133 @memcpy(pixels[row_off .. row_off + a.width * 4], a.pixels[a_off .. a_off + a.width * 4]);
134 @memcpy(pixels[row_off + a.width * 4 .. row_off + 2 * a.width * 4], b.pixels[a_off .. a_off + a.width * 4]);
135 var x: u32 = 0;
136 while (x < a.width) : (x += 1) {
137 const off = a_off + x * 4;
138 const dr = (@as(f64, @floatFromInt(a.pixels[off + 0])) - @as(f64, @floatFromInt(b.pixels[off + 0]))) / 255.0;
139 const dg = (@as(f64, @floatFromInt(a.pixels[off + 1])) - @as(f64, @floatFromInt(b.pixels[off + 1]))) / 255.0;
140 const db = (@as(f64, @floatFromInt(a.pixels[off + 2])) - @as(f64, @floatFromInt(b.pixels[off + 2]))) / 255.0;
141 const d = @sqrt((dr * dr + dg * dg + db * db) / 3.0);
142 const brightness: u8 = @intFromFloat(@min(255.0, d * 255.0 * 2.0));
143 const dst = row_off + 2 * a.width * 4 + x * 4;
144 pixels[dst + 0] = brightness;
145 pixels[dst + 1] = brightness;
146 pixels[dst + 2] = brightness;
147 pixels[dst + 3] = 255;
148 }
149 }
150 return .{ .width = w, .height = h, .pixels = pixels };
151 }