forked from alejandrojapkin/CocoaLUT
/
LUTColorSpace.m
306 lines (260 loc) · 15.5 KB
/
LUTColorSpace.m
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//
// LUTColorSpace.m
// Pods
//
// Created by Greg Cotten on 4/2/14.
//
//
#import "LUTColorSpace.h"
@interface LUTColorSpace ()
@end
@implementation LUTColorSpace
+ (instancetype)LUTColorSpaceWithDefaultWhitePoint:(LUTColorSpaceWhitePoint *)whitePoint
redChromaticityX:(double)redChromaticityX
redChromaticityY:(double)redChromaticityY
greenChromaticityX:(double)greenChromaticityX
greenChromaticityY:(double)greenChromaticityY
blueChromaticityX:(double)blueChromaticityX
blueChromaticityY:(double)blueChromaticityY
name:(NSString *)name{
return [[self alloc] initWithDefaultWhitePoint:whitePoint
redChromaticityX:redChromaticityX
redChromaticityY:redChromaticityY
greenChromaticityX:greenChromaticityX
greenChromaticityY:greenChromaticityY
blueChromaticityX:blueChromaticityX
blueChromaticityY:blueChromaticityY
forwardFootlambertCompensation:1.0
name:name];
}
+ (instancetype)LUTColorSpaceWithDefaultWhitePoint:(LUTColorSpaceWhitePoint *)whitePoint
redChromaticityX:(double)redChromaticityX
redChromaticityY:(double)redChromaticityY
greenChromaticityX:(double)greenChromaticityX
greenChromaticityY:(double)greenChromaticityY
blueChromaticityX:(double)blueChromaticityX
blueChromaticityY:(double)blueChromaticityY
forwardFootlambertCompensation:(double)flCompensation
name:(NSString *)name{
return [[self alloc] initWithDefaultWhitePoint:whitePoint
redChromaticityX:redChromaticityX
redChromaticityY:redChromaticityY
greenChromaticityX:greenChromaticityX
greenChromaticityY:greenChromaticityY
blueChromaticityX:blueChromaticityX
blueChromaticityY:blueChromaticityY
forwardFootlambertCompensation:flCompensation
name:name];
}
+ (instancetype)LUTColorSpaceWithNPM:(GLKMatrix3)npm
name:(NSString *)name{
return [[self alloc] initWithNPM:npm
forwardFootlambertCompensation:1.0
name:name];
}
+ (instancetype)LUTColorSpaceWithNPM:(GLKMatrix3)npm
forwardFootlambertCompensation:(double)flCompensation
name:(NSString *)name{
return [[self alloc] initWithNPM:npm
forwardFootlambertCompensation:flCompensation
name:name];
}
- (instancetype)initWithDefaultWhitePoint:(LUTColorSpaceWhitePoint *)whitePoint
redChromaticityX:(double)redChromaticityX
redChromaticityY:(double)redChromaticityY
greenChromaticityX:(double)greenChromaticityX
greenChromaticityY:(double)greenChromaticityY
blueChromaticityX:(double)blueChromaticityX
blueChromaticityY:(double)blueChromaticityY
forwardFootlambertCompensation:(double)flCompensation
name:(NSString *)name{
if (self = [super init]) {
self.redChromaticityX = redChromaticityX;
self.redChromaticityY = redChromaticityY;
self.greenChromaticityX = greenChromaticityX;
self.greenChromaticityY = greenChromaticityY;
self.blueChromaticityX = blueChromaticityX;
self.blueChromaticityY = blueChromaticityY;
self.forcesNPM = NO;
self.forwardFootlambertCompensation = flCompensation;
self.name = name;
}
return self;
}
- (instancetype)initWithNPM:(GLKMatrix3)npm
forwardFootlambertCompensation:(double)flCompensation
name:(NSString *)name{
if (self = [super init]) {
self.npm = npm;
self.forcesNPM = YES;
self.forwardFootlambertCompensation = flCompensation;
self.name = name;
}
return self;
}
- (instancetype)copyWithZone:(NSZone *)zone{
if(self.forcesNPM){
return [self.class LUTColorSpaceWithNPM:self.npm
forwardFootlambertCompensation:self.forwardFootlambertCompensation
name:[self.name copyWithZone:zone]];
}
else{
return [self.class LUTColorSpaceWithDefaultWhitePoint:[self.defaultWhitePoint copyWithZone:zone]
redChromaticityX:self.redChromaticityX
redChromaticityY:self.redChromaticityY
greenChromaticityX:self.greenChromaticityX
greenChromaticityY:self.greenChromaticityY
blueChromaticityX:self.blueChromaticityX
blueChromaticityY:self.blueChromaticityY
forwardFootlambertCompensation:self.forwardFootlambertCompensation
name:[self.name copyWithZone:zone]];
}
}
+ (GLKMatrix3)npmFromColorSpace:(LUTColorSpace *)colorSpace
whitePoint:(LUTColorSpaceWhitePoint *)whitePoint{
if(colorSpace.forcesNPM){
return colorSpace.npm;
}
double whiteChromaticityZ = 1 - (whitePoint.whiteChromaticityX + whitePoint.whiteChromaticityY);
double redChromaticityZ = 1 - (colorSpace.redChromaticityX + colorSpace.redChromaticityY);
double greenChromaticityZ = 1 - (colorSpace.greenChromaticityX + colorSpace.greenChromaticityY);
double blueChromaticityZ = 1 - (colorSpace.blueChromaticityX + colorSpace.blueChromaticityY);
GLKMatrix3 P = GLKMatrix3MakeWithRows(GLKVector3Make(colorSpace.redChromaticityX, colorSpace.greenChromaticityX, colorSpace.blueChromaticityX),
GLKVector3Make(colorSpace.redChromaticityY, colorSpace.greenChromaticityY, colorSpace.blueChromaticityY),
GLKVector3Make(redChromaticityZ, greenChromaticityZ, blueChromaticityZ));
GLKVector3 W = GLKVector3Make(whitePoint.whiteChromaticityX / whitePoint.whiteChromaticityY, 1.0, whiteChromaticityZ / whitePoint.whiteChromaticityY);
bool isInvertible;
GLKVector3 pInverseDotW = GLKMatrix3MultiplyVector3(GLKMatrix3Invert(P, &isInvertible), W);
NSAssert(isInvertible == YES, @"NPM can't be generated because matrix is not invertible");
GLKMatrix3 C = GLKMatrix3MakeWithRows(GLKVector3Make(pInverseDotW.x,0.0,0.0),
GLKVector3Make(0.0,pInverseDotW.y,0.0),
GLKVector3Make(0.0,0.0,pInverseDotW.z));
return GLKMatrix3Multiply(P, C);
}
+ (LUT3D *)convertLUT3D:(LUT3D *)lut fromColorSpace:(LUTColorSpace *)sourceColorSpace
whitePoint:(LUTColorSpaceWhitePoint *)sourceWhitePoint
toColorSpace:(LUTColorSpace *)destinationColorSpace
whitePoint:(LUTColorSpaceWhitePoint *)destinationWhitePoint{
//NSLog(@"Source NPM: %@\n Destination NPM: %@", NSStringFromGLKMatrix3(sourceColorSpace.npm), NSStringFromGLKMatrix3(destinationColorSpace.npm));
GLKMatrix3 transformationMatrix = [self transformationMatrixFromColorSpace:sourceColorSpace
whitePoint:sourceWhitePoint
toColorSpace:destinationColorSpace
whitePoint:destinationWhitePoint];
//NSLog(@"Transformation Matrix: %@", NSStringFromGLKMatrix3(transformationMatrix));
LUT3D *transformedLUT = [LUT3D LUTOfSize:[lut size] inputLowerBound:[lut inputLowerBound] inputUpperBound:[lut inputUpperBound]];
[transformedLUT copyMetaPropertiesFromLUT:lut];
double sourceFLCompensation = 1.0/sourceColorSpace.forwardFootlambertCompensation;
double destinationFLCompensation = destinationColorSpace.forwardFootlambertCompensation;
BOOL useFLCompensation = sourceFLCompensation != 1.0/destinationFLCompensation;
[transformedLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *sourceColor = [lut colorAtR:r g:g b:b];
if (useFLCompensation && sourceFLCompensation != 1.0) {
sourceColor = [sourceColor colorByMultiplyingByNumber:sourceFLCompensation];
}
GLKVector3 transformedColor = GLKMatrix3MultiplyVector3(transformationMatrix, GLKVector3Make(sourceColor.red, sourceColor.green, sourceColor.blue));
LUTColor *destinationColor = [LUTColor colorWithRed:transformedColor.x green:transformedColor.y blue:transformedColor.z];
if (useFLCompensation && destinationFLCompensation != 1.0) {
destinationColor = [destinationColor colorByMultiplyingByNumber:destinationFLCompensation];
}
[transformedLUT setColor:destinationColor r:r g:g b:b];
}];
return transformedLUT;
}
+ (GLKMatrix3)transformationMatrixFromColorSpace:(LUTColorSpace *)sourceColorSpace
whitePoint:(LUTColorSpaceWhitePoint *)sourceWhitePoint
toColorSpace:(LUTColorSpace *)destinationColorSpace
whitePoint:(LUTColorSpaceWhitePoint *)destinationWhitePoint{
bool isInvertible;
GLKMatrix3 transformationMatrix = GLKMatrix3Multiply(GLKMatrix3Invert([self npmFromColorSpace:destinationColorSpace whitePoint:destinationWhitePoint], &isInvertible), [self npmFromColorSpace:sourceColorSpace whitePoint:sourceWhitePoint]);
NSAssert(isInvertible == YES, @"Transformation Matrix can't be generated because matrix is not invertible");
return transformationMatrix;
}
+ (NSArray *)knownColorSpaces{
NSArray *allKnownColorSpaces = @[[self rec709ColorSpace],
[self dciP3ColorSpace],
[self rec2020ColorSpace],
[self alexaWideGamutColorSpace],
[self sGamut3CineColorSpace],
[self sGamutColorSpace],
[self acesGamutColorSpace],
[self xyzColorSpace]];
return allKnownColorSpaces;
}
+ (instancetype)rec709ColorSpace{
return [self LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint d65WhitePoint]
redChromaticityX:0.64
redChromaticityY:0.33
greenChromaticityX:0.30
greenChromaticityY:0.60
blueChromaticityX:0.15
blueChromaticityY:0.06
name:@"Rec. 709"];
}
+ (instancetype)dciP3ColorSpace{
return [self LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint dciWhitePoint]
redChromaticityX:0.680
redChromaticityY:0.320
greenChromaticityX:0.265
greenChromaticityY:0.69
blueChromaticityX:0.15
blueChromaticityY:0.06
name:@"DCI-P3"];
}
+ (instancetype)rec2020ColorSpace{
return [LUTColorSpace LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint d65WhitePoint]
redChromaticityX:0.708
redChromaticityY:0.292
greenChromaticityX:0.170
greenChromaticityY:0.797
blueChromaticityX:0.131
blueChromaticityY:0.046
name:@"Rec. 2020"];
}
+ (instancetype)alexaWideGamutColorSpace{
return [LUTColorSpace LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint d65WhitePoint]
redChromaticityX:0.6840
redChromaticityY:0.3130
greenChromaticityX:0.2210
greenChromaticityY:0.8480
blueChromaticityX:0.0861
blueChromaticityY:-0.1020
name:@"Alexa Wide Gamut"];
}
+ (instancetype)sGamut3CineColorSpace{
return [LUTColorSpace LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint d65WhitePoint]
redChromaticityX:0.76600
redChromaticityY:0.27500
greenChromaticityX:0.22500
greenChromaticityY:0.80000
blueChromaticityX:0.08900
blueChromaticityY:-0.08700
name:@"S-Gamut3.Cine"];
}
+ (instancetype)sGamutColorSpace{
return [LUTColorSpace LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint d65WhitePoint]
redChromaticityX:0.73000
redChromaticityY:0.28000
greenChromaticityX:0.14000
greenChromaticityY:0.85500
blueChromaticityX:0.10000
blueChromaticityY:-0.05000
name:@"S-Gamut/S-Gamut3"];
}
+ (instancetype)acesGamutColorSpace{
return [LUTColorSpace LUTColorSpaceWithDefaultWhitePoint:[LUTColorSpaceWhitePoint d60WhitePoint]
redChromaticityX:0.73470
redChromaticityY:0.26530
greenChromaticityX:0.00000
greenChromaticityY:1.00000
blueChromaticityX:0.00010
blueChromaticityY:-0.07700
name:@"ACES Gamut"];
}
+ (instancetype)xyzColorSpace{
return [LUTColorSpace LUTColorSpaceWithNPM:GLKMatrix3MakeWithRows(GLKVector3Make(1.0, 0.0, 0.0),
GLKVector3Make(0.0, 1.0, 0.0),
GLKVector3Make(0.0, 0.0, 1.0))
forwardFootlambertCompensation:0.916555
name:@"DCI-XYZ"];
}
@end