Comparing version 0.10.5-js to 0.10.5-napi
362
index.js
@@ -7,2 +7,4 @@ 'use strict'; | ||
const PC = require('pixel-change'); | ||
class PamDiff extends Transform { | ||
@@ -127,33 +129,11 @@ | ||
if (!array) { | ||
if (this._regions) { | ||
delete this._regions; | ||
delete this._regionsLength; | ||
delete this._minDiff; | ||
} | ||
this._diffs = 0; | ||
delete this._regions; | ||
delete this._regionsArr; | ||
delete this._regionsLength; | ||
delete this._percentsArr; | ||
} 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]]}`); | ||
} 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 | ||
} | ||
); | ||
} | ||
this._regionsLength = this._regions.length; | ||
this._createPointsInPolygons(this._regions, this._width, this._height); | ||
this._regions = array; | ||
this._processRegions(); | ||
} | ||
@@ -217,6 +197,6 @@ } | ||
resetCache() { | ||
//delete this._oldPix; | ||
//delete this._newPix; | ||
//delete this._width; | ||
//delete this._length; | ||
delete this._oldPix; | ||
delete this._newPix; | ||
delete this._width; | ||
delete this._height; | ||
this._parseChunk = this._parseFirstChunk; | ||
@@ -228,16 +208,7 @@ return this; | ||
* | ||
* @param regions {Array} | ||
* @param width {Number} | ||
* @param height {Number} | ||
* @param chunk | ||
* @private | ||
*/ | ||
_createPointsInPolygons(regions, width, height) { | ||
if (regions && width && height) { | ||
this._pointsInPolygons = []; | ||
for (const region of regions) { | ||
const bitset = region.polygon.getBitset(this._width, this._height); | ||
region.pointsLength = bitset.count; | ||
this._pointsInPolygons.push(bitset.buffer); | ||
} | ||
} | ||
_blackAndWhitePixelDiff(chunk) { | ||
throw new Error("black and white pixel diff not available, yet"); | ||
} | ||
@@ -250,121 +221,29 @@ | ||
*/ | ||
_blackAndWhitePixelDiff(chunk) { | ||
_grayScalePixelDiff(chunk) { | ||
this._newPix = chunk.pixels; | ||
for (let y = 0, i = 0; y < this._height; y++) { | ||
for (let x = 0; x < this._width; x++, i++) { | ||
const diff = this._oldPix[i] !== this._newPix[i]; | ||
if (this._regions && diff === true) { | ||
for (let j = 0; j < this._regionsLength; j++) { | ||
if (this._pointsInPolygons[j][i]) { | ||
this._regions[j].diffs++; | ||
} | ||
} | ||
} else { | ||
if (diff === true) { | ||
this._diffs++; | ||
} | ||
} | ||
} | ||
} | ||
if (this._regions) { | ||
const regionDiffArray = []; | ||
const trigger = []; | ||
if (this._regionsArr) { | ||
const results = PC.compareGrayRegions(this._width, this._height, this._regionsArr, this._oldPix, this._newPix); | ||
for (let i = 0; i < this._regionsLength; i++) { | ||
const percent = Math.floor(100 * this._regions[i].diffs / this._regions[i].pointsLength); | ||
if (percent >= this._regions[i].percent) { | ||
regionDiffArray.push({name: this._regions[i].name, percent: percent}); | ||
if (results[i].percent >= this._percentsArr[i]) { | ||
trigger.push({name: results[i].name, percent: results[i].percent}); | ||
} | ||
this._regions[i].diffs = 0; | ||
} | ||
if (regionDiffArray.length > 0) { | ||
const data = {trigger: regionDiffArray, pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
} | ||
} else { | ||
const percent = Math.floor(100 * this._diffs / this._length); | ||
if (percent >= this._percent) { | ||
const data = {trigger: [{name: 'percent', percent: percent}], pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
const results = PC.compareGrayPixels(this._width, this._height, this._difference, this._oldPix, this._newPix); | ||
if (results >= this._percent) { | ||
trigger.push({name: 'percent', percent: results}); | ||
} | ||
this._diffs = 0; | ||
} | ||
this._oldPix = this._newPix; | ||
} | ||
/** | ||
* | ||
* @param chunk | ||
* @private | ||
*/ | ||
_grayScalePixelDiff(chunk) { | ||
this._newPix = chunk.pixels; | ||
for (let y = 0, i = 0; y < this._height; y++) { | ||
for (let x = 0; x < this._width; x++, i++) { | ||
if (this._oldPix[i] !== this._newPix[i]) { | ||
const diff = Math.abs(this._oldPix[i] - this._newPix[i]); | ||
if (this._regions && diff >= this._minDiff) { | ||
for (let j = 0; j < this._regionsLength; j++) { | ||
if (this._pointsInPolygons[j][i] && diff >= this._regions[j].difference) { | ||
this._regions[j].diffs++; | ||
} | ||
} | ||
} else { | ||
if (diff >= this._difference) { | ||
this._diffs++; | ||
} | ||
} | ||
} | ||
if (trigger.length) { | ||
const data = {trigger: trigger, pam:chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
} | ||
if (this._regions) { | ||
const regionDiffArray = []; | ||
for (let i = 0; i < this._regionsLength; i++) { | ||
const percent = Math.floor(100 * this._regions[i].diffs / this._regions[i].pointsLength); | ||
if (percent >= this._regions[i].percent) { | ||
regionDiffArray.push({name: this._regions[i].name, percent: percent}); | ||
} | ||
this._regions[i].diffs = 0; | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (regionDiffArray.length > 0) { | ||
const data = {trigger: regionDiffArray, pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
} else { | ||
const percent = Math.floor(100 * this._diffs / this._length); | ||
if (percent >= this._percent) { | ||
const data = {trigger: [{name: 'percent', percent: percent}], pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
} | ||
this._diffs = 0; | ||
} | ||
@@ -381,56 +260,27 @@ this._oldPix = this._newPix; | ||
this._newPix = chunk.pixels; | ||
for (let y = 0, i = 0, p = 0; y < this._height; y++) { | ||
for (let x = 0; x < this._width; x++, i += 3, p++) { | ||
if (this._oldPix[i] !== this._newPix[i] || this._oldPix[i + 1] !== this._newPix[i + 1] || this._oldPix[i + 2] !== this._newPix[i + 2]) { | ||
const diff = Math.abs(this._oldPix[i] + this._oldPix[i + 1] + this._oldPix[i + 2] - this._newPix[i] - this._newPix[i + 1] - this._newPix[i + 2])/3; | ||
if (this._regions && diff >= this._minDiff) { | ||
for (let j = 0; j < this._regionsLength; j++) { | ||
if (this._pointsInPolygons[j][p] && diff >= this._regions[j].difference) { | ||
this._regions[j].diffs++; | ||
} | ||
} | ||
} else { | ||
if (diff >= this._difference) { | ||
this._diffs++; | ||
} | ||
} | ||
const trigger = []; | ||
if (this._regionsArr) { | ||
const results = PC.compareRgbRegions(this._width, this._height, this._regionsArr, this._oldPix, this._newPix); | ||
for (let i = 0; i < this._regionsLength; i++) { | ||
if (results[i].percent >= this._percentsArr[i]) { | ||
trigger.push({name: results[i].name, percent: results[i].percent}); | ||
} | ||
} | ||
} else { | ||
const results = PC.compareRgbPixels(this._width, this._height, this._difference, this._oldPix, this._newPix); | ||
if (results >= this._percent) { | ||
trigger.push({name: 'percent', percent: results}); | ||
} | ||
} | ||
if (this._regions) { | ||
const regionDiffArray = []; | ||
for (let i = 0; i < this._regionsLength; i++) { | ||
const percent = Math.floor(100 * this._regions[i].diffs / this._regions[i].pointsLength); | ||
if (percent >= this._regions[i].percent) { | ||
regionDiffArray.push({name: this._regions[i].name, percent: percent}); | ||
} | ||
this._regions[i].diffs = 0; | ||
if (trigger.length) { | ||
const data = {trigger: trigger, pam:chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (regionDiffArray.length > 0) { | ||
const data = {trigger: regionDiffArray, pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
} else { | ||
const percent = Math.floor(100 * this._diffs / this._length); | ||
if (percent >= this._percent) { | ||
const data = {trigger: [{name: 'percent', percent: percent}], pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
this._diffs = 0; | ||
} | ||
@@ -447,56 +297,27 @@ this._oldPix = this._newPix; | ||
this._newPix = chunk.pixels; | ||
for (let y = 0, i = 0, p = 0; y < this._height; y++) { | ||
for (let x = 0; x < this._width; x++, i += 4, p++) { | ||
if (this._oldPix[i] !== this._newPix[i] || this._oldPix[i + 1] !== this._newPix[i + 1] || this._oldPix[i + 2] !== this._newPix[i + 2]) { | ||
const diff = Math.abs(this._oldPix[i] + this._oldPix[i + 1] + this._oldPix[i + 2] - this._newPix[i] - this._newPix[i + 1] - this._newPix[i + 2])/3; | ||
if (this._regions && diff >= this._minDiff) { | ||
for (let j = 0; j < this._regionsLength; j++) { | ||
if (this._pointsInPolygons[j][p] && diff >= this._regions[j].difference) { | ||
this._regions[j].diffs++; | ||
} | ||
} | ||
} else { | ||
if (diff >= this._difference) { | ||
this._diffs++; | ||
} | ||
} | ||
const trigger = []; | ||
if (this._regionsArr) { | ||
const results = PC.compareRgbaRegions(this._width, this._height, this._regionsArr, this._oldPix, this._newPix); | ||
for (let i = 0; i < this._regionsLength; i++) { | ||
if (results[i].percent >= this._percentsArr[i]) { | ||
trigger.push({name: results[i].name, percent: results[i].percent}); | ||
} | ||
} | ||
} else { | ||
const results = PC.compareRgbaPixels(this._width, this._height, this._difference, this._oldPix, this._newPix); | ||
if (results >= this._percent) { | ||
trigger.push({name: 'percent', percent: results}); | ||
} | ||
} | ||
if (this._regions) { | ||
const regionDiffArray = []; | ||
for (let i = 0; i < this._regionsLength; i++) { | ||
const percent = Math.floor(100 * this._regions[i].diffs / this._regions[i].pointsLength); | ||
if (percent >= this._regions[i].percent) { | ||
regionDiffArray.push({name: this._regions[i].name, percent: percent}); | ||
} | ||
this._regions[i].diffs = 0; | ||
if (trigger.length) { | ||
const data = {trigger: trigger, pam:chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (regionDiffArray.length > 0) { | ||
const data = {trigger: regionDiffArray, pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
} else { | ||
const percent = Math.floor(100 * this._diffs / this._length); | ||
if (percent >= this._percent) { | ||
const data = {trigger: [{name: 'percent', percent: percent}], pam: chunk.pam}; | ||
if (this._callback) { | ||
this._callback(data); | ||
} | ||
if (this._readableState.pipesCount > 0) { | ||
this.push(data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
if (this.listenerCount('diff') > 0) { | ||
this.emit('diff', data); | ||
} | ||
this._diffs = 0; | ||
} | ||
@@ -506,2 +327,28 @@ this._oldPix = this._newPix; | ||
_processRegions() { | ||
if (this._regions && this._width && this._height) { | ||
this._regionsArr = [];//will be an array of objects {name, diff, count, bitset} | ||
this._percentsArr = []; | ||
for (const region of this._regions) { | ||
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 bitset = polygonPoints.getBitset(this._width, this._height); | ||
const difference = PamDiff._validateNumber(parseInt(region.difference), this._difference, 1, 255); | ||
const percent = PamDiff._validateNumber(parseInt(region.percent), this._percent, 1, 100); | ||
this._regionsArr.push( | ||
{ | ||
name: region.name, | ||
diff: difference, | ||
count: bitset.count, | ||
bitset: bitset.buffer | ||
} | ||
); | ||
this._percentsArr.push(percent); | ||
} | ||
this._regionsLength = this._regions.length; | ||
} | ||
} | ||
/** | ||
@@ -516,18 +363,27 @@ * | ||
this._oldPix = chunk.pixels; | ||
this._length = this._width * this._height; | ||
this._createPointsInPolygons(this._regions, this._width, this._height); | ||
this._processRegions(); | ||
switch (chunk.tupltype) { | ||
case 'blackandwhite' : | ||
if (this._width * this._height !== this._oldPix.length) { | ||
throw new Error("Pixel count does not match width * height"); | ||
} | ||
this._parseChunk = this._blackAndWhitePixelDiff; | ||
break; | ||
case 'grayscale' : | ||
if (this._width * this._height !== this._oldPix.length) { | ||
throw new Error("Pixel count does not match width * height"); | ||
} | ||
this._parseChunk = this._grayScalePixelDiff; | ||
break; | ||
case 'rgb' : | ||
if (this._width * this._height * 3 !== this._oldPix.length) { | ||
throw new Error("Pixel count does not match width * height * 3"); | ||
} | ||
this._parseChunk = this._rgbPixelDiff; | ||
//this._increment = 3;//future use | ||
break; | ||
case 'rgb_alpha' : | ||
if (this._width * this._height * 4 !== this._oldPix.length) { | ||
throw new Error("Pixel count does not match width * height * 4"); | ||
} | ||
this._parseChunk = this._rgbAlphaPixelDiff; | ||
//this._increment = 4;//future use | ||
break; | ||
@@ -534,0 +390,0 @@ default : |
{ | ||
"name": "pam-diff", | ||
"version": "0.10.5-js", | ||
"version": "0.10.5-napi", | ||
"description": "Measure differences between pixel arrays extracted from pam images", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"postversion": "npm run doc", | ||
"examples": "node examples/example && node examples/example2 && node examples/example3 && node examples/example4 && node examples/example5", | ||
"examples": "node examples/example && node examples/example2 && node examples/example3 && node examples/example4 && node examples/example5 && node examples/example6", | ||
"out": " node examples/grayOut && node examples/rgb24Out", | ||
@@ -37,2 +37,3 @@ "all": "npm test && npm run examples && npm run out", | ||
"dependencies": { | ||
"pixel-change": "0.0.8", | ||
"polygon-points": "^0.5.1" | ||
@@ -39,0 +40,0 @@ }, |
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
18818
2
383
+ Addedpixel-change@0.0.8
+ Addednode-addon-api@1.7.2(transitive)
+ Addedpixel-change@0.0.8(transitive)