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

« back to all changes in this revision

Viewing changes to build/pypng/pipwindow

  • 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/pipwindow $
 
3
# $Rev: 173 $
 
4
 
 
5
# pipwindow
 
6
# Tool to crop/expand an image to a rectangular window.  Come the
 
7
# revolution this tool will allow the image and the window to be placed
 
8
# arbitrarily (in particular the window can be bigger than the picture
 
9
# and/or overlap it only partially) and the image can be OpenGL style
 
10
# border/repeat effects (repeat, mirrored repeat, clamp, fixed
 
11
# background colour, background colour from source file).  For now it
 
12
# only acts as crop.  The window must be no greater than the image in
 
13
# both x and y.
 
14
 
 
15
def window(tl, br, inp, out):
 
16
    """Place a window onto the image and cut-out the resulting
 
17
    rectangle.  The window is an axis aligned rectangle opposite corners
 
18
    at *tl* and *br* (each being an (x,y) pair). *inp* specifies the
 
19
    input file which should be a PNG image.
 
20
    """
 
21
 
 
22
    import png
 
23
 
 
24
    r = png.Reader(file=inp)
 
25
    x,y,pixels,meta = r.asDirect()
 
26
    if not (0 <= tl[0] < br[0] <= x):
 
27
        raise NotImplementedError()
 
28
    if not (0 <= tl[1] < br[1] <= y):
 
29
        raise NotImplementedError()
 
30
    # Compute left and right bounds for each row
 
31
    l = tl[0] * meta['planes']
 
32
    r = br[0] * meta['planes']
 
33
    def itercrop():
 
34
        """An iterator to perform the crop."""
 
35
 
 
36
        for i,row in enumerate(pixels):
 
37
            if i < tl[1]:
 
38
                continue
 
39
            if i >= br[1]:
 
40
                # Same as "raise StopIteration"
 
41
                return
 
42
            yield row[l:r]
 
43
    meta['size'] = (br[0]-tl[0], br[1]-tl[1])
 
44
    w = png.Writer(**meta)
 
45
    w.write(out, itercrop())
 
46
 
 
47
def main(argv=None):
 
48
    import sys
 
49
 
 
50
    if argv is None:
 
51
        argv = sys.argv
 
52
    argv = argv[1:]
 
53
 
 
54
    tl = (0,0)
 
55
    br = tuple(map(int, argv[:2]))
 
56
    if len(argv) >= 4:
 
57
        tl = br
 
58
        br = tuple(map(int, argv[2:4]))
 
59
    if len(argv) in (2, 4):
 
60
        f = sys.stdin
 
61
    else:
 
62
        f = open(argv[-1], 'rb')
 
63
 
 
64
    return window(tl, br, f, sys.stdout)
 
65
 
 
66
if __name__ == '__main__':
 
67
    main()