3 local LUA_ARGS_LIMIT = 1000
5 local image = setmetatable({}, {
6 __call = function(self, ...)
7 local t = setmetatable({}, {__index = self})
13 function image:constructor(pixels)
15 self.chunks = {self.bytes}
17 self.width = #pixels[1]
23 function image:insert(byte)
24 table.insert(self.bytes, byte)
25 if #self.bytes == LUA_ARGS_LIMIT then
27 table.insert(self.chunks, self.bytes)
31 function image:littleendian(size, value)
33 local byte = value % 256
40 function image:encode_colormap_spec()
42 self:littleendian(2, 0)
44 self:littleendian(2, 0)
45 -- number of bits per pixel
49 function image:encode_image_spec()
51 self:littleendian(2, 0)
52 self:littleendian(2, 0)
54 self:littleendian(2, self.width)
55 self:littleendian(2, self.height)
62 function image:encode_header()
64 self:insert(0) -- no image id info
66 self:insert(0) -- no color map
68 self:insert(2) -- uncompressed true-color image
69 -- color map specification
70 self:encode_colormap_spec()
71 -- image specification
72 self:encode_image_spec()
75 function image:encode_data()
76 for _, row in ipairs(self.pixels) do
77 for _, pixel in ipairs(row) do
85 function image:encode()
88 -- no color map and image id data
94 function image:get_data()
96 for _, bytes in ipairs(self.chunks) do
97 data = data .. string.char(unpack(bytes))
99 return data .. string.char(0, 0, 0, 0) .. string.char(0, 0, 0, 0) .. "TRUEVISION-XFILE." .. string.char(0)
102 function image:save(filename)
103 self.data = self.data or self:get_data()
104 local f = assert(io.open(filename, "w"))
109 tga_encoder.image = image