Skip to content

Commit 7c96fee

Browse files
author
Darcy Liu
committed
add PocketCoreImage
1 parent c3a65a5 commit 7c96fee

24 files changed

Lines changed: 1362 additions & 1 deletion

PocketCoreImage.zip

96.9 KB
Binary file not shown.

PocketCoreImage/PocketCoreImage/PocketCoreImage.xcodeproj/project.pbxproj

Lines changed: 335 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
File: FilterDetailCategory.m
3+
Abstract: Extensions to MainViewController that handle configuring a CIFilter's parameters with
4+
random values.
5+
Version: 1.0
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2011 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
#import "MainViewController.h"
50+
51+
52+
@implementation MainViewController (FilterDetailCategory)
53+
54+
//
55+
// Helper method.
56+
// Returns an NSDictionary containg only the parameters we want our configure method
57+
// to operate on.
58+
+ (NSDictionary*)deriveEditableAttributesForFilter:(CIFilter*)filter
59+
{
60+
NSMutableDictionary *editableAttributes = [NSMutableDictionary dictionary];
61+
NSDictionary *filterAttributes = [filter attributes];
62+
63+
for (NSString *key in filterAttributes) {
64+
if ([key isEqualToString:@"CIAttributeFilterCategories"]) continue;
65+
else if ([key isEqualToString:@"CIAttributeFilterDisplayName"]) continue;
66+
else if ([key isEqualToString:@"inputImage"]) continue;
67+
else if ([key isEqualToString:@"outputImage"]) continue;
68+
else if (![[[filter attributes] objectForKey:key] isKindOfClass:[NSDictionary class]]) continue;
69+
70+
[editableAttributes setObject:[[filter attributes] objectForKey:key] forKey:key];
71+
}
72+
73+
return editableAttributes;
74+
}
75+
76+
//
77+
// Helper function that returns a random float value within the specified range.
78+
float randFloat(float a, float b);
79+
float randFloat(float a, float b)
80+
{
81+
srand(time(NULL));
82+
return ((b-a)*((float)arc4random()/RAND_MAX))+a;
83+
}
84+
85+
//
86+
// Given a filter, examine all its parameters and configure them with randomly generated values.
87+
+ (void)configureFilter:(CIFilter*)filter
88+
{
89+
// Get the filter's parameters we're interested in configuring here.
90+
NSDictionary *editableAttributes = [MainViewController deriveEditableAttributesForFilter:filter];
91+
92+
for (NSString *key in editableAttributes) {
93+
94+
NSDictionary *attributeDictionary = [editableAttributes objectForKey:key];
95+
96+
// Our method here only supports generating random values for parameters that expect numbers.
97+
// Some paramters take an image, color, or vector.
98+
if ([[attributeDictionary objectForKey:kCIAttributeClass] isEqualToString:@"NSNumber"]) {
99+
100+
// The number types are further broken down into sub types. For our purposes, we
101+
// can group them into types that require either a boolean, float, or integer.
102+
if ([attributeDictionary objectForKey:kCIAttributeType] == kCIAttributeTypeBoolean)
103+
{
104+
NSInteger randomValue = (rand() % 2);
105+
106+
NSLog(@"Setting %i for key %@ of type BOOL", randomValue, key);
107+
[filter setValue:[NSNumber numberWithInteger:randomValue] forKey:key];
108+
}
109+
else if([attributeDictionary objectForKey:kCIAttributeType] == kCIAttributeTypeScalar ||
110+
[attributeDictionary objectForKey:kCIAttributeType] == kCIAttributeTypeDistance ||
111+
[attributeDictionary objectForKey:kCIAttributeType] == kCIAttributeTypeAngle)
112+
{
113+
// Get the min and max values
114+
float maximumValue = [[attributeDictionary valueForKey:kCIAttributeSliderMax] floatValue];
115+
float minimumValue = [[attributeDictionary valueForKey:kCIAttributeSliderMin] floatValue];
116+
117+
float randomValue = randFloat(minimumValue, maximumValue);
118+
119+
NSLog(@"Setting %f for key %@ of type Decimal", randomValue, key);
120+
[filter setValue:[NSNumber numberWithFloat:randomValue] forKey:key];
121+
}
122+
else
123+
{
124+
// Get the min and max values
125+
NSInteger maximumValue = [[attributeDictionary valueForKey:kCIAttributeMax] integerValue];
126+
NSInteger minimumValue = [[attributeDictionary valueForKey:kCIAttributeMin] integerValue];
127+
128+
NSInteger randomValue = (rand() % (maximumValue - minimumValue)) + minimumValue;
129+
130+
NSLog(@"Setting %i for key %@ of type Integer", randomValue, key);
131+
[filter setValue:[NSNumber numberWithInteger:randomValue] forKey:key];
132+
}
133+
134+
}
135+
136+
}
137+
}
138+
139+
@end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
File: FilteredImageView.h
3+
Abstract: UIView subclass that renders a UIImage with a set of filters applied.
4+
The filters are requested from it's data source.
5+
Version: 1.0
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2011 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
#import <UIKit/UIKit.h>
50+
51+
@protocol FilteredViewDatasource <NSObject>
52+
53+
- (NSMutableArray*)filtersToApply;
54+
55+
@end
56+
57+
58+
59+
@interface FilteredImageView : UIView {
60+
CIImage *_filteredImage;
61+
}
62+
63+
@property (nonatomic, weak) IBOutlet id<FilteredViewDatasource> datasource;
64+
65+
@property (nonatomic, strong) UIImage *inputImage;
66+
67+
- (void)reloadData;
68+
69+
@end
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
File: FilteredImageView.m
3+
Abstract: UIView subclass that renders a UIImage with a set of filters applied.
4+
The filters are requested from it's data source.
5+
Version: 1.0
6+
7+
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple
8+
Inc. ("Apple") in consideration of your agreement to the following
9+
terms, and your use, installation, modification or redistribution of
10+
this Apple software constitutes acceptance of these terms. If you do
11+
not agree with these terms, please do not use, install, modify or
12+
redistribute this Apple software.
13+
14+
In consideration of your agreement to abide by the following terms, and
15+
subject to these terms, Apple grants you a personal, non-exclusive
16+
license, under Apple's copyrights in this original Apple software (the
17+
"Apple Software"), to use, reproduce, modify and redistribute the Apple
18+
Software, with or without modifications, in source and/or binary forms;
19+
provided that if you redistribute the Apple Software in its entirety and
20+
without modifications, you must retain this notice and the following
21+
text and disclaimers in all such redistributions of the Apple Software.
22+
Neither the name, trademarks, service marks or logos of Apple Inc. may
23+
be used to endorse or promote products derived from the Apple Software
24+
without specific prior written permission from Apple. Except as
25+
expressly stated in this notice, no other rights or licenses, express or
26+
implied, are granted by Apple herein, including but not limited to any
27+
patent rights that may be infringed by your derivative works or by other
28+
works in which the Apple Software may be incorporated.
29+
30+
The Apple Software is provided by Apple on an "AS IS" basis. APPLE
31+
MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
32+
THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
33+
FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
34+
OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
35+
36+
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
37+
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
39+
INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
40+
MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
41+
AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
42+
STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
43+
POSSIBILITY OF SUCH DAMAGE.
44+
45+
Copyright (C) 2011 Apple Inc. All Rights Reserved.
46+
47+
*/
48+
49+
#import "FilteredImageView.h"
50+
51+
@implementation FilteredImageView
52+
53+
@synthesize datasource;
54+
@synthesize inputImage = _inputImage;
55+
56+
//
57+
// Requests the list of filters from the data source and applies each filter
58+
// in order to the _inputImage.
59+
- (void)reloadData
60+
{
61+
if (!_inputImage)
62+
return;
63+
64+
// Create a CIImage from the _inputImage. While UIImage has a property returning
65+
// a CIImage representation of it, there are cases where it will not work. This is the
66+
// most compatible route.
67+
_filteredImage = [[CIImage alloc] initWithCGImage:_inputImage.CGImage options:nil];
68+
69+
// Retrieve the list of CIFilters to apply from our datasource.
70+
NSArray *filters = [self.datasource filtersToApply];
71+
if (filters)
72+
// Iterate through each filter setting our CIImage as the input and re-assigning
73+
// the filter's output back to our CIImage. This creates a chaining effect.
74+
for(CIFilter *filter in filters) {
75+
[filter setValue:_filteredImage forKey:@"inputImage"];
76+
// Certain filters place restrictions on their input values that we may not have accounted for
77+
// in the configuration method. For example, CIColorCube requires its parameter to be a power
78+
// of 2. In such as case, the filter will throw an exception when we ask it generate and image.
79+
// Catch the exception and pretend nothing happened thereby bypassing the filter.
80+
@try {
81+
_filteredImage = filter.outputImage;
82+
}
83+
@catch (NSException* e) { }
84+
}
85+
86+
// Inform UIKit that we need to be redrawn.
87+
[self setNeedsDisplay];
88+
}
89+
90+
- (void)drawRect:(CGRect)rect
91+
{
92+
[super drawRect:rect];
93+
94+
if (!_filteredImage)
95+
return;
96+
97+
// This is the rect we'll draw our final image into. By making it a bit smaller than our bounds
98+
// we'll get a nice border.
99+
CGRect innerBounds = CGRectMake(5, 5, self.bounds.size.width - 10, self.bounds.size.height - 10);
100+
101+
// To display the image, convert it back to a UIImage and draw it in our rect. UIImage takes
102+
// into account the orientation of an image when drawing which we would have needed to worry about
103+
// when drawing it directly with Core Image and Core Graphics calls.
104+
[[UIImage imageWithCIImage:_filteredImage] drawInRect:innerBounds];
105+
}
106+
107+
- (void)setInputImage:(UIImage *)inputImage
108+
{
109+
// Since Core Image filters must be operate on every pixel in an image, you may want to
110+
// consider resizing an input image to the view size before applying any filters.
111+
//
112+
113+
_inputImage = inputImage;
114+
115+
[self reloadData];
116+
}
117+
118+
@end

0 commit comments

Comments
 (0)