Do not encode images with illegal colormap indexes

This commit is contained in:
Nils Dagsson Moskopp 2023-09-22 20:05:27 +02:00
parent 4dd3833f3b
commit bb68ceb38d
No known key found for this signature in database
GPG Key ID: A3BC671C35191080
2 changed files with 27 additions and 0 deletions

@ -164,3 +164,18 @@ local pixels = {
{ _, _, _, K, _, _, _ }, { _, _, _, K, _, _, _ },
} }
tga_encoder.image(pixels):save("colormapped_B8G8R8A8.tga", {colormap=colormap}) tga_encoder.image(pixels):save("colormapped_B8G8R8A8.tga", {colormap=colormap})
-- encoding a colormapped image with illegal colormap indexes should error out
local colormap = {
{ 0, 0, 0, 0 },
{ 0, 0, 0, 255 },
}
local status, message = pcall(
function ()
tga_encoder.image(pixels):encode({colormap=colormap})
end
)
assert(
false == status and
"init.lua:36: colormap index 2 not in colormap of size 2" == message
)

@ -28,6 +28,18 @@ function image:encode_colormap_spec(properties)
colormap_pixel_depth = pixel_depth_by_color_format[ colormap_pixel_depth = pixel_depth_by_color_format[
properties.color_format properties.color_format
] ]
-- ensure that each pixel references a legal colormap entry
for _, row in ipairs(self.pixels) do
for _, pixel in ipairs(row) do
local colormap_index = pixel[1]
if colormap_index >= #colormap then
error(
"colormap index " .. colormap_index ..
" not in colormap of size " .. #colormap
)
end
end
end
end end
local colormap_spec = local colormap_spec =
string.char(0, 0) .. -- first entry index string.char(0, 0) .. -- first entry index