|
| 1 | +// From http://www.cocoadev.com/index.pl?NSImageCategory |
| 2 | +// NSBitmapImageRep+CoreImage.m |
| 3 | +// iTerm2 |
| 4 | + |
| 5 | +#import <QuartzCore/QuartzCore.h> |
| 6 | +#import "NSBitmapImageRep+CoreImage.h" |
| 7 | +#import "NSImage+CoreImage.h" |
| 8 | + |
| 9 | +#define CIIMAGE_PADDING 16.0f |
| 10 | + |
| 11 | +@implementation NSBitmapImageRep (CoreImage) |
| 12 | + |
| 13 | +- (void)drawAtPoint: (NSPoint)point fromRect: (NSRect)fromRect coreImageFilter: (NSString *)filterName arguments: (NSDictionary *)arguments { |
| 14 | + NSAutoreleasePool *pool; |
| 15 | + CIFilter *filter; |
| 16 | + CIImage *before; |
| 17 | + CIImage *after; |
| 18 | + CIContext *ciContext; |
| 19 | + CGContextRef cgContext; |
| 20 | + |
| 21 | + pool = [[NSAutoreleasePool alloc] init]; |
| 22 | + before = nil; |
| 23 | + |
| 24 | + @try { |
| 25 | + before = [[CIImage alloc] initWithBitmapImageRep: self]; |
| 26 | + if (before) { |
| 27 | + filter = [CIFilter filterWithName: filterName]; |
| 28 | + [filter setDefaults]; |
| 29 | + if (arguments) |
| 30 | + [filter setValuesForKeysWithDictionary: arguments]; |
| 31 | + [filter setValue: before forKey: @"inputImage"]; |
| 32 | + } else { |
| 33 | + filter = nil; |
| 34 | + } |
| 35 | + |
| 36 | + after = [filter valueForKey: @"outputImage"]; |
| 37 | + if (after) { |
| 38 | + if (![[arguments objectForKey: @"gt_noRenderPadding"] boolValue]) { |
| 39 | + /* Add a wide berth to the bounds -- the padding can be turned |
| 40 | + off by passing an NSNumber with a YES value in the argument |
| 41 | + "gt_noRenderPadding" in the argument dictionary. */ |
| 42 | + fromRect.origin.x -= CIIMAGE_PADDING; |
| 43 | + fromRect.origin.y -= CIIMAGE_PADDING; |
| 44 | + fromRect.size.width += CIIMAGE_PADDING * 2.0f; |
| 45 | + fromRect.size.height += CIIMAGE_PADDING * 2.0f; |
| 46 | + point.x -= CIIMAGE_PADDING; |
| 47 | + point.y -= CIIMAGE_PADDING; |
| 48 | + } |
| 49 | + |
| 50 | + cgContext = CGContextRetain((CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]); |
| 51 | + if (cgContext) { |
| 52 | + ciContext = [CIContext contextWithCGContext: cgContext options: nil]; |
| 53 | + [ciContext |
| 54 | + drawImage: after |
| 55 | + atPoint: *(CGPoint *)(&point) |
| 56 | + fromRect: *(CGRect *)(&fromRect)]; |
| 57 | + CGContextRelease(cgContext); |
| 58 | + } |
| 59 | + } |
| 60 | + } @catch (NSException *e) { |
| 61 | + NSLog("exception encountered during core image filtering: %@", e); |
| 62 | + } @finally { |
| 63 | + [before release]; |
| 64 | + } |
| 65 | + |
| 66 | + [pool release]; |
| 67 | +} |
| 68 | +@end |
0 commit comments