a modern tui library written in zig
17
fork

Configure Feed

Select the types of activity you want to include in your feed.

image: allow transmitting zigimg Images directly

And with other transmission formats

+55 -15
+6
src/Image.zig
··· 15 15 mem: []const u8, 16 16 }; 17 17 18 + pub const TransmitFormat = enum { 19 + rgb, 20 + rgba, 21 + png, 22 + }; 23 + 18 24 pub const Placement = struct { 19 25 img_id: u32, 20 26 options: Image.DrawOptions,
+49 -15
src/Vaxis.zig
··· 717 717 return result; 718 718 } 719 719 720 - pub fn loadImage( 720 + pub fn transmitImage( 721 721 self: *Vaxis, 722 722 alloc: std.mem.Allocator, 723 723 tty: AnyWriter, 724 - src: Image.Source, 724 + img: *zigimg.Image, 725 + format: Image.TransmitFormat, 725 726 ) !Image { 726 727 if (!self.caps.kitty_graphics) return error.NoGraphicsCapability; 727 728 defer self.next_img_id += 1; 728 729 729 - var img = switch (src) { 730 - .path => |path| try zigimg.Image.fromFilePath(alloc, path), 731 - .mem => |bytes| try zigimg.Image.fromMemory(alloc, bytes), 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 + }, 732 747 }; 733 - defer img.deinit(); 734 - const png_buf = try alloc.alloc(u8, img.imageByteSize()); 735 - defer alloc.free(png_buf); 736 - const png = try img.writeToMemory(png_buf, .{ .png = .{} }); 737 - const b64_buf = try alloc.alloc(u8, base64Encoder.calcSize(png.len)); 738 - const encoded = base64Encoder.encode(b64_buf, png); 739 - defer alloc.free(b64_buf); 748 + 749 + const b64_buf = try arena.allocator().alloc(u8, base64Encoder.calcSize(buf.len)); 750 + const encoded = base64Encoder.encode(b64_buf, buf); 740 751 741 752 const id = self.next_img_id; 742 753 754 + const fmt: u8 = switch (format) { 755 + .rgb => 24, 756 + .rgba => 32, 757 + .png => 100, 758 + }; 759 + 743 760 if (encoded.len < 4096) { 744 761 try tty.print( 745 - "\x1b_Gf=100,i={d};{s}\x1b\\", 762 + "\x1b_Gf={d},i={d};{s}\x1b\\", 746 763 .{ 764 + fmt, 747 765 id, 748 766 encoded, 749 767 }, ··· 752 770 var n: usize = 4096; 753 771 754 772 try tty.print( 755 - "\x1b_Gf=100,i={d},m=1;{s}\x1b\\", 756 - .{ id, encoded[0..n] }, 773 + "\x1b_Gf={d},i={d},m=1;{s}\x1b\\", 774 + .{ fmt, id, encoded[0..n] }, 757 775 ); 758 776 while (n < encoded.len) : (n += 4096) { 759 777 const end: usize = @min(n + 4096, encoded.len); ··· 772 790 .width = img.width, 773 791 .height = img.height, 774 792 }; 793 + } 794 + 795 + pub fn loadImage( 796 + self: *Vaxis, 797 + alloc: std.mem.Allocator, 798 + tty: AnyWriter, 799 + src: Image.Source, 800 + ) !Image { 801 + if (!self.caps.kitty_graphics) return error.NoGraphicsCapability; 802 + 803 + var img = switch (src) { 804 + .path => |path| try zigimg.Image.fromFilePath(alloc, path), 805 + .mem => |bytes| try zigimg.Image.fromMemory(alloc, bytes), 806 + }; 807 + defer img.deinit(); 808 + return self.transmitImage(alloc, tty, &img, .png); 775 809 } 776 810 777 811 /// deletes an image from the terminal's memory