-
Notifications
You must be signed in to change notification settings - Fork 1
/
img-netfree.cc
351 lines (257 loc) · 8.78 KB
/
img-netfree.cc
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <nan.h>
#include <node.h>
#include <v8.h>
#include <node_buffer.h>
#include <node_object_wrap.h>
#include <FreeImage.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <iostream>
#include <map>
using v8::FunctionTemplate;
using v8::Handle;
using v8::Object;
using v8::String;
using Nan::GetFunction;
using Nan::New;
using Nan::Set;
using namespace node;
using namespace v8;
std::map<int,FIBITMAP *> titles;
static BOOL Combine32(FIBITMAP *dst_dib, FIBITMAP *src_dib, unsigned x, unsigned y ) {
// check the bit depth of src and dst images
if((FreeImage_GetBPP(dst_dib) != 32) || (FreeImage_GetBPP(src_dib) != 32)) {
return FALSE;
}
if(((int)x)<0 || ((int)y)<0){
return FALSE;
}
// check the size of src image
if((x + FreeImage_GetWidth(src_dib) > FreeImage_GetWidth(dst_dib)) || (y + FreeImage_GetHeight(src_dib) > FreeImage_GetHeight(dst_dib))) {
return FALSE;
}
BYTE *dst_bits = FreeImage_GetBits(dst_dib) + ((FreeImage_GetHeight(dst_dib) - FreeImage_GetHeight(src_dib) - y) * FreeImage_GetPitch(dst_dib)) + (x * 4);
BYTE *src_bits = FreeImage_GetBits(src_dib);
unsigned alpha = 0;
unsigned height = FreeImage_GetHeight(src_dib);
unsigned line = FreeImage_GetLine(src_dib);
for(unsigned rows = 0; rows < height; rows++) {
for( unsigned cols = 0; cols < line; cols++ ){
#if 1
alpha = src_bits[cols+3];
if(alpha != 0){
dst_bits[cols] = (BYTE)(((src_bits[cols] - dst_bits[cols]) * alpha + (dst_bits[cols] << 8)) >> 8);
cols++;
dst_bits[cols] = (BYTE)(((src_bits[cols] - dst_bits[cols]) * alpha + (dst_bits[cols] << 8)) >> 8);
cols++;
dst_bits[cols] = (BYTE)(((src_bits[cols] - dst_bits[cols]) * alpha + (dst_bits[cols] << 8)) >> 8);
cols++;
if(alpha > dst_bits[cols]) dst_bits[cols] = alpha;
}else
cols+=3;
#else
alpha = src_bits[cols+3];
if(alpha != 0){
dst_bits[cols] = (BYTE)((((0xff-dst_bits[cols]) - dst_bits[cols]) * alpha + (dst_bits[cols] << 8)) >> 8);
cols++;
dst_bits[cols] = (BYTE)((((0xff-dst_bits[cols]) - dst_bits[cols]) * alpha + (dst_bits[cols] << 8)) >> 8);
cols++;
dst_bits[cols] = (BYTE)((((0xff-dst_bits[cols]) - dst_bits[cols]) * alpha + (dst_bits[cols] << 8)) >> 8);
cols++;
}else{
cols+=3;
}
#endif
}
dst_bits += FreeImage_GetPitch(dst_dib);
src_bits += FreeImage_GetPitch(src_dib);
}
return TRUE;
}
class BlurWorker : public Nan::AsyncWorker {
public:
BlurWorker(Nan::Callback *callback) : Nan::AsyncWorker(callback) {
save_format = FIF_PNG;
}
~BlurWorker() {}
// Executed inside the worker-thread.
// It is not safe to access V8, or V8 data structures
// here, so everything we need for input and output
// should go on `this`.
void Execute () {
FIBITMAP * fiBitmap = NULL, *thumbnail1 = NULL, *thumbnail2 = NULL , *tmpImage = NULL;
FREE_IMAGE_FORMAT format;
int bpp;
std::map<int,FIBITMAP *>::iterator iter;
int tw , th ;
fiMemoryIn = FreeImage_OpenMemory((BYTE *) imageBuffer, lengthBuffer);
format = FreeImage_GetFileTypeFromMemory(fiMemoryIn);
if (format < 0 /*|| FIF_GIF == format*/)
goto ret;
fiBitmap = FreeImage_LoadFromMemory(format, fiMemoryIn);
if (!fiBitmap)
goto ret;
// FILTER_BOX = 0, // Box, pulse, Fourier window, 1st order (constant) b-spline
// FILTER_BICUBIC = 1, // Mitchell & Netravali's two-param cubic filter
// FILTER_BILINEAR = 2, // Bilinear filter
// FILTER_BSPLINE * = 3, // 4th order (cubic) b-spline
// FILTER_CATMULLROM = 4, // Catmull-Rom spline, Overhauser spline
// FILTER_LANCZOS3 = 5 // Lanczos3 filter
width = FreeImage_GetWidth(fiBitmap);
height = FreeImage_GetHeight(fiBitmap);
bpp = FreeImage_GetBPP(fiBitmap);
if(bpp != 32){
tmpImage = FreeImage_ConvertTo32Bits(fiBitmap);
FreeImage_Unload(fiBitmap);
fiBitmap = tmpImage;
}
thumbnail1 = FreeImage_Rescale(fiBitmap, count_pixel,count_pixel, FILTER_BOX);
if (!thumbnail1)
goto ret;
thumbnail2 = FreeImage_Rescale( thumbnail1, width, height, FILTER_BOX );
if (!thumbnail2)
goto ret;
if(title_index){
iter = titles.find(title_index);
if (iter != titles.end() )
{
if(iter->second){
tw = FreeImage_GetWidth(iter->second);
th = FreeImage_GetHeight(iter->second);
Combine32( thumbnail2, iter->second, (width/2)-(tw/2) ,(height/2)-(th/2) );
}
}
}
if( FIF_JPEG == save_format ){
tmpImage = FreeImage_ConvertTo24Bits(thumbnail2);
FreeImage_Unload(thumbnail2);
thumbnail2 = tmpImage;
}
fiMemoryOut = FreeImage_OpenMemory();
FreeImage_SaveToMemory( save_format, thumbnail2, fiMemoryOut, 0 );
ret:
if (fiMemoryIn)
FreeImage_CloseMemory(fiMemoryIn);
if (fiBitmap)
FreeImage_Unload(fiBitmap);
if (thumbnail1)
FreeImage_Unload(thumbnail1);
if(thumbnail2)
FreeImage_Unload( thumbnail2 );
if(imageBuffer)
delete imageBuffer;
}
// Executed when the async work is complete
// this function will be run inside the main event loop
// so it is safe to use V8 again
void HandleOKCallback () {
Nan::HandleScope scope;
if ( fiMemoryOut ) {
const unsigned argc = 4;
const char* data;
int datalen;
FreeImage_AcquireMemory(fiMemoryOut,(BYTE**)&data, (DWORD*)&datalen );
Local<Value> argv[argc] = {
Nan::Null(),
Nan::CopyBuffer((char*)data,datalen).ToLocalChecked(),
Nan::New(width),Nan::New(height)
};
Nan::TryCatch try_catch;
callback->Call(Nan::GetCurrentContext()->Global(), argc, argv);
if (try_catch.HasCaught())
Nan::FatalException(try_catch);
} else {
Local < Value > err = Exception::Error(New("error").ToLocalChecked());
const unsigned argc = 1;
Local<Value> argv[argc] = {err};
Nan::TryCatch try_catch;
callback->Call(Nan::GetCurrentContext()->Global(), argc, argv);
if (try_catch.HasCaught())
Nan::FatalException(try_catch);
}
if (fiMemoryOut)
FreeImage_CloseMemory(fiMemoryOut);
}
FIMEMORY* fiMemoryOut;
FIMEMORY* fiMemoryIn;
char *imageBuffer;
size_t lengthBuffer;
int count_pixel;
int title_index;
FREE_IMAGE_FORMAT save_format;
int width , height;
//private:
};
NAN_METHOD(imageBlur) {
Nan::HandleScope scope;
int indexCallback = info.Length() - 1;
if (info.Length() < 2)
return Nan::ThrowError("Expecting 2 arguments");
if (!Buffer::HasInstance(info[0]))
return Nan::ThrowError("First argument must be a Buffer");
if (!info[indexCallback]->IsFunction())
return Nan::ThrowError( "no have callback" );
Nan::Callback *callback = new Nan::Callback(info[indexCallback].As<v8::Function>());
BlurWorker* worker = new BlurWorker(callback);
worker->count_pixel = 3;
worker->title_index = 0;
if( info.Length() > 2){
if(info[1]->IntegerValue())
worker->count_pixel = info[1]->IntegerValue();
else{
Local<Object> objOps = info[1]->ToObject();
if(objOps->Has(Nan::New("pixels").ToLocalChecked())){
worker->count_pixel = objOps->Get(Nan::New("pixels").ToLocalChecked())->IntegerValue();
}
if(objOps->Has(Nan::New("title").ToLocalChecked())){
worker->title_index = objOps->Get(Nan::New("title").ToLocalChecked())->IntegerValue();
}
if(objOps->Has(Nan::New("format").ToLocalChecked())){
worker->save_format = (FREE_IMAGE_FORMAT) objOps->Get(Nan::New("format").ToLocalChecked())->IntegerValue();
}
}
}
if( info.Length() > 3 ){
if(info[2]->IntegerValue())
worker->title_index = info[2]->IntegerValue();
}
Local<Value> buffer_obj = info[0];
worker->lengthBuffer = Buffer::Length(buffer_obj);
worker->imageBuffer = new char[worker->lengthBuffer];
std::memcpy(worker->imageBuffer, Buffer::Data(buffer_obj) , worker->lengthBuffer);
worker->fiMemoryIn = NULL;
worker->fiMemoryOut = NULL;
Nan::AsyncQueueWorker(worker);
}
NAN_METHOD(addImageTitle) {
Nan::HandleScope scope;
int index = 0;
FIBITMAP * imageTitle = NULL;
if (info.Length() < 2)
return Nan::ThrowError("Expecting 2 arguments" );
if (!Buffer::HasInstance(info[0]))
return Nan::ThrowError("First argument must be a Buffer");
if(info[1]->IntegerValue())
index = info[1]->IntegerValue();
Local<Value> buffer_obj = info[0];
FIMEMORY * fiMemoryIn = FreeImage_OpenMemory((BYTE *) Buffer::Data(buffer_obj), Buffer::Length(buffer_obj));
imageTitle = FreeImage_LoadFromMemory(FreeImage_GetFileTypeFromMemory(fiMemoryIn), fiMemoryIn);
if (fiMemoryIn)
FreeImage_CloseMemory(fiMemoryIn);
std::map<int,FIBITMAP *>::iterator iter = titles.find(index);
if (iter != titles.end() ){
if(iter->second) FreeImage_Unload( iter->second);
}
titles[index] = imageTitle;
info.GetReturnValue().SetUndefined();
}
NAN_MODULE_INIT(init) {
Nan::Set(target, Nan::New<String>("imageBlur").ToLocalChecked(),
GetFunction(Nan::New<FunctionTemplate>(imageBlur)).ToLocalChecked());
Nan::Set(target, Nan::New<String>("addImageTitle").ToLocalChecked(),
GetFunction(Nan::New<FunctionTemplate>(addImageTitle)).ToLocalChecked());
}
NODE_MODULE(imgnetfree, init)