forked from alejandrojapkin/CocoaLUT
/
LUTFormatterDiscreet1DLUT.m
170 lines (127 loc) · 5.62 KB
/
LUTFormatterDiscreet1DLUT.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
//
// LUTFormatterDiscreet1DLUT.m
// Pods
//
// Created by Greg Cotten on 3/5/14.
//
//
#import "LUTFormatterDiscreet1DLUT.h"
@implementation LUTFormatterDiscreet1DLUT
+ (void)load{
[super load];
}
+ (LUT *)LUTFromLines:(NSArray *)lines {
NSString *description;
NSMutableDictionary *metadata;
NSMutableDictionary *passthroughFileOptions = [NSMutableDictionary dictionary];
NSMutableArray *redCurve = [NSMutableArray array];
NSMutableArray *greenCurve = [NSMutableArray array];
NSMutableArray *blueCurve = [NSMutableArray array];
NSMutableArray *trimmedLines = [NSMutableArray array];
int integerMaxOutput = -1;
int lutSize = -1;
NSUInteger lutLinesStartIndex = findFirstLUTLineInLines(lines, @"", 1, 0);
if(lutLinesStartIndex == -1){
@throw [NSException exceptionWithName:@"LUTParserError" reason:@"Couldn't find start of LUT data lines." userInfo:nil];
}
NSArray *headerLines = [lines subarrayWithRange:NSMakeRange(0, lutLinesStartIndex)];
NSDictionary *metadataAndDescription = [LUTMetadataFormatter metadataAndDescriptionFromLines:headerLines];
metadata = metadataAndDescription[@"metadata"];
description = metadataAndDescription[@"description"];
//trim for lut values only and grab the max code value
for (NSString *line in lines) {
NSString *trimmedLine = [line stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if(trimmedLine.length > 0 && [trimmedLine rangeOfString:@"#"].location == NSNotFound && [trimmedLine rangeOfString:@"LUT"].location == NSNotFound){
[trimmedLines addObject:trimmedLine];
}
if([trimmedLine rangeOfString:@"Scale"].location != NSNotFound){
integerMaxOutput = [[trimmedLine componentsSeparatedByString:@":"][1] intValue];
passthroughFileOptions[@"integerMaxOutput"] = @(integerMaxOutput);
}
if([trimmedLine rangeOfString:@"LUT"].location != NSNotFound){
lutSize = [[trimmedLine componentsSeparatedByString:@" "][2] intValue];
}
}
if(trimmedLines.count < lutSize*3){
@throw [NSException exceptionWithName:@"LUTParserError" reason:@"Incomplete data lines." userInfo:nil];
}
for(NSString *checkLine in trimmedLines){
if(stringIsValidNumber(checkLine) == NO){
@throw [NSException exceptionWithName:@"LUTParserError" reason:[NSString stringWithFormat:@"NaN detected in LUT"] userInfo:nil];
}
}
//get red values
for (int i = 0; i < lutSize; i++) {
[redCurve addObject:@(nsremapint01([trimmedLines[i] integerValue], integerMaxOutput))];
}
//get green values
for (int i = lutSize; i < 2*lutSize; i++) {
[greenCurve addObject:@(nsremapint01([trimmedLines[i] integerValue], integerMaxOutput))];
}
//get blue values
for (int i = 2*lutSize; i < 3*lutSize; i++) {
[blueCurve addObject:@(nsremapint01([trimmedLines[i] integerValue], integerMaxOutput))];
}
LUT1D *lut = [LUT1D LUT1DWithRedCurve:redCurve greenCurve:greenCurve blueCurve:blueCurve lowerBound:0.0 upperBound:1.0];
[lut setMetadata:metadata];
lut.descriptionText = description;
[lut setPassthroughFileOptions:@{[self utiString]: passthroughFileOptions}];
return lut;
}
+ (NSString *)stringFromLUT:(LUT *)lut withOptions:(NSDictionary *)options {
if(![self optionsAreValid:options]){
@throw [NSException exceptionWithName:@"Discreet1DLUTWriteError" reason:[NSString stringWithFormat:@"Options don't pass the spec: %@", options] userInfo:nil];
}
else{
options = options[[self utiString]];
}
NSMutableString *string = [NSMutableString stringWithString:@""];
NSUInteger integerMaxOutput;
integerMaxOutput = [options[@"integerMaxOutput"] integerValue];
[string appendString:[NSString stringWithFormat:@"#\n# Discreet LUT file\n#\tChannels: 3\n# Input Samples: %d\n# Ouput Scale: %d\n#\n# Exported from CocoaLUT\n#\nLUT: 3 %d\n", (int)[lut size], (int)integerMaxOutput, (int)[lut size]]];
LUT1D *lut1D = LUTAsLUT1D(lut, [lut size]);
//write red
for (int i = 0; i < [lut size]; i++) {
[string appendString:[NSString stringWithFormat:@"%d\n", (int)([lut1D valueAtR:i]*(double)integerMaxOutput) ]];
}
//write green
for (int i = 0; i < [lut size]; i++) {
[string appendString:[NSString stringWithFormat:@"%d\n", (int)([lut1D valueAtG:i]*(double)integerMaxOutput) ]];
}
//write blue
for (int i = 0; i < [lut size]; i++) {
[string appendString:[NSString stringWithFormat:@"%d\n", (int)([lut1D valueAtB:i]*(double)integerMaxOutput) ]];
}
return string;
}
+ (LUTFormatterOutputType)outputType{
return LUTFormatterOutputType1D;
}
+ (NSArray *)allOptions{
NSDictionary *discreetOptions =
@{@"fileTypeVariant":@"Discreet",
@"integerMaxOutput": M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"12-bit": @(pow(2, 12) - 1)},
@{@"16-bit": @(pow(2, 16) - 1)}])};
return @[discreetOptions];
}
+ (NSDictionary *)defaultOptions{
NSDictionary *dictionary = @{@"fileTypeVariant": @"Discreet",
@"integerMaxOutput": @((int)(pow(2, 12) - 1))};
return @{[self utiString]:dictionary};
}
+ (NSString *)utiString{
return @"com.discreet.lut";
}
+ (NSArray *)fileExtensions{
return @[@"lut"];
}
+ (NSString *)formatterName{
return @"Discreet 1D LUT";
}
+ (BOOL)canRead{
return YES;
}
+ (BOOL)canWrite{
return YES;
}
@end