Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- CraftOS 1.7 BMP loader (no seek, no read(n), only read())
- -- Supports 24-bit uncompressed BMP
- local function read_byte(f)
- local b = f.read()
- if b == nil then error("Unexpected EOF while reading byte") end
- return b
- end
- local function skip(f, n)
- for i = 1, n do
- if f.read() == nil then error("Unexpected EOF while skipping") end
- end
- end
- local function readIntLE(f, n)
- local v = 0
- for i = 0, n-1 do
- local b = read_byte(f)
- v = v + (b * (256 ^ i))
- end
- return v
- end
- local function bmpToHexColorTable(path)
- local f = fs.open(path, "rb")
- if not f then error("File not found: " .. path) end
- -- BMP signature "BM"
- if read_byte(f) ~= 0x42 or read_byte(f) ~= 0x4D then
- error("Not a BMP file")
- end
- -- file size (4), reserved (4)
- skip(f, 8)
- -- pixel data offset
- local pixelOffset = readIntLE(f, 4)
- -- DIB header
- local dibSize = readIntLE(f, 4)
- local width = readIntLE(f, 4)
- local heightS = readIntLE(f, 4)
- local height = math.abs(heightS)
- local planes = readIntLE(f, 2)
- if planes ~= 1 then error("Unsupported color planes") end
- local bpp = readIntLE(f, 2)
- if bpp ~= 24 then error("Only 24-bit BMP supported") end
- -- Skip rest of DIB header
- skip(f, dibSize - 16)
- -- MANUAL SEEK → skip until pixelOffset
- local headerEnd = 14 + dibSize
- if pixelOffset < headerEnd then error("Invalid BMP offset") end
- skip(f, pixelOffset - headerEnd)
- -- Pixel reading
- local rowSize = width * 3
- local padding = (4 - (rowSize % 4)) % 4
- local colorTable = {}
- local yStart, yEnd, yStep
- if heightS < 0 then
- -- top-down
- yStart, yEnd, yStep = 0, height - 1, 1
- else
- -- bottom-up
- yStart, yEnd, yStep = height - 1, 0, -1
- end
- for y = yStart, yEnd, yStep do
- colorTable[y] = {}
- for x = 0, width - 1 do
- local b = read_byte(f)
- local g = read_byte(f)
- local r = read_byte(f)
- colorTable[y][x] = tonumber(string.format("0x%02X%02X%02X", r, g, b))
- end
- skip(f, padding)
- end
- f.close()
- return colorTable
- end
- --main
- local filename = "pic.bmp"
- local ok, tbl = pcall(bmpToHexColorTable, filename)
- if not ok then
- print("Error reading BMP:", tbl)
- return
- end
- print("BMP loaded successfully.")
- rednet.open("back")
- local startID = 15676
- local i = 0
- local keys = {}
- for y,_ in pairs(tbl) do table.insert(keys, y) end
- table.sort(keys)
- for _, y in ipairs(keys) do
- local row = tbl[y]
- local msg = "function c(col) local l=peripheral.wrap('bottom') l.setColor(col) turtle.forward() end"
- -- rows use numeric keys starting at 0
- for x, col in pairs(row) do
- msg = msg .. " c(" .. tostring(col) .. ")"
- end
- rednet.send(startID + i, msg)
- sleep(1)
- i = i + 1
- end
- print("Done sending.")
Advertisement
Add Comment
Please, Sign In to add comment