/
morph.js
61 lines (42 loc) · 1.15 KB
/
morph.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// gm - Copyright Aaron Heckmann <aaron.heckmann+github@gmail.com> (MIT Licensed)
/**
* Module dependencies.
*/
var fs = require('fs');
/**
* Extend proto.
*/
module.exports = function (proto) {
/**
* Do nothing.
*/
function noop () {}
// http://www.graphicsmagick.org/GraphicsMagick.html#details-morph
proto.morph = function morph (other, outname, callback) {
if (!callback) callback = noop;
if (!outname) {
throw new Error("an output filename is required");
}
var self = this;
self.out(other, "-morph", 1);
self.write(outname, function (err, stdout, stderr, cmd) {
if (err) {
return callback.call(self, err, stdout, stderr, cmd);
}
var remaining = 3;
fs.unlink(outname + ".0", next);
fs.unlink(outname + ".2", next);
fs.rename(outname + ".1", outname, next);
function next (err) {
if (next.err) return;
if (err) {
next.err = err;
return callback.call(self, err, stdout, stderr, cmd);
}
if (--remaining) return;
callback.call(self, err, stdout, stderr, cmd);
}
});
return self;
}
}