demo-04-20/demo.lua

71 lines
1.7 KiB
Lua
Raw Permalink Normal View History

2020-04-24 14:38:12 +02:00
local cairo = require'cairo' --used to open the file
2020-04-22 13:39:56 +02:00
local bitmap = require'bitmap' --used to read its content
2020-05-03 18:19:49 +02:00
equivalents = dofile'equivalents.lua'
2020-05-02 19:24:52 +02:00
2020-05-03 18:19:49 +02:00
function to_chr(nb)
2020-05-02 19:24:52 +02:00
if equivalents[nb] then return equivalents[nb] end
2020-04-25 13:18:29 +02:00
if nb > 255 then print("uh", nb) end
if nb < 16 then add = "0"
else add = "" end
return [[\x]]..add..string.format("%x", nb)
2020-04-22 18:58:33 +02:00
end
2021-01-24 13:29:39 +01:00
file = io.open("spring.py", "w")
2020-04-25 10:52:10 +02:00
first = true
pallet = {} --will store colors
2020-04-25 13:18:29 +02:00
pallet_last = 0
mem_pallet_last = 0
2020-04-25 10:52:10 +02:00
file:write([[from draw import *
def main():
black=(0,0,0)
for x in range(384):
for y in range(192):
set_pixel(x,y,black)
]])
2020-04-22 18:58:33 +02:00
2021-01-24 13:29:39 +01:00
for i = 1, 1, 1 do
2020-04-25 10:52:10 +02:00
print(i)
2020-04-24 14:38:12 +02:00
do --open image
2021-01-24 13:29:39 +01:00
path = "spring.png"
2020-04-24 14:38:12 +02:00
local image = cairo.load_png(path)
image_infos = {width = image:width(), height = image:height()}
image_bitmap = image:bitmap()
end
local getpixel, setpixel = bitmap.pixel_interface(image_bitmap)
2020-04-22 18:58:33 +02:00
file:write("\n", [[ data=b"]])
2020-04-25 12:13:57 +02:00
--end
2020-04-24 14:38:12 +02:00
for j = 1, image_infos.height, 1 do
for i = 1, image_infos.width, 1 do
r, g, b = getpixel(i, j)
if not pallet[r..","..g..","..b] then
pallet[r..","..g..","..b] = pallet_last
pallet_last = pallet_last + 1
end
2020-05-03 18:19:49 +02:00
file:write(to_chr(pallet[r..","..g..","..b]))
2020-04-22 18:58:33 +02:00
end
end
2020-04-25 12:13:57 +02:00
file:write('"')
2020-04-22 18:58:33 +02:00
2020-04-24 14:38:12 +02:00
--let's write the pallet
2020-04-25 12:13:57 +02:00
if pallet_last > mem_pallet_last then
mem_pallet_last = pallet_last
file:write("\n pallet={")
2020-04-25 10:52:10 +02:00
for i, v in pairs(pallet) do
file:write(v, ":(", i, "),")
end
2020-04-25 12:13:57 +02:00
file:write("}")
2020-04-24 14:38:12 +02:00
end
file:write("\n draw(", math.floor(192 - image_infos.width / 2), ",")
2020-04-25 11:16:34 +02:00
file:write(math.floor(96 - image_infos.height / 2), ",data,pallet,")
file:write(image_infos.width, ")")
2020-04-25 12:13:57 +02:00
first = false
2020-04-24 14:38:12 +02:00
end
2021-01-24 13:29:39 +01:00
--file:write('\n wait_ac("Press AC/ON to continue...")')
2020-04-25 10:52:10 +02:00
file:close()