forked from alejandrojapkin/CocoaLUT
/
LUTFormatterImageBased.m
193 lines (162 loc) · 6.67 KB
/
LUTFormatterImageBased.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
//
// LUTFormatterImageBased.m
// Pods
//
// Created by Greg Cotten on 6/23/14.
//
//
#import "LUTFormatterImageBased.h"
#import "M13OrderedDictionary.h"
@implementation LUTFormatterImageBased
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
+ (NSData *)dataFromLUT:(LUT *)lut withOptions:(NSDictionary *)options {
NSException *exception = [NSException exceptionWithName:@"Unsupported Platform"
reason:@"LUTFormatterCMSTestPattern doesn't currently support iOS." userInfo:nil];
@throw exception;
return nil;
}
+ (UIImage *)imageFromLUT:(LUT *)lut
bitdepth:(NSUInteger)bitdepth {
NSException *exception = [NSException exceptionWithName:@"Unsupported Platform"
reason:@"LUTFormatterCMSTestPattern doesn't currently support iOS." userInfo:nil];
@throw exception;
return nil;
}
+ (LUT *)LUTFromImage:(UIImage *)image {
NSException *exception = [NSException exceptionWithName:@"Unsupported Platform"
reason:@"LUTFormatterCMSTestPattern doesn't currently support iOS." userInfo:nil];
@throw exception;
return nil;
}
#elif TARGET_OS_MAC
+ (NSData *)dataFromLUT:(LUT *)lut withOptions:(NSDictionary *)options {
if(![self optionsAreValid:options]){
@throw [NSException exceptionWithName:@"ImageBasedWriteError" reason:[NSString stringWithFormat:@"Options don't pass the spec: %@", options] userInfo:nil];
}
NSDictionary *exposedOptions = options[[self utiString]];
//
// #if defined(COCOAPODS_POD_AVAILABLE_oiiococoa)
// if([exposedOptions[@"fileTypeVariant"] isEqualToString:@"DPX"]){
// NSString *tempFileName = [NSString stringWithFormat:@"%@_%@", [[NSProcessInfo processInfo] globallyUniqueString], @"file.dpx"];
// NSURL *tempFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:tempFileName]];
//
// OIIOImageEncodingType oiioEncodingType = [exposedOptions[@"bitDepth"] integerValue];
//
// BOOL writeSuccess = [[self imageFromLUT:lut] oiio_forceWriteToURL:tempFileURL encodingType:oiioEncodingType];
// if(!writeSuccess){
// @throw [NSException exceptionWithName:@"ImageBasedReadError"
// reason:[NSString stringWithFormat:@"OIIO failed to write the file with options: %@.", options] userInfo:nil];
// }
// else{
// NSData *data = [NSData dataWithContentsOfURL:tempFileURL];
// [[NSFileManager defaultManager] removeItemAtURL:tempFileURL error:nil];
// return data;
// }
// }
// #endif
return [[self imageFromLUT:lut bitdepth:[exposedOptions[@"bitDepth"] integerValue]] TIFFRepresentation];
}
+(LUT *)LUTFromURL:(NSURL *)fileURL{
if(![[self fileExtensions] containsObject:[fileURL pathExtension].lowercaseString]){
@throw [NSException exceptionWithName:@"ImageBasedReadError"
reason:@"Invalid file extension." userInfo:nil];
}
NSMutableDictionary *passthroughFileOptions = [NSMutableDictionary dictionary];
NSImage *image;
// #if defined(COCOAPODS_POD_AVAILABLE_oiiococoa)
// image = [NSImage oiio_imageWithContentsOfURL:fileURL];
// if([image oiio_findOIIOImageRep] != nil){
// passthroughFileOptions[@"bitDepth"] = @([image oiio_findOIIOImageRep].encodingType);
// }
// else{
// passthroughFileOptions[@"bitDepth"] = @([(NSImageRep*)image.representations[0] bitsPerSample]);
// }
// #else
image = [[NSImage alloc] initWithContentsOfURL:fileURL];
passthroughFileOptions[@"bitDepth"] = @([(NSImageRep*)image.representations[0] bitsPerSample]);
// #endif
NSString *fileTypeVariant = [fileURL pathExtension].uppercaseString;
if([fileTypeVariant isEqualToString:@"TIF"]){
fileTypeVariant = @"TIFF";
}
passthroughFileOptions[@"fileTypeVariant"] = fileTypeVariant;
LUT *lut = [self LUTFromImage:image];
lut.passthroughFileOptions = @{[self utiString] : passthroughFileOptions};
return lut;
}
+ (NSImage *)imageFromLUT:(LUT *)lut bitdepth:(NSUInteger)bitdepth{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (LUT *)LUTFromImage:(NSImage *)image{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
#endif
+(BOOL)isValidReaderForURL:(NSURL *)fileURL{
if([super isValidReaderForURL:fileURL] == NO){
return NO;
}
LUT *lut;
@try {
lut = [self LUTFromURL:fileURL];
}
@catch (NSException *exception) {
//NSLog(@"Exception reading file: %@: %@", exception.name, exception);
return NO;
}
return YES;
}
+ (LUTFormatterOutputType)outputType{
return LUTFormatterOutputType3D;
}
+ (NSArray *)fileExtensions{
// #if defined(COCOAPODS_POD_AVAILABLE_oiiococoa)
// return @[@"tiff", @"tif", @"dpx"];
// #else
return @[@"tiff", @"tif"];
// #endif
}
+ (BOOL)canRead{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return NO;
#elif TARGET_OS_MAC
return YES;
#endif
}
+ (BOOL)canWrite{
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return NO;
#elif TARGET_OS_MAC
return YES;
#endif
}
+ (NSDictionary *)constantConstraints{
return @{@"outputBounds":@[@0, @1]};
}
+ (NSArray *)allOptions{
M13OrderedDictionary *tiffBitDepthOrderedDict = [[M13OrderedDictionary alloc] initWithObjects:@[@(16),@(8)] pairedWithKeys:@[@"16-bit", @"8-bit"]];
NSDictionary *tiffOptions =
@{@"fileTypeVariant":@"TIFF",
@"bitDepth":tiffBitDepthOrderedDict};
// #if defined(COCOAPODS_POD_AVAILABLE_oiiococoa)
// M13OrderedDictionary *dpxBitDepthOrderedDict = [[M13OrderedDictionary alloc] initWithObjects:@[@(OIIOImageEncodingTypeUINT10), @(OIIOImageEncodingTypeUINT12), @(OIIOImageEncodingTypeUINT16)] pairedWithKeys:@[@"10-bit", @"12-bit", @"16-bit"]];
//
// NSDictionary *dpxOptions =
// @{@"fileTypeVariant":@"DPX",
// @"bitDepth":dpxBitDepthOrderedDict};
// return @[dpxOptions, tiffOptions];
// #else
return @[tiffOptions];
// #endif
}
+ (NSDictionary *)defaultOptions{
NSDictionary *dictionary;
// #if defined(COCOAPODS_POD_AVAILABLE_oiiococoa)
// dictionary = @{@"fileTypeVariant": @"DPX",
// @"bitDepth": @(OIIOImageEncodingTypeUINT10)};
// #else
dictionary = @{@"fileTypeVariant": @"TIFF",
@"bitDepth": @(16)};
//#endif
return @{[self utiString]: dictionary};
}
@end