Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

FixedPngStack now returns Buffer instead of binary string

  • Loading branch information...
commit 71c83b2586b17d79154190878bda59a763873f50 1 parent 82942bf
@pkrumins authored
Showing with 12 additions and 18 deletions.
  1. +12 −18 src/fixed_png_stack.cpp
View
30 src/fixed_png_stack.cpp
@@ -56,9 +56,12 @@ FixedPngStack::PngEncodeSync()
buffer_type pbt = (buf_type == BUF_BGR || buf_type == BUF_BGRA) ? BUF_BGRA : BUF_RGBA;
try {
- PngEncoder p(data, width, height, pbt);
- p.encode();
- return scope.Close(Encode((char *)p.get_png(), p.get_png_len(), BINARY));
+ PngEncoder encoder(data, width, height, pbt);
+ encoder.encode();
+ int png_len = encoder.get_png_len();
+ Buffer *retbuf = Buffer::New(png_len);
+ memcpy(retbuf->data(), encoder.get_png(), png_len);
+ return scope.Close(retbuf->handle_);
}
catch (const char *err) {
return VException(err);
@@ -175,17 +178,11 @@ FixedPngStack::EIO_PngEncode(eio_req *req)
FixedPngStack *png = (FixedPngStack *)enc_req->png_obj;
try {
- PngEncoder p(png->data, png->width, png->height, png->buf_type);
- p.encode();
- enc_req->png_len = p.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 FixedPngStack::EIO_PngEncode failed.");
- return 0;
- }
- else {
- memcpy(enc_req->png, p.get_png(), enc_req->png_len);
- }
+ PngEncoder encoder(png->data, png->width, png->height, png->buf_type);
+ encoder.encode();
+ int png_len = encoder.get_png_len();
+ enc_req->png_buf = Buffer::New(png_len);
+ memcpy(enc_req->png_buf->data(), encoder.get_png(), png_len);
}
catch (const char *err) {
enc_req->error = strdup(err);
@@ -209,7 +206,7 @@ FixedPngStack::EIO_PngEncodeAfter(eio_req *req)
argv[1] = ErrorException(enc_req->error);
}
else {
- argv[0] = Local<Value>::New(Encode(enc_req->png, enc_req->png_len, BINARY));
+ argv[0] = enc_req->png_buf->handle_;
argv[1] = Undefined();
}
@@ -221,7 +218,6 @@ FixedPngStack::EIO_PngEncodeAfter(eio_req *req)
FatalException(try_catch);
enc_req->callback.Dispose();
- free(enc_req->png);
free(enc_req->error);
((FixedPngStack *)enc_req->png_obj)->Unref();
@@ -250,8 +246,6 @@ FixedPngStack::PngEncodeAsync(const Arguments &args)
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;
eio_custom(EIO_PngEncode, EIO_PRI_DEFAULT, EIO_PngEncodeAfter, enc_req);
Please sign in to comment.
Something went wrong with that request. Please try again.