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.

84 lines
2.2 KiB

4 years ago
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # GD file handling
  6. #
  7. # History:
  8. # 1996-04-12 fl Created
  9. #
  10. # Copyright (c) 1997 by Secret Labs AB.
  11. # Copyright (c) 1996 by Fredrik Lundh.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15. # NOTE: This format cannot be automatically recognized, so the
  16. # class is not registered for use with Image.open(). To open a
  17. # gd file, use the GdImageFile.open() function instead.
  18. # THE GD FORMAT IS NOT DESIGNED FOR DATA INTERCHANGE. This
  19. # implementation is provided for convenience and demonstrational
  20. # purposes only.
  21. from . import ImageFile, ImagePalette
  22. from ._binary import i8, i16be as i16, i32be as i32
  23. __version__ = "0.1"
  24. ##
  25. # Image plugin for the GD uncompressed format. Note that this format
  26. # is not supported by the standard <b>Image.open</b> function. To use
  27. # this plugin, you have to import the <b>GdImageFile</b> module and
  28. # use the <b>GdImageFile.open</b> function.
  29. class GdImageFile(ImageFile.ImageFile):
  30. format = "GD"
  31. format_description = "GD uncompressed images"
  32. def _open(self):
  33. # Header
  34. s = self.fp.read(1037)
  35. if not i16(s[:2]) in [65534, 65535]:
  36. raise SyntaxError("Not a valid GD 2.x .gd file")
  37. self.mode = "L" # FIXME: "P"
  38. self._size = i16(s[2:4]), i16(s[4:6])
  39. trueColor = i8(s[6])
  40. trueColorOffset = 2 if trueColor else 0
  41. # transparency index
  42. tindex = i32(s[7+trueColorOffset:7+trueColorOffset+4])
  43. if tindex < 256:
  44. self.info["transparency"] = tindex
  45. self.palette = ImagePalette.raw("XBGR", s[7+trueColorOffset+4:7+trueColorOffset+4+256*4])
  46. self.tile = [("raw", (0, 0)+self.size, 7+trueColorOffset+4+256*4,
  47. ("L", 0, 1))]
  48. def open(fp, mode="r"):
  49. """
  50. Load texture from a GD image file.
  51. :param filename: GD file name, or an opened file handle.
  52. :param mode: Optional mode. In this version, if the mode argument
  53. is given, it must be "r".
  54. :returns: An image instance.
  55. :raises IOError: If the image could not be read.
  56. """
  57. if mode != "r":
  58. raise ValueError("bad mode")
  59. try:
  60. return GdImageFile(fp)
  61. except SyntaxError:
  62. raise IOError("cannot identify this image file")