Coverage for /usr/lib/python3/dist-packages/PIL/GimpGradientFile.py: 24%
70 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
« prev ^ index » next coverage.py v7.9.1, created at 2025-06-14 15:55 +0200
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#
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
23from math import log, pi, sin, sqrt
25from ._binary import o8
27EPSILON = 1e-10
28"""""" # Enable auto-doc for data member
31def linear(middle, pos):
32 if pos <= middle:
33 if middle < EPSILON:
34 return 0.0
35 else:
36 return 0.5 * pos / middle
37 else:
38 pos = pos - middle
39 middle = 1.0 - middle
40 if middle < EPSILON:
41 return 1.0
42 else:
43 return 0.5 + 0.5 * pos / middle
46def curved(middle, pos):
47 return pos ** (log(0.5) / log(max(middle, EPSILON)))
50def sine(middle, pos):
51 return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
54def sphere_increasing(middle, pos):
55 return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
58def sphere_decreasing(middle, pos):
59 return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
62SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
63"""""" # Enable auto-doc for data member
66class GradientFile:
67 gradient = None
69 def getpalette(self, entries=256):
70 palette = []
72 ix = 0
73 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
75 for i in range(entries):
76 x = i / (entries - 1)
78 while x1 < x:
79 ix += 1
80 x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
82 w = x1 - x0
84 if w < EPSILON:
85 scale = segment(0.5, 0.5)
86 else:
87 scale = segment((xm - x0) / w, (x - x0) / w)
89 # expand to RGBA
90 r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
91 g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
92 b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
93 a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
95 # add to palette
96 palette.append(r + g + b + a)
98 return b"".join(palette), "RGBA"
101class GimpGradientFile(GradientFile):
102 """File handler for GIMP's gradient format."""
104 def __init__(self, fp):
105 if fp.readline()[:13] != b"GIMP Gradient":
106 msg = "not a GIMP gradient file"
107 raise SyntaxError(msg)
109 line = fp.readline()
111 # GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
112 if line.startswith(b"Name: "):
113 line = fp.readline().strip()
115 count = int(line)
117 gradient = []
119 for i in range(count):
120 s = fp.readline().split()
121 w = [float(x) for x in s[:11]]
123 x0, x1 = w[0], w[2]
124 xm = w[1]
125 rgb0 = w[3:7]
126 rgb1 = w[7:11]
128 segment = SEGMENTS[int(s[11])]
129 cspace = int(s[12])
131 if cspace != 0:
132 msg = "cannot handle HSV colour space"
133 raise OSError(msg)
135 gradient.append((x0, x1, xm, rgb0, rgb1, segment))
137 self.gradient = gradient