From 52e5c955f0f9d745a23b057de5e48a8e2ad456e8 Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Thu, 4 Nov 2021 01:53:58 +0100 Subject: [PATCH] Use RLE compression in tga_encoder --- init.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/init.lua b/init.lua index 96afda5e1..973e44855 100644 --- a/init.lua +++ b/init.lua @@ -38,18 +38,31 @@ function image:encode_header() self.data = self.data .. string.char(0) -- image id .. string.char(0) -- color map type - .. string.char(2) -- image type (uncompressed true-color image = 2) + .. string.char(10) -- image type (RLE RGB = 10) self:encode_colormap_spec() -- color map specification self:encode_image_spec() -- image specification end function image:encode_data() + local current_pixel = '' + local previous_pixel = '' + local count = 1 + local encoded = '' + local rle_packet = '' for _, row in ipairs(self.pixels) do for _, pixel in ipairs(row) do - self.data = self.data - .. string.char(pixel[3], pixel[2], pixel[1]) + current_pixel = string.char(pixel[3], pixel[2], pixel[1]) + if current_pixel ~= previous_pixel or count == 128 then + encoded = encoded .. rle_packet + count = 1 + previous_pixel = current_pixel + else + count = count + 1 + end + rle_packet = string.char(128 + count - 1) .. current_pixel end end + self.data = self.data .. encoded .. rle_packet end function image:encode_footer()