summaryrefslogtreecommitdiff
path: root/framebuf.lua
blob: c6b6bea35092b7558860197cd00d1f3c72391e85 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
local M = {}

local hex_digits = {
	"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"
}

function M.new()
	return setmetatable({}, M)
end

function M:__newindex(k, v)
	error("assignment to framebuffer object", 2)
end

function M:__index(k)
	if type(k) == "number" and math.floor(k) == k then
		rawset(self, k, {})
		return rawget(self, k)
	else
		return M[k]
	end
end

function M:clear()
	for k in pairs(self) do
		if type(k) == "number" then
			self[k] = nil
		end
	end
end

local function round(x, y)
	return math.floor(x + 0.5), math.floor(y + 0.5)
end

function M:plot(x, y, r, g, b)
	local x, y = round(x, y)
	self[x][y] = colors.packRGB(r, g, b)
end

function M:get(x, y)
	local x, y = round(x, y)
	if self[x][y] then
		return colors.unpackRGB(self[x][y])
	end
end

function M:rect(x1, y1, x2, y2, r, g, b)
	for x = x1, x2 do
		for y = y1, y2 do
			self:plot(x, y, r, g, b)
		end
	end
end

function M:get_size()
	return #self, #(self[1] or {})
end

function M:ppm(data, x, y)
	if type(data) ~= "string" then
		local f = data
		data = assert(f.readAll())
	end
	local x, y = round(x or 1, y or 1)
	local w, h, depth, data = data:match "^P6\n(%d+) (%d+)\n(%d+)\n(.+)"
	assert(w and h and depth, "invalid format; binary PPM (P6) requried")
	for dy = 1, h do
		for dx = 1, w do
			local i = (((dy - 1) * w + dx) - 1) * 3
			local r, g, b =
				data:byte(i + 1) / depth,
				data:byte(i + 2) / depth,
				data:byte(i + 3) / depth
			self:plot(dx + x - 1, dy + y - 1, r, g, b)
		end
	end
end

-- determine a palette for a set of colors
local function median_cut(cols, nbuckets)
	local max, min = {}, {}
	for _, color in ipairs(cols) do
		for ch, v in ipairs(color) do
			if not max[ch] or v > max[ch] then max[ch] = v end
			if not min[ch] or v < min[ch] then min[ch] = v end
		end
	end
	local max_variance, max_variant
	for ch in ipairs(max) do
		local variance = max[ch] - min[ch]
		if not max_variance or variance > max_variance then
			max_variance = variance
			max_variant = ch
		end
	end
	table.sort(cols, function(a, b) return a[max_variant] <= b[max_variant] end)
	local partitions = {{}, {}}
	local half = math.floor(#cols / 2)
	for i = 1, half do
		table.insert(partitions[1], cols[i])
	end
	for i = half + 1, #cols do
		table.insert(partitions[2], cols[i])
	end
	local palette, map = {}, {}
	if nbuckets <= 2 then
		for n, part in ipairs(partitions) do
			local avg = {0, 0, 0}
			for _, color in ipairs(part) do
				for ch, v in ipairs(color) do
					avg[ch] = avg[ch] + v
				end
				map[colors.packRGB(unpack(color))] = n
			end
			for ch, v in ipairs(avg) do
				avg[ch] = avg[ch] / #part
			end
			palette[n] = colors.packRGB(unpack(avg))
		end
	else
		local palette1, map1 = median_cut(partitions[1], nbuckets / 2)
		local palette2, map2 = median_cut(partitions[2], nbuckets / 2)
		for b, m in pairs {[palette1] = map1, [palette2] = map2} do
			local offs = #palette
			for i, b in ipairs(b) do
				palette[i + offs] = b
			end
			for c, i in pairs(m) do
				map[c] = i + offs
			end
		end
	end
	return palette, map
end

local function get_char(fb, x, y)
	local cols = {}
	local pixels = {}
	for y = y, y + 2 do
		for x = x, x + 1 do
			local color = {fb:get(x, y)}
			if color[1] then
				table.insert(cols, color)
				table.insert(pixels, colors.packRGB(unpack(color)))
			else
				table.insert(pixels, 0)
			end
		end
	end
	if #cols == 0 then
		return string.char(0x80)
	end
	local palette, map = median_cut(cols, 2)
	local char = 0x80
	-- TODO: document this
	local p6 = map[pixels[6]] or 1
	for i = 1, 5 do
		if (map[pixels[i]] or 1) ~= p6 then
			char = char + 2^(i - 1)
		end
	end
	local fg, bg
	if map[pixels[6]] == 2 then
		fg, bg = palette[1], palette[2]
	else
		fg, bg = palette[2], palette[1]
	end
	return string.char(char), fg, bg
end

function M.resolution(mon)
	local w, h = mon.getSize()
	return w * 2, h * 3
end

function M:present(mon, offsx, offsy)
	print("PrESENT")
	offsx, offsy = offsx or 0, offsy or 0
	local w, h = mon.getSize()
	local pw, ph = M.resolution(mon)
	local cols = {}
	local chars = {}
	-- print("","1")
	for y = offsy + 1, offsy + ph, 3 do
		for x = offsx + 1, offsx + pw, 2 do
			local char, fg, bg = get_char(self, x, y)
			table.insert(chars, {char, fg, bg})
			if fg and bg then
				table.insert(cols, {colors.unpackRGB(fg)})
				table.insert(cols, {colors.unpackRGB(bg)})
			end
		end
	end
	-- print("","2")
	local palette, map = median_cut(cols, 16)
	for i, color in ipairs(palette) do
		mon.setPaletteColor(2^(i - 1), color)
	end
	-- print("","3")
	for i = 1, w * h, w do
		mon.setCursorPos(1, (i - 1) / w)
		local blit_char, blit_fg, blit_bg = {}, {}, {}
		for i = i, i + w - 1 do
			local char, fg, bg = unpack(chars[i])
			table.insert(blit_char, char)
			table.insert(blit_fg, fg and hex_digits[map[fg]] or "1")
			table.insert(blit_bg, bg and hex_digits[map[bg]] or "1")
		end
		mon.blit(
			table.concat(blit_char),
			table.concat(blit_fg),
			table.concat(blit_bg))
	end
end

return M