/
NSImage-kod.m
67 lines (56 loc) · 2.38 KB
/
NSImage-kod.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
// Copyright (c) 2010-2011, Rasmus Andersson. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
#import "NSImage-kod.h"
#import "CIImage-kod.h"
@implementation NSImage (kod)
+ (NSImage*)imageWithCIImage:(CIImage*)ciImage {
CGRect ciImageExtent = [ciImage extent];
NSImage *image = [[[NSImage alloc] initWithSize:
NSSizeFromCGSize(ciImageExtent.size)] autorelease];
[image lockFocus];
CIContext *ciContext = [[NSGraphicsContext currentContext] CIContext];
[ciContext drawImage:ciImage
atPoint:CGPointMake(0, 0)
fromRect:ciImageExtent];
[image unlockFocus];
return image;
/*
According to an old article
<http://inessential.com/2007/03/07/workaround_for_ciimage_to_nsimage_memory>
the above code might leak and the below code doesn't. But that was in an older
version of OS X. Might still be a good idea to investigate this.
NSImage *image = [[[NSImage alloc] initWithSize:
NSMakeSize([ciImage extent].size.width,
[ciImage extent].size.height)]
autorelease];
[image lockFocus];
CGContextRef contextRef = [[NSGraphicsContext currentContext] graphicsPort];
NSDictionary *options = [NSDictionary dictionaryWithObject:
[NSNumber numberWithBool:YES] forKey:kCIContextUseSoftwareRenderer];
CIContext *ciContext =
[CIContext contextWithCGContext:contextRef
options:options];
[ciContext drawImage:ciImage
atPoint:CGPointMake(0, 0) fromRect:[ciImage extent]];
// Note: Does not leak when using the software renderer. See
[image unlockFocus];
return image;*/
}
- (NSImage*)imageByApplyingCIFilterNamed:(NSString*)ciFilterName
filterParameters:(NSDictionary*)filterParameters {
CIImage* ciImage = [self ciImage];
//if ([image isFlipped])
// ciImage = [ciImage flippedImage];
ciImage = [ciImage imageByApplyingFilterNamed:ciFilterName
parameters:filterParameters];
return [NSImage imageWithCIImage:ciImage];
}
- (NSImage*)imageByApplyingCIFilterNamed:(NSString*)ciFilterName {
return [self imageByApplyingCIFilterNamed:ciFilterName
filterParameters:nil];
}
- (CIImage*)ciImage {
return [[[CIImage alloc] initWithData:[self TIFFRepresentation]] autorelease];
}
@end