forked from alejandrojapkin/CocoaLUT
/
LUTFormatterCube.m
458 lines (361 loc) · 19.2 KB
/
LUTFormatterCube.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
//
// LUTFormatterCube.m
// DropLUT
//
// Created by Wil Gieseler on 12/15/13.
// Copyright (c) 2013 Wil Gieseler. All rights reserved.
//
#import "LUTFormatterCube.h"
#import <RegExCategories/RegExCategories.h>
@implementation LUTFormatterCube
+ (void)load{
[super load];
}
+ (LUT *)LUTFromLines:(NSArray *)lines {
NSMutableString __block *title = [NSMutableString stringWithString:@""];
NSString *description;
NSMutableDictionary *metadata;
NSMutableDictionary *passthroughFileOptions = [NSMutableDictionary dictionary];
NSMutableDictionary *data = [NSMutableDictionary dictionary];
BOOL __block isLUT3D = NO;
BOOL __block isLUT1D = NO;
NSUInteger cubeLinesStartIndex = findFirstLUTLineInLines(lines, @" ", 3, 0);
if(cubeLinesStartIndex == -1){
@throw [NSException exceptionWithName:@"LUTParserError" reason:@"Couldn't find start of LUT data lines." userInfo:nil];
}
NSArray *headerLines = [lines subarrayWithRange:NSMakeRange(0, cubeLinesStartIndex)];
NSDictionary *metadataAndDescription = [LUTMetadataFormatter metadataAndDescriptionFromLines:headerLines];
metadata = metadataAndDescription[@"metadata"];
description = metadataAndDescription[@"description"];
for(NSString *untrimmedLine in headerLines){
NSString *titleMatch;
NSString *line = [untrimmedLine stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if([line rangeOfString:@"#"].location == NSNotFound){
if ([line rangeOfString:@"LUT_3D_SIZE"].location != NSNotFound) {
isLUT3D = YES;
if (data[@"cubeSize"] != nil){
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Size parameter already once."
userInfo:nil];
}
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(splitLine.count == 2 && stringIsValidNumber(splitLine[1])){
data[@"cubeSize"] = @([splitLine[1] integerValue]);
}
else{
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Size parameter is invalid."
userInfo:nil];
}
}
else if ([line rangeOfString:@"LUT_1D_SIZE"].location != NSNotFound) {
isLUT1D = YES;
if (data[@"cubeSize"] != nil){
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Size parameter already once."
userInfo:nil];
}
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(splitLine.count == 2 && stringIsValidNumber(splitLine[1])){
data[@"cubeSize"] = @([splitLine[1] integerValue]);
}
else{
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Size parameter is invalid."
userInfo:nil];
}
}
else if ([line rangeOfString:@"LUT_3D_INPUT_RANGE"].location != NSNotFound) {
if (data[@"inputLowerBound"] != nil || data[@"inputUpperBound"] != nil){
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Input Bounds already defined."
userInfo:nil];
}
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(splitLine.count == 3 && stringIsValidNumber(splitLine[1]) && stringIsValidNumber(splitLine[2])){
data[@"inputLowerBound"] = @([splitLine[1] doubleValue]);
data[@"inputUpperBound"] = @([splitLine[2] doubleValue]);
if ([splitLine[1] length] >= 14) {
passthroughFileOptions[@"fileTypeVariant"] = @"High Precision";
}
else{
passthroughFileOptions[@"fileTypeVariant"] = @"Resolve";
}
}
else{
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"INPUT_RANGE invalid."
userInfo:nil];
}
}
else if ([line rangeOfString:@"LUT_1D_INPUT_RANGE"].location != NSNotFound) {
if (data[@"inputLowerBound"] != nil || data[@"inputUpperBound"] != nil){
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Input Bounds already defined."
userInfo:nil];
}
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(splitLine.count == 3 && stringIsValidNumber(splitLine[1]) && stringIsValidNumber(splitLine[2])){
data[@"inputLowerBound"] = @([splitLine[1] doubleValue]);
data[@"inputUpperBound"] = @([splitLine[2] doubleValue]);
if ([splitLine[1] length] >= 14) {
passthroughFileOptions[@"fileTypeVariant"] = @"High Precision";
}
else{
passthroughFileOptions[@"fileTypeVariant"] = @"Resolve";
}
}
else{
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"INPUT_RANGE invalid."
userInfo:nil];
}
}
else if ([line rangeOfString:@"DOMAIN_MIN"].location != NSNotFound) {
passthroughFileOptions[@"fileTypeVariant"] = @"Iridas/Adobe";
if (data[@"inputLowerBound"] != nil){
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Input Bound already defined."
userInfo:nil];
}
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(splitLine.count == 4 && [splitLine[1] doubleValue] == [splitLine[2] doubleValue] && [splitLine[1] doubleValue] == [splitLine[3] doubleValue] && stringIsValidNumber(splitLine[1])){
data[@"inputLowerBound"] = @([splitLine[1] doubleValue]);
}
else{
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"DOMAIN_MIN invalid."
userInfo:nil];
}
}
else if ([line rangeOfString:@"DOMAIN_MAX"].location != NSNotFound) {
passthroughFileOptions[@"fileTypeVariant"] = @"Iridas/Adobe";
if (data[@"inputUpperBound"] != nil){
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"Input Bound already defined."
userInfo:nil];
}
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(splitLine.count == 4 && [splitLine[1] doubleValue] == [splitLine[2] doubleValue] && [splitLine[1] doubleValue] == [splitLine[3] doubleValue] && stringIsValidNumber(splitLine[1])){
data[@"inputUpperBound"] = @([splitLine[1] doubleValue]);
}
else{
@throw [NSException exceptionWithName:@"CubeLUTParseError"
reason:@"DOMAIN_MAX invalid."
userInfo:nil];
}
}
else if ((titleMatch = [line firstMatch:RX(@"(?<=TITLE \")[^\"]*(?=\")")])) {
[title appendString:titleMatch];
}
}
}
NSUInteger cubeSize;
if (data[@"cubeSize"] == nil) {
NSException *exception = [NSException exceptionWithName:@"CubeLUTParseError" reason:@"Couldn't find LUT size in file" userInfo:nil];
@throw exception;
}
else{
cubeSize = [data[@"cubeSize"] integerValue];
}
if ((isLUT1D && isLUT3D) || (!isLUT1D && !isLUT3D)){
@throw [NSException exceptionWithName:@"CubeLUTParseError" reason:@"Couldn't figure out if 3D or 1D LUT" userInfo:nil];
}
double inputLowerBound;
double inputUpperBound;
if(data[@"inputLowerBound"] == nil && data[@"inputUpperBound"] == nil){
passthroughFileOptions[@"fileTypeVariant"] = @"Nuke";
inputLowerBound = 0;
inputUpperBound = 1;
}
else{
inputLowerBound = [data[@"inputLowerBound"] doubleValue];
inputUpperBound = [data[@"inputUpperBound"] doubleValue];
}
LUT *lut;
if(isLUT3D){
lut = [LUT3D LUTOfSize:cubeSize inputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
NSUInteger currentCubeIndex = 0;
for (NSString *line in [lines subarrayWithRange:NSMakeRange(cubeLinesStartIndex, lines.count - cubeLinesStartIndex)]) {
if (line.length > 0 && [line rangeOfString:@"#"].location == NSNotFound) {
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
splitLine = arrayWithEmptyElementsRemoved(splitLine);
if (splitLine.count == 3) {
for(NSString *checkLine in splitLine){
if(stringIsValidNumber(checkLine) == NO){
@throw [NSException exceptionWithName:@"CubeLUTParseError" reason:[NSString stringWithFormat:@"NaN detected at line %i", (int)currentCubeIndex+(int)cubeLinesStartIndex] userInfo:nil];
}
}
// Valid cube line
LUTColorValue redValue = ((NSString *)splitLine[0]).doubleValue;
LUTColorValue greenValue = ((NSString *)splitLine[1]).doubleValue;
LUTColorValue blueValue = ((NSString *)splitLine[2]).doubleValue;
LUTColor *color = [LUTColor colorWithRed:redValue green:greenValue blue:blueValue];
NSUInteger redIndex = currentCubeIndex % cubeSize;
NSUInteger greenIndex = ( (currentCubeIndex % (cubeSize * cubeSize)) / (cubeSize) );
NSUInteger blueIndex = currentCubeIndex / (cubeSize * cubeSize);
[lut setColor:color r:redIndex g:greenIndex b:blueIndex];
currentCubeIndex++;
}
}
}
if(currentCubeIndex < cubeSize*cubeSize*cubeSize){
@throw [NSException exceptionWithName:@"CubeLUTParseError" reason:@"Incomplete data lines" userInfo:nil];
}
}
else{
//1D LUT
lut = [LUT1D LUTOfSize:cubeSize inputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
NSUInteger currentLineIndex = 0;
for (NSString *line in [lines subarrayWithRange:NSMakeRange(cubeLinesStartIndex, lines.count - cubeLinesStartIndex)]) {
if (line.length > 0 && [line rangeOfString:@"#"].location == NSNotFound) {
NSArray *splitLine = [line componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (splitLine.count == 3) {
for(NSString *checkLine in splitLine){
if(stringIsValidNumber(checkLine) == NO){
@throw [NSException exceptionWithName:@"CubeLUTParseError" reason:[NSString stringWithFormat:@"NaN detected at line %i", (int)currentLineIndex+(int)cubeLinesStartIndex] userInfo:nil];
}
}
// Valid cube line
LUTColorValue redValue = ((NSString *)splitLine[0]).doubleValue;
LUTColorValue greenValue = ((NSString *)splitLine[1]).doubleValue;
LUTColorValue blueValue = ((NSString *)splitLine[2]).doubleValue;
LUTColor *color = [LUTColor colorWithRed:redValue green:greenValue blue:blueValue];
[lut setColor:color r:currentLineIndex g:currentLineIndex b:currentLineIndex];
currentLineIndex++;
}
}
}
if(currentLineIndex < cubeSize){
@throw [NSException exceptionWithName:@"CubeLUTParseError" reason:@"Incomplete data lines" userInfo:nil];
}
}
[lut setTitle:title];
lut.descriptionText = description;
[lut setMetadata:metadata];
lut.passthroughFileOptions = @{[self formatterID]:passthroughFileOptions};
return lut;
}
+ (NSString *)stringFromLUT:(LUT *)lut withOptions:(NSDictionary *)options{
if(![self optionsAreValid:options]){
@throw [NSException exceptionWithName:@"CubeLUTWriteError" reason:[NSString stringWithFormat:@"Options don't pass the spec: %@", options] userInfo:nil];
}
else{
options = options[[self formatterID]];
}
NSMutableString *string = [NSMutableString stringWithString:@""];
NSUInteger lutSize = [lut size];
if (lut.title && lut.title.length > 0) {
[string appendString:[NSString stringWithFormat:@"TITLE \"%@\"\n", lut.title]];
}
//metadata and description write
[string appendString: [LUTMetadataFormatter stringFromMetadata:lut.metadata description:lut.descriptionText]];
[string appendString:@"\n"];
BOOL highPrecision = [options[@"fileTypeVariant"] isEqualToString:@"High Precision"];
if(isLUT1D(lut)){
//maybe implement writing a CUBE as 1D here?
[string appendString:[NSString stringWithFormat:@"LUT_1D_SIZE %i\n", (int)lutSize]];
if (highPrecision) {
[string appendString:[NSString stringWithFormat:@"LUT_1D_INPUT_RANGE %.12f %.12f\n", [lut inputLowerBound], [lut inputUpperBound]]];
}
else if ([options[@"fileTypeVariant"] isEqualToString:@"Resolve"]) {
[string appendString:[NSString stringWithFormat:@"LUT_1D_INPUT_RANGE %.6f %.6f\n", [lut inputLowerBound], [lut inputUpperBound]]];
}
else if ([options[@"fileTypeVariant"] isEqualToString:@"Iridas/Adobe"]) {
[string appendString:[NSString stringWithFormat:@"DOMAIN_MIN %f %f %f\n", [lut inputLowerBound], [lut inputLowerBound], [lut inputLowerBound]]];
[string appendString:[NSString stringWithFormat:@"DOMAIN_MAX %f %f %f\n", [lut inputUpperBound], [lut inputUpperBound], [lut inputUpperBound]]];
}
[string appendString:@"\n"];
for (int i = 0; i < lutSize; i++){
LUTColor *color = [lut colorAtR:i g:i b:i];
if (highPrecision){
[string appendString:[NSString stringWithFormat:@"%.12f %.12f %.12f", color.red, color.green, color.blue]];
}
else{
[string appendString:[NSString stringWithFormat:@"%.10f %.10f %.10f", color.red, color.green, color.blue]];
}
if(i != lutSize - 1) {
[string appendString:@"\n"];
}
}
}
else if(isLUT3D(lut)){
[string appendString:[NSString stringWithFormat:@"LUT_3D_SIZE %i\n", (int)lutSize]];
if (highPrecision) {
[string appendString:[NSString stringWithFormat:@"LUT_3D_INPUT_RANGE %.12f %.12f\n", [lut inputLowerBound], [lut inputUpperBound]]];
}
else if ([options[@"fileTypeVariant"] isEqualToString:@"Resolve"]) {
[string appendString:[NSString stringWithFormat:@"LUT_3D_INPUT_RANGE %.6f %.6f\n", [lut inputLowerBound], [lut inputUpperBound]]];
}
else if ([options[@"fileTypeVariant"] isEqualToString:@"Iridas/Adobe"]) {
[string appendString:[NSString stringWithFormat:@"DOMAIN_MIN %.6f %.6f %.6f\n", [lut inputLowerBound], [lut inputLowerBound], [lut inputLowerBound]]];
[string appendString:[NSString stringWithFormat:@"DOMAIN_MAX %.6f %.6f %.6f\n", [lut inputUpperBound], [lut inputUpperBound], [lut inputUpperBound]]];
}
[string appendString:@"\n"];
NSUInteger arrayLength = lutSize * lutSize * lutSize;
for (int i = 0; i < arrayLength; i++) {
int redIndex = i % lutSize;
int greenIndex = ((i % (lutSize * lutSize)) / (lutSize) );
int blueIndex = i / (lutSize * lutSize);
LUTColor *color = [lut colorAtR:redIndex g:greenIndex b:blueIndex];
if (highPrecision) {
[string appendString:[NSString stringWithFormat:@"%.12f %.12f %.12f", color.red, color.green, color.blue]];
}
else{
[string appendString:[NSString stringWithFormat:@"%.6f %.6f %.6f", color.red, color.green, color.blue]];
}
if(i != arrayLength - 1) {
[string appendString:@"\n"];
}
}
}
//but not for now
return string;
}
+ (NSArray *)allOptions{
NSDictionary *nukeOptions = @{@"fileTypeVariant":@"Nuke"};
NSDictionary *resolveOptions = @{@"fileTypeVariant":@"Resolve"};
NSDictionary *iridasAdobeOptions = @{@"fileTypeVariant":@"Iridas/Adobe"};
NSDictionary *highPrecisionOptions = @{@"fileTypeVariant":@"High Precision"};
return @[resolveOptions, nukeOptions, iridasAdobeOptions, highPrecisionOptions];
}
+ (NSArray *)conformanceLUTActionsForLUT:(LUT *)lut options:(NSDictionary *)options{
NSMutableArray *array = [NSMutableArray arrayWithArray:[super conformanceLUTActionsForLUT:lut options:options]];
//options are validated by superclass, no need to do that here.
NSDictionary *exposedOptions = options[[self formatterID]];
if ([exposedOptions[@"fileTypeVariant"] isEqualToString:@"Nuke"]) {
if(lut.inputLowerBound != 0 || lut.inputUpperBound != 1){
[array addObject:[LUTAction actionWithLUTByChangingInputLowerBound:0 inputUpperBound:1]];
}
}
return array.count == 0 ? nil : array;
}
+ (NSDictionary *)defaultOptions{
NSDictionary *dictionary = @{@"fileTypeVariant": @"Resolve"};
return @{[self formatterID]:dictionary};
}
+ (LUTFormatterOutputType)outputType{
return LUTFormatterOutputTypeEither;
}
+ (NSString *)utiString{
return @"com.blackmagicdesign.cube";
}
+ (NSArray *)fileExtensions{
return @[@"cube"];
}
+ (NSString *)formatterID{
return @"cube";
}
+ (NSString *)formatterName{
return @"Cube LUT";
}
+ (BOOL)canRead{
return YES;
}
+ (BOOL)canWrite{
return YES;
}
+ (NSDictionary *)constantConstraints{
return @{@"outputBounds":@[[NSNull null], [NSNull null]]};
}
@end