~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/PIL/GimpGradientFile.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
from math import pi, log, sin, sqrt
 
17
import string
 
18
 
 
19
# --------------------------------------------------------------------
 
20
# Stuff to translate curve segments to palette values (derived from
 
21
# the corresponding code in GIMP, written by Federico Mena Quintero.
 
22
# See the GIMP distribution for more information.)
 
23
#
 
24
 
 
25
EPSILON = 1e-10
 
26
 
 
27
def linear(middle, pos):
 
28
    if pos <= middle:
 
29
        if middle < EPSILON:
 
30
            return 0.0
 
31
        else:
 
32
            return 0.5 * pos / middle
 
33
    else:
 
34
        pos = pos - middle
 
35
        middle = 1.0 - middle
 
36
        if middle < EPSILON:
 
37
            return 1.0
 
38
        else:
 
39
            return 0.5 + 0.5 * pos / middle
 
40
 
 
41
def curved(middle, pos):
 
42
    return pos ** (log(0.5) / log(max(middle, EPSILON)))
 
43
 
 
44
def sine(middle, pos):
 
45
    return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
 
46
 
 
47
def sphere_increasing(middle, pos):
 
48
    return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
 
49
 
 
50
def sphere_decreasing(middle, pos):
 
51
    return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
 
52
 
 
53
SEGMENTS = [ linear, curved, sine, sphere_increasing, sphere_decreasing ]
 
54
 
 
55
class GradientFile:
 
56
 
 
57
    gradient = None
 
58
 
 
59
    def getpalette(self, entries = 256):
 
60
 
 
61
        palette = []
 
62
 
 
63
        ix = 0
 
64
        x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
 
65
 
 
66
        for i in range(entries):
 
67
 
 
68
            x = i / float(entries-1)
 
69
 
 
70
            while x1 < x:
 
71
                ix = ix + 1
 
72
                x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
 
73
 
 
74
            w = x1 - x0
 
75
 
 
76
            if w < EPSILON:
 
77
                scale = segment(0.5, 0.5)
 
78
            else:
 
79
                scale = segment((xm - x0) / w, (x - x0) / w)
 
80
 
 
81
            # expand to RGBA
 
82
            r = chr(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
 
83
            g = chr(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
 
84
            b = chr(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
 
85
            a = chr(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
 
86
 
 
87
            # add to palette
 
88
            palette.append(r + g + b + a)
 
89
 
 
90
        return string.join(palette, ""), "RGBA"
 
91
 
 
92
##
 
93
# File handler for GIMP's gradient format.
 
94
 
 
95
class GimpGradientFile(GradientFile):
 
96
 
 
97
    def __init__(self, fp):
 
98
 
 
99
        if fp.readline()[:13] != "GIMP Gradient":
 
100
            raise SyntaxError, "not a GIMP gradient file"
 
101
 
 
102
        count = int(fp.readline())
 
103
 
 
104
        gradient = []
 
105
 
 
106
        for i in range(count):
 
107
 
 
108
            s = string.split(fp.readline())
 
109
            w = map(float, s[:11])
 
110
 
 
111
            x0, x1  = w[0], w[2]
 
112
            xm      = w[1]
 
113
            rgb0    = w[3:7]
 
114
            rgb1    = w[7:11]
 
115
 
 
116
            segment = SEGMENTS[int(s[11])]
 
117
            cspace  = int(s[12])
 
118
 
 
119
            if cspace != 0:
 
120
                raise IOError, "cannot handle HSV colour space"
 
121
 
 
122
            gradient.append((x0, x1, xm, rgb0, rgb1, segment))
 
123
 
 
124
        self.gradient = gradient