This repository was archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathYUCITriangularPixellate.m
executable file
·77 lines (66 loc) · 2.48 KB
/
YUCITriangularPixellate.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
68
69
70
71
72
73
74
75
76
77
//
// YUCITriangularPixellate.m
// Pods
//
// Created by YuAo on 2/9/16.
//
//
#import "YUCITriangularPixellate.h"
#import "YUCIFilterConstructor.h"
@implementation YUCITriangularPixellate
+ (void)load {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
@autoreleasepool {
if ([CIFilter respondsToSelector:@selector(registerFilterName:constructor:classAttributes:)]) {
[CIFilter registerFilterName:NSStringFromClass([YUCITriangularPixellate class])
constructor:[YUCIFilterConstructor constructor]
classAttributes:@{kCIAttributeFilterCategories: @[kCICategoryStillImage,kCICategoryVideo,kCICategoryStylize],
kCIAttributeFilterDisplayName: @"Triangular Pixellate"}];
}
}
});
}
+ (CIKernel *)filterKernel {
static CIKernel *kernel;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *kernelString = [[NSString alloc] initWithContentsOfURL:[[NSBundle bundleForClass:self] URLForResource:NSStringFromClass([YUCITriangularPixellate class]) withExtension:@"cikernel"] encoding:NSUTF8StringEncoding error:nil];
kernel = [CIKernel kernelWithString:kernelString];
});
return kernel;
}
- (NSNumber *)inputScale {
if (!_inputScale) {
_inputScale = @(20);
}
return _inputScale;
}
- (NSNumber *)inputVertexAngle {
if (!_inputVertexAngle) {
_inputVertexAngle = @(M_PI/2.0);
}
return _inputVertexAngle;
}
- (CIVector *)inputCenter {
if (!_inputCenter) {
_inputCenter = [CIVector vectorWithX:0 Y:0];
}
return _inputCenter;
}
- (void)setDefaults {
self.inputScale = nil;
self.inputVertexAngle = nil;
self.inputCenter = nil;
}
- (CIImage *)outputImage {
if (!self.inputImage) {
return nil;
}
CGFloat tanHalfInputAngle = tan(self.inputVertexAngle.floatValue/2.0);
return [[YUCITriangularPixellate filterKernel] applyWithExtent:self.inputImage.extent
roiCallback:^CGRect(int index, CGRect destRect) {
return CGRectInset(destRect, -self.inputScale.floatValue, -self.inputScale.floatValue/2.0 * tanHalfInputAngle);
} arguments:@[self.inputImage.imageByClampingToExtent,self.inputCenter,self.inputScale,@(tanHalfInputAngle)]];
}
@end