From 5db01369e4cb6f72840d298bc90a30d20624d9de Mon Sep 17 00:00:00 2001 From: neopheus Date: Mon, 22 Aug 2016 11:36:30 +0200 Subject: [PATCH] Check code 2 (#3186) * JSDoc update + same name for variables --- src/filters/resize_filter.class.js | 87 +++++++++++++++++++----------- 1 file changed, 57 insertions(+), 30 deletions(-) diff --git a/src/filters/resize_filter.class.js b/src/filters/resize_filter.class.js index 49d92954..ee209a5f 100644 --- a/src/filters/resize_filter.class.js +++ b/src/filters/resize_filter.class.js @@ -90,61 +90,70 @@ /** * Filter sliceByTwo * @param {Object} canvasEl Canvas element to apply filter to - * @param {Number} width - * @param {Number} height - * @param {Number} newWidth - * @param {Number} newHeight + * @param {Number} oW Original Width + * @param {Number} oH Original Height + * @param {Number} dW Destination Width + * @param {Number} dH Destination Height * @returns {ImageData} */ - sliceByTwo: function(canvasEl, width, height, newWidth, newHeight) { + sliceByTwo: function(canvasEl, oW, oH, dW, dH) { var context = canvasEl.getContext('2d'), imageData, multW = 0.5, multH = 0.5, signW = 1, signH = 1, - doneW = false, doneH = false, stepW = width, stepH = height, + doneW = false, doneH = false, stepW = oW, stepH = oH, tmpCanvas = fabric.util.createCanvasElement(), tmpCtx = tmpCanvas.getContext('2d'); - newWidth = floor(newWidth); - newHeight = floor(newHeight); - tmpCanvas.width = max(newWidth, width); - tmpCanvas.height = max(newHeight, height); + dW = floor(dW); + dH = floor(dH); + tmpCanvas.width = max(dW, oW); + tmpCanvas.height = max(dH, oH); - if (newWidth > width) { + if (dW > oW) { multW = 2; signW = -1; } - if (newHeight > height) { + if (dH > oH) { multH = 2; signH = -1; } - imageData = context.getImageData(0, 0, width, height); - canvasEl.width = max(newWidth, width); - canvasEl.height = max(newHeight, height); + imageData = context.getImageData(0, 0, oW, oH); + canvasEl.width = max(dW, oW); + canvasEl.height = max(dH, oH); context.putImageData(imageData, 0, 0); while (!doneW || !doneH) { - width = stepW; - height = stepH; - if (newWidth * signW < floor(stepW * multW * signW)) { + oW = stepW; + oH = stepH; + if (dW * signW < floor(stepW * multW * signW)) { stepW = floor(stepW * multW); } else { - stepW = newWidth; + stepW = dW; doneW = true; } - if (newHeight * signH < floor(stepH * multH * signH)) { + if (dH * signH < floor(stepH * multH * signH)) { stepH = floor(stepH * multH); } else { - stepH = newHeight; + stepH = dH; doneH = true; } - imageData = context.getImageData(0, 0, width, height); + imageData = context.getImageData(0, 0, oW, oH); tmpCtx.putImageData(imageData, 0, 0); context.clearRect(0, 0, stepW, stepH); - context.drawImage(tmpCanvas, 0, 0, width, height, 0, 0, stepW, stepH); + context.drawImage(tmpCanvas, 0, 0, oW, oH, 0, 0, stepW, stepH); } - return context.getImageData(0, 0, newWidth, newHeight); + return context.getImageData(0, 0, dW, dH); }, + /** + * Filter lanczosResize + * @param {Object} canvasEl Canvas element to apply filter to + * @param {Number} oW Original Width + * @param {Number} oH Original Height + * @param {Number} dW Destination Width + * @param {Number} dH Destination Height + * @returns {ImageData} + */ lanczosResize: function(canvasEl, oW, oH, dW, dH) { function lanczosCreate(lobes) { @@ -226,20 +235,29 @@ return process(0); }, - bilinearFiltering: function(canvasEl, w, h, w2, h2) { + /** + * bilinearFiltering + * @param {Object} canvasEl Canvas element to apply filter to + * @param {Number} oW Original Width + * @param {Number} oH Original Height + * @param {Number} dW Destination Width + * @param {Number} dH Destination Height + * @returns {ImageData} + */ + bilinearFiltering: function(canvasEl, oW, oH, dW, dH) { var a, b, c, d, x, y, i, j, xDiff, yDiff, chnl, color, offset = 0, origPix, ratioX = this.rcpScaleX, ratioY = this.rcpScaleY, context = canvasEl.getContext('2d'), - w4 = 4 * (w - 1), img = context.getImageData(0, 0, w, h), - pixels = img.data, destImage = context.getImageData(0, 0, w2, h2), + w4 = 4 * (oW - 1), img = context.getImageData(0, 0, oW, oH), + pixels = img.data, destImage = context.getImageData(0, 0, dW, dH), destPixels = destImage.data; - for (i = 0; i < h2; i++) { - for (j = 0; j < w2; j++) { + for (i = 0; i < dH; i++) { + for (j = 0; j < dW; j++) { x = floor(ratioX * j); y = floor(ratioY * i); xDiff = ratioX * j - x; yDiff = ratioY * i - y; - origPix = 4 * (y * w + x); + origPix = 4 * (y * oW + x); for (chnl = 0; chnl < 4; chnl++) { a = pixels[origPix + chnl]; @@ -255,6 +273,15 @@ return destImage; }, + /** + * hermiteFastResize + * @param {Object} canvasEl Canvas element to apply filter to + * @param {Number} oW Original Width + * @param {Number} oH Original Height + * @param {Number} dW Destination Width + * @param {Number} dH Destination Height + * @returns {ImageData} + */ hermiteFastResize: function(canvasEl, oW, oH, dW, dH) { var ratioW = this.rcpScaleX, ratioH = this.rcpScaleY, ratioWHalf = ceil(ratioW / 2),