Advertisement
MoonlightOwl

ToShow Viewer

Jan 27th, 2015
1,577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | None | 0 0
  1. --[[
  2.   Advanced PNG ToShow
  3.   (PNG decoding lib by TehSomeLuigi, 2014)
  4.  
  5.   PNG images Viewer by Totoro, 2015
  6. ]]--
  7.  
  8. local args = {...}
  9.  
  10. local event = require("event")
  11. local term = require("term")
  12. local fs = require("filesystem")
  13. local shell = require("shell")
  14. local computer = require('computer')
  15. local bit = require("bit32")
  16. local PNGImage = require("libPNGimage")
  17. local com = require("component")
  18. local gpu = com.gpu
  19.  
  20. local out = io.stdout
  21. local err = io.stderr
  22.  
  23. local color = {}
  24. color.fore = 0xFFFFFF
  25. color.back = 0x000000
  26. local refreshtime = 300
  27.  
  28. if not args[1] then
  29.   print("Enter filename of PNG Image:")
  30.   io.stdout:write(": ")
  31.   args[1] = io.read()
  32. elseif args[1] == "-h" or args[1] == "--help" or args[1] == "-?" then
  33.   print("Usage: toshow <filename>")
  34. end
  35.  
  36. args[1] = shell.resolve(args[1], "png")
  37.  
  38. if not fs.exists(args[1]) then
  39.   err:write("[ToShow Error]\n")
  40.   err:write("The file '" .. tostring(args[1]) .. "' does not exist.\n")
  41.   return
  42. end
  43.  
  44. -- now attempt to load the PNG image
  45. -- run in protected call to handle potential errors
  46.  
  47. local success, pngi = pcall(PNGImage.newFromFile, args[1])
  48.  
  49. if not success then
  50.   err:write("[ToShow: PNG Loading Error]\n")
  51.   err:write("While attempting to load '" .. tostring(args[1]) .. "' as PNG, libPNGImage erred:\n")
  52.   err:write(pngi)
  53.   return
  54. end
  55.  
  56. local imgW, imgH = pngi:getSize()
  57. local maxresW, maxresH = gpu.maxResolution()
  58.  
  59. if imgW > maxresW then
  60.   err:write("[ToShow: PNG Display Error]\n")
  61.   err:write("A width resolution of at least " .. imgW .. " is required, only " .. maxresW .. " available.\n")
  62.   return
  63. end
  64.  
  65. if imgH > maxresH*2 then
  66.   err:write("[ToShow: PNG Display Error]\n")
  67.   err:write("A height resolution of at least " .. imgH .. " is required, only " .. maxresH .. " available.\n")
  68.   return
  69. end
  70.  
  71. -- draw loaded image
  72.  
  73. local oldResW, oldResH = gpu.getResolution() -- store for later
  74. local oldBackN, oldBackB = gpu.getBackground()
  75. local oldForeN, oldForeB = gpu.getForeground()
  76.  
  77. local block = '▀'
  78. local H = math.ceil(imgH/2)
  79.  
  80. gpu.setBackground(color.back, false)
  81. gpu.setResolution(imgW, H)
  82.  
  83. function rgb2hex(r,g,b)
  84.   return r*65536+g*256+b
  85. end
  86.  
  87. function draw()
  88.   for x = 0, imgW-1 do
  89.     local dy = 1
  90.     for y = 0, imgH-1, 2 do
  91.       -- upper half
  92.       local r, g, b, a = pngi:getPixel(x, y)
  93.       if a > 0 then
  94.         gpu.setForeground(bit.bor(bit.lshift(r, 16), bit.bor(bit.lshift(g, 8), b)))
  95.       else
  96.         gpu.setForeground(color.back)
  97.       end
  98.       -- lower half
  99.       if (y+1) < imgH then
  100.         r, g, b, a = pngi:getPixel(x, y+1)
  101.         if a > 0 then
  102.           gpu.setBackground(bit.bor(bit.lshift(r, 16), bit.bor(bit.lshift(g, 8), b)))
  103.         else
  104.           gpu.setBackground(color.back)
  105.         end
  106.       else
  107.         gpu.setBackground(color.back)
  108.       end
  109.       -- output
  110.       gpu.set(x+1, dy, block)
  111.       dy = dy + 1
  112.     end
  113.   end
  114. end
  115.  
  116. draw()
  117.  
  118. time = computer.uptime()
  119. while true do
  120.   name, add, x, y = event.pull(10)
  121.   if name == 'key_down' then break
  122.   elseif name == "touch" then
  123.     --gpu.set(x, y, "*")
  124.   end
  125.   if computer.uptime() - time > 60 then
  126.     gpu.set(1,1,'_')
  127.     gpu.set(1,1,' ')
  128.     time = computer.uptime()
  129.   end
  130. end
  131.  
  132. -- restore old resolution and colors
  133. gpu.setForeground(oldForeN, oldForeB)
  134. gpu.setBackground(oldBackN, oldBackB)
  135. term.clear()
  136. gpu.setResolution(oldResW, oldResH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement