summaryrefslogtreecommitdiff
path: root/img/print.lua
blob: 89d55e8e5ab0ba3e387f168c9b1457998931ac6c (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
package.path="/?;/?.lua;"..package.path

local printer = peripheral.wrap"right"

local load_pnm = require"load_pnm".load
local file,err = io.open(arg[1],"rb")
if err then error("io.open: "..err) end
local ff = file:read("a")
local img = load_pnm(ff)
local W,H = img.width, img.height

local function c2c(col)
	local r = math.floor(col[1]*255)
	local g = math.floor(col[2]*255)
	local b = math.floor(col[3]*255)
	return b + 0x100*g + 0x10000*r
end

local function print_tile(tilex,tiley)
	print("tile ",tilex,tiley)
	local tilex0 = tilex-1
	local tiley0 = tiley-1
	local xm = (8*tilex0)+1
	local ym = (8*tiley0)+1
	local xM = math.min(W,xm+7)
	local yM = math.min(H,ym+7)
	print("  x from ",xm,"to",xM)
	print("  y from ",ym,"to",yM)

	-- really i should have called this u and v instead of x and y. joker emoji
	printer.reset()
	shapes = {}
	for px = xm,xM do
		for py = ym,yM do
			local offx = px-xm
			local offy = py-ym
			table.insert(shapes,{
				2*offx,   14-2*offy, 15,
				2*offx+2, 16-2*offy, 16,
				texture="sc-peripherals:block/white",
				tint=c2c(img[py][px])
			})
		end
	end
	printer.addShapes(shapes)
	printer.setTooltip("("..tilex..","..tiley..")")
	printer.commit(1)

end

-- 0...
for tilex = 1, math.ceil(W/8) do
	for tiley = 1, math.ceil(H/8) do
		print_tile(tilex,tiley)
	end
end