aboutsummaryrefslogtreecommitdiff
path: root/init.lua
blob: 45b9e85a238701cbf5f5f4cc5cb42826fcbdba28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
tga_encoder = {}

local LUA_ARGS_LIMIT = 1000

local image = setmetatable({}, {
	__call = function(self, ...)
		local t = setmetatable({}, {__index = self})
		t:constructor(...)
		return t
	end,
})

function image:constructor(pixels)
	self.bytes = {}
	self.chunks = {self.bytes}
	self.pixels = pixels
	self.width = #pixels[1]
	self.height = #pixels

	self:encode()
end

function image:insert(byte)
	table.insert(self.bytes, byte)
	if #self.bytes == LUA_ARGS_LIMIT then
		self.bytes = {}
		table.insert(self.chunks, self.bytes)
	end
end

function image:littleendian(size, value)
	for i = 1, size do
		local byte = value % 256
		value = value - byte
		value = value / 256
		self:insert(byte)
	end
end

function image:encode_colormap_spec()
	-- first entry index
	self:littleendian(2, 0)
	-- number of entries
	self:littleendian(2, 0)
	-- number of bits per pixel
	self:insert(0)
end

function image:encode_image_spec()
	-- X- and Y- origin
	self:littleendian(2, 0)
	self:littleendian(2, 0)
	-- width and height
	self:littleendian(2, self.width)
	self:littleendian(2, self.height)
	-- pixel depth
	self:insert(24)
	-- image descriptor
	self:insert(0)
end

function image:encode_header()
	-- id length
	self:insert(0) -- no image id info
	-- color map type
	self:insert(0) -- no color map
	-- image type
	self:insert(2) -- uncompressed true-color image
	-- color map specification
	self:encode_colormap_spec()
	-- image specification
	self:encode_image_spec()
end

function image:encode_data()
	for _, row in ipairs(self.pixels) do
		for _, pixel in ipairs(row) do
			self:insert(pixel[3])
			self:insert(pixel[2])
			self:insert(pixel[1])
		end
	end
end

function image:encode()
	-- encode header
	self:encode_header()
	-- no color map and image id data
	-- encode data
	self:encode_data()
	-- no extension area
end

function image:get_data()
	local data = ""
	for _, bytes in ipairs(self.chunks) do
		data = data .. string.char(unpack(bytes))
	end
	return data .. string.char(0, 0, 0, 0) .. string.char(0, 0, 0, 0) .. "TRUEVISION-XFILE." .. string.char(0)
end

function image:save(filename)
	self.data = self.data or self:get_data()
	local f = assert(io.open(filename, "w"))
	f:write(self.data)
	f:close()
end

tga_encoder.image = image