備忘録やめた

備忘録として使用していたけどやめた.このブログに載せてあるコードのライセンスは別途記載がない限りWTFPL OR NYSLです.

++++++++++[>+++++++>++++++++++>+++++++++++>++++++++++++>+++<<<<<-]>+.>+.>.<.>++++.<----.>++.<++++.>>>++.<+.<-----.>----.<+++.>>.<<<---.>.<-.++++++++.>----.<---.>>.<<---.>---.>>.<<<.>++++.<+.+.>>>.<<<+.>+++.---.--.>>.<<<-----.>+.>>.<<-----.++++.<.++++++.--

++++++++++[>+++++++>+++++++++++>+++>++++++++++++>++++++++++<<<<<-]>.>++++.---.--.>++.>+.<<++.>>----.<<+++.>.>>+++++.++++.------------.++++++.--.<<.>-.<<---.>.>>----.<<.>>+.<--.>-.<<<------.>>----.<<---.>>+++++++.>++.<<<+++++.>.>>.<<<++++.>>>+.+.

# This Python code is licensed under GPL-3.0.
# See https://www.gnu.org/licenses/gpl-3.0.txt for the full license texts.
from krita import Krita
import sys

# String to brainfuck converter is from https://github.com/pocmo/Python-Brainfuck.
class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""

    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
        import tty
        import sys

    def __call__(self):
        import sys
        import tty
        import termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows:
    def __init__(self):
        import msvcrt

    def __call__(self):
        import msvcrt
        return msvcrt.getch()


getch = _Getch()


def evaluate(code):
    code = cleanup(list(code))
    bracemap = buildbracemap(code)

    cells, codeptr, cellptr = [0], 0, 0

    while codeptr < len(code):
        command = code[codeptr]

        if command == ">":
            cellptr += 1
            if cellptr == len(cells):
                cells.append(0)

        if command == "<":
            cellptr = 0 if cellptr <= 0 else cellptr - 1

        if command == "+":
            cells[cellptr] = cells[cellptr] + 1 if cells[cellptr] < 255 else 0

        if command == "-":
            cells[cellptr] = cells[cellptr] - 1 if cells[cellptr] > 0 else 255

        if command == "[" and cells[cellptr] == 0:
            codeptr = bracemap[codeptr]
        if command == "]" and cells[cellptr] != 0:
            codeptr = bracemap[codeptr]
        if command == ".":
            sys.stdout.write(chr(cells[cellptr]))
        if command == ",":
            cells[cellptr] = ord(getch.getch())

        codeptr += 1


def cleanup(code):
    return ''.join(filter(lambda x: x in ['.', ',', '[', ']', '<', '>', '+', '-'], code))


def buildbracemap(code):
    temp_bracestack, bracemap = [], {}

    for position, command in enumerate(code):
        if command == "[":
            temp_bracestack.append(position)
        if command == "]":
            start = temp_bracestack.pop()
            bracemap[start] = position
            bracemap[position] = start
    return bracemap

# (b, g, r, a)
colorToCode = {
    (0, 0, 0, 255): '>',
    (255, 0, 0, 255): '<',
    (0, 255, 0, 255): '+',
    (0, 0, 255, 255): '-',
    (255, 255, 0, 255): '.',
    (255, 0, 255, 255): ',',
    (0, 255, 255, 255): '[',
    (255, 255, 255, 255): ']',
}


doc = Krita.instance().activeDocument()
w = doc.width()
h = doc.height()

b = doc.pixelData(0, 0, w, h)
code = ""
for i in range(0, len(b), 4):
    chunk = tuple([ord(b) for b in (b[i:i+4])])
    if chunk in colorToCode:
        code += colorToCode[chunk]

evaluate(code)

++++++++++[>++++++++>++++++++++++>+++++++++++>+++>++++++++++>++++>++++++<<<<<<<-]>++.>---.>.>++.<<-.>>>++++.+.<<<-.>>.<++.<++++++.>++++.>>-.<<-----.-.>.<+++++.>>-----.<<-.>>++++++.<<--.++++.>.>.+++++.<.<<<-------.>>--.>>-----.<<++.>>--------.>-.<<<-.>.<.>>++.<<-.>>++++++.<<--.++++.-----.+++.>>>>--.

++++++++++[>+++++++>++++++++++>++++++++++++>+++++++++++>+++>++++++++>++++<<<<<<<-]>-----.>++.>----.<-.>--.>>++.<<.+++.>..<<++++.>>.<<--.>>>.<<-.<+.---.>>>.<++.<+++++.>++++.<<+++.>>-----.-.>.<+++++.<<-----.>>-.<<++++++.>>--.++++.>.<-----.-.>.>-----.<<++++.<<.>>++.<<--------.>>>>>-.<<<-.>.<.<<++.>>-.<<++++++.>>--.++++.-----.+++.

