Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local index = {}
- for i=0,15 do
- index[2^i] = i
- end
- local function combine(bg, fg)
- return bit.blshift(bg, 4) + fg
- end
- local function split(color) local bg = bit.brshift(color, 4) local fgFactor = bg * 16 local fg = color - fgFactor return bg, fg end
- function image(x, y)
- local imdata = {}
- for i=1,x do
- imdata[i] = {}
- for j=1,y do
- imdata[i][j] = {
- ["color"] = 0,
- ["char"] = " "
- }
- end
- end
- local obj = {}
- function obj.place(xpos, ypos, char)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- imdata[xpos][ypos].char = char
- return obj
- end
- function obj.setTextColor(xpos, ypos, color)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- if not index[color] then
- error('Invalid color!', 2)
- end
- local bg, _ = split(imdata[xpos][ypos].color)
- local fg = index[color]
- imdata[xpos][ypos].color = combine(bg, fg)
- return obj
- end
- function obj.setBackgroundColor(xpos, ypos, color)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- if not index[color] then
- error('Invalid color!', 2)
- end
- local _, fg = split(imdata[xpos][ypos].color)
- local bg = index[color]
- imdata[xpos][ypos].color = combine(bg, fg)
- return obj
- end
- function obj.export()
- local data = {}
- for i = 1, y do
- data[i] = ""
- for j = 1, x do
- data[i] = data[i] .. string.char(imdata[j][i].color) .. imdata[j][i].char
- end
- end
- return textutils.serialise(
- {
- ["x"] = x,
- ["y"] = y,
- ["data"] = data
- }
- )
- end
- function obj.get(xpos, ypos)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- local bg, fg = split(imdata[xpos][ypos].color)
- return bg, fg, imdata[xpos][ypos].char
- end
- function obj.getSize()
- return x,y
- end
- return obj
- end
- function load(data)
- -- Parsing image starts here
- local data = textutils.unserialise(data)
- local imdata = {}
- local x = data.x
- local y = data.y
- for i=1,x do
- imdata[i] = {}
- for j=1,y do
- imdata[i][j] = {
- ["color"] = 0,
- ["char"] = " "
- }
- end
- end
- for i=1,y do
- local dpart = data.data[i]
- for j=1,x do
- local ppart = string.sub(dpart, 2*j - 1, 2*j)
- imdata[j][i] = {
- ["color"] = string.byte(string.sub(ppart, 1,1)),
- ["char"] = string.sub(ppart, 2, 2)
- }
- end
- end
- local obj = {}
- function obj.place(xpos, ypos, char)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- imdata[xpos][ypos].char = char
- return obj
- end
- function obj.setTextColor(xpos, ypos, color)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- if not index[color] then
- error('Invalid color!', 2)
- end
- local bg, _ = split(imdata[xpos][ypos].color)
- local fg = index[color]
- imdata[xpos][ypos].color = combine(bg, fg)
- return obj
- end
- function obj.setBackgroundColor(xpos, ypos, color)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- if not index[color] then
- error('Invalid color!', 2)
- end
- local _, fg = split(imdata[xpos][ypos].color)
- local bg = index[color]
- imdata[xpos][ypos].color = combine(bg, fg)
- return obj
- end
- function obj.export()
- local data = {}
- for i = 1, y do
- data[i] = ""
- for j = 1, x do
- data[i] = data[i] .. string.char(imdata[j][i].color) .. imdata[j][i].char
- end
- end
- return textutils.serialise(
- {
- ["x"] = x,
- ["y"] = y,
- ["data"] = data
- }
- )
- end
- function obj.get(xpos, ypos)
- if xpos > x then
- error('x pos too high!',2)
- end
- if ypos > y then
- error('y pos too high!', 2)
- end
- local bg, fg = split(imdata[xpos][ypos].color)
- return 2^bg, 2^fg, imdata[xpos][ypos].char
- end
- function obj.getSize()
- return x,y
- end
- return obj
- end
- function compress(data)
- local data = textutils.unserialise(data)
- local str = ""
- str = str .. data.x .. "s"
- str = str .. data.y .. "s"
- for i=1,data.y do
- str = str .. data.data[i]
- end
- return str
- end
- function uncompress(data)
- local tab = {}
- for s in string.gmatch(data, "([^s]+)") do
- table.insert(tab, s)
- end
- local rdata = {
- ["x"] = tonumber(tab[1]),
- ["y"] = tonumber(tab[2]),
- ["data"] = {}
- }
- local ndata = ""
- local amnt = #tab[1] + 2 + #tab[2]
- ndata = string.sub(data, amnt + 1)
- for i=1,rdata.y do
- rdata.data[i] = string.sub(ndata, rdata.x * i * 2 - rdata.x * 2 + 1, rdata.x * i * 2)
- end
- return textutils.serialise(
- rdata
- )
- end
Advertisement
Add Comment
Please, Sign In to add comment