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.

236 lines
8.9 KiB

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