Advertisement
Freack100

Some Random Image API

Aug 11th, 2015
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. local function log2(num)
  2.     return math.log(num)/math.log(2)
  3. end
  4.  
  5. local function dump(tbl, indent)
  6.     local indent = indent or 0
  7.     for k,v in pairs(tbl) do
  8.         if(type(v) == "table") then
  9.             dump(v,indent+1)
  10.         else
  11.             print(string.rep(" ",indent)..k.. " "..v)
  12.         end
  13.     end
  14. end
  15.  
  16. -------------------------------------------
  17. -- Thanks for this, Lignum :)            --
  18. -------------------------------------------
  19. local function unify(bgColour, fgColour)
  20.   local bg = log2(bgColour)
  21.   local fg = log2(fgColour)
  22.  
  23.   local field = bit.bor(bg, bit.blshift(fg, 4))
  24.   return field
  25. end
  26.  
  27. local function extractColors(bitfield)
  28.   local bg = 2^(bit.band(bitfield, 0xF))
  29.   local fg = 2^(bit.brshift(bitfield, 4))
  30.   return bg, fg
  31. end
  32. -------------------------------------------
  33.  
  34.  
  35. -------------------------------------------
  36. -- Intended for conversion and stuff.    --
  37. -- But not yet implemented.              --
  38. -------------------------------------------
  39. local colorChars = "0123456789abcdef"
  40.  
  41. local function getCharForColor(col)
  42.     local c = log2(col)+1
  43.     return colorChars:sub(c,c)
  44. end
  45.  
  46. local function getColorForChar(ch)
  47.     for i = 1,16 do
  48.         if(colorChars:sub(i,i) == ch) then
  49.             return 2^(i-1)
  50.         end
  51.     end
  52. end
  53. -------------------------------------------
  54.  
  55. local function getDimension(data)
  56.     local h = #data
  57.     local w = 0
  58.     for k,v in pairs(data) do
  59.         if(#v>w) then w = #v end
  60.     end
  61.     return w,h
  62. end
  63.  
  64. local function isValid(data)
  65.     local w = #data[1] --This is the reference width. Everything which is not equal to w is not valid.
  66.     for k,v in pairs(data) do
  67.         if(#v ~= w) then
  68.             printError("Row "..k.." has an invalid length. Expected: "..w.." Got: "..#v)
  69.             return false
  70.         end
  71.     end
  72.     return true
  73. end
  74.  
  75. function load(path)
  76.     local content = {}
  77.     local handle = fs.open(path,"rb")
  78.     local currentLine = {}
  79.     local isChar = false
  80.     local w,h = handle.read(),handle.read()
  81.     local linePos = 0
  82.     for byte in handle.read do
  83.         if(isChar) then
  84.             currentLine[#currentLine][3] = string.char(byte)
  85.             isChar = false
  86.             linePos = linePos + 1
  87.         else
  88.             currentLine[#currentLine+1] = {extractColors(byte)}
  89.             isChar = true
  90.         end
  91.         if(linePos == w) then
  92.             table.insert(content,currentLine)
  93.             currentLine = {}
  94.             isChar = false
  95.             linePos = 0
  96.         end
  97.     end
  98.     print(#content)
  99.     handle.close()
  100.     return content
  101. end
  102.  
  103. function draw(img)
  104.     for y,line in pairs(img) do
  105.         for x,char in pairs(line) do
  106.             term.setCursorPos(x,y)
  107.             term.setBackgroundColor(char[1])
  108.             term.setTextColor(char[2])
  109.             term.write(char[3])
  110.         end
  111.     end
  112. end
  113.  
  114. function save(data,file)
  115.     local handle = fs.open(file,"wb")
  116.     local w,h = getDimension(data)
  117.     handle.write(w)
  118.     handle.write(h)
  119.     for _y,y in pairs(data) do
  120.         for _x,x in pairs(y) do
  121.             handle.write(unify(x[1],x[2]))
  122.             handle.write(x[3] ~= nil and string.byte(x[3]) or string.byte(" "))
  123.         end
  124.     end
  125.     handle.close()
  126. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement