GimpGradientFile.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. # Python Imaging Library
  3. # $Id$
  4. #
  5. # stuff to read (and render) GIMP gradient files
  6. #
  7. # History:
  8. # 97-08-23 fl Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1997.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15. """
  16. Stuff to translate curve segments to palette values (derived from
  17. the corresponding code in GIMP, written by Federico Mena Quintero.
  18. See the GIMP distribution for more information.)
  19. """
  20. from __future__ import annotations
  21. from math import log, pi, sin, sqrt
  22. from ._binary import o8
  23. TYPE_CHECKING = False
  24. if TYPE_CHECKING:
  25. from collections.abc import Callable
  26. from typing import IO
  27. EPSILON = 1e-10
  28. """""" # Enable auto-doc for data member
  29. def linear(middle: float, pos: float) -> float:
  30. if pos <= middle:
  31. if middle < EPSILON:
  32. return 0.0
  33. else:
  34. return 0.5 * pos / middle
  35. else:
  36. pos = pos - middle
  37. middle = 1.0 - middle
  38. if middle < EPSILON:
  39. return 1.0
  40. else:
  41. return 0.5 + 0.5 * pos / middle
  42. def curved(middle: float, pos: float) -> float:
  43. return pos ** (log(0.5) / log(max(middle, EPSILON)))
  44. def sine(middle: float, pos: float) -> float:
  45. return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
  46. def sphere_increasing(middle: float, pos: float) -> float:
  47. return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
  48. def sphere_decreasing(middle: float, pos: float) -> float:
  49. return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
  50. SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
  51. """""" # Enable auto-doc for data member
  52. class GradientFile:
  53. gradient: (
  54. list[
  55. tuple[
  56. float,
  57. float,
  58. float,
  59. list[float],
  60. list[float],
  61. Callable[[float, float], float],
  62. ]
  63. ]
  64. | None
  65. ) = None
  66. def getpalette(self, entries: int = 256) -> tuple[bytes, str]:
  67. assert self.gradient is not None
  68. palette = []
  69. ix = 0
  70. x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  71. for i in range(entries):
  72. x = i / (entries - 1)
  73. while x1 < x:
  74. ix += 1
  75. x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  76. w = x1 - x0
  77. if w < EPSILON:
  78. scale = segment(0.5, 0.5)
  79. else:
  80. scale = segment((xm - x0) / w, (x - x0) / w)
  81. # expand to RGBA
  82. r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
  83. g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
  84. b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
  85. a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
  86. # add to palette
  87. palette.append(r + g + b + a)
  88. return b"".join(palette), "RGBA"
  89. class GimpGradientFile(GradientFile):
  90. """File handler for GIMP's gradient format."""
  91. def __init__(self, fp: IO[bytes]) -> None:
  92. if not fp.readline().startswith(b"GIMP Gradient"):
  93. msg = "not a GIMP gradient file"
  94. raise SyntaxError(msg)
  95. line = fp.readline()
  96. # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
  97. if line.startswith(b"Name: "):
  98. line = fp.readline().strip()
  99. count = int(line)
  100. self.gradient = []
  101. for i in range(count):
  102. s = fp.readline().split()
  103. w = [float(x) for x in s[:11]]
  104. x0, x1 = w[0], w[2]
  105. xm = w[1]
  106. rgb0 = w[3:7]
  107. rgb1 = w[7:11]
  108. segment = SEGMENTS[int(s[11])]
  109. cspace = int(s[12])
  110. if cspace != 0:
  111. msg = "cannot handle HSV colour space"
  112. raise OSError(msg)
  113. self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))