| 1 |
#!/usr/bin/env python3 |
| 2 |
from copy import deepcopy |
| 3 |
import os |
| 4 |
import sys |
| 5 |
|
| 6 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 7 |
|
| 8 |
from DisplayCAL.colormath import ( |
| 9 |
RGB2XYZ, |
| 10 |
convert_range, |
| 11 |
get_rgb_space, |
| 12 |
get_whitepoint, |
| 13 |
special_pow, |
| 14 |
) |
| 15 |
from DisplayCAL.icc_profile import ( |
| 16 |
create_synthetic_clut_profile, |
| 17 |
ICCProfile, |
| 18 |
LUT16Type, |
| 19 |
XYZType, |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def create_rendering_intent_test_profile( |
| 24 |
filename, add_fallback_matrix=True, generate_B2A_tables=True, swap=False |
| 25 |
): |
| 26 |
desc = "Rendering Intent Test Profile" |
| 27 |
if not add_fallback_matrix: |
| 28 |
desc += " (cLUT only)" |
| 29 |
else: |
| 30 |
desc += " (cLUT + fallback matrix)" |
| 31 |
srgb = get_rgb_space("sRGB") |
| 32 |
if swap: |
| 33 |
rgb_space = list(srgb) |
| 34 |
rgb_space[2:5] = rgb_space[3], rgb_space[4], rgb_space[2] |
| 35 |
rgb_space = get_rgb_space(rgb_space) |
| 36 |
else: |
| 37 |
rgb_space = srgb |
| 38 |
clutres = 9 # Minimum 9 because we require f >= 2 |
| 39 |
f = int(round((clutres - 1) / 4.0)) |
| 40 |
p = create_synthetic_clut_profile(rgb_space, desc, clutres=clutres) |
| 41 |
tagnames = ["A2B"] |
| 42 |
if generate_B2A_tables: |
| 43 |
p.tags.B2A0.clut = [] |
| 44 |
tagnames.append("B2A") |
| 45 |
else: |
| 46 |
del p.tags["B2A0"] |
| 47 |
for i in range(1, 3): |
| 48 |
for tagname in tagnames: |
| 49 |
tag0 = p.tags[tagname + "0"] |
| 50 |
tag = p.tags[tagname + "%i" % i] = LUT16Type() |
| 51 |
for component_name in ("matrix", "input", "clut", "output"): |
| 52 |
component = getattr(tag0, component_name) |
| 53 |
if component_name == "clut": |
| 54 |
component = deepcopy(component) |
| 55 |
setattr(tag, component_name, component) |
| 56 |
for i in range(3): |
| 57 |
for tagname in tagnames: |
| 58 |
tag = p.tags[tagname + "%i" % i] |
| 59 |
if i == 0: |
| 60 |
# Perceptual |
| 61 |
RGB = (f, f, 0) |
| 62 |
elif i == 1: |
| 63 |
# Colorimetric |
| 64 |
RGB = (0, f, f) |
| 65 |
else: |
| 66 |
# Saturation |
| 67 |
RGB = (0, f, 0) |
| 68 |
block = 0 |
| 69 |
for R in range(clutres): |
| 70 |
r = min(max(R, f), clutres - 2) |
| 71 |
b1 = min(max(R, f) + 1, clutres - 2) |
| 72 |
b2 = min(max(R - 1, f), clutres - 2) |
| 73 |
for G in range(clutres): |
| 74 |
if tagname == "B2A": |
| 75 |
tag.clut.append([]) |
| 76 |
for B in range(clutres): |
| 77 |
if tagname == "B2A": |
| 78 |
tag.clut[block].append( |
| 79 |
[v / (clutres - 1.0) * 65535 for v in (R, G, B)] |
| 80 |
) |
| 81 |
# Everything should be good as long as primaries |
| 82 |
# are sensible (otherwise, when embedded in a PNG, |
| 83 |
# some programs like Firefox and XnView will ignore |
| 84 |
# the profile. This doesn't happen when embedded in |
| 85 |
# JPEG. Go figure...) |
| 86 |
if (R, G, B) in ( |
| 87 |
(f, f, 0), |
| 88 |
(0, f, f), |
| 89 |
(0, f, 0), |
| 90 |
# Magenta 64..223 |
| 91 |
(r, 0, r), |
| 92 |
(r, 0, b1), |
| 93 |
(r, 0, b2), |
| 94 |
# Red 64..223 |
| 95 |
(r, 0, 0), |
| 96 |
(r, 0, 1), |
| 97 |
(r, 1, 0), |
| 98 |
# CMY 255 |
| 99 |
(0, clutres - 1, clutres - 1), |
| 100 |
(clutres - 1, 0, clutres - 1), |
| 101 |
(clutres - 1, clutres - 1, 0), |
| 102 |
): |
| 103 |
if (R, G, B) == (0, clutres - 1, clutres - 1): |
| 104 |
# Map C -> R |
| 105 |
if tagname == "B2A": |
| 106 |
triplet = (65535, 0, 0) |
| 107 |
else: |
| 108 |
triplet = RGB2XYZ(1, 0, 0, scale=32768) |
| 109 |
elif (R, G, B) == (clutres - 1, 0, clutres - 1): |
| 110 |
# Map M -> G |
| 111 |
if tagname == "B2A": |
| 112 |
triplet = (0, 65535, 0) |
| 113 |
else: |
| 114 |
triplet = RGB2XYZ(0, 1, 0, scale=32768) |
| 115 |
elif (R, G, B) == (clutres - 1, clutres - 1, 0): |
| 116 |
# Map Y -> B |
| 117 |
if tagname == "B2A": |
| 118 |
triplet = (0, 0, 65535) |
| 119 |
else: |
| 120 |
triplet = RGB2XYZ(0, 0, 1, scale=32768) |
| 121 |
elif (R, G, B) != RGB: |
| 122 |
triplet = [0, 0, 0] |
| 123 |
else: |
| 124 |
if tagname == "B2A": |
| 125 |
# White RGB |
| 126 |
triplet = [65535] * 3 |
| 127 |
else: |
| 128 |
# White XYZ D50 |
| 129 |
triplet = get_whitepoint("D50", 32768) |
| 130 |
tag.clut[block][B] = triplet |
| 131 |
block += 1 |
| 132 |
tag.clut_writepng(filename[:-3] + tagname + "%i.CLUT.png" % i) |
| 133 |
if tagname == "A2B": |
| 134 |
tag.clut_writecgats(filename[:-3] + tagname + "%i.CLUT.ti3" % i) |
| 135 |
if add_fallback_matrix: |
| 136 |
srgb_icc = ICCProfile.from_rgb_space(srgb, "sRGB") |
| 137 |
p.tags.rTRC = srgb_icc.tags.rTRC |
| 138 |
p.tags.gTRC = srgb_icc.tags.gTRC |
| 139 |
p.tags.bTRC = srgb_icc.tags.bTRC |
| 140 |
# if False: # NEVER |
| 141 |
# # Don't really need this...? |
| 142 |
# t = specialpow(1 / 3.0, -2.4) |
| 143 |
# for i, v in enumerate(p.tags.gTRC[1:], 1): |
| 144 |
# v /= 65535.0 |
| 145 |
# p.tags.rTRC[i] = p.tags.gTRC[i] = max(convert_range(v, t, 1, 0, 1), 0) * 65535 |
| 146 |
# p.tags.bTRC[i] = min(convert_range(v, 0, t, 0, 1), 1) * 65535 |
| 147 |
p.tags.rXYZ = XYZType() # Map red to black |
| 148 |
p.tags.gXYZ = XYZType() # Map green to black |
| 149 |
p.tags.bXYZ = srgb_icc.tags.wtpt.pcs # Map blue to white |
| 150 |
p.calculate_id() |
| 151 |
p.write(filename) |
| 152 |
|
| 153 |
|
| 154 |
if __name__ == "__main__": |
| 155 |
create_rendering_intent_test_profile(sys.argv[1], add_fallback_matrix=sys.argv[2:3]) |