This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (88 loc) · 2.5 KB
/
Copy pathindex.js
File metadata and controls
105 lines (88 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
B8E3
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
'use strict';
const q = require('q');
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 q(this);
}
getSize() {
return this._img.size();
}
getRGBA(x, y) {
return this._img.get(x, y);
}
save(file) {
return q.ninvoke(this._img, 'save', file);
}
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 q.nfcall(looksSame, path1, path2, compareOptions);
}
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 q.nfcall(looksSame.createDiff, diffOptions);
}
}
module.exports = Image;