forked from alejandrojapkin/CocoaLUT
/
LUTFormatterFSIDAT.m
351 lines (259 loc) · 11.3 KB
/
LUTFormatterFSIDAT.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
//
// LUTFormatterFSIDAT.m
// Pods
//
// Created by Greg Cotten on 8/11/14.
//
//
#import "LUTFormatterFSIDAT.h"
struct LUTFormatterFSIDAT_FileHeader{
unsigned int magic; // 4Bytes, must be 0x42340299
unsigned int ver; // 4Bytes, 0x01000002 or 0x02000000
char model[16]; // 16Bytes, monitor model. No match required for DIT LUT.
char version[16]; // 16Bytes, data version, eg. “1.0.11”
unsigned int data_checksum; // 4Bytes, data sum
unsigned int length; // 4Bytes, data length = 1048576 (v1) or 19652 (v2)
char description[16]; //16Bytes, 3dlut description info, e.g. “LightSpace(c)”
unsigned int reserved2; // 4Bytes, reserved
char name[16]; // 16Bytes, information or name of lut
char reserved[43]; // 43 Bytes, reserved
unsigned char header_checksum; // file header sum
};
@implementation LUTFormatterFSIDAT
+ (void)load{
[super load];
}
+ (LUT *)LUTFromData:(NSData *)data{
struct LUTFormatterFSIDAT_FileHeader fileHeader;
[data getBytes:&fileHeader length:128];
LUT3D *lut;
if (fileHeader.ver < 0x02000000) {
unsigned int lutBytes[64*64*64];
[data getBytes:lutBytes range:NSMakeRange(128, sizeof(lutBytes))];
lut = [LUT3D LUTOfSize:64 inputLowerBound:0 inputUpperBound:1];
for (int currentCubeIndex = 0; currentCubeIndex < 64*64*64; currentCubeIndex++) {
// Valid cube line
unsigned int rgbPacked = lutBytes[currentCubeIndex];
LUTColorValue redValue = (double)(rgbPacked & 1023) / 1008.0;
LUTColorValue greenValue = (double)((rgbPacked >> 10) & 1023) / 1008.0;
LUTColorValue blueValue = (double)((rgbPacked >> 20) & 1023) / 1008.0;
LUTColor *color = [LUTColor colorWithRed:redValue green:greenValue blue:blueValue];
NSUInteger redIndex = currentCubeIndex % 64;
NSUInteger greenIndex = ( (currentCubeIndex % (64 * 64)) / (64) );
NSUInteger blueIndex = currentCubeIndex / (64 * 64);
[lut setColor:color r:redIndex g:greenIndex b:blueIndex];
}
lut.passthroughFileOptions = @{[self formatterID]: @{@"fileTypeVariant": @"v1",
@"lutSize": @(64)}};
}
else if (fileHeader.ver == 0x02000000) {
unsigned int lutBytes[17*17*17];
[data getBytes:lutBytes range:NSMakeRange(128, sizeof(lutBytes))];
lut = [LUT3D LUTOfSize:17 inputLowerBound:0 inputUpperBound:1];
for (int currentCubeIndex = 0; currentCubeIndex < 17*17*17; currentCubeIndex++) {
// Valid cube line
unsigned int rgbPacked = CFSwapInt32(lutBytes[currentCubeIndex]);
LUTColorValue redValue = (double)(rgbPacked & 1023) / 1023.0;
LUTColorValue greenValue = (double)((rgbPacked >> 10) & 1023) / 1023.0;
LUTColorValue blueValue = (double)((rgbPacked >> 20) & 1023) / 1023.0;
LUTColor *color = [LUTColor colorWithRed:redValue green:greenValue blue:blueValue];
NSUInteger redIndex = currentCubeIndex % 17;
NSUInteger greenIndex = ( (currentCubeIndex % (17 * 17)) / (17) );
NSUInteger blueIndex = currentCubeIndex / (17 * 17);
[lut setColor:color r:redIndex g:greenIndex b:blueIndex];
}
lut.passthroughFileOptions = @{[self formatterID]: @{@"fileTypeVariant": @"v2",
@"lutSize": @(17)}};
}
else{
@throw [NSException exceptionWithName:@"FSIDATLUTReadError" reason:@"Incompatible Version" userInfo:nil];
}
NSString *nameString = [NSString stringWithCString:fileHeader.name encoding:NSUTF8StringEncoding] ?: @"";
lut.title = nameString;
NSString *modelString = [NSString stringWithCString:fileHeader.model encoding:NSUTF8StringEncoding] ?: @"";
NSString *headerDescription = [NSString stringWithCString:fileHeader.description encoding:NSUTF8StringEncoding];
lut.descriptionText = headerDescription ? headerDescription : @"";
NSString *versionString = [NSString stringWithCString:fileHeader.version encoding:NSUTF8StringEncoding];
if (versionString) {
lut.metadata = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"version", versionString, @"model", modelString, nil];
}
return lut;
}
+ (NSData *)dataFromLUT:(LUT *)lut withOptions:(NSDictionary *)options{
if(![self optionsAreValid:options]){
@throw [NSException exceptionWithName:@"FSIDATLUTWriteError" reason:[NSString stringWithFormat:@"Options don't pass the spec: %@", options] userInfo:nil];
}
else{
options = options[[self formatterID]];
}
if ([options[@"fileTypeVariant"] isEqualToString:@"v1"]) {
return [self dataFromLUTv1:lut withOptions:options];
}
else if ([options[@"fileTypeVariant"] isEqualToString:@"v2"]) {
return [self dataFromLUTv2:lut withOptions:options];
}
return nil;
}
+ (NSData *)dataFromLUTv2:(LUT *)lut withOptions:(NSDictionary *)options{
//pack LUT in bytes
if (lut.size != 17) {
@throw [NSException exceptionWithName:@"FSIDATLUTWriteError" reason:@"LUT is not size 17" userInfo:nil];
}
unsigned int *lutBytes = malloc(17*17*17*4);
for (int currentCubeIndex = 0; currentCubeIndex < 17*17*17; currentCubeIndex++) {
NSUInteger redIndex = currentCubeIndex % 17;
NSUInteger greenIndex = ( (currentCubeIndex % (17 * 17)) / (17) );
NSUInteger blueIndex = currentCubeIndex / (17 * 17);
LUTColor *color = [lut colorAtR:redIndex g:greenIndex b:blueIndex];
unsigned int rgbPacked = (unsigned int)round(color.red * 1023.0) | ((unsigned int)round(color.green * 1023.0) << 10) | ((unsigned int)round(color.blue * 1023.0) << 20);
lutBytes[currentCubeIndex] = CFSwapInt32(rgbPacked);
}
//--end LUT
//---create header
struct LUTFormatterFSIDAT_FileHeader fileHeader;
//set constants in header
fileHeader.magic = 0x42340299;
fileHeader.ver = 0x02000000;
fileHeader.length = 17*17*17*4;
strncpy(fileHeader.reserved, "", sizeof(fileHeader.reserved));
//set variables in header
if (lut.metadata[@"version"]) {
strncpy(fileHeader.version, [lut.metadata[@"version"] UTF8String], sizeof(fileHeader.version));
}
else{
strncpy(fileHeader.version, "", sizeof(fileHeader.version));
}
if (lut.metadata[@"model"]) {
strncpy(fileHeader.model, [lut.metadata[@"model"] UTF8String], sizeof(fileHeader.model));
}
else{
strncpy(fileHeader.model, "", sizeof(fileHeader.model));
}
strncpy(fileHeader.name, lut.title ? [lut.title UTF8String] : "", sizeof(fileHeader.name));
strncpy(fileHeader.description, lut.descriptionText ? [lut.descriptionText UTF8String] : "", sizeof(fileHeader.description));
//LUT checksum
unsigned int data_sum = 0;
for (int i = 0; i < 17*17*17*4; i++){
data_sum += ((unsigned char *)lutBytes)[i];
}
fileHeader.data_checksum = data_sum;
//HEADER checksum
unsigned char header_sum = 0;
for (int i = 0; i < 128; i++){
header_sum += ((unsigned char *)&fileHeader)[i];
}
fileHeader.header_checksum = header_sum;
//---end header
NSMutableData *data = [NSMutableData dataWithBytes:&fileHeader length:sizeof(fileHeader)];
[data appendBytes:lutBytes length:17*17*17*4];
free(lutBytes);
return data;
}
+ (NSData *)dataFromLUTv1:(LUT *)lut withOptions:(NSDictionary *)options{
if (lut.size != 64) {
@throw [NSException exceptionWithName:@"FSIDATLUTWriteError" reason:@"LUT is not size 64" userInfo:nil];
}
//pack LUT in bytes
unsigned int *lutBytes = malloc(64*64*64*4);
for (int currentCubeIndex = 0; currentCubeIndex < 64*64*64; currentCubeIndex++) {
NSUInteger redIndex = currentCubeIndex % 64;
NSUInteger greenIndex = ( (currentCubeIndex % (64 * 64)) / (64) );
NSUInteger blueIndex = currentCubeIndex / (64 * 64);
LUTColor *color = [lut colorAtR:redIndex g:greenIndex b:blueIndex];
unsigned int rgbPacked = (unsigned int)(color.red * 1008.0) | ((unsigned int)(color.green * 1008.0) << 10) | ((unsigned int)(color.blue * 1008.0) << 20);
lutBytes[currentCubeIndex] = rgbPacked;
}
//--end LUT
//---create header
struct LUTFormatterFSIDAT_FileHeader fileHeader;
//set constants in header
fileHeader.magic = 0x42340299;
fileHeader.ver = 0x01000002;
fileHeader.length = 64*64*64*4;
strncpy(fileHeader.reserved, "", sizeof(fileHeader.reserved));
//set variables in header
if (lut.metadata[@"version"]) {
strncpy(fileHeader.version, [lut.metadata[@"version"] UTF8String], sizeof(fileHeader.version));
}
else{
strncpy(fileHeader.version, "", sizeof(fileHeader.version));
}
if (lut.metadata[@"model"]) {
strncpy(fileHeader.model, [lut.metadata[@"model"] UTF8String], sizeof(fileHeader.model));
}
else{
strncpy(fileHeader.model, "", sizeof(fileHeader.model));
}
strncpy(fileHeader.name, lut.title ? [lut.title UTF8String] : "", sizeof(fileHeader.name));
strncpy(fileHeader.description, lut.descriptionText ? [lut.descriptionText UTF8String] : "", sizeof(fileHeader.description));
//LUT checksum
unsigned int data_sum = 0;
for (int i = 0; i < 64*64*64*4; i++){
data_sum += ((unsigned char *)lutBytes)[i];
}
fileHeader.data_checksum = data_sum;
//HEADER checksum
unsigned char header_sum = 0;
for (int i = 0; i < 127; i++){
header_sum += ((unsigned char *)&fileHeader)[i];
}
fileHeader.header_checksum = header_sum;
//---end header
NSMutableData *data = [NSMutableData dataWithBytes:&fileHeader length:sizeof(fileHeader)];
[data appendBytes:lutBytes length:64*64*64*4];
free(lutBytes);
return data;
}
+ (NSDictionary *)constantConstraints{
return @{@"inputBounds":@[@0, @1],
@"outputBounds":@[@0, @1]};
}
+ (LUTFormatterOutputType)outputType{
return LUTFormatterOutputType3D;
}
+ (BOOL)isValidReaderForURL:(NSURL *)fileURL{
if ([super isValidReaderForURL:fileURL] == NO) {
return NO;
}
NSData *data = [NSData dataWithContentsOfURL:fileURL];
struct LUTFormatterFSIDAT_FileHeader fileHeader;
[data getBytes:&fileHeader length:sizeof(fileHeader)];
if (data.length != 128 + 64*64*64*4 && data.length != 128 + 17*17*17*4) {
return NO;
}
if (fileHeader.magic == 0x42340299) {
return YES;
}
return NO;
}
+ (NSString *)formatterName{
return @"FSI DAT 3D LUT";
}
+ (NSString *)formatterID{
return @"fsiDAT";
}
+ (BOOL)canRead{
return YES;
}
+ (BOOL)canWrite{
return YES;
}
+ (NSString *)utiString{
return @"public.dat-lut";
}
+ (NSArray *)fileExtensions{
return @[@"dat"];
}
+ (NSArray *)allOptions{
NSDictionary *v1Options = @{@"fileTypeVariant":@"v1",
@"lutSize": M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"64": @(64)}])};
NSDictionary *v2Options = @{@"fileTypeVariant":@"v2",
@"lutSize": M13OrderedDictionaryFromOrderedArrayWithDictionaries(@[@{@"17": @(17)}])};
return @[v1Options, v2Options];
}
+ (NSDictionary *)defaultOptions{
NSDictionary *dictionary = @{@"fileTypeVariant":@"v1",
@"lutSize": @(64)};
return @{[self formatterID]: dictionary};
}
@end