forked from alejandrojapkin/CocoaLUT
/
LUTFormatterILUT.m
97 lines (67 loc) · 2.49 KB
/
LUTFormatterILUT.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
//
// LUTFormatterILUT.m
// Pods
//
// Created by Greg Cotten on 4/9/14.
//
//
#import "LUTFormatterILUT.h"
@implementation LUTFormatterILUT
+ (void)load{
[super load];
}
+ (LUT *)LUTFromLines:(NSArray *)lines {
NSMutableArray *redCurve = [NSMutableArray array];
NSMutableArray *greenCurve = [NSMutableArray array];
NSMutableArray *blueCurve = [NSMutableArray array];
NSMutableArray *trimmedLines = [NSMutableArray array];
for (NSString *line in lines) {
NSString *trimmedLine = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if(trimmedLine.length > 0){
[trimmedLines addObject:trimmedLine];
}
}
NSUInteger maxCodeValue = pow(2,14) - 1;
for (NSString *line in trimmedLines) {
NSArray *splitLine = [line componentsSeparatedByString:@","];
for(NSString *checkLine in splitLine){
if(stringIsValidNumber(checkLine) == NO){
@throw [NSException exceptionWithName:@"LUTParserError" reason:[NSString stringWithFormat:@"NaN detected in LUT"] userInfo:nil];
}
}
[redCurve addObject:@(nsremapint01([splitLine[0] integerValue], maxCodeValue))];
[greenCurve addObject:@(nsremapint01([splitLine[1] integerValue], maxCodeValue))];
[blueCurve addObject:@(nsremapint01([splitLine[2] integerValue], maxCodeValue))];
}
return [LUT1D LUT1DWithRedCurve:redCurve greenCurve:greenCurve blueCurve:blueCurve lowerBound:0 upperBound:1];
}
+ (NSString *)stringFromLUT:(LUT *)lut withOptions:(NSDictionary *)options {
NSMutableString *string = [NSMutableString stringWithString:@""];
LUT1D *lut1D = LUTAsLUT1D(lut, pow(2,14));
for (int i = 0; i < pow(2,14); i++){
int red = (int)(clamp01([lut1D valueAtR:i])*(double)pow(2,14));
int green = (int)(clamp01([lut1D valueAtG:i])*(double)pow(2,14));
int blue = (int)(clamp01([lut1D valueAtB:i])*(double)pow(2,14));
[string appendString:[NSString stringWithFormat:@"%d,%d,%d,%d\n", red, green, blue, 0]];
}
return string;
}
+ (LUTFormatterOutputType)outputType{
return LUTFormatterOutputType1D;
}
+ (NSString *)utiString{
return @"com.blackmagicdesign.ilut";
}
+ (NSArray *)fileExtensions{
return @[@"ilut"];
}
+ (NSString *)formatterName{
return @"Blackmagic Design 1D LUT";
}
+ (BOOL)canRead{
return YES;
}
+ (BOOL)canWrite{
return YES;
}
@end