ImageUtils.lua 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. --ImageUtils, useful functions foe use when processing imagedatas.
  2. --Localized Lua Library
  3. --The API
  4. local ImageUtils = {}
  5. --A queued fill algorithm.
  6. function ImageUtils.queuedFill(img,sx,sy,rcol,minx,miny,maxx,maxy)
  7. local get = img.getPixel
  8. local set = img.setPixel
  9. local tcol = get(img,sx,sy) --The target color
  10. if tcol == rcol then return end
  11. --Queue, QueueSize, QueuePosition
  12. local q, qs, qp = {}, 0,0
  13. set(img,sx,sy,rcol)
  14. qs = qs + 1
  15. q[qs] = {sx,sy}
  16. local function test(x,y)
  17. if minx and (x < minx or y < miny or x > maxx or y > maxy) then return end
  18. if get(img,x,y) == tcol then
  19. set(img,x,y,rcol)
  20. qs = qs + 1
  21. q[qs] = {x,y}
  22. end
  23. end
  24. while qp < qs do --While there are items in the queue.
  25. qp = qp + 1
  26. local n = q[qp]
  27. local x,y = n[1], n[2]
  28. test(x-1,y) test(x+1,y)
  29. test(x,y-1) test(x,y+1)
  30. end
  31. end
  32. local darkPal = {
  33. {0,0,5,1,2,1,13,6,2,4,9,3,13,5,13,6}, --Level 1
  34. {0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1} --Level 2
  35. }
  36. function ImageUtils.darken(img, lvl)
  37. img:map(function(x,y,c)
  38. return darkPal[lvl or 1][c+1]
  39. end)
  40. end
  41. --Make the ImageUtils a global
  42. _G["ImageUtils"] = ImageUtils