Advertisement
nonogamer9

teezt

Feb 3rd, 2025 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. -- BMP Image Viewer for CraftOS-PC with URL support
  2.  
  3. local function downloadImage(url)
  4. shell.run("wget", url, "temp.bmp")
  5. if not fs.exists("temp.bmp") then
  6. error("Failed to download image")
  7. end
  8. end
  9.  
  10. local function readBMP(path)
  11. local file = fs.open(path, "rb")
  12. if not file then error("Failed to open file") end
  13.  
  14. local header = file.read(54)
  15. local width = string.unpack("<I4", header:sub(19, 22))
  16. local height = string.unpack("<I4", header:sub(23, 26))
  17. local bpp = string.unpack("<I2", header:sub(29, 30))
  18.  
  19. if bpp ~= 24 then error("Only 24-bit BMPs supported") end
  20.  
  21. local pixels = {}
  22. local padding = (4 - ((width * 3) % 4)) % 4
  23. for y = height-1, 0, -1 do
  24. pixels[y] = {}
  25. for x = 0, width-1 do
  26. local b, g, r = file.read(1):byte(), file.read(1):byte(), file.read(1):byte()
  27. pixels[y][x] = {r, g, b}
  28. end
  29. file.read(padding)
  30. end
  31.  
  32. file.close()
  33. return {width=width, height=height, pixels=pixels}
  34. end
  35.  
  36. local function displayBMP(bmp)
  37. term.setGraphicsMode(2) -- Set 256-color mode
  38.  
  39. for y = 0, bmp.height-1 do
  40. for x = 0, bmp.width-1 do
  41. local r, g, b = table.unpack(bmp.pixels[y][x])
  42. local color = bit32.bor(bit32.lshift(r, 16), bit32.lshift(g, 8), b)
  43. term.setPixel(x, y, color)
  44. end
  45. end
  46. end
  47.  
  48. -- Main program
  49. local args = {...}
  50. if #args < 1 then
  51. print("Usage: bmpviewer <filename or URL>")
  52. return
  53. end
  54.  
  55. local path = args[1]
  56. if path:match("^https?://") then
  57. downloadImage(path)
  58. path = "temp.bmp"
  59. end
  60.  
  61. local bmp = readBMP(path)
  62. displayBMP(bmp)
  63.  
  64. -- Clean up temporary file
  65. if fs.exists("temp.bmp") then
  66. fs.delete("temp.bmp")
  67. end
  68.  
  69. -- Wait for key press before exiting
  70. os.pullEvent("key")
  71. term.setGraphicsMode(0) -- Return to text mode
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement