Skip to content

Commit 6a4f462

Browse files
authored
Merge pull request #4566 from plotly/gl3d-to-image-with-alpha
Fix download images to match live gl3d plot
2 parents 7246537 + 2244c5e commit 6a4f462

29 files changed

+33
-11
lines changed

src/plots/gl3d/scene.js

+33-11
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,37 @@ proto.updateFx = function(dragmode, hovermode) {
10181018
scene.fullSceneLayout.hovermode = hovermode;
10191019
};
10201020

1021+
function flipPixels(pixels, w, h) {
1022+
for(var i = 0, q = h - 1; i < q; ++i, --q) {
1023+
for(var j = 0; j < w; ++j) {
1024+
for(var k = 0; k < 4; ++k) {
1025+
var a = 4 * (w * i + j) + k;
1026+
var b = 4 * (w * q + j) + k;
1027+
var tmp = pixels[a];
1028+
pixels[a] = pixels[b];
1029+
pixels[b] = tmp;
1030+
}
1031+
}
1032+
}
1033+
}
1034+
1035+
function correctRGB(pixels, w, h) {
1036+
for(var i = 0; i < h; ++i) {
1037+
for(var j = 0; j < w; ++j) {
1038+
var k = 4 * (w * i + j);
1039+
1040+
var a = pixels[k + 3]; // alpha
1041+
if(a > 0) {
1042+
var q = 255 / a;
1043+
1044+
for(var l = 0; l < 3; ++l) { // RGB
1045+
pixels[k + l] = Math.min(q * pixels[k + l], 255);
1046+
}
1047+
}
1048+
}
1049+
}
1050+
}
1051+
10211052
proto.toImage = function(format) {
10221053
var scene = this;
10231054

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

10371068
var pixels = new Uint8Array(w * h * 4);
10381069
gl.readPixels(0, 0, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
1039-
1040-
// Flip pixels
1041-
for(var j = 0, k = h - 1; j < k; ++j, --k) {
1042-
for(var i = 0; i < w; ++i) {
1043-
for(var l = 0; l < 4; ++l) {
1044-
var tmp = pixels[4 * (w * j + i) + l];
1045-
pixels[4 * (w * j + i) + l] = pixels[4 * (w * k + i) + l];
1046-
pixels[4 * (w * k + i) + l] = tmp;
1047-
}
1048-
}
1049-
}
1070+
flipPixels(pixels, w, h);
1071+
correctRGB(pixels, w, h);
10501072

10511073
var canvas = document.createElement('canvas');
10521074
canvas.width = w;
6.02 KB
Loading

test/image/baselines/gl3d_bunny.png

5.11 KB
Loading
1.33 KB
Loading
97 Bytes
Loading
5.11 KB
Loading

test/image/baselines/gl3d_cube.png

1.58 KB
Loading
Loading
Loading
4.08 KB
Loading
280 Bytes
Loading
Loading
Loading
-63 Bytes
Loading
19 Bytes
Loading
Loading
-796 Bytes
Loading
Loading
-254 Bytes
Loading
Loading
Loading
316 Bytes
Loading
1.48 KB
Loading
Loading
-295 Bytes
Loading
12.1 KB
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)