-- montage.lua : upscale generated assets into preview PNGs for visual QA
local dir = app.params["outdir"]
local function open(name) return app.open(dir.."/"..name) end
local function blit(dst, src, dx, dy, sc)
  for y=0,src.height-1 do for x=0,src.width-1 do
    local p = src:getPixel(x,y)
    for j=0,sc-1 do for i=0,sc-1 do dst:drawPixel(dx+x*sc+i, dy+y*sc+j, p) end end
  end end
end
-- tiles preview
do
  local ts = open("tileset.png"); local si = ts.cels[1].image
  local out = Sprite(128*5, 144*5, ColorMode.RGB)
  local oi = out.cels[1].image
  for y=0,oi.height-1 do for x=0,oi.width-1 do oi:drawPixel(x,y, app.pixelColor.rgba(30,30,36,255)) end end
  blit(oi, si, 0, 0, 5)
  out:saveAs(dir.."/_preview_tiles.png"); out:close(); ts:close()
end
-- chars preview
do
  local out = Sprite(760, 420, ColorMode.RGB)
  local oi = out.cels[1].image
  for y=0,oi.height-1 do for x=0,oi.width-1 do oi:drawPixel(x,y, app.pixelColor.rgba(40,44,52,255)) end end
  local pl = open("player.png"); blit(oi, pl.cels[1].image, 0, 0, 4); pl:close()
  local po = open("portraits.png"); blit(oi, po.cels[1].image, 280, 0, 4); po:close()
  local bi = open("birds.png"); blit(oi, bi.cels[1].image, 560, 0, 5); bi:close()
  local sp = open("sparrow.png"); blit(oi, sp.cels[1].image, 680, 0, 5); sp:close()
  local ex = open("extras.png"); blit(oi, ex.cels[1].image, 560, 140, 4); ex:close()
  local names = {"afu","yuan","dabao","mimi","fu","gui"}
  for i,n in ipairs(names) do
    local np = open("npc_"..n..".png"); blit(oi, np.cels[1].image, (i-1)*84, 200, 5); np:close()
  end
  out:saveAs(dir.."/_preview_chars.png"); out:close()
end
print("montage done")
