forked from card-io/card.io-iOS-source
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCardIOGPUFilter.m
105 lines (84 loc) · 2.81 KB
/
CardIOGPUFilter.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// CardIOGPUFilter.m
// See the file "LICENSE.md" for the full license governing this code.
//
#import "CardIOGPUFilter.h"
@implementation CardIOGPUFilter
#pragma mark Utility classmethods
- (id)initWithSize:(CGSize)size vertexShaderSrc:(NSString *)vertexShaderSrc fragmentShaderSrc:(NSString *)fragmentShaderSrc;
{
if((self = [super init])) {
// Create GPUImageView, which actually handles the processing
_gpuRenderer = [[CardIOGPURenderer alloc] initWithSize:size
vertexShaderSrc:vertexShaderSrc
fragmentShaderSrc:fragmentShaderSrc];
if (!_gpuRenderer) {
self = nil;
}
}
return self;
}
- (CGSize)size {
return _gpuRenderer.size;
}
- (void)finish {
[_gpuRenderer finish];
}
// Process images
- (UIImage *)processUIImage:(UIImage *)srcUIImage toSize:(const CGSize)size {
[_gpuRenderer renderUIImage:srcUIImage toSize:size];
return [_gpuRenderer captureUIImageOfSize:size];
}
#if USE_CAMERA
- (UIImage *)processIplToUIImage:(IplImage *)srcImg toSize:(const CGSize)size {
[_gpuRenderer renderIplImage:srcImg toSize:(CGSize)size];
return [_gpuRenderer captureUIImageOfSize:size];
}
- (void)processIplImage:(IplImage *)srcImg dstIplImg:(IplImage *)dstImg {
[_gpuRenderer renderIplImage:srcImg toSize:CGSizeMake(dstImg->width, dstImg->height)];
[_gpuRenderer captureIplImage:dstImg];
}
#endif // USE_CAMERA
#pragma mark Matrices
+ (void)loadIdentityMatrix:(GLfloat *)matrix size:(GLuint)size {
for (int i = 0; i < size * size; i++) {
matrix[i] = (i % (size + 1) == 0) ? 1 : 0;
}
}
// Brad Larson's BSD-licensed work on this is interesting. Check it out.
+ (void)loadOrthoMatrix:(GLfloat *)matrix left:(GLfloat)left right:(GLfloat)right bottom:(GLfloat)bottom top:(GLfloat)top near:(GLfloat)near far:(GLfloat)far {
GLfloat r_l = right - left;
GLfloat t_b = top - bottom;
GLfloat f_n = far - near;
GLfloat tx = - (right + left) / (right - left);
GLfloat ty = - (top + bottom) / (top - bottom);
GLfloat tz = - (far + near) / (far - near);
matrix[0] = 2.0f / r_l;
matrix[1] = 0.0f;
matrix[2] = 0.0f;
matrix[3] = tx;
matrix[4] = 0.0f;
matrix[5] = 2.0f / t_b;
matrix[6] = 0.0f;
matrix[7] = ty;
matrix[8] = 0.0f;
matrix[9] = 0.0f;
matrix[10] = 2.0f / f_n;
matrix[11] = tz;
matrix[12] = 0.0f;
matrix[13] = 0.0f;
matrix[14] = 0.0f;
matrix[15] = 1.0f;
}
+ (NSString *)matrixAsString:(GLfloat *)matrix size:(GLuint)size rowMajor:(BOOL)rowMajor {
NSString *ret = [NSString string];
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
int index = rowMajor ? (i * size + j) : j * size + i;
ret = [ret stringByAppendingString:[NSString stringWithFormat:@"%.2f ", matrix[index]]];
}
ret = [ret stringByAppendingString:@"\n"];
}
return ret;
}
@end