-
Notifications
You must be signed in to change notification settings - Fork 1
/
RGB_XYZ.py
181 lines (140 loc) · 7.07 KB
/
RGB_XYZ.py
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
# http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
from ColorChannelInst import createColorChannelInstance, WPD65, WPD50, WORKSPACE_CIEXYZ
from EXIF_ColorProfileParser import getChromaticAdaptationMat, CHROMATIC_ADAPTION_BRADFORD
"""
D65 =====================================================
"""
sRGBtoXYZ_D65 = [[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]]
XYZtosRGB_D65 = [[3.2404542, -1.5371385, -0.4985314],
[-0.9692660, 1.8760108, 0.0415560],
[0.0556434, -0.2040259, 1.0572252]]
AdobeRGBtoXYZ_D65 = [[0.5767309, 0.1855540, 0.1881852],
[0.2973769, 0.6273491, 0.0752741],
[0.0270343, 0.0706872, 0.9911085]]
XYZtoAdobeRGB_D65 = [[2.0413690, -0.5649464, -0.3446944],
[-0.9692660, 1.8760108, 0.0415560],
[0.0134474, -0.1183897, 1.0154096]]
"""
D50 =====================================================
"""
sRGBtoXYZ_D50 = [[0.4360747, 0.3850649, 0.1430804],
[0.2225045, 0.7168786, 0.0606169],
[0.0139322, 0.0971045, 0.7141733]]
XYZtosRGB_D50 = [[3.1338561, -1.6168667, -0.4906146],
[-0.9787684, 1.9161415, 0.0334540],
[0.0719453, -0.2289914, 1.4052427]]
AdobeRGBtoXYZ_D50 = [[0.6097559, 0.2052401, 0.1492240],
[0.3111242, 0.6256560, 0.0632197],
[0.0194811, 0.0608902, 0.7448387]]
XYZtoAdobeRGB_D50 = [[1.9624274, -0.6105343,-0.3413404],
[-0.9787684, 1.9161415, 0.0334540],
[0.0286869, -0.1406752, 1.3487655]]
ProPhototoXYZ_D50 = [[0.7976749, 0.1351917, 0.0313534],
[0.2880402, 0.7118741, 0.0000857],
[0.0000000, 0.0000000, 0.8252100]]
XYZtoProPhoto_D50 = [[1.3459433, -0.2556075, -0.0511118],
[-0.5445989, 1.5081673, 0.0205351],
[0.0000000, 0.0000000, 1.2118128]]
dicWPRGBtoXYZMatrix = { WPD65 : { 'sRGB' : sRGBtoXYZ_D65,
'AdobeRGB' : AdobeRGBtoXYZ_D65},
WPD50 : { 'sRGB' : sRGBtoXYZ_D50,
'AdobeRGB' : AdobeRGBtoXYZ_D50,
'ProPhotoRGB' : ProPhototoXYZ_D50}}
dicWPXYZtoRGBMartix = { WPD65 : { 'sRGB' : XYZtosRGB_D65,
'AdobeRGB' : XYZtoAdobeRGB_D65},
WPD50 : { 'sRGB' : XYZtosRGB_D50,
'AdobeRGB' : XYZtoAdobeRGB_D50,
'ProPhotoRGB' : XYZtoProPhoto_D50}}
dictGammaValue = { 'sRGB' : 2.2,
'AdobeRGB' : 2.2,
'ProPhotoRGB' : 1.8 }
def gammaDecode(normColor, gamma=2.2):
return normColor ** (gamma)
def gammaEncode(normColor, gamma=2.2):
return normColor ** (1.0/gamma)
def normalizeColor(color):
assert (type(color)==int and 0 <= color <= 255)
return color / 255.0
def denormalizeTo8bit(normValue):
return min(255, max(0, int(normValue * 255)))
def fixNormalBoundary(value):
return max(0.0, min(1.0, value))
def RGBtoXYZ(ccInst):
w, h = ccInst.size[0], ccInst.size[1]
inputWP = ccInst.getWhitePoint()
inputWS = ccInst.getWorkSpace()
matrix = dicWPRGBtoXYZMatrix.get(inputWP, {}).get(inputWS, {})
inputGamma = dictGammaValue.get(inputWS, 0)
if not matrix or inputGamma == 0:
assert False, "No matching inputr matrix - %s / %s / %f "%(inputWS,\
inputWP, inputGamma)
return None
newCCInst = createColorChannelInstance(package={'w':w,
'h':h,
'comp':'XYZ',
'ws':WORKSPACE_CIEXYZ,
'wp':inputWP})
for y in range(h):
for x in range(w):
R,G,B = ccInst[x,y]
nR, nG, nB = normalizeColor(R), normalizeColor(G), normalizeColor(B)
nLR = gammaDecode(nR, inputGamma)
nLG = gammaDecode(nG, inputGamma)
nLB = gammaDecode(nB, inputGamma)
X = sum(map(lambda x,y: x*y, [nLR, nLG, nLB], matrix[0]))
Y = sum(map(lambda x,y: x*y, [nLR, nLG, nLB], matrix[1]))
Z = sum(map(lambda x,y: x*y, [nLR, nLG, nLB], matrix[2]))
newCCInst[x,y] = X,Y,Z
return newCCInst
def XYZ_WPTransform(ccInst, targetWP):
w, h = ccInst.size[0], ccInst.size[1]
inputWP = ccInst.getWhitePoint()
matrix = getChromaticAdaptationMat(CHROMATIC_ADAPTION_BRADFORD, inputWP,\
targetWP)
if inputWP == targetWP:
print "Input white point is the same as target white point, no need to transform"
return ccInst
if not matrix:
print "No chromatic adaptation matrix found, do NOT transform"
return ccInst
newCCInst = createColorChannelInstance(package={'w':w,
'h':h,
'comp':'XYZ',
'ws':WORKSPACE_CIEXYZ,
'wp':targetWP})
for y in range(h):
for x in range(w):
Xs, Ys, Zs = ccInst[x,y]
Xd = sum(map(lambda x,y:x*y, [Xs, Ys, Zs], matrix[0]))
Yd = sum(map(lambda x,y:x*y, [Xs, Ys, Zs], matrix[1]))
Zd = sum(map(lambda x,y:x*y, [Xs, Ys, Zs], matrix[2]))
newCCInst[x,y] = fixNormalBoundary(Xd), fixNormalBoundary(Yd), fixNormalBoundary(Zd)
return newCCInst
def XYZtoRGB(ccInst, ws='sRGB', wp=None):
w, h = ccInst.size[0], ccInst.size[1]
inputWP = ccInst.getWhitePoint()
targetWP = wp if wp != None else inputWP
targetWS = ws if ws != 'sRGB' else 'sRGB'
newCCInst = createColorChannelInstance(package={'w':w,
'h':h,
'comp':'RGB',
'ws':targetWS,
'wp':targetWP})
matrix = dicWPXYZtoRGBMartix.get(targetWP, {}).get(targetWS, {})
targetGamma = dictGammaValue.get(targetWS, 0)
if not matrix or targetGamma == 0:
assert False, "No matching target matrix - %s / %s / %f "%(targetWS, targetWP, targetGamma)
return None
for y in range(h):
for x in range(w):
X,Y,Z = ccInst[x,y]
nLR2 = fixNormalBoundary(sum(map(lambda x,y:x*y, [X, Y, Z], matrix[0])))
nLG2 = fixNormalBoundary(sum(map(lambda x,y:x*y, [X, Y, Z], matrix[1])))
nLB2 = fixNormalBoundary(sum(map(lambda x,y:x*y, [X, Y, Z], matrix[2])))
nGER = gammaEncode(nLR2, targetGamma)
nGEG = gammaEncode(nLG2, targetGamma)
nGEB = gammaEncode(nLB2, targetGamma)
newCCInst[x,y] = denormalizeTo8bit(nGER), denormalizeTo8bit(nGEG), denormalizeTo8bit(nGEB)
return newCCInst