Skip to content

Commit

Permalink
Merge pull request #6529 from davepagurek/fix/opaque
Browse files Browse the repository at this point in the history
Handle premultiplied alpha in filter(OPAQUE)
  • Loading branch information
Qianqianye authored Nov 23, 2023
2 parents 08d3e4b + 6e42b73 commit bfb5b9b
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 6 deletions.
1 change: 0 additions & 1 deletion src/image/pixels.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ p5.prototype._copyHelper = (
dw,
dh
) => {
srcImage.loadPixels();
const s = srcImage.canvas.width / srcImage.width;
// adjust coord system for 3D when renderer
// ie top-left = -width/2, -height/2
Expand Down
2 changes: 1 addition & 1 deletion src/webgl/shaders/filters/opaque.frag
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ uniform sampler2D tex0;

void main() {
vec4 color = texture2D(tex0, vTexCoord);
gl_FragColor = vec4(color.rgb, 1.0);
gl_FragColor = vec4(color.rgb / color.a, 1.0);
}
4 changes: 2 additions & 2 deletions src/webgl/shaders/filters/posterize.frag
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ vec3 quantize(vec3 color, float n) {
void main() {
vec4 color = texture2D(tex0, vTexCoord);

vec3 restrictedColor = quantize(color.rgb, filterParameter);
vec3 restrictedColor = quantize(color.rgb / color.a, filterParameter);

gl_FragColor = vec4(restrictedColor.rgb, color.a);
gl_FragColor = vec4(restrictedColor.rgb * color.a, color.a);
}
4 changes: 2 additions & 2 deletions src/webgl/shaders/filters/threshold.frag
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ float luma(vec3 color) {

void main() {
vec4 color = texture2D(tex0, vTexCoord);
float gray = luma(color.rgb);
float gray = luma(color.rgb / color.a);
// floor() used to match src/image/filters.js
float threshold = floor(filterParameter * 255.0) / 255.0;
float blackOrWhite = step(threshold, gray);
gl_FragColor = vec4(vec3(blackOrWhite), color.a);
gl_FragColor = vec4(vec3(blackOrWhite) * color.a, color.a);
}

0 comments on commit bfb5b9b

Please sign in to comment.