Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix download images to match live gl3d plot #4566

Merged
merged 5 commits into from
Mar 10, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,6 +1003,23 @@ function flipPixels(pixels, w, h) {
}
}

function correctRGB(pixels, w, h) {
for(var i = 0; i < h; ++i) {
for(var j = 0; j < w; ++j) {
var k = 4 * (w * i + j);

var a = pixels[k + 3]; // alpha
if(a > 0) {
var q = 255 / a;

for(var l = 0; l < 3; ++l) { // RGB
pixels[k + l] = Math.min(q * pixels[k + l], 255);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can explain why we need to apply this transform when exporting images?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlike openGL, webGL premultipliedAlpha in enabled by default.
To get correct RGB values from the webgl context it was needed to devide RGB values by alpha.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, what would happen if we instead disable premultipliedAlpha on image exports?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question; but that didn't work: demo.

}
}
}
}
}

proto.toImage = function(format) {
var scene = this;

Expand All @@ -1022,6 +1039,7 @@ proto.toImage = function(format) {
var pixels = new Uint8Array(w * h * 4);
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
flipPixels(pixels, w, h);
correctRGB(pixels, w, h);

var canvas = document.createElement('canvas');
canvas.width = w;
Expand Down