Advertisement
Guest User

drawsif3.lua

a guest
Feb 16th, 2019
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.57 KB | None | 0 0
  1. function readInteger(hndl)
  2.   local buffer = hndl:read(4)
  3.   if buffer == nil or #buffer < 4 then
  4.     return nil
  5.   end
  6.  
  7.   local bytes = {}
  8.   bytes[1] = string.byte(buffer, 1)
  9.   bytes[2] = string.byte(buffer, 2)
  10.   bytes[3] = string.byte(buffer, 3)
  11.   bytes[4] = string.byte(buffer, 4)
  12.  
  13.   local num = bytes[1] + bit32.lshift(bytes[2], 8) + bit32.lshift(bytes[3], 16) + bit32.lshift(bytes[4], 24)
  14.   return num
  15. end
  16.  
  17. function mkBrairle(x1y1,x1y2,x1y3,x1y4,x2y1,x2y2,x3y2,x4y2)
  18.   local base = 0x2800
  19.   if x1y1 then
  20.     base = base + 0x01
  21.   end
  22.   if x1y2 then
  23.     base = base + 0x02
  24.   end
  25.   if x1y3 then
  26.     base = base + 0x04
  27.   end
  28.   if x1y4 then
  29.     base = base + 0x40
  30.   end
  31.   if x2y1 then
  32.     base = base + 0x08
  33.   end
  34.   if x2y2 then
  35.     base = base + 0x10
  36.   end
  37.   if x2y3 then
  38.     base = base + 0x20
  39.   end
  40.   if x2y4 then
  41.     base = base + 0x80
  42.   end
  43.   return require("unicode").char(base)
  44. end
  45.  
  46. function reverseTable(input)
  47.   local new = {}
  48.   for i=1,#input do
  49.     new[#input+1-i] = input[i]
  50.   end
  51.   return new
  52. end
  53.  
  54. function addColors(r,g,b)
  55.   local c = b
  56.   local t = math.pow(2,8)
  57.   c = t * g + c
  58.   t = math.pow(2,16)
  59.   c = t * r + c
  60.   return c
  61. end
  62. local gpu = require('component').gpu
  63. function splitByte(byte)
  64.   local data = {}
  65.   local remaining = byte
  66.   for i=7,0,-1 do
  67.     local toRm = math.pow(2, i)
  68.     if remaining - toRm > -1 then
  69.       remaining = remaining - toRm
  70.       data[i + 1] = true
  71.     else
  72.       data[i + 1] = false
  73.     end
  74.   end
  75.   return data
  76. end
  77.  
  78. function zdraw(path)
  79.   local handle = io.open(path, 'rb')
  80.   readInteger(handle)
  81.  
  82.   local readstart = readInteger(handle)
  83.   local w = readInteger(handle)
  84.   local h = readInteger(handle)
  85.  
  86.   local rawSegments = {}
  87.  
  88.   for j=1,h do
  89.     for i=1,w do
  90.       if rawSegments[i] == nil then
  91.         rawSegments[i] = {}
  92.       end
  93.       seg = handle:read(7)
  94.       rawSegments[i][j] = {}
  95.       for k=1,7 do
  96.         rawSegments[i][j][k] = seg:sub(k,k)
  97.       end
  98.     end
  99.   end
  100.  
  101.   for i=1,#rawSegments do
  102.     for j=1,#rawSegments[i] do
  103.       gpu.setBackground(addColors(
  104.         string.byte(rawSegments[i][j][1]),
  105.         string.byte(rawSegments[i][j][2]),
  106.         string.byte(rawSegments[i][j][3])))
  107.       gpu.setForeground(addColors(
  108.         string.byte(rawSegments[i][j][4]),
  109.         string.byte(rawSegments[i][j][5]),
  110.         string.byte(rawSegments[i][j][6])))
  111.      
  112.       local pix = splitByte(string.byte(rawSegments[i][j][7]))
  113.  --     pix = reverseTable(pix)
  114.       local char = mkBrairle(pix[1], pix[2], pix[3], pix[4], pix[5], pix[6], pix[7], pix[8])
  115.       gpu.set(i, j, char)
  116.     end
  117.   end
  118. end  
  119. local argv=  {...}
  120. zdraw(argv[1])
  121. print(mkBrairle(true,false,false,true,true))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement