Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
107 lines (90 loc) · 2.62 KB
/
Copy pathindex.js
File metadata and controls
107 lines (90 loc) · 2.62 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
102
103
104
105
106
107
'use strict';
const Promise = require('bluebird');
const looksSame = require('looks-same');
const PngImg = require('png-img');
const utils = require('png-img/utils');
const SafeRect = require('./safe-rect');
class Image {
constructor(buffer) {
this._img = new PngImg(buffer);
}
crop(rect, opts) {
rect = this._scale(rect, (opts || {}).scaleFactor);
const imageSize = this.getSize();
const safeRect = SafeRect.create(rect, imageSize);
this._img.crop(
safeRect.left,
safeRect.top,
safeRect.width,
safeRect.height
);
return Promise.resolve(this);
}
getSize() {
return this._img.size();
}
getRGBA(x, y) {
return this._img.get(x, y);
}
save(file) {
return Promise.fromCallback((cb) => this._img.save(file, cb));
}
clear(area, opts) {
area = this._scale(area, (opts || {}).scaleFactor);
this._img.fill(
area.left,
area.top,
area.width,
area.height,
'#000000'
);
}
join(newImage) {
const imageSize = this.getSize();
this._img
.setSize(imageSize.width, imageSize.height + newImage.getSize().height)
.insert(newImage._img, 0, imageSize.height);
return this;
}
_scale(area, scaleFactor) {
scaleFactor = scaleFactor || 1;
return {
left: area.left * scaleFactor,
top: area.top * scaleFactor,
width: area.width * scaleFactor,
height: area.height * scaleFactor
};
}
static fromBase64(base64) {
return new Image(new Buffer(base64, 'base64'));
}
static RGBToString(rgb) {
return utils.RGBToString(rgb);
}
static compare(path1, path2, opts) {
opts = opts || {};
const compareOptions = {
ignoreCaret: opts.canHaveCaret,
pixelRatio: opts.pixelRatio
};
if ('tolerance' in opts) {
compareOptions.tolerance = opts.tolerance;
}
return Promise.fromCallback((cb) => {
looksSame(path1, path2, compareOptions, cb);
});
}
static buildDiff(opts) {
const diffOptions = {
reference: opts.reference,
current: opts.current,
diff: opts.diff,
highlightColor: opts.diffColor
};
if ('tolerance' in opts) {
diffOptions.tolerance = opts.tolerance;
}
return Promise.fromCallback((cb) => looksSame.createDiff(diffOptions, cb));
}
}
module.exports = Image;