forked from harthur/kittydar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
110 lines (91 loc) · 2.56 KB
/
utils.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var url = require("url"),
fs = require("fs"),
http = require("http"),
Canvas = require("canvas");
exports.getCatData = function(file, callback) {
fs.readFile(file, "utf-8", function(err, data) {
if (err) throw err;
var vals = data.split(" ").map(parseFloat);
var length = vals[0];
if (length != 9) {
console.log("different number of points:", length);
}
var features = ["leye", "reye", "mouth", "lear1", "lear2",
"lear3", "rear1", "rear2", "rear3"];
var points = {};
for (var i = 0; i < length; i ++) {
points[features[i]] = {
x: vals[i * 2 + 1],
y: vals[i * 2 + 2]
}
}
callback(points);
})
}
exports.dataToCanvas = function(imagedata) {
img = new Canvas.Image();
img.src = new Buffer(imagedata, 'binary');
var canvas = new Canvas(img.width, img.height);
var ctx = canvas.getContext('2d');
ctx.patternQuality = "best";
ctx.drawImage(img, 0, 0, img.width, img.height,
0, 0, img.width, img.height);
return canvas;
}
exports.drawImgToCanvas = function(file, callback) {
fs.readFile(file, function(err, data) {
if (err) {
// console.log(file, err)
return callback(err);
}
try {
var canvas = exports.dataToCanvas(data);
} catch(err) {
// console.log(file, err)
return callback(err);
}
callback(null, canvas);
});
}
exports.drawImgToCanvasSync = function(file) {
var data = fs.readFileSync(file)
var canvas = exports.dataToCanvas(data);
return canvas;
}
exports.writeCanvasToFile = function(canvas, file, callback) {
var out = fs.createWriteStream(file)
var stream = canvas.createJPEGStream();
stream.on('data', function(chunk) {
out.write(chunk);
});
stream.on('end', function() {
callback();
});
}
exports.saveImgToFile = function(file, imagedata) {
fs.writeFile(file, imagedata, 'binary', function(err) {
if (err) throw err;
console.log("File " + file + " saved");
})
}
exports.getImg = function(uri, callback) {
var options = url.parse(uri);
http.get(options, function(res) {
var imagedata = "";
res.setEncoding('binary');
res.on('data', function(chunk) {
imagedata += chunk
});
res.on('end', function() {
callback(imagedata);
})
})
}
exports.resizeCanvas = function(canvas, width, height) {
var resizeCanvas = new Canvas(width, height);
var ctx = resizeCanvas.getContext('2d');
ctx.patternQuality = "best";
ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height,
0, 0, width, height);
return resizeCanvas;
}