GdImageFile.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. """
  16. .. note::
  17. This format cannot be automatically recognized, so the
  18. class is not registered for use with :py:func:`PIL.Image.open()`. To open a
  19. gd file, use the :py:func:`PIL.GdImageFile.open()` function instead.
  20. .. warning::
  21. THE GD FORMAT IS NOT DESIGNED FOR DATA INTERCHANGE. This
  22. implementation is provided for convenience and demonstrational
  23. purposes only.
  24. """
  25. from __future__ import annotations
  26. from typing import IO
  27. from . import ImageFile, ImagePalette, UnidentifiedImageError
  28. from ._binary import i16be as i16
  29. from ._binary import i32be as i32
  30. from ._typing import StrOrBytesPath
  31. class GdImageFile(ImageFile.ImageFile):
  32. """
  33. Image plugin for the GD uncompressed format. Note that this format
  34. is not supported by the standard :py:func:`PIL.Image.open()` function. To use
  35. this plugin, you have to import the :py:mod:`PIL.GdImageFile` module and
  36. use the :py:func:`PIL.GdImageFile.open()` function.
  37. """
  38. format = "GD"
  39. format_description = "GD uncompressed images"
  40. def _open(self) -> None:
  41. # Header
  42. assert self.fp is not None
  43. s = self.fp.read(1037)
  44. if i16(s) not in [65534, 65535]:
  45. msg = "Not a valid GD 2.x .gd file"
  46. raise SyntaxError(msg)
  47. self._mode = "L" # FIXME: "P"
  48. self._size = i16(s, 2), i16(s, 4)
  49. true_color = s[6]
  50. true_color_offset = 2 if true_color else 0
  51. # transparency index
  52. tindex = i32(s, 7 + true_color_offset)
  53. if tindex < 256:
  54. self.info["transparency"] = tindex
  55. self.palette = ImagePalette.raw(
  56. "XBGR", s[7 + true_color_offset + 4 : 7 + true_color_offset + 4 + 256 * 4]
  57. )
  58. self.tile = [
  59. ImageFile._Tile(
  60. "raw",
  61. (0, 0) + self.size,
  62. 7 + true_color_offset + 4 + 256 * 4,
  63. "L",
  64. )
  65. ]
  66. def open(fp: StrOrBytesPath | IO[bytes], mode: str = "r") -> GdImageFile:
  67. """
  68. Load texture from a GD image file.
  69. :param fp: GD file name, or an opened file handle.
  70. :param mode: Optional mode. In this version, if the mode argument
  71. is given, it must be "r".
  72. :returns: An image instance.
  73. :raises OSError: If the image could not be read.
  74. """
  75. if mode != "r":
  76. msg = "bad mode"
  77. raise ValueError(msg)
  78. try:
  79. return GdImageFile(fp)
  80. except SyntaxError as e:
  81. msg = "cannot identify this image file"
  82. raise UnidentifiedImageError(msg) from e