local serial = require("serialization") local fs = require("filesystem") local comp = require("component") local image = {} function image.load(path) local retIm = {} if fs.exists(path) then file = io.open(path, "r") retIm = serial.unserialize(file:read("*all")) file:close() local imData = compressImage(retIm) return imData end return false end function compressImage(data) local pTab = {} for i = 1, #data["layerData"] do pTab[i] = {} if data["layerData"][i]["hidden"] then pTab[i]["hidden"] = true else pTab[i]["hidden"] = false end for line = #data["layerData"][i], 1, -1 do local colCount = 0 local curBg = data["layerData"][i][line][1]["bg"] local curFg = data["layerData"][i][line][1]["fg"] local curSt = data["layerData"][i][line][1]["char"] local stx = 1 local sty = 1 + (line - 1) pTab[i][line] = {} for pix = 1, #data["layerData"][i][line] do curBg = data["layerData"][i][line][pix]["bg"] curFg = data["layerData"][i][line][pix]["fg"] curSt = data["layerData"][i][line][pix]["char"] if curBg == "trans" then for x = i, 1, -1 do if not data["layerData"][x]["hidden"] then if data["layerData"][x][line][pix]["bg"] ~= "trans" then curBg = data["layerData"][x][line][pix]["bg"] end elseif x == 1 then curBg = data["layerData"][x][line][pix]["bg"] end end end if pix == 1 or curBg ~= pTab[i][line][colCount]["bg"] or curFg ~= pTab[i][line][colCount]["fg"] then colCount = colCount + 1 pTab[i][line][colCount] = { bg = curBg, fg = curFg, st = curSt, yp = sty, xp = stx, } else pTab[i][line][colCount]["st"] = pTab[i][line][colCount]["st"]..curSt end stx = stx + 1 end end end return(pTab) end function image.draw(data, gpu, x, y) if data ~= nil then for layer = 1, #data do for line = 1, #data[layer] do if not data[layer]["hidden"] then for col = 1, #data[layer][line] do if data[layer][line][col]["bg"] ~= "trans" then local xPos = x + data[layer][line][col]["xp"] - 1 local yPos = y + data[layer][line][col]["yp"] - 1 gpu.setBackground(data[layer][line][col]["bg"]) gpu.setForeground(data[layer][line][col]["fg"]) gpu.set(xPos, yPos, data[layer][line][col]["st"]) end end end end end end gpu.setBackground(0x000000) gpu.setForeground(0xFFFFFF) end return image