Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/PIL/GimpGradientFile.py: 26%

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

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

21 

22from __future__ import annotations 

23 

24from math import log, pi, sin, sqrt 

25 

26from ._binary import o8 

27 

28TYPE_CHECKING = False 

29if TYPE_CHECKING: 

30 from collections.abc import Callable 

31 from typing import IO 

32 

33EPSILON = 1e-10 

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

35 

36 

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

38 if pos <= middle: 

39 if middle < EPSILON: 

40 return 0.0 

41 else: 

42 return 0.5 * pos / middle 

43 else: 

44 pos = pos - middle 

45 middle = 1.0 - middle 

46 if middle < EPSILON: 

47 return 1.0 

48 else: 

49 return 0.5 + 0.5 * pos / middle 

50 

51 

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

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

54 

55 

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

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

58 

59 

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

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

62 

63 

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

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

66 

67 

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

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

70 

71 

72class GradientFile: 

73 gradient: ( 

74 list[ 

75 tuple[ 

76 float, 

77 float, 

78 float, 

79 list[float], 

80 list[float], 

81 Callable[[float, float], float], 

82 ] 

83 ] 

84 | None 

85 ) = None 

86 

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

88 assert self.gradient is not None 

89 palette = [] 

90 

91 ix = 0 

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

93 

94 for i in range(entries): 

95 x = i / (entries - 1) 

96 

97 while x1 < x: 

98 ix += 1 

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

100 

101 w = x1 - x0 

102 

103 if w < EPSILON: 

104 scale = segment(0.5, 0.5) 

105 else: 

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

107 

108 # expand to RGBA 

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

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

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

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

113 

114 # add to palette 

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

116 

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

118 

119 

120class GimpGradientFile(GradientFile): 

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

122 

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

124 if not fp.readline().startswith(b"GIMP Gradient"): 

125 msg = "not a GIMP gradient file" 

126 raise SyntaxError(msg) 

127 

128 line = fp.readline() 

129 

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

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

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

133 

134 count = int(line) 

135 

136 self.gradient = [] 

137 

138 for i in range(count): 

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

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

141 

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

143 xm = w[1] 

144 rgb0 = w[3:7] 

145 rgb1 = w[7:11] 

146 

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

148 cspace = int(s[12]) 

149 

150 if cspace != 0: 

151 msg = "cannot handle HSV colour space" 

152 raise OSError(msg) 

153 

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