Advertisement
hbar

lumiere

Nov 30th, 2013
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.44 KB | None | 0 0
  1. local m = peripheral.wrap("back")
  2.  
  3. local WIDTH = 16
  4. local HEIGHT = 16
  5.  
  6. local SPEED = 0.1
  7.  
  8. if not http then
  9.   print("HTTP must be enabled.")
  10.   return
  11. end
  12.  
  13.  
  14. local VERSION = "0.1.0"
  15.  
  16. print("Lumiere v "..VERSION)
  17.  
  18. local apiURL = "http://lakka.kapsi.fi:62096"
  19. local playerURL = "http://s3.amazonaws.com/MinecraftSkins/"
  20.  
  21. local perplist = m.getNamesRemote()
  22.  
  23. local min_id = 1000000000000
  24.  
  25. for _,name in pairs(perplist) do
  26.   if name:find("glowstone_illuminator") then
  27.     local id = tonumber(name:sub(23,#name))
  28.     if id < min_id then
  29.       min_id = id
  30.     end
  31.   end
  32. end
  33.  
  34.  
  35. local illuminators = {}
  36. for i = min_id,min_id+HEIGHT*WIDTH-1 do
  37.   local l = peripheral.wrap("glowstone_illuminator_"..tostring(i))
  38.   table.insert(illuminators,l)
  39. end
  40.  
  41. local index = function(x,y)
  42.   if x%2 == 1 then
  43.     return HEIGHT*(x-1)+(HEIGHT-y)+1
  44.   else
  45.     return HEIGHT*(x-1)+y
  46.   end
  47. end
  48.  
  49. local display = function(image,w,h)
  50.   for r = 1,HEIGHT do
  51.     for c = 1,WIDTH do
  52.       if r <= h and c <= w then
  53.         illuminators[index(c,r)].setColor(tonumber(image[r]:sub(1+8*(c-1),8+8*(c-1)),16))
  54.       else
  55.         illuminators[index(c,r)].setColor(0xFFFFFF)
  56.       end
  57.     end
  58.   end
  59. end
  60.  
  61. -- Returns HEX representation of num
  62. -- posted in snipplr.com by ukpyr
  63. function num2hex(num)
  64.     local hexstr = '0123456789abcdef'
  65.     local s = ''
  66.     while num > 0 do
  67.         local mod = math.fmod(num, 16)
  68.         s = string.sub(hexstr, mod+1, mod+1) .. s
  69.         num = math.floor(num / 16)
  70.     end
  71.     if s == '' then
  72.       s = '00'
  73.     elseif #s < 2 then
  74.       s = '0'..s
  75.     end
  76.     return s
  77. end
  78.  
  79. local loadimg = function(name)
  80.   img = {}
  81.  
  82.   local f = fs.open(name..".txt","r")
  83.  
  84.   line = f.readLine()
  85.   while line ~= nil do
  86.     table.insert(img,line)
  87.     line = f.readLine()
  88.   end
  89.   f.close()
  90.   return img
  91. end
  92.  
  93. local getImage = function(url,size)
  94.   vars = "url="..url
  95.   vars = vars.."&maxsize="..tostring(size)
  96.   return http.post(apiURL,vars)
  97. end
  98.  
  99. local getGIF = function(url,size)
  100.   vars = "url="..url
  101.   vars = vars.."&maxsize="..tostring(size)
  102.   vars = vars.."&animate=true"
  103.   return http.post(apiURL,vars)
  104. end
  105.  
  106.  
  107. local splitGIF = function(img)
  108.   frames = {}
  109.   frame = {}
  110.   for _,line in pairs(img) do
  111.     if line == "~" then
  112.       table.insert(frames,frame)
  113.       frame = {}
  114.     else
  115.       table.insert(frame,line)
  116.     end
  117.   end
  118.   return frames
  119. end
  120.  
  121. local displayAnimation = function (frames,w,h)
  122.   for i = 1,#frames do
  123.     display(frames[i],w,h)
  124.     sleep(SPEED)
  125.   end
  126. end
  127.  
  128. local fader = {}
  129.  
  130. local run = function(self,steps,dt)
  131.  
  132.   for i = 1,steps do
  133.     local current = {}
  134.     for r = 1,16 do
  135.       line = ""
  136.       for c = 1,16 do
  137.         local s_px = self.start[r]:sub(1+8*(c-1),8+8*(c-1))
  138.         local e_px = self.final[r]:sub(1+8*(c-1),8+8*(c-1))
  139.         local s_r, s_g, s_b = tonumber(s_px:sub(3,4),16), tonumber(s_px:sub(5,6),16), tonumber(s_px:sub(7,8),16)
  140.         local e_r, e_g, e_b = tonumber(e_px:sub(3,4),16), tonumber(e_px:sub(5,6),16), tonumber(e_px:sub(7,8),16)
  141.         local d_r = ((e_r-s_r)/(steps-1))*(i-1)
  142.         local d_g = ((e_g-s_g)/(steps-1))*(i-1)
  143.         local d_b = ((e_b-s_b)/(steps-1))*(i-1)
  144.         local c_r, c_g, c_b = s_r + d_r, s_g + d_g, s_b + d_b
  145.         line = line.."0x"..num2hex(c_r)..num2hex(c_g)..num2hex(c_b)
  146.       end
  147.       table.insert(current,line)
  148.     end
  149.     display(current)
  150.     sleep(dt)
  151.   end
  152. end
  153.  
  154. fader.run = run
  155.  
  156. print "URL:"
  157. local url = io.read()
  158.  
  159. local size = math.max(HEIGHT,WIDTH)
  160.  
  161. local resp = getGIF(url,size)
  162.  
  163. if not resp then
  164.   print("No response from image server.")
  165.   return
  166. elseif resp.getResponseCode() ~= 200 then
  167.   print("Error while connecting to server!")
  168.   print("Server response")
  169.   print("Code: "..resp.getResponseCode())
  170.   print("Message:")
  171.   print(resp.readAll())
  172.   return
  173. end
  174.  
  175. local oldSize = resp.readLine()
  176. local newSize = resp.readLine()
  177.  
  178. if oldSize ~= newSize then
  179.   print("Original image size was (w,h): "..oldSize)
  180.   print("New size is (w,h): "..newSize)
  181. else
  182.   print("Image size is (w,h): "..newSize)
  183. end
  184.  
  185. local sizes = {}
  186. for word in newSize:gmathc("%w+") do table.insert(sizes, word) end
  187.  
  188. local picWidth = tonumber(sizes[1])
  189. local picHeight = tonumber(sizes[2])
  190.  
  191. local img = {}
  192.  
  193. local line = resp.readLine()
  194. while line ~= nil do
  195.   table.insert(img,line)
  196.   line = resp.readLine()
  197. end
  198. resp.close()
  199.  
  200. while true do
  201.   displayAnimation(splitGIF(img),picWidth,picHeight)
  202.   --sleep(1)
  203. end
  204.  
  205. --display(img)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement