forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSBitmapImageRep+CoreImage.m
68 lines (59 loc) · 2.35 KB
/
NSBitmapImageRep+CoreImage.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
// From http://www.cocoadev.com/index.pl?NSImageCategory
// NSBitmapImageRep+CoreImage.m
// iTerm2
#import <QuartzCore/QuartzCore.h>
#import "NSBitmapImageRep+CoreImage.h"
#import "NSImage+CoreImage.h"
#define CIIMAGE_PADDING 16.0f
@implementation NSBitmapImageRep (CoreImage)
- (void)drawAtPoint: (NSPoint)point fromRect: (NSRect)fromRect coreImageFilter: (NSString *)filterName arguments: (NSDictionary *)arguments {
NSAutoreleasePool *pool;
CIFilter *filter;
CIImage *before;
CIImage *after;
CIContext *ciContext;
CGContextRef cgContext;
pool = [[NSAutoreleasePool alloc] init];
before = nil;
@try {
before = [[CIImage alloc] initWithBitmapImageRep: self];
if (before) {
filter = [CIFilter filterWithName: filterName];
[filter setDefaults];
if (arguments)
[filter setValuesForKeysWithDictionary: arguments];
[filter setValue: before forKey: @"inputImage"];
} else {
filter = nil;
}
after = [filter valueForKey: @"outputImage"];
if (after) {
if (![[arguments objectForKey: @"gt_noRenderPadding"] boolValue]) {
/* Add a wide berth to the bounds -- the padding can be turned
off by passing an NSNumber with a YES value in the argument
"gt_noRenderPadding" in the argument dictionary. */
fromRect.origin.x -= CIIMAGE_PADDING;
fromRect.origin.y -= CIIMAGE_PADDING;
fromRect.size.width += CIIMAGE_PADDING * 2.0f;
fromRect.size.height += CIIMAGE_PADDING * 2.0f;
point.x -= CIIMAGE_PADDING;
point.y -= CIIMAGE_PADDING;
}
cgContext = CGContextRetain((CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]);
if (cgContext) {
ciContext = [CIContext contextWithCGContext: cgContext options: nil];
[ciContext
drawImage: after
atPoint: *(CGPoint *)(&point)
fromRect: *(CGRect *)(&fromRect)];
CGContextRelease(cgContext);
}
}
} @catch (NSException *e) {
NSLog(@"exception encountered during core image filtering: %@", e);
} @finally {
[before release];
}
[pool release];
}
@end