Skip to content

Commit ac88d0e

Browse files
committed
Merge pull request BradLarson#1024 from jjxtra/master
Solid color generator filter alpha option
2 parents 4d9f930 + 4907ea7 commit ac88d0e

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
lines changed

framework/Source/GPUImageSolidColorGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
@interface GPUImageSolidColorGenerator : GPUImageFilter
88
{
99
GLint colorUniform;
10+
GLint useExistingAlphaUniform;
1011
}
1112

1213
// This color dictates what the output image will be filled with
1314
@property(readwrite, nonatomic) GPUVector4 color;
15+
@property(readwrite, nonatomic, assign) BOOL useExistingAlpha; // whether to use the alpha of the existing image or not, default is NO
1416

1517
- (void)setColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GLfloat)blueComponent alpha:(GLfloat)alphaComponent;
1618

framework/Source/GPUImageSolidColorGenerator.m

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,32 @@
44
NSString *const kGPUSolidColorFragmentShaderString = SHADER_STRING
55
(
66
precision lowp float;
7-
7+
88
varying highp vec2 textureCoordinate;
9-
109
uniform sampler2D inputImageTexture;
1110
uniform vec4 color;
12-
11+
uniform float useExistingAlpha;
12+
1313
void main()
1414
{
15-
gl_FragColor = color;
15+
lowp vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
16+
gl_FragColor = vec4(color.rgb, max(textureColor.a, 1.0 - useExistingAlpha));
1617
}
17-
);
18+
);
1819
#else
1920
NSString *const kGPUSolidColorFragmentShaderString = SHADER_STRING
2021
(
2122
varying vec2 textureCoordinate;
22-
2323
uniform sampler2D inputImageTexture;
2424
uniform vec4 color;
25-
25+
uniform float useExistingAlpha;
26+
2627
void main()
2728
{
28-
gl_FragColor = color;
29+
vec4 textureColor = texture2D(inputImageTexture, textureCoordinate);
30+
gl_FragColor = vec4(color.rgb, max(textureColor.a, 1.0 - useExistingAlpha));
2931
}
30-
);
32+
);
3133
#endif
3234

3335
@implementation GPUImageSolidColorGenerator
@@ -42,8 +44,10 @@ - (id)init;
4244
}
4345

4446
colorUniform = [filterProgram uniformIndex:@"color"];
47+
useExistingAlphaUniform = [filterProgram uniformIndex:@"useExistingAlpha"];
4548

4649
self.color = (GPUVector4){0.0f, 0.0f, 0.5f, 1.0f};
50+
self.useExistingAlpha = NO;
4751

4852
return self;
4953
}
@@ -65,7 +69,7 @@ - (void)forceProcessingAtSize:(CGSize)frameSize;
6569
- (void)addTarget:(id<GPUImageInput>)newTarget atTextureLocation:(NSInteger)textureLocation;
6670
{
6771
[super addTarget:newTarget atTextureLocation:textureLocation];
68-
72+
6973
if (!CGSizeEqualToSize(inputTextureSize, CGSizeZero))
7074
{
7175
[newTarget setInputSize:inputTextureSize atIndex:textureLocation];
@@ -86,11 +90,13 @@ - (void)setColorRed:(GLfloat)redComponent green:(GLfloat)greenComponent blue:(GL
8690
_color.four = alphaComponent;
8791

8892
[self setVec4:_color forUniform:colorUniform program:filterProgram];
89-
90-
if (!CGSizeEqualToSize(inputTextureSize, CGSizeZero))
91-
{
92-
[self newFrameReadyAtTime:kCMTimeIndefinite atIndex:0];
93-
}
93+
}
94+
95+
- (void)setUseExistingAlpha:(BOOL)useExistingAlpha;
96+
{
97+
_useExistingAlpha = useExistingAlpha;
98+
99+
[self setInteger:(useExistingAlpha ? 1 : 0) forUniform:useExistingAlphaUniform program:filterProgram];
94100
}
95101

96102
@end

0 commit comments

Comments
 (0)