-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathtest.js
More file actions
58 lines (53 loc) · 1.95 KB
/
test.js
File metadata and controls
58 lines (53 loc) · 1.95 KB
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
"use strict";
var parse_args = require('./commargs.js'),
fs = require('fs'),
F = require('./filterwithcanvas.js'),
filter, input, output, selection,
args = parse_args(),
parallel = !!args.options['parallel'],
wasm = !!args.options['wasm'];
console.log('Test runs "' + (parallel ? 'parallel' : 'synchronous') + (wasm ? ' in assembly' : ' in javascript') + '"');
filter = new F.ColorMatrixFilter().grayscale().contrast(1);
/*filter = new F.FrequencyFilter(function(re, im, i, j, w, h){
//if (i > 0.2*w || j > 0.2*h) {re = 0; im = 0;}
return [re, im];
}).setMode(F.MODE.GRAY);*/
input = __dirname+'/che.jpg';
output = __dirname+'/che_selection.png';
selection = __dirname+'/che_mask.png';
if (wasm) filter.makeWASM(true);
if (parallel) filter.worker(true);
function process(img, filter)
{
console.log('Applying filter to image/selection..');
filter.apply(img, function() {
if (parallel) filter.worker(false);
console.log('Saving filtered image..');
img.oCanvas.toPNG().then(function(png) {
fs.writeFile(output, png, function(err) {
if (err) console.log('error while saving image: ' + err.toString());
else console.log('filtered image saved');
})
}).catch(function(err) {
console.log('error while saving image: ' + err.toString());
});
});
}
console.log('Loading image..');
fs.readFile(input, function(err, buffer) {
if (err) console.log('error while reading image: ' + err.toString());
else F.Image.load(buffer, function(img) {
console.log('image loaded with dims: ' + img.width + ',' + img.height);
if (selection)
{
F.Image.load(selection, function(mask) {
img.select(new F.Util.Image.Selection(mask.getData(), img.width, img.height, 4));
process(img, filter);
});
}
else
{
process(img, filter);
}
});
});