Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

eyes.utils

Package Overview
Dependencies
Maintainers
1
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eyes.utils - npm Package Compare versions

Comparing version 0.0.18 to 0.0.19

1

index.js

@@ -14,3 +14,2 @@ exports.ArgumentGuard = require('./src/ArgumentGuard');

exports.RegionProvider = require('./src/RegionProvider');
exports.ScaleMethod = require('./src/ScaleMethod');
exports.ScaleProvider = require('./src/ScaleProvider');

@@ -17,0 +16,0 @@ exports.ScaleProviderFactory = require('./src/ScaleProviderFactory');

2

package.json
{
"name": "eyes.utils",
"version": "0.0.18",
"version": "0.0.19",
"description": "General purpose Javascript utilities.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -34,3 +34,3 @@ /*

"else { var b = document.getElementsByTagName('body')[0]; if (b.clientWidth) { width = b.clientWidth;} }; " +
"return {width: width, height: height};";
"return [width, height];";

@@ -400,17 +400,100 @@ /**

BrowserUtils.getViewportSize = function (browser, promiseFactory) {
return promiseFactory.makePromise(function (resolve) {
try {
return BrowserUtils.executeScript(browser, JS_GET_VIEWPORT_SIZE, promiseFactory, undefined).then(function (size) {
resolve(size);
return promiseFactory.makePromise(function (resolve, reject) {
return BrowserUtils.executeScript(browser, JS_GET_VIEWPORT_SIZE, promiseFactory, undefined).then(function (results) {
if (isNaN(results[0]) || isNaN(results[1])) {
reject("Can't parse values.");
} else {
resolve({
width: parseInt(results[0], 10) || 0,
height: parseInt(results[1], 10) || 0
});
}
}, function (err) {
reject(err);
});
});
};
/**
* @param {Logger} logger
* @param {WebDriver} browser The browser to use.
* @param {PromiseFactory} promiseFactory
* @return {Promise<{width: number, height: number}>} The viewport size of the current context, or the display size if the viewport size cannot be retrieved.
*/
BrowserUtils.getViewportSizeOrDisplaySize = function (logger, browser, promiseFactory) {
logger.verbose("getViewportSizeOrDisplaySize()");
return BrowserUtils.getViewportSize(browser, promiseFactory).catch(function (err) {
logger.verbose("Failed to extract viewport size using Javascript:", err);
// If we failed to extract the viewport size using JS, will use the window size instead.
logger.verbose("Using window size as viewport size.");
return browser.manage().window().getSize().then(function (size) {
logger.verbose(String.format("Done! Size is", size));
return size;
});
});
};
/**
* @param {Logger} logger
* @param {WebDriver} browser The browser to use.
* @param {{width: number, height: number}} requiredSize
* @param {PromiseFactory} promiseFactory
* @return {Promise<boolean>}
*/
BrowserUtils.setBrowserSize = function (logger, browser, requiredSize, promiseFactory) {
return _setBrowserSize(logger, browser, requiredSize, 3, promiseFactory).then(function () {
return true;
}, function () {
return false;
});
};
function _setBrowserSize(logger, browser, requiredSize, retries, promiseFactory) {
return promiseFactory.makePromise(function (resolve, reject) {
logger.verbose("Trying to set browser size to:", requiredSize);
return browser.manage().window().setSize(requiredSize.width, requiredSize.height).then(function () {
return BrowserUtils.sleep(1000, promiseFactory);
}).then(function () {
return browser.manage().window().getSize();
}).then(function (currentSize) {
logger.log("Current browser size:", currentSize);
if (currentSize.width === requiredSize.width && currentSize.height === requiredSize.height) {
resolve();
return;
}
if (retries === 0) {
reject();
return;
}
_setBrowserSize(logger, browser, requiredSize, retries - 1, promiseFactory).then(function () {
resolve();
}, function () {
browser.manage().window().getSize().then(function (size) {
resolve(size);
});
reject();
});
} catch (err) {
browser.manage().window().getSize().then(function (size) {
resolve(size);
});
}
}.bind(this));
});
});
}
/**
* @param {Logger} logger
* @param {WebDriver} browser The browser to use.
* @param {{width: number, height: number}} actualViewportSize
* @param {{width: number, height: number}} requiredViewportSize
* @param {PromiseFactory} promiseFactory
* @return {Promise<boolean>}
*/
BrowserUtils.setBrowserSizeByViewportSize = function (logger, browser, actualViewportSize, requiredViewportSize, promiseFactory) {
return browser.manage().window().getSize().then(function (browserSize) {
logger.verbose("Current browser size:", browserSize);
var requiredBrowserSize = {
width: browserSize.width + (requiredViewportSize.width - actualViewportSize.width),
height: browserSize.height + (requiredViewportSize.height - actualViewportSize.height)
};
return BrowserUtils.setBrowserSize(logger, browser, requiredBrowserSize, promiseFactory);
});
};

@@ -421,16 +504,21 @@

*
* @param {Logger} logger
* @param {WebDriver} browser The browser to use.
* @param {{width: number, height: number}} size The viewport size.
* @param {{width: number, height: number}} requiredSize The viewport size.
* @param {PromiseFactory} promiseFactory
* @param {Logger} logger
* @param {boolean|undefined} lastRetry
* @returns {Promise<void>}
*/
BrowserUtils.setViewportSize = function (browser, size, promiseFactory, logger, lastRetry) {
BrowserUtils.setViewportSize = function (logger, browser, requiredSize, promiseFactory) {
// First we will set the window size to the required size.
// Then we'll check the viewport size and increase the window size accordingly.
logger.verbose("setViewportSize(", requiredSize, ")");
return promiseFactory.makePromise(function (resolve, reject) {
try {
BrowserUtils.getViewportSize(browser, promiseFactory).then(function (actualViewportSize) {
if (actualViewportSize.width === size.width && actualViewportSize.height === size.height) {
var actualViewportSize;
BrowserUtils.getViewportSize(browser, promiseFactory).then(function (viewportSize) {
actualViewportSize = viewportSize;
logger.verbose("Initial viewport size:", actualViewportSize);
// If the viewport size is already the required size
if (actualViewportSize.width == requiredSize.width && actualViewportSize.height == requiredSize.height) {
resolve();

@@ -440,7 +528,12 @@ return;

browser.manage().window().getSize().then(function (browserSize) {
// Edge case.
if (browserSize.height < actualViewportSize.height || browserSize.width < actualViewportSize.width) {
logger.log("Browser window size is smaller than the viewport! Using current viewport size as is.");
// We move the window to (0,0) to have the best chance to be able to
// set the viewport size as requested.
browser.manage().window().setPosition(0, 0).catch(function () {
logger.verbose("Warning: Failed to move the browser window to (0,0)");
}).then(function () {
return BrowserUtils.setBrowserSizeByViewportSize(logger, browser, actualViewportSize, requiredSize, promiseFactory);
}).then(function () {
return BrowserUtils.getViewportSize(browser, promiseFactory);
}).then(function (actualViewportSize) {
if (actualViewportSize.width == requiredSize.width && actualViewportSize.height == requiredSize.height) {
resolve();

@@ -450,32 +543,41 @@ return;

var requiredBrowserSize = {
height: browserSize.height + (size.height - actualViewportSize.height),
width: browserSize.width + (size.width - actualViewportSize.width)
};
// Additional attempt. This Solves the "maximized browser" bug
// (border size for maximized browser sometimes different than non-maximized, so the original browser size calculation is wrong).
logger.verbose("Trying workaround for maximization...");
return BrowserUtils.setBrowserSizeByViewportSize(logger, browser, actualViewportSize, requiredSize, promiseFactory).then(function () {
return BrowserUtils.getViewportSize(browser, promiseFactory);
}).then(function (viewportSize) {
actualViewportSize = viewportSize;
logger.verbose("Current viewport size:", actualViewportSize);
logger.log("Trying to set browser size to: " + requiredBrowserSize.width + "x" + requiredBrowserSize.height);
_setWindowSize(browser, requiredBrowserSize, 3, promiseFactory, logger).then(function () {
BrowserUtils.getViewportSize(browser, promiseFactory).then(function (updatedViewportSize) {
if (updatedViewportSize.width === size.width &&
updatedViewportSize.height === size.height) {
resolve();
return;
}
if (actualViewportSize.width == requiredSize.width && actualViewportSize.height == requiredSize.height) {
resolve();
return;
}
if (lastRetry) {
reject(new Error("Failed to set viewport size! " +
"(Got " + updatedViewportSize.width + "x" + updatedViewportSize.height + ") " +
"Please try using a smaller viewport size."));
} else {
BrowserUtils.setViewportSize(browser, size, promiseFactory, logger, true).then(function () {
return browser.manage().window().getSize().then(function (browserSize) {
var MAX_DIFF = 3;
var widthDiff = actualViewportSize.width - requiredSize.width;
var widthStep = widthDiff != 0 ? (widthDiff / (-widthDiff)) : 1; // -1 for smaller size, 1 for larger
var heightDiff = actualViewportSize.height - requiredSize.height;
var heightStep = heightDiff != 0 ? (heightDiff / (-heightDiff)) : 1;
var currWidthChange = 0;
var currHeightChange = 0;
// We try the zoom workaround only if size difference is reasonable.
if (Math.abs(widthDiff) <= MAX_DIFF && Math.abs(heightDiff) <= MAX_DIFF) {
logger.verbose("Trying workaround for zoom...");
return _setWindowSize(logger, browser, requiredSize, actualViewportSize, browserSize,
widthDiff, widthStep, heightDiff, heightStep, currWidthChange, currHeightChange, promiseFactory).then(function () {
resolve();
}, function (err) {
reject(err);
}, function () {
logger.verbose("Zoom workaround failed.");
reject();
});
}
});
}, function () {
reject(new Error("Failed to set browser size! Please try using a smaller viewport size."));
});
});
}).catch(function () {
reject("Failed to set viewport size!");
});

@@ -485,3 +587,3 @@ } catch (err) {

}
}.bind(this));
});
};

@@ -491,18 +593,38 @@

* @private
* @param {WebDriver} driver
* @param {{width: number, height: number}} size
* @param {number} retries
* @param {Logger} logger
* @param {WebDriver} browser
* @param {{width: number, height: number}} requiredSize
* @param actualViewportSize
* @param browserSize
* @param widthDiff
* @param widthStep
* @param heightDiff
* @param heightStep
* @param currWidthChange
* @param currHeightChange
* @param {PromiseFactory} promiseFactory
* @param {Logger} logger
* @return {Promise<void>}
*/
function _setWindowSize(driver, size, retries, promiseFactory, logger) {
function _setWindowSize(logger, browser, requiredSize, actualViewportSize, browserSize, widthDiff, widthStep, heightDiff, heightStep, currWidthChange, currHeightChange, promiseFactory) {
return promiseFactory.makePromise(function (resolve, reject) {
return driver.manage().window().setSize(size.width, size.height).then(function () {
return BrowserUtils.sleep(1000, promiseFactory);
}).then(function () {
return driver.manage().window().getSize();
}).then(function (browserSize) {
logger.log("Current browser size: " + browserSize.width + "x" + browserSize.height);
if (browserSize.width === size.width && browserSize.height === size.height) {
// We specifically use "<=" (and not "<"), so to give an extra resize attempt
// in addition to reaching the diff, due to floating point issues.
if (Math.abs(currWidthChange) <= Math.abs(widthDiff) && actualViewportSize.width != requiredSize.width) {
currWidthChange += widthStep;
}
if (Math.abs(currHeightChange) <= Math.abs(heightDiff) && actualViewportSize.height != requiredSize.height) {
currHeightChange += heightStep;
}
var sizeToSet = {
width: browserSize.width + currWidthChange,
height: browserSize.height + currHeightChange
};
return BrowserUtils.setBrowserSize(logger, browser, sizeToSet, promiseFactory).then(function () {
return BrowserUtils.getViewportSize(browser, promiseFactory);
}).then(function (actualViewportSize) {
logger.verbose("Current viewport size:", actualViewportSize);
if (actualViewportSize.width === requiredSize.width && actualViewportSize.height === requiredSize.height) {
resolve();

@@ -512,12 +634,8 @@ return;

if (retries === 0) {
reject();
return;
if (Math.abs(currWidthChange) <= Math.abs(widthDiff) || Math.abs(currHeightChange) <= Math.abs(heightDiff)) {
return _setWindowSize(logger, browser, requiredSize, actualViewportSize, browserSize,
widthDiff, widthStep, heightDiff, heightStep, currWidthChange, currHeightChange, promiseFactory);
}
_setWindowSize(driver, size, retries - 1, promiseFactory, logger).then(function () {
resolve();
}, function () {
reject();
});
reject();
});

@@ -672,3 +790,3 @@ });

if (scaleProvider && scaleProvider.getScaleRatio() !== 1) {
return parsedImage.scaleImage(scaleProvider);
return parsedImage.scaleImage(scaleProvider.getScaleRatio());
}

@@ -675,0 +793,0 @@ })

@@ -15,3 +15,2 @@ /*

var StreamUtils = require('./StreamUtils'),
ScaleMethod = require('./ScaleMethod'),
fs = require('fs'),

@@ -83,7 +82,6 @@ /** @type {PNG} */

* @param {number} scaleRatio factor to multiply the image dimensions by (lower than 1 for scale down)
* @param {ScaleMethod} scaleMethod
* @param {PromiseFactory} promiseFactory
* @returns {Promise<void>}
**/
ImageUtils.scaleImage = function scaleImage(imageBmp, scaleRatio, scaleMethod, promiseFactory) {
ImageUtils.scaleImage = function scaleImage(imageBmp, scaleRatio, promiseFactory) {
if (scaleRatio === 1) {

@@ -95,5 +93,5 @@ return promiseFactory.makePromise(function (resolve) {

var scaledWidth = imageBmp.width * scaleRatio;
var scaledHeight = imageBmp.height * scaleRatio;
return ImageUtils.resizeImage(imageBmp, scaledWidth, scaledHeight, scaleMethod, promiseFactory);
var scaledWidth = Math.ceil(imageBmp.width * scaleRatio);
var scaledHeight = Math.ceil(imageBmp.height * scaleRatio);
return ImageUtils.resizeImage(imageBmp, scaledWidth, scaledHeight, promiseFactory);
};

@@ -105,11 +103,17 @@

* @param {PNG} imageBmp - will be modified
* @param {number} width The width to resize the image to
* @param {number} height The height to resize the image to
* @param {ScaleMethod} scaleMethod
* @param {int} targetWidth The width to resize the image to
* @param {int} targetHeight The height to resize the image to
* @param {PromiseFactory} promiseFactory
* @returns {Promise<void>}
**/
ImageUtils.resizeImage = function resizeImage(imageBmp, width, height, scaleMethod, promiseFactory) {
ImageUtils.resizeImage = function resizeImage(imageBmp, targetWidth, targetHeight, promiseFactory) {
function doBicubicInterpolation(src, dst) {
function _interpolateCubic(x0, x1, x2, x3, t) {
var a0 = x3 - x2 - x0 + x1;
var a1 = x0 - x1 - a0;
var a2 = x2 - x0;
return Math.max(0, Math.min(255, (a0 * (t * t * t)) + (a1 * (t * t)) + (a2 * t) + (x1)));
}
function _doBicubicInterpolation(src, dst) {
var bufSrc = src.data;

@@ -130,9 +134,2 @@ var bufDst = dst.data;

var interpolateCubic = function (x0, x1, x2, x3, t) {
var a0 = x3 - x2 - x0 + x1;
var a1 = x0 - x1 - a0;
var a2 = x2 - x0;
return Math.max(0, Math.min(255, (a0 * (t * t * t)) + (a1 * (t * t)) + (a2 * t) + (x1)));
};
var i, j, x, y, k, t, xPos, yPos, kPos, buf1Pos, buf2Pos;

@@ -157,3 +154,3 @@

var x3 = (xPos < wSrc - 2) ? bufSrc[kPos + 8] : 2 * bufSrc[kPos + 4] - bufSrc[kPos];
buf1[buf1Pos + k] = interpolateCubic(x0, x1, x2, x3, t);
buf1[buf1Pos + k] = _interpolateCubic(x0, x1, x2, x3, t);
}

@@ -180,3 +177,3 @@ }

buf2[buf2Pos + k] = interpolateCubic(y0, y1, y2, y3, t);
buf2[buf2Pos + k] = _interpolateCubic(y0, y1, y2, y3, t);
}

@@ -221,3 +218,3 @@ }

function scaleImageIncrementally(src, dst, method) {
function _scaleImageIncrementally(src, dst) {
var incrementCount = 0;

@@ -233,3 +230,4 @@ var currentWidth = src.width,

var fraction = (method == ScaleMethod.ULTRA_QUALITY ? 7 : 2);
// For ultra quality should use 7
var fraction = 2;

@@ -240,13 +238,7 @@ do {

/*
* If the current width is bigger than our target, cut it in half
* and sample again.
*/
// If the current width is bigger than our target, cut it in half and sample again.
if (currentWidth > targetWidth) {
currentWidth -= (currentWidth / fraction);
/*
* If we cut the width too far it means we are on our last
* iteration. Just set it to the target width and finish up.
*/
// If we cut the width too far it means we are on our last iteration. Just set it to the target width and finish up.
if (currentWidth < targetWidth)

@@ -256,13 +248,7 @@ currentWidth = targetWidth;

/*
* If the current height is bigger than our target, cut it in half
* and sample again.
*/
// If the current height is bigger than our target, cut it in half and sample again.
if (currentHeight > targetHeight) {
currentHeight -= (currentHeight / fraction);
/*
* If we cut the height too far it means we are on our last
* iteration. Just set it to the target height and finish up.
*/
// If we cut the height too far it means we are on our last iteration. Just set it to the target height and finish up.
if (currentHeight < targetHeight)

@@ -272,14 +258,3 @@ currentHeight = targetHeight;

/*
* Stop when we cannot incrementally step down anymore.
*
* This used to use a || condition, but that would cause problems
* when using FIT_EXACT such that sometimes the width OR height
* would not change between iterations, but the other dimension
* would (e.g. resizing 500x500 to 500x250).
*
* Now changing this to an && condition requires that both
* dimensions do not change between a resize iteration before we
* consider ourselves done.
*/
// Stop when we cannot incrementally step down anymore.
if (prevCurrentWidth == currentWidth && prevCurrentHeight == currentHeight)

@@ -294,8 +269,6 @@ break;

};
doBicubicInterpolation(dst, incrementalImage);
_doBicubicInterpolation(dst, incrementalImage);
/*
* Now treat our incremental partially scaled image as the src image
* and cycle through our loop again to do another incremental scaling of it (if necessary).
*/
// Now treat our incremental partially scaled image as the src image
// and cycle through our loop again to do another incremental scaling of it (if necessary).
dst.data = incrementalImage.data;

@@ -312,46 +285,16 @@ dst.width = incrementalImage.width;

function doNearestNeighbor(src, dst) {
var wSrc = src.width, hSrc = src.height, bufSrc = src.data,
wDst = dst.width, hDst = dst.height, bufDst = dst.data,
i, j, iSrc, jSrc, posDst, posSrc;
var hScale = hSrc / hDst, wScale = wSrc / wDst;
for (i = 0; i < hDst; i++) {
for (j = 0; j < wDst; j++) {
iSrc = Math.round(i * hScale);
jSrc = Math.round(j * wScale);
posDst = (i * wDst + j) << 2;
posSrc = (iSrc * wSrc + jSrc) << 2;
bufDst[posDst++] = bufSrc[posSrc++];
bufDst[posDst++] = bufSrc[posSrc++];
bufDst[posDst++] = bufSrc[posSrc++];
bufDst[posDst++] = bufSrc[posSrc++];
}
}
return dst;
}
return promiseFactory.makePromise(function (resolve) {
width = Math.ceil(width);
height = Math.ceil(height);
var ratio = imageBmp.height / imageBmp.width;
targetHeight = Math.round(targetWidth * ratio);
var dst = {
data: new Buffer(width * height * 4),
width: width,
height: height
data: new Buffer(targetWidth * targetHeight * 4),
width: targetWidth,
height: targetHeight
};
switch (scaleMethod) {
case ScaleMethod.SPEED:
doNearestNeighbor(imageBmp, dst);
break;
default:
if (dst.width > imageBmp.width || dst.height > imageBmp.height) {
doBicubicInterpolation(imageBmp, dst);
} else {
scaleImageIncrementally(imageBmp, dst, scaleMethod);
}
if (dst.width > imageBmp.width || dst.height > imageBmp.height) {
_doBicubicInterpolation(imageBmp, dst);
} else {
_scaleImageIncrementally(imageBmp, dst);
}

@@ -463,7 +406,11 @@

var y, dstY, srcY, x, dstX, srcX, dstIndex, srcIndex;
for (y = 0; y < size.height; ++y) {
// Fix the problem when src image was out of dst image and pixels was copied to wrong position in dst image.
var maxHeight = dstPosition.y + size.height <= dst.height ? size.height : dst.height - dstPosition.y;
var maxWidth = dstPosition.x + size.width <= dst.width ? size.width : dst.width - dstPosition.x;
for (y = 0; y < maxHeight; ++y) {
dstY = dstPosition.y + y;
srcY = srcPosition.y + y;
for (x = 0; x < size.width; ++x) {
for (x = 0; x < maxWidth; ++x) {
dstX = dstPosition.x + x;

@@ -470,0 +417,0 @@ srcX = srcPosition.x + x;

@@ -157,6 +157,6 @@ /*

*
* @param {ScaleProvider} scaleProvider
* @param {number} scaleRatio
* @return {Promise<MutableImage>}
*/
MutableImage.prototype.scaleImage = function (scaleProvider) {
MutableImage.prototype.scaleImage = function (scaleRatio) {
var that = this;

@@ -166,3 +166,3 @@ return _parseImage(that)

if (that._isParsed) {
return ImageUtils.scaleImage(that._imageBmp, scaleProvider.getScaleRatio(), scaleProvider.getScaleMethod(), that._promiseFactory)
return ImageUtils.scaleImage(that._imageBmp, scaleRatio, that._promiseFactory)
.then(function () {

@@ -169,0 +169,0 @@ that._width = that._imageBmp.width;

@@ -14,9 +14,4 @@ (function () {

/**
* @return {ScaleMethod} The scale method used for the scaling.
*/
ScaleProvider.prototype.getScaleMethod = function () {};
module.exports = ScaleProvider;
}());
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc