3 local image = setmetatable({}, {
4 __call = function(self, ...)
5 local t = setmetatable({}, {__index = image})
11 function image:constructor(pixels)
14 self.width = #pixles[1]
20 function image:write(size, value)
21 -- TGA uses little endian encoding
24 local byte = value % 256
27 self.bytes[l + i] = byte
31 function image:encode_colormap_spec()
36 -- number of bits per pixel
40 function image:encode_image_spec()
53 function image:encode_header()
55 self:write(1, 0) -- no image id info
57 self:write(1, 0) -- no color map
59 self:write(1, 2) -- uncompressed true-color image
60 -- color map specification
61 self:encode_colormap_spec()
62 -- image specification
63 self:encode_image_spec()
66 function image:encode_data()
67 for _, row in ipairs(self.pixels) do
68 for _, pixel in ipairs(row) do
76 function image:encode()
79 -- no color map and image id data
82 -- no extension area or file footer
85 function image:save(filename)
86 self.data = self.data or string.char(unpack(self.bytes))
87 local f = assert(io.open(filename))
92 tga_encoder.image = image