GimpGradientFile.py 3.3 KB

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