def c2p():
    m0 = Word.Mask('00ff')
    m1 = Word.Mask('0f0f')

    print "=[ c2p 1x1 ham6 (blitter + mangled) ]=".center(48, '-')

    def MakeWord(c, color):
        # mangle: [ 0  0  0  0 r0 r1 r2 r3 g0 g1 g2 g3 b0 b1 b2 b3] =>
        #         [r0 g0 b0 b0 r1 g1 b1 b1 r2 g2 b2 b2 r3 g3 b3 b3]
        return Word([
            Bit.Var(c[0], 0, color),
            Bit.Var(c[1], 0, color),
            Bit.Var(c[2], 0, color),
            Bit.Var(c[3], 0, color),
            Bit.Var(c[0], 1, color),
            Bit.Var(c[1], 1, color),
            Bit.Var(c[2], 1, color),
            Bit.Var(c[3], 1, color),
            Bit.Var(c[0], 2, color),
            Bit.Var(c[1], 2, color),
            Bit.Var(c[2], 2, color),
            Bit.Var(c[3], 2, color),
            Bit.Var(c[0], 3, color),
            Bit.Var(c[1], 3, color),
            Bit.Var(c[2], 3, color),
            Bit.Var(c[3], 3, color)
        ])

    A = Array.Make(MakeWord)
    N = len(A)
    Array.Print("Data:", *A)

    B = Array.Zero(N, 16)
    Blit(lambda a, b: ((a >> 8) & m0) | (b & ~m0), N / 4, 2, Channel(A, 2, 2),
         Channel(A, 0, 2), Channel(B, 0, 2))
    Blit(lambda a, b: ((a << 8) & ~m0) | (b & m0), N / 4, 2, Channel(A, 0, 2),
         Channel(A, 2, 2), Channel(B, 2, 2))
    Array.Print("Swap 8x4:", *B)

    C = [Array.Zero(N / 4, 16) for i in range(4)]
    Blit(lambda a, b: ((a >> 4) & m1) | (b & ~m1), N / 4, 1, Channel(B, 1, 3),
         Channel(B, 0, 3), Channel(C[0]))
    Blit(lambda a, b: ((a >> 4) & m1) | (b & ~m1), N / 4, 1, Channel(B, 3, 3),
         Channel(B, 2, 3), Channel(C[2]))
    Blit(lambda a, b: ((a << 4) & ~m1) | (b & m1), N / 4, 1, Channel(B, 0, 3),
         Channel(B, 1, 3), Channel(C[1]))
    Blit(lambda a, b: ((a << 4) & ~m1) | (b & m1), N / 4, 1, Channel(B, 2, 3),
         Channel(B, 3, 3), Channel(C[3]))
    print("Bitplanes:")
    Array.Print("[0]:", *C[0])
    Array.Print("[1]:", *C[1])
    Array.Print("[2]:", *C[2])
    Array.Print("[3]:", *C[3])
Esempio n. 2
0
def c2p_2_1_4():
    m0 = Word.Mask('3333')
    m1 = Word.Mask('cccc')
    m2 = Word.Mask('5555')
    m3 = Word.Mask('aaaa')

    print "c2p 2x1 4-bitplanes".center(96, '-')

    a0 = Word.Data('a', color="31m")
    a1 = Word.Data('b', color="32m")
    a2 = Word.Data('c', color="33m")
    a3 = Word.Data('d', color="34m")

    Array.Print("Data:", a0, a1, a2, a3)

    b0 = (a0 & m1) | (a2.lsr(2) & m0)
    b1 = (a1 & m1) | (a3.lsr(2) & m0)
    b2 = (a0.lsl(2) & m1) | (a2 & m0)
    b3 = (a1.lsl(2) & m1) | (a3 & m0)

    Array.Print("Swap 2x2:", b0, b1, b2, b3)

    c0 = (b0 & m3) | (b1.lsr(1) & m2)
    c1 = (b0.lsl(1) & m3) | (b1 & m2)
    c2 = (b2 & m3) | (b3.lsr(1) & m2)
    c3 = (b2.lsl(1) & m3) | (b3 & m2)

    Array.Print("Swap 1x1:", c0, c1, c2, c3)
Esempio n. 3
0
def read():
    """ Reads dictionary from file.
    """

    fp = open('bla.dict', 'rb')

    ways = read_ways(fp)

    word = Word()
    word.read(fp)

    root = ReadNode(fp, fp.tell())
    child = root[3]

    fp.close()
Esempio n. 4
0
def process_stream(f, name, *args):
    total_count, output_count = 0, 0
    document_id, document_info, sentences = None, None, []
    comments, words = [], []
    for ln, l in enumerate(f, start=1):
        l = l.rstrip('\n')
        if not l or l.isspace():
            sentences.append((comments, words))
            comments, words = [], []
        elif l.startswith('#'):
            if is_document_boundary(l):
                if sentences:
                    output_count += process_document(sentences, *args)
                    total_count += 1
                sentences = []
            comments.append(l)
        else:
            words.append(Word(*l.split('\t')))
        if ln % 100000 == 0:
            print('processed {} lines, output {}/{} ({:.1%}) documents ...'.\
                      format(ln, output_count, total_count,
                             output_count/total_count),
                  file=sys.stderr, flush=True)
    if sentences:
        output_count += process_document(sentences, *args)
        total_count += 1
    print('{}: output {}/{} ({:.1%})'.format(
        os.path.basename(name), output_count, total_count,
        output_count/total_count), file=sys.stderr, flush=True)
Esempio n. 5
0
def write():
    """ Writes dictionary to file.
    """

    fp = open('bla.dict', 'wb')

    write_ways(fp, get_ways())
    word = Word('Labas', 'Ačiū')
    word.write(fp)

    root = WriteNode()
    child = WriteNode()
    child.add_word(word)
    root.add_child(3, child)
    root.write(fp)

    fp.close()
Esempio n. 6
0
    def __init__(self, fp, address):

        self.address = address
        self.fp = fp

        fp.seek(address)

        children_count, = unpack('!L', fp.read(4))
        word_count, = unpack('!L', fp.read(4))
        self.word_count, = unpack('!L', fp.read(4))

        self.addresses = {}
        for i in range(children_count):
            way, address = unpack('!BL', fp.read(5))
            self.addresses[way] = address

        self.words = []
        for i in range(word_count):
            word = Word()
            word.read(fp)
            self.words.append(word)
Esempio n. 7
0
def sample_stream(f, fn, options):
    sentences, comments, words = [], [], []
    for ln, l in enumerate(f, start=1):
        l = l.rstrip('\n')
        if not l or l.isspace():
            sentences.append((comments, words))
            comments, words = [], []
        elif l.startswith('#'):
            if is_document_boundary(l):
                if sentences:
                    process_document(sentences, options)
                sentences = []
            comments.append(l)
        else:
            words.append(Word(*l.split('\t')))
    if sentences:
        process_document(sentences, options)
Esempio n. 8
0
 def MakeWord(c, color):
     return Word([Bit.Var(c[0], 0, color),
                  Bit.Var(c[0], 1, color),
                  Bit.Var(c[1], 0, color),
                  Bit.Var(c[1], 1, color),
                  Bit.Var(c[0], 2, color),
                  Bit.Var(c[0], 3, color),
                  Bit.Var(c[1], 2, color),
                  Bit.Var(c[1], 3, color),
                  Bit.Var(c[2], 0, color),
                  Bit.Var(c[2], 1, color),
                  Bit.Var(c[3], 0, color),
                  Bit.Var(c[3], 1, color),
                  Bit.Var(c[2], 2, color),
                  Bit.Var(c[2], 3, color),
                  Bit.Var(c[3], 2, color),
                  Bit.Var(c[3], 3, color)])
 def MakeWord(c, color):
     # mangle: [ 0  0  0  0 r0 r1 r2 r3 g0 g1 g2 g3 b0 b1 b2 b3] =>
     #         [r0 g0 b0 b0 r1 g1 b1 b1 r2 g2 b2 b2 r3 g3 b3 b3]
     return Word([Bit.Var(c[0], 0, color),
                  Bit.Var(c[1], 0, color),
                  Bit.Var(c[2], 0, color),
                  Bit.Var(c[3], 0, color),
                  Bit.Var(c[0], 1, color),
                  Bit.Var(c[1], 1, color),
                  Bit.Var(c[2], 1, color),
                  Bit.Var(c[3], 1, color),
                  Bit.Var(c[0], 2, color),
                  Bit.Var(c[1], 2, color),
                  Bit.Var(c[2], 2, color),
                  Bit.Var(c[3], 2, color),
                  Bit.Var(c[0], 3, color),
                  Bit.Var(c[1], 3, color),
                  Bit.Var(c[2], 3, color),
                  Bit.Var(c[3], 3, color)])
