forked from alejandrojapkin/CocoaLUT
/
LUTFormatter.m
348 lines (296 loc) · 12.4 KB
/
LUTFormatter.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
//
// LUTFormatter.m
// DropLUT
//
// Created by Wil Gieseler on 12/15/13.
// Copyright (c) 2013 Wil Gieseler. All rights reserved.
//
#import "LUTFormatter.h"
static NSMutableArray *allFormatters;
@implementation LUTFormatter
+ (void)load{
if (self == [LUTFormatter class]) {
allFormatters = [[NSMutableArray alloc] init];
}
else{
[allFormatters addObject:self];
}
}
+ (NSArray *)allFormattersFileExtensions{
NSMutableArray *array = [NSMutableArray array];
for(LUTFormatter *formatter in allFormatters){
for(NSString *fileExtension in [formatter.class fileExtensions]){
[array addObject:fileExtension];
}
}
return array;
}
+ (NSArray *)lut1DFormatterFileExtensions{
NSMutableArray *array = [NSMutableArray array];
for(LUTFormatter *formatter in allFormatters){
if ([formatter.class outputType] == LUTFormatterOutputType1D) {
for(NSString *fileExtension in [formatter.class fileExtensions]){
[array addObject:fileExtension];
}
}
}
return array;
}
+ (NSArray *)lut3DFormatterFileExtensions{
NSMutableArray *array = [NSMutableArray array];
for(LUTFormatter *formatter in allFormatters){
if ([formatter.class outputType] == LUTFormatterOutputType3D) {
for(NSString *fileExtension in [formatter.class fileExtensions]){
[array addObject:fileExtension];
}
}
}
return array;
}
+ (NSArray *)LUTFormattersForFileExtension:(NSString *)fileExtension{
NSMutableArray *formatters = [NSMutableArray array];
for(LUTFormatter *formatter in allFormatters){
if([[[formatter class] fileExtensions] containsObject:fileExtension.lowercaseString]){
[formatters addObject:formatter];
}
}
return [NSArray arrayWithArray:formatters];
}
+ (LUTFormatter *)LUTFormatterWithID:(NSString *)identifier{
for(LUTFormatter* formatter in allFormatters){
if([[[formatter class] formatterID] isEqualToString:identifier]){
return formatter;
}
}
return nil;
}
+ (LUTFormatter *)LUTFormatterValidForReadingURL:(NSURL *)fileURL{
NSArray *formatters = [self LUTFormattersForFileExtension:fileURL.pathExtension.lowercaseString];
for(LUTFormatter* formatter in formatters){
if([[formatter class] isValidReaderForURL:fileURL]){
return formatter;
}
}
return nil;
}
+ (NSArray *)LUTFormattersValidForWritingLUTType:(LUT *)lut{
NSMutableArray *array = [NSMutableArray array];
for(LUTFormatter* formatter in allFormatters){
if([[formatter class] isValidWriterForLUTType:lut]){
[array addObject:formatter];
}
}
return array;
}
+ (BOOL)isValidReaderForURL:(NSURL *)fileURL{
if ([self canRead] == NO) {
return NO;
}
if([fileURL checkResourceIsReachableAndReturnError:nil] == NO){
return NO;
}
if([[self fileExtensions] containsObject:fileURL.pathExtension.lowercaseString]){
return YES;
}
return NO;
}
+ (BOOL)isValidWriterForLUTType:(LUT *)lut{
if([self canWrite] == NO){
return NO;
}
if([self outputType] == LUTFormatterOutputTypeEither){
return YES;
}
else if(isLUT1D(lut) && [self outputType] == LUTFormatterOutputType1D){
return YES;
}
else if(isLUT3D(lut) && [self outputType] == LUTFormatterOutputType3D){
return YES;
}
return NO;
}
+ (BOOL)isDestructiveWithOptions:(NSDictionary *)options{
return NO;
}
+ (LUT *)LUTFromURL:(NSURL *)fileURL {
return [self LUTFromData:[NSData dataWithContentsOfURL:fileURL]];
}
+ (LUT *)LUTFromData:(NSData *)data {
return [self LUTFromString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
}
+ (LUT *)LUTFromString:(NSString *)string {
return [self LUTFromLines:[string componentsSeparatedByCharactersInSet:NSCharacterSet.newlineCharacterSet]];
}
+ (LUT *)LUTFromLines:(NSArray *)lines {
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (NSData *)dataFromLUT:(LUT *)lut withOptions:(NSDictionary *)options {
return [[self stringFromLUT:lut withOptions:options] dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
}
+ (NSString *)stringFromLUT:(LUT *)lut withOptions:(NSDictionary *)options {
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (LUTFormatterOutputType)outputType{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
//structure is NSArray(of the variants)->NSDictionaries(of the variants options)->Strings or NSDictionaries with the option name or dicts of names:value
+ (NSArray *)allOptions {
return nil;
}
+ (NSDictionary *)defaultOptions{
return @{[self formatterID]: @{}};
}
+ (BOOL)optionsAreValid:(NSDictionary *)options{
NSDictionary *defaultOptionsExposed = [self defaultOptions][[self formatterID]];
NSDictionary *optionsExposed = options[[self formatterID]];
if (optionsExposed == nil) {
return NO;
}
if (optionsExposed.count == 0 && defaultOptionsExposed.count == 0) {
return YES;
}
else if(optionsExposed.count != 0 && defaultOptionsExposed.count != 0){
NSArray *allOptions = [self allOptions];
NSDictionary *allOptionsOfSameFileTypeVariant;
for(NSDictionary *optionDict in allOptions){
if ([optionDict[@"fileTypeVariant"] isEqualToString:optionsExposed[@"fileTypeVariant"]]) {
allOptionsOfSameFileTypeVariant = optionDict;
}
}
if(allOptionsOfSameFileTypeVariant == nil){
return NO;
}
for(NSString *key in [optionsExposed allKeys]){
//NSLog(@"%@", allOptionsOfSameFileTypeVariant[key]);
if(allOptionsOfSameFileTypeVariant[key] == nil && [[(M13OrderedDictionary *)allOptionsOfSameFileTypeVariant[key] allObjects] indexOfObject:optionsExposed[key]] == NSNotFound){
return NO;
}
}
return YES;
}
return NO;
}
+ (NSString *)utiString{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (NSArray *)fileExtensions{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (NSString *)formatterName{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (NSString *)formatterID{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (NSString *)nameWithExtensions{
NSMutableString *extensionsString = [[NSMutableString alloc] initWithString:@"("];
NSArray *fileExtensions = [self fileExtensions];
for(int i = 0; i < fileExtensions.count; i++){
[extensionsString appendString:[@"." stringByAppendingString:fileExtensions[i]]];
if(i + 1 < fileExtensions.count){
[extensionsString appendString:@", "];
}
}
[extensionsString appendString:@")"];
return [NSString stringWithFormat:@"%@ %@", [self formatterName], extensionsString];
}
+ (LUTFormatterRole)formatterRole{
if([self canRead] == YES && [self canWrite] == YES){
return LUTFormatterRoleReadAndWrite;
}
else if([self canRead] == YES && [self canWrite] == NO){
return LUTFormatterRoleReadOnly;
}
else if([self canRead] == NO && [self canWrite] == YES){
return LUTFormatterRoleWriteOnly;
}
return LUTFormatterRoleNone;
}
+ (BOOL)canRead{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (BOOL)canWrite{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (NSDictionary *)constantConstraints{
return @{@"inputBounds":@[@0, @1],
@"outputBounds":@[[NSNull null], [NSNull null]]};
}
+ (NSArray *)conformanceLUTActionsForLUT:(LUT *)lut options:(NSDictionary *)options{
NSMutableArray *arrayOfActions = [NSMutableArray array];
if(![self optionsAreValid:options]){
@throw [NSException exceptionWithName:@"LUTActionsForLUTError" reason:[NSString stringWithFormat:@"Provided options don't pass the spec: %@", options] userInfo:nil];
}
NSDictionary *exposedOptions = options[[self formatterID]];
NSDictionary *formatterConstantConstraints = [self constantConstraints];
if(formatterConstantConstraints != nil){
if(formatterConstantConstraints[@"inputBounds"] != nil){
NSArray *array = formatterConstantConstraints[@"inputBounds"];
if(array[0] != [NSNull null] && array[1] != [NSNull null]){
if(lut.inputLowerBound != [array[0] doubleValue] || lut.inputUpperBound != [array[1] doubleValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByChangingInputLowerBound:[array[0] doubleValue] inputUpperBound:[array[1] doubleValue]]];
}
}
else if(array[0] == [NSNull null] && array[1] != [NSNull null]){
//strange setup, bro
if(lut.inputUpperBound != [array[1] doubleValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByChangingInputLowerBound:lut.inputLowerBound inputUpperBound:[array[1] doubleValue]]];
}
}
else if(array[0] != [NSNull null] && array[1] == [NSNull null]){
//strange setup, bro
if(lut.inputLowerBound != [array[0] doubleValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByChangingInputLowerBound:[array[0] doubleValue] inputUpperBound:lut.inputUpperBound]];
}
}
}
if(formatterConstantConstraints[@"outputBounds"] != nil){
NSArray *array = formatterConstantConstraints[@"outputBounds"];
if(array[0] != [NSNull null] && array[1] != [NSNull null]){
if(lut.minimumOutputValue < [array[0] doubleValue] || lut.maximumOutputValue > [array[1] doubleValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByClampingLowerBound:[array[0] doubleValue] upperBound:[array[1] doubleValue]]];
}
}
else if(array[0] == [NSNull null] && array[1] != [NSNull null]){
//strange setup, bro
if(lut.maximumOutputValue > [array[1] doubleValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByClampingLowerBound:lut.minimumOutputValue upperBound:[array[1] doubleValue]]];
}
}
else if(array[0] != [NSNull null] && array[1] == [NSNull null]){
//strange setup, bro
if(lut.minimumOutputValue < [array[0] doubleValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByClampingLowerBound:[array[0] doubleValue] upperBound:lut.maximumOutputValue]];
}
}
}
if(formatterConstantConstraints[@"lutSize"] != nil){
NSArray *array = formatterConstantConstraints[@"lutSize"];
if(array[1] != [NSNull null]){
if(lut.size > [array[1] integerValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByResizingToSize:[array[1] integerValue]]];
}
}
else if (array[0] != [NSNull null]) {
if(lut.size < [array[0] integerValue]){
[arrayOfActions addObject:[LUTAction actionWithLUTByResizingToSize:[array[0] integerValue]]];
}
}
}
}
if(exposedOptions.count != 0){
if(exposedOptions[@"lutSize"] != nil){
NSInteger resizeSize = [exposedOptions[@"lutSize"] integerValue];
if(resizeSize != lut.size){
[arrayOfActions addObject:[LUTAction actionWithLUTByResizingToSize:resizeSize]];
}
}
}
if(arrayOfActions.count == 0){
return nil;
}
else{
return arrayOfActions;
}
}
@end