Advertisement
pedrosgali

imageLib.lua

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