This repository has been archived by the owner on Mar 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
gd.js
98 lines (78 loc) · 2.47 KB
/
gd.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
/*
Copyright (c) 2009-2011, Taegon Kim <gonom9@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
var fs = require('fs');
try {
// node 0.6+
var bind = require('./build/Release/gd_bindings');
} catch (e) {
// node 0.4.x
var bind = require('./build/default/gd_bindings');
}
for(var p in bind) {
if (bind[p] !== undefined) exports[p] = bind[p];
}
var formats = {
jpeg : [1,2], // format : arguments' length [open, save]
png : [1,2],
gif : [1,1],
gd : [1,1],
gd2 : [1,1],
gd2Part : [5,-1],
WBMP : [1,1]
};
function open_func(format, len) {
return function() {
if (!(arguments instanceof Array))
arguments = Array.prototype.slice.call(arguments);
var args = [].concat(arguments);
var filename = args.shift();
var callback = args[len-1];
if (typeof callback != 'function') {
return bind['createFrom'+format].apply(arguments);
}
args.pop();
fs.readFile(filename, 'binary', function(err, data) {
if(err) throw err;
callback(bind['createFrom'+format+'Ptr'](data));
});
}
}
function save_func(format, len) {
format = format.toLowerCase();
return function() {
var args = [].concat(arguments);
var filename = args.shift();
var callback = args[len-1];
if (typeof callback != 'function') {
return this[format].apply(this, arguments);
}
var data = this[format+'Ptr'].apply(this, args);
fs.writeFile(filename, data, "binary", function(err) {
if (err) throw err;
callback();
});
}
}
// create wrapper functions
for(var format in formats) {
var v = formats[format];
format = format.replace(/^[a-z]/, function(m0){ return m0.toUpperCase() });
if (v[0] >= 0) {
exports['open'+format] = open_func(format, v[0]);
}
if (v[1] >= 0) {
bind.Image.prototype['save'+format] = save_func(format, v[1]);
}
}
exports.noop = function(){};