Esempio n. 10
0
 def MakeWord(a, b, e, f, c):
     return Word([
         Bit.Var(a, 3, c),
         Bit.Var(a, 2, c),
         Bit.Var(b, 3, c),
         Bit.Var(b, 2, c),
         Bit.Var(a, 1, c),
         Bit.Var(a, 0, c),
         Bit.Var(b, 1, c),
         Bit.Var(b, 0, c),
         Bit.Var(e, 3, c),
         Bit.Var(e, 2, c),
         Bit.Var(f, 3, c),
         Bit.Var(f, 2, c),
         Bit.Var(e, 1, c),
         Bit.Var(e, 0, c),
         Bit.Var(f, 1, c),
         Bit.Var(f, 0, c)
     ])
Esempio n. 11
0
def process_stream(f, name, options):
    stats = defaultdict(int)
    total_count, output_count = 0, 0
    document_id, document_info, sentences = None, None, []
    comments, words = [], []
    for ln, l in enumerate(f, start=1):
        l = l.rstrip('\n')
        if not l or l.isspace():
            sentences.append((comments, words))
            comments, words = [], []
        elif l.startswith('#'):
            if is_document_boundary(l):
                if sentences:
                    output_count += process_document(sentences, stats, options)
                    total_count += 1
                sentences = []
                if options.limit is not None and total_count >= options.limit:
                    break
            comments.append(l)
        else:
            words.append(Word(*l.split('\t')))
        if ln % 100000 == 0:
            print('processed {} lines ({} docs) ...'.format(ln, total_count),
                  file=sys.stderr,
                  flush=True)
    if sentences:
        output_count += process_document(sentences, stats, options)
        total_count += 1
    for k, v in stats.items():
        print('{}:{}\t{}'.format(os.path.basename(name), k, v),
              file=sys.stderr,
              flush=True)
    print('{}: output {}/{} ({:.1%})'.format(os.path.basename(name),
                                             output_count, total_count,
                                             output_count / total_count),
          file=sys.stderr,
          flush=True)
Esempio n. 12
0
from common import Rule, Word

rules = []
rules.append(Rule('S', 'NP VP'))
rules.append(Rule('NP', 'N'))
rules.append(Rule('NP', 'SURNAME N'))
rules.append(Rule('NP', 'N N'))
rules.append(Rule('NP', 'V N'))
rules.append(Rule('PP', 'PREP NP'))
rules.append(Rule('VP', 'V NP'))
rules.append(Rule('VP', 'ADV VP'))
rules.append(Rule('VP', 'PP VP'))

words = '王 翻译 在 翻译 小说'
words = words.split()
dictionary = []
dictionary.append(Word('王', 'SURNAME N'))
dictionary.append(Word('翻译', 'N V'))
dictionary.append(Word('在', 'V ADV PREP'))
dictionary.append(Word('小说', 'N'))

if __name__ == '__main__':
    for i in rules:
        print(i)

    print()

    for i in dictionary:
        print(i)
Esempio n. 13
0
 def MakeWord(chars, color):
     bits = []
     for c in chars:
         for i in range(4):
             bits.append(Bit.Var(c, i, color))
     return Word(bits)
Esempio n. 14
0
def c2p_2_1_4_stingray():
    m0 = Word.Mask('f0f0')
    m1 = Word.Mask('0f0f')
    m2 = Word.Mask('aaaa')
    m3 = Word.Mask('5555')

    print "=[ c2p 2x1 4bpl (stingray) ]=".center(48, '-')

    def MakeWord(a, b, e, f, c):
        return Word([
            Bit.Var(a, 3, c),
            Bit.Var(a, 2, c),
            Bit.Var(b, 3, c),
            Bit.Var(b, 2, c),
            Bit.Var(a, 1, c),
            Bit.Var(a, 0, c),
            Bit.Var(b, 1, c),
            Bit.Var(b, 0, c),
            Bit.Var(e, 3, c),
            Bit.Var(e, 2, c),
            Bit.Var(f, 3, c),
            Bit.Var(f, 2, c),
            Bit.Var(e, 1, c),
            Bit.Var(e, 0, c),
            Bit.Var(f, 1, c),
            Bit.Var(f, 0, c)
        ])

    i0 = MakeWord('a', 'b', 'e', 'f', "31m")
    i1 = MakeWord('c', 'd', 'g', 'h', "32m")
    i2 = MakeWord('A', 'B', 'E', 'F', "33m")
    i3 = MakeWord('C', 'D', 'G', 'H', "34m")

    Array.Print("Data:", i0, i1, i2, i3)

    a0 = (i0.lsl(4) & m0) | (i1 & m1)
    a2 = (i2.lsl(4) & m0) | (i3 & m1)
    a1 = (i0 & m0) | (i1.lsr(4) & m1)
    a3 = (i2 & m0) | (i3.lsr(4) & m1)

    Array.Print("Swap 4x2:", a0, a1, a2, a3)

    b0 = a0 & m3
    b2 = a1 & m3
    b4 = a2 & m3
    b6 = a3 & m3

    b1 = a0.lsr(1) & m3
    b3 = a1.lsr(1) & m3
    b5 = a2.lsr(1) & m3
    b7 = a3.lsr(1) & m3

    Array.Print("Bitplanes:", b0, b1, b2, b3, b4, b5, b6, b7)

    b0 = (a0 & m3) | (a0.lsl(1) & m2)
    b2 = (a1 & m3) | (a1.lsl(1) & m2)
    b4 = (a2 & m3) | (a2.lsl(1) & m2)
    b6 = (a3 & m3) | (a3.lsl(1) & m2)

    b1 = (a0.lsr(1) & m3) | (a0 & m2)
    b3 = (a1.lsr(1) & m3) | (a1 & m2)
    b5 = (a2.lsr(1) & m3) | (a2 & m2)
    b7 = (a3.lsr(1) & m3) | (a3 & m2)

    Array.Print("Bitplanes:", b0, b1, b2, b3, b4, b5, b6, b7)
Esempio n. 15
0
def c2p(bitplane_output=True):
    m0 = Word.Mask('00ff')
    m1 = Word.Mask('0f0f')
    m2 = Word.Mask('3333')
    m3 = Word.Mask('5555')

    print "=[ c2p 1x1 4bpl (blitter) ]=".center(48, '-')

    def MakeWord(chars, color):
        bits = []
        for c in chars:
            for i in range(4):
                bits.append(Bit.Var(c, i, color))
        return Word(bits)

    A = Array.Make(MakeWord)
    N = len(A)
    Array.Print("Data:", *A)

    B = Array.Zero(N, 16)
    Blit(lambda a, b: ((a >> 8) & m0) | (b & ~m0), N / 4, 2, Channel(A, 2, 2),
         Channel(A, 0, 2), Channel(B, 0, 2))
    Blit(lambda a, b: ((a << 8) & ~m0) | (b & m0), N / 4, 2, Channel(A, 0, 2),
         Channel(A, 2, 2), Channel(B, 2, 2))
    Array.Print("Swap 8x4:", *B)

    C = Array.Zero(N, 16)
    Blit(lambda a, b: ((a >> 4) & m1) | (b & ~m1), N / 2, 1, Channel(B, 1, 1),
         Channel(B, 0, 1), Channel(C, 0, 1))
    Blit(lambda a, b: ((a << 4) & ~m1) | (b & m1), N / 2, 1, Channel(B, 0, 1),
         Channel(B, 1, 1), Channel(C, 1, 1))
    Array.Print("Swap 4x2:", *C)

    D = Array.Zero(N, 16)
    Blit(lambda a, b: ((a >> 2) & m2) | (b & ~m2), N / 4, 2, Channel(C, 2, 2),
         Channel(C, 0, 2), Channel(D, 0, 2))
    Blit(lambda a, b: ((a << 2) & ~m2) | (b & m2), N / 4, 2, Channel(C, 0, 2),
         Channel(C, 2, 2), Channel(D, 2, 2))
    Array.Print("Swap 2x2:", *D)

    if bitplane_output:
        E = [Array.Zero(N / 4, 16) for i in range(4)]
        Blit(lambda a, b: ((a >> 1) & m3) | (b & ~m3), N / 4, 1,
             Channel(D, 1, 3), Channel(D, 0, 3), Channel(E[0], 0, 0))
        Blit(lambda a, b: ((a >> 1) & m3) | (b & ~m3), N / 4, 1,
             Channel(D, 3, 3), Channel(D, 2, 3), Channel(E[2], 0, 0))
        Blit(lambda a, b: ((a << 1) & ~m3) | (b & m3), N / 4, 1,
             Channel(D, 0, 3), Channel(D, 1, 3), Channel(E[1], 0, 0))
        Blit(lambda a, b: ((a << 1) & ~m3) | (b & m3), N / 4, 1,
             Channel(D, 2, 3), Channel(D, 3, 3), Channel(E[3], 0, 0))
        print("Bitplanes:")
        Array.Print("[0]:", *E[0])
        Array.Print("[1]:", *E[1])
        Array.Print("[2]:", *E[2])
        Array.Print("[3]:", *E[3])
    else:
        E = Array.Zero(N, 16)
        Blit(lambda a, b: ((a >> 1) & m3) | (b & ~m3), N / 2, 1,
             Channel(D, 1, 1), Channel(D, 0, 1), Channel(E, 0, 1))
        Blit(lambda a, b: ((a << 1) & ~m3) | (b & m3), N / 2, 1,
             Channel(D, 0, 1), Channel(D, 1, 1), Channel(E, 1, 1))
        Array.Print("Swap 1x1:", *E)
Esempio n. 16
0
def c2p_1_1_8():
    m0 = Word.Mask('0f0f')
    m1 = Word.Mask('f0f0')
    m2 = Word.Mask('3333')
    m3 = Word.Mask('cccc')
    m4 = Word.Mask('5555')
    m5 = Word.Mask('aaaa')

    print "c2p 1x1 8-bitplanes".center(96, '-')

    a0 = Word.Data('a', color="31m")
    a1 = Word.Data('b', color="32m")
    a2 = Word.Data('c', color="33m")
    a3 = Word.Data('d', color="34m")
    a4 = Word.Data('e', color="35m")
    a5 = Word.Data('f', color="36m")
    a6 = Word.Data('g', color="37m")
    a7 = Word.Data('h', color="38m")

    Array.Print("Data:", a0, a1, a2, a3, a4, a5, a6, a7)

    b0 = (a0 & m1) | (a4.lsr(4) & m0)
    b1 = (a1 & m1) | (a5.lsr(4) & m0)
    b2 = (a2 & m1) | (a6.lsr(4) & m0)
    b3 = (a3 & m1) | (a7.lsr(4) & m0)
    b4 = (a0.lsl(4) & m1) | (a4 & m0)
    b5 = (a1.lsl(4) & m1) | (a5 & m0)
    b6 = (a2.lsl(4) & m1) | (a6 & m0)
    b7 = (a3.lsl(4) & m1) | (a7 & m0)

    Array.Print("Swap 4x4:", b0, b1, b2, b3, b4, b5, b6, b7)

    c0 = (b0 & m3) | (b2.lsr(2) & m2)
    c1 = (b1 & m3) | (b3.lsr(2) & m2)
    c2 = (b0.lsl(2) & m3) | (b2 & m2)
    c3 = (b1.lsl(2) & m3) | (b3 & m2)
    c4 = (b4 & m3) | (b6.lsr(2) & m2)
    c5 = (b5 & m3) | (b7.lsr(2) & m2)
    c6 = (b4.lsl(2) & m3) | (b6 & m2)
    c7 = (b5.lsl(2) & m3) | (b7 & m2)

    Array.Print("Swap 2x2:", c0, c1, c2, c3, c4, c5, c6, c7)

    d0 = (c0 & m5) | (c1.lsr(1) & m4)
    d1 = (c0.lsl(1) & m5) | (c1 & m4)
    d2 = (c2 & m5) | (c3.lsr(1) & m4)
    d3 = (c2.lsl(1) & m5) | (c3 & m4)
    d4 = (c4 & m5) | (c5.lsr(1) & m4)
    d5 = (c4.lsl(1) & m5) | (c5 & m4)
    d6 = (c6 & m5) | (c7.lsr(1) & m4)
    d7 = (c6.lsl(1) & m5) | (c7 & m4)

    Array.Print("Swap 1x1:", d0, d1, d2, d3, d4, d5, d6, d7)
Esempio n. 17
0
from common import Rule, Word

rules = []
rules.append(Rule('S', 'NP VP'))
rules.append(Rule('NP', 'ART ADJ N'))
rules.append(Rule('NP', 'ART N'))
rules.append(Rule('NP', 'ADJ N'))
rules.append(Rule('VP', 'AUX VP'))
rules.append(Rule('VP', 'V NP'))

words = 'the large can can hold the water'
words = words.split()

dictionary = []
dictionary.append(Word('the', 'ART'))
dictionary.append(Word('large', 'ADJ'))
dictionary.append(Word('can', 'N AUX V'))
dictionary.append(Word('hold', 'N V'))
dictionary.append(Word('water', 'N V'))

if __name__ == '__main__':
    for i in rules:
        print(i)

    print()

    for i in dictionary:
        print(i)
def c2p(bitplane_output=True):
    m0 = Word.Mask('ff00')
    m1 = Word.Mask('f0f0')
    m2 = Word.Mask('cccc')

    print "=[ c2p 1x1 4bpl (blitter + mangled) ]=".center(48, '-')

    def MakeWord(c, color):
        return Word([
            Bit.Var(c[0], 0, color),
            Bit.Var(c[1], 0, color),
            Bit.Var(c[0], 2, color),
            Bit.Var(c[1], 2, color),
            Bit.Var(c[0], 1, color),
            Bit.Var(c[1], 1, color),
            Bit.Var(c[0], 3, color),
            Bit.Var(c[1], 3, color),
            Bit.Var(c[2], 0, color),
            Bit.Var(c[3], 0, color),
            Bit.Var(c[2], 2, color),
            Bit.Var(c[3], 2, color),
            Bit.Var(c[2], 1, color),
            Bit.Var(c[3], 1, color),
            Bit.Var(c[2], 3, color),
            Bit.Var(c[3], 3, color)
        ])

    A = Array.Make(MakeWord)
    N = len(A)
    Array.Print("Data:", *A)

    B = Array.Zero(N, 16)
    Blit(lambda a, b: (a & m0) | ((b >> 8) & ~m0), N / 4, 2, Channel(A, 0, 2),
         Channel(A, 2, 2), Channel(B, 0, 2))
    Blit(lambda a, b: ((a << 8) & m0) | (b & ~m0), N / 4, 2, Channel(A, 0, 2),
         Channel(A, 2, 2), Channel(B, 2, 2))
    Array.Print("Swap 8x4:", *B)

    C = Array.Zero(N, 16)
    Blit(lambda a, b: (a & m1) | ((b >> 4) & ~m1), N / 2, 1, Channel(B, 0, 1),
         Channel(B, 1, 1), Channel(C, 0, 1))
    Blit(lambda a, b: ((a << 4) & m1) | (b & ~m1), N / 2, 1, Channel(B, 0, 1),
         Channel(B, 1, 1), Channel(C, 1, 1))
    Array.Print("Swap 4x2:", *C)

    if bitplane_output:
        D = [Array.Zero(N / 4, 16) for i in range(4)]
        Blit(lambda a, b: (a & m2) | ((b >> 2) & ~m2), N / 4, 1,
             Channel(C, 0, 3), Channel(C, 2, 3), Channel(D[0], 0, 0))
        Blit(lambda a, b: (a & m2) | ((b >> 2) & ~m2), N / 4, 1,
             Channel(C, 1, 3), Channel(C, 3, 3), Channel(D[1], 0, 0))
        Blit(lambda a, b: ((a << 2) & m2) | (b & ~m2), N / 4, 1,
             Channel(C, 0, 3), Channel(C, 2, 3), Channel(D[2], 0, 0))
        Blit(lambda a, b: ((a << 2) & m2) | (b & ~m2), N / 4, 1,
             Channel(C, 1, 3), Channel(C, 3, 3), Channel(D[3], 0, 0))
        print("Bitplanes:")
        Array.Print("[0]:", *D[0])
        Array.Print("[1]:", *D[1])
        Array.Print("[2]:", *D[2])
        Array.Print("[3]:", *D[3])
    else:
        D = Array.Zero(N, 16)
        Blit(lambda a, b: ((a >> 2) & ~m2) | (b & m2), N / 4, 2,
             Channel(C, 2, 2), Channel(C, 0, 2), Channel(D, 0, 2))
        Blit(lambda a, b: ((a << 2) & m2) | (b & ~m2), N / 4, 2,
             Channel(C, 0, 2), Channel(C, 2, 2), Channel(D, 2, 2))
        Array.Print("Swap 2x2:", *D)
