summaryrefslogtreecommitdiff
path: root/common/pixmap.py
blob: 13142d5c608418e13a8831331fc2d7acf69317cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from __future__ import generators
import cStringIO

def decodepixmap(data):
    f = cStringIO.StringIO(data)
    sig = f.readline().strip()
    assert sig == "P6"
    while 1:
        line = f.readline().strip()
        if not line.startswith('#'):
            break
    wh = line.split()
    w, h = map(int, wh)
    sig = f.readline().strip()
    assert sig == "255"
    data = f.read()
    f.close()
    return w, h, data

def encodepixmap(w, h, data):
    return 'P6\n%d %d\n255\n%s' % (w, h, data)

def cropimage((w, h, data), (x1, y1, w1, h1)):
    assert 0 <= x1 <= x1+w1 <= w
    assert 0 <= y1 <= y1+h1 <= h
    scanline = w*3
    lines = [data[p:p+w1*3]
             for p in range(y1*scanline + x1*3,
                            (y1+h1)*scanline + x1*3,
                            scanline)]
    return w1, h1, ''.join(lines)

def vflip(w, h, data):
    scanline = w*3
    lines = [data[p:p+scanline] for p in range(0, len(data), scanline)]
    lines.reverse()
    return ''.join(lines)

def hflip(w, h, data):
    scanline = w*3
    lines = [''.join([data[p:p+3] for p in range(p1+scanline-3, p1-3, -3)])
             for p1 in range(0, len(data), scanline)]
    return ''.join(lines)

def rotate_cw(w, h, data):
    scanline = w*3
    lastline = len(data) - scanline
    lines = [''.join([data[p:p+3] for p in range(lastline + p1, -1, -scanline)])
             for p1 in range(0, scanline, 3)]
    return ''.join(lines)

def rotate_ccw(w, h, data):
    scanline = w*3
    lines = [''.join([data[p:p+3] for p in range(p1, len(data), scanline)])
             for p1 in range(scanline-3, -3, -3)]
    return ''.join(lines)

def rotate_180(w, h, data):
    scanline = w*3
    lines = [''.join([data[p:p+3] for p in range(p1+scanline-3, p1-3, -3)])
             for p1 in range(0, len(data), scanline)]
    lines.reverse()
    return ''.join(lines)

def makebkgnd(w, h, data):
    scanline = 3*w
    result = []
    for position in range(0, scanline*h, scanline):
        line = []
        for p in range(position, position+scanline, 3):
            line.append(2 * (chr(ord(data[p  ]) >> 3) +
                             chr(ord(data[p+1]) >> 3) +
                             chr(ord(data[p+2]) >> 3)))
        line = ''.join(line)
        result.append(line)
        result.append(line)
    return w*2, h*2, ''.join(result)

translation_darker = ('\x00\x01' + '\x00'*126 +
                      ''.join([chr(n//4) for n in range(0,128)]))
translation_dragon = translation_darker[:255] + '\xC0'

def make_dark((w, h, data), translation):
    return w, h, data.translate(translation)

def col((r, g, b)):
    r = ord(r)
    g = ord(g)
    b = ord(b)
    return ((g>>2 + r>>3) << 24) | (b << 16) | (g << 8) | r

def imagezoomer(w, h, data):
    "Zoom a cartoon image by a factor of three, progressively."
    scale = 3
    scanline = 3*w
    rw = (w-1)*scale+1
    rh = (h-1)*scale+1
    pixels = []
    colcache = {}
    revcache = {}
    for base in range(0, scanline*h, scanline):
        line = []
        for x in range(w):
            key = data[base + 3*x : base + 3*(x+1)]
            try:
                c = colcache[key]
            except KeyError:
                c = colcache[key] = col(key)
                revcache[c] = key
            line.append(c)
        pixels.append(line)
        yield None

    Pairs = {
        (0, 0): [(0, 0, 0, 0),
                 (-1,0, 1, 0),
                 (0,-1, 0, 1)],
        (1, 0): [(0, 0, 1, 0),
                 (0, 1, 1,-1),
                 (0,-1, 1, 1)],
        (2, 0): [(0, 0, 1, 0),
                 (0, 1, 1,-1),
                 (0,-1, 1, 1)],
        (0, 1): [(0, 0, 0, 1),
                 (-1,0, 1, 1),
                 (1, 0,-1, 1)],
        (1, 1): [(0, 0, 1, 1),
                 (0, 1, 1, -1),
                 (1, 0,-1, 1)],
        (2, 1): [(1, 0, 0, 1),
                 (0,-1, 1, 1),
                 (0, 0, 2, 1)],
        (0, 2): [(0, 0, 0, 1),
                 (-1,0, 1, 1),
                 (1, 0,-1, 1)],
        (1, 2): [(0, 1, 1, 0),
                 (-1,0, 1, 1),
                 (0, 0, 1, 2)],
        (2, 2): [(0, 0, 1, 1),
                 (0, 1, 2, 0),
                 (1, 0, 0, 2)],
        }
    result = []
    for y in range(rh):
        yield None
        for x in range(rw):
            # ______________________________

            i = x//scale
            j = y//scale
            ps = []
            for dx1, dy1, dx2, dy2 in Pairs[x%scale, y%scale]:
                if (0 <= i+dx1 < w and 0 <= i+dx2 < w and
                    0 <= j+dy1 < h and 0 <= j+dy2 < h):
                    p1 = pixels[j+dy1][i+dx1]
                    p2 = pixels[j+dy2][i+dx2]
                    ps.append(max(p1, p2))
            p1 = min(ps)

            # ______________________________
            result.append(revcache[p1])
    data = ''.join(result)
    yield (rw, rh, data)