XVThumbImagePlugin.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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__ is deprecated and will be removed in a future version. Use
  22. # PIL.__version__ instead.
  23. __version__ = "0.1"
  24. _MAGIC = b"P7 332"
  25. # standard color palette for thumbnails (RGB332)
  26. PALETTE = b""
  27. for r in range(8):
  28. for g in range(8):
  29. for b in range(4):
  30. PALETTE = PALETTE + (
  31. o8((r * 255) // 7) + o8((g * 255) // 7) + o8((b * 255) // 3)
  32. )
  33. def _accept(prefix):
  34. return prefix[:6] == _MAGIC
  35. ##
  36. # Image plugin for XV thumbnail images.
  37. class XVThumbImageFile(ImageFile.ImageFile):
  38. format = "XVThumb"
  39. format_description = "XV thumbnail image"
  40. def _open(self):
  41. # check magic
  42. if not _accept(self.fp.read(6)):
  43. raise SyntaxError("not an XV thumbnail file")
  44. # Skip to beginning of next line
  45. self.fp.readline()
  46. # skip info comments
  47. while True:
  48. s = self.fp.readline()
  49. if not s:
  50. raise SyntaxError("Unexpected EOF reading XV thumbnail file")
  51. if i8(s[0]) != 35: # ie. when not a comment: '#'
  52. break
  53. # parse header line (already read)
  54. s = s.strip().split()
  55. self.mode = "P"
  56. self._size = int(s[0]), int(s[1])
  57. self.palette = ImagePalette.raw("RGB", PALETTE)
  58. self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), (self.mode, 0, 1))]
  59. # --------------------------------------------------------------------
  60. Image.register_open(XVThumbImageFile.format, XVThumbImageFile, _accept)