Add RGBA support (only RAW encoding)

This commit is contained in:
Nils Dagsson Moskopp 2022-05-16 01:39:33 +02:00
parent 2112637faf
commit 36ee45ebe3
No known key found for this signature in database
GPG Key ID: A3BC671C35191080
2 changed files with 31 additions and 4 deletions

View File

@ -65,6 +65,15 @@ gradients:save("gradients_16bpp_rle.tga", {colors="RGB", compression="RLE", pixe
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})
gradients:save("gradients_24bpp_rle.tga", {colors="RGB", compression="RLE", pixel_depth=24}) gradients:save("gradients_24bpp_rle.tga", {colors="RGB", compression="RLE", pixel_depth=24})
for x = 1,16,1 do -- left to right
for z = 1,16,1 do -- bottom to top
local color = pixels[z][x]
color[#color+1] = ((x * x) + (z * z)) % 256
pixels[z][x] = color
end
end
gradients:save("gradients_32bpp_raw.tga", {colors="RGBA", compression="RAW", pixel_depth=32})
local pixels = {} local pixels = {}
for x = 1,512,1 do -- left to right for x = 1,512,1 do -- left to right
for z = 1,512,1 do -- bottom to top for z = 1,512,1 do -- bottom to top

View File

@ -12,7 +12,6 @@ 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()
@ -28,7 +27,8 @@ function image:encode_image_spec(properties)
assert( assert(
"BW" == colors and 8 == pixel_depth or -- (8 bit grayscale = 1 byte = 8 bits) "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 16 == pixel_depth or -- (A1R5G5B5 = 2 bytes = 16 bits)
"RGB" == colors and 24 == pixel_depth -- (B8G8R8 = 3 bytes = 24 bits) "RGB" == colors and 24 == pixel_depth or -- (B8G8R8 = 3 bytes = 24 bits)
"RGBA" == colors and 32 == pixel_depth -- (B8G8R8A8 = 4 bytes = 32 bits)
) )
self.data = self.data self.data = self.data
.. string.char(0, 0) -- X-origin .. string.char(0, 0) -- X-origin
@ -48,10 +48,11 @@ function image:encode_header(properties)
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 or
"RGBA" == colors and 32 == pixel_depth
) then ) then
if "RAW" == compression then if "RAW" == compression then
image_type = 2 -- RAW RGB image_type = 2 -- RAW RGB(A)
elseif "RLE" == compression then elseif "RLE" == compression then
image_type = 10 -- RLE RGB image_type = 10 -- RLE RGB
end end
@ -68,6 +69,7 @@ function image:encode_data(properties)
local colors = properties.colors local colors = properties.colors
local compression = properties.compression local compression = properties.compression
local pixel_depth = properties.pixel_depth local pixel_depth = properties.pixel_depth
self.pixel_depth = #self.pixels[1][1]
if "BW" == colors and "RAW" == compression and 8 == pixel_depth then if "BW" == colors and "RAW" == compression and 8 == pixel_depth then
if 1 == self.pixel_depth then if 1 == self.pixel_depth then
@ -87,6 +89,10 @@ function image:encode_data(properties)
elseif "RLE" == compression then elseif "RLE" == compression then
self:encode_data_r8g8b8_rle() self:encode_data_r8g8b8_rle()
end end
elseif "RGBA" == colors and 32 == pixel_depth then
if "RAW" == compression then
self:encode_data_r8g8b8a8_raw()
end
end end
end end
@ -323,6 +329,18 @@ function image:encode_data_r8g8b8_rle()
self.data = self.data .. table.concat(packets) self.data = self.data .. table.concat(packets)
end end
function image:encode_data_r8g8b8a8_raw()
assert(4 == self.pixel_depth)
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
function image:encode_footer() function image:encode_footer()
self.data = self.data self.data = self.data
.. string.char(0, 0, 0, 0) -- extension area offset .. string.char(0, 0, 0, 0) -- extension area offset