MspImagePlugin.py 5.7 KB

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