GimpGradientFile.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 typing import IO, Callable
  23. from ._binary import o8
  24. EPSILON = 1e-10
  25. """""" # Enable auto-doc for data member
  26. def linear(middle: float, pos: float) -> float:
  27. if pos <= middle:
  28. if middle < EPSILON:
  29. return 0.0
  30. else:
  31. return 0.5 * pos / middle
  32. else:
  33. pos = pos - middle
  34. middle = 1.0 - middle
  35. if middle < EPSILON:
  36. return 1.0
  37. else:
  38. return 0.5 + 0.5 * pos / middle
  39. def curved(middle: float, pos: float) -> float:
  40. return pos ** (log(0.5) / log(max(middle, EPSILON)))
  41. def sine(middle: float, pos: float) -> float:
  42. return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
  43. def sphere_increasing(middle: float, pos: float) -> float:
  44. return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
  45. def sphere_decreasing(middle: float, pos: float) -> float:
  46. return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
  47. SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
  48. """""" # Enable auto-doc for data member
  49. class GradientFile:
  50. gradient: (
  51. list[
  52. tuple[
  53. float,
  54. float,
  55. float,
  56. list[float],
  57. list[float],
  58. Callable[[float, float], float],
  59. ]
  60. ]
  61. | None
  62. ) = None
  63. def getpalette(self, entries: int = 256) -> tuple[bytes, str]:
  64. assert self.gradient is not None
  65. palette = []
  66. ix = 0
  67. x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  68. for i in range(entries):
  69. x = i / (entries - 1)
  70. while x1 < x:
  71. ix += 1
  72. x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
  73. w = x1 - x0
  74. if w < EPSILON:
  75. scale = segment(0.5, 0.5)
  76. else:
  77. scale = segment((xm - x0) / w, (x - x0) / w)
  78. # expand to RGBA
  79. r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
  80. g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
  81. b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
  82. a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
  83. # add to palette
  84. palette.append(r + g + b + a)
  85. return b"".join(palette), "RGBA"
  86. class GimpGradientFile(GradientFile):
  87. """File handler for GIMP's gradient format."""
  88. def __init__(self, fp: IO[bytes]) -> None:
  89. if fp.readline()[:13] != b"GIMP Gradient":
  90. msg = "not a GIMP gradient file"
  91. raise SyntaxError(msg)
  92. line = fp.readline()
  93. # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
  94. if line.startswith(b"Name: "):
  95. line = fp.readline().strip()
  96. count = int(line)
  97. self.gradient = []
  98. for i in range(count):
  99. s = fp.readline().split()
  100. w = [float(x) for x in s[:11]]
  101. x0, x1 = w[0], w[2]
  102. xm = w[1]
  103. rgb0 = w[3:7]
  104. rgb1 = w[7:11]
  105. segment = SEGMENTS[int(s[11])]
  106. cspace = int(s[12])
  107. if cspace != 0:
  108. msg = "cannot handle HSV colour space"
  109. raise OSError(msg)
  110. self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))