Permalink
Browse files

Port to v0.3

  • Loading branch information...
1 parent e7d770d commit d48260e5779a4d99195c48a0965d23536434ca01 @ry ry committed with Jan 5, 2011
Showing with 118 additions and 25 deletions.
  1. +57 −0 src/buffer_compat.cpp
  2. +15 −0 src/buffer_compat.h
  3. +1 −0 src/common.h
  4. +11 −6 src/dynamic_png_stack.cpp
  5. +1 −1 src/dynamic_png_stack.h
  6. +7 −6 src/fixed_png_stack.cpp
  7. +1 −1 src/fixed_png_stack.h
  8. +23 −8 src/png.cpp
  9. +1 −2 src/png.h
  10. +1 −1 wscript
View
@@ -0,0 +1,57 @@
+#include <node.h>
+#include <node_buffer.h>
+#include <v8.h>
+#include "buffer_compat.h"
+
+
+#if NODE_MINOR_VERSION < 3
+
+
+char *BufferData(node::Buffer *b) {
+ return b->data();
+}
+
+
+size_t BufferLength(node::Buffer *b) {
+ return b->length();
+}
+
+
+char *BufferData(v8::Local<v8::Object> buf_obj) {
+ v8::HandleScope scope;
+ node::Buffer *buf = node::ObjectWrap::Unwrap<node::Buffer>(buf_obj);
+ return buf->data();
+}
+
+
+size_t BufferLength(v8::Local<v8::Object> buf_obj) {
+ v8::HandleScope scope;
+ node::Buffer *buf = node::ObjectWrap::Unwrap<node::Buffer>(buf_obj);
+ return buf->length();
+}
+
+#else // NODE_VERSION
+
+
+char *BufferData(node::Buffer *b) {
+ return node::Buffer::Data(b->handle_);
+}
+
+
+size_t BufferLength(node::Buffer *b) {
+ return node::Buffer::Length(b->handle_);
+}
+
+
+char *BufferData(v8::Local<v8::Object> buf_obj) {
+ v8::HandleScope scope;
+ return node::Buffer::Data(buf_obj);
+}
+
+
+size_t BufferLength(v8::Local<v8::Object> buf_obj) {
+ v8::HandleScope scope;
+ return node::Buffer::Length(buf_obj);
+}
+
+#endif // NODE_VERSION
View
@@ -0,0 +1,15 @@
+#ifndef buffer_compat_h
+#define buffer_compat_h
+
+#include <node.h>
+#include <node_buffer.h>
+#include <v8.h>
+
+char *BufferData(node::Buffer *b);
+size_t BufferLength(node::Buffer *b);
+
+char *BufferData(v8::Local<v8::Object> buf_obj);
+size_t BufferLength(v8::Local<v8::Object> buf_obj);
+
+
+#endif // buffer_compat_h
View
@@ -30,6 +30,7 @@ struct encode_request {
char *png;
int png_len;
char *error;
+ char *buf_data;
};
#endif
@@ -1,5 +1,6 @@
#include "png_encoder.h"
#include "dynamic_png_stack.h"
+#include "buffer_compat.h"
using namespace v8;
using namespace node;
@@ -71,10 +72,10 @@ DynamicPngStack::~DynamicPngStack()
}
Handle<Value>
-DynamicPngStack::Push(Buffer *buf, int x, int y, int w, int h)
+DynamicPngStack::Push(unsigned char *buf_data, size_t buf_len, int x, int y, int w, int h)
{
try {
- Png *png = new Png((unsigned char *)buf->data(), buf->length(), x, y, w, h);
+ Png *png = new Png(buf_data, buf_len, x, y, w, h);
png_stack.push_back(png);
return Undefined();
}
@@ -111,7 +112,7 @@ DynamicPngStack::PngEncodeSync()
free(data);
int png_len = encoder.get_png_len();
Buffer *retbuf = Buffer::New(png_len);
- memcpy(retbuf->data(), encoder.get_png(), png_len);
+ memcpy(BufferData(retbuf), encoder.get_png(), png_len);
return scope.Close(retbuf->handle_);
}
catch (const char *err) {
@@ -183,7 +184,6 @@ DynamicPngStack::Push(const Arguments &args)
if (!args[4]->IsInt32())
return VException("Fifth argument must be integer h.");
- Buffer *data = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
int x = args[1]->Int32Value();
int y = args[2]->Int32Value();
int w = args[3]->Int32Value();
@@ -199,7 +199,12 @@ DynamicPngStack::Push(const Arguments &args)
return VException("Height smaller than 0.");
DynamicPngStack *png_stack = ObjectWrap::Unwrap<DynamicPngStack>(args.This());
- return scope.Close(png_stack->Push(data, x, y, w, h));
+
+ Local<Object> buf_obj = args[0]->ToObject();
+ char *buf_data = BufferData(buf_obj);
+ size_t buf_len = BufferLength(buf_obj);
+
+ return scope.Close(png_stack->Push((unsigned char*)buf_data, buf_len, x, y, w, h));
}
Handle<Value>
@@ -286,7 +291,7 @@ DynamicPngStack::EIO_PngEncodeAfter(eio_req *req)
}
else {
Buffer *buf = Buffer::New(enc_req->png_len);
- memcpy(buf->data(), enc_req->png, enc_req->png_len);
+ memcpy(BufferData(buf), enc_req->png, enc_req->png_len);
argv[0] = buf->handle_;
argv[1] = png->Dimensions();
argv[2] = Undefined();
@@ -47,7 +47,7 @@ class DynamicPngStack : public node::ObjectWrap {
DynamicPngStack(buffer_type bbuf_type);
~DynamicPngStack();
- v8::Handle<v8::Value> Push(node::Buffer *buf, int x, int y, int w, int h);
+ v8::Handle<v8::Value> Push(unsigned char *buf_data, size_t buf_len, int x, int y, int w, int h);
v8::Handle<v8::Value> Dimensions();
v8::Handle<v8::Value> PngEncodeSync();
@@ -2,6 +2,7 @@
#include "png_encoder.h"
#include "fixed_png_stack.h"
+#include "buffer_compat.h"
using namespace v8;
using namespace node;
@@ -33,9 +34,8 @@ FixedPngStack::~FixedPngStack()
}
void
-FixedPngStack::Push(Buffer *buf, int x, int y, int w, int h)
+FixedPngStack::Push(unsigned char *buf_data, int x, int y, int w, int h)
{
- unsigned char *buf_data = (unsigned char *)buf->data();
int start = y*width*4 + x*4;
for (int i = 0; i < h; i++) {
unsigned char *datap = &data[start + i*width*4];
@@ -60,7 +60,7 @@ FixedPngStack::PngEncodeSync()
encoder.encode();
int png_len = encoder.get_png_len();
Buffer *retbuf = Buffer::New(png_len);
- memcpy(retbuf->data(), encoder.get_png(), png_len);
+ memcpy(BufferData(retbuf), encoder.get_png(), png_len);
return scope.Close(retbuf->handle_);
}
catch (const char *err) {
@@ -134,7 +134,6 @@ FixedPngStack::Push(const Arguments &args)
return VException("Fifth argument must be integer h.");
FixedPngStack *png_stack = ObjectWrap::Unwrap<FixedPngStack>(args.This());
- Buffer *data_buf = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
int x = args[1]->Int32Value();
int y = args[2]->Int32Value();
int w = args[3]->Int32Value();
@@ -157,7 +156,9 @@ FixedPngStack::Push(const Arguments &args)
if (y+h > png_stack->height)
return VException("Pushed PNG exceeds FixedPngStack's height.");
- png_stack->Push(data_buf, x, y, w, h);
+ char *buf_data = BufferData(args[0]->ToObject());
+
+ png_stack->Push((unsigned char*)buf_data, x, y, w, h);
return Undefined();
}
@@ -213,7 +214,7 @@ FixedPngStack::EIO_PngEncodeAfter(eio_req *req)
}
else {
Buffer *buf = Buffer::New(enc_req->png_len);
- memcpy(buf->data(), enc_req->png, enc_req->png_len);
+ memcpy(BufferData(buf), enc_req->png, enc_req->png_len);
argv[0] = buf->handle_;
argv[1] = Undefined();
}
@@ -19,7 +19,7 @@ class FixedPngStack : public node::ObjectWrap {
FixedPngStack(int wwidth, int hheight, buffer_type bbuf_type);
~FixedPngStack();
- void Push(node::Buffer *buf, int x, int y, int w, int h);
+ void Push(unsigned char *buf_data, int x, int y, int w, int h);
v8::Handle<v8::Value> PngEncodeSync();
static v8::Handle<v8::Value> New(const v8::Arguments &args);
View
@@ -3,6 +3,7 @@
#include "common.h"
#include "png_encoder.h"
#include "png.h"
+#include "buffer_compat.h"
using namespace v8;
using namespace node;
@@ -19,20 +20,24 @@ Png::Initialize(Handle<Object> target)
target->Set(String::NewSymbol("Png"), t->GetFunction());
}
-Png::Png(Buffer *ddata, int wwidth, int hheight, buffer_type bbuf_type) :
- data(ddata), width(wwidth), height(hheight), buf_type(bbuf_type) {}
+Png::Png(int wwidth, int hheight, buffer_type bbuf_type) :
+ width(wwidth), height(hheight), buf_type(bbuf_type) {}
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 *)data->data(), width, height, buf_type);
+ PngEncoder encoder((unsigned char*)buf_data, width, height, buf_type);
encoder.encode();
int png_len = encoder.get_png_len();
Buffer *retbuf = Buffer::New(png_len);
- memcpy(retbuf->data(), encoder.get_png(), png_len);
+ memcpy(BufferData(retbuf), encoder.get_png(), png_len);
return scope.Close(retbuf->handle_);
}
catch (const char *err) {
@@ -78,7 +83,7 @@ Png::New(const Arguments &args)
return VException("Fourth argument wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
}
- Buffer *data = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
+
int w = args[1]->Int32Value();
int h = args[2]->Int32Value();
@@ -87,8 +92,12 @@ Png::New(const Arguments &args)
if (h < 0)
return VException("Height smaller than 0.");
- Png *png = new Png(data, w, h, buf_type);
+ Png *png = new Png(w, h, buf_type);
png->Wrap(args.This());
+
+ // Save buffer.
+ png->handle_->SetHiddenValue(String::New("buffer"), args[0]);
+
return args.This();
}
@@ -107,7 +116,7 @@ Png::EIO_PngEncode(eio_req *req)
Png *png = (Png *)enc_req->png_obj;
try {
- PngEncoder encoder((unsigned char *)png->data->data(), png->width, png->height, png->buf_type);
+ PngEncoder encoder((unsigned char *)enc_req->buf_data, png->width, png->height, png->buf_type);
encoder.encode();
enc_req->png_len = encoder.get_png_len();
enc_req->png = (char *)malloc(sizeof(*enc_req->png)*enc_req->png_len);
@@ -142,7 +151,7 @@ Png::EIO_PngEncodeAfter(eio_req *req)
}
else {
Buffer *buf = Buffer::New(enc_req->png_len);
- memcpy(buf->data(), enc_req->png, enc_req->png_len);
+ memcpy(BufferData(buf), enc_req->png, enc_req->png_len);
argv[0] = buf->handle_;
argv[1] = Undefined();
}
@@ -188,6 +197,12 @@ Png::PngEncodeAsync(const Arguments &args)
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());
+
eio_custom(EIO_PngEncode, EIO_PRI_DEFAULT, EIO_PngEncodeAfter, enc_req);
ev_ref(EV_DEFAULT_UC);
View
@@ -9,14 +9,13 @@
class Png : public node::ObjectWrap {
int width;
int height;
- node::Buffer *data;
buffer_type buf_type;
static int EIO_PngEncode(eio_req *req);
static int EIO_PngEncodeAfter(eio_req *req);
public:
static void Initialize(v8::Handle<v8::Object> target);
- Png(node::Buffer *ddata, int wwidth, int hheight, buffer_type bbuf_type);
+ Png(int wwidth, int hheight, buffer_type bbuf_type);
v8::Handle<v8::Value> PngEncodeSync();
static v8::Handle<v8::Value> New(const v8::Arguments &args);
View
@@ -17,7 +17,7 @@ def configure(conf):
def build(bld):
obj = bld.new_task_gen("cxx", "shlib", "node_addon")
obj.target = "png"
- obj.source = "src/common.cpp src/png_encoder.cpp src/png.cpp src/fixed_png_stack.cpp src/dynamic_png_stack.cpp src/module.cpp"
+ obj.source = "src/common.cpp src/png_encoder.cpp src/png.cpp src/fixed_png_stack.cpp src/dynamic_png_stack.cpp src/module.cpp src/buffer_compat.cpp"
obj.uselib = "PNG"
obj.cxxflags = ["-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE"]

0 comments on commit d48260e

Please sign in to comment.