-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmd.js
executable file
·97 lines (75 loc) · 2.09 KB
/
cmd.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env node
/**
* CLI interface
*/
'use strict';
var nomnom = require('nomnom'); // Options parser
var getImageOutline = require('./node.js'); // Node interface
var pixelFnNames = Object.keys(require('./pixel-fns.js')); // Available pixelFns
var EOL = require('os').EOL; // \n or \r\n on Windows
function validateNumber(name, num) {
if (!isFinite(num)) {
return name + " must be a number";
}
}
/*
* Output functions, each takes a stream and an array of {x,y} objects
*/
var outputters = {
json: function(stream, poly) {
stream.write(JSON.stringify(poly));
stream.write('\n');
},
plain: function(stream, poly) {
poly.forEach(function(pt) {
stream.write(pt.x + ',' + pt.y + EOL);
});
}
};
var outputterNames = Object.keys(outputters);
nomnom.script('imageoutline').nocolors();
nomnom.options({
path: {
position: 0,
help: 'Filename or URL',
required: true
},
imageThreshold: {
abbr: 'i',
full: 'image-threshold',
default: 170,
help: 'Image Threshold (number)',
callback: validateNumber.bind(null, "--image-threshold")
},
simplifyThreshold: {
abbr: 's',
full: 'simplify-threshold',
default: '1',
help: 'Line Simplification Threshold (number)',
callback: validateNumber.bind(null, "--simplify-threshold")
},
pixelFn: {
abbr: 'p',
full: 'pixel-fn',
default: 'opaque',
help: 'How to determine if pixel is inside region - ' + pixelFnNames.join(', '),
choices: pixelFnNames
},
output: {
abbr: 'o',
default: 'plain',
choices: outputterNames,
help: 'Output format - ' + outputterNames.join(', ')
}
});
var options = nomnom.nom();
// Asychrounously get the image outline
getImageOutline(options.path, options, function(err, poly) {
if(err) {
// Fail out if there was an error
process.stderr.write(err.toString() + '\n');
process.exit(1);
}
// Output the results to stdout if it succeeded
outputters[options.output](process.stdout, poly);
});