this repo has no description
13
fork

Configure Feed

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

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).

+50 -38
+50 -38
src/Vaxis.zig
··· 717 717 return result; 718 718 } 719 719 720 - pub fn transmitImage( 720 + /// Transmit an image which has been pre-base64 encoded 721 + pub fn transmitPreEncodedImage( 721 722 self: *Vaxis, 722 - alloc: std.mem.Allocator, 723 723 tty: AnyWriter, 724 - img: *zigimg.Image, 724 + bytes: []const u8, 725 + width: usize, 726 + height: usize, 725 727 format: Image.TransmitFormat, 726 728 ) !Image { 727 - if (!self.caps.kitty_graphics) return error.NoGraphicsCapability; 728 729 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 - 752 730 const id = self.next_img_id; 753 731 754 732 const fmt: u8 = switch (format) { ··· 757 735 .png => 100, 758 736 }; 759 737 760 - if (encoded.len < 4096) { 738 + if (bytes.len < 4096) { 761 739 try tty.print( 762 740 "\x1b_Gf={d},s={d},v={d},i={d};{s}\x1b\\", 763 741 .{ 764 742 fmt, 765 - img.width, 766 - img.height, 743 + width, 744 + height, 767 745 id, 768 - encoded, 746 + bytes, 769 747 }, 770 748 ); 771 749 } else { ··· 773 751 774 752 try tty.print( 775 753 "\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] }, 777 755 ); 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; 781 759 try tty.print( 782 760 "\x1b_Gm={d};{s}\x1b\\", 783 761 .{ 784 762 m, 785 - encoded[n..end], 763 + bytes[n..end], 786 764 }, 787 765 ); 788 766 } 789 767 } 790 768 return .{ 791 769 .id = id, 792 - .width = img.width, 793 - .height = img.height, 770 + .width = width, 771 + .height = height, 794 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 + }, 801 + }; 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); 795 807 } 796 808 797 809 pub fn loadImage(