def c2p(bitplane_output=True):
    print "=[ c2p 2x1 4bpl (blitter + mangled) ]=".center(48, '-')

    # premangled pixels:
    # 1) [- - - - a b c d] => [a b - - c d - -]
    # 2) [- - - - e f g h] => [- - e f - - g h]
    def MakeWord(c, color):
        return Word([
            Bit.Var(c[0], 0, color),
            Bit.Var(c[0], 1, color),
            Bit.Var(c[1], 0, color),
            Bit.Var(c[1], 1, color),
            Bit.Var(c[0], 2, color),
            Bit.Var(c[0], 3, color),
            Bit.Var(c[1], 2, color),
            Bit.Var(c[1], 3, color),
            Bit.Var(c[2], 0, color),
            Bit.Var(c[2], 1, color),
            Bit.Var(c[3], 0, color),
            Bit.Var(c[3], 1, color),
            Bit.Var(c[2], 2, color),
            Bit.Var(c[2], 3, color),
            Bit.Var(c[3], 2, color),
            Bit.Var(c[3], 3, color)
        ])

    # premangled pixel buffer: [AB CD EF GH] => [AB EF CD GH]
    def ArrayMake(fn):
        return [
            fn("abef", color="31m"),
            fn("cdgh", color="32m"),
            fn("ijmn", color="33m"),
            fn("klop", color="34m"),
            fn("qruv", color="35m"),
            fn("stwx", color="36m"),
            fn("ABEF", color="31;1m"),
            fn("CDGH", color="32;1m"),
            fn("IJMN", color="33;1m"),
            fn("KLOP", color="34;1m"),
            fn("QRUV", color="35;1m"),
            fn("STWX", color="36;1m")
        ]

    A = ArrayMake(MakeWord)
    N = len(A)
    Array.Print("Data:", *A)

    m0 = Word.Mask('f0f0')
    m1 = Word.Mask('aaaa')

    B = [Array.Zero(N / 2, 16) for i in range(2)]
    Blit(lambda a, b: (a & m0) | ((b >> 4) & ~m0), N / 2, 1, Channel(A, 0, 1),
         Channel(A, 1, 1), Channel(B[0]))
    Blit(lambda a, b: ((a << 4) & m0) | (b & ~m0), N / 2, 1, Channel(A, 0, 1),
         Channel(A, 1, 1), Channel(B[1]))
    print("Swap 4x2:")
    Array.Print("[0]:", *B[0])
    Array.Print("[1]:", *B[1])

    C = [Array.Zero(N / 2, 16) for i in range(4)]
    Blit(lambda a, b: (a & m1) | ((b >> 1) & ~m1), N / 2, 1, Channel(B[0]),
         Channel(B[0]), Channel(C[0]))
    Blit(lambda a, b: (a & m1) | ((b >> 1) & ~m1), N / 2, 1, Channel(B[1]),
         Channel(B[1]), Channel(C[2]))
    Blit(lambda a, b: ((a << 1) & m1) | (b & ~m1), N / 2, 1, Channel(B[0]),
         Channel(B[0]), Channel(C[1]))
    Blit(lambda a, b: ((a << 1) & m1) | (b & ~m1), N / 2, 1, Channel(B[1]),
         Channel(B[1]), Channel(C[3]))
    print("Expand 2x1:")
    Array.Print("[0]:", *C[0])
    Array.Print("[1]:", *C[1])
    Array.Print("[2]:", *C[2])
    Array.Print("[3]:", *C[3])