++++++++++[>+++++++>++++++++++>+++>+++++++++++>++++++++++++>+++++>++++++++>++++<<<<<<<<-]>-.>---.++.+++++.>++.>++.<<+.>>>.<<<----.>>----.<.>++++++.<<.>>--.++.<<.>>+.<<.>>-----.>----.-.<<.<----.>.<+.>>>-.<<<-.>>-----.>----.<---.>+++++++.<<<++.>>+++++.<.<.>>++++.<<+.+.>>>>----.<<<.>>>>-.<<<-.--.>++++.<<.<.>>---.<<++.+.>>>-----.<<.<-----.>>>-----.<+++.>.+++.+.<<.<--.>>>-.<<<++++.>.>>.<<<.--.>>+++.<<++++.>>-.<<++.>>>++++++++.<<<----.-.>.<---.>>+++++.<.<.>.>+++.<<.>>----------.---.<<+++.>.<-.>>++++++.<<+.+.>>>>--.<<<.<----.>>-.<<+++.>.<---.>>--..<.>>------.<----.---.<.>>-----.+++++.<+++.---.>--.<<.<++.>>>---.---.+++.+++.+.<<.<--.>>>-.<.<.>>++.--.<.<<.>>>++.<.-.<.<.>>>-.<<.>-.>----.--..<++.>+.++++++.-.>++.<<<.<<---.>>>>++++++.<<.>>-----.<+++.---.<.>>+++.<<<.>>>++.>--.<<<.++++++++++[>+++++++++++>++++++++++++>++++++++++>+++++++<<<<-]>++.>>+++++.<.>----.<<----.>>>--.<----.<----.>.++++++++++[>+++>+++++++++++>++++++++++>++++++++++++>+++++++>++++++++>++++>+++++<<<<<<<<-]>++.>++++.>+.<++.+.---.----.+++++.<.>+.>+++.---.<<.>>---.>+.<<.>+++.<-.<.>-.>.<--.++.>.<+.>.<-----.>>-----.<++++.<.>--.<<.>>--.----.++.+++++.<<.>++.>+.>++++.<----.<----.<.>---.+++++.<.>>>----.<+++.---.<<.>+.>>--.<-.+.>.<<<.>.>+.<<.>>>>----.+++++.>++.<------.>>++++.<<<<<<.>-.+.>>++.<<<.>>>>>.<++++++.-----.-.>>++.

++++++++++[>+++++++>+++++++++++>++++++++++++>++++++++++>+++<<<<<-]>---.>+.-.>--.>+.<<++++.++.>>>++.<<+++.<-----.>----.<+++.>>>.<---.<<.>>-.++++++++.<<----.>>---.<.>---.<<---.>>>.<.<<++++.>>+.+.>.<<-.<.>>>.<----.<<-.>>>.<<<-----.++++.>>.<<------.--.

# This Python code is licensed under GPL-3.0.
# See https://www.gnu.org/licenses/gpl-3.0.txt for the full license texts.
from krita import Krita

colorToCode = {
    '>': (0, 0, 0, 255),
    '<': (255, 0, 0, 255),
    '+': (0, 255, 0, 255),
    '-': (0, 0, 255, 255),
    '.': (255, 255, 0, 255),
    ',': (255, 0, 255, 255),
    '[': (0, 255, 255, 255),
    ']': (255, 255, 255, 255),
}


doc = Krita.instance().activeDocument()
w = doc.width()
h = doc.height()

code = []
for c in '++++++++++[>+++++++++>++++++++++>+++++++++++>+++>++++++++++++>++++++<<<<<<-]>---.>++++.>+.>++.<+++.<---.----.+++.>+.>.<+.<++++.+.>-.>>>+++.':
    if c in colorToCode:
        code += list(colorToCode[c])

code += [0]*(w*h*4-len(code))

l = doc.createNode("brainfuckCode", "paintLayer")
l.setPixelData(bytes(code), 0, 0, w, h)

doc.rootNode().addChildNode(l, None)

++++++++++[>++++++++>++++++++++++>+++++++++++>+++>++++++++++>++++>++++++<<<<<<<-]>++.>---.>.>++.<<-.>>>++++.+.<<<-.>>.<<<--.>++++++.-----.>>>-.<<+.-.>.<<-.>>>-----.<<<-.>-----.<--.++++.>>.<<-----.-.>>.<<<-----.>++++.>.<++.>>>--.>-.<<<<-.>>.<<.>>>++.<<<-.>.<--.++++.>>>++.<<<--.>>>>>--.

