Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mlmorg committed Oct 22, 2012
0 parents commit 1dd829f
Show file tree
Hide file tree
Showing 11 changed files with 608 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
build/
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2012 Brewster Inc., Matt Morgan, John Crepezzi

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
4 changes: 4 additions & 0 deletions Makefile
@@ -0,0 +1,4 @@
all: build

build:
node-gyp rebuild
40 changes: 40 additions & 0 deletions README.md
@@ -0,0 +1,40 @@
# Magick

ImageMagick bindings for Node.js.

## Installation

``` bash
$ npm install magick
```

## Usage

``` javascript
var fs = require('fs');
var magick = require('magick');

// Load the file data into Magick
var data = fs.readFileSync('./image.jpg');
var file = new magick.File(data);

// Perform operations
file.setFormat('PNG');
file.opacity(0.5);
file.resize(200, 250);
file.blur(5);
file.paint(5);

// Get file and release memory
var buffer = file.getBuffer();
file.release();

// Write to new file
fs.writeFileSync('./image-altered.png', buffer);
```

## License

Magick is distributed under the MIT License. See
[LICENSE](https://github.com/brewster/magick/blob/master/LICENSE) for more
details.
51 changes: 51 additions & 0 deletions binding.gyp
@@ -0,0 +1,51 @@
{
'targets': [
{
'conditions': [

[
'OS=="win"',
{
# no windows support
},
{
'libraries': [
'<!@(pkg-config --static --libs MagickWand)',
]
}
],

[
'OS=="mac"',
{
'xcode_settings': {
'OTHER_CFLAGS': [
'<!@(pkg-config --static --cflags MagickWand)',
'-Wall'
],
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
'GCC_ENABLE_CPP_RTTI': 'YES'
}
},
{
'cflags': [
'<!@(pkg-config --static --cflags MagickWand)',
'-Wall'
],
'cflags_cc!': [
'-fno-rtti',
'-fno-exceptions'
]
}
]

],
'target_name': 'magick',
'sources': [
'src/image_wrap.cc',
'src/file.cc',
'src/magick.cc'
]
}
]
}
23 changes: 23 additions & 0 deletions package.json
@@ -0,0 +1,23 @@
{
"name": "magick",
"version": "0.0.1",
"description": "ImageMagick bindings for Node.js",
"homepage": "https://github.com/brewster/magick",

"repository": {
"type": "git",
"url": "https://github.com/brewster/magick"
},

"main": "./build/Release/magick",

"dependencies": {
"node-gyp": "~ 0.4.5"
},

"scripts": {
"install": "node-gyp rebuild"
},

"license": "MIT"
}
231 changes: 231 additions & 0 deletions src/file.cc
@@ -0,0 +1,231 @@
#define BUILDING_NODE_EXTENSION
#include "file.h"

void File::Init(v8::Handle<v8::Object> target) {
// Get ready
ImageWrap::Init();

// Get a template for the C++ object we're wrapping
v8::Local<v8::FunctionTemplate> tplate = v8::FunctionTemplate::New(New);
tplate->SetClassName(v8::String::NewSymbol("File"));
tplate->InstanceTemplate()->SetInternalFieldCount(1);

// Set the getString method on the prototype
v8::Local<v8::ObjectTemplate> proto = tplate->PrototypeTemplate();
proto->Set(v8::String::NewSymbol("getBuffer"),
v8::FunctionTemplate::New(GetBuffer)->GetFunction()
);
proto->Set(v8::String::NewSymbol("release"),
v8::FunctionTemplate::New(Release)->GetFunction()
);
proto->Set(v8::String::NewSymbol("dimensions"),
v8::FunctionTemplate::New(Dimensions)->GetFunction()
);
proto->Set(v8::String::NewSymbol("setFormat"),
v8::FunctionTemplate::New(SetFormat)->GetFunction()
);
proto->Set(v8::String::NewSymbol("resize"),
v8::FunctionTemplate::New(Resize)->GetFunction()
);
proto->Set(v8::String::NewSymbol("crop"),
v8::FunctionTemplate::New(Crop)->GetFunction()
);
proto->Set(v8::String::NewSymbol("resizeToFill"),
v8::FunctionTemplate::New(ResizeToFill)->GetFunction()
);
proto->Set(v8::String::NewSymbol("opacity"),
v8::FunctionTemplate::New(Opacity)->GetFunction()
);
proto->Set(v8::String::NewSymbol("blur"),
v8::FunctionTemplate::New(Blur)->GetFunction()
);
proto->Set(v8::String::NewSymbol("charcoal"),
v8::FunctionTemplate::New(Charcoal)->GetFunction()
);
proto->Set(v8::String::NewSymbol("paint"),
v8::FunctionTemplate::New(Paint)->GetFunction()
);
proto->Set(v8::String::NewSymbol("sepia"),
v8::FunctionTemplate::New(Sepia)->GetFunction()
);

// Set the constructor on the module
target->Set(
v8::String::NewSymbol("File"),
v8::Persistent<v8::Function>::New(tplate->GetFunction())
);
}

v8::Handle<v8::Value> File::GetBuffer(const v8::Arguments &args) {
char *result;
size_t length;

File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());
thisOne->image->GetBlob(result, length);

// We want to make sure we get a fast-buffer back
// see: http://sambro.is-super-awesome.com/category/node-js/
node::Buffer *slowBuffer = node::Buffer::New(result, length);
v8::Local<v8::Object> globalObj = v8::Context::GetCurrent()->Global();
v8::Local<v8::Function> bufferConstructor =
v8::Local<v8::Function>::Cast(globalObj->Get(v8::String::New("Buffer")));
v8::Handle<v8::Value> constructorArgs[3] =
{ slowBuffer->handle_, v8::Integer::New(length), v8::Integer::New(0) };
v8::Local<v8::Object> actualBuffer =
bufferConstructor->NewInstance(3, constructorArgs);

MagickRelinquishMemory(result); // can also be delete[] result

v8::HandleScope scope;
return scope.Close(actualBuffer);
}

