// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The image headers the devices prefix each page of image data with, //! and the conversion between the surface a caller draws on and the //! pixel buffer an encoder wants. //! //! Drawing and encoding both happen on a `z2d.Surface` -- z2d draws it //! and z2dimg writes it out -- so what is left here is the rotation and //! the flips a particular display needs, which `transform` applies. const std = @import("std"); const z2d = @import("z2d"); const Rotation = @import("root.zig").ImageRotation; pub const ImageHeaderV1 = extern struct { byte0: u8 = 0x02, byte1: u8 = 0x01, page_number: u8, byte3: u8 = 0x00, last: bool align(1), key: u8, padding: [10]u8 = std.mem.zeroes([10]u8), }; pub const ImageHeaderV2 = extern struct { byte0: u8 = 0x02, byte1: u8 = 0x07, key: u8, last: bool align(1), length: u16, page_number: u16, }; /// The header on each page of an image sent to the Stream Deck Neo's /// info strip. It is the shape of `ImageHeaderV2` with a command byte /// of its own, and no key index, since the device has only the one /// screen; unlike `ImageHeaderV3` it carries no rectangle, so the whole /// strip is written or none of it. pub const ImageHeaderV4 = extern struct { byte0: u8 = 0x02, byte1: u8 = 0x0b, byte2: u8 = 0x00, last: bool align(1), length: u16, page_number: u16, }; /// The header on each page of an image sent to the whole panel behind /// the keys. It is the shape of `ImageHeaderV4` with a command byte of /// its own; there is only one such screen per device, so it carries no /// index, and it is always written whole, so it carries no rectangle. pub const ImageHeaderV5 = extern struct { byte0: u8 = 0x02, byte1: u8 = 0x08, /// Which screen the image is for, on a model whose panel is more /// than one: the Studio's two halves are screens 0 and 1, left and /// right. Zero everywhere else. It sits where a key image header /// carries the key index. screen: u8 = 0x00, last: bool align(1), length: u16, page_number: u16, }; /// The header on each page of a **background** image. /// /// It is the same eight bytes as `ImageHeaderV2` in every respect but /// one, and that one is a trap: the chunk index and the chunk size are /// **the other way round**. A key image carries its size at `+0x04` /// and its page number at `+0x06`; a background carries its page /// number at `+0x04` and its size at `+0x06`. Elgato's Update /// Background and Update Key Image tables differ in exactly that, /// which is easy to read past. /// /// Getting it backwards would not fail loudly. The first page of a /// small image has page number zero and a size of about a kilobyte, so /// a swapped header asks the device to write page 1016 of nothing -- /// and the device simply shows no background, with nothing to say why. /// This library once had the same fault in the touchscreen header, and /// that is what it looked like there too. pub const ImageHeaderV6 = extern struct { byte0: u8 = 0x02, byte1: u8 = 0x0d, /// Which background slot is being written. index: u8, last: bool align(1), /// At `+0x04`, where a key image keeps its length. page_number: u16, /// At `+0x06`, where a key image keeps its page number. length: u16, }; test ImageHeaderV6 { try std.testing.expectEqual(8, @sizeOf(ImageHeaderV6)); var buf: [16]u8 = undefined; var writer = std.Io.Writer.fixed(&buf); try writer.writeStruct(ImageHeaderV6{ .index = 2, .last = true, .page_number = 0x0304, .length = 0x0102, }, .little); // The whole point of this header is the field order, so it is // pinned byte by byte: page number first, then length. try std.testing.expectEqualSlices(u8, &.{ 0x02, 0x0d, 0x02, 0x01, 0x04, 0x03, 0x02, 0x01, }, writer.buffered()); // And said the other way about: a key image with the same numbers // puts them in the opposite order, which is what makes mixing the // two silent rather than obvious. var key_buf: [16]u8 = undefined; var key_writer = std.Io.Writer.fixed(&key_buf); try key_writer.writeStruct(ImageHeaderV2{ .key = 2, .last = true, .length = 0x0102, .page_number = 0x0304, }, .little); try std.testing.expectEqualSlices(u8, &.{ 0x02, 0x07, 0x02, 0x01, 0x02, 0x01, 0x04, 0x03, }, key_writer.buffered()); } test ImageHeaderV5 { try std.testing.expectEqual(8, @sizeOf(ImageHeaderV5)); var buf: [16]u8 = undefined; var writer = std.Io.Writer.fixed(&buf); try writer.writeStruct(ImageHeaderV5{ .last = true, .length = 0x0102, .page_number = 0x0304, }, .little); // The command byte is what separates this from the strip header, // so it is worth pinning rather than assuming. try std.testing.expectEqualSlices(u8, &.{ 0x02, 0x08, 0x00, 0x01, 0x02, 0x01, 0x04, 0x03, }, writer.buffered()); } test ImageHeaderV4 { try std.testing.expectEqual(8, @sizeOf(ImageHeaderV4)); var buf: [16]u8 = undefined; var writer = std.Io.Writer.fixed(&buf); try writer.writeStruct(ImageHeaderV4{ .last = true, .length = 0x0102, .page_number = 0x0304, }, .little); try std.testing.expectEqualSlices(u8, &.{ 0x02, 0x0b, 0x00, 0x01, 0x02, 0x01, 0x04, 0x03, }, writer.buffered()); } /// The header on each page of a touchscreen image. Every field is /// `align(1)`, which is not decoration: `page_number` sits at offset 11 /// and `length` at 13, and without it the compiler pads the struct out /// to the natural alignment of a `u16` and the whole thing becomes 18 /// bytes with both fields two bytes late. pub const ImageHeaderV3 = extern struct { byte0: u8 = 0x02, byte1: u8 = 0x0c, x: u16 align(1), y: u16 align(1), width: u16 align(1), height: u16 align(1), last: bool align(1), page_number: u16 align(1), length: u16 align(1), byte15: u8 = 0x00, }; test ImageHeaderV3 { // The device is handed these bytes as they sit in memory, so the // layout is the protocol. 16 bytes, and the two `u16`s after // `last` land on odd offsets. try std.testing.expectEqual(16, @sizeOf(ImageHeaderV3)); try std.testing.expectEqual(2, @offsetOf(ImageHeaderV3, "x")); try std.testing.expectEqual(4, @offsetOf(ImageHeaderV3, "y")); try std.testing.expectEqual(6, @offsetOf(ImageHeaderV3, "width")); try std.testing.expectEqual(8, @offsetOf(ImageHeaderV3, "height")); try std.testing.expectEqual(10, @offsetOf(ImageHeaderV3, "last")); try std.testing.expectEqual(11, @offsetOf(ImageHeaderV3, "page_number")); try std.testing.expectEqual(13, @offsetOf(ImageHeaderV3, "length")); try std.testing.expectEqual(15, @offsetOf(ImageHeaderV3, "byte15")); var buf: [32]u8 = undefined; var writer = std.Io.Writer.fixed(&buf); try writer.writeStruct(ImageHeaderV3{ .x = 0x0102, .y = 0x0304, .width = 0x0506, .height = 0x0708, .last = true, .page_number = 0x090a, .length = 0x0b0c, }, .little); try std.testing.expectEqualSlices(u8, &.{ 0x02, 0x0c, 0x02, 0x01, 0x04, 0x03, 0x06, 0x05, 0x08, 0x07, 0x01, 0x0a, 0x09, 0x0c, 0x0b, 0x00, }, writer.buffered()); } test ImageHeaderV1 { try std.testing.expectEqual(16, @sizeOf(ImageHeaderV1)); } test ImageHeaderV2 { try std.testing.expectEqual(8, @sizeOf(ImageHeaderV2)); } /// A black surface of the given size, ready to be drawn on. The caller /// owns it and must release it with `deinit`. pub fn blankImage(alloc: std.mem.Allocator, width: usize, height: usize) !z2d.Surface { var surface: z2d.Surface = try .init( .image_surface_rgb, alloc, @intCast(width), @intCast(height), ); errdefer surface.deinit(alloc); surface.paintPixel(.{ .rgb = .{ .r = 0x0, .g = 0x0, .b = 0x0 } }); return surface; } /// The rotation and flips to apply while copying a surface out. A /// display is not necessarily mounted the same way up as the image that /// is drawn for it; `Keys.KeyImageFeature` and /// `Touchscreen.TouchScreenFeature` say which way round each one is. pub const Transform = struct { /// Rotation, applied first, clockwise. A quarter turn either way /// exchanges the width and the height. rotation: ?Rotation = null, /// Mirror horizontally, applied after the rotation. flip_left_right: bool = false, /// Mirror vertically, applied after the rotation. flip_top_bottom: bool = false, }; /// A copy of `surface` with `t` applied. The original is left /// untouched and the caller owns the result, which must be released /// with `deinit`. /// /// This exists because a key's display is not necessarily mounted the /// same way up as the image drawn for it: `Keys.KeyImageFeature` and /// `Touchscreen.TouchScreenFeature` say which way round each one is, /// and the transform is undone by the physical mounting. pub fn transform(alloc: std.mem.Allocator, surface: z2d.Surface, t: Transform) !z2d.Surface { const src_width: usize = @intCast(surface.getWidth()); const src_height: usize = @intCast(surface.getHeight()); // A quarter turn either way exchanges the two axes. const quarter_turn = switch (t.rotation orelse .@"180") { .@"90", .@"270" => true, .@"180" => false, }; const dst_width = if (quarter_turn) src_height else src_width; const dst_height = if (quarter_turn) src_width else src_height; var out: z2d.Surface = try .init( .image_surface_rgb, alloc, @intCast(dst_width), @intCast(dst_height), ); errdefer out.deinit(alloc); for (0..dst_height) |dy| { for (0..dst_width) |dx| { // The flips come after the rotation, so they are undone // first. Each is its own inverse. const fx = if (t.flip_left_right) dst_width - 1 - dx else dx; const fy = if (t.flip_top_bottom) dst_height - 1 - dy else dy; // Undo the rotation to find the source pixel. var sx: usize = fx; var sy: usize = fy; if (t.rotation) |rotation| switch (rotation) { .@"90" => { sx = fy; sy = src_height - 1 - fx; }, .@"180" => { sx = src_width - 1 - fx; sy = src_height - 1 - fy; }, .@"270" => { sx = src_width - 1 - fy; sy = fx; }, }; out.putPixel( @intCast(dx), @intCast(dy), surface.getPixel(@intCast(sx), @intCast(sy)) orelse unreachable, ); } } return out; } /// Read one pixel back as plain RGB, for the tests below. fn at(surface: z2d.Surface, x: i32, y: i32) z2d.pixel.RGB { return .fromPixel(surface.getPixel(x, y).?); } const red: z2d.Pixel = .{ .rgb = .{ .r = 0xff, .g = 0x00, .b = 0x00 } }; const green: z2d.Pixel = .{ .rgb = .{ .r = 0x00, .g = 0xff, .b = 0x00 } }; test transform { const alloc = std.testing.allocator; // A 2x1 surface, red on the left and green on the right, so that // the geometry of a transform shows up. var surface: z2d.Surface = try .init(.image_surface_rgb, alloc, 2, 1); defer surface.deinit(alloc); surface.putPixel(0, 0, red); surface.putPixel(1, 0, green); // Nothing asked for is nothing done. { var out = try transform(alloc, surface, .{}); defer out.deinit(alloc); try std.testing.expectEqual(@as(i32, 2), out.getWidth()); try std.testing.expectEqual(@as(i32, 1), out.getHeight()); try std.testing.expect(red.equal(at(out, 0, 0).asPixel())); try std.testing.expect(green.equal(at(out, 1, 0).asPixel())); } // A horizontal mirror reverses the row. { var out = try transform(alloc, surface, .{ .flip_left_right = true }); defer out.deinit(alloc); try std.testing.expect(green.equal(at(out, 0, 0).asPixel())); try std.testing.expect(red.equal(at(out, 1, 0).asPixel())); } // A quarter turn clockwise makes the 2x1 row a 1x2 column, with the // left-hand pixel on top. { var out = try transform(alloc, surface, .{ .rotation = .@"90" }); defer out.deinit(alloc); try std.testing.expectEqual(@as(i32, 1), out.getWidth()); try std.testing.expectEqual(@as(i32, 2), out.getHeight()); try std.testing.expect(red.equal(at(out, 0, 0).asPixel())); try std.testing.expect(green.equal(at(out, 0, 1).asPixel())); } // And a quarter turn the other way puts it on the bottom. { var out = try transform(alloc, surface, .{ .rotation = .@"270" }); defer out.deinit(alloc); try std.testing.expectEqual(@as(i32, 1), out.getWidth()); try std.testing.expectEqual(@as(i32, 2), out.getHeight()); try std.testing.expect(green.equal(at(out, 0, 0).asPixel())); try std.testing.expect(red.equal(at(out, 0, 1).asPixel())); } // A half turn reverses the row and leaves the dimensions alone. { var out = try transform(alloc, surface, .{ .rotation = .@"180" }); defer out.deinit(alloc); try std.testing.expectEqual(@as(i32, 2), out.getWidth()); try std.testing.expectEqual(@as(i32, 1), out.getHeight()); try std.testing.expect(green.equal(at(out, 0, 0).asPixel())); try std.testing.expect(red.equal(at(out, 1, 0).asPixel())); } } test "the transforms a device asks for compose the way the device does" { const alloc = std.testing.allocator; var surface: z2d.Surface = try .init(.image_surface_rgb, alloc, 3, 2); defer surface.deinit(alloc); for (0..2) |y| { for (0..3) |x| { surface.putPixel(@intCast(x), @intCast(y), .{ .rgb = .{ .r = @intCast(x * 40), .g = @intCast(y * 40), .b = 0, } }); } } // Both flips together are a half turn, which is how every gen 2 // model's table describes its upside-down displays. var flipped = try transform(alloc, surface, .{ .flip_left_right = true, .flip_top_bottom = true, }); defer flipped.deinit(alloc); var turned = try transform(alloc, surface, .{ .rotation = .@"180" }); defer turned.deinit(alloc); for (0..2) |y| { for (0..3) |x| { const a = at(flipped, @intCast(x), @intCast(y)); const b = at(turned, @intCast(x), @intCast(y)); try std.testing.expectEqual(a.r, b.r); try std.testing.expectEqual(a.g, b.g); } } // And doing it twice is doing nothing. var back = try transform(alloc, flipped, .{ .flip_left_right = true, .flip_top_bottom = true, }); defer back.deinit(alloc); for (0..2) |y| { for (0..3) |x| { const a = at(surface, @intCast(x), @intCast(y)); const b = at(back, @intCast(x), @intCast(y)); try std.testing.expectEqual(a.r, b.r); try std.testing.expectEqual(a.g, b.g); } } } test blankImage { const alloc = std.testing.allocator; var surface = try blankImage(alloc, 4, 3); defer surface.deinit(alloc); try std.testing.expectEqual(@as(i32, 4), surface.getWidth()); try std.testing.expectEqual(@as(i32, 3), surface.getHeight()); for (0..3) |y| { for (0..4) |x| { const px = at(surface, @intCast(x), @intCast(y)); try std.testing.expectEqual(@as(u8, 0), px.r); try std.testing.expectEqual(@as(u8, 0), px.g); try std.testing.expectEqual(@as(u8, 0), px.b); } } } test "a surface and a transform of it clean up after a failed allocation" { try std.testing.checkAllAllocationFailures(std.testing.allocator, struct { fn run(alloc: std.mem.Allocator) !void { var surface = try blankImage(alloc, 12, 8); defer surface.deinit(alloc); var turned = try transform(alloc, surface, .{ .rotation = .@"90", .flip_left_right = true, .flip_top_bottom = true, }); defer turned.deinit(alloc); } }.run, .{}); } test { std.testing.refAllDecls(@This()); }