jgtrosh

jpg_transform.lua

Nov 3rd, 2013
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.37 KB | None | 0 0
  1. require "gd"
  2.  
  3. function project(px, py, pz, win_width, win_height, fov, cam)
  4.     local factor = fov / (cam+pz)
  5.     local x = px*factor + win_width/2
  6.     local y = -py*factor + win_height/2
  7.     return x, y
  8. end
  9.  
  10. local base = gd.createFromJpeg("base.jpg")
  11. local basepal = base:createPaletteFromTrueColor(true, 150)
  12. local black = basepal:colorAllocate(0, 0, 0)
  13. local sizex, sizey = base:sizeXY()
  14. local colors = {}
  15. for x = 1, sizex do
  16.     colors[x] = {}
  17.     for y = 1, sizey do
  18.         local color = base:getPixel(x, y)
  19.         local colorpal = basepal:getPixel(x, y)
  20.         colors[x][y] = {
  21.             r = base:red(color),
  22.             g = base:green(color),
  23.             b = base:blue(color),
  24.             p = colorpal}
  25.         local c = colors[x][y]
  26.         c.w = (c.r+c.g+c.b)/3
  27.     end
  28. end
  29. local xo = {}
  30. for x = 1, sizex/2 do table.insert(xo, x) end
  31. for x = sizex, sizex/2, -1 do table.insert(xo, x) end
  32. basepal:gifAnimBegin("out.gif", true, 0)
  33. for frm = 0, 29 do
  34.     local prev = basepal
  35.     basepal:filledRectangle(0, 0, sizex, sizey, black)
  36.     for _, x in pairs(xo) do
  37.         for y = 1, sizey do
  38.             local px, py = project(x-sizex/2, -y+sizey/2,
  39.                 -frm*math.pow(math.max(0, colors[x][y].w-20)/200, 3), sizex, sizey, 30, 30)
  40.             basepal:setPixel(px, py, colors[x][y].p)
  41.         end
  42.     end
  43.     basepal:gifAnimAdd("out.gif", false, 0, 0, 5, gd.DISPOSAL_NONE, prev)
  44.     --gd.DISPOSAL_RESTORE_BACKGROUND)
  45.     print("frame", frm)
  46. end
  47. gd.gifAnimEnd("out.gif")
  48. os.execute("out.gif")
Advertisement
Add Comment
Please, Sign In to add comment