v8::Handle<v8::Value> File::Release(const v8::Arguments &args) {
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());
thisOne->ReleaseImage();
v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Dimensions(const v8::Arguments &args) {
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

char *orig;
thisOne->image->GetDimensionString(orig);
v8::Local<v8::String> dim = v8::String::New(orig);
delete[] orig;

v8::HandleScope scope;
return scope.Close(dim);
}

v8::Handle<v8::Value> File::SetFormat(const v8::Arguments &args) {
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

v8::String::AsciiValue format(args[0]);
thisOne->image->SetFormat(*format);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Resize(const v8::Arguments &args) {
int desiredWidth = args[0]->NumberValue();
int desiredHeight = args[1]->NumberValue();
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Resize(desiredWidth, desiredHeight);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Crop(const v8::Arguments &args) {
int desiredWidth = args[0]->NumberValue();
int desiredHeight = args[1]->NumberValue();
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Crop(desiredWidth, desiredHeight);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::ResizeToFill(const v8::Arguments &args) {
int desiredWidth = args[0]->NumberValue();
int desiredHeight = args[1]->NumberValue();
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->ResizeToFill(desiredWidth, desiredHeight);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Opacity(const v8::Arguments &args) {
float desiredOpacity = args[1]->NumberValue();
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Opacity(desiredOpacity);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Blur(const v8::Arguments &args) {
int sigma = args[0]->NumberValue();
int radius = 0;
if (args[1]->IsNumber()) {
radius = args[1]->NumberValue();
}
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Blur(sigma, radius);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Charcoal(const v8::Arguments &args) {
int sigma = args[0]->NumberValue();
int radius = 0;
if (args[1]->IsNumber()) {
radius = args[1]->NumberValue();
}
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Charcoal(sigma, radius);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Paint(const v8::Arguments &args) {
int radius = 0;
if (args[0]->IsNumber()) {
radius = args[0]->NumberValue();
}
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Paint(radius);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::Sepia(const v8::Arguments &args) {
int threshold = args[0]->NumberValue();
File *thisOne = node::ObjectWrap::Unwrap<File>(args.This());

thisOne->image->Sepia(threshold);

v8::HandleScope scope;
return scope.Close(v8::Undefined());
}

v8::Handle<v8::Value> File::New(const v8::Arguments &args) {
File* thisOne = new File();
thisOne->Wrap(args.This());

v8::Local<v8::Object> buffer = args[0]->ToObject();
char *data = node::Buffer::Data(buffer);
size_t length = node::Buffer::Length(buffer);
thisOne->image = new ImageWrap((void *) data, length);

return args.This();
}

ImageWrap* File::GetImageWrap(void) {
return this->image;
}

void File::ReleaseImage() {
if (this->image) {
delete this->image;
this->image = 0;
}
}

File::~File() {
this->ReleaseImage();
}

0 comments on commit 1dd829f

Please sign in to comment.