Dumfing

Image renderer

Jan 16th, 2021 (edited)
1,040
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. os = require("os")
  2. component = require("component")
  3. local io = require("io")
  4.  
  5.  
  6. file_in = io.open("input2.txt", "r")
  7. file_data = file_in:read("*all")
  8. file_in:close()
  9.  
  10. gpu1 = component.proxy(component.get("c295"))
  11.  
  12. screen = component.proxy(component.get("3bc7"))
  13.  
  14. gpu1.bind(screen.address)
  15.  
  16.  
  17. function ssplit (inputstr, sep)
  18.         if sep == nil then
  19.                 sep = "%s"
  20.         end
  21.         local t={}
  22.         for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  23.                 table.insert(t, str)
  24.         end
  25.         return t
  26. end
  27.  
  28. function get_colour(r_char, g_char, b_char)
  29.   r = math.floor(((string.byte(r_char) - string.byte("!"))/94)*255)
  30.   g = math.floor(((string.byte(g_char) - string.byte("!"))/94)*255)
  31.   b = math.floor(((string.byte(b_char) - string.byte("!"))/94)*255)
  32.   combined = (r << 16) + (g << 8) + b
  33.   -- print(r, g, b, combined)
  34.   return combined
  35. end
  36.  
  37. function draw_image(to_draw)
  38.   file_sections = ssplit(to_draw, " ")
  39.   image_width = tonumber(file_sections[1])
  40.   data = file_sections[2]
  41.   image_height = string.len(data)/image_width/3
  42.   print(image_width, image_height)
  43.   print(string.len(data))
  44.   -- gpu1.setActiveBuffer(1)
  45.   for y=0,image_height-1,2 do
  46.     for x=0, image_width-1 do
  47.       top_colour_r = y * image_width * 3 + (x*3) + 1
  48.       bottom_colour_r = (y+1) * image_width * 3 + (x*3) + 1
  49.       -- print(top_colour_r)
  50.       colour = get_colour(string.sub(data, top_colour_r, top_colour_r), string.sub(data, top_colour_r+1, top_colour_r+1), string.sub(data, top_colour_r + 2, top_colour_r + 2))
  51.       gpu1.setForeground(colour)
  52.       if bottom_colour_r < string.len(data) then
  53.         colour2 = get_colour(string.sub(data, bottom_colour_r, bottom_colour_r), string.sub(data, bottom_colour_r+1, bottom_colour_r+1), string.sub(data, bottom_colour_r + 2, bottom_colour_r + 2))
  54.         gpu1.setBackground(colour2)
  55.       end
  56.       gpu1.set(x, y/2, "▀")    
  57.     end
  58.   end
  59. end
  60.  
  61. -- draw_image(file_data)
RAW Paste Data