Advertisement
VlaD00m

Modified bmp24.lua

Mar 22nd, 2021 (edited)
562
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. local shell = require("shell")
  2. local filesystem = require("filesystem")
  3. local internet = require("internet")
  4. local component = require("component")
  5. local computer = require("computer")
  6. local term = require("term")
  7. local pull = require('event').pull
  8. local gpu = component.gpu
  9.  
  10. local args, ops = shell.parse(...)
  11.  
  12.  
  13. function rgbToHex(rgb)
  14.   if rgb == nil then return '0X000000' end
  15.   local hexadecimal = '0X'
  16.   for key, value in pairs(rgb) do
  17.     local hex = ''
  18.     while(value > 0)do
  19.       local index = math.fmod(value, 16) + 1
  20.       value = math.floor(value / 16)
  21.       hex = string.sub('0123456789ABCDEF', index, index) .. hex    
  22.     end
  23.     if(string.len(hex) == 0)then
  24.       hex = '00'
  25.     elseif(string.len(hex) == 1)then
  26.       hex = '0' .. hex
  27.     end
  28.     hexadecimal = hexadecimal .. hex
  29.   end
  30.   return hexadecimal
  31. end
  32.  
  33. local res = filesystem.size(ops["path"])
  34.  
  35. local img = io.open(ops["path"], "rb")
  36. if(img==nil) then
  37.   error("no such file found")
  38. end
  39. --тут читаются 2 байта, так как первые 2 байта у .bmp должны быть равны BM
  40. local isBmp = img:read(2)
  41. if isBmp ~= "BM" then error("input file is not .bmp") end
  42. --тут читаются байты в промежутке с 19 по 23 байт, где находится информация о ширине изображения
  43. img:read(16)
  44. local w = string.unpack(">B", img:read(1))
  45. --а тут читаются байты в промежутке с 24 по 27 байт, где находится информация о высоте изображения
  46. img:read(3)
  47. local h = string.unpack(">B", img:read(1))
  48. --а тут читаются оставшиеся 31 байт, так как с 1 до 54 байта включительно находится заголовок .bmp
  49. img:read(31)
  50. local extrabytes=((res-54)-w*h*3)/h --не спрашивайте зачем, так надо
  51. local rgb_t = {}
  52. local px_t = {}
  53. print("Loading picture...")
  54. local upt=computer.uptime()
  55. for i=1, res-54-extrabytes*h do
  56.   local byte_to_int=string.unpack(">B", img:read(1))
  57.   table.insert(rgb_t, 1, byte_to_int)
  58.   if(i%(w*3)==0) then
  59.     img:read(extrabytes)
  60.   end
  61.   if(#rgb_t==3) then
  62.     table.insert(px_t, 1, rgb_t)
  63.     rgb_t={}
  64.   end
  65.   if computer.uptime()>=upt+4 then --при обработке больших картинок цикл должен иногда спать (иначе TLWY)
  66.     os.sleep()
  67.     upt=computer.uptime()
  68.   end
  69. end
  70. term.clear()
  71. local l = 1
  72. for k=1, math.ceil(h/2) do
  73.   for j=1, w do
  74.     gpu.setForeground(tonumber(rgbToHex(px_t[l]))) --передней части символа задаем цвет пикселя с нечетной строки
  75.     gpu.setBackground(tonumber(rgbToHex(px_t[l+w]))) --задней части символа задаем цвет пикселя с четной строки
  76.     gpu.set(w-j+1, k, "▀") --рисуем два пикселя в одном символе
  77.     l=l+1
  78.   end
  79.   l=l+w --перескакиваем на другую нечетную строку
  80. end
  81.  
  82. img:close()
  83. pull('key_down')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement