|
| 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 |
0 commit comments