-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathXFImageFilter.m
More file actions
207 lines (163 loc) · 5.3 KB
/
XFImageFilter.m
File metadata and controls
207 lines (163 loc) · 5.3 KB
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
//
// XFImageFilter.m
// ExtraFile
//
// Created by Kim Asendorf on 06.05.11.
//
#import "XFImageFilter.h"
@implementation XFImageFilter
- (void)dealloc
{
CGImageRelease(xfImage);
[xfCIImage release];
[xfProfile release];
[xfCIExposure release];
[xfCIColorControls release];
[xfCIColorCube release];
[super dealloc];
}
- (id)initWithImage:(CGImageRef)image
{
if ((self = [super init]))
{
xfImage = CGImageRetain(image);
xfCIImage = [[CIImage imageWithCGImage:xfImage] retain];
}
return self;
}
- (void)setProfile:(XFProfile *)profile
{
[xfProfile release];
xfProfile = [profile retain];
if (xfProfile == nil)
{
[xfCIColorCube autorelease];
xfCIColorCube = nil;
}
else
{
// Use the CIColorCube filter three-dimensional color table
// to transform the source image pixels
if (xfCIColorCube == nil)
xfCIColorCube = [[CIFilter filterWithName: @"CIColorCube"] retain];
// Get the transformed data
static const int kSoftProofGrid = 32;
NSData *data = [xfProfile dataForCISoftproofTextureWithGridSize:kSoftProofGrid];
[xfCIColorCube setValue:data
forKey:@"inputCubeData"];
[xfCIColorCube setValue:[NSNumber numberWithInt:kSoftProofGrid]
forKey:@"inputCubeDimension"];
}
}
- (void)setExposure:(NSNumber *)exposure
{
if (xfCIExposure == nil)
xfCIExposure = [[CIFilter filterWithName: @"CIExposureAdjust"] retain];
[xfCIExposure setValue:exposure
forKey: @"inputEV"];
}
- (void)setSaturation:(NSNumber *)saturation
{
if (xfCIColorControls == nil)
xfCIColorControls = [[CIFilter filterWithName: @"CIColorControls"] retain];
[xfCIColorControls setValue:saturation
forKey: @"inputSaturation"];
[xfCIColorControls setValue:[[[xfCIColorControls attributes]
objectForKey: @"inputBrightness"]
objectForKey: @"CIAttributeIdentity"]
forKey: @"inputBrightness"];
[xfCIColorControls setValue:[[[xfCIColorControls attributes]
objectForKey: @"inputContrast"]
objectForKey: @"CIAttributeIdentity"]
forKey: @"inputContrast"];
}
- (CIImage *)imageWithTransform:(CGAffineTransform)ctm
{
// Returns a new image representing the original image with the transform
// 'ctm' appended to it.
CIImage* ciimg = [xfCIImage imageByApplyingTransform:ctm];
if (xfCIExposure)
{
[xfCIExposure setValue:ciimg forKey:@"inputImage"];
ciimg = [xfCIExposure valueForKey: @"outputImage"];
}
if (xfCIColorControls)
{
[xfCIColorControls setValue:ciimg forKey:@"inputImage"];
ciimg = [xfCIColorControls valueForKey: @"outputImage"];
}
if (xfCIColorCube)
{
[xfCIColorCube setValue:ciimg forKey: @"inputImage"];
ciimg = [xfCIColorCube valueForKey: @"outputImage"];
}
return ciimg;
}
- (CGImageRef)createCGImage
{
if (xfImage==nil)
return nil;
XFProfile* prof = xfProfile;
if (xfProfile==nil)
prof = [XFProfile profileWithGenericRGB];
// calculate bits per pixel and row bytes and alphaInfo
size_t height = CGImageGetHeight(xfImage);
size_t width = CGImageGetWidth(xfImage);
CGRect rect = {{0,0}, {width, height}};
size_t bitsPerComponent = 8;
size_t bytesPerRow = 0;
CGImageAlphaInfo alphaInfo = kCGImageAlphaNone;
switch ([prof spaceType])
{
case cmGrayData:
bytesPerRow = width;
alphaInfo = kCGImageAlphaNone; /* RGB. */
break;
case cmRGBData:
bytesPerRow = width*4;
alphaInfo = kCGImageAlphaPremultipliedLast; /* premultiplied RGBA */
break;
case cmCMYKData:
bytesPerRow = width*4;
alphaInfo = kCGImageAlphaNone; /* RGB. */
break;
default:
return nil;
break;
}
CGContextRef context = CGBitmapContextCreate(nil, width, height,
bitsPerComponent, bytesPerRow,
[prof colorspace], alphaInfo);
CIContext* cicontext = [CIContext contextWithCGContext: context options: nil];
// If context doesn't support alpha then first fill it with white.
// That is most likely to be desireable.
if (alphaInfo == kCGImageAlphaNone)
{
CGColorSpaceRef graySpace = CGColorSpaceCreateDeviceGray();
const CGFloat whiteComps[2] = {1.0, 1.0};
CGColorRef whiteColor = CGColorCreate(graySpace, whiteComps);
CFRelease(graySpace);
CGContextSetFillColorWithColor(context, whiteColor);
CGContextFillRect(context, rect);
CFRelease(whiteColor);
}
CIImage* ciimg = xfCIImage;
// exposure adjustment
if (xfCIExposure)
{
[xfCIExposure setValue:ciimg forKey:@"inputImage"];
ciimg = [xfCIExposure valueForKey: @"outputImage"];
}
// three-dimensional color table adjustment
if (xfCIColorControls)
{
[xfCIColorControls setValue:ciimg forKey:@"inputImage"];
ciimg = [xfCIColorControls valueForKey: @"outputImage"];
}
CGRect extent = [ciimg extent];
[cicontext drawImage: ciimg inRect:rect fromRect:extent];
CGImageRef image = CGBitmapContextCreateImage(context);
CGContextRelease(context);
return image;
}
@end