forked from alejandrojapkin/CocoaLUT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LUTAction.m
393 lines (326 loc) · 18.6 KB
/
LUTAction.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//
// LUTAction.m
// Lattice
//
// Created by Greg Cotten on 6/19/14.
// Copyright (c) 2014 Wil Gieseler. All rights reserved.
//
#import "LUTAction.h"
@interface LUTAction ()
@property (strong) LUT* cachedInLUT;
@property (strong) LUT* cachedOutLUT;
@end
@implementation LUTAction
+(instancetype)actionWithBlock:(LUT *(^)(LUT *lut))actionBlock
actionName:(NSString *)actionName
actionMetadata:(M13OrderedDictionary *)actionMetadata{
return [[self alloc] initWithBlock:actionBlock actionName:actionName actionMetadata:actionMetadata];
}
+(instancetype)actionWithBypassBlockWithName:(NSString *)actionName{
return [self actionWithBlock:^LUT *(LUT *lut) {
return lut;
}
actionName:actionName
actionMetadata:M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"Bypass"}])];
}
-(instancetype)initWithBlock:(LUT *(^)(LUT *lut))actionBlock
actionName:(NSString *)actionName
actionMetadata:(M13OrderedDictionary *)actionMetadata{
if(self = [super init]){
self.actionBlock = actionBlock;
self.actionName = actionName ? actionName : @"Untitled Action";
if (actionMetadata == nil){
@throw [NSException exceptionWithName:@"LUTActionInitError" reason:@"Action metadata must not be nil." userInfo:nil];
}
if (actionMetadata[@"id"] == nil){
@throw [NSException exceptionWithName:@"LUTActionInitError" reason:@"Action metadata doesn't contain an ID." userInfo:nil];
}
self.actionMetadata = actionMetadata;
}
return self;
}
-(LUT *)LUTByUsingActionBlockOnLUT:(LUT *)lut{
if(self.cachedInLUT != nil && self.cachedInLUT == lut){
//NSLog(@"\"%@\" cached", self);
[self.cachedOutLUT copyMetaPropertiesFromLUT:lut];
return self.cachedOutLUT;
}
else{
//NSLog(@"\"%@\" not cached", self);
self.cachedInLUT = lut;
self.cachedOutLUT = self.actionBlock(lut);
[self.cachedOutLUT copyMetaPropertiesFromLUT:lut];
return self.cachedOutLUT;
}
}
-(NSString *)description{
return [self.actionName stringByAppendingString:[NSString stringWithFormat:@": %@", [self actionDetails]]];
}
-(instancetype)copyWithZone:(NSZone *)zone{
LUTAction *copiedAction = [LUTAction actionWithBlock:self.actionBlock actionName:[self.actionName copyWithZone:zone] actionMetadata:[self.actionMetadata copyWithZone:zone]];
copiedAction.cachedInLUT = self.cachedInLUT;
copiedAction.cachedOutLUT = self.cachedOutLUT;
return copiedAction;
}
-(NSString *)actionDetails{
NSString *outString = [NSString string];
for(NSString *key in [self.actionMetadata allKeys]){
if(![key isEqualToString:@"id"]){
outString = [outString stringByAppendingString:[NSString stringWithFormat:@"\n%@: %@", key, self.actionMetadata[key]]];
}
}
return outString;
}
+(instancetype)actionWithLUTBySwizzlingWithMethod:(LUT1DSwizzleChannelsMethod)method{
M13OrderedDictionary *methods = [LUT1D LUT1DSwizzleChannelsMethods];
NSString *methodName;
for(NSString *key in methods.allKeys){
if ([methods[key] isEqualToNumber:@(method)]) {
methodName = key;
}
}
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"MixCurves"},
@{@"method":methodName?:@"Unknown"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
if (isLUT1D(lut)) {
return [(LUT1D *)lut LUT1DBySwizzling1DChannelsWithMethod:method];
}
else{
return [(LUT3D *)lut LUT3DBySwizzling1DChannelsWithMethod:method strictness:NO];
}
}
actionName:[NSString stringWithFormat:@"Mix Curves (%@)", methodName]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUT3DByConvertingColorTemperatureFromSourceColorSpace:(LUTColorSpace *)sourceColorSpace
sourceTransferFunction:(LUTColorTransferFunction *)sourceTransferFunction
sourceColorTemperature:(LUTColorSpaceWhitePoint *)sourceColorTemperature
destinationColorTemperature:(LUTColorSpaceWhitePoint *)destinationColorTemperature{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ConvertColorTemperature"},
@{@"sourceColorSpace": sourceColorSpace.name},
@{@"sourceTransferFunction": sourceTransferFunction.name},
@{@"sourceColorTemperature": sourceColorTemperature.name},
@{@"destinationColorTemperature": destinationColorTemperature.name}]);
return [self actionWithBlock:^LUT *(LUT *lut) {
return [LUTColorSpace convertColorTemperatureFromLUT3D:(LUT3D *)lut
sourceColorSpace:sourceColorSpace
sourceTransferFunction:sourceTransferFunction
sourceColorTemperature:sourceColorTemperature
destinationColorTemperature:destinationColorTemperature];
}
actionName:[NSString stringWithFormat:@"Change Color Temperature"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByChangingInputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ChangeInputBounds"},
@{@"inputLowerBound": @(inputLowerBound)},
@{@"inputUpperBound": @(inputUpperBound)}]);
return [self actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByChangingInputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
}
actionName:[NSString stringWithFormat:@"Change Input Bounds to [%.3f, %.3f]", inputLowerBound, inputUpperBound]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByClampingLowerBound:(double)lowerBound
upperBound:(double)upperBound{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"Clamp"},
@{@"lowerBound": @(lowerBound)},
@{@"upperBound": @(upperBound)}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByClampingLowerBound:lowerBound upperBound:upperBound];
}
actionName:[NSString stringWithFormat:@"Clamp LUT to [%.3f, %.3f]", lowerBound, upperBound]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByCombiningWithLUT:(LUT *)lutToCombine
lutURL:(NSURL *)lutURL{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"Combine"},
@{@"lutPath": [lutURL path]}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByCombiningWithLUT:lutToCombine];
}
actionName:[NSString stringWithFormat:@"Combine with LUT"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByCombiningBehindWithLUT:(LUT *)lutToCombineBehind
lutURL:(NSURL *)lutURL{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"CombineBehind"},
@{@"lutPath": [lutURL path]}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lutToCombineBehind LUTByCombiningWithLUT:lut];
}
actionName:[NSString stringWithFormat:@"Combine Behind LUT"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUT3DByApplyingColorMatrixColumnMajorM00:(double)m00
m01:(double)m01
m02:(double)m02
m10:(double)m10
m11:(double)m11
m12:(double)m12
m20:(double)m20
m21:(double)m21
m22:(double)m22{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ApplyColorMatrix"},
@{@"m00": @(m00)},
@{@"m01": @(m01)},
@{@"m02": @(m02)},
@{@"m10": @(m10)},
@{@"m11": @(m11)},
@{@"m12": @(m12)},
@{@"m20": @(m20)},
@{@"m21": @(m21)},
@{@"m22": @(m22)}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [(LUT3D *)lut LUT3DByApplyingColorMatrixColumnMajorM00:m00
m01:m01
m02:m02
m10:m10
m11:m11
m12:m12
m20:m20
m21:m21
m22:m22];
}
actionName:[NSString stringWithFormat:@"Apply Color Matrix"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByRemappingValuesWithInputLow:(double)inputLow
inputHigh:(double)inputHigh
outputLow:(double)outputLow
outputHigh:(double)outputHigh{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleOutput"},
@{@"inputLow": @(inputLow)},
@{@"inputHigh": @(inputHigh)},
@{@"outputLow": @(outputLow)},
@{@"outputHigh": @(outputHigh)}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByRemappingValuesWithInputLow:inputLow
inputHigh:inputHigh
outputLow:outputLow
outputHigh:outputHigh
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Scale Output"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByRemappingValuesWithInputLowColor:(LUTColor *)inputLowColor
inputHigh:(LUTColor *)inputHighColor
outputLow:(LUTColor *)outputLowColor
outputHigh:(LUTColor *)outputHighColor{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleOutputRGB"},
@{@"inputLowColor": inputLowColor.rgbArray},
@{@"inputHighColor": inputHighColor.rgbArray},
@{@"outputLowColor": outputLowColor.rgbArray},
@{@"outputHighColor": outputHighColor.rgbArray}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByRemappingFromInputLowColor:inputLowColor
inputHigh:inputHighColor
outputLow:outputLowColor
outputHigh:outputHighColor
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Scale Output RGB"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByScalingTo01{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleTo01"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByRemappingValuesWithInputLow:lut.minimumOutputValue
inputHigh:lut.maximumOutputValue
outputLow:0
outputHigh:1
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Scale Absolute 0 to 1"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByScalingCurvesTo01{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleCurvesTo01"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
LUT *usedLUT = isLUT1D(lut) ? lut : LUTAsLUT1D(lut, lut.size);
return [lut LUTByRemappingValuesWithInputLow:usedLUT.minimumOutputValue
inputHigh:usedLUT.maximumOutputValue
outputLow:0
outputHigh:1
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Scale Curves 0 to 1"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByScalingRGBTo01{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleRGBTo01"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByRemappingFromInputLowColor:lut.minimumOutputColor
inputHigh:lut.maximumOutputColor
outputLow:[LUTColor colorWithRed:0 green:0 blue:0]
outputHigh:[LUTColor colorWithRed:1 green:1 blue:1]
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Scale Absolute RGB 0 to 1"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByScalingCurvesRGBTo01{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleCurvesRGBTo01"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
LUT *usedLUT = isLUT1D(lut) ? lut : LUTAsLUT1D(lut, lut.size);
return [lut LUTByRemappingFromInputLowColor:usedLUT.minimumOutputColor
inputHigh:usedLUT.maximumOutputColor
outputLow:[LUTColor colorWithRed:0 green:0 blue:0]
outputHigh:[LUTColor colorWithRed:1 green:1 blue:1]
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Scale Curves RGB 0 to 1"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByScalingLegalToExtended{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleLegalToExtended"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByRemappingValuesWithInputLow:LEGAL_LEVELS_MIN
inputHigh:LEGAL_LEVELS_MAX
outputLow:EXTENDED_LEVELS_MIN
outputHigh:EXTENDED_LEVELS_MAX
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Legal to Extended"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByScalingExtendedToLegal{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"ScaleExtendedToLegal"}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByRemappingValuesWithInputLow:EXTENDED_LEVELS_MIN
inputHigh:EXTENDED_LEVELS_MAX
outputLow:LEGAL_LEVELS_MIN
outputHigh:LEGAL_LEVELS_MAX
bounded:NO];
}
actionName:[NSString stringWithFormat:@"Extended to Legal"]
actionMetadata:actionMetadata];
}
+(instancetype)actionWithLUTByResizingToSize:(NSUInteger)size{
M13OrderedDictionary *actionMetadata =
M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"id":@"Resize"},
@{@"size": @(size)}]);
return [LUTAction actionWithBlock:^LUT *(LUT *lut) {
return [lut LUTByResizingToSize:size];
}
actionName:[NSString stringWithFormat:@"Resize to %i", (int)size]
actionMetadata:actionMetadata];
}
@end