-
Notifications
You must be signed in to change notification settings - Fork 231
/
paste_worker.cpp
55 lines (51 loc) · 1.95 KB
/
paste_worker.cpp
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
#include "image.h"
PasteWorker::PasteWorker(
size_t left,
size_t top,
Local<Object> & pixbuf,
size_t width,
size_t height,
CImg<unsigned char> * cimg,
NanCallback * callback
): NanAsyncWorker(callback), _left(left), _top(top), _width(width),
_height(height), _cimg(cimg) {
SaveToPersistent("pixbuf", pixbuf);
_pixels = (unsigned char *) Buffer::Data(pixbuf);
}
PasteWorker::~PasteWorker() {}
void PasteWorker::Execute () {
try {
CImg<unsigned char> pst(_pixels, _width, _height, 1, 4, true);
for (size_t x = _left ; x < _left + pst.width() ; x++) {
for (size_t y = _top ; y < _top + pst.height() ; y++) {
float a = ((float) (*_cimg)(x, y, 0, 3)) / 100.0,
pa = ((float) pst(x - _left, y - _top, 0, 3)) / 100.0,
acpa = a * (1 - pa),
na = pa + acpa;
unsigned char r = (*_cimg)(x, y, 0, 0),
g = (*_cimg)(x, y, 0, 1),
b = (*_cimg)(x, y, 0, 2),
pr = pst(x - _left, y - _top, 0, 0),
pg = pst(x - _left, y - _top, 0, 1),
pb = pst(x - _left, y - _top, 0, 2),
nr = 0, ng = 0, nb = 0;
if (na != 0) {
nr = (unsigned char) ((((float) pr) * pa + ((float) r) * acpa) / na);
ng = (unsigned char) ((((float) pg) * pa + ((float) g) * acpa) / na);
nb = (unsigned char) ((((float) pb) * pa + ((float) b) * acpa) / na);
}
_cimg->fillC(x, y, 0, nr, ng, nb, (unsigned char) (na * 100.0));
}
}
} catch (CImgException e) {
SetErrorMessage("Unable to paste image");
return;
}
}
void PasteWorker::HandleOKCallback () {
NanScope();
Local<Value> argv[] = {
NanNull()
};
callback->Call(1, argv);
}