From 3f60b5baa67934674edebd9add85e474f122e32b Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Thu, 4 Nov 2021 15:15:28 +0100 Subject: [PATCH] Speed up TGA encoding by creating fewer strings --- init.lua | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 973e44855..39309c9c9 100644 --- a/init.lua +++ b/init.lua @@ -47,13 +47,13 @@ function image:encode_data() local current_pixel = '' local previous_pixel = '' local count = 1 - local encoded = '' + local packets = {} local rle_packet = '' for _, row in ipairs(self.pixels) do for _, pixel in ipairs(row) do current_pixel = string.char(pixel[3], pixel[2], pixel[1]) if current_pixel ~= previous_pixel or count == 128 then - encoded = encoded .. rle_packet + packets[#packets +1] = rle_packet count = 1 previous_pixel = current_pixel else @@ -62,7 +62,8 @@ function image:encode_data() rle_packet = string.char(128 + count - 1) .. current_pixel end end - self.data = self.data .. encoded .. rle_packet + packets[#packets +1] = rle_packet + self.data = self.data .. table.concat(packets) end function image:encode_footer()