forked from alejandrojapkin/CocoaLUT
/
LUTFormatterCMSTestPattern.m
127 lines (87 loc) · 4.56 KB
/
LUTFormatterCMSTestPattern.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
//
// LUTFormatterNukeCMSTestPattern.m
//
//
// Created by Greg Cotten on 3/30/14.
//
//
#import "LUTFormatterCMSTestPattern.h"
#import "CocoaLUT.h"
@implementation LUTFormatterCMSTestPattern
+ (void)load{
[super load];
}
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR
#elif TARGET_OS_MAC
+ (NSImage *)imageFromLUT:(LUT *)lut
bitdepth:(NSUInteger)bitdepth {
LUT3D *lut3D = LUTAsLUT3D(lut, clampUpperBound([lut size], 64));
int cubeSize = (int)[lut3D size];
int height = round(sqrt(cubeSize)*(double)cubeSize);
int width = ceil(((double)pow(cubeSize,3))/(double)height);
//NSLog(@"w:%i h:%i", width*7, height*7);
// CGColorSpaceRef cgColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
// NSColorSpace *linearRGBColorSpace = [[NSColorSpace alloc] initWithCGColorSpace:cgColorSpace];
// CGColorSpaceRelease(cgColorSpace);
NSBitmapImageRep *imageRep = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:(CGFloat)width*7
pixelsHigh:(CGFloat)height*7
bitsPerSample:bitdepth
samplesPerPixel:3
hasAlpha:NO
isPlanar:NO
colorSpaceName:NSDeviceRGBColorSpace
bytesPerRow:(width*7 * (bitdepth * 3)) / 8
bitsPerPixel:bitdepth * 3];
for(int y = 0; y < height; y++){
for(int x = 0; x < width; x++){
NSUInteger currentCubeIndex = y*width + x;
// NSLog(@"ax%i ay%i", x, (int)(height - (y+1)));
// NSLog(@"px%i py%i", x*7, (height - (y+1))*7);
NSUInteger redIndex = currentCubeIndex % cubeSize;
NSUInteger greenIndex = ( (currentCubeIndex % (cubeSize * cubeSize)) / (cubeSize) );
NSUInteger blueIndex = currentCubeIndex / (cubeSize * cubeSize);
if(currentCubeIndex < cubeSize*cubeSize*cubeSize){
NSColor *color = [[lut3D colorAtR:redIndex g:greenIndex b:blueIndex].systemColor colorUsingColorSpaceName:NSDeviceRGBColorSpace];
for(int px = (int)x*7; px < x*7+7; px++){
for(int py = (int)(height - (y+1))*7; py < (height - (y+1))*7+7; py++){
[imageRep setColor:color atX:px y:py];
}
}
}
}
}
NSImage* image = [[NSImage alloc] initWithSize:NSMakeSize(width, height)];
[image addRepresentation:imageRep];
return image;
}
+ (LUT *)LUTFromImage:(NSImage *)image {
NSBitmapImageRep* imageRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]];
int cubeSize = (int)(round(pow((imageRep.pixelsHigh/7)*(imageRep.pixelsHigh/7), 1.0/3.0)));
int height = round(sqrt(cubeSize)*(double)cubeSize);
int width = ceil(((double)pow(cubeSize,3))/(double)height);
if (imageRep.pixelsWide != width*7 || imageRep.pixelsHigh != height*7) {
NSException *exception = [NSException exceptionWithName:@"CMSTestPatternReadError"
reason:@"Image dimensions don't conform to spec." userInfo:nil];
@throw exception;
}
LUT3D *lut = [LUT3D LUTOfSize:cubeSize inputLowerBound:0.0 inputUpperBound:1.0];
LUTConcurrentRectLoop(width, height, ^(NSUInteger x, NSUInteger y) {
NSUInteger currentCubeIndex = y*width + x;
NSUInteger redIndex = currentCubeIndex % cubeSize;
NSUInteger greenIndex = ( (currentCubeIndex % (cubeSize * cubeSize)) / (cubeSize) );
NSUInteger blueIndex = currentCubeIndex / (cubeSize * cubeSize);
if(currentCubeIndex < cubeSize*cubeSize*cubeSize){
[lut setColor:[LUTColor colorWithSystemColor:[imageRep colorAtX:x*7 y:(height - (y+1))*7]] r:redIndex g:greenIndex b:blueIndex];
}
});
return lut;
}
#endif
+ (NSString *)utiString{
return @"public.cms-test-pattern-lut";
}
+ (NSString *)formatterName{
return @"CMS Test Pattern Image 3D LUT";
}
@end