Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function log2(num)
- return math.log(num)/math.log(2)
- end
- local function dump(tbl, indent)
- local indent = indent or 0
- for k,v in pairs(tbl) do
- if(type(v) == "table") then
- dump(v,indent+1)
- else
- print(string.rep(" ",indent)..k.. " "..v)
- end
- end
- end
- -------------------------------------------
- -- Thanks for this, Lignum :) --
- -------------------------------------------
- local function unify(bgColour, fgColour)
- local bg = log2(bgColour)
- local fg = log2(fgColour)
- local field = bit.bor(bg, bit.blshift(fg, 4))
- return field
- end
- local function extractColors(bitfield)
- local bg = 2^(bit.band(bitfield, 0xF))
- local fg = 2^(bit.brshift(bitfield, 4))
- return bg, fg
- end
- -------------------------------------------
- -------------------------------------------
- -- Intended for conversion and stuff. --
- -- But not yet implemented. --
- -------------------------------------------
- local colorChars = "0123456789abcdef"
- local function getCharForColor(col)
- local c = log2(col)+1
- return colorChars:sub(c,c)
- end
- local function getColorForChar(ch)
- for i = 1,16 do
- if(colorChars:sub(i,i) == ch) then
- return 2^(i-1)
- end
- end
- end
- -------------------------------------------
- local function getDimension(data)
- local h = #data
- local w = 0
- for k,v in pairs(data) do
- if(#v>w) then w = #v end
- end
- return w,h
- end
- local function isValid(data)
- local w = #data[1] --This is the reference width. Everything which is not equal to w is not valid.
- for k,v in pairs(data) do
- if(#v ~= w) then
- printError("Row "..k.." has an invalid length. Expected: "..w.." Got: "..#v)
- return false
- end
- end
- return true
- end
- function load(path)
- local content = {}
- local handle = fs.open(path,"rb")
- local currentLine = {}
- local isChar = false
- local w,h = handle.read(),handle.read()
- local linePos = 0
- for byte in handle.read do
- if(isChar) then
- currentLine[#currentLine][3] = string.char(byte)
- isChar = false
- linePos = linePos + 1
- else
- currentLine[#currentLine+1] = {extractColors(byte)}
- isChar = true
- end
- if(linePos == w) then
- table.insert(content,currentLine)
- currentLine = {}
- isChar = false
- linePos = 0
- end
- end
- print(#content)
- handle.close()
- return content
- end
- function draw(img)
- for y,line in pairs(img) do
- for x,char in pairs(line) do
- term.setCursorPos(x,y)
- term.setBackgroundColor(char[1])
- term.setTextColor(char[2])
- term.write(char[3])
- end
- end
- end
- function save(data,file)
- local handle = fs.open(file,"wb")
- local w,h = getDimension(data)
- handle.write(w)
- handle.write(h)
- for _y,y in pairs(data) do
- for _x,x in pairs(y) do
- handle.write(unify(x[1],x[2]))
- handle.write(x[3] ~= nil and string.byte(x[3]) or string.byte(" "))
- end
- end
- handle.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement