Advertisement
jgtrosh

pixelsort_anim.lua

Nov 11th, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. require "gd"
  2.  
  3. local base = gd.createFromJpeg("frm/frm1.jpg")
  4. local black = base:colorAllocate(0, 0, 0)
  5. local sizex, sizey = base:sizeXY()
  6. local frame = gd.create(sizex, sizey)
  7.  
  8. function lum(c)
  9.     local r = base:red(c)
  10.     local g = base:green(c)
  11.     local b = base:blue(c)
  12.     return (r+g+b)/3
  13. end
  14.  
  15. function sortfrmv(base, sizex, sizey, p2p, threshold, ord)
  16.     local out = gd.createTrueColor(sizex, sizey)
  17.     for x = 1, sizex do
  18.         local startsort = 1
  19.         local sortc = base:getPixel(x, 1)
  20.         local sortw = lum(sortc)
  21.         local sortlist = {{c = sortc, w = sortw}}
  22.         for y = 2, sizey do
  23.             local color = base:getPixel(x, y)
  24.             local w = lum(color)
  25.             if (p2p and math.abs(w-sortlist[#sortlist].w) > threshold)
  26.             or (not p2p and math.abs(w-sortw) > threshold) or y == sizey then
  27.                 if ord == 'inc' then
  28.                     table.sort(sortlist, function(a, b) return a.w < b.w end)
  29.                 elseif ord == 'dec' then
  30.                     table.sort(sortlist, function(a, b) return a.w > b.w end)
  31.                 else
  32.                     if sortlistside(sortlist) < 0 then
  33.                         table.sort(sortlist, function(a, b) return a.w < b.w end)
  34.                     else
  35.                         table.sort(sortlist, function(a, b) return a.w > b.w end)
  36.                     end
  37.                 end
  38.                 for ly = 1, #sortlist do
  39.                     out:setPixel(x, startsort+ly-1, sortlist[ly].c)
  40.                 end
  41.                 startsort = y
  42.                 sortw = w
  43.                 sortlist = {{c = color, w = w}}
  44.             else
  45.                 table.insert(sortlist, {c = color, w = w})
  46.             end
  47.         end
  48.     end
  49.     return out
  50. end
  51.  
  52. function sortfrmh(base, sizex, sizey, p2p, threshold, ord)
  53.     local out = gd.createTrueColor(sizex, sizey)
  54.     for y = 1, sizey do
  55.         local startsort = 1
  56.         local sortc = base:getPixel(1, y)
  57.         local sortw = lum(sortc)
  58.         local sortlist = {{c = sortc, w = sortw}}
  59.         for x = 2, sizex do
  60.             local color = base:getPixel(x, y)
  61.             local w = lum(color)
  62.             if (p2p and math.abs(w-sortlist[#sortlist].w) > threshold)
  63.             or (not p2p and math.abs(w-sortw) > threshold) or x == sizex then
  64.                 if ord == "inc" then
  65.                     table.sort(sortlist, function(a, b) return a.w < b.w end)
  66.                 elseif ord == "dec" then
  67.                     table.sort(sortlist, function(a, b) return a.w > b.w end)
  68.                 else
  69.                     if sortlistside(sortlist) < 0 then
  70.                         table.sort(sortlist, function(a, b) return a.w < b.w end)
  71.                     else
  72.                         table.sort(sortlist, function(a, b) return a.w > b.w end)
  73.                     end
  74.                 end
  75.                 for lx = 1, #sortlist do
  76.                     out:setPixel(startsort+lx-1, y, sortlist[lx].c)
  77.                 end
  78.                 startsort = x
  79.                 sortw = w
  80.                 sortlist = {{c = color, w = w}}
  81.             else
  82.                 table.insert(sortlist, {c = color, w = w})
  83.             end
  84.         end
  85.     end
  86.     return out
  87. end
  88.  
  89. frame:gifAnimBegin("out.gif", false, 0)
  90. for frm = 1, 50 do
  91.     base = gd.createFromJpeg("frm/frm"..frm..".jpg")
  92.     local th = frm-1
  93.     local sorted = sortfrmv(base, sizex, sizey, true, th, 'dec')
  94.     frame = sorted:createPaletteFromTrueColor(true, 40)
  95.     frame:gifAnimAdd("out.gif", true, 0, 0, 8, gd.DISPOSAL_NONE)
  96.     print("frame:", frm, "threshold:", th)
  97. end
  98. gd.gifAnimEnd("out.gif")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement