Allow saving R8G8B8 images as BW8

This commit is contained in:
Nils Dagsson Moskopp 2022-05-15 19:22:56 +02:00
parent 376b6404b2
commit 759b0a188f
Signed by untrusted user who does not match committer: erle
GPG Key ID: A3BC671C35191080
2 changed files with 34 additions and 3 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_raw.tga", {colors="BW", compression="RAW", pixel_depth=8}) tga_encoder.image(pixels):save("gradient_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,6 +59,7 @@ 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_8bpp_raw.tga", {colors="BW", compression="RAW", pixel_depth=8})
gradients:save("gradients_16bpp_raw.tga", {colors="RGB", compression="RAW", pixel_depth=16}) gradients:save("gradients_16bpp_raw.tga", {colors="RGB", compression="RAW", pixel_depth=16})
gradients:save("gradients_16bpp_rle.tga", {colors="RGB", compression="RLE", pixel_depth=16}) 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_raw.tga", {colors="RGB", compression="RAW", pixel_depth=24})
@ -84,4 +85,6 @@ for x = 1,512,1 do -- left to right
pixels[z][x] = color pixels[z][x] = color
end end
end end
tga_encoder.image(pixels):save("fractal_8bpp.tga", {colors="BW", pixel_depth=8})
tga_encoder.image(pixels):save("fractal_16bpp.tga", {colors="RGB", pixel_depth=16})
tga_encoder.image(pixels):save("fractal_24bpp.tga", {colors="RGB", pixel_depth=24}) tga_encoder.image(pixels):save("fractal_24bpp.tga", {colors="RGB", pixel_depth=24})

View File

@ -12,6 +12,7 @@ function image:constructor(pixels)
self.pixels = pixels self.pixels = pixels
self.width = #pixels[1] self.width = #pixels[1]
self.height = #pixels self.height = #pixels
self.pixel_depth = #pixels[1][1]
end end
function image:encode_colormap_spec() function image:encode_colormap_spec()
@ -69,7 +70,11 @@ function image:encode_data(properties)
local pixel_depth = properties.pixel_depth local pixel_depth = properties.pixel_depth
if "BW" == colors and "RAW" == compression and 8 == pixel_depth then if "BW" == colors and "RAW" == compression and 8 == pixel_depth then
self:encode_data_bw8() if 1 == self.pixel_depth then
self:encode_data_bw8_to_bw8_raw()
elseif 3 == self.pixel_depth then
self:encode_data_r8g8b8_to_bw8_raw()
end
elseif "RGB" == colors and 16 == pixel_depth then elseif "RGB" == colors and 16 == pixel_depth then
if "RAW" == compression then if "RAW" == compression then
self:encode_data_a1r5g5b5_raw() self:encode_data_a1r5g5b5_raw()
@ -85,7 +90,8 @@ function image:encode_data(properties)
end end
end end
function image:encode_data_bw8() function image:encode_data_bw8_to_bw8_raw()
assert(1 == self.pixel_depth)
local raw_pixels = {} local raw_pixels = {}
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
@ -96,7 +102,26 @@ 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_r8g8b8_to_bw8_raw()
assert(3 == self.pixel_depth)
local raw_pixels = {}
for _, row in ipairs(self.pixels) do
for _, pixel in ipairs(row) do
-- see <https://alienryderflex.com/saturation.html>
local gray = math.floor(
0.299 * pixel[1] +
0.587 * pixel[2] +
0.114 * pixel[3]
)
local raw_pixel = string.char(gray)
raw_pixels[#raw_pixels + 1] = raw_pixel
end
end
self.data = self.data .. table.concat(raw_pixels)
end
function image:encode_data_a1r5g5b5_raw() function image:encode_data_a1r5g5b5_raw()
assert(3 == self.pixel_depth)
local raw_pixels = {} local raw_pixels = {}
-- Sample depth rescaling is done according to the algorithm presented in: -- Sample depth rescaling is done according to the algorithm presented in:
-- <https://www.w3.org/TR/2003/REC-PNG-20031110/#13Sample-depth-rescaling> -- <https://www.w3.org/TR/2003/REC-PNG-20031110/#13Sample-depth-rescaling>
@ -116,6 +141,7 @@ function image:encode_data_a1r5g5b5_raw()
end end
function image:encode_data_a1r5g5b5_rle() function image:encode_data_a1r5g5b5_rle()
assert(3 == self.pixel_depth)
local colorword = nil local colorword = nil
local previous_r = nil local previous_r = nil
local previous_g = nil local previous_g = nil
@ -205,6 +231,7 @@ function image:encode_data_a1r5g5b5_rle()
end end
function image:encode_data_r8g8b8_raw() function image:encode_data_r8g8b8_raw()
assert(3 == self.pixel_depth)
local raw_pixels = {} local raw_pixels = {}
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
@ -216,6 +243,7 @@ function image:encode_data_r8g8b8_raw()
end end
function image:encode_data_r8g8b8_rle() function image:encode_data_r8g8b8_rle()
assert(3 == self.pixel_depth)
local previous_r = nil local previous_r = nil
local previous_g = nil local previous_g = nil
local previous_b = nil local previous_b = nil