Skip to content

Commit

Permalink
Merge pull request #4566 from plotly/gl3d-to-image-with-alpha
Browse files Browse the repository at this point in the history
Fix download images to match live gl3d plot
  • Loading branch information
archmoj authored Mar 10, 2020
2 parents 7246537 + 2244c5e commit 6a4f462
Show file tree
Hide file tree
Showing 29 changed files with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,37 @@ proto.updateFx = function(dragmode, hovermode) {
scene.fullSceneLayout.hovermode = hovermode;
};

function flipPixels(pixels, w, h) {
for(var i = 0, q = h - 1; i < q; ++i, --q) {
for(var j = 0; j < w; ++j) {
for(var k = 0; k < 4; ++k) {
var a = 4 * (w * i + j) + k;
var b = 4 * (w * q + j) + k;
var tmp = pixels[a];
pixels[a] = pixels[b];
pixels[b] = tmp;
}
}
}
}

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);
}
}
}
}
}

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

Expand All @@ -1036,17 +1067,8 @@ proto.toImage = function(format) {

var pixels = new Uint8Array(w * h * 4);
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels);

// Flip pixels
for(var j = 0, k = h - 1; j < k; ++j, --k) {
for(var i = 0; i < w; ++i) {
for(var l = 0; l < 4; ++l) {
var tmp = pixels[4 * (w * j + i) + l];
pixels[4 * (w * j + i) + l] = pixels[4 * (w * k + i) + l];
pixels[4 * (w * k + i) + l] = tmp;
}
}
}
flipPixels(pixels, w, h);
correctRGB(pixels, w, h);

var canvas = document.createElement('canvas');
canvas.width = w;
Expand Down
Binary file modified test/image/baselines/gl3d_bunny-hull.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_bunny.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_coloraxes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_cone-lighting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_convex-hull.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_cube.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_directions-streamtube1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_directions-streamtube2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_directions-volume1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_indicator_scatter3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_isosurface_thin_slices_transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_isosurface_transparent.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_marker-arrays.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_marker-color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_opacity-scaling-spikes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_rgb_dont_accept_alpha_scatter3d.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_scatter3d-colorscale.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_surface-lighting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_traces-with-legend.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_traces-with-opacity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_transparent_same-depth.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_volume_airflow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/gl3d_volume_multiple-traces.png
Binary file modified test/image/baselines/gl3d_volume_multiple-traces_one-cube.png
Binary file modified test/image/baselines/gl3d_volume_opacityscale-iso.png

0 comments on commit 6a4f462

Please sign in to comment.