From 32c32c18e3e84d93f7dad7c4cbf1055ba49711b8 Mon Sep 17 00:00:00 2001 From: fkaelberer Date: Fri, 2 Jan 2015 15:40:30 +0100 Subject: [PATCH] Better thumbnail image scaling --- web/thumbnail_view.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/web/thumbnail_view.js b/web/thumbnail_view.js index 1e16a7de4..747db1943 100644 --- a/web/thumbnail_view.js +++ b/web/thumbnail_view.js @@ -218,34 +218,36 @@ var ThumbnailView = function thumbnailView(container, id, defaultViewport, } this.renderingState = RenderingStates.FINISHED; var ctx = this.getPageDrawContext(); + var canvas = ctx.canvas; - var reducedImage = img; - var reducedWidth = img.width; - var reducedHeight = img.height; - - // drawImage does an awful job of rescaling the image, doing it gradually - var MAX_SCALE_FACTOR = 2.0; - if (Math.max(img.width / ctx.canvas.width, - img.height / ctx.canvas.height) > MAX_SCALE_FACTOR) { - reducedWidth >>= 1; - reducedHeight >>= 1; - reducedImage = getTempCanvas(reducedWidth, reducedHeight); + if (img.width <= 2 * canvas.width) { + ctx.drawImage(img, 0, 0, img.width, img.height, + 0, 0, canvas.width, canvas.height); + } else { + // drawImage does an awful job of rescaling the image, doing it gradually + var MAX_NUM_SCALING_STEPS = 3; + var reducedWidth = canvas.width << MAX_NUM_SCALING_STEPS; + var reducedHeight = canvas.height << MAX_NUM_SCALING_STEPS; + var reducedImage = getTempCanvas(reducedWidth, reducedHeight); var reducedImageCtx = reducedImage.getContext('2d'); + + while (reducedWidth > img.width || reducedHeight > img.height) { + reducedWidth >>= 1; + reducedHeight >>= 1; + } reducedImageCtx.drawImage(img, 0, 0, img.width, img.height, 0, 0, reducedWidth, reducedHeight); - while (Math.max(reducedWidth / ctx.canvas.width, - reducedHeight / ctx.canvas.height) > MAX_SCALE_FACTOR) { + while (reducedWidth > 2 * canvas.width) { reducedImageCtx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, 0, 0, reducedWidth >> 1, reducedHeight >> 1); reducedWidth >>= 1; reducedHeight >>= 1; } + ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, + 0, 0, canvas.width, canvas.height); } - ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight, - 0, 0, ctx.canvas.width, ctx.canvas.height); - this.hasImage = true; }; };