PalmImagePlugin.py 8.9 KB

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