GimpGradientFile.py 3.3 KB

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