From 006422963537074fa3708e6178d839eb27be8ea4 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Mon, 5 Aug 2024 07:58:04 -0500 Subject: [PATCH] image: implement local filesystem transmission Implement transmission over a local filesystem, by file, temp file, or shared mem. --- src/Image.zig | 6 ++++++ src/Vaxis.zig | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/src/Image.zig b/src/Image.zig index 750f1f5..97b1042 100644 --- a/src/Image.zig +++ b/src/Image.zig @@ -21,6 +21,12 @@ pub const TransmitFormat = enum { png, }; +pub const TransmitMedium = enum { + file, + temp_file, + shared_mem, +}; + pub const Placement = struct { img_id: u32, options: Image.DrawOptions, diff --git a/src/Vaxis.zig b/src/Vaxis.zig index ceb8c41..b24d720 100644 --- a/src/Vaxis.zig +++ b/src/Vaxis.zig @@ -717,6 +717,61 @@ pub fn translateMouse(self: Vaxis, mouse: Mouse) Mouse { return result; } +/// Transmit an image using the local filesystem. Allocates only for base64 encoding +pub fn transmitLocalImagePath( + self: *Vaxis, + allocator: std.mem.Allocator, + tty: AnyWriter, + payload: []const u8, + height: usize, + width: usize, + medium: Image.TransmitMedium, + format: Image.TransmitFormat, +) !Image { + defer self.next_img_id += 1; + + const id = self.next_img_id; + + const size = base64Encoder.calcSize(payload.len); + if (size >= 4096) return error.PathTooLong; + + const buf = try allocator.alloc(u8, size); + const encoded = base64Encoder.encode(buf, payload); + defer allocator.free(buf); + + const medium_char: u8 = switch (medium) { + .file => 'f', + .temp_file => 't', + .shared_mem => 's', + }; + + switch (format) { + .rgb => { + try tty.print( + "\x1b_Gf=24,s={d},v={d},i={d},t={c};{s}\x1b\\", + .{ width, height, id, medium_char, encoded }, + ); + }, + .rgba => { + try tty.print( + "\x1b_Gf=32,s={d},v={d},i={d},t={c};{s}\x1b\\", + .{ width, height, id, medium_char, encoded }, + ); + }, + .png => { + try tty.print( + "\x1b_Gf=100,i={d},t={c};{s}\x1b\\", + .{ id, medium_char, encoded }, + ); + }, + } + return .{ + .id = id, + .width = width, + .height = height, + }; +} + /// Transmit an image which has been pre-base64 encoded pub fn transmitPreEncodedImage( self: *Vaxis,