Advertisement
Guest User

Image To Glasses Lua Un-efficient

a guest
Jan 18th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.31 KB | None | 0 0
  1. -- Define your web directory here:
  2. local webDirectoy = "http://example.com/Glasses/"
  3. -- Change to 'true' if you are using a small image and can manage to fit it in one volume on the computer
  4. --      Note: If the file header specifies multiple volumes this setting will be ignored
  5. local localDirectory = true
  6.  
  7. -- Change to the name of the image file if you plan to launch the program at startup
  8.     local fileName = "someFile"
  9.  
  10. -- Glasses Bridge Side
  11.     local side = "top"
  12.  
  13. local g = peripheral.wrap(side)
  14. local function b(x,y,leng,hex,opacity)
  15. -- g.addBox(x,y,leng,1,hex,opacity)
  16. end
  17. --g.clear()
  18.  
  19. -- Some UTF-8 functions have been taken and adapted from Kyle Smith's library at https://github.com/Stepets/utf8.lua
  20. local byte = string.byte
  21. local len = string.len
  22. local sub = string.sub
  23. local shift_6 = 2^6
  24. local shift_12 = 2^12
  25. local shift_18 = 2^18
  26. local function utf8charbytes(s, i)
  27.   -- argument defaults
  28.   i = i or 1
  29.   -- argument checking
  30.   if type(s) ~= "string" then
  31.     error("bad argument #1 to 'utf8charbytes' (string expected, got ".. type(s).. ")")
  32.   end
  33.   if type(i) ~= "number" then
  34.     error("bad argument #2 to 'utf8charbytes' (number expected, got ".. type(i).. ")")
  35.   end
  36.   local c = byte(s, i)
  37.   -- determine bytes needed for character, based on RFC 3629
  38.   -- validate byte 1
  39.   if c > 0 and c <= 127 then
  40.     -- UTF8-1
  41.     return 1
  42.   elseif c >= 194 and c <= 223 then
  43.     -- UTF8-2
  44.     local c2 = byte(s, i + 1)
  45.     if not c2 then
  46.       error("UTF-8 string terminated early")
  47.     end
  48.     -- validate byte 2
  49.     if c2 < 128 or c2 > 191 then
  50.       error("Invalid UTF-8 character")
  51.     end
  52.     return 2
  53.   elseif c >= 224 and c <= 239 then
  54.     -- UTF8-3
  55.     local c2 = byte(s, i + 1)
  56.     local c3 = byte(s, i + 2)
  57.     if not c2 or not c3 then
  58.       error("UTF-8 string terminated early")
  59.     end
  60.     -- validate byte 2
  61.     if c == 224 and (c2 < 160 or c2 > 191) then
  62.       error("Invalid UTF-8 character")
  63.     elseif c == 237 and (c2 < 128 or c2 > 159) then
  64.       error("Invalid UTF-8 character")
  65.     elseif c2 < 128 or c2 > 191 then
  66.       error("Invalid UTF-8 character")
  67.     end
  68.     -- validate byte 3
  69.     if c3 < 128 or c3 > 191 then
  70.       error("Invalid UTF-8 character")
  71.     end
  72.     return 3
  73.   elseif c >= 240 and c <= 244 then
  74.     -- UTF8-4
  75.     local c2 = byte(s, i + 1)
  76.     local c3 = byte(s, i + 2)
  77.     local c4 = byte(s, i + 3)
  78.     if not c2 or not c3 or not c4 then
  79.       error("UTF-8 string terminated early")
  80.     end
  81.     -- validate byte 2
  82.     if c == 240 and (c2 < 144 or c2 > 191) then
  83.       error("Invalid UTF-8 character")
  84.     elseif c == 244 and (c2 < 128 or c2 > 143) then
  85.       error("Invalid UTF-8 character")
  86.     elseif c2 < 128 or c2 > 191 then
  87.       error("Invalid UTF-8 character")
  88.     end
  89.     -- validate byte 3
  90.     if c3 < 128 or c3 > 191 then
  91.       error("Invalid UTF-8 character")
  92.     end
  93.     -- validate byte 4
  94.     if c4 < 128 or c4 > 191 then
  95.       error("Invalid UTF-8 character")
  96.     end
  97.     return 4
  98.   else
  99.     error("Invalid UTF-8 character")
  100.   end
  101. end
  102.  
  103. local function utf8sub(s, i, j)
  104.   -- argument defaults
  105.   j = j or -1
  106.   local pos = 1
  107.   local bytes = len(s)
  108.   local length = 0
  109.   -- only set l if i or j is negative
  110.   local l = (i >= 0 and j >= 0) or utf8len(s)
  111.   local startChar = (i >= 0) and i or l + i + 1
  112.   local endChar   = (j >= 0) and j or l + j + 1
  113.   -- can't have start before end!
  114.   if startChar > endChar then
  115.     return ""
  116.   end
  117.   -- byte offsets to pass to string.sub
  118.   local startByte,endByte = 1,bytes
  119.   while pos <= bytes do
  120.     length = length + 1
  121.     if length == startChar then
  122.       startByte = pos
  123.     end
  124.     pos = pos + utf8charbytes(s, pos)
  125.     if length == endChar then
  126.       endByte = pos - 1
  127.       break
  128.     end
  129.   end
  130.   if startChar > length then startByte = bytes+1   end
  131.   if endChar   < 1      then endByte   = 0         end
  132.   return sub(s, startByte, endByte)
  133. end
  134.  
  135. local function utf8unicode(str, i, j, byte_pos)
  136.   i = i or 1
  137.   j = j or i
  138.   if i > j then return end
  139.   local ch,bytes
  140.   if byte_pos then
  141.     bytes = utf8charbytes(str,byte_pos)
  142.     ch  = sub(str,byte_pos,byte_pos-1+bytes)
  143.   else
  144.     ch,byte_pos = utf8sub(str,i,i), 0
  145.     bytes       = #ch
  146.   end
  147.   local unicode
  148.   if bytes == 1 then unicode = byte(ch) end
  149.   if bytes == 2 then
  150.     local byte0,byte1 = byte(ch,1,2)
  151.     local code0,code1 = byte0-0xC0,byte1-0x80
  152.     unicode = code0*shift_6 + code1
  153.   end
  154.   if bytes == 3 then
  155.     local byte0,byte1,byte2 = byte(ch,1,3)
  156.     local code0,code1,code2 = byte0-0xE0,byte1-0x80,byte2-0x80
  157.     unicode = code0*shift_12 + code1*shift_6 + code2
  158.   end
  159.   if bytes == 4 then
  160.     local byte0,byte1,byte2,byte3 = byte(ch,1,4)
  161.     local code0,code1,code2,code3 = byte0-0xF0,byte1-0x80,byte2-0x80,byte3-0x80
  162.     unicode = code0*shift_18 + code1*shift_12 + code2*shift_6 + code3
  163.   end
  164.   return unicode,bytes
  165. end
  166.   local function hexy(IN)
  167.     local B,K,O,I,D=16,"0123456789ABCDEF","",0
  168.     while IN>0 do
  169.       I=I+1
  170.       IN,D=math.floor(IN/B)/>/>/>,math.mod(IN,B)/>/>/>+1
  171.       O=string.sub(K,D,D)..O
  172.     end
  173.     return O
  174.   end
  175.  
  176.  
  177.   -- Misc. Vars
  178.   local data = ""
  179.   local trueLength = 1
  180.   volumes = 1
  181.   local pos = 1
  182.  
  183.   local function readData(file)
  184.     local file = fs.open(file, "r")
  185.     data = file.readAll()
  186.     file.close()
  187.   end
  188.  
  189.   -- Get Real length of file (Will add value to header, comment out and manually define if you don't want to wait
  190.  
  191.   readData(fileName)
  192.  
  193. for i=1, #data do
  194.     local charSize = #utf8sub(data,i,i)
  195.     if(charSize > 1) then
  196.       i = i+charSize
  197.       trueLength = trueLength + 1
  198.      -- print(i.." "..#data.." "..trueLength)
  199.     end
  200.   end
  201.  
  202.  
  203.   local function readVolumes()
  204.     local A,B = utf8unicode(data,pos)
  205.     pos = (pos+B)/>/>/>-(B-1)
  206.     volumes = (A-32)
  207.   end
  208.  
  209.  
  210.   local function readPos()
  211.     local X,Y
  212.     local first = true
  213.     local chars = 0
  214.     while chars < 2 do
  215.       local A,B = utf8unicode(data,pos)
  216.       if first then X = A else Y = A end
  217.       pos = (pos+B)/>/>/>-(B-1)
  218.       chars = chars+1
  219.       first = false
  220.     end
  221.     return (X-32),(Y-32)
  222.   end
  223.  
  224.  
  225.   local function readColor()
  226.     local hex = "0x"
  227.     local chars = 0
  228.     while chars < 3 do
  229.       local A,B = utf8unicode(data,pos)
  230.       A = hexy(A)
  231.       pos = (pos+B)/>/>/>-(B-1)
  232.       hex = hex..A
  233.       chars = chars+1
  234.     end
  235.     return hex
  236.   end
  237.  
  238.  
  239.   local function readAlpha()
  240.     local A,B = utf8unicode(data,pos)
  241.     pos = (pos+B)/>/>/>-(B-1)
  242.     return ((A-32)/255)
  243.   end
  244.  
  245.  
  246.   local function readLength()
  247.     local A,B = utf8unicode(data,pos)
  248.     pos = (pos+B)/>/>/>-(B-1)
  249.     return (A-32)
  250.   end
  251.  
  252.  
  253.   local function loadBox()
  254.     local X,Y = readPos()
  255.     local length,color,alpha = readLength(),readColor(),readAlpha()
  256.     print(X.." "..Y)
  257.     print(length)
  258.     print(color)
  259.     print(alpha)
  260. sleep(0.001)
  261.   end
  262.  
  263.   -- Program Start
  264.  
  265.   readVolumes()
  266.  
  267. -- Find another volume
  268.   if(volumes > 1) then
  269.     if(fs.exists("paster") == false) then shell.run("pastebin get hPNk9ZDj paster")
  270.    end
  271.   end
  272.  
  273. --print(pos.." "..trueLength)
  274.  
  275.   for j=0, volumes+1 do
  276.     while pos < trueLength do
  277.       loadBox()
  278.     end
  279.  
  280.     if(volumes > 1 and localDirectory == false) then
  281.       fs.delete(fileName)
  282.       shell.run("paster get "..fileName..j.." "..fileName.." "..webDirectory)
  283.     end
  284.   end
  285.  
  286.   -- Program End
  287.   --g.sync()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement