1 | var exec = require('child_process').exec;
|
2 | var fs = require('fs');
|
3 | var path = require('path');
|
4 | var async = require('async');
|
5 | var gm = require('gm').subClass({imageMagick: true});
|
6 | var shellQuote = require('shell-quote');
|
7 | var mkdirp = require('mkdirp');
|
8 | var tmp = require('tmp');
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | gm.prototype.fillFromTo = function (params) {
|
15 |
|
16 |
|
17 | this.borderColor('transparent');
|
18 | this.border(Math.max(params.toWidth - params.fromWidth, 0), Math.max(params.toHeight - params.fromHeight, 0));
|
19 |
|
20 |
|
21 |
|
22 | this.gravity('SouthEast');
|
23 |
|
24 |
|
25 | this.crop(params.toWidth, params.toHeight, 0, 0);
|
26 |
|
27 |
|
28 | return this;
|
29 | };
|
30 |
|
31 | function ImageDiff() {
|
32 | }
|
33 | ImageDiff.getImageSize = function (filepath, cb) {
|
34 |
|
35 | fs.stat(filepath, function (err, stats) {
|
36 |
|
37 | if (err) {
|
38 | if (err.code === 'ENOENT') {
|
39 | return cb(null, null);
|
40 | } else {
|
41 | return cb(err);
|
42 | }
|
43 | }
|
44 |
|
45 | gm(filepath).size(function (err, value) {
|
46 | if (err) {
|
47 | return cb(err);
|
48 | }
|
49 | cb(null, value);
|
50 | });
|
51 | });
|
52 | };
|
53 | ImageDiff.createDiff = function (options, cb) {
|
54 |
|
55 | var diffCmd = shellQuote.quote([
|
56 | 'compare',
|
57 | '-verbose',
|
58 |
|
59 | '-metric', 'RMSE',
|
60 | '-highlight-color', 'RED',
|
61 | '-compose', 'Src',
|
62 | options.actualPath,
|
63 | options.expectedPath,
|
64 | options.diffPath
|
65 | ]);
|
66 | exec(diffCmd, function processDiffOutput (err, stdout, stderr) {
|
67 |
|
68 | if (err && !stderr) {
|
69 | return cb(err);
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | var resultInfo = stderr.match(/all: (\d+\.?\d*) \((\d+\.?\d*)\)/);
|
77 |
|
78 |
|
79 | if (!resultInfo) {
|
80 | return cb(new Error('Expected `image-diff\'s stderr` to contain \'all\' but received "' + stderr + '"'));
|
81 | }
|
82 |
|
83 |
|
84 | var totalDifference = resultInfo[1];
|
85 | return cb(null, totalDifference === '0');
|
86 | });
|
87 | };
|
88 | ImageDiff.prototype = {
|
89 | diff: function (options, callback) {
|
90 |
|
91 | var actualPath = options.actualImage;
|
92 | var expectedPath = options.expectedImage;
|
93 | var diffPath = options.diffImage;
|
94 |
|
95 |
|
96 | if (!actualPath) {
|
97 | return process.nextTick(function () {
|
98 | callback(new Error('`options.actualPath` was not passed to `image-diff`'));
|
99 | });
|
100 | }
|
101 | if (!expectedPath) {
|
102 | return process.nextTick(function () {
|
103 | callback(new Error('`options.expectedPath` was not passed to `image-diff`'));
|
104 | });
|
105 | }
|
106 | if (!diffPath) {
|
107 | return process.nextTick(function () {
|
108 | callback(new Error('`options.diffPath` was not passed to `image-diff`'));
|
109 | });
|
110 | }
|
111 |
|
112 | var actualTmpPath;
|
113 | var expectedTmpPath;
|
114 | var imagesAreSame;
|
115 | async.waterfall([
|
116 | function assertActualPathExists (cb) {
|
117 | fs.exists(actualPath, function handleActualExists (actualExists) {
|
118 | if (actualExists) {
|
119 | cb();
|
120 | } else {
|
121 | cb(new Error('`image-diff` expected "' + actualPath + '" to exist but it didn\'t'));
|
122 | }
|
123 | });
|
124 | },
|
125 | function collectImageSizes (cb) {
|
126 |
|
127 | async.map([actualPath, expectedPath], ImageDiff.getImageSize, cb);
|
128 | },
|
129 | function resizeImages (sizes, cb) {
|
130 |
|
131 | var actualSize = sizes[0];
|
132 | var expectedSize = sizes[1] || {doesNotExist: true, height: 0, width: 0};
|
133 | var maxHeight = Math.max(actualSize.height, expectedSize.height);
|
134 | var maxWidth = Math.max(actualSize.width, expectedSize.width);
|
135 |
|
136 |
|
137 | async.parallel([
|
138 | function resizeActualImage (cb) {
|
139 |
|
140 | tmp.tmpName({postfix: '.png'}, function (err, filepath) {
|
141 |
|
142 | if (err) {
|
143 | return cb(err);
|
144 | }
|
145 |
|
146 |
|
147 | actualTmpPath = filepath;
|
148 | gm(actualPath).fillFromTo({
|
149 | fromWidth: actualSize.width,
|
150 | fromHeight: actualSize.height,
|
151 | toWidth: maxWidth,
|
152 | toHeight: maxHeight
|
153 | }).write(actualTmpPath, cb);
|
154 | });
|
155 | },
|
156 | function resizeExpectedImage (cb) {
|
157 | tmp.tmpName({postfix: '.png'}, function (err, filepath) {
|
158 |
|
159 | if (err) {
|
160 | return cb(err);
|
161 | }
|
162 |
|
163 |
|
164 | expectedTmpPath = filepath;
|
165 | if (expectedSize.doesNotExist) {
|
166 | gm(maxWidth, maxHeight, 'transparent').write(expectedTmpPath, cb);
|
167 |
|
168 | } else {
|
169 | gm(expectedPath).fillFromTo({
|
170 | fromWidth: expectedSize.width,
|
171 | fromHeight: expectedSize.height,
|
172 | toWidth: maxWidth,
|
173 | toHeight: maxHeight
|
174 | }).write(expectedTmpPath, cb);
|
175 | }
|
176 | });
|
177 | }
|
178 | ], cb);
|
179 | },
|
180 | function createDiffDirectory (/*..., cb*/) {
|
181 | var cb = [].slice.call(arguments, -1)[0];
|
182 | mkdirp(path.dirname(diffPath), function (err) {
|
183 | cb(err);
|
184 | });
|
185 | },
|
186 | function createDiff (cb) {
|
187 | ImageDiff.createDiff({
|
188 | actualPath: actualTmpPath,
|
189 | expectedPath: expectedTmpPath,
|
190 | diffPath: diffPath
|
191 | }, function saveResult (err, _imagesAreSame) {
|
192 | imagesAreSame = _imagesAreSame;
|
193 | cb(err);
|
194 | });
|
195 | }
|
196 | ], function cleanup (err) {
|
197 |
|
198 | var cleanupPaths = [actualTmpPath, expectedTmpPath].filter(function (filepath) {
|
199 | return !!filepath;
|
200 | });
|
201 | async.forEach(cleanupPaths, function cleanupFile (filepath, cb) {
|
202 | fs.unlink(filepath, cb);
|
203 | }, function callOriginalCallback (_err) {
|
204 |
|
205 | callback(err, imagesAreSame);
|
206 | });
|
207 | });
|
208 | }
|
209 | };
|
210 |
|
211 |
|
212 | function diffImages(options, cb) {
|
213 | var differ = new ImageDiff();
|
214 | differ.diff(options, cb);
|
215 | }
|
216 |
|
217 |
|
218 | diffImages.ImageDiff = ImageDiff;
|
219 | module.exports = diffImages;
|