Treat empty colormap as no colormap

This commit is contained in:
Nils Dagsson Moskopp 2022-05-16 17:56:53 +02:00
parent 9f9b78eed9
commit e83894fcfa
No known key found for this signature in database
GPG Key ID: A3BC671C35191080

@ -15,19 +15,15 @@ function image:constructor(pixels)
end end
function image:encode_colormap_spec(properties) function image:encode_colormap_spec(properties)
local colormap_spec local colormap = properties.colormap
if nil ~= properties.colormap then local colormap_pixel_depth = 0
local color_count = #properties.colormap if 0 ~= #colormap then
colormap_spec = colormap_pixel_depth = #colormap[1] * 8
string.char(0, 0) .. -- first entry index
string.char(color_count % 256, math.floor(color_count / 256)) .. -- number of entries
string.char(#properties.colormap[1] * 8) -- bits per pixel
else -- no colormap
colormap_spec =
string.char(0, 0) .. -- first entry index
string.char(0, 0) .. -- number of entries
string.char(0) -- bits per pixel
end end
local colormap_spec =
string.char(0, 0) .. -- first entry index
string.char(#colormap % 256, math.floor(#colormap / 256)) .. -- number of entries
string.char(colormap_pixel_depth) -- bits per pixel
self.data = self.data .. colormap_spec self.data = self.data .. colormap_spec
end end
@ -46,7 +42,7 @@ function image:encode_image_spec(properties)
["B8G8R8A8"] = 32, ["B8G8R8A8"] = 32,
} }
local pixel_depth local pixel_depth
if nil ~= properties.colormap then if 0 ~= #properties.colormap then
pixel_depth = self.pixel_depth pixel_depth = self.pixel_depth
else else
pixel_depth = pixel_depth_by_color_format[color_format] pixel_depth = pixel_depth_by_color_format[color_format]
@ -63,7 +59,7 @@ end
function image:encode_colormap(properties) function image:encode_colormap(properties)
local colormap = properties.colormap local colormap = properties.colormap
if nil == colormap then if 0 == #colormap then
return return
end end
local colormap_pixel_depth = #colormap[1] * 8 local colormap_pixel_depth = #colormap[1] * 8
@ -87,8 +83,10 @@ function image:encode_header(properties)
local color_format = properties.color_format local color_format = properties.color_format
local colormap = properties.colormap local colormap = properties.colormap
local compression = properties.compression local compression = properties.compression
local colormap_type
local image_type local image_type
if "Y8" == color_format and "RAW" == compression then if "Y8" == color_format and "RAW" == compression then
colormap_type = 0
image_type = 3 -- grayscale image_type = 3 -- grayscale
elseif ( elseif (
"A1R5G5B5" == color_format or "A1R5G5B5" == color_format or
@ -96,20 +94,20 @@ function image:encode_header(properties)
"B8G8R8A8" == color_format "B8G8R8A8" == color_format
) then ) then
if "RAW" == compression then if "RAW" == compression then
if nil ~= colormap then if 0 ~= #colormap then
colormap_type = 1
image_type = 1 -- colormapped RGB(A) image_type = 1 -- colormapped RGB(A)
else else
colormap_type = 0
image_type = 2 -- RAW RGB(A) image_type = 2 -- RAW RGB(A)
end end
elseif "RLE" == compression then elseif "RLE" == compression then
colormap_type = 0
image_type = 10 -- RLE RGB image_type = 10 -- RLE RGB
end end
end end
assert( nil ~= colormap_type )
assert( nil ~= image_type ) assert( nil ~= image_type )
local colormap_type = 0
if nil ~= colormap then
colormap_type = 1
end
self.data = self.data self.data = self.data
.. string.char(0) -- image id .. string.char(0) -- image id
.. string.char(colormap_type) .. string.char(colormap_type)
@ -138,7 +136,7 @@ function image:encode_data(properties)
self:encode_data_R8G8B8_as_A1R5G5B5_rle() self:encode_data_R8G8B8_as_A1R5G5B5_rle()
end end
elseif "B8G8R8" == color_format then elseif "B8G8R8" == color_format then
if nil ~= colormap then if 0 ~= #colormap then
if "RAW" == compression then if "RAW" == compression then
if 8 == self.pixel_depth then if 8 == self.pixel_depth then
self:encode_data_Y8_as_Y8_raw() self:encode_data_Y8_as_Y8_raw()
@ -509,6 +507,7 @@ end
function image:save(filename, properties) function image:save(filename, properties)
local properties = properties or {} local properties = properties or {}
properties.colormap = properties.colormap or {}
properties.compression = properties.compression or "RAW" properties.compression = properties.compression or "RAW"
self.pixel_depth = #self.pixels[1][1] * 8 self.pixel_depth = #self.pixels[1][1] * 8
@ -519,7 +518,7 @@ function image:save(filename, properties)
[32] = "B8G8R8A8", [32] = "B8G8R8A8",
} }
if nil == properties.color_format then if nil == properties.color_format then
if nil ~= properties.colormap then if 0 ~= #properties.colormap then
properties.color_format = properties.color_format =
color_format_defaults_by_pixel_depth[ color_format_defaults_by_pixel_depth[
#properties.colormap[1] * 8 #properties.colormap[1] * 8