2021-05-01 14:52:16 +00:00
|
|
|
tga_encoder = {}
|
|
|
|
|
|
|
|
local image = setmetatable({}, {
|
|
|
|
__call = function(self, ...)
|
|
|
|
local t = setmetatable({}, {__index = self})
|
|
|
|
t:constructor(...)
|
|
|
|
return t
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2022-05-14 18:44:12 +00:00
|
|
|
function image:constructor(pixels, properties)
|
|
|
|
local properties = properties or {}
|
2022-05-14 20:41:52 +00:00
|
|
|
properties.colors = properties.colors or "RGB"
|
|
|
|
properties.pixel_depth = properties.pixel_depth or 16
|
2022-05-14 18:44:12 +00:00
|
|
|
|
2021-05-02 15:47:46 +00:00
|
|
|
self.data = ""
|
2021-05-01 14:52:16 +00:00
|
|
|
self.pixels = pixels
|
|
|
|
self.width = #pixels[1]
|
|
|
|
self.height = #pixels
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
self:encode(properties)
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function image:encode_colormap_spec()
|
2021-05-02 15:47:46 +00:00
|
|
|
self.data = self.data
|
|
|
|
.. string.char(0, 0) -- first entry index
|
|
|
|
.. string.char(0, 0) -- number of entries
|
|
|
|
.. string.char(0) -- bits per pixel
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_image_spec(properties)
|
|
|
|
local colors = properties.colors
|
|
|
|
local pixel_depth = properties.pixel_depth
|
2022-05-14 18:44:12 +00:00
|
|
|
assert(
|
2022-05-14 20:41:52 +00:00
|
|
|
"BW" == colors and 8 == pixel_depth or -- (8 bit grayscale = 1 byte = 8 bits)
|
|
|
|
"RGB" == colors and 16 == pixel_depth or -- (A1R5G5B5 = 2 bytes = 16 bits)
|
|
|
|
"RGB" == colors and 24 == pixel_depth -- (B8G8R8 = 3 bytes = 24 bits)
|
2022-05-14 18:44:12 +00:00
|
|
|
)
|
2021-05-02 15:47:46 +00:00
|
|
|
self.data = self.data
|
|
|
|
.. string.char(0, 0) -- X-origin
|
|
|
|
.. string.char(0, 0) -- Y-origin
|
|
|
|
.. string.char(self.width % 256, math.floor(self.width / 256)) -- width
|
|
|
|
.. string.char(self.height % 256, math.floor(self.height / 256)) -- height
|
2022-05-14 18:44:12 +00:00
|
|
|
.. string.char(pixel_depth)
|
2021-05-02 15:47:46 +00:00
|
|
|
.. string.char(0) -- image descriptor
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_header(properties)
|
|
|
|
local colors = properties.colors
|
|
|
|
local pixel_depth = properties.pixel_depth
|
|
|
|
local image_type
|
|
|
|
if "BW" == colors and 8 == pixel_depth then
|
|
|
|
image_type = 3 -- grayscale
|
|
|
|
elseif (
|
|
|
|
"RGB" == colors and 16 == pixel_depth or
|
|
|
|
"RGB" == colors and 24 == pixel_depth
|
|
|
|
) then
|
|
|
|
image_type = 10 -- RLE RGB
|
|
|
|
end
|
2021-05-02 15:47:46 +00:00
|
|
|
self.data = self.data
|
|
|
|
.. string.char(0) -- image id
|
|
|
|
.. string.char(0) -- color map type
|
2022-05-14 20:41:52 +00:00
|
|
|
.. string.char(image_type)
|
2021-05-02 15:47:46 +00:00
|
|
|
self:encode_colormap_spec() -- color map specification
|
2022-05-14 20:41:52 +00:00
|
|
|
self:encode_image_spec(properties) -- image specification
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_data(properties)
|
|
|
|
local colors = properties.colors
|
|
|
|
local pixel_depth = properties.pixel_depth
|
|
|
|
if "BW" == colors and 8 == pixel_depth then
|
|
|
|
self:encode_data_bw8()
|
|
|
|
elseif "RGB" == colors and 16 == pixel_depth then
|
2022-05-14 18:44:12 +00:00
|
|
|
self:encode_data_a1r5g5b5_rle()
|
2022-05-14 20:41:52 +00:00
|
|
|
elseif "RGB" == colors and 24 == pixel_depth then
|
2022-05-14 18:44:12 +00:00
|
|
|
self:encode_data_r8g8b8_rle()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_data_bw8()
|
|
|
|
local raw_pixels = {}
|
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
|
|
|
local raw_pixel = string.char(pixel[1])
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(raw_pixels)
|
|
|
|
end
|
|
|
|
|
2022-05-14 18:44:12 +00:00
|
|
|
function image:encode_data_a1r5g5b5_rle()
|
2021-11-05 17:58:01 +00:00
|
|
|
local colorword = nil
|
|
|
|
local previous_r = nil
|
|
|
|
local previous_g = nil
|
|
|
|
local previous_b = nil
|
2022-02-09 12:40:47 +00:00
|
|
|
local raw_pixel = ''
|
|
|
|
local raw_pixels = {}
|
2021-11-04 00:53:58 +00:00
|
|
|
local count = 1
|
2021-11-04 14:15:28 +00:00
|
|
|
local packets = {}
|
2022-02-09 12:40:47 +00:00
|
|
|
local raw_packet = ''
|
2021-11-04 00:53:58 +00:00
|
|
|
local rle_packet = ''
|
2021-11-05 17:58:01 +00:00
|
|
|
-- Sample depth rescaling is done according to the algorithm presented in:
|
|
|
|
-- <https://www.w3.org/TR/2003/REC-PNG-20031110/#13Sample-depth-rescaling>
|
|
|
|
local max_sample_in = math.pow(2, 8) - 1
|
|
|
|
local max_sample_out = math.pow(2, 5) - 1
|
2021-05-01 14:52:16 +00:00
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
2021-11-05 17:58:01 +00:00
|
|
|
if pixel[1] ~= previous_r or pixel[2] ~= previous_g or pixel[3] ~= previous_b or count == 128 then
|
2022-02-09 12:40:47 +00:00
|
|
|
if nil ~= previous_r then
|
2021-11-05 17:58:01 +00:00
|
|
|
colorword = 32768 +
|
|
|
|
((math.floor((previous_r * max_sample_out / max_sample_in) + 0.5)) * 1024) +
|
|
|
|
((math.floor((previous_g * max_sample_out / max_sample_in) + 0.5)) * 32) +
|
|
|
|
((math.floor((previous_b * max_sample_out / max_sample_in) + 0.5)) * 1)
|
2022-02-09 12:40:47 +00:00
|
|
|
if 1 == count then
|
|
|
|
-- remember pixel verbatim for raw encoding
|
|
|
|
raw_pixel = string.char(colorword % 256, math.floor(colorword / 256))
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
if 128 == #raw_pixels then
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- encode raw pixels, if any
|
|
|
|
if #raw_pixels > 0 then
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
end
|
|
|
|
-- RLE encoding
|
|
|
|
rle_packet = string.char(128 + count - 1, colorword % 256, math.floor(colorword / 256))
|
|
|
|
packets[#packets +1] = rle_packet
|
|
|
|
end
|
2021-11-05 17:58:01 +00:00
|
|
|
end
|
2021-11-04 00:53:58 +00:00
|
|
|
count = 1
|
2021-11-05 17:58:01 +00:00
|
|
|
previous_r = pixel[1]
|
|
|
|
previous_g = pixel[2]
|
|
|
|
previous_b = pixel[3]
|
2021-11-04 00:53:58 +00:00
|
|
|
else
|
|
|
|
count = count + 1
|
|
|
|
end
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
end
|
2021-11-05 17:58:01 +00:00
|
|
|
colorword = 32768 +
|
|
|
|
((math.floor((previous_r * max_sample_out / max_sample_in) + 0.5)) * 1024) +
|
|
|
|
((math.floor((previous_g * max_sample_out / max_sample_in) + 0.5)) * 32) +
|
|
|
|
((math.floor((previous_b * max_sample_out / max_sample_in) + 0.5)) * 1)
|
2022-02-09 12:40:47 +00:00
|
|
|
if 1 == count then
|
|
|
|
raw_pixel = string.char(colorword % 256, math.floor(colorword / 256))
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
else
|
|
|
|
-- encode raw pixels, if any
|
|
|
|
if #raw_pixels > 0 then
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
end
|
|
|
|
-- RLE encoding
|
|
|
|
rle_packet = string.char(128 + count - 1, colorword % 256, math.floor(colorword / 256))
|
|
|
|
packets[#packets +1] = rle_packet
|
|
|
|
end
|
2021-11-04 14:15:28 +00:00
|
|
|
self.data = self.data .. table.concat(packets)
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 18:44:12 +00:00
|
|
|
function image:encode_data_r8g8b8_rle()
|
|
|
|
local previous_r = nil
|
|
|
|
local previous_g = nil
|
|
|
|
local previous_b = nil
|
|
|
|
local raw_pixel = ''
|
|
|
|
local raw_pixels = {}
|
|
|
|
local count = 1
|
|
|
|
local packets = {}
|
|
|
|
local raw_packet = ''
|
|
|
|
local rle_packet = ''
|
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
|
|
|
if pixel[1] ~= previous_r or pixel[2] ~= previous_g or pixel[3] ~= previous_b or count == 128 then
|
|
|
|
if nil ~= previous_r then
|
|
|
|
if 1 == count then
|
|
|
|
-- remember pixel verbatim for raw encoding
|
|
|
|
raw_pixel = string.char(previous_b, previous_g, previous_r)
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
if 128 == #raw_pixels then
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- encode raw pixels, if any
|
|
|
|
if #raw_pixels > 0 then
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
end
|
|
|
|
-- RLE encoding
|
|
|
|
rle_packet = string.char(128 + count - 1, previous_b, previous_g, previous_r)
|
|
|
|
packets[#packets +1] = rle_packet
|
|
|
|
end
|
|
|
|
end
|
|
|
|
count = 1
|
|
|
|
previous_r = pixel[1]
|
|
|
|
previous_g = pixel[2]
|
|
|
|
previous_b = pixel[3]
|
|
|
|
else
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if 1 == count then
|
|
|
|
raw_pixel = string.char(previous_b, previous_g, previous_r)
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
else
|
|
|
|
-- encode raw pixels, if any
|
|
|
|
if #raw_pixels > 0 then
|
|
|
|
raw_packet = string.char(#raw_pixels - 1)
|
|
|
|
packets[#packets + 1] = raw_packet
|
|
|
|
for i=1, #raw_pixels do
|
|
|
|
packets[#packets +1] = raw_pixels[i]
|
|
|
|
end
|
|
|
|
raw_pixels = {}
|
|
|
|
end
|
|
|
|
-- RLE encoding
|
|
|
|
rle_packet = string.char(128 + count - 1, previous_b, previous_g, previous_r)
|
|
|
|
packets[#packets +1] = rle_packet
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(packets)
|
|
|
|
end
|
|
|
|
|
2021-05-02 15:47:46 +00:00
|
|
|
function image:encode_footer()
|
|
|
|
self.data = self.data
|
|
|
|
.. string.char(0, 0, 0, 0) -- extension area offset
|
|
|
|
.. string.char(0, 0, 0, 0) -- developer area offset
|
|
|
|
.. "TRUEVISION-XFILE"
|
|
|
|
.. "."
|
|
|
|
.. string.char(0)
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode(properties)
|
|
|
|
self:encode_header(properties) -- header
|
2021-05-02 15:47:46 +00:00
|
|
|
-- no color map and image id data
|
2022-05-14 20:41:52 +00:00
|
|
|
self:encode_data(properties) -- encode data
|
2021-05-02 15:47:46 +00:00
|
|
|
-- no extension or developer area
|
|
|
|
self:encode_footer() -- footer
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function image:save(filename)
|
2022-01-30 19:33:06 +00:00
|
|
|
local f = assert(io.open(filename, "wb"))
|
2021-05-01 14:52:16 +00:00
|
|
|
f:write(self.data)
|
|
|
|
f:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
tga_encoder.image = image
|