OSDN Git Service

0.2.1
[eliscolors/main.git] / ElisCustomFilter.m
1 //  Copyright (c) 2009 Yanagi Asakura
2 //
3 //  This software is provided 'as-is', without any express or implied
4 //  warranty. In no event will the authors be held liable for any damages
5 //  arising from the use of this software.
6 //
7 //  Permission is granted to anyone to use this software for any purpose,
8 //  including commercial applications, and to alter it and redistribute it
9 //  freely, subject to the following restrictions:
10 //
11 //  1. The origin of this software must not be misrepresented; you must not
12 //  claim that you wrote the original software. If you use this software
13 //  in a product, an acknowledgment in the product documentation would be
14 //  appreciated but is not required.
15 //
16 //  2. Altered source versions must be plainly marked as such, and must not be
17 //  misrepresented as being the original software.
18 //
19 //  3. This notice may not be removed or altered from any source
20 //  distribution.
21
22 //
23 //  ElisCustomFilter.m
24 //  Elis
25 //
26 //  Created by 柳 on 09/09/02.
27 //  Copyright 2009 Yanagi Entertainment. All rights reserved.
28 //
29
30 #import "ElisCustomFilter.h"
31
32 @implementation ElisCustomFilter
33
34 static CIKernel* s_opacityKernel = nil; 
35
36 - (id)init
37 {
38     if(s_opacityKernel == nil){
39 //        NSBundle *bundle = [NSBundle bundleForClass:[self class]];
40         NSBundle* bundle = [NSBundle mainBundle];
41         NSString *code = [NSString stringWithContentsOfFile:[bundle pathForResource:@"ElisCoreImageKernel" ofType:@"cikernel"]];
42         NSLog(code);
43         NSArray *kernels = [CIKernel kernelsWithString:code];
44         s_opacityKernel = [[kernels objectAtIndex:0] retain];
45     }
46     
47     return [super init];
48 }
49
50 + (void)initialize
51 {
52     NSLog(@"Initializing Custom Filter ...");
53     
54     [CIFilter registerFilterName:@"CHOpacity"
55                      constructor:self
56                  classAttributes:[NSDictionary dictionaryWithObjectsAndKeys:@"Change", kCIAttributeFilterDisplayName,
57                                   [NSArray arrayWithObjects:kCICategoryColorAdjustment, kCICategoryVideo, kCICategoryStillImage,
58                                    kCICategoryNonSquarePixels, nil], kCIAttributeFilterCategories, nil]];
59 //                                  [NSDictionary dictionaryWithObjectsAndKeys:
60 //                                   [NSNumber numberWithDouble:0.0], kCIAttributeMin,
61 //                                   [NSNumber numberWithDouble:1.0], kCIAttributeMax,
62 //                                   [NSNumber numberWithDouble:0.0], kCIAttributeSliderMin,
63 //                                   [NSNumber numberWithDouble:1.0], kCIAttributeSliderMax,
64 //                                   [NSNumber numberWithDouble:1.0], kCIAttributeDefault,
65 //                                   [NSNumber numberWithDouble:1.0], kCIAttributeIdentity,
66 //                                   kCIAttributeTypeScalar, kCIAttributeType, nil], @"inputOpacity", nil]];
67 }
68      
69 - (NSDictionary*)customAttributes
70 {
71     return [NSDictionary dictionaryWithObjectsAndKeys:
72             [NSDictionary dictionaryWithObjectsAndKeys:
73              [NSNumber numberWithDouble:0.0], kCIAttributeMin,
74              [NSNumber numberWithDouble:1.0], kCIAttributeMax,
75              [NSNumber numberWithDouble:0.0], kCIAttributeSliderMin,
76              [NSNumber numberWithDouble:1.0], kCIAttributeSliderMax,
77              [NSNumber numberWithDouble:1.0], kCIAttributeDefault,
78              [NSNumber numberWithDouble:1.0], kCIAttributeIdentity,
79              kCIAttributeTypeScalar, kCIAttributeType, nil], @"inputOpacity", nil];
80 }
81
82 - (CIImage*)outputImage
83 {
84     CISampler *src = [CISampler samplerWithImage:inputImage];
85     
86     return [self apply:s_opacityKernel, src, inputOpacity, kCIApplyOptionDefinition, [src definition], nil];
87 }
88
89 + (CIFilter*)filterWithName:(NSString *)name
90 {
91     CIFilter *filter;
92     
93     filter = [[self alloc] init];
94     return [filter autorelease];
95 }
96
97 @end