jgtrosh

pixelsort.lua

Nov 10th, 2013
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.30 KB | None | 0 0
  1. require "gd"
  2.  
  3. local base = gd.createFromJpeg("base.jpg")
  4. local sizex, sizey = base:sizeXY()
  5. local frame = gd.create(sizex, sizey)
  6. local out = gd.createTrueColor(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. frame:gifAnimBegin("out.gif", false, 0)
  16. for frm = 1, 32 do
  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 math.abs(w-sortw) > 8*(frm-1) then
  26. --stop building a "similar colors" line: sort it, draw it then start a new one
  27.                 table.sort(sortlist, function(a, b) return a.w < b.w end)
  28.                 for ly = 1, #sortlist do
  29.                     out:setPixel(x, startsort+ly-1, sortlist[ly].c) --sortlist[#sortlist-ly+1].c)
  30. --the #sortlist-ly+1 index allows easily switching between the order in which sorted lines are redrawn
  31.                 end
  32.                 startsort = y
  33.                 sortw = w
  34.                 sortlist = {{c = color, w = w}}
  35.             else
  36.                 table.insert(sortlist, {c = color, w = w})
  37.             end
  38.         end
  39.     end
  40.     frame = out:createPaletteFromTrueColor(true, 60)
  41.     frame:gifAnimAdd("out.gif", true, 0, 0, 8, gd.DISPOSAL_NONE)
  42.     print("frame:", frm)
  43. end
  44. gd.gifAnimEnd("out.gif")
Advertisement
Add Comment
Please, Sign In to add comment