/
ScreenshotWatcher.js
255 lines (217 loc) · 7.17 KB
/
ScreenshotWatcher.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const Jimp = require('jimp');
const path = require('path');
const convert = require('color-convert');
const moment = require('moment');
const chokidar = require('chokidar');
const logger = require("./Log").getLogger(__filename);
const EventEmitter = require('events');
var settings;
var watcher;
var currentWatchDirectory;
var app = require('electron').app || require('electron').remote.app;
var emitter = new EventEmitter();
function tryClose() {
if(watcher) {
try {
watcher.close();
watcher.unwatch(currentWatchDirectory);
watcher = null;
currentWatchDirectory = null;
}
catch(err) {
logger.info("Error closing screenshot watcher: " + err.message);
}
}
}
function start() {
tryClose();
settings = require('./settings').get();
if (settings.screenshotDir !== "disabled") {
logger.info("Watching " + settings.screenshotDir);
watcher = chokidar.watch(
`${settings.screenshotDir}`,
{usePolling: true, awaitWriteFinish: true, ignoreInitial: true, disableGlobbing: true}
);
watcher.on("add", (path) => {
logger.info("Cropping new screenshot: " + path);
process(path);
});
currentWatchDirectory = settings.screenshotDir;
} else {
logger.info("Screenshot directory is disabled");
}
}
function process(file) {
var filename = moment().format("YMMDDHHmmss");
if (file.length > 0) {
Jimp.read(file).then(image => {
try {
// 1920 x 1080 image scaled up 3x;
// scale differently sized images proportionately
var scaleFactor = 3 * (1920 / image.bitmap.width);
var yBounds = getYBounds(image);
var xBounds = getXBounds(image, yBounds);
var image2 = image.clone();
// take only rightmost 14% of screen for area info (no area name is longer than this)
var areaInfoWidth = Math.floor(image.bitmap.width * 0.14);
// chop off top 24px of area info - blank
image.crop(xBounds, 24, image.bitmap.width - xBounds, yBounds[0] - 24);
if (image.bitmap.width > areaInfoWidth) {
image.crop(image.bitmap.width - areaInfoWidth, 0, areaInfoWidth, image.bitmap.height);
}
image.color([
{apply: 'red', params: [99]},
{apply: 'blue', params: [-99]},
{apply: 'green', params: [-99]},
{apply: 'saturate', params: [100]},
]);
enhanceImage(image, scaleFactor);
image.write(path.join(app.getPath('userData'), '.temp_capture', filename + "." + path.basename(file, ".png") + ".area.png"));
image2.crop(xBounds, yBounds[0], image2.bitmap.width - xBounds, yBounds[1] - yBounds[0]);
image2.color([
{apply: 'red', params: [-50]},
{apply: 'green', params: [-50]},
]);
enhanceImage(image2, scaleFactor);
image2.write(path.join(app.getPath('userData'), '.temp_capture', filename + "." + path.basename(file, ".png") + ".mods.png"));
} catch(e) {
logFailedCapture(e);
}
});
}
}
function enhanceImage(image, scaleFactor) {
image.scale(scaleFactor, Jimp.RESIZE_BEZIER);
image.invert();
image.greyscale();
/*
image.convolute([
[0, 0, 0, 0, 0],
[0, 0, -1, 0, 0],
[0, -1, 5, -1, 0],
[0, 0, -1, 0, 0],
[0, 0, 0, 0, 0]
]);
*/
image.convolute([
[-1 / 8, -1 / 8, -1 / 8],
[-1 / 8, 2, -1 / 8],
[-1 / 8, -1 / 8, -1 / 8]
]);
image.brightness(-0.43);
image.contrast(0.75);
}
function getXBounds(image, yBounds) {
var numCols = 40;
var blueArray = [];
for (var x = image.bitmap.width - 1; x > 0; x--) {
var pixCount = 0;
for (var y = yBounds[0]; y < yBounds[1]; y++) {
var pixel = image.getPixelColor(x, y);
if (isBlue(pixel)) {
pixCount++;
}
}
blueArray.push(pixCount);
if (blueArray.length === numCols) {
var blueAvg = blueArray.reduce((acc, curr) => {
return acc + curr;
}) / numCols;
if (blueAvg < 1) {
return x;
}
blueArray.shift();
}
}
return 0;
}
/*
* Detects the bottom edge of the list of map mods.
*
* The map mod list is a solid block of pixels that are either black or blue,
* in the upper-right corner of the screen
*/
function getYBounds(image) {
var numRows = Math.floor(image.bitmap.width / 100);
var numCols = Math.floor(image.bitmap.width / 10);
var pixArray = [];
var blueArray = [];
var blueStarted = false;
var lowerBound = 0;
// start from top of image
for (var y = 0; y < image.bitmap.height; y++) {
var bluePixels = 0;
var blackPixels = 0;
// scan a row of pixels from left to right starting {numCols} px from the right edge,
// counting the number of blue or black pixels in the row
for (var x = image.bitmap.width - numCols; x < image.bitmap.width; x++) {
var pixel = image.getPixelColor(x, y);
if (isBlue(pixel)) {
bluePixels++;
} else if (isBlack(pixel, 20)) {
blackPixels++;
}
}
// number of blue or black pixels in row
pixArray.push(bluePixels + blackPixels);
// number of blue pixels only
blueArray.push(bluePixels);
// when {numRows} rows have been scanned, get running average
if (pixArray.length === numRows) {
// average number of blue/black pixels
var totalAvg = pixArray.reduce((acc, curr) => {
return acc + curr;
}) / numRows;
// average number of blue pixels
var blueAvg = blueArray.reduce((acc, curr) => {
return acc + curr;
}) / numRows;
if (totalAvg > (numRows * 0.95)) {
if (blueAvg > 25 && !blueStarted) {
// if the top of the mod list has not already been found,
// mark it if average blue/black pixels is > 95% and average blue pixels > 25
blueStarted = true;
lowerBound = y - numRows;
} else if (blueStarted && (blueAvg < 25 || (bluePixels + blackPixels < (numRows * 0.7)))) {
// if top of mod list has already been found, check if we've gone past the bottom:
// average blue pixels in past {numRows} rows < 25, or current row has less than 70% blue/black pixels
// if so, return bounds of mod list
return [lowerBound, y];
}
} else if (blueStarted) {
// also return bounds of mod list if top has already been found
// and average blue/black pixels in past {numRows} rows < 95%
return [lowerBound, y];
}
// only keep pixel count for last {numRows} rows
pixArray.shift();
blueArray.shift();
}
}
}
function isBlue(pixel) {
var rgba = Jimp.intToRGBA(pixel);
var hsv = convert.rgb.hsl([rgba.r, rgba.g, rgba.b]);
// map mod blue:
// hue 240
// saturation + value > 40
// red and green components equal and both > 70
return (
hsv[0] <= 250
&& hsv[0] >= 235
&& hsv[1] + hsv[2] > 40
&& Math.abs(rgba.r - rgba.g) <= 10
&& rgba.r > 70
);
}
function isBlack(pixel, tolerance) {
var rgba = Jimp.intToRGBA(pixel);
var hsv = convert.rgb.hsl([rgba.r, rgba.g, rgba.b]);
return (hsv[2] <= tolerance);
}
function logFailedCapture(e) {
logger.info(`Error processing screenshot: ${e}`);
emitter.emit("OCRError");
}
module.exports.start = start;
module.exports.emitter = emitter;