Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pillow-10.4.0-py3.8-linux-x86_64.egg/PIL/GimpGradientFile.py: 24%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

70 statements  

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""" 

17Stuff to translate curve segments to palette values (derived from 

18the corresponding code in GIMP, written by Federico Mena Quintero. 

19See the GIMP distribution for more information.) 

20""" 

21from __future__ import annotations 

22 

23from math import log, pi, sin, sqrt 

24from typing import IO, Callable 

25 

26from ._binary import o8 

27 

28EPSILON = 1e-10 

29"""""" # Enable auto-doc for data member 

30 

31 

32def linear(middle: float, pos: float) -> float: 

33 if pos <= middle: 

34 if middle < EPSILON: 

35 return 0.0 

36 else: 

37 return 0.5 * pos / middle 

38 else: 

39 pos = pos - middle 

40 middle = 1.0 - middle 

41 if middle < EPSILON: 

42 return 1.0 

43 else: 

44 return 0.5 + 0.5 * pos / middle 

45 

46 

47def curved(middle: float, pos: float) -> float: 

48 return pos ** (log(0.5) / log(max(middle, EPSILON))) 

49 

50 

51def sine(middle: float, pos: float) -> float: 

52 return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0 

53 

54 

55def sphere_increasing(middle: float, pos: float) -> float: 

56 return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2) 

57 

58 

59def sphere_decreasing(middle: float, pos: float) -> float: 

60 return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2) 

61 

62 

63SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing] 

64"""""" # Enable auto-doc for data member 

65 

66 

67class GradientFile: 

68 gradient: ( 

69 list[ 

70 tuple[ 

71 float, 

72 float, 

73 float, 

74 list[float], 

75 list[float], 

76 Callable[[float, float], float], 

77 ] 

78 ] 

79 | None 

80 ) = None 

81 

82 def getpalette(self, entries: int = 256) -> tuple[bytes, str]: 

83 assert self.gradient is not None 

84 palette = [] 

85 

86 ix = 0 

87 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix] 

88 

89 for i in range(entries): 

90 x = i / (entries - 1) 

91 

92 while x1 < x: 

93 ix += 1 

94 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix] 

95 

96 w = x1 - x0 

97 

98 if w < EPSILON: 

99 scale = segment(0.5, 0.5) 

100 else: 

101 scale = segment((xm - x0) / w, (x - x0) / w) 

102 

103 # expand to RGBA 

104 r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5)) 

105 g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5)) 

106 b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5)) 

107 a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5)) 

108 

109 # add to palette 

110 palette.append(r + g + b + a) 

111 

112 return b"".join(palette), "RGBA" 

113 

114 

115class GimpGradientFile(GradientFile): 

116 """File handler for GIMP's gradient format.""" 

117 

118 def __init__(self, fp: IO[bytes]) -> None: 

119 if fp.readline()[:13] != b"GIMP Gradient": 

120 msg = "not a GIMP gradient file" 

121 raise SyntaxError(msg) 

122 

123 line = fp.readline() 

124 

125 # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do 

126 if line.startswith(b"Name: "): 

127 line = fp.readline().strip() 

128 

129 count = int(line) 

130 

131 self.gradient = [] 

132 

133 for i in range(count): 

134 s = fp.readline().split() 

135 w = [float(x) for x in s[:11]] 

136 

137 x0, x1 = w[0], w[2] 

138 xm = w[1] 

139 rgb0 = w[3:7] 

140 rgb1 = w[7:11] 

141 

142 segment = SEGMENTS[int(s[11])] 

143 cspace = int(s[12]) 

144 

145 if cspace != 0: 

146 msg = "cannot handle HSV colour space" 

147 raise OSError(msg) 

148 

149 self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))