Something went wrong. Try again.
A Zig library for driving Elgato Stream Deck devices over USB HID.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! BMP encoding of images destined for a Stream Deck. Devices that//! expect `ImageFormat.BMP` key or touchscreen data are fed the bytes//! produced here.
const std = @import("std");
const z2d = @import("z2d");const z2dimg = @import("z2dimg");
/// Encode a surface as BMP data suitable for sending to a Stream Deck.////// The shape is not a free choice. The models that take BMP at all --/// the Original and the two Minis -- parse a fixed header rather than a/// format, so the twenty-four bit `BI_RGB` under a plain/// `BITMAPINFOHEADER` that `.rgb24` writes is the only thing they/// accept, and the rows go bottom-up as a BMP's have since 1990.////// The returned slice is owned by the caller and must be freed with/// `alloc`.pub fn encode(alloc: std.mem.Allocator, surface: z2d.Surface) ![]u8 { var out: std.Io.Writer.Allocating = .init(alloc); errdefer out.deinit();
z2dimg.encode(alloc, &surface, &out.writer, .{ .bmp = .{ .row_order = .bottom_up, .depth = .rgb24, } }) catch |err| switch (err) { // An allocating writer has exactly one way to fail, and it is // running out of memory -- but it has to report that as // `WriteFailed`, because that is the only failure a `Writer` // has. Put the real reason back, so that a caller is not told // a write failed when there was no writer to fail. error.WriteFailed => return error.OutOfMemory, else => |e| return e, };
return try out.toOwnedSlice();}
test encode { const alloc = std.testing.allocator;
var surface: z2d.Surface = try .init(.image_surface_rgb, alloc, 8, 4); defer surface.deinit(alloc); surface.paintPixel(.{ .rgb = .{ .r = 0, .g = 0, .b = 0 } });
const data = try encode(alloc, surface); defer alloc.free(data);
try std.testing.expectEqualSlices(u8, &.{ 0x42, 0x4d }, data[0..2]);
// A fifty-four byte header, then three bytes a pixel with each row // padded to a multiple of four. Eight pixels is twenty-four bytes, // which needs no padding. try std.testing.expectEqual(54 + 8 * 3 * 4, data.len);}