File: create_rendering_intent_test_profile.py

package info (click to toggle)
displaycal-py3 3.9.17-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 29,124 kB
  • sloc: python: 115,810; javascript: 11,545; xml: 598; sh: 257; makefile: 173
file content (157 lines) | stat: -rw-r--r-- 6,406 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from copy import deepcopy
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from DisplayCAL.colormath import (
    RGB2XYZ,
    convert_range,
    get_rgb_space,
    get_whitepoint,
    specialpow,
)
from DisplayCAL.icc_profile import (
    create_synthetic_clut_profile,
    ICCProfile,
    LUT16Type,
    XYZType,
)


def create_rendering_intent_test_profile(
    filename, add_fallback_matrix=True, generate_B2A_tables=True, swap=False
):
    desc = "Rendering Intent Test Profile"
    if not add_fallback_matrix:
        desc += " (cLUT only)"
    else:
        desc += " (cLUT + fallback matrix)"
    srgb = get_rgb_space("sRGB")
    if swap:
        rgb_space = list(srgb)
        rgb_space[2:5] = rgb_space[3], rgb_space[4], rgb_space[2]
        rgb_space = get_rgb_space(rgb_space)
    else:
        rgb_space = srgb
    clutres = 9  # Minimum 9 because we require f >= 2
    f = int(round((clutres - 1) / 4.0))
    p = create_synthetic_clut_profile(rgb_space, desc, clutres=clutres)
    tagnames = ["A2B"]
    if generate_B2A_tables:
        p.tags.B2A0.clut = []
        tagnames.append("B2A")
    else:
        del p.tags["B2A0"]
    for i in range(1, 3):
        for tagname in tagnames:
            tag0 = p.tags[tagname + "0"]
            tag = p.tags[tagname + "%i" % i] = LUT16Type()
            for component_name in ("matrix", "input", "clut", "output"):
                component = getattr(tag0, component_name)
                if component_name == "clut":
                    component = deepcopy(component)
                setattr(tag, component_name, component)
    for i in range(3):
        for tagname in tagnames:
            tag = p.tags[tagname + "%i" % i]
            if i == 0:
                # Perceptual
                RGB = (f, f, 0)
            elif i == 1:
                # Colorimetric
                RGB = (0, f, f)
            else:
                # Saturation
                RGB = (0, f, 0)
            block = 0
            for R in range(clutres):
                r = min(max(R, f), clutres - 2)
                b1 = min(max(R, f) + 1, clutres - 2)
                b2 = min(max(R - 1, f), clutres - 2)
                for G in range(clutres):
                    if tagname == "B2A":
                        tag.clut.append([])
                    for B in range(clutres):
                        if tagname == "B2A":
                            tag.clut[block].append(
                                [v / (clutres - 1.0) * 65535 for v in (R, G, B)]
                            )
                        # Everything should be good as long as primaries
                        # are sensible (otherwise, when embedded in a PNG,
                        # some programs like Firefox and XnView will ignore
                        # the profile. This doesn't happen when embedded in
                        # JPEG. Go figure...)
                        if (R, G, B) in (
                            (f, f, 0),
                            (0, f, f),
                            (0, f, 0),
                            # Magenta 64..223
                            (r, 0, r),
                            (r, 0, b1),
                            (r, 0, b2),
                            # Red 64..223
                            (r, 0, 0),
                            (r, 0, 1),
                            (r, 1, 0),
                            # CMY 255
                            (0, clutres - 1, clutres - 1),
                            (clutres - 1, 0, clutres - 1),
                            (clutres - 1, clutres - 1, 0),
                        ):
                            if (R, G, B) == (0, clutres - 1, clutres - 1):
                                # Map C -> R
                                if tagname == "B2A":
                                    triplet = (65535, 0, 0)
                                else:
                                    triplet = RGB2XYZ(1, 0, 0, scale=32768)
                            elif (R, G, B) == (clutres - 1, 0, clutres - 1):
                                # Map M -> G
                                if tagname == "B2A":
                                    triplet = (0, 65535, 0)
                                else:
                                    triplet = RGB2XYZ(0, 1, 0, scale=32768)
                            elif (R, G, B) == (clutres - 1, clutres - 1, 0):
                                # Map Y -> B
                                if tagname == "B2A":
                                    triplet = (0, 0, 65535)
                                else:
                                    triplet = RGB2XYZ(0, 0, 1, scale=32768)
                            elif (R, G, B) != RGB:
                                triplet = [0, 0, 0]
                            else:
                                if tagname == "B2A":
                                    # White RGB
                                    triplet = [65535] * 3
                                else:
                                    # White XYZ D50
                                    triplet = get_whitepoint("D50", 32768)
                            tag.clut[block][B] = triplet
                    block += 1
            tag.clut_writepng(filename[:-3] + tagname + "%i.CLUT.png" % i)
            if tagname == "A2B":
                tag.clut_writecgats(filename[:-3] + tagname + "%i.CLUT.ti3" % i)
    if add_fallback_matrix:
        srgb_icc = ICCProfile.from_rgb_space(srgb, "sRGB")
        p.tags.rTRC = srgb_icc.tags.rTRC
        p.tags.gTRC = srgb_icc.tags.gTRC
        p.tags.bTRC = srgb_icc.tags.bTRC
        # if False:  # NEVER
        #     # Don't really need this...?
        #     t = specialpow(1 / 3.0, -2.4)
        #     for i, v in enumerate(p.tags.gTRC[1:], 1):
        #         v /= 65535.0
        #         p.tags.rTRC[i] = p.tags.gTRC[i] = max(convert_range(v, t, 1, 0, 1), 0) * 65535
        #         p.tags.bTRC[i] = min(convert_range(v, 0, t, 0, 1), 1) * 65535
        p.tags.rXYZ = XYZType()  # Map red to black
        p.tags.gXYZ = XYZType()  # Map green to black
        p.tags.bXYZ = srgb_icc.tags.wtpt.pcs  # Map blue to white
    p.calculateID()
    p.write(filename)


if __name__ == "__main__":
    create_rendering_intent_test_profile(sys.argv[1], add_fallback_matrix=sys.argv[2:3])