-
Notifications
You must be signed in to change notification settings - Fork 0
/
resizer.cc
196 lines (145 loc) · 4.62 KB
/
resizer.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
/* This code is PUBLIC DOMAIN, and is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND. See the accompanying
* LICENSE file.
*/
#include <v8.h>
#include <node.h>
#include <magick/api.h>
#include <string.h>
#include <unistd.h>
using namespace node;
using namespace v8;
#define REQ_FUN_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsFunction()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a function"))); \
Local<Function> VAR = Local<Function>::Cast(args[I]);
#define REQ_NUM_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsInt32()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a number"))); \
int VAR = args[I]->Int32Value();
#define REQ_STR_ARG(I, VAR) \
if (args.Length() <= (I) || !args[I]->IsString()) \
return ThrowException(Exception::TypeError( \
String::New("Argument " #I " must be a string"))); \
v8::String::AsciiValue VAR(args[I]);
class Resizer: ObjectWrap
{
public:
static Persistent<FunctionTemplate> s_ct;
static void Init(Handle<Object> target)
{
InitializeMagick("./");
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
s_ct = Persistent<FunctionTemplate>::New(t);
s_ct->InstanceTemplate()->SetInternalFieldCount(1);
s_ct->SetClassName(String::NewSymbol("Resizer"));
NODE_SET_PROTOTYPE_METHOD(s_ct, "resize", Resize);
target->Set(String::NewSymbol("Resizer"),
s_ct->GetFunction());
}
Resizer()
{
}
~Resizer()
{
}
static Handle<Value> New(const Arguments& args)
{
HandleScope scope;
Resizer* resizer = new Resizer();
resizer->Wrap(args.This());
return args.This();
}
struct resize_baton_t {
Resizer *resizer;
char* from;
char* to;
int width;
int height;
Persistent<Function> cb;
char* err;
};
static Handle<Value> Resize(const Arguments& args)
{
HandleScope scope;
REQ_STR_ARG(0, from);
REQ_STR_ARG(1, to);
REQ_NUM_ARG(2, width);
REQ_NUM_ARG(3, height);
REQ_FUN_ARG(4, cb);
Resizer* resizer = ObjectWrap::Unwrap<Resizer>(args.This());
resize_baton_t *baton = new resize_baton_t();
baton->resizer = resizer;
baton->from = new char[(from.length() + 1)];
strcpy(baton->from, *from);
baton->to = new char[(to.length() + 1)];
strcpy(baton->to, *to);
baton->width = width;
baton->height = height;
baton->cb = Persistent<Function>::New(cb);
baton->err = NULL;
resizer->Ref();
eio_custom(EIO_Resize, EIO_PRI_DEFAULT, EIO_AfterResize, baton);
ev_ref(EV_DEFAULT_UC);
return Undefined();
}
static int EIO_Resize(eio_req *req)
{
resize_baton_t *baton = static_cast<resize_baton_t *>(req->data);
ExceptionInfo exception;
ImageInfo *info = CloneImageInfo((ImageInfo *) NULL);
Image *image = NULL;
Image *thumb = NULL;
GetExceptionInfo(&exception);
printf("To %s From %s Size %dx%d\n", baton->to, baton->from, baton->width, baton->height);
strcpy(info->filename, baton->from);
image = ReadImage(info, &exception);
if (!image)
goto CLEANUP;
thumb = ThumbnailImage(image, baton->width, baton->height, &exception);
if (!thumb)
goto CLEANUP;
strcpy(info->filename, baton->to);
WriteImage(info, thumb);
CLEANUP:
// if (exception->error_number)
// baton->err = GetExceptionMessage(exception->error_number);
if (image)
DestroyImage(image);
if (thumb)
DestroyImage(thumb);
DestroyImageInfo(info);
DestroyExceptionInfo(&exception);
return 0;
}
static int EIO_AfterResize(eio_req *req)
{
HandleScope scope;
resize_baton_t *baton = static_cast<resize_baton_t *>(req->data);
ev_unref(EV_DEFAULT_UC);
baton->resizer->Unref();
Local<Value> argv[1];
argv[0] = String::New(baton->err ? baton->err : "");
TryCatch try_catch;
baton->cb->Call(Context::GetCurrent()->Global(), 1, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
baton->cb.Dispose();
delete baton->to;
delete baton->from;
delete baton;
return 0;
}
};
Persistent<FunctionTemplate> Resizer::s_ct;
extern "C" {
static void init (Handle<Object> target)
{
Resizer::Init(target);
}
NODE_MODULE(resizer, init);
}