Use RLE compression in tga_encoder

This commit is contained in:
Nils Dagsson Moskopp 2021-11-04 01:53:58 +01:00
parent b88579b52e
commit 52e5c955f0
Signed by untrusted user who does not match committer: erle
GPG Key ID: A3BC671C35191080
1 changed files with 16 additions and 3 deletions

View File

@ -38,18 +38,31 @@ function image:encode_header()
self.data = self.data self.data = self.data
.. string.char(0) -- image id .. string.char(0) -- image id
.. string.char(0) -- color map type .. 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_colormap_spec() -- color map specification
self:encode_image_spec() -- image specification self:encode_image_spec() -- image specification
end end
function image:encode_data() 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 _, row in ipairs(self.pixels) do
for _, pixel in ipairs(row) do for _, pixel in ipairs(row) do
self.data = self.data current_pixel = string.char(pixel[3], pixel[2], pixel[1])
.. 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
end end
self.data = self.data .. encoded .. rle_packet
end end
function image:encode_footer() function image:encode_footer()