forked from alejandrojapkin/CocoaLUT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LUT.m
418 lines (333 loc) · 16.9 KB
/
LUT.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
//
// LUT.m
// DropLUT
//
// Created by Wil Gieseler on 12/15/13.
// Copyright (c) 2013 Wil Gieseler. All rights reserved.
//
#import "LUT.h"
#import "CocoaLUT.h"
#import "LUTFormatter.h"
@interface LUT ()
@end
@implementation LUT
- (instancetype)init {
if (self = [super init]) {
self.metadata = [NSMutableDictionary dictionary];
}
return self;
}
- (instancetype)initWithSize:(NSUInteger)size
inputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
if (self = [super init]) {
if(inputLowerBound >= inputUpperBound){
@throw [NSException exceptionWithName:@"LUTCreationError" reason:@"Input Lower Bound >= Input Upper Bound" userInfo:nil];
}
self.metadata = [NSMutableDictionary dictionary];
self.passthroughFileOptions = nil;
self.size = size;
self.inputLowerBound = inputLowerBound;
self.inputUpperBound = inputUpperBound;
}
return self;
}
+ (instancetype)LUTFromURL:(NSURL *)url {
LUTFormatter *formatter = [LUTFormatter LUTFormatterValidForReadingURL:url];
return [[formatter class] LUTFromURL:url];
}
- (NSData *)dataFromLUTWithUTIString:(NSString *)utiString
options:(NSDictionary *)options{
LUTFormatter *formatter = [LUTFormatter LUTFormatterForUTIString:utiString];
if(formatter == nil){
NSLog(@"Formatter for UTI %@ not found.", utiString);
return nil;
}
else if([[formatter class] isValidWriterForLUTType:self] == NO){
NSLog(@"%@ is not a valid writer for LUT (%@) with options: %@.", [[formatter class] formatterName], self, options);
return nil;
}
else{
return [[formatter class] dataFromLUT:self withOptions:options];
}
}
+ (instancetype)LUTOfSize:(NSUInteger)size
inputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
+ (instancetype)LUTIdentityOfSize:(NSUInteger)size
inputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
LUT *identityLUT = [self LUTOfSize:size inputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
[identityLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[identityLUT setColor:[identityLUT identityColorAtR:r g:g b:b] r:r g:g b:b];
}];
return identityLUT;
}
- (void)copyMetaPropertiesFromLUT:(LUT *)lut{
self.title = [lut.title copy];
self.descriptionText = [lut.descriptionText copy];
self.metadata = [lut.metadata mutableCopy];
self.passthroughFileOptions = [lut.passthroughFileOptions copy];
}
- (void)LUTLoopWithBlock:(void (^)(size_t r, size_t g, size_t b))block{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (instancetype)LUTByResizingToSize:(NSUInteger)newSize {
if (newSize == [self size]) {
return [self copy];
}
LUT *resizedLUT = [[self class] LUTOfSize:newSize inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[resizedLUT copyMetaPropertiesFromLUT:self];
double ratio = ((double)self.size - 1.0) / ((double)newSize - 1.0);
[resizedLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtInterpolatedR:clampUpperBound(r * ratio, self.size-1.0) g:clampUpperBound(g * ratio, self.size-1.0) b:clampUpperBound(b * ratio, self.size-1.0)];
[resizedLUT setColor:color r:r g:g b:b];
}];
return resizedLUT;
}
- (instancetype)LUTByClampingLowerBound:(double)lowerBound
upperBound:(double)upperBound{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] clampedWithLowerBound:lowerBound upperBound:upperBound] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByClampingLowerBoundOnly:(double)lowerBound{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] clampedWithLowerBoundOnly:lowerBound] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByClampingUpperBoundOnly:(double)upperBound{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] clampedWithUpperBoundOnly:upperBound] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByRemappingValuesWithInputLow:(double)inputLow
inputHigh:(double)inputHigh
outputLow:(double)outputLow
outputHigh:(double)outputHigh
bounded:(BOOL)bounded{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self colorAtR:r g:g b:b] remappedFromInputLow:inputLow
inputHigh:inputHigh
outputLow:outputLow
outputHigh:outputHigh
bounded:bounded] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByChangingStrength:(double)strength{
if(strength > 1.0){
@throw [NSException exceptionWithName:@"ChangeStrengthError" reason:[NSString stringWithFormat:@"You can't set the strength of the LUT past 1.0 (%f)", strength] userInfo:nil];
}
if(strength == 1.0){
return [self copy];
}
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
[newLUT setColor:[[self identityColorAtR:r g:g b:b] lerpTo:[self colorAtR:r g:g b:b] amount:strength] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByCombiningWithLUT:(LUT *)otherLUT {
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (instancetype)LUTBySwizzling1DChannelsWithMethod:(LUT1DSwizzleChannelsMethod)method{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (instancetype)LUTByChangingInputLowerBound:(double)inputLowerBound
inputUpperBound:(double)inputUpperBound{
if(inputLowerBound == [self inputLowerBound] && inputUpperBound == [self inputUpperBound]){
return [self copy];
}
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:inputLowerBound inputUpperBound:inputUpperBound];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *identityColor = [newLUT identityColorAtR:r g:g b:b];
[newLUT setColor:[self colorAtColor:identityColor] r:r g:g b:b];
}];
return newLUT;
}
- (instancetype)LUTByInvertingColor{
LUT *newLUT = [[self class] LUTOfSize:[self size] inputLowerBound:self.inputLowerBound inputUpperBound:self.inputUpperBound];
[newLUT copyMetaPropertiesFromLUT:self];
[newLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *startColor = [self colorAtR:r g:g b:b];
[newLUT setColor:[startColor colorByInvertingColorWithMinimumValue:0 maximumValue:1] r:r g:g b:b];
}];
return newLUT;
}
- (LUTColor *)identityColorAtR:(double)redPoint g:(double)greenPoint b:(double)bluePoint{
double red = remap(redPoint, 0, [self size] - 1, [self inputLowerBound], [self inputUpperBound]);
double green = remap(greenPoint, 0, [self size] - 1, [self inputLowerBound], [self inputUpperBound]);
double blue = remap(bluePoint, 0, [self size] - 1, [self inputLowerBound], [self inputUpperBound]);
return [LUTColor colorWithRed:red green:green blue:blue];
}
- (LUTColor *)colorAtColor:(LUTColor *)color{
color = [color clampedWithLowerBound:[self inputLowerBound] upperBound:[self inputUpperBound]];
double redRemappedInterpolatedIndex = remap(color.red, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
double greenRemappedInterpolatedIndex = remap(color.green, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
double blueRemappedInterpolatedIndex = remap(color.blue, [self inputLowerBound], [self inputUpperBound], 0, [self size]-1);
return [self colorAtInterpolatedR:clamp(redRemappedInterpolatedIndex, 0, self.size-1)
g:clamp(greenRemappedInterpolatedIndex, 0, self.size-1)
b:clamp(blueRemappedInterpolatedIndex, 0, self.size-1)];
}
- (LUTColor *)colorAtR:(NSUInteger)r g:(NSUInteger)g b:(NSUInteger)b{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (LUTColor *)colorAtInterpolatedR:(double)redPoint g:(double)greenPoint b:(double)bluePoint{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (void)setColor:(LUTColor *)color r:(NSUInteger)r g:(NSUInteger)g b:(NSUInteger)b{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (double)maximumOutputValue{
__block double maxValue = [self colorAtR:0 g:0 b:0].red;
[self LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtR:r g:g b:b];
if(color.red > maxValue){
maxValue = color.red;
}
if(color.green > maxValue){
maxValue = color.green;
}
if(color.blue > maxValue){
maxValue = color.blue;
}
}];
return maxValue;
}
- (double)minimumOutputValue{
__block double minValue = [self colorAtR:0 g:0 b:0].red;
[self LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *color = [self colorAtR:r g:g b:b];
if(color.red < minValue){
minValue = color.red;
}
if(color.green < minValue){
minValue = color.green;
}
if(color.blue < minValue){
minValue = color.blue;
}
}];
return minValue;
}
//000
- (bool) equalsIdentityLUT{
return [self equalsLUT:[[self class] LUTIdentityOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]]];
}
- (bool)equalsLUT:(LUT *)comparisonLUT{
@throw [NSException exceptionWithName:@"NotImplemented" reason:[NSString stringWithFormat:@"\"%s\" Not Implemented", __func__] userInfo:nil];
}
- (id)copyWithZone:(NSZone *)zone {
LUT *copiedLUT = [[self class] LUTOfSize:[self size] inputLowerBound:[self inputLowerBound] inputUpperBound:[self inputUpperBound]];
[copiedLUT setMetadata:[[self metadata] mutableCopyWithZone:zone]];
copiedLUT.descriptionText = [[self description] mutableCopyWithZone:zone];
[copiedLUT setTitle:[[self title] mutableCopyWithZone:zone]];
[copiedLUT setPassthroughFileOptions:[[self passthroughFileOptions] mutableCopyWithZone:zone]];
return copiedLUT;
}
- (CIFilter *)coreImageFilterWithCurrentColorSpace {
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
return [self coreImageFilterWithColorSpace:CGColorSpaceCreateDeviceRGB()];
#elif TARGET_OS_MAC
//good for render, not good for viewing
return [self coreImageFilterWithColorSpace:CGColorSpaceCreateDeviceRGB()];
//good for viewing, not good for render
//return [self coreImageFilterWithColorSpace:[[[NSScreen mainScreen] colorSpace] CGColorSpace]];
#endif
}
- (CIFilter *)coreImageFilterWithColorSpace:(CGColorSpaceRef)colorSpace {
NSUInteger sizeOfColorCubeFilter = clamp([self size], 0, COCOALUT_MAX_CICOLORCUBE_SIZE);
LUT3D *used3DLUT = [LUTAsLUT3D(self, sizeOfColorCubeFilter) LUTByChangingInputLowerBound:0.0 inputUpperBound:1.0];
size_t size = [used3DLUT size];
size_t cubeDataSize = size * size * size * sizeof (float) * 4;
float *cubeData = (float *) malloc (cubeDataSize);
[used3DLUT LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
LUTColor *transformedColor = [used3DLUT colorAtR:r g:g b:b];
size_t offset = 4*(b*size*size + g*size + r);
cubeData[offset] = (float)transformedColor.red;
cubeData[offset+1] = (float)transformedColor.green;
cubeData[offset+2] = (float)transformedColor.blue;
cubeData[offset+3] = 1.0f;
}];
NSData *data = [NSData dataWithBytesNoCopy:cubeData length:cubeDataSize freeWhenDone:YES];
CIFilter *colorCube;
if (colorSpace) {
colorCube = [CIFilter filterWithName:@"CIColorCubeWithColorSpace"];
[colorCube setValue:(__bridge id)(colorSpace) forKey:@"inputColorSpace"];
}
else {
colorCube = [CIFilter filterWithName:@"CIColorCube"];
}
[colorCube setValue:@(size) forKey:@"inputCubeDimension"];
[colorCube setValue:data forKey:@"inputCubeData"];
return colorCube;
}
- (CIImage *)processCIImage:(CIImage *)image {
CIFilter *filter = [self coreImageFilterWithCurrentColorSpace];
[filter setValue:image forKey:@"inputImage"];
return [filter valueForKey:@"outputImage"];
}
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
- (UIImage *)processUIImage:(UIImage *)image withColorSpace:(CGColorSpaceRef)colorSpace {
return [[UIImage alloc] initWithCIImage:[self processCIImage:image.CIImage]];
}
#elif TARGET_OS_MAC
- (NSImage *)processNSImage:(NSImage *)image
renderPath:(LUTImageRenderPath)renderPath {
if (renderPath == LUTImageRenderPathCoreImage || renderPath == LUTImageRenderPathCoreImageSoftware) {
NSImageRep *firstRep = [image.representations firstObject];
if (![firstRep isKindOfClass:[NSBitmapImageRep class]]) {
return nil;
}
CIImage *inputCIImage = [[CIImage alloc] initWithBitmapImageRep:(NSBitmapImageRep *)firstRep];;
CIImage *outputCIImage = [self processCIImage:inputCIImage];
return LUTNSImageFromCIImage(outputCIImage, renderPath == LUTImageRenderPathCoreImageSoftware);
}
else if (renderPath == LUTImageRenderPathDirect) {
return [self processNSImageDirectly:image];
}
return nil;
}
- (NSImage *)processNSImageDirectly:(NSImage *)image {
NSBitmapImageRep *inImageRep = [image representations][0];
int nchannels = 3;
int bps = 16;
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:image.size.width
pixelsHigh:image.size.height
bitsPerSample:bps
samplesPerPixel:nchannels
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:(image.size.width * (bps * nchannels)) / 8
bitsPerPixel:bps * nchannels];
for (int x = 0; x < image.size.width; x++) {
for (int y = 0; y < image.size.height; y++) {
LUTColor *lutColor = [LUTColor colorWithSystemColor:[inImageRep colorAtX:x y:y]];
LUTColor *transformedColor =[self colorAtColor:lutColor];
[imageRep setColor:transformedColor.systemColor atX:x y:y];
}
}
NSImage* outImage = [[NSImage alloc] initWithSize:image.size];
[outImage addRepresentation:imageRep];
return outImage;
}
#endif
@end