summaryrefslogtreecommitdiffstats
path: root/src/icc.py
blob: e08dfa7adcaa4782311cd0d6650777d453192dde (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3

# Copyright © 2014  Mattias Andrée (maandree@member.fsf.org)
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from subprocess import Popen, PIPE

from curve import *

# /usr/libexec
LIBEXECDIR = 'bin'



def load_icc(pathname):
    '''
    Load ICC profile from a file
    
    @param   pathname:str  The ICC profile file
    @return  :()→void      Function to invoke, parameterless, to apply the ICC profile to the colour curves
    '''
    content = None
    with open(pathname, 'rb') as file:
        content = file.read()
    return parse_icc(content)


def get_current_icc():
    '''
    Get all currently applied ICC profiles as profile applying functions
    
    @return  list<(screen:int, monitor:int, profile:()→void)>  List of used profiles
    '''
    return [(screen, monitor, parse_icc(profile)) for screen, monitor, profile in get_current_icc_raw()]


def get_current_icc_raw():
    '''
    Get all currently applied ICC profiles as raw profile data
    
    @return  list<(screen:int, monitor:int, profile:bytes())>  List of used profiles
    '''
    process = Popen([LIBEXECDIR + "/blueshift_iccprofile"], stdout = PIPE)
    lines = process.communicate()[0].decode('utf-8', 'error').split('\n')
    while process.returncode is None:
        process.wait()
    if process.returncode != 0:
        raise Exception('blueshift_iccprofile exited with value %i' % process.returncode)
    rc = []
    for line in lines:
        if len(line) == 0:
            continue
        (s, m, p) = line.split(': ')
        p = bytes([int(p[i : i + 2], 16) for i in range(0, len(p), 2)])
        rc.append((int(s), int(m), p))
    return rc


def parse_icc(content):
    '''
    Parse ICC profile from raw data
    
    @param   content:bytes  The ICC profile data
    @return  :()→void       Function to invoke, parameterless, to apply the ICC profile to the colour curves
    '''
    MLUT_TAG = 0x6d4c5554
    VCGT_TAG = 0x76636774
    
    def fcurve(R_curve, G_curve, B_curve):
        for curve, icc in curves(R_curve, G_curve, B_curve):
            for i in range(i_size):
                y = int(curve[i] * (len(icc) - 1) + 0.5)
                y = min(max(0, y), len(icc) - 1)
                curve[i] = icc[y]
    
    int_ = lambda bs : sum([bs[len(bs) - 1 - i] << (8 * i) for i in range(len(bs))])
    def read(n):
        if len(content) < n:
            raise Except("Premature end of file: %s" % pathname)
        rc, content[:] = content[:n], content[n:]
        return rc
    
    content = list(content)
    read(128)
    
    R_curve, G_curve, B_curve = [], [], []
    n_tags, ptr = int_(read(4)), 128 + 4
    for i_tag in range(n_tags):
        tag_name   = int_(read(4))
        tag_offset = int_(read(4))
        tag_size   = int_(read(4))
        ptr += 3 * 4
        if tag_name == MLUT_TAG:
            read(tag_offset - ptr)
            for i in range(256):  R_curve.append(int_(read(2)) / 65535)
            for i in range(256):  G_curve.append(int_(read(2)) / 65535)
            for i in range(256):  B_curve.append(int_(read(2)) / 65535)
            return lambda : fcurve(R_curve, G_curve, B_curve)
        elif tag_name == VCGT_TAG:
            read(tag_offset - ptr)
            tag_name = int_(read(4))
            if not tag_name == VCGT_TAG:
                break
            read(4)
            gamma_type = int_(read(4))
            if gamma_type == 0:
                n_channels = int_(read(2))
                n_entries  = int_(read(2))
                entry_size = int_(read(2))
                if tag_size == 1584:
                    n_channels, n_entries, entry_size = 3, 256, 2
                if not n_channels == 3: # assuming sRGB
                    break
                int__ = lambda m : int_(read(m)) / ((256 ** m) - 1)
                for i in range(n_entries):  R_curve.append(int__(entry_size))
                for i in range(n_entries):  G_curve.append(int__(entry_size))
                for i in range(n_entries):  B_curve.append(int__(entry_size))
                return lambda : fcurve(R_curve, G_curve, B_curve)
            elif gamma_type == 1:
                r_gamma = int_(read(4)) / 65535
                r_min   = int_(read(4)) / 65535
                r_max   = int_(read(4)) / 65535
                g_gamma = int_(read(4)) / 65535
                g_min   = int_(read(4)) / 65535
                g_max   = int_(read(4)) / 65535
                b_gamma = int_(read(4)) / 65535
                b_min   = int_(read(4)) / 65535
                b_max   = int_(read(4)) / 65535
                def f():
                    gamma(r_gamma, g_gamma, b_gamma)
                    rgb_limits(r_min, r_max, g_min, g_max, b_min, b_max)
                return f
            break
    
    raise Exception("Unsupported ICC profile file")


def make_icc_interpolation(profiles):
    '''
    An interpolation function for ICC profiles
    
    @param   profiles:list<()→void>                  Profile applying functions
    @return  :(timepoint:float, alpha:float)→void()  A function that applies an interpolation of the profiles,
                                                     it takes to arguments: the timepoint and the filter
                                                     alpha. The timepoint is normally a [0, 1] floating point
                                                     of the dayness level, this means that you only have two
                                                     ICC profiles, but you have multiple profiles, in such
                                                     case profile #⌊timepoint⌋ and profile #(⌊timepoint⌋ + 1)
                                                     (modulus the number of profiles) are interpolated with
                                                     (timepoint - ⌊timepoint⌋) weight to the second profile.
                                                     The filter alpha is a [0, 1] floating point of the degree
                                                     to which the profile should be applied.
    '''
    def f(t, a):
        pro0 = profiles[(int(t) + 0) % len(profiles)]
        pro1 = profiles[(int(t) + 1) % len(profiles)]
        t %= 1
        if (pro0 is pro1) and (a == 1):
            pro0()
            return
        r_, g_, b_ = r_curve[:], g_curve[:], b_curve[:]
        start_over()
        pro0()
        r0, g0, b0 = r_curve[:], g_curve[:], b_curve[:]
        n = len(r0) - 1
        r, g, b = None, None, None
        if pro0 is pro1:
            r = [v * a + i * (1 - a) / n for i, v in enumerate(r0)]
            g = [v * a + i * (1 - a) / n for i, v in enumerate(g0)]
            b = [v * a + i * (1 - a) / n for i, v in enumerate(b0)]
        else:
            start_over()
            pro1()
            r1, g1, b1 = r_curve[:], g_curve[:], b_curve[:]
            interpol = lambda i, v0, v1 : (v0 * (1 - t) + v1 * t) * a + i * (1 - a) / n
            r = [interpol(i, v0, v1) for i, (v0, v1) in enumerate(zip(r0, r1))]
            g = [interpol(i, v0, v1) for i, (v0, v1) in enumerate(zip(g0, g1))]
            b = [interpol(i, v0, v1) for i, (v0, v1) in enumerate(zip(b0, b1))]
        r_curve[:], g_curve[:], b_curve[:] = r_, g_, b_
        for curve, icc in curves(r, g, b):
            for i in range(i_size):
                y = int(curve[i] * (len(icc) - 1) + 0.5)
                y = min(max(0, y), len(icc) - 1)
                curve[i] = icc[y]
    return f