Advertisement
Metalhead33

ComputerCraft TextImage

Nov 3rd, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.08 KB | None | 0 0
  1. -- ComputerCraft Screen Resolution: 51 x 19
  2. -- Out of this, we should be only using 51 x 18, leaving one row at the top or bottom
  3. -- This leaves us with 918 pixels in total to store.
  4.        
  5. -- Since a computercraft screen is 624x354 large...
  6. -- We can assume that each ComputerCraft "pixel" is actually 12.235294117647 x 18.631578947368 large.
  7. -- Or not... Each screen character/pixel is actually 6x9.
  8. -- What this means that we have 51*6 x 19*9 screens... in other words, 306 x 171.
  9.  
  10. -- Therefore, when we want to display pictures, we have to first fit it to the same aspect ratio as 306 x 171, then squeeze it down to 51 x 19.
  11.        
  12. os.loadAPI("corefunc")
  13.  
  14. function GetCoords(number,max_y) -- the maximum of Y (number of rows) is irrelevant to us. We only need to know how many columns are there.
  15.     local x = math.ceil(number/max_y)
  16.     local y = number % max_y
  17.     if y == 0 then
  18.         y = max_y
  19.     end
  20.     return x,y
  21. end
  22.  
  23. function TransCord(x,y,max_y) -- the maximum of X (number of rows) is once again, irrelevant to us. We only need to know how many columns are there.
  24.     return ((x*max_y)+y)
  25. end
  26.  
  27. -- These two functions are going to be important to us later on.
  28.  
  29. function PrintBwImage(bits,max_y) -- Black and white, 1-bit image
  30.     term.clear()
  31.     for key,value in ipairs(bits) do
  32.         local pos_y,pos_x = GetCoords(key,max_y)
  33.         term.setCursorPos(pos_x,pos_y)
  34.         if value == true then
  35.             term.setBackgroundColor(colors.white)
  36.         else
  37.             term.setBackgroundColor(colors.black)
  38.         end
  39.         term.write(" ")
  40.     end
  41.     term.setBackgroundColor(colors.black)
  42.     term.setTextColor(colors.white)
  43. end
  44.  
  45. function PrintCdImage(hexes,max_y) -- Coloured image. Unfortunately, ComputerCraft uses 16-bit integers for the 16 colours. Very wasteful, but fortunately I found a way to get around it.
  46.     term.clear()
  47.     for key,value in ipairs(hexes) do
  48.         local pos_y,pos_x = GetCoords(key,max_y)
  49.         term.setCursorPos(pos_x,pos_y)
  50.         term.setBackgroundColor(math.pow(2,value))
  51.         term.write(" ")
  52.     end
  53.     term.setBackgroundColor(colors.black)
  54.     term.setTextColor(colors.white)
  55. end
  56.  
  57. -- Here come "interlaced" images, which have backgrounds and foregrounds!
  58.  
  59. local grey = '\127'
  60.  
  61. function PrintGrImage(bits,max_y) -- Greyscale dithered image, with black, white, light grey and dakr grey.
  62.     term.clear()
  63.     local length = math.ceil(#bits/2)
  64.     for i=1,length do
  65.         local pos_y,pos_x = GetCoords(i,max_y)
  66.         term.setCursorPos(pos_x,pos_y)
  67.         if bits[(i*2)-1] == true then
  68.             term.setBackgroundColor(colors.white)
  69.             term.setTextColor(colors.black)
  70.         else
  71.             term.setBackgroundColor(colors.black)
  72.             term.setTextColor(colors.white)
  73.         end
  74.         if bits[i*2] == true then
  75.             term.write(grey)
  76.         else
  77.             term.write(" ")
  78.         end
  79.     end
  80.     term.setBackgroundColor(colors.black)
  81.     term.setTextColor(colors.white)
  82. end
  83.  
  84.  
  85. function PrintCsImage(hexes,max_y) -- Colour-dithered image, for lack of better term. Allows us to get more complex "colour combinations" by having... nevermind.
  86.     term.clear()
  87.     local length = math.ceil(#hexes/2)
  88.     for i=1,length do
  89.         local pos_y,pos_x = GetCoords(i,max_y)
  90.         term.setCursorPos(pos_x,pos_y)
  91.         term.setBackgroundColor(math.pow(2,hexes[(i*2)-1]))
  92.         term.setTextColor(math.pow(2,hexes[i*2]))
  93.         term.write(grey)
  94.     end
  95.     term.setBackgroundColor(colors.black)
  96.     term.setTextColor(colors.white)
  97. end
  98.  
  99. function OpenImage(path) -- This will not display it yet, just load its bytes into the memory and buffer it. Keep that in mind.
  100.     local h = fs.open(path, "rb")
  101.     if h == nil then
  102.         h.close()
  103.         return nil
  104.     end
  105.     local buf = { }
  106.     buf.bytes = { }
  107.     local c = h.read() -- We find out what mode the thing is.
  108.     if c == nil or c < 128 then
  109.         return nil
  110.     else buf.mode = (c - 128)
  111.     end
  112.     buf.width = h.read()
  113.     buf.height = h.read()
  114.     local i = 1
  115.     while true do
  116.         c = h.read()
  117.         if c == nil then
  118.             h.close()
  119.             break
  120.         else
  121.             buf.bytes[i] = c
  122.         end
  123.     end
  124.     return buf
  125. end
  126.  
  127. function DisplayImage(buf)
  128.     if buf == nil then
  129.         return nil
  130.     end
  131.     local pixel_buffer = { }
  132.     local coloured = (buf.mode % 2 == 1)
  133.     local dithered = (buf.mode >= 2)
  134.     local pixels = { }
  135.     if coloured == true then
  136.         for key,value in ipairs(buf.bytes) do
  137.             pixels[(key*2)-1],pixels[key*2] = splitHex(value)
  138.         end
  139.     else
  140.         byte2bit(buf.bytes,pixels)
  141.     end
  142.     if dithered == true then
  143.         if coloured == true then
  144.             PrintCsImage(pixels,buf.width)
  145.             return true
  146.         else
  147.             PrintGrImage(pixels,buf.width)
  148.             return true
  149.         end
  150.     else
  151.         if coloured == true then
  152.             PrintCdImage(pixels,buf.width)
  153.             return true
  154.         else
  155.             PrintBwImage(pixels,buf.width)
  156.             return true
  157.         end
  158.     end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement