diff options
author | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-04-29 15:36:53 +0200 |
---|---|---|
committer | Elias Fleckenstein <eliasfleckenstein@web.de> | 2021-04-29 15:36:53 +0200 |
commit | ff9e09cff61c58ba1ced1233c5b3052a1e758c5e (patch) | |
tree | c7aba48f7d06056bae84feea2b009ba75b2ecc0b | |
parent | 79da40f399a7aaf2a281de59d6ae013344fc8ff6 (diff) | |
download | tga_encoder-ff9e09cff61c58ba1ced1233c5b3052a1e758c5e.tar.xz |
Implement header encoding
-rw-r--r-- | init.lua | 83 |
1 files changed, 79 insertions, 4 deletions
@@ -1,11 +1,86 @@ tga_encoder = {} -function tga_encoder.encode_pixels(pixels) +local image = setmetatable({}, { + __call = function(self, ...) + local t = setmetatable({}, {__index = image}) + t:constructor(...) + return t + end, +}) + +function image:constructor(pixels) + self.bytes = {} + self.pixels = pixels + self.width = #pixles[1] + self.height = #pixels + + self:encode() +end + +function image:write(size, value) + -- TGA uses little endian encoding + local l = #self.bytes + for i = 1, size do + local byte = value % 256 + value = value - byte + value = value / 256 + self.bytes[l + i] = byte + end +end + +function image:encode_colormap_spec() + -- first entry index + self:write(2, 0) + -- number of entries + self:write(2, 0) + -- number of bits per pixel + self:write(1, 0) +end + +function image:encode_image_spec() + -- X- and Y- origin + self:write(2, 0) + self:write(2, 0) + -- width and height + self:write(2, width) + self:write(2, height) + -- pixel depth + self:write(1, 24) + -- image descriptor + self:write(1, 0) end -function tga_encoder.save_image(filename, pixels) +function image:encode_header() + -- id length + self:write(1, 0) -- no image id info + -- color map type + self:write(1, 0) -- no color map + -- image type + self:write(1, 2) -- uncompressed true-color image + -- color map specification + self:encode_colormap_spec() + -- image specification + self:encode_image_spec() +end + +function image:encode_data() + -- ToDo +end + +function image:encode() + -- encode header + self:encode_header() + -- no color map and image id data + -- encode data + self:encode_data() + -- no extension area or file footer +end + +function image:save(filename) + self.data = self.data or string.char(unpack(self.bytes)) local f = assert(io.open(filename)) - local encoded = tga_encoder.encode_pixels(pixels) - f:write(encoded) + f:write(data) f:close() end + +tga_encoder.image = image |