ecco7777

CC Luminum Lamp Turtle Pixelart Painter Rednet Remote Computer Controller

Dec 3rd, 2025 (edited)
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. -- CraftOS 1.7 BMP loader (no seek, no read(n), only read())
  2. -- Supports 24-bit uncompressed BMP
  3.  
  4. local function read_byte(f)
  5.     local b = f.read()
  6.     if b == nil then error("Unexpected EOF while reading byte") end
  7.     return b
  8. end
  9.  
  10. local function skip(f, n)
  11.     for i = 1, n do
  12.         if f.read() == nil then error("Unexpected EOF while skipping") end
  13.     end
  14. end
  15.  
  16. local function readIntLE(f, n)
  17.     local v = 0
  18.     for i = 0, n-1 do
  19.         local b = read_byte(f)
  20.         v = v + (b * (256 ^ i))
  21.     end
  22.     return v
  23. end
  24.  
  25. local function bmpToHexColorTable(path)
  26.     local f = fs.open(path, "rb")
  27.     if not f then error("File not found: " .. path) end
  28.  
  29.     -- BMP signature "BM"
  30.     if read_byte(f) ~= 0x42 or read_byte(f) ~= 0x4D then
  31.         error("Not a BMP file")
  32.     end
  33.  
  34.     -- file size (4), reserved (4)
  35.     skip(f, 8)
  36.  
  37.     -- pixel data offset
  38.     local pixelOffset = readIntLE(f, 4)
  39.  
  40.     -- DIB header
  41.     local dibSize = readIntLE(f, 4)
  42.     local width   = readIntLE(f, 4)
  43.     local heightS = readIntLE(f, 4)
  44.     local height  = math.abs(heightS)
  45.  
  46.     local planes = readIntLE(f, 2)
  47.     if planes ~= 1 then error("Unsupported color planes") end
  48.  
  49.     local bpp = readIntLE(f, 2)
  50.     if bpp ~= 24 then error("Only 24-bit BMP supported") end
  51.  
  52.     -- Skip rest of DIB header
  53.     skip(f, dibSize - 16)
  54.  
  55.     -- MANUAL SEEK → skip until pixelOffset
  56.     local headerEnd = 14 + dibSize
  57.     if pixelOffset < headerEnd then error("Invalid BMP offset") end
  58.     skip(f, pixelOffset - headerEnd)
  59.  
  60.     -- Pixel reading
  61.     local rowSize = width * 3
  62.     local padding = (4 - (rowSize % 4)) % 4
  63.     local colorTable = {}
  64.  
  65.     local yStart, yEnd, yStep
  66.     if heightS < 0 then
  67.         -- top-down
  68.         yStart, yEnd, yStep = 0, height - 1, 1
  69.     else
  70.         -- bottom-up
  71.         yStart, yEnd, yStep = height - 1, 0, -1
  72.     end
  73.  
  74.     for y = yStart, yEnd, yStep do
  75.         colorTable[y] = {}
  76.         for x = 0, width - 1 do
  77.             local b = read_byte(f)
  78.             local g = read_byte(f)
  79.             local r = read_byte(f)
  80.             colorTable[y][x] = tonumber(string.format("0x%02X%02X%02X", r, g, b))
  81.         end
  82.         skip(f, padding)
  83.     end
  84.  
  85.     f.close()
  86.     return colorTable
  87. end
  88.  
  89. --main
  90.  
  91. local filename = "pic.bmp"
  92. local ok, tbl = pcall(bmpToHexColorTable, filename)
  93. if not ok then
  94.     print("Error reading BMP:", tbl)
  95.     return
  96. end
  97.  
  98. print("BMP loaded successfully.")
  99.  
  100. rednet.open("back")
  101.  
  102. local startID = 15676
  103. local i = 0
  104.  
  105. local keys = {}
  106. for y,_ in pairs(tbl) do table.insert(keys, y) end
  107. table.sort(keys)
  108.  
  109. for _, y in ipairs(keys) do
  110.     local row = tbl[y]
  111.     local msg = "function c(col) local l=peripheral.wrap('bottom') l.setColor(col) turtle.forward() end"
  112.  
  113.     -- rows use numeric keys starting at 0
  114.     for x, col in pairs(row) do
  115.         msg = msg .. " c(" .. tostring(col) .. ")"
  116.     end
  117.  
  118.     rednet.send(startID + i, msg)
  119.     sleep(1)
  120.     i = i + 1
  121. end
  122.  
  123. print("Done sending.")
  124.  
Advertisement
Add Comment
Please, Sign In to add comment