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

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

77 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 

24__lazy_modules__ = {"PIL._binary", "math"} 

25 

26from math import log, pi, sin, sqrt 

27 

28from ._binary import o8 

29 

30TYPE_CHECKING = False 

31if TYPE_CHECKING: 

32 from collections.abc import Callable 

33 from typing import IO 

34 

35EPSILON = 1e-10 

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

37 

38 

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

40 if pos <= middle: 

41 if middle < EPSILON: 

42 return 0.0 

43 else: 

44 return 0.5 * pos / middle 

45 else: 

46 pos = pos - middle 

47 middle = 1.0 - middle 

48 if middle < EPSILON: 

49 return 1.0 

50 else: 

51 return 0.5 + 0.5 * pos / middle 

52 

53 

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

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

56 

57 

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

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

60 

61 

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

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

64 

65 

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

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

68 

69 

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

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

72 

73 

74class GradientFile: 

75 gradient: ( 

76 list[ 

77 tuple[ 

78 float, 

79 float, 

80 float, 

81 list[float], 

82 list[float], 

83 Callable[[float, float], float], 

84 ] 

85 ] 

86 | None 

87 ) = None 

88 

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

90 assert self.gradient is not None 

91 palette = [] 

92 

93 ix = 0 

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

95 

96 for i in range(entries): 

97 x = i / (entries - 1) 

98 

99 while x1 < x: 

100 ix += 1 

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

102 

103 w = x1 - x0 

104 

105 if w < EPSILON: 

106 scale = segment(0.5, 0.5) 

107 else: 

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

109 

110 # expand to RGBA 

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

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

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

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

115 

116 # add to palette 

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

118 

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

120 

121 

122class GimpGradientFile(GradientFile): 

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

124 

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

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

127 msg = "not a GIMP gradient file" 

128 raise SyntaxError(msg) 

129 

130 line = fp.readline() 

131 

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

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

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

135 

136 count = int(line) 

137 

138 self.gradient = [] 

139 

140 for i in range(count): 

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

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

143 

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

145 xm = w[1] 

146 rgb0 = w[3:7] 

147 rgb1 = w[7:11] 

148 

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

150 cspace = int(s[12]) 

151 

152 if cspace != 0: 

153 msg = "cannot handle HSV colour space" 

154 raise OSError(msg) 

155 

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