From 4dd3833f3bf3dfc40e31e80e4068adac695a2dd8 Mon Sep 17 00:00:00 2001 From: Nils Dagsson Moskopp Date: Mon, 18 Sep 2023 16:45:31 +0200 Subject: [PATCH] Move encoding format heuristics from image:save() to image:encode() --- examples.lua | 16 ++++++++++++++++ init.lua | 18 +++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/examples.lua b/examples.lua index 7642281aa..0dd61090c 100644 --- a/examples.lua +++ b/examples.lua @@ -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 } diff --git a/init.lua b/init.lua index ed387eec0..ec7b3b8d3 100644 --- a/init.lua +++ b/init.lua @@ -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"))