Skip to content

Commit 49fcf22

Browse files
committed
Removed a divide-by-zero error in the soft light blend, caused by accounting for premultiplied alpha.
1 parent de7c1a1 commit 49fcf22

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

framework/Source/GPUImageSoftLightBlendFilter.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ void main()
1414
mediump vec4 base = texture2D(inputImageTexture, textureCoordinate);
1515
mediump vec4 overlay = texture2D(inputImageTexture2, textureCoordinate2);
1616

17-
gl_FragColor = base * (overlay.a * (base / base.a) + (2.0 * overlay * (1.0 - (base / base.a)))) + overlay * (1.0 - base.a) + base * (1.0 - overlay.a);
17+
lowp float alphaDivisor = base.a + step(base.a, 0.0); // Protect against a divide-by-zero blacking out things in the output
18+
gl_FragColor = base * (overlay.a * (base / alphaDivisor) + (2.0 * overlay * (1.0 - (base / alphaDivisor)))) + overlay * (1.0 - base.a) + base * (1.0 - overlay.a);
1819
}
1920
);
2021
#else
@@ -31,7 +32,8 @@ void main()
3132
vec4 base = texture2D(inputImageTexture, textureCoordinate);
3233
vec4 overlay = texture2D(inputImageTexture2, textureCoordinate2);
3334

34-
gl_FragColor = base * (overlay.a * (base / base.a) + (2.0 * overlay * (1.0 - (base / base.a)))) + overlay * (1.0 - base.a) + base * (1.0 - overlay.a);
35+
float alphaDivisor = base.a + step(base.a, 0.0); // Protect against a divide-by-zero blacking out things in the output
36+
gl_FragColor = base * (overlay.a * (base / alphaDivisor) + (2.0 * overlay * (1.0 - (base / alphaDivisor)))) + overlay * (1.0 - base.a) + base * (1.0 - overlay.a);
3537
}
3638
);
3739
#endif

0 commit comments

Comments
 (0)