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.

80 lines
1.9 KiB

4 years ago
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # XV Thumbnail file handler by Charles E. "Gene" Cash
  6. # (gcash@magicnet.net)
  7. #
  8. # see xvcolor.c and xvbrowse.c in the sources to John Bradley's XV,
  9. # available from ftp://ftp.cis.upenn.edu/pub/xv/
  10. #
  11. # history:
  12. # 98-08-15 cec created (b/w only)
  13. # 98-12-09 cec added color palette
  14. # 98-12-28 fl added to PIL (with only a few very minor modifications)
  15. #
  16. # To do:
  17. # FIXME: make save work (this requires quantization support)
  18. #
  19. from . import Image, ImageFile, ImagePalette
  20. from ._binary import i8, o8
  21. __version__ = "0.1"
  22. _MAGIC = b"P7 332"
  23. # standard color palette for thumbnails (RGB332)
  24. PALETTE = b""
  25. for r in range(8):
  26. for g in range(8):
  27. for b in range(4):
  28. PALETTE = PALETTE + (o8((r*255)//7)+o8((g*255)//7)+o8((b*255)//3))
  29. def _accept(prefix):
  30. return prefix[:6] == _MAGIC
  31. ##
  32. # Image plugin for XV thumbnail images.
  33. class XVThumbImageFile(ImageFile.ImageFile):
  34. format = "XVThumb"
  35. format_description = "XV thumbnail image"
  36. def _open(self):
  37. # check magic
  38. if not _accept(self.fp.read(6)):
  39. raise SyntaxError("not an XV thumbnail file")
  40. # Skip to beginning of next line
  41. self.fp.readline()
  42. # skip info comments
  43. while True:
  44. s = self.fp.readline()
  45. if not s:
  46. raise SyntaxError("Unexpected EOF reading XV thumbnail file")
  47. if i8(s[0]) != 35: # ie. when not a comment: '#'
  48. break
  49. # parse header line (already read)
  50. s = s.strip().split()
  51. self.mode = "P"
  52. self._size = int(s[0]), int(s[1])
  53. self.palette = ImagePalette.raw("RGB", PALETTE)
  54. self.tile = [
  55. ("raw", (0, 0)+self.size,
  56. self.fp.tell(), (self.mode, 0, 1)
  57. )]
  58. # --------------------------------------------------------------------
  59. Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)