Move encoding format heuristics from image:save() to image:encode()

This commit is contained in:
Nils Dagsson Moskopp 2023-09-18 16:45:31 +02:00
parent 3029147ed7
commit 4dd3833f3b
No known key found for this signature in database
GPG Key ID: A3BC671C35191080
2 changed files with 25 additions and 9 deletions

View File

@ -14,6 +14,22 @@ local pixels = {
}
tga_encoder.image(pixels):save("bitmap_small.tga")
-- test that image can be encoded
local bitmap_small_0 = tga_encoder.image(pixels)
bitmap_small_0:encode()
assert(191 == #bitmap_small_0.data)
-- test that imbage can be encoded with parameters
local bitmap_small_1 = tga_encoder.image(pixels)
bitmap_small_1:encode(
{
colormap = {},
color_format = "B8G8R8",
compression = "RAW",
}
)
assert(191 == #bitmap_small_1.data)
-- change a single pixel, then rescale the bitmap
local pixels_orig = pixels
pixels_orig[4][4] = { 255, 255, 255 }

View File

@ -549,15 +549,6 @@ function image:encode_footer()
end
function image:encode(properties)
self.data = ""
self:encode_header(properties) -- header
-- no color map and image id data
self:encode_data(properties) -- encode data
-- no extension or developer area
self:encode_footer() -- footer
end
function image:save(filename, properties)
local properties = properties or {}
properties.colormap = properties.colormap or {}
properties.compression = properties.compression or "RAW"
@ -584,6 +575,15 @@ function image:save(filename, properties)
end
assert( nil ~= properties.color_format )
self.data = ""
self:encode_header(properties) -- header
-- no color map and image id data
self:encode_data(properties) -- encode data
-- no extension or developer area
self:encode_footer() -- footer
end
function image:save(filename, properties)
self:encode(properties)
local f = assert(io.open(filename, "wb"))