PalmImagePlugin.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. ##
  6. # Image plugin for Palm pixmap images (output only).
  7. ##
  8. from __future__ import annotations
  9. __lazy_modules__ = {"PIL._binary"}
  10. from . import Image, ImageFile
  11. from ._binary import o8
  12. from ._binary import o16be as o16b
  13. TYPE_CHECKING = False
  14. if TYPE_CHECKING:
  15. from typing import IO
  16. _Palm8BitColormapValues = (
  17. (255, 255, 255), (255, 204, 255), (255, 153, 255), (255, 102, 255),
  18. (255, 51, 255), (255, 0, 255), (255, 255, 204), (255, 204, 204),
  19. (255, 153, 204), (255, 102, 204), (255, 51, 204), (255, 0, 204),
  20. (255, 255, 153), (255, 204, 153), (255, 153, 153), (255, 102, 153),
  21. (255, 51, 153), (255, 0, 153), (204, 255, 255), (204, 204, 255),
  22. (204, 153, 255), (204, 102, 255), (204, 51, 255), (204, 0, 255),
  23. (204, 255, 204), (204, 204, 204), (204, 153, 204), (204, 102, 204),
  24. (204, 51, 204), (204, 0, 204), (204, 255, 153), (204, 204, 153),
  25. (204, 153, 153), (204, 102, 153), (204, 51, 153), (204, 0, 153),
  26. (153, 255, 255), (153, 204, 255), (153, 153, 255), (153, 102, 255),
  27. (153, 51, 255), (153, 0, 255), (153, 255, 204), (153, 204, 204),
  28. (153, 153, 204), (153, 102, 204), (153, 51, 204), (153, 0, 204),
  29. (153, 255, 153), (153, 204, 153), (153, 153, 153), (153, 102, 153),
  30. (153, 51, 153), (153, 0, 153), (102, 255, 255), (102, 204, 255),
  31. (102, 153, 255), (102, 102, 255), (102, 51, 255), (102, 0, 255),
  32. (102, 255, 204), (102, 204, 204), (102, 153, 204), (102, 102, 204),
  33. (102, 51, 204), (102, 0, 204), (102, 255, 153), (102, 204, 153),
  34. (102, 153, 153), (102, 102, 153), (102, 51, 153), (102, 0, 153),
  35. (51, 255, 255), (51, 204, 255), (51, 153, 255), (51, 102, 255),
  36. (51, 51, 255), (51, 0, 255), (51, 255, 204), (51, 204, 204),
  37. (51, 153, 204), (51, 102, 204), (51, 51, 204), (51, 0, 204),
  38. (51, 255, 153), (51, 204, 153), (51, 153, 153), (51, 102, 153),
  39. (51, 51, 153), (51, 0, 153), (0, 255, 255), (0, 204, 255),
  40. (0, 153, 255), (0, 102, 255), (0, 51, 255), (0, 0, 255),
  41. (0, 255, 204), (0, 204, 204), (0, 153, 204), (0, 102, 204),
  42. (0, 51, 204), (0, 0, 204), (0, 255, 153), (0, 204, 153),
  43. (0, 153, 153), (0, 102, 153), (0, 51, 153), (0, 0, 153),
  44. (255, 255, 102), (255, 204, 102), (255, 153, 102), (255, 102, 102),
  45. (255, 51, 102), (255, 0, 102), (255, 255, 51), (255, 204, 51),
  46. (255, 153, 51), (255, 102, 51), (255, 51, 51), (255, 0, 51),
  47. (255, 255, 0), (255, 204, 0), (255, 153, 0), (255, 102, 0),
  48. (255, 51, 0), (255, 0, 0), (204, 255, 102), (204, 204, 102),
  49. (204, 153, 102), (204, 102, 102), (204, 51, 102), (204, 0, 102),
  50. (204, 255, 51), (204, 204, 51), (204, 153, 51), (204, 102, 51),
  51. (204, 51, 51), (204, 0, 51), (204, 255, 0), (204, 204, 0),
  52. (204, 153, 0), (204, 102, 0), (204, 51, 0), (204, 0, 0),
  53. (153, 255, 102), (153, 204, 102), (153, 153, 102), (153, 102, 102),
  54. (153, 51, 102), (153, 0, 102), (153, 255, 51), (153, 204, 51),
  55. (153, 153, 51), (153, 102, 51), (153, 51, 51), (153, 0, 51),
  56. (153, 255, 0), (153, 204, 0), (153, 153, 0), (153, 102, 0),
  57. (153, 51, 0), (153, 0, 0), (102, 255, 102), (102, 204, 102),
  58. (102, 153, 102), (102, 102, 102), (102, 51, 102), (102, 0, 102),
  59. (102, 255, 51), (102, 204, 51), (102, 153, 51), (102, 102, 51),
  60. (102, 51, 51), (102, 0, 51), (102, 255, 0), (102, 204, 0),
  61. (102, 153, 0), (102, 102, 0), (102, 51, 0), (102, 0, 0),
  62. (51, 255, 102), (51, 204, 102), (51, 153, 102), (51, 102, 102),
  63. (51, 51, 102), (51, 0, 102), (51, 255, 51), (51, 204, 51),
  64. (51, 153, 51), (51, 102, 51), (51, 51, 51), (51, 0, 51),
  65. (51, 255, 0), (51, 204, 0), (51, 153, 0), (51, 102, 0),
  66. (51, 51, 0), (51, 0, 0), (0, 255, 102), (0, 204, 102),
  67. (0, 153, 102), (0, 102, 102), (0, 51, 102), (0, 0, 102),
  68. (0, 255, 51), (0, 204, 51), (0, 153, 51), (0, 102, 51),
  69. (0, 51, 51), (0, 0, 51), (0, 255, 0), (0, 204, 0),
  70. (0, 153, 0), (0, 102, 0), (0, 51, 0), (17, 17, 17),
  71. (34, 34, 34), (68, 68, 68), (85, 85, 85), (119, 119, 119),
  72. (136, 136, 136), (170, 170, 170), (187, 187, 187), (221, 221, 221),
  73. (238, 238, 238), (192, 192, 192), (128, 0, 0), (128, 0, 128),
  74. (0, 128, 0), (0, 128, 128), (0, 0, 0), (0, 0, 0),
  75. (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
  76. (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
  77. (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
  78. (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
  79. (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
  80. (0, 0, 0), (0, 0, 0), (0, 0, 0), (0, 0, 0),
  81. ) # fmt: skip
  82. # so build a prototype image to be used for palette resampling
  83. def build_prototype_image() -> Image.Image:
  84. image = Image.new("L", (1, len(_Palm8BitColormapValues)))
  85. image.putdata(list(range(len(_Palm8BitColormapValues))))
  86. palettedata: tuple[int, ...] = ()
  87. for colormapValue in _Palm8BitColormapValues:
  88. palettedata += colormapValue
  89. palettedata += (0, 0, 0) * (256 - len(_Palm8BitColormapValues))
  90. image.putpalette(palettedata)
  91. return image
  92. Palm8BitColormapImage = build_prototype_image()
  93. # OK, we now have in Palm8BitColormapImage,
  94. # a "P"-mode image with the right palette
  95. #
  96. # --------------------------------------------------------------------
  97. _FLAGS = {"custom-colormap": 0x4000, "is-compressed": 0x8000, "has-transparent": 0x2000}
  98. _COMPRESSION_TYPES = {"none": 0xFF, "rle": 0x01, "scanline": 0x00}
  99. #
  100. # --------------------------------------------------------------------
  101. ##
  102. # (Internal) Image save plugin for the Palm format.
  103. def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
  104. if im.mode == "P":
  105. rawmode = "P"
  106. bpp = 8
  107. version = 1
  108. elif im.mode == "L":
  109. if im.encoderinfo.get("bpp") in (1, 2, 4):
  110. # this is 8-bit grayscale, so we shift it to get the high-order bits,
  111. # and invert it because
  112. # Palm does grayscale from white (0) to black (1)
  113. bpp = im.encoderinfo["bpp"]
  114. maxval = (1 << bpp) - 1
  115. shift = 8 - bpp
  116. im = im.point(lambda x: maxval - (x >> shift))
  117. elif im.info.get("bpp") in (1, 2, 4):
  118. # here we assume that even though the inherent mode is 8-bit grayscale,
  119. # only the lower bpp bits are significant.
  120. # We invert them to match the Palm.
  121. bpp = im.info["bpp"]
  122. maxval = (1 << bpp) - 1
  123. im = im.point(lambda x: maxval - (x & maxval))
  124. else:
  125. msg = f"cannot write mode {im.mode} as Palm"
  126. raise OSError(msg)
  127. # we ignore the palette here
  128. im._mode = "P"
  129. rawmode = f"P;{bpp}"
  130. version = 1
  131. elif im.mode == "1":
  132. # monochrome -- write it inverted, as is the Palm standard
  133. rawmode = "1;I"
  134. bpp = 1
  135. version = 0
  136. else:
  137. msg = f"cannot write mode {im.mode} as Palm"
  138. raise OSError(msg)
  139. #
  140. # make sure image data is available
  141. im.load()
  142. # write header
  143. cols = im.size[0]
  144. rows = im.size[1]
  145. rowbytes = int((cols + (16 // bpp - 1)) / (16 // bpp)) * 2
  146. transparent_index = 0
  147. compression_type = _COMPRESSION_TYPES["none"]
  148. flags = 0
  149. if im.mode == "P":
  150. flags |= _FLAGS["custom-colormap"]
  151. colormap = im.im.getpalette()
  152. colors = len(colormap) // 3
  153. colormapsize = 4 * colors + 2
  154. else:
  155. colormapsize = 0
  156. if "offset" in im.info:
  157. offset = (rowbytes * rows + 16 + 3 + colormapsize) // 4
  158. else:
  159. offset = 0
  160. fp.write(o16b(cols) + o16b(rows) + o16b(rowbytes) + o16b(flags))
  161. fp.write(o8(bpp))
  162. fp.write(o8(version))
  163. fp.write(o16b(offset))
  164. fp.write(o8(transparent_index))
  165. fp.write(o8(compression_type))
  166. fp.write(o16b(0)) # reserved by Palm
  167. # now write colormap if necessary
  168. if colormapsize:
  169. fp.write(o16b(colors))
  170. for i in range(colors):
  171. fp.write(o8(i))
  172. fp.write(colormap[3 * i : 3 * i + 3])
  173. # now convert data to raw form
  174. ImageFile._save(
  175. im, fp, [ImageFile._Tile("raw", (0, 0, *im.size), 0, (rawmode, rowbytes, 1))]
  176. )
  177. if hasattr(fp, "flush"):
  178. fp.flush()
  179. #
  180. # --------------------------------------------------------------------
  181. Image.register_save("PALM", _save)
  182. Image.register_extension("PALM", ".palm")
  183. Image.register_mime("PALM", "image/palm")