ElijahCrafter

ImageView

Nov 29th, 2025 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | Source Code | 0 0
  1. -- CC:Tweaked Image Converter
  2. -- Converts PNG images to CC:Tweaked compatible formats
  3.  
  4. local args = {...}
  5.  
  6. if #args < 1 then
  7.     print("CC:Tweaked Image Viewer")
  8.     print("")
  9.     print("Usage: imgview <filename>")
  10.     print("")
  11.     print("Supported formats:")
  12.     print("  .nfp - NFP paint format")
  13.     print("  .ccimg - CC image format")
  14.     print("")
  15.     print("To create images, use the Python")
  16.     print("converter on your PC:")
  17.     print("  python image_converter.py input.png")
  18.     return
  19. end
  20.  
  21. local filename = args[1]
  22.  
  23. -- Try different extensions
  24. local extensions = {"", ".nfp", ".ccimg"}
  25. local file = nil
  26. local format = nil
  27.  
  28. for _, ext in ipairs(extensions) do
  29.     local path = filename .. ext
  30.     if fs.exists(path) then
  31.         file = fs.open(path, "r")
  32.         if ext == ".nfp" or ext == "" then
  33.             format = "nfp"
  34.         else
  35.             format = "ccimg"
  36.         end
  37.         filename = path
  38.         break
  39.     end
  40. end
  41.  
  42. if not file then
  43.     print("Error: File not found: " .. filename)
  44.     print("Tried: " .. filename .. ", " .. filename .. ".nfp, " .. filename .. ".ccimg")
  45.     return
  46. end
  47.  
  48. local content = file.readAll()
  49. file.close()
  50.  
  51. -- Color character mapping
  52. local colorChars = {
  53.     ["0"] = colors.white,
  54.     ["1"] = colors.orange,
  55.     ["2"] = colors.magenta,
  56.     ["3"] = colors.lightBlue,
  57.     ["4"] = colors.yellow,
  58.     ["5"] = colors.lime,
  59.     ["6"] = colors.pink,
  60.     ["7"] = colors.gray,
  61.     ["8"] = colors.lightGray,
  62.     ["9"] = colors.cyan,
  63.     ["a"] = colors.purple,
  64.     ["b"] = colors.blue,
  65.     ["c"] = colors.brown,
  66.     ["d"] = colors.green,
  67.     ["e"] = colors.red,
  68.     ["f"] = colors.black,
  69. }
  70.  
  71. -- Parse and display NFP format
  72. local function displayNFP(content)
  73.     term.setBackgroundColor(colors.black)
  74.     term.clear()
  75.     term.setCursorPos(1, 1)
  76.    
  77.     local y = 1
  78.     for line in content:gmatch("[^\n]+") do
  79.         term.setCursorPos(1, y)
  80.         for i = 1, #line do
  81.             local char = line:sub(i, i)
  82.             local color = colorChars[char:lower()]
  83.             if color then
  84.                 term.setBackgroundColor(color)
  85.                 term.write(" ")
  86.             elseif char == " " then
  87.                 term.setBackgroundColor(colors.black)
  88.                 term.write(" ")
  89.             end
  90.         end
  91.         y = y + 1
  92.     end
  93. end
  94.  
  95. -- Parse and display CCIMG format (custom format with more features)
  96. local function displayCCIMG(content)
  97.     local data = textutils.unserialize(content)
  98.     if not data then
  99.         print("Error: Invalid .ccimg file")
  100.         return
  101.     end
  102.    
  103.     term.setBackgroundColor(colors.black)
  104.     term.clear()
  105.    
  106.     for y, row in ipairs(data.pixels or data) do
  107.         for x, colorChar in ipairs(row) do
  108.             term.setCursorPos(x, y)
  109.             local color = colorChars[colorChar:lower()] or colors.black
  110.             term.setBackgroundColor(color)
  111.             term.write(" ")
  112.         end
  113.     end
  114. end
  115.  
  116. -- Display based on format
  117. if format == "nfp" then
  118.     displayNFP(content)
  119. elseif format == "ccimg" then
  120.     displayCCIMG(content)
  121. end
  122.  
  123. -- Wait for key
  124. term.setCursorPos(1, 1)
  125. term.setBackgroundColor(colors.black)
  126. term.setTextColor(colors.white)
  127.  
  128. local w, h = term.getSize()
  129. term.setCursorPos(1, h)
  130. term.setBackgroundColor(colors.gray)
  131. term.write(" Press any key to exit ")
  132. term.setBackgroundColor(colors.black)
  133.  
  134. os.pullEvent("key")
  135. term.setBackgroundColor(colors.black)
  136. term.clear()
  137. term.setCursorPos(1, 1)
  138.  
Advertisement
Add Comment
Please, Sign In to add comment