Skip to content

Commit f58d6d6

Browse files
committed
Fixed image resize algorithm bug
This patch fixes a bug in the resize algorithm that "eats" some lines, both horizontal and vertical. To see the actual bug just try to resize to 800x600 this 4000x3000 test pattern image https://docs.google.com/file/d/0B9gDTEhcnnOVMzhpZDZrMWQtZDg/edit?usp=sharing You should find the artefacts I've highlighted in testpattern_highlight.jpg Just to know, the bug was born because the renderImageToCanvas method renders a large HTML5 canvas picture splitting it in 1Mpixels blocks, but the junction between one block and another was made in each cycle with "ceil" & "floor" functions, that introduce a non-linear behaviour in resize algorithm which sometimes would result in skipped pixel lines inside the final image. The patch I've written tries to calculate the best scaling factor before entering the main 'while' cycle of a single 'tile' and then it uses it as an unique scaling factor for all image blocks. This should result in perfect junctions between all blocks inside the image with just (in worst cases) a tiny part of the original image cut off from the right and lower borders.
1 parent a8a96e4 commit f58d6d6

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

src/megapix-image.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,22 @@
8686
tmpCanvas.width = tmpCanvas.height = d;
8787
var tmpCtx = tmpCanvas.getContext('2d');
8888
var vertSquashRatio = detectVerticalSquash(img, iw, ih);
89+
var dw = Math.ceil(d * width / iw);
90+
var dh = Math.ceil(d * height / ih / vertSquashRatio);
8991
var sy = 0;
92+
var dy = 0;
9093
while (sy < ih) {
91-
var sh = sy + d > ih ? ih - sy : d;
9294
var sx = 0;
95+
var dx = 0;
9396
while (sx < iw) {
94-
var sw = sx + d > iw ? iw - sx : d;
9597
tmpCtx.clearRect(0, 0, d, d);
9698
tmpCtx.drawImage(img, -sx, -sy);
97-
var dx = (sx * width / iw) << 0;
98-
var dw = Math.ceil(sw * width / iw);
99-
var dy = (sy * height / ih / vertSquashRatio) << 0;
100-
var dh = Math.ceil(sh * height / ih / vertSquashRatio);
101-
ctx.drawImage(tmpCanvas, 0, 0, sw, sh, dx, dy, dw, dh);
99+
ctx.drawImage(tmpCanvas, 0, 0, d, d, dx, dy, dw, dh);
102100
sx += d;
101+
dx += dw;
103102
}
104103
sy += d;
104+
dy += dh;
105105
}
106106
ctx.restore();
107107
tmpCanvas = tmpCtx = null;

0 commit comments

Comments
 (0)