Skip to content

Commit 81a6b37

Browse files
committed
image: add transmitPreEncodedImage method
Add a method that allows users of the library to pre-encode an image using whichever base64 encoder they want (ie a simd encoder).
1 parent 22fab6f commit 81a6b37

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

src/Vaxis.zig

Lines changed: 50 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -717,38 +717,16 @@ pub fn translateMouse(self: Vaxis, mouse: Mouse) Mouse {
717717
return result;
718718
}
719719

720-
pub fn transmitImage(
720+
/// Transmit an image which has been pre-base64 encoded
721+
pub fn transmitPreEncodedImage(
721722
self: *Vaxis,
722-
alloc: std.mem.Allocator,
723723
tty: AnyWriter,
724-
img: *zigimg.Image,
724+
bytes: []const u8,
725+
width: usize,
726+
height: usize,
725727
format: Image.TransmitFormat,
726728
) !Image {
727-
if (!self.caps.kitty_graphics) return error.NoGraphicsCapability;
728729
defer self.next_img_id += 1;
729-
730-
var arena = std.heap.ArenaAllocator.init(alloc);
731-
defer arena.deinit();
732-
733-
const buf = switch (format) {
734-
.png => png: {
735-
const png_buf = try arena.allocator().alloc(u8, img.imageByteSize());
736-
const png = try img.writeToMemory(png_buf, .{ .png = .{} });
737-
break :png png;
738-
},
739-
.rgb => rgb: {
740-
try img.convert(.rgb24);
741-
break :rgb img.rawBytes();
742-
},
743-
.rgba => rgba: {
744-
try img.convert(.rgba32);
745-
break :rgba img.rawBytes();
746-
},
747-
};
748-
749-
const b64_buf = try arena.allocator().alloc(u8, base64Encoder.calcSize(buf.len));
750-
const encoded = base64Encoder.encode(b64_buf, buf);
751-
752730
const id = self.next_img_id;
753731

754732
const fmt: u8 = switch (format) {
@@ -757,41 +735,75 @@ pub fn transmitImage(
757735
.png => 100,
758736
};
759737

760-
if (encoded.len < 4096) {
738+
if (bytes.len < 4096) {
761739
try tty.print(
762740
"\x1b_Gf={d},s={d},v={d},i={d};{s}\x1b\\",
763741
.{
764742
fmt,
765-
img.width,
766-
img.height,
743+
width,
744+
height,
767745
id,
768-
encoded,
746+
bytes,
769747
},
770748
);
771749
} else {
772750
var n: usize = 4096;
773751

774752
try tty.print(
775753
"\x1b_Gf={d},s={d},v={d},i={d},m=1;{s}\x1b\\",
776-
.{ fmt, img.width, img.height, id, encoded[0..n] },
754+
.{ fmt, width, height, id, bytes[0..n] },
777755
);
778-
while (n < encoded.len) : (n += 4096) {
779-
const end: usize = @min(n + 4096, encoded.len);
780-
const m: u2 = if (end == encoded.len) 0 else 1;
756+
while (n < bytes.len) : (n += 4096) {
757+
const end: usize = @min(n + 4096, bytes.len);
758+
const m: u2 = if (end == bytes.len) 0 else 1;
781759
try tty.print(
782760
"\x1b_Gm={d};{s}\x1b\\",
783761
.{
784762
m,
785-
encoded[n..end],
763+
bytes[n..end],
786764
},
787765
);
788766
}
789767
}
790768
return .{
791769
.id = id,
792-
.width = img.width,
793-
.height = img.height,
770+
.width = width,
771+
.height = height,
772+
};
773+
}
774+
775+
pub fn transmitImage(
776+
self: *Vaxis,
777+
alloc: std.mem.Allocator,
778+
tty: AnyWriter,
779+
img: *zigimg.Image,
780+
format: Image.TransmitFormat,
781+
) !Image {
782+
if (!self.caps.kitty_graphics) return error.NoGraphicsCapability;
783+
784+
var arena = std.heap.ArenaAllocator.init(alloc);
785+
defer arena.deinit();
786+
787+
const buf = switch (format) {
788+
.png => png: {
789+
const png_buf = try arena.allocator().alloc(u8, img.imageByteSize());
790+
const png = try img.writeToMemory(png_buf, .{ .png = .{} });
791+
break :png png;
792+
},
793+
.rgb => rgb: {
794+
try img.convert(.rgb24);
795+
break :rgb img.rawBytes();
796+
},
797+
.rgba => rgba: {
798+
try img.convert(.rgba32);
799+
break :rgba img.rawBytes();
800+
},
794801
};
802+
803+
const b64_buf = try arena.allocator().alloc(u8, base64Encoder.calcSize(buf.len));
804+
const encoded = base64Encoder.encode(b64_buf, buf);
805+
806+
return self.transmitPreEncodedImage(tty, encoded, img.width, img.height, format);
795807
}
796808

797809
pub fn loadImage(

0 commit comments

Comments
 (0)