++++++++++[>+++++++>++++++++++>++++++++++++>+++++++++++>+++>++++++++<<<<<<-]>-----.>++.>----.<-.>--.>>++.<<.+++.>..<<++++.>>.<<--.>>>.<<-.<+.---.>>>.>.<<<+++++.-----.<+++.>>+.-.>.<<-.<-----.>-.>-----.<--.++++.

++++++++++[>++++++++>++++++++++>+++>++++++++++++>+++++++++++<<<<<-]>++++.>++++.---.>++.>-----.<<--.>>-.>-----.<--.++++.<.>+++.>.+++..<<.<++++.--.>>>++.<<<.>>>++++.<<<----.>>>++.<<<++++.>.>>.<<<+++.---.>.>>----.<<<----.>>>-------.+++++.<---.<.>>--.<<<.>>+++++.<<++++.>>>++++++.<<.>>----.<<<----.>>>-.<<<++++.-.>.++++++++++[>++++++++++>+++++++++++>++++++++++++>+++++++<<<<-]>--.>++++.<-.++++++++.>----.<---.>>---.<<---.>---.>>---.<<++++.<+.+.++++++++++[>+++>++++++++++>+++++++++++>++++++++++++>+++++++>+++++<<<<<<-]>++.>-.>+.-.>----.<<--.>-----.+++++.-----.+++++.<++++++.<.>>>.<<+.---.<.>>>>----.<--.<<----.>-----.>----.<---.>+++++++.<<++.>+++++.<<.>.>++++.<+.+.>>>>----.

++++++++++[>++++++++>++++++++++>++++++++++++>++++>+++>+++++++++++<<<<<<-]>----.>+.>----.>-.>++.<<-.>>.<<<++++.>>>>.<<<+.<----.>--.--.++.<.>++.>>.<<.<+++.---.>>>.<<<++++.>>>>-.<<<<--------.++++++.--.>>>+.

++++++++++[>+++++++>++++++++++>++++++++++++>+++++++++++>+++<<<<<-]>-----.>++.>----.<-.>--.>>++.<<<++++.>>.<++.<----.>--.--.++.<.>++.<++++.>>.<<--.>>>.<<.<+.---.>>>.<<<++++.>>-.<<--------.++++++.--.

++++++++++[>+++++++>+++++++++++>++++++++++>++++++++++++>+++++<<<<<-]>+.>++++.>+.----.<++.>>>----.

++++++++++[>+++++++>++++++++<<-]>.-----.>+.

++++++++++[>+++++++>+++++++++++>++++++++++++>+++>++++++++++<<<<<-]>++.>+.>-.>++.<---.<.>>.>++++.-------.<<<-.>>>+++.<<<--.>>>+.<.++++++++++[>++++<-]>++++.++++++++++[>++++++>+++>+++++++>+++++++++++>++++++++++++>++++++++++<<<<<<-]>+++.>++.>++.>+.>-.<<<.>>>---.<.<<.>>>--.>+.----.+++.<<<<<.

++++++++++[>++++++++>+++++++++++>++++++++++++>++++>+++>+++++++>++++++++++>+++++<<<<<<<<-]>+++.>+.+++..>+.>++++.>++.>+++.<.>>.<<<<<---.-.>>-----.<-----.>>.<<<---.+++.+.>+++.>>.<<<<+.>>>>>-------.++++++.>>----.

++++++++++[>+++++++>+++++++++++>++++++++++++>+++>++++++++++>++++++<<<<<<-]>++.>+.>-.>++.>.+++++.-----.<.<++.<.>----.>.>+++.--.<<<-.>>>.<<---.>>----.<<++.>>++++.<.<.>>+++.---.<<-.>>.<.>---.<<-.>>-.<<<-----.>----.<---.>+++++++.>>++.<<<+++++.>>.>.<<<++++.>>>+.+.<<--.>.>++++.<<<-.>>.<+.>>-.+.<<-.>.>--------.<<-.++.<-----.>>>++.<<<+++.>>>++.>+++.

++++++++++[>+++++++>+++>++++++++++++>++++++++++<<<<-]>+++.>++.>---.--.>+.-.<<.++++++++++[>++++++++++++>++++++++++>+++++++++++>+++<<<<-]>----.>++++.+.<-.>>>++.<<------.>+.<+.+. ++++++++++[>+++++<-]>----.