Advertisement
klindley

imglib

Oct 29th, 2015
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. local serial = require("serialization")
  2. local fs = require("filesystem")
  3. local comp = require("component")
  4.  
  5. local image = {}
  6.  
  7. function image.load(path)
  8. local retIm = {}
  9. if fs.exists(path) then
  10. file = io.open(path, "r")
  11. retIm = serial.unserialize(file:read("*all"))
  12. file:close()
  13. local imData = compressImage(retIm)
  14. return imData
  15. end
  16. return false
  17. end
  18.  
  19. function compressImage(data)
  20. local pTab = {}
  21. for i = 1, #data["layerData"] do
  22. pTab[i] = {}
  23. if data["layerData"][i]["hidden"] then
  24. pTab[i]["hidden"] = true
  25. else
  26. pTab[i]["hidden"] = false
  27. end
  28. for line = #data["layerData"][i], 1, -1 do
  29. local colCount = 0
  30. local curBg = data["layerData"][i][line][1]["bg"]
  31. local curFg = data["layerData"][i][line][1]["fg"]
  32. local curSt = data["layerData"][i][line][1]["char"]
  33. local stx = 1
  34. local sty = 1 + (line - 1)
  35. pTab[i][line] = {}
  36. for pix = 1, #data["layerData"][i][line] do
  37. curBg = data["layerData"][i][line][pix]["bg"]
  38. curFg = data["layerData"][i][line][pix]["fg"]
  39. curSt = data["layerData"][i][line][pix]["char"]
  40. if curBg == "trans" then
  41. for x = i, 1, -1 do
  42. if not data["layerData"][x]["hidden"] then
  43. if data["layerData"][x][line][pix]["bg"] ~= "trans" then
  44. curBg = data["layerData"][x][line][pix]["bg"]
  45. end
  46. elseif x == 1 then
  47. curBg = data["layerData"][x][line][pix]["bg"]
  48. end
  49. end
  50. end
  51. if pix == 1 or curBg ~= pTab[i][line][colCount]["bg"] or curFg ~= pTab[i][line][colCount]["fg"] then
  52. colCount = colCount + 1
  53. pTab[i][line][colCount] = {
  54. bg = curBg,
  55. fg = curFg,
  56. st = curSt,
  57. yp = sty,
  58. xp = stx,
  59. }
  60. else
  61. pTab[i][line][colCount]["st"] = pTab[i][line][colCount]["st"]..curSt
  62. end
  63. stx = stx + 1
  64. end
  65. end
  66. end
  67. return(pTab)
  68. end
  69.  
  70. function image.draw(data, gpu, x, y)
  71. if data ~= nil then
  72. for layer = 1, #data do
  73. for line = 1, #data[layer] do
  74. if not data[layer]["hidden"] then
  75. for col = 1, #data[layer][line] do
  76. if data[layer][line][col]["bg"] ~= "trans" then
  77. local xPos = x + data[layer][line][col]["xp"] - 1
  78. local yPos = y + data[layer][line][col]["yp"] - 1
  79. gpu.setBackground(data[layer][line][col]["bg"])
  80. gpu.setForeground(data[layer][line][col]["fg"])
  81. gpu.set(xPos, yPos, data[layer][line][col]["st"])
  82. end
  83. end
  84. end
  85. end
  86. end
  87. end
  88. gpu.setBackground(0x000000)
  89. gpu.setForeground(0xFFFFFF)
  90. end
  91.  
  92. return image
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement