Comparing version 0.10.3 to 0.10.4
242
index.js
'use strict'; | ||
const Transform = require('stream').Transform; | ||
const { Transform } = require('stream'); | ||
const PP = require('polygon-points'); | ||
class PamDiff extends Transform { | ||
/** | ||
* | ||
* @param [options] {Object} | ||
* @param [callback] {Function} | ||
*/ | ||
constructor(options, callback) { | ||
super(options); | ||
Transform.call(this, {objectMode: true}); | ||
this.difference = PamDiff.parseOptions('difference', options);//global option, can be overridden per region | ||
this.percent = PamDiff.parseOptions('percent', options);//global option, can be overridden per region | ||
this.regions = PamDiff.parseOptions('regions', options);//can be no regions or a single region or multiple regions. if no regions, all pixels will be compared. | ||
this.difference = PamDiff._parseOptions('difference', options);//global option, can be overridden per region | ||
this.percent = PamDiff._parseOptions('percent', options);//global option, can be overridden per region | ||
this.regions = PamDiff._parseOptions('regions', options);//can be no regions or a single region or multiple regions. if no regions, all pixels will be compared. | ||
this.callback = callback;//callback function to be called when pixel difference is detected | ||
@@ -17,5 +24,10 @@ this._parseChunk = this._parseFirstChunk;//first parsing will be reading settings and configuring internal pixel reading | ||
//static methods | ||
static parseOptions(option, options) { | ||
/** | ||
* | ||
* @param option {String} | ||
* @param options {Object} | ||
* @return {*} | ||
* @private | ||
*/ | ||
static _parseOptions(option, options) { | ||
if (options && options.hasOwnProperty(option)) { | ||
@@ -27,3 +39,12 @@ return options[option]; | ||
static validateNumber(number, def, low, high) { | ||
/** | ||
* | ||
* @param number {Number} | ||
* @param def {Number} | ||
* @param low {Number} | ||
* @param high {Number} | ||
* @return {Number} | ||
* @private | ||
*/ | ||
static _validateNumber(number, def, low, high) { | ||
if (isNaN(number)) { | ||
@@ -40,26 +61,67 @@ return def; | ||
//public methods | ||
/** | ||
* | ||
* @deprecated | ||
* @param string {String} | ||
*/ | ||
setGrayscale(string) { | ||
console.warn('grayscale option has been removed, averaging has proven to most accurate'); | ||
console.warn('grayscale option has been removed, "average" has proven to most accurate and is the default'); | ||
} | ||
/** | ||
* | ||
* @param number {Number} | ||
*/ | ||
set difference(number) { | ||
this._difference = PamDiff.validateNumber(parseInt(number), 5, 1, 255); | ||
this._difference = PamDiff._validateNumber(parseInt(number), 5, 1, 255); | ||
} | ||
/** | ||
* | ||
* @return {Number} | ||
*/ | ||
get difference() { | ||
return this._difference; | ||
} | ||
/** | ||
* | ||
* @param number {Number} | ||
* @return {PamDiff} | ||
*/ | ||
setDifference(number) { | ||
console.warn('setDifference deprecated and will be removed in next minor version, use difference = number'); | ||
this.difference = number; | ||
return this; | ||
} | ||
/** | ||
* | ||
* @param number {Number} | ||
*/ | ||
set percent(number) { | ||
this._percent = PamDiff.validateNumber(parseInt(number), 5, 1, 100); | ||
this._percent = PamDiff._validateNumber(parseInt(number), 5, 1, 100); | ||
} | ||
/** | ||
* | ||
* @return {Number} | ||
*/ | ||
get percent() { | ||
return this._percent; | ||
} | ||
/** | ||
* | ||
* @param number {Number} | ||
* @return {PamDiff} | ||
*/ | ||
setPercent(number) { | ||
console.warn('setPercent deprecated and will be removed in next minor version, use difference = number'); | ||
this.percent = number; | ||
return this; | ||
} | ||
/** | ||
* | ||
* @param array {Array} | ||
*/ | ||
set regions(array) { | ||
@@ -73,61 +135,100 @@ if (!array) { | ||
this._diffs = 0; | ||
return; | ||
} else if (!Array.isArray(array) || array.length < 1) { | ||
throw new Error(`Regions must be an array of at least 1 region object {name: 'region1', difference: 10, percent: 10, polygon: [[0, 0], [0, 50], [50, 50], [50, 0]]}`); | ||
} | ||
this._regions = []; | ||
this._minDiff = 255; | ||
for (const region of array) { | ||
if (!region.hasOwnProperty('name') || !region.hasOwnProperty('polygon')) { | ||
throw new Error('Region must include a name and a polygon property'); | ||
} else { | ||
this._regions = []; | ||
this._minDiff = 255; | ||
for (const region of array) { | ||
if (!region.hasOwnProperty('name') || !region.hasOwnProperty('polygon')) { | ||
throw new Error('Region must include a name and a polygon property'); | ||
} | ||
const polygonPoints = new PP(region.polygon); | ||
const difference = PamDiff._validateNumber(parseInt(region.difference), this._difference, 1, 255); | ||
const percent = PamDiff._validateNumber(parseInt(region.percent), this._percent, 1, 100); | ||
this._minDiff = Math.min(this._minDiff, difference); | ||
this._regions.push( | ||
{ | ||
name: region.name, | ||
polygon: polygonPoints, | ||
difference: difference, | ||
percent: percent, | ||
diffs: 0 | ||
} | ||
); | ||
} | ||
const polygonPoints = new PP(region.polygon); | ||
const difference = PamDiff.validateNumber(parseInt(region.difference), this._difference, 1, 255); | ||
const percent = PamDiff.validateNumber(parseInt(region.percent), this._percent, 1, 100); | ||
this._minDiff = Math.min(this._minDiff, difference); | ||
this._regions.push( | ||
{ | ||
name: region.name, | ||
polygon: polygonPoints, | ||
difference: difference, | ||
percent: percent, | ||
diffs: 0 | ||
} | ||
); | ||
this._regionsLength = this._regions.length; | ||
this._createPointsInPolygons(this._regions, this._width, this._height); | ||
} | ||
this._regionsLength = this._regions.length; | ||
this._createPointsInPolygons(this._regions, this._width, this._height); | ||
} | ||
/** | ||
* | ||
* @return {Array} | ||
*/ | ||
get regions() { | ||
return this._regions; | ||
} | ||
/** | ||
* | ||
* @param array {Array} | ||
* @return {PamDiff} | ||
*/ | ||
setRegions(array) { | ||
console.warn('setRegions deprecated and will be removed in next minor version, use regions = value'); | ||
this.regions = array; | ||
return this; | ||
} | ||
/** | ||
* | ||
* @param func {Function} | ||
*/ | ||
set callback(func) { | ||
if (typeof func === 'function') { | ||
if (func.length !== 1) { | ||
throw new Error('Callback function must only accept 1 argument'); | ||
} | ||
if (!func) { | ||
delete this._callback; | ||
} else if (typeof func === 'function' && func.length === 1) { | ||
this._callback = func; | ||
} else { | ||
delete this._callback; | ||
throw new Error('Callback must be a function that accepts 1 argument.'); | ||
} | ||
} | ||
/** | ||
* | ||
* @return {Function} | ||
*/ | ||
get callback() { | ||
return this._callback; | ||
} | ||
/** | ||
* | ||
* @param func {Function} | ||
* @return {PamDiff} | ||
*/ | ||
setCallback(func) { | ||
console.warn('setCallback deprecated and will be removed in next minor version, use callback = function'); | ||
this.callback = func; | ||
return this; | ||
} | ||
/** | ||
* | ||
* @return {PamDiff} | ||
*/ | ||
resetCache() { | ||
delete this._oldPix; | ||
delete this._newPix; | ||
delete this._width; | ||
delete this._length; | ||
//delete this._oldPix; | ||
//delete this._newPix; | ||
//delete this._width; | ||
//delete this._length; | ||
this._parseChunk = this._parseFirstChunk; | ||
return this; | ||
} | ||
//private methods | ||
/** | ||
* | ||
* @param regions {Array} | ||
* @param width {Number} | ||
* @param height {Number} | ||
* @private | ||
*/ | ||
_createPointsInPolygons(regions, width, height) { | ||
@@ -144,2 +245,7 @@ if (regions && width && height) { | ||
/** | ||
* | ||
* @param chunk | ||
* @private | ||
*/ | ||
_blackAndWhitePixelDiff(chunk) { | ||
@@ -203,2 +309,7 @@ this._newPix = chunk.pixels; | ||
/** | ||
* | ||
* @param chunk | ||
* @private | ||
*/ | ||
_grayScalePixelDiff(chunk) { | ||
@@ -264,2 +375,7 @@ this._newPix = chunk.pixels; | ||
/** | ||
* | ||
* @param chunk | ||
* @private | ||
*/ | ||
_rgbPixelDiff(chunk) { | ||
@@ -325,2 +441,7 @@ this._newPix = chunk.pixels; | ||
/** | ||
* | ||
* @param chunk | ||
* @private | ||
*/ | ||
_rgbAlphaPixelDiff(chunk) { | ||
@@ -386,2 +507,7 @@ this._newPix = chunk.pixels; | ||
/** | ||
* | ||
* @param chunk | ||
* @private | ||
*/ | ||
_parseFirstChunk(chunk) { | ||
@@ -413,2 +539,9 @@ this._width = parseInt(chunk.width); | ||
/** | ||
* | ||
* @param chunk | ||
* @param encoding | ||
* @param callback | ||
* @private | ||
*/ | ||
_transform(chunk, encoding, callback) { | ||
@@ -419,2 +552,7 @@ this._parseChunk(chunk); | ||
/** | ||
* | ||
* @param callback | ||
* @private | ||
*/ | ||
_flush(callback) { | ||
@@ -426,3 +564,7 @@ this.resetCache(); | ||
/** | ||
* | ||
* @type {PamDiff} | ||
*/ | ||
module.exports = PamDiff; | ||
//todo get bounding box of all regions combined to exclude some pixels before checking if they exist inside specific regions |
{ | ||
"name": "pam-diff", | ||
"version": "0.10.3", | ||
"version": "0.10.4", | ||
"description": "Measure differences between pixel arrays extracted from pam images", | ||
@@ -9,5 +9,7 @@ "main": "index.js", | ||
"preversion": "npm test", | ||
"postversion": "npm run doc", | ||
"examples": "node examples/example && node examples/example2 && node examples/example3 && node examples/example4 && node examples/example5", | ||
"out": " node examples/grayOut && node examples/rgb24Out", | ||
"all": "npm test && npm run examples && npm run out" | ||
"all": "npm test && npm run examples && npm run out", | ||
"doc": "jsdoc index.js -d docs && git commit -m 'update docs' -- docs" | ||
}, | ||
@@ -38,4 +40,5 @@ "repository": { | ||
"devDependencies": { | ||
"jsdoc": "^3.5.5", | ||
"pipe2pam": "^0.6.2" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24936
528
2