diff --git a/examples.lua b/examples.lua index 0dd6109..f304998 100644 --- a/examples.lua +++ b/examples.lua @@ -164,3 +164,18 @@ local pixels = { { _, _, _, K, _, _, _ }, } 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 +) diff --git a/init.lua b/init.lua index ec7b3b8..7a3169c 100644 --- a/init.lua +++ b/init.lua @@ -28,6 +28,18 @@ function image:encode_colormap_spec(properties) colormap_pixel_depth = pixel_depth_by_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 local colormap_spec = string.char(0, 0) .. -- first entry index