~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to build/pypng/pipcomposite

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# $URL: http://pypng.googlecode.com/svn/trunk/code/pipcomposite $
 
3
# $Rev: 208 $
 
4
# pipcomposite
 
5
# Image alpha compositing.
 
6
 
 
7
"""
 
8
pipcomposite [--background #rrggbb] file.png
 
9
 
 
10
Composite an image onto a background and output the result.  The
 
11
background colour is specified with an HTML-style triple (3, 6, or 12
 
12
hex digits), and defaults to black (#000).
 
13
 
 
14
The output PNG has no alpha channel.
 
15
 
 
16
It is valid for the input to have no alpha channel, but it doesn't
 
17
make much sense: the output will equal the input.
 
18
"""
 
19
 
 
20
import sys
 
21
 
 
22
def composite(out, inp, background):
 
23
    import png
 
24
 
 
25
    p = png.Reader(file=inp)
 
26
    w,h,pixel,info = p.asRGBA()
 
27
 
 
28
    outinfo = dict(info)
 
29
    outinfo['alpha'] = False
 
30
    outinfo['planes'] -= 1
 
31
    outinfo['interlace'] = 0
 
32
 
 
33
    # Convert to tuple and normalise to same range as source.
 
34
    background = rgbhex(background)
 
35
    maxval = float(2**info['bitdepth'] - 1)
 
36
    background = map(lambda x: int(0.5 + x*maxval/65535.0),
 
37
                     background)
 
38
    # Repeat background so that it's a whole row of sample values.
 
39
    background *= w
 
40
 
 
41
    def iterrow():
 
42
        for row in pixel:
 
43
            # Remove alpha from row, then create a list with one alpha
 
44
            # entry _per channel value_.
 
45
            # Squirrel the alpha channel away (and normalise it).
 
46
            t = map(lambda x: x/maxval, row[3::4])
 
47
            row = list(row)
 
48
            del row[3::4]
 
49
            alpha = row[:]
 
50
            for i in range(3):
 
51
                alpha[i::3] = t
 
52
            assert len(alpha) == len(row) == len(background)
 
53
            yield map(lambda a,v,b: int(0.5 + a*v + (1.0-a)*b),
 
54
                      alpha, row, background)
 
55
 
 
56
    w = png.Writer(**outinfo)
 
57
    w.write(out, iterrow())
 
58
 
 
59
def rgbhex(s):
 
60
    """Take an HTML style string of the form "#rrggbb" and return a
 
61
    colour (R,G,B) triple.  Following the initial '#' there can be 3, 6,
 
62
    or 12 digits (for 4-, 8- or 16- bits per channel).  In all cases the
 
63
    values are expanded to a full 16-bit range, so the returned values
 
64
    are all in range(65536).
 
65
    """
 
66
 
 
67
    assert s[0] == '#'
 
68
    s = s[1:]
 
69
    assert len(s) in (3,6,12)
 
70
 
 
71
    # Create a target list of length 12, and expand the string s to make
 
72
    # it length 12.
 
73
    l = ['z']*12
 
74
    if len(s) == 3:
 
75
        for i in range(4):
 
76
            l[i::4] = s
 
77
    if len(s) == 6:
 
78
        for i in range(2):
 
79
            l[i::4] = s[i::2]
 
80
            l[i+2::4] = s[i::2]
 
81
    if len(s) == 12:
 
82
        l[:] = s
 
83
    s = ''.join(l)
 
84
    return map(lambda x: int(x, 16), (s[:4], s[4:8], s[8:]))
 
85
 
 
86
class Usage(Exception):
 
87
    pass
 
88
 
 
89
def main(argv=None):
 
90
    import getopt
 
91
    import sys
 
92
 
 
93
    if argv is None:
 
94
        argv = sys.argv
 
95
 
 
96
    argv = argv[1:]
 
97
 
 
98
    try:
 
99
        try:
 
100
            opt,arg = getopt.getopt(argv, '',
 
101
                                    ['background='])
 
102
        except getopt.error, msg:
 
103
            raise Usage(msg)
 
104
        background = '#000'
 
105
        for o,v in opt:
 
106
            if o in ['--background']:
 
107
                background = v
 
108
    except Usage, err:
 
109
        print >>sys.stderr, __doc__
 
110
        print >>sys.stderr, str(err)
 
111
        return 2
 
112
 
 
113
    if len(arg) > 0:
 
114
        f = open(arg[0], 'rb')
 
115
    else:
 
116
        f = sys.stdin
 
117
    return composite(sys.stdout, f, background)
 
118
 
 
119
 
 
120
if __name__ == '__main__':
 
121
    main()