forked from alejandrojapkin/CocoaLUT
/
LUT.m
854 lines (702 loc) · 33.9 KB
/
LUT.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
//
// LUT.m
// DropLUT
//
// Created by Wil Gieseler on 12/15/13.
// Copyright (c) 2013 Wil Gieseler. All rights reserved.
//
#import "LUT.h"
#import "CocoaLUT.h"
#import "LUTFormatter.h"
#import "LUTError.h"
#if defined(COCOAPODS_POD_AVAILABLE_VVSceneLinearImageRep)
#import <VVSceneLinearImageRep/NSImage+SceneLinear.h>
#endif
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#elif TARGET_OS_MAC
#import "NSImage+CocoaLUT.h"
#endif
@interface LUT ()
@end
@implementation LUT
- (instancetype)init {
if (self = [super init]) {
self.metadata = [NSMutableDictionary dictionary];
self.userInfo = [NSMutableDictionary dictionary];
self.passthroughFileOptions = @{};
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
if (self = [super init]) {
self.metadata = [aDecoder decodeObjectForKey:@"metadata"];
self.userInfo = [aDecoder decodeObjectForKey:@"userInfo"]?:[NSMutableDictionary dictionary];
self.passthroughFileOptions = [aDecoder decodeObjectForKey:@"passthroughFileOptions"];
self.size = [aDecoder decodeIntegerForKey:@"size"];
self.inputLowerBound = [aDecoder decodeDoubleForKey:@"inputLowerBound"];
self.inputUpperBound = [aDecoder decodeDoubleForKey:@"inputUpperBound"];
if(self.inputLowerBound >= self.inputUpperBound){
@throw [NSException exceptionWithName:@"LUTCreationError" reason:@"Input Lower Bound >= Input Upper Bound" userInfo:nil];
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.metadata forKey:@"metadata"];
[aCoder encodeObject:self.userInfo forKey:@"userInfo"];
[aCoder encodeObject:self.passthroughFileOptions forKey:@"passthroughFileOptions"];
[aCoder encodeInteger:self.size forKey:@"size"];
[aCoder encodeDouble:self.inputLowerBound forKey:@"inputLowerBound"];
[aCoder encodeDouble:self.inputUpperBound forKey:@"inputUpperBound"];
}
- (instancetype)initWithSize:(NSUInteger)size
inputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
if (self = [super init]) {
if(inputLowerBound >= inputUpperBound){
@throw [NSException exceptionWithName:@"LUTCreationError" reason:@"Input Lower Bound >= Input Upper Bound" userInfo:nil];
}
self.metadata = [NSMutableDictionary dictionary];
self.passthroughFileOptions = [NSDictionary dictionary];
self.userInfo = [NSMutableDictionary dictionary];
self.size = size;
self.inputLowerBound = inputLowerBound;
self.inputUpperBound = inputUpperBound;
}
return self;
}
+ (instancetype)LUTFromURL:(NSURL *)url error:(NSError * __autoreleasing *)error{
LUTFormatter *formatter = [LUTFormatter LUTFormatterValidForReadingURL:url];
if(formatter == nil){
if (error) {
*error = [NSError errorWithDomain:LUTErrorDomain
code:LUTErrorLUTCouldNotBeRead
userInfo:@{NSLocalizedDescriptionKey:NSLocalizedString(@"No suitable reader was found.", nil), @"errorName":@"LUTFormatterNotFound"}];
}
return nil;
}
LUT *lut;
@try {
lut = [[formatter class] LUTFromURL:url];
}
@catch (NSException *exception) {
if (error) {
*error = [NSError errorWithDomain:LUTErrorDomain
code:LUTErrorLUTCouldNotBeRead
userInfo:@{NSLocalizedDescriptionKey:NSLocalizedString(exception.reason?:@"Unknown", nil), @"errorName":exception.name?:@"Unknown", @"lutFormatterName":[formatter.class formatterName]}];
}
return nil;
}
return lut;
}
+ (instancetype)LUTFromData:(NSData *)data formatterID:(NSString *)formatterID error:(NSError * __autoreleasing *)error
{
LUTFormatter *formatter = [LUTFormatter LUTFormatterWithID:formatterID];
if(formatter == nil){
if (error) {
*error = [NSError errorWithDomain:LUTErrorDomain
code:LUTErrorLUTCouldNotBeRead
userInfo:@{NSLocalizedDescriptionKey:NSLocalizedString(@"No suitable reader was found.", nil), @"errorName":@"LUTFormatterNotFound"}];
}
return nil;
}
LUT *lut;
@try {
lut = [[formatter class] LUTFromData:data];
}
@catch (NSException *exception) {
if (error) {
*error = [NSError errorWithDomain:LUTErrorDomain
code:LUTErrorLUTCouldNotBeRead
userInfo:@{NSLocalizedDescriptionKey:NSLocalizedString(exception.reason, nil)?:NSLocalizedString(@"Unknown", nil), @"errorName":exception.name?:@"Unknown", @"lutFormatterName":[formatter.class formatterName]}];
}
return nil;
}
}
+ (instancetype)LUTFromDataRepresentation:(NSData *)data{
return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}
- (NSData *)dataRepresentation{
return [NSKeyedArchiver archivedDataWithRootObject:self];
}
- (BOOL)writeToURL:(NSURL *)url
atomically:(BOOL)atomically
formatterID:(NSString *)formatterID
options:(NSDictionary *)options
conformLUT:(BOOL)conformLUT{
LUTFormatter *formatter = [LUTFormatter LUTFormatterWithID:formatterID];
//validation
if(formatter == nil){
NSLog(@"Formatter not found for ID \"%@\".", formatterID);
return NO;
}
if([[formatter class] isValidWriterForLUTType:self] == NO){
NSLog(@"%@ is not a valid writer for LUT (%@) with options: %@.", [[formatter class] formatterName], self, options);
return NO;
}
if (options == nil) {
options = [[formatter class] defaultOptions];
}
//----
NSArray *actionsForConformance = [[formatter class] conformanceLUTActionsForLUT:self options:options];
if (actionsForConformance.count != 0 && conformLUT == NO) {
NSMutableString *conformanceInfo = [[NSMutableString alloc] init];
for (int i = 0; i < actionsForConformance.count; i++) {
[conformanceInfo appendString:((LUTAction *)actionsForConformance[0]).actionName];
if (i+1 != actionsForConformance.count) {
[conformanceInfo appendString:@"\n"];
}
}
NSLog(@"LUT requires conformance before saving. Info:\n%@", conformanceInfo);
return NO;
}
LUT *outputLUT = [self copy];
for(LUTAction *action in actionsForConformance){
outputLUT = [action LUTByUsingActionBlockOnLUT:outputLUT];
}
NSData *lutData = [outputLUT dataFromLUTWithFormatterID:formatterID options:options];
if (lutData == nil) {
NSLog(@"Formatter couldn't create data from LUT.");
return NO;
}
return [lutData writeToURL:url atomically:atomically];
}
- (NSData *)dataFromLUTWithFormatterID:(NSString *)formatterID
options:(NSDictionary *)options{
LUTFormatter *formatter = [LUTFormatter LUTFormatterWithID:formatterID];
if (options == nil) {
NSLog(@"Options can't be nil.");
return nil;
}
if(formatter == nil){
NSLog(@"Formatter can't be nil.");
return nil;
}
else if([[formatter class] isValidWriterForLUTType:self] == NO){
NSLog(@"%@ is not a valid writer for LUT (%@) with options: %@.", [[formatter class] formatterName], self, options);
return nil;
}
else{
return [[formatter class] dataFromLUT:self withOptions:options];
}
}
+ (instancetype)LUTOfSize:(NSUInteger)size
inputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (instancetype)LUTIdentityOfSize:(NSUInteger)size
inputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
LUT *identityLUT = [self LUTOfSize:size inputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
[identityLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[identityLUT setColor:[identityLUT identityColorAtR:r g:g b:b] r:r g:g b:b];
}];
return identityLUT;
}
- (void)copyMetaPropertiesFromLUT:(LUT *)lut{
self.title = [lut.title copy];
self.descriptionText = [lut.descriptionText copy];
self.metadata = [lut.metadata mutableCopy];
self.userInfo = [lut.userInfo mutableCopy];
self.passthroughFileOptions = [lut.passthroughFileOptions copy];
}
- (void)LUTLoopWithBlock:(void (^)(size_t r, size_t g, size_t b))block{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (instancetype)LUTByResizingToSize:(NSUInteger)newSize {
if (newSize == [self size]) {
return [self copy];
}
LUT *resizedLUT = [[self class] LUTOfSize:newSize inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[resizedLUT copyMetaPropertiesFromLUT:self];
double ratio = ((double)self.size - 1.0) / ((double)newSize - 1.0);
[resizedLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtInterpolatedR:clampUpperBound(r * ratio, self.size-1.0) g:clampUpperBound(g * ratio, self.size-1.0) b:clampUpperBound(b * ratio, self.size-1.0)];
[resizedLUT setColor:color r:r g:g b:b];
}];
return resizedLUT;
}
- (instancetype)LUTByClampingLowerBound:(double)lowerBound
upperBound:(double)upperBound{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] clampedWithLowerBound:lowerBound upperBound:upperBound] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByClampingLowerBoundOnly:(double)lowerBound{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] clampedWithLowerBoundOnly:lowerBound] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByClampingUpperBoundOnly:(double)upperBound{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] clampedWithUpperBoundOnly:upperBound] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByOffsettingWithColor:(LUTColor *)offsetColor{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] colorByAddingColor:offsetColor] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByRemappingValuesWithInputLow:(double)inputLow
inputHigh:(double)inputHigh
outputLow:(double)outputLow
outputHigh:(double)outputHigh
bounded:(BOOL)bounded{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] remappedFromInputLow:inputLow
inputHigh:inputHigh
outputLow:outputLow
outputHigh:outputHigh
bounded:bounded] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByRemappingFromInputLowColor:(LUTColor *)inputLowColor
inputHigh:(LUTColor *)inputHighColor
outputLow:(LUTColor *)outputLowColor
outputHigh:(LUTColor *)outputHighColor
bounded:(BOOL)bounded{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] colorByRemappingFromInputLowColor:inputLowColor
inputHigh:inputHighColor
outputLow:outputLowColor
outputHigh:outputHighColor
bounded:bounded] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByMultiplyingByColor:(LUTColor *)color{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] colorByMultiplyingColor:color] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByChangingStrength:(double)strength{
if(strength > 1.0){
@throw [NSException exceptionWithName:@"ChangeStrengthError" reason:[NSString stringWithFormat:@"You can't set the strength of the LUT past 1.0 (%f)", strength] userInfo:nil];
}
if(strength == 1.0){
return [self copy];
}
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self identityColorAtR:r g:g b:b] lerpTo:[self colorAtR:r g:g b:b] amount:strength] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByCombiningWithLUT:(LUT *)otherLUT {
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (instancetype)LUTByChangingInputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
if(inputLowerBound == [self inputLowerBound] && inputUpperBound == [self inputUpperBound]){
return [self copy];
}
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *identityColor = [newLUT identityColorAtR:r g:g b:b];
[newLUT setColor:[self colorAtColor:identityColor] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByInvertingColor{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:self.inputLowerBound inputUpperBound:self.inputUpperBound];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *startColor = [self colorAtR:r g:g b:b];
[newLUT setColor:[startColor colorByInvertingColorWithMinimumValue:0 maximumValue:1] r:r g:g b:b];
}];
return newLUT;
}
- (LUTColor *)identityColorAtR:(double)redPoint g:(double)greenPoint b:(double)bluePoint{
double red = remap(redPoint, 0, [self size] - 1, [self inputLowerBound], [self inputUpperBound]);
double green = remap(greenPoint, 0, [self size] - 1, [self inputLowerBound], [self inputUpperBound]);
double blue = remap(bluePoint, 0, [self size] - 1, [self inputLowerBound], [self inputUpperBound]);
return [LUTColor colorWithRed:red green:green blue:blue];
}
- (LUTColor *)indexForColor:(LUTColor *)color{
double redRemappedInterpolatedIndex = remapNoError(color.red, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
double greenRemappedInterpolatedIndex = remapNoError(color.green, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
double blueRemappedInterpolatedIndex = remapNoError(color.blue, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
return [LUTColor colorWithRed:redRemappedInterpolatedIndex green:greenRemappedInterpolatedIndex blue:blueRemappedInterpolatedIndex];
}
- (LUTColor *)colorAtColor:(LUTColor *)color{
color = [color clampedWithLowerBound:[self inputLowerBound] upperBound:[self inputUpperBound]];
double redRemappedInterpolatedIndex = remap(color.red, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
double greenRemappedInterpolatedIndex = remap(color.green, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
double blueRemappedInterpolatedIndex = remap(color.blue, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
return [self colorAtInterpolatedR:clamp(redRemappedInterpolatedIndex, 0, self.size-1)
g:clamp(greenRemappedInterpolatedIndex, 0, self.size-1)
b:clamp(blueRemappedInterpolatedIndex, 0, self.size-1)];
}
- (LUTColor *)colorAtR:(NSUInteger)r g:(NSUInteger)g b:(NSUInteger)b{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (LUTColor *)colorAtInterpolatedR:(double)redPoint g:(double)greenPoint b:(double)bluePoint{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (void)setColor:(LUTColor *)color r:(NSUInteger)r g:(NSUInteger)g b:(NSUInteger)b{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (LUTColor *)maximumOutputColor{
LUTColor *start = [self colorAtR:0 g:0 b:0];
__block double maxRed = start.red;
__block double maxGreen = start.green;
__block double maxBlue = start.blue;
[self LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtR:r g:g b:b];
if(color.red > maxRed){
maxRed = color.red;
}
if(color.green > maxGreen){
maxGreen = color.green;
}
if(color.blue > maxBlue){
maxBlue = color.blue;
}
}];
return [LUTColor colorWithRed:maxRed green:maxGreen blue:maxBlue];
}
- (LUTColor *)minimumOutputColor{
LUTColor *start = [self colorAtR:0 g:0 b:0];
__block double minRed = start.red;
__block double minGreen = start.green;
__block double minBlue = start.blue;
[self LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtR:r g:g b:b];
if(color.red < minRed){
minRed = color.red;
}
if(color.green < minGreen){
minGreen = color.green;
}
if(color.blue < minBlue){
minBlue = color.blue;
}
}];
return [LUTColor colorWithRed:minRed green:minGreen blue:minBlue];
}
- (double)maximumOutputValue{
__block double maxValue = [self colorAtR:0 g:0 b:0].red;
[self LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtR:r g:g b:b];
if(color.red > maxValue){
maxValue = color.red;
}
if(color.green > maxValue){
maxValue = color.green;
}
if(color.blue > maxValue){
maxValue = color.blue;
}
}];
return maxValue;
}
- (double)minimumOutputValue{
__block double minValue = [self colorAtR:0 g:0 b:0].red;
[self LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtR:r g:g b:b];
if(color.red < minValue){
minValue = color.red;
}
if(color.green < minValue){
minValue = color.green;
}
if(color.blue < minValue){
minValue = color.blue;
}
}];
return minValue;
}
//000
- (bool)equalsIdentityLUT{
return [self equalsLUT:[[self class] LUTIdentityOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]]];
}
- (bool)equalsLUT:(LUT *)comparisonLUT{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (bool)equalsLUTEssence:(LUT *)comparisonLUT
compareType:(bool)compareType
compareSize:(bool)compareSize
compareInputBounds:(bool)compareInputBounds{
if (compareType && ((isLUT1D(self) && !isLUT1D(comparisonLUT)) || (isLUT3D(self) && !isLUT3D(comparisonLUT)))) {
return NO;
}
if (compareSize && self.size != comparisonLUT.size) {
return NO;
}
if (compareInputBounds && (self.inputLowerBound != comparisonLUT.inputLowerBound || self.inputUpperBound != comparisonLUT.inputUpperBound)) {
return NO;
}
return YES;
}
// http://en.wikipedia.org/wiki/Symmetric_mean_absolute_percentage_error
- (LUTColor *)symetricalMeanAbsolutePercentageError:(LUT *)comparisonLUT{
if (![self equalsLUTEssence:comparisonLUT
compareType:YES
compareSize:YES
compareInputBounds:YES]) {
return [LUTColor colorWithRed:1000000 green:1000000 blue:1000000];
}
double redAbsoluteError = 0.0;
double greenAbsoluteError = 0.0;
double blueAbsoluteError = 0.0;
double redAdd = 0.0;
double greenAdd = 0.0;
double blueAdd = 0.0;
double numPoints;
if (isLUT3D(self)) {
numPoints = self.size*self.size*self.size;
for (int r = 0; r < self.size; r++) {
for (int g = 0; g < self.size; g++) {
for (int b = 0; b < self.size; b++) {
LUTColor *selfColor = [self colorAtR:r g:g b:b];
LUTColor *comparisonColor = [comparisonLUT colorAtR:r g:g b:b];
redAdd = fabs(selfColor.red - comparisonColor.red)/(selfColor.red + comparisonColor.red);
greenAdd = fabs(selfColor.green - comparisonColor.green)/(selfColor.green + comparisonColor.green);
blueAdd = fabs(selfColor.blue - comparisonColor.blue)/(selfColor.blue + comparisonColor.blue);
redAbsoluteError += !isfinite(redAdd) ? 0 : redAdd;
greenAbsoluteError += !isfinite(greenAdd) ? 0 : greenAdd;
blueAbsoluteError += !isfinite(blueAdd) ? 0 : blueAdd;
}
}
}
}
else{
//LUT1D
numPoints = self.size*3;
for (int x = 0; x < self.size; x++) {
LUTColor *selfColor = [self colorAtR:x g:x b:x];
LUTColor *comparisonColor = [comparisonLUT colorAtR:x g:x b:x];
redAdd = fabs(selfColor.red - comparisonColor.red)/(selfColor.red + comparisonColor.red);
greenAdd = fabs(selfColor.green - comparisonColor.green)/(selfColor.green + comparisonColor.green);
blueAdd = fabs(selfColor.blue - comparisonColor.blue)/(selfColor.blue + comparisonColor.blue);
redAbsoluteError += !isfinite(redAdd) ? 0 : redAdd;
greenAbsoluteError += !isfinite(greenAdd) ? 0 : greenAdd;
blueAbsoluteError += !isfinite(blueAdd) ? 0 : blueAdd;
}
}
return [LUTColor colorWithRed:redAbsoluteError/numPoints green:greenAbsoluteError/numPoints blue:blueAbsoluteError/numPoints];
}
- (LUTColor *)averageAbsoluteError:(LUT *)comparisonLUT{
if (![self equalsLUTEssence:comparisonLUT
compareType:YES
compareSize:YES
compareInputBounds:YES]) {
return [LUTColor colorWithRed:1000000 green:1000000 blue:1000000];
}
double redError = 0.0;
double greenError = 0.0;
double blueError = 0.0;
double numPoints;
if (isLUT3D(self)) {
numPoints = self.size*self.size*self.size;
for (int r = 0; r < self.size; r++) {
for (int g = 0; g < self.size; g++) {
for (int b = 0; b < self.size; b++) {
LUTColor *selfColor = [self colorAtR:r g:g b:b];
LUTColor *comparisonColor = [comparisonLUT colorAtR:r g:g b:b];
LUTColor *diff = [selfColor colorBySubtractingColor:comparisonColor];
LUTColor *absDiff = [LUTColor colorWithRed:fabs(diff.red) green:fabs(diff.green) blue:fabs(diff.blue)];
redError += !isfinite(absDiff.red) ? 0 : absDiff.red;
greenError += !isfinite(absDiff.green) ? 0 : absDiff.green;
blueError += !isfinite(absDiff.blue) ? 0 : absDiff.blue;
}
}
}
}
else{
numPoints = self.size*3;
for (int x = 0; x < self.size; x++) {
LUTColor *selfColor = [self colorAtR:x g:x b:x];
LUTColor *comparisonColor = [comparisonLUT colorAtR:x g:x b:x];
LUTColor *diff = [selfColor colorBySubtractingColor:comparisonColor];
LUTColor *absDiff = [LUTColor colorWithRed:fabs(diff.red) green:fabs(diff.green) blue:fabs(diff.blue)];
redError += !isfinite(absDiff.red) ? 0 : absDiff.red;
greenError += !isfinite(absDiff.green) ? 0 : absDiff.green;
blueError += !isfinite(absDiff.blue) ? 0 : absDiff.blue;
}
}
return [LUTColor colorWithRed:redError/numPoints green:greenError/numPoints blue:blueError/numPoints];
}
- (LUTColor *)maximumAbsoluteError:(LUT *)comparisonLUT{
if (![self equalsLUTEssence:comparisonLUT
compareType:YES
compareSize:YES
compareInputBounds:YES]) {
return [LUTColor colorWithRed:1000000 green:1000000 blue:1000000];
}
double redMaxError = 0.0;
double greenMaxError = 0.0;
double blueMaxError = 0.0;
if (isLUT3D(self)) {
for (int r = 0; r < self.size; r++) {
for (int g = 0; g < self.size; g++) {
for (int b = 0; b < self.size; b++) {
LUTColor *selfColor = [self colorAtR:r g:g b:b];
LUTColor *comparisonColor = [comparisonLUT colorAtR:r g:g b:b];
LUTColor *diff = [selfColor colorBySubtractingColor:comparisonColor];
if (fabs(diff.red) > redMaxError) {
redMaxError = fabs(diff.red);
}
if (fabs(diff.green) > greenMaxError) {
greenMaxError = fabs(diff.green);
}
if (fabs(diff.blue) > blueMaxError) {
blueMaxError = fabs(diff.blue);
}
}
}
}
}
else{
for (int x = 0; x < self.size; x++) {
LUTColor *selfColor = [self colorAtR:x g:x b:x];
LUTColor *comparisonColor = [comparisonLUT colorAtR:x g:x b:x];
LUTColor *diff = [selfColor colorBySubtractingColor:comparisonColor];
if (fabs(diff.red) > redMaxError) {
redMaxError = fabs(diff.red);
}
if (fabs(diff.green) > greenMaxError) {
greenMaxError = fabs(diff.green);
}
if (fabs(diff.blue) > blueMaxError) {
blueMaxError = fabs(diff.blue);
}
}
}
return [LUTColor colorWithRed:redMaxError green:greenMaxError blue:blueMaxError];
}
- (id)copyWithZone:(NSZone *)zone {
LUT *copiedLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[copiedLUT setMetadata:[[self metadata] mutableCopyWithZone:zone]];
[copiedLUT setUserInfo:[self.userInfo mutableCopyWithZone:zone]];
copiedLUT.descriptionText = [[self descriptionText] mutableCopyWithZone:zone];
[copiedLUT setTitle:[[self title] mutableCopyWithZone:zone]];
[copiedLUT setPassthroughFileOptions:[[self passthroughFileOptions] mutableCopyWithZone:zone]];
return copiedLUT;
}
- (NSData *)lutDataRGBAf{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (CIFilter *)coreImageFilterWithColorSpace:(CGColorSpaceRef)colorSpace {
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (CIFilter *)coreImageFilterWithCurrentColorSpace {
CIFilter *filter;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
filter = [self coreImageFilterWithColorSpace:colorspace];
#elif TARGET_OS_MAC
//good for render, not good for viewing
filter = [self coreImageFilterWithColorSpace:colorspace];
//good for viewing, not good for render
//return [self coreImageFilterWithColorSpace:[[[NSScreen mainScreen] colorSpace] CGColorSpace]];
#endif
CGColorSpaceRelease(colorspace);
return filter;
}
- (CIImage *)processCIImage:(CIImage *)image {
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR)
CIFilter *filter = [self coreImageFilterWithCurrentColorSpace];
#elif TARGET_OS_MAC
CIFilter *filter = [self coreImageFilterWithColorSpace:image.colorSpace];
#endif
[filter setValue:image forKey:@"inputImage"];
return [filter valueForKey:@"outputImage"];
}
#if (TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR) && defined(COCOAPODS_POD_AVAILABLE_GPUImage)
- (GPUImageCocoaLUTFilter *)GPUImageCocoaLUTFilter {
return [[GPUImageCocoaLUTFilter alloc] initWithLUT:self];
}
#endif
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
- (UIImage *)processUIImage:(UIImage *)image withColorSpace:(CGColorSpaceRef)colorSpace {
return [[UIImage alloc] initWithCIImage:[self processCIImage:image.CIImage]];
}
#elif TARGET_OS_MAC
- (NSImage *)processNSImage:(NSImage *)image
preserveEmbeddedColorSpace:(BOOL)preserveEmbeddedColorSpace
renderPath:(LUTImageRenderPath)renderPath {
if (![image.representations.firstObject isKindOfClass:[NSBitmapImageRep class]]) {
return nil;
}
LUT *usedLUT;
#if defined(COCOAPODS_POD_AVAILABLE_VVSceneLinearImageRep)
if ([image isSceneLinear]) {
if (!preserveEmbeddedColorSpace) {
image = [[image imageInDeviceRGBColorSpace] imageByNormalizingSceneLinearData];
usedLUT = [self LUTByChangingInputLowerBound:[image minimumSceneValue] inputUpperBound:[image maximumSceneValue]];
}
else{
image = [[image imageInGenericHDRColorSpace] imageByDenormalizingSceneLinearData];
usedLUT = [self LUTByChangingInputLowerBound:0 inputUpperBound:1];
}
}
else {
usedLUT = [self LUTByChangingInputLowerBound:0 inputUpperBound:1];
}
#else
usedLUT = [self LUTByChangingInputLowerBound:0 inputUpperBound:1];
#endif
if (!preserveEmbeddedColorSpace) {
image = [image cocoaLUT_imageWithDeviceRGBColorspace];
}
if (renderPath == LUTImageRenderPathCoreImage || renderPath == LUTImageRenderPathCoreImageSoftware) {
CIImage *inputCIImage = [[CIImage alloc] initWithBitmapImageRep:(NSBitmapImageRep *)image.representations.firstObject];
CIImage *outputCIImage = [usedLUT processCIImage:inputCIImage];
return LUTNSImageFromCIImage(outputCIImage, renderPath == LUTImageRenderPathCoreImageSoftware);
}
else if (renderPath == LUTImageRenderPathDirect) {
return [usedLUT processNSImageDirectly:image];
}
return nil;
}
- (NSImage *)processNSImageDirectly:(NSImage *)image {
NSBitmapImageRep *inImageRep = [image representations][0];
int nchannels = 3;
int bps = 16;
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:image.size.width
pixelsHigh:image.size.height
bitsPerSample:bps
samplesPerPixel:nchannels
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:(image.size.width * (bps * nchannels)) / 8
bitsPerPixel:bps * nchannels];
for (int x = 0; x < image.size.width; x++) {
for (int y = 0; y < image.size.height; y++) {
LUTColor *lutColor = [LUTColor colorWithSystemColor:[inImageRep colorAtX:x y:y]];
LUTColor *transformedColor =[self colorAtColor:lutColor];
[imageRep setColor:transformedColor.systemColor atX:x y:y];
}
}
NSImage* outImage = [[NSImage alloc] initWithSize:image.size];
[outImage addRepresentation:imageRep];
return outImage;
}
#endif
@end