include node include node_buffer include v8 include sys stat include f

  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
#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>
#include "bicubic.cc"
using namespace v8;
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)
{
//DBG("JPEG ERROR HANDLER CALLED");
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)
{
// This functions must handle zero resize height or width. One of them should always be non-zero.
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 -1; }
ssize_t decode(void* data, size_t size, void** output, size_t* outputWidth, size_t* outputHeight)
{
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 -1;
}
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;
size_t 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 outputSize;
}
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;
}
void RunCallback(const FunctionCallbackInfo<Value>& args)
{
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
std::string tempString(*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]);
size_t originalSize;
size_t width = targetWidth;
size_t height = targetHeight;
ssize_t size;
void* decompressed = NULL;
{
void* buffer = NULL;
readFile(tempString.c_str(), &buffer, &originalSize);
size = decode(buffer, originalSize, &decompressed, &width, &height);
free(buffer);
if (size == -1)
{
if (buffer != NULL)
{
free(buffer);
}
if (decompressed != NULL)
{
free(decompressed);
}
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);
}
}
if (width > height)
{
targetHeight = round(double(height) / width * targetWidth);
}
else
{
targetWidth = round(double(width) / height * targetHeight);
}
size = targetWidth * targetHeight * 4;
void* scaledOutput = malloc(size);
interpolate(decompressed, width, height, scaledOutput, targetWidth, targetHeight);
free(decompressed);
const unsigned argc = 3;
Local<Value> argv[argc] =
{
node::Buffer::New(isolate, (const char*)scaledOutput, size),
Number::New(isolate, double(targetWidth)),
Number::New(isolate, double(targetHeight))
};
cb->Call(isolate->GetCurrentContext()->Global(), argc, argv);
free(scaledOutput);
}
void init(Handle<Object> exports)
{
NODE_SET_METHOD(exports, "scale", RunCallback);
}
NODE_MODULE(addon, init)