Rahph

RIF

Jan 14th, 2018
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.92 KB | None | 0 0
  1. local index = {}
  2. for i=0,15 do
  3.     index[2^i] = i
  4. end
  5. local function combine(bg, fg)
  6.     return bit.blshift(bg, 4) + fg
  7. end
  8. local function split(color) local bg = bit.brshift(color, 4) local fgFactor = bg * 16 local fg = color - fgFactor return bg, fg end
  9. function image(x, y)
  10.     local imdata = {}
  11.     for i=1,x do
  12.         imdata[i] = {}
  13.         for j=1,y do
  14.             imdata[i][j] = {
  15.                 ["color"] = 0,
  16.                 ["char"] = " "
  17.             }
  18.         end
  19.     end
  20.     local obj = {}
  21.     function obj.place(xpos, ypos, char)
  22.         if xpos > x then
  23.             error('x pos too high!',2)
  24.         end
  25.         if ypos > y then
  26.             error('y pos too high!', 2)
  27.         end
  28.         imdata[xpos][ypos].char = char
  29.         return obj
  30.     end
  31.     function obj.setTextColor(xpos, ypos, color)
  32.         if xpos > x then
  33.             error('x pos too high!',2)
  34.         end
  35.         if ypos > y then
  36.             error('y pos too high!', 2)
  37.         end
  38.         if not index[color] then
  39.             error('Invalid color!', 2)
  40.         end
  41.         local bg, _ = split(imdata[xpos][ypos].color)
  42.         local fg = index[color]
  43.         imdata[xpos][ypos].color = combine(bg, fg)
  44.         return obj
  45.     end
  46.     function obj.setBackgroundColor(xpos, ypos, color)
  47.         if xpos > x then
  48.             error('x pos too high!',2)
  49.         end
  50.         if ypos > y then
  51.             error('y pos too high!', 2)
  52.         end
  53.         if not index[color] then
  54.             error('Invalid color!', 2)
  55.         end
  56.         local _, fg = split(imdata[xpos][ypos].color)
  57.         local bg = index[color]
  58.         imdata[xpos][ypos].color = combine(bg, fg)
  59.         return obj
  60.     end
  61.     function obj.export()
  62.         local data = {}
  63.         for i = 1, y do
  64.             data[i] = ""
  65.             for j = 1, x do
  66.                 data[i] = data[i] .. string.char(imdata[j][i].color) .. imdata[j][i].char
  67.             end
  68.         end
  69.         return textutils.serialise(
  70.             {
  71.                 ["x"] =  x,
  72.                 ["y"] =  y,
  73.                 ["data"] = data
  74.             }
  75.         )
  76.     end
  77.     function obj.get(xpos, ypos)
  78.         if xpos > x then
  79.             error('x pos too high!',2)
  80.         end
  81.         if ypos > y then
  82.             error('y pos too high!', 2)
  83.         end
  84.         local bg, fg = split(imdata[xpos][ypos].color)
  85.         return bg, fg, imdata[xpos][ypos].char
  86.     end
  87.     function obj.getSize()
  88.         return x,y
  89.     end
  90.     return obj
  91. end
  92. function load(data)
  93.     -- Parsing image starts here
  94.     local data = textutils.unserialise(data)
  95.     local imdata = {}
  96.     local x = data.x
  97.     local y = data.y
  98.     for i=1,x do
  99.         imdata[i] = {}
  100.         for j=1,y do
  101.             imdata[i][j] = {
  102.                 ["color"] = 0,
  103.                 ["char"] = " "
  104.             }
  105.         end
  106.     end
  107.     for i=1,y do
  108.         local dpart = data.data[i]
  109.         for j=1,x do
  110.             local ppart = string.sub(dpart, 2*j - 1, 2*j)
  111.             imdata[j][i] = {
  112.                 ["color"] = string.byte(string.sub(ppart, 1,1)),
  113.                 ["char"] = string.sub(ppart, 2, 2)
  114.             }
  115.         end
  116.     end
  117.     local obj = {}
  118.     function obj.place(xpos, ypos, char)
  119.         if xpos > x then
  120.             error('x pos too high!',2)
  121.         end
  122.         if ypos > y then
  123.             error('y pos too high!', 2)
  124.         end
  125.         imdata[xpos][ypos].char = char
  126.         return obj
  127.     end
  128.     function obj.setTextColor(xpos, ypos, color)
  129.         if xpos > x then
  130.             error('x pos too high!',2)
  131.         end
  132.         if ypos > y then
  133.             error('y pos too high!', 2)
  134.         end
  135.         if not index[color] then
  136.             error('Invalid color!', 2)
  137.         end
  138.         local bg, _ = split(imdata[xpos][ypos].color)
  139.         local fg = index[color]
  140.         imdata[xpos][ypos].color = combine(bg, fg)
  141.         return obj
  142.     end
  143.     function obj.setBackgroundColor(xpos, ypos, color)
  144.         if xpos > x then
  145.             error('x pos too high!',2)
  146.         end
  147.         if ypos > y then
  148.             error('y pos too high!', 2)
  149.         end
  150.         if not index[color] then
  151.             error('Invalid color!', 2)
  152.         end
  153.         local _, fg = split(imdata[xpos][ypos].color)
  154.         local bg = index[color]
  155.         imdata[xpos][ypos].color = combine(bg, fg)
  156.         return obj
  157.     end
  158.     function obj.export()
  159.         local data = {}
  160.         for i = 1, y do
  161.             data[i] = ""
  162.             for j = 1, x do
  163.                 data[i] = data[i] .. string.char(imdata[j][i].color) .. imdata[j][i].char
  164.             end
  165.         end
  166.         return textutils.serialise(
  167.             {
  168.                 ["x"] =  x,
  169.                 ["y"] =  y,
  170.                 ["data"] = data
  171.             }
  172.         )
  173.     end
  174.     function obj.get(xpos, ypos)
  175.         if xpos > x then
  176.             error('x pos too high!',2)
  177.         end
  178.         if ypos > y then
  179.             error('y pos too high!', 2)
  180.         end
  181.         local bg, fg = split(imdata[xpos][ypos].color)
  182.         return 2^bg, 2^fg, imdata[xpos][ypos].char
  183.     end
  184.     function obj.getSize()
  185.         return x,y
  186.     end
  187.     return obj
  188. end
  189. function compress(data)
  190.     local data = textutils.unserialise(data)
  191.     local str = ""
  192.     str = str .. data.x .. "s"
  193.     str = str .. data.y .. "s"
  194.     for i=1,data.y do
  195.         str = str .. data.data[i]
  196.     end
  197.     return str
  198. end
  199. function uncompress(data)
  200.     local tab = {}
  201.     for s in string.gmatch(data, "([^s]+)") do
  202.         table.insert(tab, s)
  203.     end
  204.     local rdata = {
  205.         ["x"] = tonumber(tab[1]),
  206.         ["y"] = tonumber(tab[2]),
  207.         ["data"] = {}
  208.     }
  209.     local ndata = ""
  210.     local amnt = #tab[1] + 2 + #tab[2]
  211.     ndata = string.sub(data, amnt + 1)
  212.     for i=1,rdata.y do
  213.         rdata.data[i] = string.sub(ndata, rdata.x * i * 2 - rdata.x * 2 + 1, rdata.x * i * 2)
  214.     end
  215.     return textutils.serialise(
  216.         rdata
  217.     )
  218. end
Advertisement
Add Comment
Please, Sign In to add comment