Browse Source

Started PixelArray

v1.x
Tj Holowaychuk 14 years ago
parent
commit
866c9cd1ce
  1. 4
      src/ImageData.cc
  2. 94
      src/PixelArray.cc
  3. 32
      src/PixelArray.h

4
src/ImageData.cc

@ -46,7 +46,7 @@ ImageData::New(const Arguments &args) {
Handle<Value>
ImageData::GetWidth(Local<String> prop, const AccessorInfo &info) {
ImageData *imageData = ObjectWrap::Unwrap<ImageData>(info.This());
return Number::New(imageData->pixelArray()->getWidth());
return Number::New(imageData->pixelArray()->width());
}
/*
@ -56,5 +56,5 @@ ImageData::GetWidth(Local<String> prop, const AccessorInfo &info) {
Handle<Value>
ImageData::GetHeight(Local<String> prop, const AccessorInfo &info) {
ImageData *imageData = ObjectWrap::Unwrap<ImageData>(info.This());
return Number::New(imageData->pixelArray()->getHeight());
return Number::New(imageData->pixelArray()->height());
}

94
src/PixelArray.cc

@ -0,0 +1,94 @@
//
// PixelArray.cc
//
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
//
#include "PixelArray.h"
#include <stdlib.h>
#include <string.h>
/*
* Initialize PixelArray.
*/
void
PixelArray::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(PixelArray::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(String::NewSymbol("CanvasPixelArray"));
Local<ObjectTemplate> proto = t->InstanceTemplate();
proto->SetAccessor(String::NewSymbol("length"), GetLength);
target->Set(String::NewSymbol("CanvasPixelArray"), t->GetFunction());
}
/*
* Initialize a new PixelArray.
*/
Handle<Value>
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<Canvas>(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<Value>
PixelArray::GetLength(Local<String> 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() {
}

32
src/PixelArray.h

@ -0,0 +1,32 @@
//
// PixelArray.h
//
// Copyright (c) 2010 LearnBoost <tj@learnboost.com>
//
#ifndef __NODE_PIXEL_ARRAY_H__
#define __NODE_PIXEL_ARRAY_H__
#include "Canvas.h"
class PixelArray: public node::ObjectWrap {
public:
static void Initialize(Handle<Object> target);
static Handle<Value> New(const Arguments &args);
static Handle<Value> GetLength(Local<String> 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
Loading…
Cancel
Save