[IMAGE-SIG] PIL 0.3a1 and xbm file formats

Fredrik Lundh fredrik@pythonware.com
Tue, 22 Jul 1997 15:05:34 +0200

This is a multi-part message in MIME format.

Content-Type: text/plain;
Content-Transfer-Encoding: 7bit

>Most xbm format images can be opened ok, but there are a few
>with extra #define lines at the top, which PIL doesn't like.

The driver was supposed to handle that; not sure what made me
think ".*" would match multiple lines :-(

I've attached a more tolerant version of the XBM parser. Should
probably rewrite the parser from scratch, but I'll take that another

Cheers /F (on vacation)

Content-Type: application/octet-stream;
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;

# The Python Imaging Library.
# $Id: XbmImagePlugin.py,v 1.3 1996/11/10 17:52:14 fredrik Exp $
# XBM File handling
# History:
#	95-09-08 fl	Created
#	96-11-01 fl	Added save support
#	97-07-07 fl	Made header parser more tolerant
#	97-07-22 fl	Fixed yet another parser bug
# Copyright (c) Fredrik Lundh 1996-97.  All rights reserved.
# See the README file for information on usage and redistribution.

__version__ = "0.3"

import regex, string
import Image, ImageFile

# XBM header
xbm_head = regex.compile(
    "#define[ \t]+[^_]*_width[ \t]+\([0-9]*\)[\r\n]+"
    "#define[ \t]+[^_]*_height[ \t]+\([0-9]*\)[\r\n]+"

def _accept(prefix):
    return prefix[:7] == "#define"

class XbmImageFile(ImageFile.ImageFile):

    format = "XBM"
    format_description = "X11 Bitmap"

    def _open(self):

	s = xbm_head.match(self.fp.read(512))

	if s > 0:

	    xsize = string.atoi(xbm_head.group(1))
	    ysize = string.atoi(xbm_head.group(2))

	    self.mode = "1"
	    self.size = xsize, ysize

	    self.tile = [("xbm", (0, 0)+self.size, s, None)]

def _save(im, fp, filename):

    if im.mode != "1":
	raise IOError, "cannot write mode %s as XBM" % im.mode

    fp.write("#define im_width %d\n" % im.size[0])
    fp.write("#define im_height %d\n" % im.size[1])
    fp.write("static char im_bits[] = {\n")

    ImageFile._save(im, fp, [("xbm", (0,0)+im.size, 0, None)])


Image.register_open("XBM", XbmImageFile, _accept)
Image.register_save("XBM", _save)

Image.register_extension("XBM", ".xbm")

Image.register_mime("XBM", "image/xbm")


IMAGE-SIG - SIG on Image Processing with Python

send messages to: image-sig@python.org
administrivia to: image-sig-request@python.org