Comparing version 0.2.12 to 0.2.13
49
index.js
@@ -8,8 +8,9 @@ var FS = require("fs"); | ||
var Resize = require("./resize.js"); | ||
var StreamToBuffer = require('stream-to-buffer'); | ||
var ReadChunk = require('read-chunk'); // npm install read-chunk | ||
var FileType = require('file-type'); | ||
var StreamToBuffer = require("stream-to-buffer"); | ||
var ReadChunk = require("read-chunk"); | ||
var FileType = require("file-type"); | ||
var PixelMatch = require("pixelmatch"); | ||
// polyfill Promise for Node < 0.12 | ||
require('es6-promise').polyfill(); | ||
require("es6-promise").polyfill(); | ||
@@ -312,2 +313,38 @@ // logging methods | ||
/** | ||
* Compares two images and returns | ||
* @param img1 a Jimp image to compare | ||
* @param img2 a Jimp image to compare | ||
* @param (optional) threshold a number, 0 to 1, the smaller the value the more sensitive the comparison (default: 0.1) | ||
* @returns an object { percent: percent similar, diff: a Jimp image highlighting differences } | ||
*/ | ||
Jimp.diff = function (img1, img2, threshold) { | ||
if ("object" != typeof img1 || img1.constructor != Jimp || "object" != typeof img2 || img2.constructor != Jimp) | ||
return throwError.call(this, "img1 and img2 must be an Jimp images"); | ||
if (img1.bitmap.width != img2.bitmap.width || img1.bitmap.height != img2.bitmap.height) | ||
return throwError.call(this, "img1 and img2 must be the same width and height"); | ||
threshold = threshold || 0.1; | ||
if ("number" != typeof threshold || threshold < 0 || threshold > 1) | ||
return throwError.call(this, "threshold must be a number between 0 and 1"); | ||
var diff = new Jimp(img1.bitmap.width, img1.bitmap.height, 0xFFFFFFFF); | ||
var numDiffPixels = PixelMatch( | ||
img1.bitmap.data, | ||
img2.bitmap.data, | ||
diff.bitmap.data, | ||
diff.bitmap.width, | ||
diff.bitmap.height, | ||
{threshold: threshold} | ||
); | ||
return { | ||
percent: numDiffPixels / (diff.bitmap.width * diff.bitmap.height), | ||
image: diff | ||
}; | ||
} | ||
// An object representing a bitmap in memory, comprising: | ||
@@ -1452,3 +1489,3 @@ // - data: a buffer of the bitmap data | ||
if (!actions | actions.constructor !== Array) | ||
throwError.call(this, "actions must be an array", cb); | ||
return throwError.call(this, "actions must be an array", cb); | ||
@@ -1489,3 +1526,3 @@ var originalScope = this; | ||
if (!fn) { | ||
throwError.call(originalScope, "action " + action.apply + " not supported", cb); | ||
return throwError.call(originalScope, "action " + action.apply + " not supported", cb); | ||
} | ||
@@ -1492,0 +1529,0 @@ clr = fn.apply(clr, action.params); |
{ | ||
"name": "jimp", | ||
"version": "0.2.12", | ||
"version": "0.2.13", | ||
"description": "An image processing library written entirely in JavaScript (i.e. zero external or native dependencies).", | ||
@@ -33,2 +33,3 @@ "main": "index.js", | ||
"mime": "^1.3.4", | ||
"pixelmatch": "^4.0.0", | ||
"pngjs": "^2.0.0", | ||
@@ -35,0 +36,0 @@ "read-chunk": "^1.0.1", |
@@ -248,2 +248,12 @@ # Jimp # | ||
## Diffing images ## | ||
Jimp allows the diffing of two Jimp images using [PixelMatch](https://github.com/mapbox/pixelmatch) as follows: | ||
```js | ||
var diff = Jimp.diff(image1, image2, threshold); // threshold ranges 0-1 | ||
diff.image; // a Jimp image showing differences | ||
diff.percent; // the amount of different pixels (0-1) | ||
``` | ||
## Chaining or callbacks ## | ||
@@ -250,0 +260,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
96019
1903
285
11
+ Addedpixelmatch@^4.0.0
+ Addedpixelmatch@4.0.2(transitive)
+ Addedpngjs@3.4.0(transitive)