Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[Example img
- local img =
- {
- { { 0x100000, 0x100000, "F" }, { 0x200000, 0x200000, "N" }, { 0x300000, 0xa00000, "l" } },
- { { 0x00f000, 0x000000, "C" }, { 0x000000, 0x000000, "t" }, { 0x000000, 0x000000, "k" } },
- { { 0x000000, 0x000000, "H" }, { 0x000000, 0x000000, "f" }, { 0xffffff, 0x000000, "c" } }
- }
- ]]
- local colorlib = require "colorlib"
- local fileio = {}
- --Encode
- local function depth8Encode(bgcolor, fgcolor, char)
- local bgr, bgg, bgb = colorlib.HEXtoRGB(bgcolor)
- local fgr, fgg, fgb = colorlib.HEXtoRGB(fgcolor)
- bgr, bgg, bgb = string.char(bgr), string.char(bgg), string.char(bgb)
- fgr, fgg, fgb = string.char(fgr), string.char(fgg), string.char(fgb)
- return bgr, bgg, bgb, fgr, fgg, fgb, char
- end
- --Decode
- local function depth8Decode(code) --BBBFFF[C]
- local BGColor = colorlib.RGBtoHEX( code:byte(1), code:byte(2), code:byte(3) )
- local FGColor = colorlib.RGBtoHEX( code:byte(4), code:byte(5), code:byte(6) )
- local Char = code:sub(-1)
- return BGColor, FGColor, Char
- end
- ----------------------------------------------------------------------------------
- function fileio.read(sFile)
- local tImg = {}
- local f = io.open(sFile, "r")
- local xlen, data = f:read("*l"), f:read("*l")
- local counter = 0
- for pixel in data:gmatch(".......") do
- if ( counter == 0 ) then tImg[#tImg] = {} end
- tImg[#tImg][counter+1] = {depth8Decode(pixel)}
- counter = (counter+1) % xlen
- end
- f:close()
- return tImg
- end
- function fileio.write(sFile, tImg)
- local f = io.open(sFile, "w")
- local ylen, xlen = #tImg, #tImg[1]
- f:write(xlen, "\n")
- for y = 1, ylen, 1 do
- for x = 1, xlen, 1 do
- f:write( depth8Encode(tImg[y][x][1], tImg[y][x][2], tImg[y][x][3]) )
- end
- end
- f:close()
- end
- return fileio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement