You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
5.4 KiB

4 years ago
  1. #
  2. # The Python Imaging Library.
  3. #
  4. # MSP file handling
  5. #
  6. # This is the format used by the Paint program in Windows 1 and 2.
  7. #
  8. # History:
  9. # 95-09-05 fl Created
  10. # 97-01-03 fl Read/write MSP images
  11. # 17-02-21 es Fixed RLE interpretation
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1995-97.
  15. # Copyright (c) Eric Soroos 2017.
  16. #
  17. # See the README file for information on usage and redistribution.
  18. #
  19. # More info on this format: https://archive.org/details/gg243631
  20. # Page 313:
  21. # Figure 205. Windows Paint Version 1: "DanM" Format
  22. # Figure 206. Windows Paint Version 2: "LinS" Format. Used in Windows V2.03
  23. #
  24. # See also: http://www.fileformat.info/format/mspaint/egff.htm
  25. from . import Image, ImageFile
  26. from ._binary import i16le as i16, o16le as o16, i8
  27. import struct
  28. import io
  29. __version__ = "0.1"
  30. #
  31. # read MSP files
  32. def _accept(prefix):
  33. return prefix[:4] in [b"DanM", b"LinS"]
  34. ##
  35. # Image plugin for Windows MSP images. This plugin supports both
  36. # uncompressed (Windows 1.0).
  37. class MspImageFile(ImageFile.ImageFile):
  38. format = "MSP"
  39. format_description = "Windows Paint"
  40. def _open(self):
  41. # Header
  42. s = self.fp.read(32)
  43. if s[:4] not in [b"DanM", b"LinS"]:
  44. raise SyntaxError("not an MSP file")
  45. # Header checksum
  46. checksum = 0
  47. for i in range(0, 32, 2):
  48. checksum = checksum ^ i16(s[i:i+2])
  49. if checksum != 0:
  50. raise SyntaxError("bad MSP checksum")
  51. self.mode = "1"
  52. self._size = i16(s[4:]), i16(s[6:])
  53. if s[:4] == b"DanM":
  54. self.tile = [("raw", (0, 0)+self.size, 32, ("1", 0, 1))]
  55. else:
  56. self.tile = [("MSP", (0, 0)+self.size, 32, None)]
  57. class MspDecoder(ImageFile.PyDecoder):
  58. # The algo for the MSP decoder is from
  59. # http://www.fileformat.info/format/mspaint/egff.htm
  60. # cc-by-attribution -- That page references is taken from the
  61. # Encyclopedia of Graphics File Formats and is licensed by
  62. # O'Reilly under the Creative Common/Attribution license
  63. #
  64. # For RLE encoded files, the 32byte header is followed by a scan
  65. # line map, encoded as one 16bit word of encoded byte length per
  66. # line.
  67. #
  68. # NOTE: the encoded length of the line can be 0. This was not
  69. # handled in the previous version of this encoder, and there's no
  70. # mention of how to handle it in the documentation. From the few
  71. # examples I've seen, I've assumed that it is a fill of the
  72. # background color, in this case, white.
  73. #
  74. #
  75. # Pseudocode of the decoder:
  76. # Read a BYTE value as the RunType
  77. # If the RunType value is zero
  78. # Read next byte as the RunCount
  79. # Read the next byte as the RunValue
  80. # Write the RunValue byte RunCount times
  81. # If the RunType value is non-zero
  82. # Use this value as the RunCount
  83. # Read and write the next RunCount bytes literally
  84. #
  85. # e.g.:
  86. # 0x00 03 ff 05 00 01 02 03 04
  87. # would yield the bytes:
  88. # 0xff ff ff 00 01 02 03 04
  89. #
  90. # which are then interpreted as a bit packed mode '1' image
  91. _pulls_fd = True
  92. def decode(self, buffer):
  93. img = io.BytesIO()
  94. blank_line = bytearray((0xff,)*((self.state.xsize+7)//8))
  95. try:
  96. self.fd.seek(32)
  97. rowmap = struct.unpack_from("<%dH" % (self.state.ysize),
  98. self.fd.read(self.state.ysize*2))
  99. except struct.error:
  100. raise IOError("Truncated MSP file in row map")
  101. for x, rowlen in enumerate(rowmap):
  102. try:
  103. if rowlen == 0:
  104. img.write(blank_line)
  105. continue
  106. row = self.fd.read(rowlen)
  107. if len(row) != rowlen:
  108. raise IOError(
  109. "Truncated MSP file, expected %d bytes on row %s",
  110. (rowlen, x))
  111. idx = 0
  112. while idx < rowlen:
  113. runtype = i8(row[idx])
  114. idx += 1
  115. if runtype == 0:
  116. (runcount, runval) = struct.unpack_from("Bc", row, idx)
  117. img.write(runval * runcount)
  118. idx += 2
  119. else:
  120. runcount = runtype
  121. img.write(row[idx:idx+runcount])
  122. idx += runcount
  123. except struct.error:
  124. raise IOError("Corrupted MSP file in row %d" % x)
  125. self.set_as_raw(img.getvalue(), ("1", 0, 1))
  126. return 0, 0
  127. Image.register_decoder('MSP', MspDecoder)
  128. #
  129. # write MSP files (uncompressed only)
  130. def _save(im, fp, filename):
  131. if im.mode != "1":
  132. raise IOError("cannot write mode %s as MSP" % im.mode)
  133. # create MSP header
  134. header = [0] * 16
  135. header[0], header[1] = i16(b"Da"), i16(b"nM") # version 1
  136. header[2], header[3] = im.size
  137. header[4], header[5] = 1, 1
  138. header[6], header[7] = 1, 1
  139. header[8], header[9] = im.size
  140. checksum = 0
  141. for h in header:
  142. checksum = checksum ^ h
  143. header[12] = checksum # FIXME: is this the right field?
  144. # header
  145. for h in header:
  146. fp.write(o16(h))
  147. # image body
  148. ImageFile._save(im, fp, [("raw", (0, 0)+im.size, 32, ("1", 0, 1))])
  149. #
  150. # registry
  151. Image.register_open(MspImageFile.format, MspImageFile, _accept)
  152. Image.register_save(MspImageFile.format, _save)
  153. Image.register_extension(MspImageFile.format, ".msp")