-
Notifications
You must be signed in to change notification settings - Fork 53
/
png.cpp
231 lines (184 loc) · 6.46 KB
/
png.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
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
#include <cstring>
#include <cstdlib>
#include "common.h"
#include "png_encoder.h"
#include "png.h"
#include "buffer_compat.h"
using namespace v8;
using namespace node;
void
Png::Initialize(Handle<Object> target)
{
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "encode", PngEncodeAsync);
NODE_SET_PROTOTYPE_METHOD(t, "encodeSync", PngEncodeSync);
target->Set(String::NewSymbol("Png"), t->GetFunction());
}
Png::Png(int wwidth, int hheight, buffer_type bbuf_type, int bbits) :
width(wwidth), height(hheight), buf_type(bbuf_type), bits(bbits) {}
Handle<Value>
Png::PngEncodeSync()
{
HandleScope scope;
Local<Value> buf_val = handle_->GetHiddenValue(String::New("buffer"));
char *buf_data = BufferData(buf_val->ToObject());
try {
PngEncoder encoder((unsigned char*)buf_data, width, height, buf_type, bits);
encoder.encode();
int png_len = encoder.get_png_len();
Buffer *retbuf = Buffer::New(png_len);
memcpy(BufferData(retbuf), encoder.get_png(), png_len);
return scope.Close(retbuf->handle_);
}
catch (const char *err) {
return VException(err);
}
}
Handle<Value>
Png::New(const Arguments &args)
{
HandleScope scope;
if (args.Length() < 3)
return VException("At least three arguments required - data buffer, width, height, [and input buffer type]");
if (!Buffer::HasInstance(args[0]))
return VException("First argument must be Buffer.");
if (!args[1]->IsInt32())
return VException("Second argument must be integer width.");
if (!args[2]->IsInt32())
return VException("Third argument must be integer height.");
buffer_type buf_type = BUF_RGB;
if (args.Length() >= 4) {
if (!args[3]->IsString())
return VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.");
String::AsciiValue bts(args[3]->ToString());
if (!(str_eq(*bts, "rgb") || str_eq(*bts, "bgr") ||
str_eq(*bts, "rgba") || str_eq(*bts, "bgra") ||
str_eq(*bts, "gray")))
{
return VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.");
}
if (str_eq(*bts, "rgb"))
buf_type = BUF_RGB;
else if (str_eq(*bts, "bgr"))
buf_type = BUF_BGR;
else if (str_eq(*bts, "rgba"))
buf_type = BUF_RGBA;
else if (str_eq(*bts, "bgra"))
buf_type = BUF_BGRA;
else if (str_eq(*bts, "gray"))
buf_type = BUF_GRAY;
else
return VException("Fourth argument wasn't 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.");
}
int bits = 8;
if (args.Length() >= 5) {
if(buf_type != BUF_GRAY)
return VException("Pixel bit width option only valid for \"gray\" buffer type");
if(!args[4]->IsInt32())
return VException("Fifth argument must be 8 or 16");
if(args[4]->Int32Value() == 8)
bits = 8;
else if (args[4]->Int32Value() == 16)
bits = 16;
else
return VException("Fifth arguments wasn't 8 or 16");
}
int w = args[1]->Int32Value();
int h = args[2]->Int32Value();
if (w < 0)
return VException("Width smaller than 0.");
if (h < 0)
return VException("Height smaller than 0.");
Png *png = new Png(w, h, buf_type, bits);
png->Wrap(args.This());
// Save buffer.
png->handle_->SetHiddenValue(String::New("buffer"), args[0]);
return args.This();
}
Handle<Value>
Png::PngEncodeSync(const Arguments &args)
{
HandleScope scope;
Png *png = ObjectWrap::Unwrap<Png>(args.This());
return scope.Close(png->PngEncodeSync());
}
void
Png::UV_PngEncode(uv_work_t* req)
{
encode_request *enc_req = (encode_request *)req->data;
Png *png = (Png *)enc_req->png_obj;
try {
PngEncoder encoder((unsigned char *)enc_req->buf_data, png->width, png->height, png->buf_type, png->bits);
encoder.encode();
enc_req->png_len = encoder.get_png_len();
enc_req->png = (char *)malloc(sizeof(*enc_req->png)*enc_req->png_len);
if (!enc_req->png) {
enc_req->error = strdup("malloc in Png::UV_PngEncode failed.");
return;
}
else {
memcpy(enc_req->png, encoder.get_png(), enc_req->png_len);
}
}
catch (const char *err) {
enc_req->error = strdup(err);
}
}
void
Png::UV_PngEncodeAfter(uv_work_t *req)
{
HandleScope scope;
encode_request *enc_req = (encode_request *)req->data;
delete req;
Handle<Value> argv[2];
if (enc_req->error) {
argv[0] = Undefined();
argv[1] = ErrorException(enc_req->error);
}
else {
Buffer *buf = Buffer::New(enc_req->png_len);
memcpy(BufferData(buf), enc_req->png, enc_req->png_len);
argv[0] = buf->handle_;
argv[1] = Undefined();
}
TryCatch try_catch; // don't quite see the necessity of this
enc_req->callback->Call(Context::GetCurrent()->Global(), 2, argv);
if (try_catch.HasCaught())
FatalException(try_catch);
enc_req->callback.Dispose();
free(enc_req->png);
free(enc_req->error);
((Png *)enc_req->png_obj)->Unref();
free(enc_req);
}
Handle<Value>
Png::PngEncodeAsync(const Arguments &args)
{
HandleScope scope;
if (args.Length() != 1)
return VException("One argument required - callback function.");
if (!args[0]->IsFunction())
return VException("First argument must be a function.");
Local<Function> callback = Local<Function>::Cast(args[0]);
Png *png = ObjectWrap::Unwrap<Png>(args.This());
encode_request *enc_req = (encode_request *)malloc(sizeof(*enc_req));
if (!enc_req)
return VException("malloc in Png::PngEncodeAsync failed.");
enc_req->callback = Persistent<Function>::New(callback);
enc_req->png_obj = png;
enc_req->png = NULL;
enc_req->png_len = 0;
enc_req->error = NULL;
// We need to pull out the buffer data before
// we go to the thread pool.
Local<Value> buf_val = png->handle_->GetHiddenValue(String::New("buffer"));
enc_req->buf_data = BufferData(buf_val->ToObject());
uv_work_t* req = new uv_work_t;
req->data = enc_req;
uv_queue_work(uv_default_loop(), req, UV_PngEncode, (uv_after_work_cb)UV_PngEncodeAfter);
png->Ref();
return Undefined();
}
NODE_MODULE(png, Png::Initialize)