-
Notifications
You must be signed in to change notification settings - Fork 3
/
jpeg.cc
370 lines (265 loc) · 7.78 KB
/
jpeg.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <node.h>
#include <node_buffer.h>
#include <v8.h>
#include <sys/stat.h>
#include <fstream>
#include <cstring>
#include <string>
#include <turbojpeg.h>
#include <jpeglib.h>
#include <node.h>
#include <v8.h>
#include <cmath>
#include <cstdlib>
#include <setjmp.h>
void scale(void* img, size_t width, size_t height, void* output, size_t newWidth, size_t newHeight);
using namespace v8;
typedef enum ResultCode
{
OK,
ERROR
} ResultCode;
struct jpeg_error_manager
{
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
};
struct jpeg_error_mgr jerr;
void jpeg_ignore_error(j_common_ptr cinfo)
{
}
void jpeg_error_handler(j_common_ptr cinfo)
{
cinfo->err->error_exit = jpeg_ignore_error;
}
static void set_scale(struct jpeg_decompress_struct* cinfo, size_t width, size_t height, size_t resize_width, size_t resize_height)
{
if (resize_width > width && resize_height > height)
{
return;
}
int coef_width = INT_MAX;
int coef_height = INT_MAX;
if (resize_width != 0)
{
coef_width = width / resize_width;
}
if (resize_height != 0)
{
coef_height = height / resize_height;
}
int coef = coef_width < coef_height ? coef_width : coef_height;
if (coef <= 1)
{
return;
}
if (coef > 16)
{
coef = 16;
}
cinfo->scale_num = 1;
cinfo->scale_denom = coef;
jpeg_calc_output_dimensions(cinfo);
}
#define CINFO_HAS_ERROR (cinfo.err->error_exit != jpeg_error_handler)
#define DECOMPRESS_ERROR_CHECKPOINT if CINFO_HAS_ERROR { jpeg_destroy_decompress(&cinfo); fprintf(stderr, "decompression failed"); return ERROR; }
ResultCode decompress(void* data, size_t size, void** output, size_t& outputWidth, size_t& outputHeight, size_t& outputSize)
{
struct jpeg_error_mgr mgr;
jpeg_std_error(&mgr);
mgr.error_exit = jpeg_error_handler;
struct jpeg_decompress_struct cinfo;
jpeg_create_decompress(&cinfo);
cinfo.err = &mgr;
jpeg_mem_src(&cinfo, (unsigned char*)data, size);
DECOMPRESS_ERROR_CHECKPOINT
if (JPEG_HEADER_OK != jpeg_read_header(&cinfo, TRUE))
{
fprintf(stderr, "Failed to read JPEG header");
jpeg_destroy_decompress(&cinfo);
return ERROR;
}
jpeg_calc_output_dimensions(&cinfo);
set_scale(&cinfo, cinfo.output_width, cinfo.output_height, outputWidth, outputHeight);
outputWidth = cinfo.output_width;
outputHeight = cinfo.output_height;
cinfo.output_components = 4;
cinfo.out_color_space = JCS_EXT_RGBA;
outputSize = cinfo.output_width * cinfo.output_height * cinfo.output_components;
jpeg_start_decompress(&cinfo);
DECOMPRESS_ERROR_CHECKPOINT
*output = malloc(outputSize);
void* row_pointer[1];
while (cinfo.output_scanline < cinfo.output_height)
{
row_pointer[0] = &((uint8_t*)*output)[cinfo.output_scanline * cinfo.output_width * cinfo.output_components];
if (1 != jpeg_read_scanlines(&cinfo, (JSAMPARRAY)row_pointer, 1))
{
break;
}
DECOMPRESS_ERROR_CHECKPOINT
}
jpeg_finish_decompress(&cinfo);
DECOMPRESS_ERROR_CHECKPOINT
jpeg_destroy_decompress(&cinfo);
return OK;
}
ResultCode compress(void* data, size_t width, size_t height, int quality, void** output, size_t& outputSize)
{
tjhandle compressor = tjInitCompress();
*output = NULL;
int compressStatus = tjCompress2(compressor, (uint8_t*)data, width, 0, height, TJPF_RGBA,
(uint8_t**)output, &outputSize, TJSAMP_444, quality, TJFLAG_FASTDCT);
tjDestroy(compressor);
return (compressStatus == 0) ? OK : ERROR;
}
bool readFile(const char* path, void** buffer, size_t* size)
{
FILE* f = fopen(path, "r");
if (f == NULL)
{
return false;
}
struct stat fileStat;
if (0 != stat(path, &fileStat))
{
fclose(f);
return false;
}
*size = fileStat.st_size;
*buffer = malloc(fileStat.st_size);
fread(*buffer, fileStat.st_blksize, fileStat.st_blocks, f);
fclose(f);
return true;
}
ResultCode decompressAndScale(const char* filepath, void** buffer, size_t& size, size_t& targetWidth, size_t& targetHeight)
{
size_t width = targetWidth;
size_t height = targetHeight;
void* decompressed = NULL;
{
size_t originalSize;
bool gotFile = readFile(filepath, buffer, &originalSize);
ResultCode result;
if (gotFile)
{
result = decompress(*buffer, originalSize, &decompressed, width, height, size);
}
if (*buffer != NULL)
{
free(*buffer);
}
if (!gotFile || OK != result)
{
if (decompressed != NULL)
{
free(decompressed);
}
return ERROR;
}
}
if (width <= targetWidth && height <= targetHeight)
{
targetWidth = width;
targetHeight = height;
*buffer = decompressed;
return OK;
}
if (width > height)
{
targetHeight = round(double(height) / width * targetWidth);
}
else
{
targetWidth = round(double(width) / height * targetHeight);
}
size = targetWidth * targetHeight * 4;
*buffer = malloc(size);
scale(decompressed, width, height, *buffer, targetWidth, targetHeight);
free(decompressed);
return OK;
}
void decompressFunction(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
std::string filepath(*v8::String::Utf8Value(args[0]));
size_t targetWidth = args[1]->NumberValue();
size_t targetHeight = args[2]->NumberValue();
Local<Function> cb = Local<Function>::Cast(args[3]);
void* buffer = NULL;
size_t size = 0;
ResultCode decompressStatus = decompressAndScale(filepath.c_str(), &buffer, size, targetWidth, targetHeight);
if (OK != decompressStatus)
{
const unsigned argc = 1;
Local<Value> argv[argc] =
{
v8::Null(isolate)
};
Local<Function> cb = Local<Function>::Cast(args[1]);
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}
const unsigned argc = 3;
Local<Value> argv[argc] =
{
node::Buffer::New(isolate, (const char*)buffer, size),
Number::New(isolate, double(targetWidth)),
Number::New(isolate, double(targetHeight))
};
free(buffer);
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}
void scaleFunction(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
std::string filepath(*v8::String::Utf8Value(args[0]));
size_t targetWidth = args[1]->NumberValue();
size_t targetHeight = args[2]->NumberValue();
Local<Function> cb = Local<Function>::Cast(args[3]);
void* buffer = NULL;
size_t size = 0;
ResultCode decompressStatus = decompressAndScale(filepath.c_str(), &buffer, size, targetWidth, targetHeight);
if (OK != decompressStatus)
{
const unsigned argc = 1;
Local<Value> argv[argc] =
{
v8::Null(isolate)
};
Local<Function> cb = Local<Function>::Cast(args[1]);
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
return;
}
//ssize_t compress(void* data, size_t width, size_t height, int quality, void** output)
void* jpegData = NULL;
size_t jpegSize;
ResultCode compressStatus = compress(buffer, targetWidth, targetHeight, 80, &jpegData, jpegSize);
free(buffer);
if (OK != compressStatus || jpegData == NULL || jpegSize == 0)
{
const unsigned argc = 1;
Local<Value> argv[argc] =
{
v8::Null(isolate)
};
Local<Function> cb = Local<Function>::Cast(args[1]);
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
return;
}
const unsigned argc = 3;
Local<Value> argv[argc] =
{
node::Buffer::New(isolate, (const char*)jpegData, jpegSize),
Number::New(isolate, double(targetWidth)),
Number::New(isolate, double(targetHeight))
};
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
}
void init(Handle<Object> exports)
{
NODE_SET_METHOD(exports, "decompress", decompressFunction);
NODE_SET_METHOD(exports, "scale", scaleFunction);
}
NODE_MODULE(addon, init)