MspImagePlugin.py 5.4 KB

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