Esempio n. 20
0
def c2p(bitplane_output=True):
    m0 = Word.Mask('00ff')
    m1 = Word.Mask('0f0f')
    m2 = Word.Mask('5555')

    print "=[ c2p 2x1 4bpl (blitter + mangled) ]=".center(48, '-')

    # premangled pixels:
    # 1) [- - - - a b c d] => [a b - - c d - -]
    # 2) [- - - - e f g h] => [- - e f - - g h]
    def MakeWord(c, color):
        return Word([Bit.Var(c[0], 0, color),
                     Bit.Var(c[0], 1, color),
                     Bit.Var(c[1], 0, color),
                     Bit.Var(c[1], 1, color),
                     Bit.Var(c[0], 2, color),
                     Bit.Var(c[0], 3, color),
                     Bit.Var(c[1], 2, color),
                     Bit.Var(c[1], 3, color),
                     Bit.Var(c[2], 0, color),
                     Bit.Var(c[2], 1, color),
                     Bit.Var(c[3], 0, color),
                     Bit.Var(c[3], 1, color),
                     Bit.Var(c[2], 2, color),
                     Bit.Var(c[2], 3, color),
                     Bit.Var(c[3], 2, color),
                     Bit.Var(c[3], 3, color)])

    A = Array.Make(MakeWord)
    N = len(A)
    Array.Print("Data:", *A)

    B = Array.Zero(N, 16)
    Blit(lambda a, b: ((a >> 8) & m0) | (b & ~m0),
         N / 2, 1, Channel(A, 1, 1), Channel(A, 0, 1), Channel(B, 0, 1))
    Blit(lambda a, b: ((a << 8) & ~m0) | (b & m0),
         N / 2, 1, Channel(A, 0, 1), Channel(A, 1, 1), Channel(B, 1, 1))
    Array.Print("Swap 8x2:", *B)

    C = Array.Zero(N, 16)
    Blit(lambda a, b: ((a & ~m1) | ((b >> 4) & m1)),
         N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 0, 1))
    Blit(lambda a, b: (((a << 4) & ~m1) | (b & m1)),
         N / 2, 1, Channel(B, 0, 1), Channel(B, 1, 1), Channel(C, 1, 1))
    Array.Print("Swap 4x2:", *C)

    if bitplane_output:
        D = [Array.Zero(N / 2, 16) for i in range(4)]
        Blit(lambda a, b: (((a >> 1) & m2) | (b & ~m2)),
             N / 2, 1, Channel(C, 0, 1), Channel(C, 0, 1), Channel(D[0], 0, 0))
        Blit(lambda a, b: (((a >> 1) & m2) | (b & ~m2)),
             N / 2, 1, Channel(C, 1, 1), Channel(C, 1, 1), Channel(D[2], 0, 0))
        Blit(lambda a, b: (((a << 1) & ~m2) | (b & m2)),
             N / 2, 1, Channel(C, 0, 1), Channel(C, 0, 1), Channel(D[1], 0, 0))
        Blit(lambda a, b: (((a << 1) & ~m2) | (b & m2)),
             N / 2, 1, Channel(C, 1, 1), Channel(C, 1, 1), Channel(D[3], 0, 0))
        print("Bitplanes:")
        Array.Print("[0]:", *D[0])
        Array.Print("[1]:", *D[1])
        Array.Print("[2]:", *D[2])
        Array.Print("[3]:", *D[3])
    else:
        D = Array.Zero(N * 2, 16)
        Blit(lambda a, b: (((a >> 1) & m2) | (b & ~m2)),
             N, 1, Channel(C, 0, 0), Channel(C, 0, 0), Channel(D, 0, 1))
        Blit(lambda a, b: (((a << 1) & ~m2) | (b & m2)),
             N, 1, Channel(C, 0, 0), Channel(C, 0, 0), Channel(D, 1, 1))
        Array.Print("Expand 2x1:", *D)