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-15 12:54:28 +00:00
|
|
|
function image:constructor(pixels)
|
2021-05-01 14:52:16 +00:00
|
|
|
self.pixels = pixels
|
|
|
|
self.width = #pixels[1]
|
|
|
|
self.height = #pixels
|
|
|
|
end
|
|
|
|
|
2022-05-16 15:01:38 +00:00
|
|
|
function image:encode_colormap_spec(properties)
|
|
|
|
local colormap_spec
|
|
|
|
if nil ~= properties.colormap then
|
|
|
|
local color_count = #properties.colormap
|
|
|
|
colormap_spec =
|
|
|
|
string.char(0, 0) .. -- first entry index
|
|
|
|
string.char(color_count % 256, math.floor(color_count / 256)) .. -- number of entries
|
|
|
|
string.char(#properties.colormap[1] * 8) -- bits per pixel
|
|
|
|
else -- no colormap
|
|
|
|
colormap_spec =
|
|
|
|
string.char(0, 0) .. -- first entry index
|
|
|
|
string.char(0, 0) .. -- number of entries
|
|
|
|
string.char(0) -- bits per pixel
|
|
|
|
end
|
|
|
|
self.data = self.data .. colormap_spec
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_image_spec(properties)
|
2022-05-16 00:48:51 +00:00
|
|
|
local color_format = properties.color_format
|
2022-05-14 18:44:12 +00:00
|
|
|
assert(
|
2022-05-16 00:48:51 +00:00
|
|
|
"Y8" == color_format or -- (8 bit grayscale = 1 byte = 8 bits)
|
|
|
|
"A1R5G5B5" == color_format or -- (A1R5G5B5 = 2 bytes = 16 bits)
|
|
|
|
"B8G8R8" == color_format or -- (B8G8R8 = 3 bytes = 24 bits)
|
|
|
|
"B8G8R8A8" == color_format -- (B8G8R8A8 = 4 bytes = 32 bits)
|
2022-05-14 18:44:12 +00:00
|
|
|
)
|
2022-05-16 00:48:51 +00:00
|
|
|
pixel_depth_by_color_format = {
|
|
|
|
["Y8"] = 8,
|
|
|
|
["A1R5G5B5"] = 16,
|
|
|
|
["B8G8R8"] = 24,
|
|
|
|
["B8G8R8A8"] = 32,
|
|
|
|
}
|
2022-05-16 15:01:38 +00:00
|
|
|
local pixel_depth
|
|
|
|
if nil ~= properties.colormap then
|
|
|
|
pixel_depth = self.pixel_depth
|
|
|
|
else
|
|
|
|
pixel_depth = pixel_depth_by_color_format[color_format]
|
|
|
|
end
|
2022-05-16 00:48:51 +00:00
|
|
|
assert( nil ~= pixel_depth)
|
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-16 15:01:38 +00:00
|
|
|
function image:encode_colormap(properties)
|
|
|
|
local colormap = properties.colormap
|
|
|
|
if nil == colormap then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local colormap_pixel_depth = #colormap[1] * 8
|
|
|
|
assert( 24 == colormap_pixel_depth )
|
|
|
|
local colors = {}
|
|
|
|
if 24 == colormap_pixel_depth then
|
|
|
|
for i = 1,#colormap,1 do
|
|
|
|
local color = colormap[i]
|
|
|
|
local color_bytes = string.char(
|
|
|
|
color[3], -- B
|
|
|
|
color[2], -- G
|
|
|
|
color[1] -- R
|
|
|
|
)
|
|
|
|
colors[#colors + 1] = color_bytes
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(colors)
|
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_header(properties)
|
2022-05-16 00:48:51 +00:00
|
|
|
local color_format = properties.color_format
|
2022-05-16 15:01:38 +00:00
|
|
|
local colormap = properties.colormap
|
2022-05-15 13:42:48 +00:00
|
|
|
local compression = properties.compression
|
2022-05-14 20:41:52 +00:00
|
|
|
local image_type
|
2022-05-16 00:48:51 +00:00
|
|
|
if "Y8" == color_format and "RAW" == compression then
|
2022-05-14 20:41:52 +00:00
|
|
|
image_type = 3 -- grayscale
|
|
|
|
elseif (
|
2022-05-16 00:48:51 +00:00
|
|
|
"A1R5G5B5" == color_format or
|
|
|
|
"B8G8R8" == color_format or
|
|
|
|
"B8G8R8A8" == color_format
|
2022-05-14 20:41:52 +00:00
|
|
|
) then
|
2022-05-15 13:42:48 +00:00
|
|
|
if "RAW" == compression then
|
2022-05-16 15:01:38 +00:00
|
|
|
if nil ~= colormap then
|
|
|
|
image_type = 1 -- colormapped RGB(A)
|
|
|
|
else
|
|
|
|
image_type = 2 -- RAW RGB(A)
|
|
|
|
end
|
2022-05-15 13:42:48 +00:00
|
|
|
elseif "RLE" == compression then
|
|
|
|
image_type = 10 -- RLE RGB
|
|
|
|
end
|
2022-05-14 20:41:52 +00:00
|
|
|
end
|
2022-05-16 00:48:51 +00:00
|
|
|
assert( nil ~= image_type )
|
2022-05-16 15:01:38 +00:00
|
|
|
local colormap_type = 0
|
|
|
|
if nil ~= colormap then
|
|
|
|
colormap_type = 1
|
|
|
|
end
|
2021-05-02 15:47:46 +00:00
|
|
|
self.data = self.data
|
|
|
|
.. string.char(0) -- image id
|
2022-05-16 15:01:38 +00:00
|
|
|
.. string.char(colormap_type)
|
2022-05-14 20:41:52 +00:00
|
|
|
.. string.char(image_type)
|
2022-05-16 15:01:38 +00:00
|
|
|
self:encode_colormap_spec(properties) -- color map specification
|
2022-05-14 20:41:52 +00:00
|
|
|
self:encode_image_spec(properties) -- image specification
|
2022-05-16 15:01:38 +00:00
|
|
|
self:encode_colormap(properties)
|
2021-05-01 14:52:16 +00:00
|
|
|
end
|
|
|
|
|
2022-05-14 20:41:52 +00:00
|
|
|
function image:encode_data(properties)
|
2022-05-16 00:48:51 +00:00
|
|
|
local color_format = properties.color_format
|
2022-05-16 15:01:38 +00:00
|
|
|
local colormap = properties.colormap
|
2022-05-15 13:42:48 +00:00
|
|
|
local compression = properties.compression
|
|
|
|
|
2022-05-16 01:21:52 +00:00
|
|
|
local data_length_before = #self.data
|
2022-05-16 00:48:51 +00:00
|
|
|
if "Y8" == color_format and "RAW" == compression then
|
2022-05-15 23:54:21 +00:00
|
|
|
if 8 == self.pixel_depth then
|
2022-05-16 00:48:51 +00:00
|
|
|
self:encode_data_Y8_as_Y8_raw()
|
2022-05-15 23:54:21 +00:00
|
|
|
elseif 24 == self.pixel_depth then
|
2022-05-16 00:48:51 +00:00
|
|
|
self:encode_data_R8G8B8_as_Y8_raw()
|
2022-05-15 17:22:56 +00:00
|
|
|
end
|
2022-05-16 00:48:51 +00:00
|
|
|
elseif "A1R5G5B5" == color_format then
|
2022-05-15 13:42:48 +00:00
|
|
|
if "RAW" == compression then
|
2022-05-16 00:48:51 +00:00
|
|
|
self:encode_data_R8G8B8_as_A1R5G5B5_raw()
|
2022-05-15 13:42:48 +00:00
|
|
|
elseif "RLE" == compression then
|
2022-05-16 00:48:51 +00:00
|
|
|
self:encode_data_R8G8B8_as_A1R5G5B5_rle()
|
2022-05-15 13:42:48 +00:00
|
|
|
end
|
2022-05-16 00:48:51 +00:00
|
|
|
elseif "B8G8R8" == color_format then
|
2022-05-16 15:01:38 +00:00
|
|
|
if nil ~= colormap then
|
|
|
|
if "RAW" == compression then
|
|
|
|
if 8 == self.pixel_depth then
|
|
|
|
self:encode_data_Y8_as_Y8_raw()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if "RAW" == compression then
|
|
|
|
self:encode_data_R8G8B8_as_B8G8R8_raw()
|
|
|
|
elseif "RLE" == compression then
|
|
|
|
self:encode_data_R8G8B8_as_B8G8R8_rle()
|
|
|
|
end
|
2022-05-15 13:42:48 +00:00
|
|
|
end
|
2022-05-16 00:48:51 +00:00
|
|
|
elseif "B8G8R8A8" == color_format then
|
2022-05-15 23:39:33 +00:00
|
|
|
if "RAW" == compression then
|
2022-05-16 00:48:51 +00:00
|
|
|
self:encode_data_R8G8B8A8_as_B8G8R8A8_raw()
|
2022-05-16 11:16:23 +00:00
|
|
|
elseif "RLE" == compression then
|
|
|
|
self:encode_data_R8G8B8A8_as_B8G8R8A8_rle()
|
2022-05-15 23:39:33 +00:00
|
|
|
end
|
2022-05-14 18:44:12 +00:00
|
|
|
end
|
2022-05-16 01:21:52 +00:00
|
|
|
local data_length_after = #self.data
|
|
|
|
assert(
|
|
|
|
data_length_after ~= data_length_before,
|
|
|
|
"No data encoded for color format: " .. color_format
|
|
|
|
)
|
2022-05-14 18:44:12 +00:00
|
|
|
end
|
|
|
|
|
2022-05-16 00:48:51 +00:00
|
|
|
function image:encode_data_Y8_as_Y8_raw()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(8 == self.pixel_depth)
|
2022-05-14 20:41:52 +00:00
|
|
|
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-16 00:48:51 +00:00
|
|
|
function image:encode_data_R8G8B8_as_Y8_raw()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(24 == self.pixel_depth)
|
2022-05-15 17:22:56 +00:00
|
|
|
local raw_pixels = {}
|
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
2022-05-15 17:54:53 +00:00
|
|
|
-- the HSP RGB to brightness formula is
|
|
|
|
-- sqrt( 0.299 r² + .587 g² + .114 b² )
|
|
|
|
-- see <https://alienryderflex.com/hsp.html>
|
2022-05-15 17:22:56 +00:00
|
|
|
local gray = math.floor(
|
2022-05-15 17:54:53 +00:00
|
|
|
math.sqrt(
|
|
|
|
0.299 * pixel[1]^2 +
|
|
|
|
0.587 * pixel[2]^2 +
|
|
|
|
0.114 * pixel[3]^2
|
|
|
|
) + 0.5
|
2022-05-15 17:22:56 +00:00
|
|
|
)
|
|
|
|
local raw_pixel = string.char(gray)
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(raw_pixels)
|
|
|
|
end
|
|
|
|
|
2022-05-16 00:48:51 +00:00
|
|
|
function image:encode_data_R8G8B8_as_A1R5G5B5_raw()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(24 == self.pixel_depth)
|
2022-05-15 13:42:48 +00:00
|
|
|
local raw_pixels = {}
|
|
|
|
-- 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
|
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
|
|
|
local colorword = 32768 +
|
|
|
|
((math.floor((pixel[1] * max_sample_out / max_sample_in) + 0.5)) * 1024) +
|
|
|
|
((math.floor((pixel[2] * max_sample_out / max_sample_in) + 0.5)) * 32) +
|
|
|
|
((math.floor((pixel[3] * max_sample_out / max_sample_in) + 0.5)) * 1)
|
|
|
|
local raw_pixel = string.char(colorword % 256, math.floor(colorword / 256))
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(raw_pixels)
|
|
|
|
end
|
|
|
|
|
2022-05-16 00:48:51 +00:00
|
|
|
function image:encode_data_R8G8B8_as_A1R5G5B5_rle()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(24 == self.pixel_depth)
|
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-16 00:48:51 +00:00
|
|
|
function image:encode_data_R8G8B8_as_B8G8R8_raw()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(24 == self.pixel_depth)
|
2022-05-15 13:42:48 +00:00
|
|
|
local raw_pixels = {}
|
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
|
|
|
local raw_pixel = string.char(pixel[3], pixel[2], pixel[1])
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(raw_pixels)
|
|
|
|
end
|
|
|
|
|
2022-05-16 00:48:51 +00:00
|
|
|
function image:encode_data_R8G8B8_as_B8G8R8_rle()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(24 == self.pixel_depth)
|
2022-05-14 18:44:12 +00:00
|
|
|
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
|
|
|
|
|
2022-05-16 00:48:51 +00:00
|
|
|
function image:encode_data_R8G8B8A8_as_B8G8R8A8_raw()
|
2022-05-15 23:54:21 +00:00
|
|
|
assert(32 == self.pixel_depth)
|
2022-05-15 23:39:33 +00:00
|
|
|
local raw_pixels = {}
|
|
|
|
for _, row in ipairs(self.pixels) do
|
|
|
|
for _, pixel in ipairs(row) do
|
|
|
|
local raw_pixel = string.char(pixel[3], pixel[2], pixel[1], pixel[4])
|
|
|
|
raw_pixels[#raw_pixels + 1] = raw_pixel
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.data = self.data .. table.concat(raw_pixels)
|
|
|
|
end
|
|
|
|
|
2022-05-16 11:16:23 +00:00
|
|
|
function image:encode_data_R8G8B8A8_as_B8G8R8A8_rle()
|
|
|
|
assert(32 == self.pixel_depth)
|
|
|
|
local previous_r = nil
|
|
|
|
local previous_g = nil
|
|
|
|
local previous_b = nil
|
|
|
|
local previous_a = 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 pixel[4] ~= previous_a 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, previous_a)
|
|
|
|
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, previous_a)
|
|
|
|
packets[#packets +1] = rle_packet
|
|
|
|
end
|
|
|
|
end
|
|
|
|
count = 1
|
|
|
|
previous_r = pixel[1]
|
|
|
|
previous_g = pixel[2]
|
|
|
|
previous_b = pixel[3]
|
|
|
|
previous_a = pixel[4]
|
|
|
|
else
|
|
|
|
count = count + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if 1 == count then
|
|
|
|
raw_pixel = string.char(previous_b, previous_g, previous_r, previous_a)
|
|
|
|
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, previous_a)
|
|
|
|
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)
|
2022-05-15 12:54:28 +00:00
|
|
|
self.data = ""
|
2022-05-14 20:41:52 +00:00
|
|
|
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
|
|
|
|
|
2022-05-15 12:54:28 +00:00
|
|
|
function image:save(filename, properties)
|
|
|
|
local properties = properties or {}
|
2022-05-15 17:29:05 +00:00
|
|
|
properties.compression = properties.compression or "RAW"
|
2022-05-16 00:48:51 +00:00
|
|
|
|
|
|
|
self.pixel_depth = #self.pixels[1][1] * 8
|
2022-05-16 15:01:38 +00:00
|
|
|
|
|
|
|
local color_format_defaults_by_pixel_depth = {
|
|
|
|
[8] = "Y8",
|
|
|
|
[24] = "B8G8R8",
|
|
|
|
[32] = "B8G8R8A8",
|
|
|
|
}
|
2022-05-16 00:48:51 +00:00
|
|
|
if nil == properties.color_format then
|
2022-05-16 15:01:38 +00:00
|
|
|
if nil ~= properties.colormap then
|
|
|
|
properties.color_format =
|
|
|
|
color_format_defaults_by_pixel_depth[
|
|
|
|
#properties.colormap[1] * 8
|
|
|
|
]
|
|
|
|
else
|
|
|
|
properties.color_format =
|
|
|
|
color_format_defaults_by_pixel_depth[
|
|
|
|
self.pixel_depth
|
|
|
|
]
|
2022-05-16 00:48:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
assert( nil ~= properties.color_format )
|
2022-05-15 12:54:28 +00:00
|
|
|
|
|
|
|
self:encode(properties)
|
|
|
|
|
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
|