// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Images for working out what a device is doing. Nothing here is //! needed to drive a Stream Deck; it is here so that a key can be made //! to say which key it is, which is the first thing worth knowing when //! an image lands on the wrong key, upside down, or mirrored. //! //! The digits are drawn as seven segment shapes out of axis-aligned //! rectangles rather than set in a typeface. That is deliberate: z2d //! renders text from a TrueType font loaded at run time, and a //! diagnostic that needs an asset shipped alongside it is one that //! stops working exactly when something else has already gone wrong. //! Rectangles also land on whole pixels, so what comes back is crisp at //! any key size rather than blurred by antialiasing. const std = @import("std"); const z2d = @import("z2d"); const image = @import("image.zig"); /// A color as the devices take one, for the controls that are given a /// color rather than an image. pub const Rgb = struct { r: u8, g: u8, b: u8 }; /// Which way round a diagnostic image is drawn. pub const Polarity = enum { /// Yellow figures on black, which is what a key shows at rest. yellow_on_black, /// Black figures on yellow, the exact inverse of the above. black_on_yellow, /// The color the figures are drawn in. pub fn foreground(self: Polarity) z2d.Pixel { return switch (self) { .yellow_on_black => yellow, .black_on_yellow => black, }; } /// The color behind them. pub fn background(self: Polarity) z2d.Pixel { return switch (self) { .yellow_on_black => black, .black_on_yellow => yellow, }; } /// The color the figures are drawn in, as three bytes. A touch key /// has no display to draw figures on, so this is what it is given /// instead; it also keeps a caller that only wants a color from /// having to name z2d's pixel types. pub fn foregroundRgb(self: Polarity) Rgb { const px: z2d.pixel.RGB = .fromPixel(self.foreground()); return .{ .r = px.r, .g = px.g, .b = px.b }; } /// The other one. pub fn inverse(self: Polarity) Polarity { return switch (self) { .yellow_on_black => .black_on_yellow, .black_on_yellow => .yellow_on_black, }; } }; const yellow: z2d.Pixel = .{ .rgb = .{ .r = 0xff, .g = 0xff, .b = 0x00 } }; const black: z2d.Pixel = .{ .rgb = .{ .r = 0x00, .g = 0x00, .b = 0x00 } }; /// The segments of a seven segment digit, in the order they are /// traditionally lettered: /// /// aaaa /// f b /// f b /// gggg /// e c /// e c /// dddd const Segment = enum(u3) { a, b, c, d, e, f, g }; /// Which segments each digit lights. Seven segment digits are used /// here because they are unambiguous at a glance and at small sizes, /// which is the whole point of the exercise. const digits = [10][]const Segment{ &.{ .a, .b, .c, .d, .e, .f }, // 0 &.{ .b, .c }, // 1 &.{ .a, .b, .g, .e, .d }, // 2 &.{ .a, .b, .g, .c, .d }, // 3 &.{ .f, .g, .b, .c }, // 4 &.{ .a, .f, .g, .c, .d }, // 5 &.{ .a, .f, .g, .e, .c, .d }, // 6 &.{ .a, .b, .c }, // 7 &.{ .a, .b, .c, .d, .e, .f, .g }, // 8 &.{ .a, .b, .c, .d, .f, .g }, // 9 }; /// Fill a rectangle on `surface`, clipped to it. A negative or /// oversized rectangle draws nothing rather than reaching outside the /// buffer. fn fill(surface: *z2d.Surface, x: i32, y: i32, w: i32, h: i32, px: z2d.Pixel) void { if (w <= 0 or h <= 0) return; const surface_w = surface.getWidth(); const surface_h = surface.getHeight(); const x0 = @max(x, 0); const y0 = @max(y, 0); const x1 = @min(x + w, surface_w); const y1 = @min(y + h, surface_h); if (x1 <= x0 or y1 <= y0) return; var row = y0; while (row < y1) : (row += 1) { surface.paintStride(x0, row, @intCast(x1 - x0), px); } } /// Draw one seven segment digit into the box at `x`, `y` of `w` by `h`, /// with segments `t` thick. fn drawDigit(surface: *z2d.Surface, digit: u8, x: i32, y: i32, w: i32, h: i32, t: i32, px: z2d.Pixel) void { std.debug.assert(digit < 10); // The two vertical runs meet the middle segment halfway down. const half = @divTrunc(h - t, 2); const run = half - t; for (digits[digit]) |segment| { switch (segment) { .a => fill(surface, x + t, y, w - 2 * t, t, px), .g => fill(surface, x + t, y + half, w - 2 * t, t, px), .d => fill(surface, x + t, y + h - t, w - 2 * t, t, px), .f => fill(surface, x, y + t, t, run, px), .b => fill(surface, x + w - t, y + t, t, run, px), .e => fill(surface, x, y + half + t, t, run, px), .c => fill(surface, x + w - t, y + half + t, t, run, px), } } } /// The number of decimal digits `n` is written with. fn digitCount(n: u16) u16 { var count: u16 = 1; var rest = n / 10; while (rest > 0) : (rest /= 10) count += 1; return count; } /// An image of `width` by `height` carrying `index` as seven segment /// figures, centered, with a rule under them. /// /// The rule is the point of it as much as the figures are. A key image /// is rotated and flipped on its way to the display, and a device that /// has been given the wrong transform shows a 6 where a 9 belongs and /// looks perfectly reasonable doing it. An underlined 6 upside down is /// a 9 with a line over it, which is obviously wrong. /// /// The caller owns the result and must release it with `deinit`. pub fn keyIndexImage( alloc: std.mem.Allocator, width: usize, height: usize, index: u16, polarity: Polarity, ) !z2d.Surface { var surface = try image.blankImage(alloc, width, height); errdefer surface.deinit(alloc); surface.paintPixel(polarity.background()); const w: i32 = @intCast(width); const h: i32 = @intCast(height); const count: i32 = @intCast(digitCount(index)); // A thin margin, and then the figures are made as large as what is // left will hold. Sizing them to the space actually available // rather than to a fixed fraction of the key is what keeps a // two digit index legible without leaving a one digit index // needlessly small. const avail_w = w - 2 * @divTrunc(w, 16); const avail_h = h - 2 * @divTrunc(h, 16); // Start from the height. The rule and the gap above it are each a // stroke thick and a stroke is a seventh of a digit, so the whole // block is nine sevenths of the digit height. var digit_h = @divTrunc(avail_h * 7, 9); var digit_w = @divTrunc(digit_h * 5, 8); var t = @max(2, @divTrunc(digit_h, 7)); // Then give up height if that came out too wide for the number of // digits, which is what happens on a square key from ten upwards. const wanted_w = count * digit_w + (count - 1) * t; if (wanted_w > avail_w) { digit_h = @divTrunc(digit_h * avail_w, wanted_w); digit_w = @divTrunc(digit_h * 5, 8); t = @max(2, @divTrunc(digit_h, 7)); } const gap = t; const rule_gap = t; const rule_h = t; const total_w = count * digit_w + (count - 1) * gap; const total_h = digit_h + rule_gap + rule_h; const left = @divTrunc(w - total_w, 2); const top = @divTrunc(h - total_h, 2); const fg = polarity.foreground(); // Most significant digit first, left to right. var place: i32 = count - 1; var rest = index; while (place >= 0) : (place -= 1) { const digit: u8 = @intCast(rest % 10); rest /= 10; drawDigit( &surface, digit, left + place * (digit_w + gap), top, digit_w, digit_h, t, fg, ); } // The rule, spanning the figures rather than the whole key, so that // it reads as an underline and not as a border. fill(&surface, left, top + digit_h + rule_gap, total_w, rule_h, fg); return surface; } test keyIndexImage { const alloc = std.testing.allocator; var surface = try keyIndexImage(alloc, 72, 72, 6, .yellow_on_black); defer surface.deinit(alloc); try std.testing.expectEqual(@as(i32, 72), surface.getWidth()); try std.testing.expectEqual(@as(i32, 72), surface.getHeight()); // Something was drawn, and in the right colors. var yellows: usize = 0; var blacks: usize = 0; for (0..72) |y| { for (0..72) |x| { const px: z2d.pixel.RGB = .fromPixel(surface.getPixel(@intCast(x), @intCast(y)).?); if (px.r == 0xff and px.g == 0xff and px.b == 0x00) { yellows += 1; } else if (px.r == 0 and px.g == 0 and px.b == 0) { blacks += 1; } else { // No antialiasing, so there is no third color. return error.UnexpectedColor; } } } try std.testing.expect(yellows > 0); try std.testing.expect(blacks > 0); try std.testing.expectEqual(72 * 72, yellows + blacks); } test "the two polarities are exact inverses of each other" { const alloc = std.testing.allocator; for ([_]u16{ 0, 1, 6, 9, 10, 15, 31, 47 }) |index| { var normal = try keyIndexImage(alloc, 96, 96, index, .yellow_on_black); defer normal.deinit(alloc); var inverted = try keyIndexImage(alloc, 96, 96, index, .black_on_yellow); defer inverted.deinit(alloc); for (0..96) |y| { for (0..96) |x| { const a: z2d.pixel.RGB = .fromPixel(normal.getPixel(@intCast(x), @intCast(y)).?); const b: z2d.pixel.RGB = .fromPixel(inverted.getPixel(@intCast(x), @intCast(y)).?); try std.testing.expectEqual(@as(u8, 0xff), a.r ^ b.r); try std.testing.expectEqual(@as(u8, 0xff), a.g ^ b.g); try std.testing.expectEqual(@as(u8, 0x00), a.b ^ b.b); } } } } test "a 6 and a 9 differ, and so does each from the other upside down" { const alloc = std.testing.allocator; const size = 96; var six = try keyIndexImage(alloc, size, size, 6, .yellow_on_black); defer six.deinit(alloc); var nine = try keyIndexImage(alloc, size, size, 9, .yellow_on_black); defer nine.deinit(alloc); var differs = false; var six_rotated_matches_nine = true; for (0..size) |y| { for (0..size) |x| { const s: z2d.pixel.RGB = .fromPixel(six.getPixel(@intCast(x), @intCast(y)).?); const n: z2d.pixel.RGB = .fromPixel(nine.getPixel(@intCast(x), @intCast(y)).?); if (!std.meta.eql(s, n)) differs = true; // A half turn of the six, compared against the nine. The // rule is what makes these differ; without it a seven // segment 6 turned upside down is a 9. const r: z2d.pixel.RGB = .fromPixel(six.getPixel( @intCast(size - 1 - x), @intCast(size - 1 - y), ).?); if (!std.meta.eql(r, n)) six_rotated_matches_nine = false; } } try std.testing.expect(differs); try std.testing.expect(!six_rotated_matches_nine); } test "an index of more than one digit is drawn with all of them" { const alloc = std.testing.allocator; // 47 is the highest key on the largest model, so two digits is as // far as this has to go -- but the drawing is general, so check // that it does not clip or overflow at three. for ([_]u16{ 0, 9, 10, 47, 100, 999 }) |index| { var surface = try keyIndexImage(alloc, 80, 80, index, .yellow_on_black); defer surface.deinit(alloc); try std.testing.expectEqual(@as(i32, 80), surface.getWidth()); } } test "the smallest plausible key still draws something" { const alloc = std.testing.allocator; // `fill` clips rather than reaching outside the buffer, so a size // too small for the layout has to come back blank rather than // corrupt memory or panic. for ([_]usize{ 1, 2, 4, 8, 16 }) |size| { var surface = try keyIndexImage(alloc, size, size, 8, .yellow_on_black); defer surface.deinit(alloc); try std.testing.expectEqual(@as(i32, @intCast(size)), surface.getWidth()); } } test Polarity { // The two are each other's opposite, and inverting twice is a // round trip. try std.testing.expectEqual(Polarity.black_on_yellow, Polarity.yellow_on_black.inverse()); try std.testing.expectEqual(Polarity.yellow_on_black, Polarity.black_on_yellow.inverse()); // Inverting twice is a round trip. for ([_]Polarity{ .yellow_on_black, .black_on_yellow }) |p| { try std.testing.expectEqual(p, p.inverse().inverse()); } // Each one's foreground is the other's background. for ([_]Polarity{ .yellow_on_black, .black_on_yellow }) |p| { try std.testing.expect(p.foreground().equal(p.inverse().background())); try std.testing.expect(p.background().equal(p.inverse().foreground())); } try std.testing.expectEqual( Rgb{ .r = 0xff, .g = 0xff, .b = 0x00 }, Polarity.yellow_on_black.foregroundRgb(), ); try std.testing.expectEqual( Rgb{ .r = 0x00, .g = 0x00, .b = 0x00 }, Polarity.black_on_yellow.foregroundRgb(), ); } test "a diagnostic image cleans up after a failed allocation" { try std.testing.checkAllAllocationFailures(std.testing.allocator, struct { fn run(alloc: std.mem.Allocator) !void { var surface = try keyIndexImage(alloc, 72, 72, 14, .yellow_on_black); defer surface.deinit(alloc); } }.run, .{}); } test { std.testing.refAllDecls(@This()); }