This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathimage.js
More file actions
101 lines (87 loc) · 2.58 KB
/
Copy pathimage.js
File metadata and controls
101 lines (87 loc) · 2.58 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
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
'use strict';
var q = require('q'),
looksSame = require('looks-same'),
PngImg = require('png-img'),
utils = require('png-img/utils'),
inherit = require('inherit');
var Image = inherit({
__constructor: function(buffer) {
this._img = new PngImg(buffer);
},
crop: function crop(rect, opts) {
rect = this._scale(rect, (opts || {}).scaleFactor);
const imageSize = this.getSize();
this._img.crop(
Math.max(rect.left, 0),
Math.max(rect.top, 0),
Math.min(imageSize.width, rect.width),
Math.min(imageSize.height, rect.height)
);
return q.resolve(this);
},
getSize: function() {
return this._img.size();
},
getRGBA: function(x, y) {
return this._img.get(x, y);
},
save: function save(file) {
return q.ninvoke(this._img, 'save', file);
},
clear: function clear(area, opts) {
area = this._scale(area, (opts || {}).scaleFactor);
this._img.fill(
area.left,
area.top,
area.width,
area.height,
'#000000'
);
},
join: function(newImage) {
const imageSize = this.getSize();
this._img
.setSize(imageSize.width, imageSize.height + newImage.getSize().height)
.insert(newImage._img, 0, imageSize.height);
return this;
},
_scale: function(area, scaleFactor) {
scaleFactor = scaleFactor || 1;
return {
left: area.left * scaleFactor,
top: area.top * scaleFactor,
width: area.width * scaleFactor,
height: area.height * scaleFactor
};
}
}, {
fromBase64: function(base64) {
return new Image(new Buffer(base64, 'base64'));
},
RGBToString: function(rgb) {
return utils.RGBToString(rgb);
},
compare: function(path1, path2, opts) {
opts = opts || {};
var compareOptions = {
ignoreCaret: opts.canHaveCaret
};
if ('tolerance' in opts) {
compareOptions.tolerance = opts.tolerance;
}
return q.nfcall(looksSame, path1, path2, compareOptions);
},
buildDiff: function(opts) {
var diffOptions = {
reference: opts.reference,
current: opts.current,
diff: opts.diff,
highlightColor: opts.diffColor
};
if ('tolerance' in opts) {
diffOptions.tolerance = opts.tolerance;
}
return q.nfcall(looksSame.createDiff, diffOptions);
}
});
module.exports = Image;