Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Function to read an integer from bytes in little-endian format
- local function readIntLE(handle, bytes)
- local data = handle.read(bytes)
- local result = 0
- for i = 1, bytes do
- result = result + string.byte(data:sub(i, i)) * (256 ^ (i - 1))
- end
- return result
- end
- -- Function to read the BMP file and convert it to a hex color table
- local function bmpToHexColorTable(filename)
- local file = fs.open(filename, "rb")
- if not file then
- error("File not found: " .. filename)
- end
- -- BMP Header Parsing
- file.read(18) -- Skip BMP signature and other header fields we don't need
- local width = readIntLE(file, 4)
- local height = readIntLE(file, 4)
- file.read(2) -- Skip 2 bytes of color planes
- local bpp = readIntLE(file, 2) -- Bits per pixel (should be 24 for RGB)
- local pixelDataOffset = readIntLE(file, 4) -- Offset where pixel data begins
- if bpp ~= 24 then
- error("Only 24-bit BMP files are supported")
- end
- -- Move to the start of pixel data
- file.seek("set", pixelDataOffset)
- -- Create the color table to store hex colors
- local colorTable = {}
- -- Read each pixel in BMP
- for y = height - 1, 0, -1 do
- colorTable[y] = {}
- for x = 0, width - 1 do
- -- BMP stores color as BGR in 24-bit format
- local b = file.read()
- local g = file.read()
- local r = file.read()
- if tonumber(r)>151 then
- r=r
- end
- -- Convert RGB to hex color
- local hexColor = tonumber(string.format("0x%02X%02X%02X", r, g, b))
- colorTable[y][x] = hexColor
- end
- -- Each row is padded to a multiple of 4 bytes
- local padding = (4 - (width * 3) % 4) % 4
- file.read(padding)
- end
- file.close()
- return colorTable
- end
- -- Main code
- local filename = "eevee.bmp" -- Change this to your BMP file path
- local colorTable = bmpToHexColorTable(filename)
- print("read File done")
- local g=peripheral.wrap("back")
- g.autoUpdate(false)
- g.clear()
- -- Print the hex color table
- local size=2
- for y, row in pairs(colorTable) do
- for x, hexColor in pairs(row) do
- --print(string.format("Pixel (%d, %d): %s", x, y, hexColor))
- g.createRectangle({x=x*size,y=y*size,maxX=x*size+size,maxY=y*size+size, color=hexColor})
- end
- g.update()
- end
Advertisement
Add Comment
Please, Sign In to add comment