GdImageFile.py 2.9 KB

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