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()
70 function image:encode()
73 -- no color map and image id data
76 -- no extension area or file footer
79 function image:save(filename)
80 self.data = self.data or string.char(unpack(self.bytes))
81 local f = assert(io.open(filename))
86 tga_encoder.image = image