1
0
Fork 0

Allow RAW or RLE encoding for true-color images

patch_skins
Nils Dagsson Moskopp 2022-05-15 15:42:48 +02:00
parent 9bd1702d60
commit 376b6404b2
No known key found for this signature in database
GPG Key ID: A3BC671C35191080
2 changed files with 56 additions and 8 deletions

View File

@ -39,7 +39,7 @@ for x = 1,6,1 do -- left to right
pixels[z][x] = color pixels[z][x] = color
end end
end end
tga_encoder.image(pixels):save("gradients_8bpp.tga", {colors="BW", pixel_depth=8}) tga_encoder.image(pixels):save("gradients_8bpp_raw.tga", {colors="BW", compression="RAW", pixel_depth=8})
local pixels = {} local pixels = {}
for x = 1,16,1 do -- left to right for x = 1,16,1 do -- left to right
@ -59,8 +59,10 @@ for x = 1,16,1 do -- left to right
end end
end end
local gradients = tga_encoder.image(pixels) local gradients = tga_encoder.image(pixels)
gradients:save("gradients_16bpp.tga", {colors="RGB", pixel_depth=16}) gradients:save("gradients_16bpp_raw.tga", {colors="RGB", compression="RAW", pixel_depth=16})
gradients:save("gradients_24bpp.tga", {colors="RGB", pixel_depth=24}) gradients:save("gradients_16bpp_rle.tga", {colors="RGB", compression="RLE", pixel_depth=16})
gradients:save("gradients_24bpp_raw.tga", {colors="RGB", compression="RAW", pixel_depth=24})
gradients:save("gradients_24bpp_rle.tga", {colors="RGB", compression="RLE", pixel_depth=24})
local pixels = {} local pixels = {}
for x = 1,512,1 do -- left to right for x = 1,512,1 do -- left to right

View File

@ -40,16 +40,21 @@ end
function image:encode_header(properties) function image:encode_header(properties)
local colors = properties.colors local colors = properties.colors
local compression = properties.compression
local pixel_depth = properties.pixel_depth local pixel_depth = properties.pixel_depth
local image_type local image_type
if "BW" == colors and 8 == pixel_depth then if "BW" == colors and "RAW" == compression and 8 == pixel_depth then
image_type = 3 -- grayscale image_type = 3 -- grayscale
elseif ( elseif (
"RGB" == colors and 16 == pixel_depth or "RGB" == colors and 16 == pixel_depth or
"RGB" == colors and 24 == pixel_depth "RGB" == colors and 24 == pixel_depth
) then ) then
if "RAW" == compression then
image_type = 2 -- RAW RGB
elseif "RLE" == compression then
image_type = 10 -- RLE RGB image_type = 10 -- RLE RGB
end end
end
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
@ -60,15 +65,25 @@ end
function image:encode_data(properties) function image:encode_data(properties)
local colors = properties.colors local colors = properties.colors
local compression = properties.compression
local pixel_depth = properties.pixel_depth local pixel_depth = properties.pixel_depth
if "BW" == colors and 8 == pixel_depth then
if "BW" == colors and "RAW" == compression and 8 == pixel_depth then
self:encode_data_bw8() self:encode_data_bw8()
elseif "RGB" == colors and 16 == pixel_depth then elseif "RGB" == colors and 16 == pixel_depth then
if "RAW" == compression then
self:encode_data_a1r5g5b5_raw()
elseif "RLE" == compression then
self:encode_data_a1r5g5b5_rle() self:encode_data_a1r5g5b5_rle()
end
elseif "RGB" == colors and 24 == pixel_depth then elseif "RGB" == colors and 24 == pixel_depth then
if "RAW" == compression then
self:encode_data_r8g8b8_raw()
elseif "RLE" == compression then
self:encode_data_r8g8b8_rle() self:encode_data_r8g8b8_rle()
end end
end end
end
function image:encode_data_bw8() function image:encode_data_bw8()
local raw_pixels = {} local raw_pixels = {}
@ -81,6 +96,25 @@ function image:encode_data_bw8()
self.data = self.data .. table.concat(raw_pixels) self.data = self.data .. table.concat(raw_pixels)
end end
function image:encode_data_a1r5g5b5_raw()
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
function image:encode_data_a1r5g5b5_rle() function image:encode_data_a1r5g5b5_rle()
local colorword = nil local colorword = nil
local previous_r = nil local previous_r = nil
@ -170,6 +204,17 @@ function image:encode_data_a1r5g5b5_rle()
self.data = self.data .. table.concat(packets) self.data = self.data .. table.concat(packets)
end end
function image:encode_data_r8g8b8_raw()
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
function image:encode_data_r8g8b8_rle() function image:encode_data_r8g8b8_rle()
local previous_r = nil local previous_r = nil
local previous_g = nil local previous_g = nil
@ -267,6 +312,7 @@ end
function image:save(filename, properties) function image:save(filename, properties)
local properties = properties or {} local properties = properties or {}
properties.colors = properties.colors or "RGB" properties.colors = properties.colors or "RGB"
properties.compression = properties.compression or "RLE"
properties.pixel_depth = properties.pixel_depth or 16 properties.pixel_depth = properties.pixel_depth or 16
self:encode(properties) self:encode(properties)