2013-10-11 17:28:32 +00:00
|
|
|
#
|
|
|
|
# The Python Imaging Library.
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# MSP file handling
|
|
|
|
#
|
|
|
|
# This is the format used by the Paint program in Windows 1 and 2.
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 95-09-05 fl Created
|
|
|
|
# 97-01-03 fl Read/write MSP images
|
|
|
|
#
|
|
|
|
# Copyright (c) Secret Labs AB 1997.
|
|
|
|
# Copyright (c) Fredrik Lundh 1995-97.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
from PIL import Image, ImageFile, _binary
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
__version__ = "0.1"
|
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# read MSP files
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
i16 = _binary.i16le
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
def _accept(prefix):
|
2014-09-30 16:15:32 +00:00
|
|
|
return prefix[:4] in [b"DanM", b"LinS"]
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
##
|
|
|
|
# Image plugin for Windows MSP images. This plugin supports both
|
|
|
|
# uncompressed (Windows 1.0).
|
|
|
|
|
|
|
|
class MspImageFile(ImageFile.ImageFile):
|
|
|
|
|
|
|
|
format = "MSP"
|
|
|
|
format_description = "Windows Paint"
|
|
|
|
|
|
|
|
def _open(self):
|
|
|
|
|
|
|
|
# Header
|
|
|
|
s = self.fp.read(32)
|
2014-09-30 16:15:32 +00:00
|
|
|
if s[:4] not in [b"DanM", b"LinS"]:
|
|
|
|
raise SyntaxError("not an MSP file")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
# Header checksum
|
2015-11-25 01:25:01 +00:00
|
|
|
checksum = 0
|
2013-10-11 17:28:32 +00:00
|
|
|
for i in range(0, 32, 2):
|
2015-11-25 01:25:01 +00:00
|
|
|
checksum = checksum ^ i16(s[i:i+2])
|
|
|
|
if checksum != 0:
|
2014-09-30 16:15:32 +00:00
|
|
|
raise SyntaxError("bad MSP checksum")
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
self.mode = "1"
|
|
|
|
self.size = i16(s[4:]), i16(s[6:])
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
if s[:4] == b"DanM":
|
2015-11-25 01:25:01 +00:00
|
|
|
self.tile = [("raw", (0, 0)+self.size, 32, ("1", 0, 1))]
|
2013-10-11 17:28:32 +00:00
|
|
|
else:
|
2015-11-25 01:25:01 +00:00
|
|
|
self.tile = [("msp", (0, 0)+self.size, 32+2*self.size[1], None)]
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# write MSP files (uncompressed only)
|
|
|
|
|
2014-09-30 16:15:32 +00:00
|
|
|
o16 = _binary.o16le
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
|
2013-10-11 17:28:32 +00:00
|
|
|
def _save(im, fp, filename):
|
|
|
|
|
|
|
|
if im.mode != "1":
|
2014-09-30 16:15:32 +00:00
|
|
|
raise IOError("cannot write mode %s as MSP" % im.mode)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
# create MSP header
|
|
|
|
header = [0] * 16
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
header[0], header[1] = i16(b"Da"), i16(b"nM") # version 1
|
2013-10-11 17:28:32 +00:00
|
|
|
header[2], header[3] = im.size
|
|
|
|
header[4], header[5] = 1, 1
|
|
|
|
header[6], header[7] = 1, 1
|
|
|
|
header[8], header[9] = im.size
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
checksum = 0
|
2013-10-11 17:28:32 +00:00
|
|
|
for h in header:
|
2015-11-25 01:25:01 +00:00
|
|
|
checksum = checksum ^ h
|
|
|
|
header[12] = checksum # FIXME: is this the right field?
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
# header
|
|
|
|
for h in header:
|
|
|
|
fp.write(o16(h))
|
|
|
|
|
|
|
|
# image body
|
2015-11-25 01:25:01 +00:00
|
|
|
ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 32, ("1", 0, 1))])
|
2013-10-11 17:28:32 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# registry
|
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
Image.register_open(MspImageFile.format, MspImageFile, _accept)
|
|
|
|
Image.register_save(MspImageFile.format, _save)
|
2013-10-11 17:28:32 +00:00
|
|
|
|
2015-11-25 01:25:01 +00:00
|
|
|
Image.register_extension(MspImageFile.format, ".msp")
|