Advertisement
Pirnogion

imageio

Jun 10th, 2015
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.74 KB | None | 0 0
  1. --[[Example img
  2. local img =
  3. {
  4.     { { 0x100000, 0x100000, "F" }, { 0x200000, 0x200000, "N" }, { 0x300000, 0xa00000, "l" } },
  5.     { { 0x00f000, 0x000000, "C" }, { 0x000000, 0x000000, "t" }, { 0x000000, 0x000000, "k" } },
  6.     { { 0x000000, 0x000000, "H" }, { 0x000000, 0x000000, "f" }, { 0xffffff, 0x000000, "c" } }
  7. }
  8. ]]
  9.  
  10. local colorlib = require "colorlib"
  11.  
  12. local fileio = {}
  13.  
  14. --Encode
  15. local function depth8Encode(bgcolor, fgcolor, char)
  16.     local bgr, bgg, bgb = colorlib.HEXtoRGB(bgcolor)
  17.     local fgr, fgg, fgb = colorlib.HEXtoRGB(fgcolor)
  18.  
  19.     bgr, bgg, bgb = string.char(bgr), string.char(bgg), string.char(bgb)
  20.     fgr, fgg, fgb = string.char(fgr), string.char(fgg), string.char(fgb)
  21.  
  22.     return bgr, bgg, bgb, fgr, fgg, fgb, char
  23. end
  24.  
  25. --Decode
  26. local function depth8Decode(code) --BBBFFF[C]
  27.     local BGColor = colorlib.RGBtoHEX( code:byte(1), code:byte(2), code:byte(3) )
  28.     local FGColor = colorlib.RGBtoHEX( code:byte(4), code:byte(5), code:byte(6) )
  29.     local Char = code:sub(-1)
  30.  
  31.     return BGColor, FGColor, Char
  32. end
  33.  
  34. ----------------------------------------------------------------------------------
  35. function fileio.read(sFile)
  36.     local tImg = {}
  37.  
  38.     local f = io.open(sFile, "r")
  39.     local xlen, data = f:read("*l"), f:read("*l")
  40.     local counter = 0
  41.  
  42.     for pixel in data:gmatch(".......") do
  43.         if ( counter == 0 ) then tImg[#tImg] = {} end
  44.         tImg[#tImg][counter+1] = {depth8Decode(pixel)}
  45.         counter = (counter+1) % xlen
  46.     end
  47.  
  48.     f:close()
  49.  
  50.     return tImg
  51. end
  52.  
  53. function fileio.write(sFile, tImg)
  54.     local f = io.open(sFile, "w")
  55.     local ylen, xlen = #tImg, #tImg[1]
  56.  
  57.     f:write(xlen, "\n")
  58.  
  59.     for y = 1, ylen, 1 do
  60.         for x = 1, xlen, 1 do
  61.             f:write( depth8Encode(tImg[y][x][1], tImg[y][x][2], tImg[y][x][3]) )
  62.         end
  63.     end
  64.  
  65.     f:close()
  66. end
  67.  
  68. return fileio
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement