forked from alejandrojapkin/CocoaLUT
/
LUTReverser.m
186 lines (150 loc) · 6.03 KB
/
LUTReverser.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
//
// LUTReverser.m
// DropLUT
//
// Created by Wil Gieseler on 12/16/13.
// Copyright (c) 2013 Wil Gieseler. All rights reserved.
//
#import "LUTReverser.h"
#import "LUTHelper.h"
#if false
@interface LUTReverser ()
@property (strong) KDTree *kdTree;
@property (strong) NSArray *inputArray;
@property (assign) BOOL useTree;
@property (assign) NSUInteger outputSize;
@property (strong) NSOperationQueue *queue;
@end
@implementation LUTReverser
- (void)process {
[super process];
if([self.lut equalsIdentityLUT]){
[self completedWithLUT:[self.lut copy]];
return;
}
if(isLUT1D(self.lut)){
LUT1D *reversedLUT1D = [(LUT1D *)self.lut LUT1DByReversingWithStrictness:NO autoAdjustInputBounds:YES];
if(reversedLUT1D != nil){
[self completedWithLUT:reversedLUT1D];
}
else{
//not reversible
[self didCancel];
}
return;
}
self.queue = [[NSOperationQueue alloc] init];
self.useTree = YES;
self.outputSize = [self.lut size];
// RESIZE LUT
NSBlockOperation *resizeOperation = [NSBlockOperation blockOperationWithBlock:^{
timer(@"Enlarging", ^{
self.progressDescription = @"Enlarging LUT...";
self.lut = [self.lut LUTByResizingToSize:self.outputSize * 3];
});
}];
// BUILD INPUT LUT
NSBlockOperation *buildOperation = [NSBlockOperation blockOperationWithBlock:^{
timer(@"Building Array", ^{
self.progressDescription = @"Building input array...";
[self buildInputArray:self.outputSize * 3];
});
}];
[buildOperation addDependency:resizeOperation];
// FIND OUTPUTS
NSBlockOperation *findOperation = [NSBlockOperation blockOperationWithBlock:^{
timer(@"Searching Tree", ^{
self.progressDescription = @"Searching and building new LUT...";
[self search];
});
}];
[findOperation addDependency:buildOperation];
// BUILD KD TREE IF NECCESSARY
if (self.useTree) {
NSBlockOperation *buildTreeOperation = [NSBlockOperation blockOperationWithBlock:^{
timer(@"Building Search Tree", ^{
self.progressDescription = @"Building search tree...";
self.kdTree = [[KDTree alloc] initWithArray:self.inputArray];
[self setProgress:0 section:3 of:4];
});
}];
[buildTreeOperation addDependency:buildOperation];
[findOperation addDependency:buildTreeOperation];
[self.queue addOperation:buildTreeOperation];
}
[self.queue addOperation:resizeOperation];
[self.queue addOperation:findOperation];
[self.queue addOperation:buildOperation];
}
- (void) didCancel {
[super didCancel];
[self.queue cancelAllOperations];
}
- (void)buildInputArray:(NSUInteger)newSize {
NSUInteger maxValue = newSize - 1;
NSMutableArray *array = [NSMutableArray arrayWithCapacity:newSize * newSize * newSize];
NSLock *arrayLock = [[NSLock alloc] init];
int __block completedRs = 0;
dispatch_apply(newSize, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0) , ^(size_t r){
NSMutableArray *thisArray = [NSMutableArray array];
for (int g = 0; g < newSize; g++) {
for (int b = 0; b < newSize; b++) {
if ([self checkCancellation]) return;
LUTColor *latticePointReference = [LUTColor colorWithRed:nsremapint01(r, maxValue)
green:nsremapint01(g, maxValue)
blue:nsremapint01(b, maxValue)];
LUTColor *outputColor = [self.lut colorAtR:r g:g b:b];
[thisArray addObject:@[@(outputColor.red), @(outputColor.green), @(outputColor.blue), latticePointReference]];
}
}
[arrayLock lock];
[array addObjectsFromArray: thisArray];
[arrayLock unlock];
completedRs++;
[self setProgress:(float)completedRs / (float)newSize section:2 of:4];
});
self.inputArray = array;
}
- (void)search {
LUT3D *newLUT = [LUT3D LUTOfSize:self.outputSize inputLowerBound:[self.lut inputLowerBound] inputUpperBound:[self.lut inputUpperBound]];
[newLUT copyMetaPropertiesFromLUT:self.lut];
int maxValue = (int)self.outputSize - 1;
int __block completedOps = 0;
NSUInteger totalOps = self.outputSize * self.outputSize * self.outputSize;
NSLock *progressLock = [[NSLock alloc] init];
[self.lut LUTLoopWithBlock:^(size_t r, size_t g, size_t b) {
if ([self checkCancellation]) return;
if (self.useTree) {
KDLeaf *leaf = [self.kdTree findNearestNeighbor:@[@(nsremapint01(r, maxValue)),
@(nsremapint01(g, maxValue)),
@(nsremapint01(b, maxValue))]];
[newLUT setColor:leaf.metadata r:r g:g b:b];
}
else {
LUTColor *color = [self colorNearestToR:nsremapint01(r, maxValue)
g:nsremapint01(g, maxValue)
b:nsremapint01(b, maxValue)];
[newLUT setColor:color r:r g:g b:b];
}
completedOps++;
[progressLock lock];
[self setProgress:(float)completedOps / (float)totalOps section:4 of:4];
[progressLock unlock];
}];
[self completedWithLUT:newLUT];
}
- (LUTColor *)colorNearestToR:(int)r g:(int)g b:(int)b {
float __block pickedDistance = FLT_MAX;
NSUInteger __block pickedIndex = 0;
[self.inputArray enumerateObjectsUsingBlock:^(NSArray *array, NSUInteger idx, BOOL *stop) {
float distance = distancecalc([array[0] floatValue], [array[1] floatValue], [array[2] floatValue], r, g, b);
if (distance < pickedDistance) {
pickedDistance = distance;
pickedIndex = idx;
}
}];
NSArray *subArray = self.inputArray[pickedIndex];
return subArray[3];
}
@end
#endif