Skip to content

Commit

Permalink
upgrade to node 0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mnutt committed Mar 18, 2011
1 parent 9b3af33 commit 8be64dc
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 43 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
/gif.node
/build/
.lock-wscript
18 changes: 10 additions & 8 deletions src/animated_gif.cpp
Expand Up @@ -3,6 +3,7 @@
#include "common.h"
#include "gif_encoder.h"
#include "animated_gif.h"
#include "buffer_compat.h"

using namespace v8;
using namespace node;
Expand Down Expand Up @@ -105,7 +106,7 @@ AnimatedGif::New(const Arguments &args)
{
return VException("Third argument must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
}

if (str_eq(*bts, "rgb"))
buf_type = BUF_RGB;
else if (str_eq(*bts, "bgr"))
Expand Down Expand Up @@ -148,7 +149,6 @@ AnimatedGif::Push(const Arguments &args)
return VException("Fifth argument must be integer h.");

AnimatedGif *gif = ObjectWrap::Unwrap<AnimatedGif>(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();
Expand All @@ -162,17 +162,19 @@ AnimatedGif::Push(const Arguments &args)
return VException("Width smaller than 0.");
if (h < 0)
return VException("Height smaller than 0.");
if (x >= gif->width)
if (x >= gif->width)
return VException("Coordinate x exceeds AnimatedGif's dimensions.");
if (y >= gif->height)
if (y >= gif->height)
return VException("Coordinate y exceeds AnimatedGif's dimensions.");
if (x+w > gif->width)
if (x+w > gif->width)
return VException("Pushed fragment exceeds AnimatedGif's width.");
if (y+h > gif->height)
if (y+h > gif->height)
return VException("Pushed fragment exceeds AnimatedGif's height.");

try {
gif->Push((unsigned char *)data_buf->data(), x, y, w, h);
char *buf_data = BufferData(args[0]->ToObject());

gif->Push((unsigned char*)buf_data, x, y, w, h);
}
catch (const char *err) {
return VException(err);
Expand Down Expand Up @@ -206,7 +208,7 @@ AnimatedGif::GetGif(const Arguments &args)
gif->gif_encoder.finish();
int gif_len = gif->gif_encoder.get_gif_len();
Buffer *retbuf = Buffer::New(gif_len);
memcpy(retbuf->data(), gif->gif_encoder.get_gif(), gif_len);
memcpy(BufferData(retbuf), gif->gif_encoder.get_gif(), gif_len);
return scope.Close(retbuf->handle_);
}

Expand Down
22 changes: 12 additions & 10 deletions src/async_animated_gif.cpp
Expand Up @@ -5,6 +5,7 @@
#include "utils.h"
#include "gif_encoder.h"
#include "async_animated_gif.h"
#include "buffer_compat.h"

#include "loki/ScopeGuard.h"

Expand Down Expand Up @@ -81,7 +82,7 @@ int
AsyncAnimatedGif::EIO_PushAfter(eio_req *req)
{
ev_unref(EV_DEFAULT_UC);

push_request *push_req = (push_request *)req->data;
free(push_req->data);
free(push_req);
Expand Down Expand Up @@ -156,7 +157,7 @@ AsyncAnimatedGif::New(const Arguments &args)
{
return VException("Third argument must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
}

if (str_eq(*bts, "rgb"))
buf_type = BUF_RGB;
else if (str_eq(*bts, "bgr"))
Expand Down Expand Up @@ -199,8 +200,8 @@ AsyncAnimatedGif::Push(const Arguments &args)
return VException("Fifth argument must be integer h.");

AsyncAnimatedGif *gif = ObjectWrap::Unwrap<AsyncAnimatedGif>(args.This());
Buffer *data_buf = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
unsigned char *buf = (unsigned char *)data_buf->data();
// Buffer *data_buf = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
// unsigned char *buf = (unsigned char *)data_buf->data();
int x = args[1]->Int32Value();
int y = args[2]->Int32Value();
int w = args[3]->Int32Value();
Expand All @@ -214,17 +215,18 @@ AsyncAnimatedGif::Push(const Arguments &args)
return VException("Width smaller than 0.");
if (h < 0)
return VException("Height smaller than 0.");
if (x >= gif->width)
if (x >= gif->width)
return VException("Coordinate x exceeds AsyncAnimatedGif's dimensions.");
if (y >= gif->height)
if (y >= gif->height)
return VException("Coordinate y exceeds AsyncAnimatedGif's dimensions.");
if (x+w > gif->width)
if (x+w > gif->width)
return VException("Pushed fragment exceeds AsyncAnimatedGif's width.");
if (y+h > gif->height)
if (y+h > gif->height)
return VException("Pushed fragment exceeds AsyncAnimatedGif's height.");

try {
gif->Push(buf, x, y, w, h);
char *buf_data = BufferData(args[0]->ToObject());
gif->Push((unsigned char*)buf_data, x, y, w, h);
}
catch (const char *err) {
return VException(err);
Expand Down Expand Up @@ -368,7 +370,7 @@ AsyncAnimatedGif::EIO_Encode(eio_req *req)
return 0;
}
Rect dims = rect_dims(fragments[i]);
push_fragment(frame, gif->width, gif->height, gif->buf_type,
push_fragment(frame, gif->width, gif->height, gif->buf_type,
data, dims.x, dims.y, dims.w, dims.h);
}
encoder.new_frame(frame);
Expand Down
58 changes: 58 additions & 0 deletions src/buffer_compat.cpp
@@ -0,0 +1,58 @@
#include <node.h>
#include <node_buffer.h>
#include <node_version.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
15 changes: 15 additions & 0 deletions src/buffer_compat.h
@@ -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
1 change: 1 addition & 0 deletions src/common.h
Expand Up @@ -38,6 +38,7 @@ struct encode_request {
char *gif;
int gif_len;
char *error;
char *buf_data;
};

#endif
Expand Down
22 changes: 13 additions & 9 deletions src/dynamic_gif_stack.cpp
@@ -1,6 +1,7 @@
#include "common.h"
#include "gif_encoder.h"
#include "dynamic_gif_stack.h"
#include "buffer_compat.h"

using namespace v8;
using namespace node;
Expand Down Expand Up @@ -93,11 +94,10 @@ DynamicGifStack::~DynamicGifStack()
}

Handle<Value>
DynamicGifStack::Push(Buffer *buf, int x, int y, int w, int h)
DynamicGifStack::Push(unsigned char *buf_data, size_t buf_len, int x, int y, int w, int h)
{
try {
GifUpdate *gif_update =
new GifUpdate((unsigned char *)buf->data(), buf->length(), x, y, w, h);
GifUpdate *gif_update = new GifUpdate(buf_data, buf_len, x, y, w, h);
gif_stack.push_back(gif_update);
return Undefined();
}
Expand Down Expand Up @@ -137,7 +137,7 @@ DynamicGifStack::GifEncodeSync()
free(data);
int gif_len = encoder.get_gif_len();
Buffer *retbuf = Buffer::New(gif_len);
memcpy(retbuf->data(), encoder.get_gif(), gif_len);
memcpy(BufferData(retbuf), encoder.get_gif(), gif_len);
return scope.Close(retbuf->handle_);
}
catch (const char *err) {
Expand Down Expand Up @@ -175,7 +175,7 @@ DynamicGifStack::New(const Arguments &args)
{
return VException("First argument must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
}

if (str_eq(*bts, "rgb"))
buf_type = BUF_RGB;
else if (str_eq(*bts, "bgr"))
Expand Down Expand Up @@ -209,7 +209,6 @@ DynamicGifStack::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();
Expand All @@ -225,7 +224,12 @@ DynamicGifStack::Push(const Arguments &args)
return VException("Height smaller than 0.");

DynamicGifStack *gif_stack = ObjectWrap::Unwrap<DynamicGifStack>(args.This());
return scope.Close(gif_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(gif_stack->Push((unsigned char*)buf_data, buf_len, x, y, w, h));
}

Handle<Value>
Expand Down Expand Up @@ -299,7 +303,7 @@ DynamicGifStack::EIO_GifEncode(eio_req *req)
return 0;
}

int
int
DynamicGifStack::EIO_GifEncodeAfter(eio_req *req)
{
HandleScope scope;
Expand All @@ -317,7 +321,7 @@ DynamicGifStack::EIO_GifEncodeAfter(eio_req *req)
}
else {
Buffer *buf = Buffer::New(enc_req->gif_len);
memcpy(buf->data(), enc_req->gif, enc_req->gif_len);
memcpy(BufferData(buf), enc_req->gif, enc_req->gif_len);
argv[0] = buf->handle_;
argv[1] = gif->Dimensions();
argv[2] = Undefined();
Expand Down
2 changes: 1 addition & 1 deletion src/dynamic_gif_stack.h
Expand Up @@ -48,7 +48,7 @@ class DynamicGifStack : public node::ObjectWrap {
DynamicGifStack(buffer_type bbuf_type);
~DynamicGifStack();

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> GifEncodeSync();

Expand Down

0 comments on commit 8be64dc

Please sign in to comment.