extern/ppm.lua raw
 1local spr = app.activeSprite
 2
 3if not spr then
 4	app.alert("No active sprite")
 5	return
 6end
 7
 8local path = app.fs.filePath(spr.filename)
 9local name = app.fs.fileTitle(spr.filename)
10
11if path == "" then
12	path = app.fs.userDocsPath
13end
14
15local out_path = app.fs.joinPath(path, name .. ".ppm")
16
17local img = Image(spr.width, spr.height, ColorMode.RGB)
18
19-- Render visible layers / current frame into one image
20img:drawSprite(spr, app.activeFrame.frameNumber)
21
22local file = io.open(out_path, "wb")
23
24if not file then
25	app.alert("Could not write file:\n" .. out_path)
26	return
27end
28
29-- PPM binary format, P6
30file:write("P6\n")
31file:write(tostring(spr.width) .. " " .. tostring(spr.height) .. "\n")
32file:write("255\n")
33
34for y = 0, spr.height - 1 do
35	for x = 0, spr.width - 1 do
36		local pixel = img:getPixel(x, y)
37		local c = Color(pixel)
38
39		file:write(string.char(c.red))
40		file:write(string.char(c.green))
41		file:write(string.char(c.blue))
42	end
43end
44
45file:close()
46
47app.alert("Exported PPM:\n" .. out_path)