CCCPaste

ImageView

-- CC:Tweaked Image Converter
-- Converts PNG images to CC:Tweaked compatible formats

local args = {...}

if #args < 1 then
    print("CC:Tweaked Image Viewer")
    print("")
    print("Usage: imgview ")
    print("")
    print("Supported formats:")
    print("  .nfp - NFP paint format")
    print("  .ccimg - CC image format")
    print("")
    print("To create images, use the Python")
    print("converter on your PC:")
    print("  python image_converter.py input.png")
    return
end

local filename = args[1]

-- Try different extensions
local extensions = {"", ".nfp", ".ccimg"}
local file = nil
local format = nil

for _, ext in ipairs(extensions) do
    local path = filename .. ext
    if fs.exists(path) then
        file = fs.open(path, "r")
        if ext == ".nfp" or ext == "" then
            format = "nfp"
        else
            format = "ccimg"
        end
        filename = path
        break
    end
end

if not file then
    print("Error: File not found: " .. filename)
    print("Tried: " .. filename .. ", " .. filename .. ".nfp, " .. filename .. ".ccimg")
    return
end

local content = file.readAll()
file.close()

-- Color character mapping
local colorChars = {
    ["0"] = colors.white,
    ["1"] = colors.orange,
    ["2"] = colors.magenta,
    ["3"] = colors.lightBlue,
    ["4"] = colors.yellow,
    ["5"] = colors.lime,
    ["6"] = colors.pink,
    ["7"] = colors.gray,
    ["8"] = colors.lightGray,
    ["9"] = colors.cyan,
    ["a"] = colors.purple,
    ["b"] = colors.blue,
    ["c"] = colors.brown,
    ["d"] = colors.green,
    ["e"] = colors.red,
    ["f"] = colors.black,
}

-- Parse and display NFP format
local function displayNFP(content)
    term.setBackgroundColor(colors.black)
    term.clear()
    term.setCursorPos(1, 1)
    
    local y = 1
    for line in content:gmatch("[^\n]+") do
        term.setCursorPos(1, y)
        for i = 1, #line do
            local char = line:sub(i, i)
            local color = colorChars[char:lower()]
            if color then
                term.setBackgroundColor(color)
                term.write(" ")
            elseif char == " " then
                term.setBackgroundColor(colors.black)
                term.write(" ")
            end
        end
        y = y + 1
    end
end

-- Parse and display CCIMG format (custom format with more features)
local function displayCCIMG(content)
    local data = textutils.unserialize(content)
    if not data then
        print("Error: Invalid .ccimg file")
        return
    end
    
    term.setBackgroundColor(colors.black)
    term.clear()
    
    for y, row in ipairs(data.pixels or data) do
        for x, colorChar in ipairs(row) do
            term.setCursorPos(x, y)
            local color = colorChars[colorChar:lower()] or colors.black
            term.setBackgroundColor(color)
            term.write(" ")
        end
    end
end

-- Display based on format
if format == "nfp" then
    displayNFP(content)
elseif format == "ccimg" then
    displayCCIMG(content)
end

-- Wait for key
term.setCursorPos(1, 1)
term.setBackgroundColor(colors.black)
term.setTextColor(colors.white)

local w, h = term.getSize()
term.setCursorPos(1, h)
term.setBackgroundColor(colors.gray)
term.write(" Press any key to exit ")
term.setBackgroundColor(colors.black)

os.pullEvent("key")
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1, 1)