ecco7777

CC Advanced Peripheral Glasses Display bmp

Dec 23rd, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.36 KB | None | 0 0
  1. -- Function to read an integer from bytes in little-endian format
  2. local function readIntLE(handle, bytes)
  3.     local data = handle.read(bytes)
  4.     local result = 0
  5.     for i = 1, bytes do
  6.         result = result + string.byte(data:sub(i, i)) * (256 ^ (i - 1))
  7.     end
  8.     return result
  9. end
  10.  
  11. -- Function to read the BMP file and convert it to a hex color table
  12. local function bmpToHexColorTable(filename)
  13.     local file = fs.open(filename, "rb")
  14.     if not file then
  15.         error("File not found: " .. filename)
  16.     end
  17.  
  18.     -- BMP Header Parsing
  19.     file.read(18) -- Skip BMP signature and other header fields we don't need
  20.     local width = readIntLE(file, 4)
  21.     local height = readIntLE(file, 4)
  22.     file.read(2) -- Skip 2 bytes of color planes
  23.     local bpp = readIntLE(file, 2) -- Bits per pixel (should be 24 for RGB)
  24.     local pixelDataOffset = readIntLE(file, 4) -- Offset where pixel data begins
  25.  
  26.     if bpp ~= 24 then
  27.         error("Only 24-bit BMP files are supported")
  28.     end
  29.  
  30.     -- Move to the start of pixel data
  31.     file.seek("set", pixelDataOffset)
  32.  
  33.     -- Create the color table to store hex colors
  34.     local colorTable = {}
  35.  
  36.     -- Read each pixel in BMP
  37.     for y = height - 1, 0, -1 do
  38.         colorTable[y] = {}
  39.         for x = 0, width - 1 do
  40.             -- BMP stores color as BGR in 24-bit format
  41.             local b = file.read()
  42.             local g = file.read()
  43.             local r = file.read()
  44.             if tonumber(r)>151   then
  45.                 r=r
  46.             end
  47.  
  48.             -- Convert RGB to hex color
  49.             local hexColor = tonumber(string.format("0x%02X%02X%02X", r, g, b))
  50.             colorTable[y][x] = hexColor
  51.         end
  52.  
  53.         -- Each row is padded to a multiple of 4 bytes
  54.         local padding = (4 - (width * 3) % 4) % 4
  55.         file.read(padding)
  56.     end
  57.  
  58.     file.close()
  59.     return colorTable
  60. end
  61.  
  62. -- Main code
  63. local filename = "eevee.bmp" -- Change this to your BMP file path
  64. local colorTable = bmpToHexColorTable(filename)
  65. print("read File done")
  66. local g=peripheral.wrap("back")
  67. g.autoUpdate(false)
  68. g.clear()
  69. -- Print the hex color table
  70. local size=2
  71.  
  72. for y, row in pairs(colorTable) do
  73.     for x, hexColor in pairs(row) do
  74.         --print(string.format("Pixel (%d, %d): %s", x, y, hexColor))
  75.         g.createRectangle({x=x*size,y=y*size,maxX=x*size+size,maxY=y*size+size, color=hexColor})
  76.        
  77.     end
  78.     g.update()
  79. end
  80.  
Advertisement
Add Comment
Please, Sign In to add comment