developer
2023-05-20 e12c7b4c22df631ebdcd16b2f98fbef8f738f92f
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#import "GPUImageThresholdEdgeDetectionFilter.h"
 
@implementation GPUImageThresholdEdgeDetectionFilter
 
// Invert the colorspace for a sketch
#if TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE
NSString *const kGPUImageThresholdEdgeDetectionFragmentShaderString = SHADER_STRING
(
 precision highp float;
 
 varying vec2 textureCoordinate;
 varying vec2 leftTextureCoordinate;
 varying vec2 rightTextureCoordinate;
 
 varying vec2 topTextureCoordinate;
 varying vec2 topLeftTextureCoordinate;
 varying vec2 topRightTextureCoordinate;
 
 varying vec2 bottomTextureCoordinate;
 varying vec2 bottomLeftTextureCoordinate;
 varying vec2 bottomRightTextureCoordinate;
 
 uniform sampler2D inputImageTexture;
 uniform lowp float threshold;
 
 uniform float edgeStrength;
 
 void main()
 {
//     float bottomLeftIntensity = texture2D(inputImageTexture, bottomLeftTextureCoordinate).r;
//     float topRightIntensity = texture2D(inputImageTexture, topRightTextureCoordinate).r;
//     float topLeftIntensity = texture2D(inputImageTexture, topLeftTextureCoordinate).r;
//     float bottomRightIntensity = texture2D(inputImageTexture, bottomRightTextureCoordinate).r;
     float leftIntensity = texture2D(inputImageTexture, leftTextureCoordinate).r;
     float rightIntensity = texture2D(inputImageTexture, rightTextureCoordinate).r;
     float bottomIntensity = texture2D(inputImageTexture, bottomTextureCoordinate).r;
     float topIntensity = texture2D(inputImageTexture, topTextureCoordinate).r;
     float centerIntensity = texture2D(inputImageTexture, textureCoordinate).r;
//     float h = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;
//     float v = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;
//     float h = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + leftIntensity + 2.0 * centerIntensity + rightIntensity;
//     float v = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomIntensity + 2.0 * centerIntensity + topIntensity;
     float h = (centerIntensity - topIntensity) + (bottomIntensity - centerIntensity);
     float v = (centerIntensity - leftIntensity) + (rightIntensity - centerIntensity);
//     float h = (centerIntensity - topIntensity);
//     float j = (topIntensity - centerIntensity);
//     h = max(h,j);
//     j = abs(h);
//     float v = (centerIntensity - leftIntensity);
     
    float mag = length(vec2(h, v)) * edgeStrength;
     mag = step(threshold, mag);
     
//     float mag = abs(h);
     
//     gl_FragColor = vec4(h, h, h, 1.0);
//     gl_FragColor = vec4(texture2D(inputImageTexture, textureCoordinate));
//     gl_FragColor = vec4(h, centerIntensity, j, 1.0);
     gl_FragColor = vec4(mag, mag, mag, 1.0);
 }
);
#else
NSString *const kGPUImageThresholdEdgeDetectionFragmentShaderString = SHADER_STRING
(
 varying vec2 textureCoordinate;
 varying vec2 leftTextureCoordinate;
 varying vec2 rightTextureCoordinate;
 
 varying vec2 topTextureCoordinate;
 varying vec2 topLeftTextureCoordinate;
 varying vec2 topRightTextureCoordinate;
 
 varying vec2 bottomTextureCoordinate;
 varying vec2 bottomLeftTextureCoordinate;
 varying vec2 bottomRightTextureCoordinate;
 
 uniform sampler2D inputImageTexture;
 uniform float threshold;
 
 uniform float edgeStrength;
 
 void main()
 {
     float bottomLeftIntensity = texture2D(inputImageTexture, bottomLeftTextureCoordinate).r;
     float topRightIntensity = texture2D(inputImageTexture, topRightTextureCoordinate).r;
     float topLeftIntensity = texture2D(inputImageTexture, topLeftTextureCoordinate).r;
     float bottomRightIntensity = texture2D(inputImageTexture, bottomRightTextureCoordinate).r;
     float leftIntensity = texture2D(inputImageTexture, leftTextureCoordinate).r;
     float rightIntensity = texture2D(inputImageTexture, rightTextureCoordinate).r;
     float bottomIntensity = texture2D(inputImageTexture, bottomTextureCoordinate).r;
     float topIntensity = texture2D(inputImageTexture, topTextureCoordinate).r;
     float h = -topLeftIntensity - 2.0 * topIntensity - topRightIntensity + bottomLeftIntensity + 2.0 * bottomIntensity + bottomRightIntensity;
     h = max(0.0, h);
     float v = -bottomLeftIntensity - 2.0 * leftIntensity - topLeftIntensity + bottomRightIntensity + 2.0 * rightIntensity + topRightIntensity;
     v = max(0.0, v);
     
     float mag = length(vec2(h, v)) * edgeStrength;
     mag = step(threshold, mag);
     
     gl_FragColor = vec4(vec3(mag), 1.0);
 }
);
#endif
 
#pragma mark -
#pragma mark Initialization and teardown
 
@synthesize threshold = _threshold;
 
- (id)initWithFragmentShaderFromString:(NSString *)fragmentShaderString;
{
    if (!(self = [super initWithFragmentShaderFromString:fragmentShaderString]))
    {
        return nil;
    }
    
    thresholdUniform = [secondFilterProgram uniformIndex:@"threshold"];
    self.threshold = 0.25;
    self.edgeStrength = 1.0;
    
    return self;
}
 
 
- (id)init;
{
    if (!(self = [self initWithFragmentShaderFromString:kGPUImageThresholdEdgeDetectionFragmentShaderString]))
    {
        return nil;
    }
    
    return self;
}
 
#pragma mark -
#pragma mark Accessors
 
- (void)setThreshold:(CGFloat)newValue;
{
    _threshold = newValue;
    
    [self setFloat:_threshold forUniform:thresholdUniform program:secondFilterProgram];
}
 
@end