Advertisement
ChanServ

starfall img viewer (broken)

Sep 15th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.70 KB | None | 0 0
  1. --@client
  2.  
  3. local function perf(threshold)
  4.     return math.max(quotaAverage(), quotaUsed()) < quotaMax()*threshold
  5. end
  6.  
  7. -- Music
  8. local succ, ret = pcall(function()
  9.     bass.loadFile("pixels/music.ogg", "mono", function(obj, errno, errtxt)
  10.         if obj then
  11.             music = obj
  12.         else
  13.             errmsg = errtxt
  14.         end
  15.     end)
  16. end)
  17. if not succ then
  18.     errmsg = tostring(ret)
  19. end
  20.  
  21. -- Define colors
  22. colors = {
  23.     ["\xff\xff\xff"] = Color(255, 255, 255),
  24.     ["\x00\x00\x00"] = Color(0, 0, 0),
  25.     ["\xff\x5f\x00"] = Color(255, 95, 0),
  26.     ["\xff\xbf\x00"] = Color(255, 191, 0),
  27.     ["\xdf\xff\x00"] = Color(223, 255, 0),
  28.     ["\x7f\xff\x00"] = Color(127, 255, 0),
  29.     ["\x1f\xff\x00"] = Color(31, 255, 0),
  30.     ["\x00\xff\x3f"] = Color(0, 255, 63),
  31.     ["\x00\xff\x9f"] = Color(0, 255, 159),
  32.     ["\x00\xff\xff"] = Color(0, 255, 255),
  33.     ["\x00\x9f\xff"] = Color(0, 159, 255),
  34.     ["\x00\x3f\xff"] = Color(0, 63, 255),
  35.     ["\x1f\x00\xff"] = Color(31, 0, 255),
  36.     ["\x7f\x00\xff"] = Color(127, 0, 255),
  37.     ["\xdf\x00\xff"] = Color(223, 0, 255),
  38.     ["\xff\x00\xbf"] = Color(255, 0, 191),
  39.     ["\xff\x00\x5f"] = Color(255, 0, 95),
  40.     ["\xff\x00\x00"] = Color(255, 0, 0),
  41.     ["\x7f\x7f\x7f"] = Color(127, 127, 127)
  42. }
  43.  
  44. -- Load image
  45. local img = string.explode("\n", file.read("pixels/test_bars_h_big.ppm"))
  46. for i=1, #img-1 do
  47.     if img[i][1] == "#" then
  48.         table.remove(img, i)
  49.     end
  50. end
  51. if img[1] ~= "P6" then error("Bad image header") end
  52. w, h = unpack(string.explode(" ", img[2]))
  53. w, h = tonumber(w), tonumber(h)
  54. if not (w and h) then error("Could not read image dimensions") end
  55. if img[3] ~= "255" then error("Bad image byte size") end
  56. if #img ~= 4 then error("Too many newlines in image") end
  57. img_proc = {}
  58. --img[4] = string.rep("abcdefghi", w*h/3)
  59. for x=1, h do
  60.     img_proc[x] = {}
  61.     for y=1, w do
  62.         local pos = (x*y)*3
  63.         img_proc[x][y] = colors[string.sub(img[4], pos-2, pos)] or Color(31, 31, 31)
  64.     end
  65. end
  66. --printTable(img_proc[1])
  67.  
  68. -- Render
  69. hook.add("render", "init", function()
  70.     local rt = render.createRenderTarget("rt")
  71.     local size = 1024/math.max(w, h)
  72.     render.selectRenderTarget("rt")
  73.     for x=1, h do
  74.         --if not perf(0.9) then break end
  75.         for y=1, w do
  76.             render.setColor(img_proc[x][y])
  77.             render.drawRect(size*y, size*x, size, size)
  78.         end
  79.     end
  80.     render.selectRenderTarget()
  81.     hook.remove("render", "init")
  82. end)
  83.  
  84. hook.add("render", "render", function()
  85.     render.setRGBA(255, 255, 255, 255)
  86.     render.drawRectFast(0, 0, 512, 512)
  87.     render.setRenderTargetTexture("rt")
  88.     render.drawTexturedRectFast(0, 0, 512, 512)
  89.     render.setRenderTargetTexture()
  90. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement