diff --git a/src/ImageData.cc b/src/ImageData.cc index a8e09b8..43639ef 100644 --- a/src/ImageData.cc +++ b/src/ImageData.cc @@ -46,7 +46,7 @@ ImageData::New(const Arguments &args) { Handle ImageData::GetWidth(Local prop, const AccessorInfo &info) { ImageData *imageData = ObjectWrap::Unwrap(info.This()); - return Number::New(imageData->pixelArray()->getWidth()); + return Number::New(imageData->pixelArray()->width()); } /* @@ -56,5 +56,5 @@ ImageData::GetWidth(Local prop, const AccessorInfo &info) { Handle ImageData::GetHeight(Local prop, const AccessorInfo &info) { ImageData *imageData = ObjectWrap::Unwrap(info.This()); - return Number::New(imageData->pixelArray()->getHeight()); + return Number::New(imageData->pixelArray()->height()); } \ No newline at end of file diff --git a/src/PixelArray.cc b/src/PixelArray.cc new file mode 100644 index 0000000..f9135a2 --- /dev/null +++ b/src/PixelArray.cc @@ -0,0 +1,94 @@ + +// +// PixelArray.cc +// +// Copyright (c) 2010 LearnBoost +// + +#include "PixelArray.h" +#include +#include + +/* + * Initialize PixelArray. + */ + +void +PixelArray::Initialize(Handle target) { + HandleScope scope; + Local t = FunctionTemplate::New(PixelArray::New); + t->InstanceTemplate()->SetInternalFieldCount(1); + t->SetClassName(String::NewSymbol("CanvasPixelArray")); + + Local proto = t->InstanceTemplate(); + proto->SetAccessor(String::NewSymbol("length"), GetLength); + target->Set(String::NewSymbol("CanvasPixelArray"), t->GetFunction()); +} + +/* + * Initialize a new PixelArray. + */ + +Handle +PixelArray::New(const Arguments &args) { + HandleScope scope; + PixelArray *arr; + // TODO: arg handling + + switch (args.Length()) { + // width, height + case 2: + arr = new PixelArray( + args[0]->Int32Value() + , args[1]->Int32Value()); + break; + // canvas, x, y, width, height + case 5: { + Canvas *canvas = ObjectWrap::Unwrap(args[0]->ToObject()); + arr = new PixelArray( + canvas + , args[1]->Int32Value() + , args[2]->Int32Value() + , args[3]->Int32Value() + , args[4]->Int32Value()); + } + break; + default: + return ThrowException(Exception::TypeError(String::New("invalid arguments"))); + } + + // Let v8 handle accessors (and clamping) + args.This()->SetIndexedPropertiesToPixelData( + arr->data() + , arr->length()); + + arr->Wrap(args.This()); + return args.This(); +} + +/* + * Get length. + */ + +Handle +PixelArray::GetLength(Local prop, const AccessorInfo &info) { + return Number::New(info.This()->GetIndexedPropertiesPixelDataLength()); +} + +/* + * Initialize a new PixelArray. + */ + +PixelArray::PixelArray(Canvas *canvas, int src_x, int src_y, int width, int height): + _width(width), _height(height) { +} + +PixelArray::PixelArray(int width, int height): + _width(width), _height(height) { + // TODO: error handling + // TODO: implement +} + +PixelArray::~PixelArray() { + +} \ No newline at end of file diff --git a/src/PixelArray.h b/src/PixelArray.h new file mode 100644 index 0000000..769abde --- /dev/null +++ b/src/PixelArray.h @@ -0,0 +1,32 @@ + +// +// PixelArray.h +// +// Copyright (c) 2010 LearnBoost +// + +#ifndef __NODE_PIXEL_ARRAY_H__ +#define __NODE_PIXEL_ARRAY_H__ + +#include "Canvas.h" + +class PixelArray: public node::ObjectWrap { + public: + static void Initialize(Handle target); + static Handle New(const Arguments &args); + static Handle GetLength(Local prop, const AccessorInfo &info); + inline int length(){ return _width * _height * 4; } + inline int width(){ return _width; } + inline int height(){ return _height; } + inline int stride(){ return _stride; } + inline uint8_t *data(){ return _data; } + PixelArray(Canvas *canvas, int x, int y, int width, int height); + PixelArray(int width, int height); + ~PixelArray(); + private: + int _stride; + uint8_t *_data; + int _width, _height; +}; + +#endif \ No newline at end of file