GbrImagePlugin.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #
  2. # The Python Imaging Library
  3. #
  4. # load a GIMP brush file
  5. #
  6. # History:
  7. # 96-03-14 fl Created
  8. # 16-01-08 es Version 2
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. # Copyright (c) Eric Soroos 2016.
  13. #
  14. # See the README file for information on usage and redistribution.
  15. #
  16. #
  17. # See https://github.com/GNOME/gimp/blob/mainline/devel-docs/gbr.txt for
  18. # format documentation.
  19. #
  20. # This code Interprets version 1 and 2 .gbr files.
  21. # Version 1 files are obsolete, and should not be used for new
  22. # brushes.
  23. # Version 2 files are saved by GIMP v2.8 (at least)
  24. # Version 3 files have a format specifier of 18 for 16bit floats in
  25. # the color depth field. This is currently unsupported by Pillow.
  26. from __future__ import annotations
  27. __lazy_modules__ = {"PIL._binary"}
  28. from . import Image, ImageFile
  29. from ._binary import i32be as i32
  30. def _accept(prefix: bytes) -> bool:
  31. return len(prefix) >= 8 and i32(prefix, 0) >= 20 and i32(prefix, 4) in (1, 2)
  32. ##
  33. # Image plugin for the GIMP brush format.
  34. class GbrImageFile(ImageFile.ImageFile):
  35. format = "GBR"
  36. format_description = "GIMP brush file"
  37. def _open(self) -> None:
  38. assert self.fp is not None
  39. header_size = i32(self.fp.read(4))
  40. if header_size < 20:
  41. msg = "not a GIMP brush"
  42. raise SyntaxError(msg)
  43. version = i32(self.fp.read(4))
  44. if version not in (1, 2):
  45. msg = f"Unsupported GIMP brush version: {version}"
  46. raise SyntaxError(msg)
  47. width = i32(self.fp.read(4))
  48. height = i32(self.fp.read(4))
  49. if width == 0 or height == 0:
  50. msg = "not a GIMP brush"
  51. raise SyntaxError(msg)
  52. color_depth = i32(self.fp.read(4))
  53. if color_depth not in (1, 4):
  54. msg = f"Unsupported GIMP brush color depth: {color_depth}"
  55. raise SyntaxError(msg)
  56. if version == 1:
  57. comment_length = header_size - 20
  58. else:
  59. comment_length = header_size - 28
  60. magic_number = self.fp.read(4)
  61. if magic_number != b"GIMP":
  62. msg = "not a GIMP brush, bad magic number"
  63. raise SyntaxError(msg)
  64. self.info["spacing"] = i32(self.fp.read(4))
  65. self.info["comment"] = self.fp.read(comment_length)[:-1]
  66. self._mode = "L" if color_depth == 1 else "RGBA"
  67. self._size = width, height
  68. # Image might not be small
  69. Image._decompression_bomb_check(self.size)
  70. def load(self) -> Image.core.PixelAccess | None:
  71. if self._im is None:
  72. assert self.fp is not None
  73. self.im = Image.core.new(self.mode, self.size)
  74. # Data is an uncompressed block of w * h * bytes/pixel
  75. data_size = self.width * self.height * Image.getmodebands(self.mode)
  76. self.frombytes(self.fp.read(data_size))
  77. return Image.Image.load(self)
  78. #
  79. # registry
  80. Image.register_open(GbrImageFile.format, GbrImageFile, _accept)
  81. Image.register_extension(GbrImageFile.format, ".gbr")