~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/PIL/XbmImagePlugin.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (1.1.4)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130131204920-7tnuhqhlsqdza4c2
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# The Python Imaging Library.
 
3
# $Id$
 
4
#
 
5
# XBM File handling
 
6
#
 
7
# History:
 
8
# 1995-09-08 fl   Created
 
9
# 1996-11-01 fl   Added save support
 
10
# 1997-07-07 fl   Made header parser more tolerant
 
11
# 1997-07-22 fl   Fixed yet another parser bug
 
12
# 2001-02-17 fl   Use 're' instead of 'regex' (Python 2.1) (0.4)
 
13
# 2001-05-13 fl   Added hotspot handling (based on code from Bernhard Herzog)
 
14
# 2004-02-24 fl   Allow some whitespace before first #define
 
15
#
 
16
# Copyright (c) 1997-2004 by Secret Labs AB
 
17
# Copyright (c) 1996-1997 by Fredrik Lundh
 
18
#
 
19
# See the README file for information on usage and redistribution.
 
20
#
 
21
 
 
22
__version__ = "0.6"
 
23
 
 
24
import re, string
 
25
import Image, ImageFile
 
26
 
 
27
# XBM header
 
28
xbm_head = re.compile(
 
29
    "\s*#define[ \t]+[^_]*_width[ \t]+(?P<width>[0-9]+)[\r\n]+"
 
30
    "#define[ \t]+[^_]*_height[ \t]+(?P<height>[0-9]+)[\r\n]+"
 
31
    "(?P<hotspot>"
 
32
    "#define[ \t]+[^_]*_x_hot[ \t]+(?P<xhot>[0-9]+)[\r\n]+"
 
33
    "#define[ \t]+[^_]*_y_hot[ \t]+(?P<yhot>[0-9]+)[\r\n]+"
 
34
    ")?"
 
35
    "[\\000-\\377]*_bits\\[\\]"
 
36
)
 
37
 
 
38
def _accept(prefix):
 
39
    return string.lstrip(prefix)[:7] == "#define"
 
40
 
 
41
##
 
42
# Image plugin for X11 bitmaps.
 
43
 
 
44
class XbmImageFile(ImageFile.ImageFile):
 
45
 
 
46
    format = "XBM"
 
47
    format_description = "X11 Bitmap"
 
48
 
 
49
    def _open(self):
 
50
 
 
51
        m = xbm_head.match(self.fp.read(512))
 
52
 
 
53
        if m:
 
54
 
 
55
            xsize = int(m.group("width"))
 
56
            ysize = int(m.group("height"))
 
57
 
 
58
            if m.group("hotspot"):
 
59
                self.info["hotspot"] = (
 
60
                    int(m.group("xhot")), int(m.group("yhot"))
 
61
                    )
 
62
 
 
63
            self.mode = "1"
 
64
            self.size = xsize, ysize
 
65
 
 
66
            self.tile = [("xbm", (0, 0)+self.size, m.end(), None)]
 
67
 
 
68
 
 
69
def _save(im, fp, filename):
 
70
 
 
71
    if im.mode != "1":
 
72
        raise IOError, "cannot write mode %s as XBM" % im.mode
 
73
 
 
74
    fp.write("#define im_width %d\n" % im.size[0])
 
75
    fp.write("#define im_height %d\n" % im.size[1])
 
76
 
 
77
    hotspot = im.encoderinfo.get("hotspot")
 
78
    if hotspot:
 
79
        fp.write("#define im_x_hot %d\n" % hotspot[0])
 
80
        fp.write("#define im_y_hot %d\n" % hotspot[1])
 
81
 
 
82
    fp.write("static char im_bits[] = {\n")
 
83
 
 
84
    ImageFile._save(im, fp, [("xbm", (0,0)+im.size, 0, None)])
 
85
 
 
86
    fp.write("};\n")
 
87
 
 
88
 
 
89
Image.register_open("XBM", XbmImageFile, _accept)
 
90
Image.register_save("XBM", _save)
 
91
 
 
92
Image.register_extension("XBM", ".xbm")
 
93
 
 
94
Image.register_mime("XBM", "image/xbm")