2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# Python Imaging Library
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
# stuff to read (and render) GIMP gradient files
|
|
|
|
#
|
|
|
|
# History:
|
|
|
|
# 97-08-23 fl Created
|
|
|
|
#
|
|
|
|
# Copyright (c) Secret Labs AB 1997.
|
|
|
|
# Copyright (c) Fredrik Lundh 1997.
|
|
|
|
#
|
|
|
|
# See the README file for information on usage and redistribution.
|
|
|
|
#
|
|
|
|
|
|
|
|
from math import pi, log, sin, sqrt
|
2013-03-07 20:20:28 +04:00
|
|
|
from PIL._binary import o8
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# Stuff to translate curve segments to palette values (derived from
|
|
|
|
# the corresponding code in GIMP, written by Federico Mena Quintero.
|
|
|
|
# See the GIMP distribution for more information.)
|
|
|
|
#
|
|
|
|
|
|
|
|
EPSILON = 1e-10
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def linear(middle, pos):
|
|
|
|
if pos <= middle:
|
|
|
|
if middle < EPSILON:
|
|
|
|
return 0.0
|
|
|
|
else:
|
|
|
|
return 0.5 * pos / middle
|
|
|
|
else:
|
|
|
|
pos = pos - middle
|
|
|
|
middle = 1.0 - middle
|
|
|
|
if middle < EPSILON:
|
|
|
|
return 1.0
|
|
|
|
else:
|
|
|
|
return 0.5 + 0.5 * pos / middle
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def curved(middle, pos):
|
|
|
|
return pos ** (log(0.5) / log(max(middle, EPSILON)))
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def sine(middle, pos):
|
|
|
|
return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def sphere_increasing(middle, pos):
|
|
|
|
return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def sphere_decreasing(middle, pos):
|
|
|
|
return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
class GradientFile:
|
|
|
|
|
|
|
|
gradient = None
|
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
def getpalette(self, entries=256):
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
palette = []
|
|
|
|
|
|
|
|
ix = 0
|
|
|
|
x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
|
|
|
|
|
|
|
|
for i in range(entries):
|
|
|
|
|
|
|
|
x = i / float(entries-1)
|
|
|
|
|
|
|
|
while x1 < x:
|
2014-05-10 08:36:15 +04:00
|
|
|
ix += 1
|
2010-07-31 06:52:47 +04:00
|
|
|
x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
|
|
|
|
|
|
|
|
w = x1 - x0
|
|
|
|
|
|
|
|
if w < EPSILON:
|
|
|
|
scale = segment(0.5, 0.5)
|
|
|
|
else:
|
|
|
|
scale = segment((xm - x0) / w, (x - x0) / w)
|
|
|
|
|
|
|
|
# expand to RGBA
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
|
|
|
|
g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
|
|
|
|
b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
|
|
|
|
a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
# add to palette
|
|
|
|
palette.append(r + g + b + a)
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
return b"".join(palette), "RGBA"
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
##
|
|
|
|
# File handler for GIMP's gradient format.
|
|
|
|
|
|
|
|
class GimpGradientFile(GradientFile):
|
|
|
|
|
|
|
|
def __init__(self, fp):
|
|
|
|
|
py3k: The big push
There are two main issues fixed with this commit:
* bytes vs. str: All file, image, and palette data are now handled as
bytes. A new _binary module consolidates the hacks needed to do this
across Python versions. tostring/fromstring methods have been renamed to
tobytes/frombytes, but the Python 2.6/2.7 versions alias them to the old
names for compatibility. Users should move to tobytes/frombytes.
One other potentially-breaking change is that text data in image files
(such as tags, comments) are now explicitly handled with a specific
character encoding in mind. This works well with the Unicode str in
Python 3, but may trip up old code expecting a straight byte-for-byte
translation to a Python string. This also required a change to Gohlke's
tags tests (in Tests/test_file_png.py) to expect Unicode strings from
the code.
* True div vs. floor div: Many division operations used the "/" operator
to do floor division, which is now the "//" operator in Python 3. These
were fixed.
As of this commit, on the first pass, I have one failing test (improper
handling of a slice object in a C module, test_imagepath.py) in Python 3,
and three that that I haven't tried running yet (test_imagegl,
test_imagegrab, and test_imageqt). I also haven't tested anything on
Windows. All but the three skipped tests run flawlessly against Pythons
2.6 and 2.7.
2012-10-21 01:01:53 +04:00
|
|
|
if fp.readline()[:13] != b"GIMP Gradient":
|
2012-10-11 07:52:53 +04:00
|
|
|
raise SyntaxError("not a GIMP gradient file")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 13:14:58 +04:00
|
|
|
line = fp.readline()
|
|
|
|
|
|
|
|
# GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
|
2014-08-26 13:23:12 +04:00
|
|
|
if line.startswith(b"Name: "):
|
2014-08-26 13:14:58 +04:00
|
|
|
line = fp.readline().strip()
|
|
|
|
|
|
|
|
count = int(line)
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
gradient = []
|
|
|
|
|
|
|
|
for i in range(count):
|
|
|
|
|
2012-10-11 02:11:13 +04:00
|
|
|
s = fp.readline().split()
|
2012-10-16 05:58:46 +04:00
|
|
|
w = [float(x) for x in s[:11]]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 13:16:32 +04:00
|
|
|
x0, x1 = w[0], w[2]
|
|
|
|
xm = w[1]
|
|
|
|
rgb0 = w[3:7]
|
|
|
|
rgb1 = w[7:11]
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
segment = SEGMENTS[int(s[11])]
|
2014-08-26 13:16:32 +04:00
|
|
|
cspace = int(s[12])
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
if cspace != 0:
|
2012-10-11 07:52:53 +04:00
|
|
|
raise IOError("cannot handle HSV colour space")
|
2010-07-31 06:52:47 +04:00
|
|
|
|
|
|
|
gradient.append((x0, x1, xm, rgb0, rgb1, segment))
|
|
|
|
|
|
|
|
self.gradient = gradient
|