Permalink
Browse files

quality is no longer an argument to ctor. fix examples. 'rgb' is now …

…the default buffer input type.
  • Loading branch information...
1 parent 7248a56 commit 9407c4bdfca40eccbb1dce5c83ce49e43d4980b0 @pkrumins committed Aug 2, 2010
View
2 examples/dynamic-jpeg-stack.js
@@ -7,7 +7,7 @@ var Buffer = require('buffer').Buffer;
var terminal = fs.readFileSync('./rgba-terminal.dat');
-var jpegStack = new JpegLib.DynamicJpegStack(60, 'rgba');
+var jpegStack = new JpegLib.DynamicJpegStack('rgba');
jpegStack.setBackground(terminal, 720, 400);
function rectDim(fileName) {
View
2 examples/fixed-jpeg-stack.js
@@ -3,7 +3,7 @@ var fs = require('fs');
var sys = require('sys');
var Buffer = require('buffer').Buffer;
-var jpegStack = new JpegLib.FixedJpegStack(720, 400, 60, 'rgba');
+var jpegStack = new JpegLib.FixedJpegStack(720, 400, 'rgba');
function rectDim(fileName) {
var m = fileName.match(/^\d+-rgba-(\d+)-(\d+)-(\d+)-(\d+).dat$/);
View
2 examples/jpeg-example.js
@@ -7,7 +7,7 @@ var Buffer = require('buffer').Buffer;
var rgba = new Buffer(1152000);
rgba.write(fs.readFileSync('./rgba-terminal.dat', 'binary'), 'binary');
-var jpeg = new Jpeg(rgba, 720, 400, 70, 'rgba');
+var jpeg = new Jpeg(rgba, 720, 400, 'rgba');
var jpeg_img = jpeg.encode();
fs.writeFileSync('./jpeg.jpeg', jpeg_img, 'binary');
View
2 examples/jpeg-example2.js
@@ -15,7 +15,7 @@ for (var i=0; i<HEIGHT; i++) {
}
}
-var jpeg = new Jpeg(rgba, WIDTH, HEIGHT, 50, 'rgb');
+var jpeg = new Jpeg(rgba, WIDTH, HEIGHT, 'rgb');
var jpeg_img = jpeg.encode();
fs.writeFileSync('./jpeg-gradient.jpeg', jpeg_img, 'binary');
View
92 src/dynamic_jpeg_stack.cpp
@@ -22,12 +22,13 @@ DynamicJpegStack::Initialize(v8::Handle<v8::Object> target)
NODE_SET_PROTOTYPE_METHOD(t, "push", Push);
NODE_SET_PROTOTYPE_METHOD(t, "reset", Reset);
NODE_SET_PROTOTYPE_METHOD(t, "setBackground", SetBackground);
+ NODE_SET_PROTOTYPE_METHOD(t, "setQuality", SetQuality);
NODE_SET_PROTOTYPE_METHOD(t, "dimensions", Dimensions);
target->Set(String::NewSymbol("DynamicJpegStack"), t->GetFunction());
}
-DynamicJpegStack::DynamicJpegStack(int qquality, buffer_type bbuf_type) :
- quality(qquality), buf_type(bbuf_type),
+DynamicJpegStack::DynamicJpegStack(buffer_type bbuf_type) :
+ quality(60), buf_type(bbuf_type),
dyn_rect(-1, -1, 0, 0),
bg_width(0), bg_height(0), data(NULL) {}
@@ -56,7 +57,6 @@ DynamicJpegStack::UpdateOptimalDimension(int x, int y, int w, int h)
int hh = h - (dyn_rect.h - (y - dyn_rect.y));
if (hh > 0)
dyn_rect.h += hh;
-
}
Handle<Value>
@@ -152,6 +152,12 @@ DynamicJpegStack::SetBackground(unsigned char *data_buf, int w, int h)
}
void
+DynamicJpegStack::SetQuality(int q)
+{
+ quality = q;
+}
+
+void
DynamicJpegStack::Reset()
{
dyn_rect = Rect(-1, -1, 0, 0);
@@ -176,34 +182,34 @@ DynamicJpegStack::New(const Arguments &args)
{
HandleScope scope;
- if (args.Length() != 2)
- return VException("Two arguments required - quality and buffer type");
- if (!args[0]->IsInt32())
- return VException("First argument must be integer quality.");
- if (!args[1]->IsString())
- return VException("Second argument must be a string. Either 'rgb', 'bgr', 'rgba' or 'bgra'.");
-
- int quality = args[0]->Int32Value();
- String::AsciiValue bt(args[1]->ToString());
-
- if (quality < 0 || quality > 100)
- return VException("Quality must be between 0 and 100 (inclusive).");
- if (!(str_eq(*bt, "rgb") || str_eq(*bt, "bgr") || str_eq(*bt, "rgba") || str_eq(*bt, "bgra")))
- return VException("Buffer type must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
-
- buffer_type buf_type;
- if (str_eq(*bt, "rgb"))
- buf_type = BUF_RGB;
- else if (str_eq(*bt, "bgr"))
- buf_type = BUF_BGR;
- else if (str_eq(*bt, "rgba"))
- buf_type = BUF_RGBA;
- else if (str_eq(*bt, "bgra"))
- buf_type = BUF_BGRA;
- else
- return VException("Buffer type wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
-
- DynamicJpegStack *jpeg = new DynamicJpegStack(quality, buf_type);
+ if (args.Length() > 1)
+ return VException("One argument max - buffer type.");
+
+ buffer_type buf_type = BUF_RGB;
+ if (args.Length() == 1) {
+ if (!args[0]->IsString())
+ return VException("First argument must be a string. Either 'rgb', 'bgr', 'rgba' or 'bgra'.");
+
+ String::AsciiValue bt(args[0]->ToString());
+ if (!(str_eq(*bt, "rgb") || str_eq(*bt, "bgr") ||
+ str_eq(*bt, "rgba") || str_eq(*bt, "bgra")))
+ {
+ return VException("Buffer type must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ }
+
+ if (str_eq(*bt, "rgb"))
+ buf_type = BUF_RGB;
+ else if (str_eq(*bt, "bgr"))
+ buf_type = BUF_BGR;
+ else if (str_eq(*bt, "rgba"))
+ buf_type = BUF_RGBA;
+ else if (str_eq(*bt, "bgra"))
+ buf_type = BUF_BGRA;
+ else
+ return VException("Buffer type wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ }
+
+ DynamicJpegStack *jpeg = new DynamicJpegStack(buf_type);
jpeg->Wrap(args.This());
return args.This();
}
@@ -321,3 +327,27 @@ DynamicJpegStack::Dimensions(const Arguments &args)
return scope.Close(jpeg->Dimensions());
}
+Handle<Value>
+DynamicJpegStack::SetQuality(const Arguments &args)
+{
+ HandleScope scope;
+
+ if (args.Length() != 1)
+ return VException("One argument required - quality");
+
+ if (!args[0]->IsInt32())
+ return VException("First argument must be integer quality");
+
+ int q = args[0]->Int32Value();
+
+ if (q < 0)
+ return VException("Quality must be greater or equal to 0.");
+ if (q > 100)
+ return VException("Quality must be less than or equal to 100.");
+
+ DynamicJpegStack *jpeg = ObjectWrap::Unwrap<DynamicJpegStack>(args.This());
+ jpeg->SetQuality(q);
+
+ return Undefined();
+}
+
View
4 src/dynamic_jpeg_stack.h
@@ -22,12 +22,13 @@ class DynamicJpegStack : public node::ObjectWrap {
void UpdateOptimalDimension(int x, int y, int w, int h);
public:
- DynamicJpegStack(int qquality, buffer_type bbuf_type);
+ DynamicJpegStack(buffer_type bbuf_type);
~DynamicJpegStack();
v8::Handle<v8::Value> JpegEncode();
void Push(unsigned char *data_buf, int x, int y, int w, int h);
void SetBackground(unsigned char *data_buf, int w, int h);
+ void SetQuality(int q);
v8::Handle<v8::Value> Dimensions();
void Reset();
@@ -36,6 +37,7 @@ class DynamicJpegStack : public node::ObjectWrap {
static v8::Handle<v8::Value> JpegEncode(const v8::Arguments &args);
static v8::Handle<v8::Value> Push(const v8::Arguments &args);
static v8::Handle<v8::Value> SetBackground(const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetQuality(const v8::Arguments &args);
static v8::Handle<v8::Value> Dimensions(const v8::Arguments &args);
static v8::Handle<v8::Value> Reset(const v8::Arguments &args);
};
View
87 src/fixed_jpeg_stack.cpp
@@ -20,11 +20,12 @@ FixedJpegStack::Initialize(v8::Handle<v8::Object> target)
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "encode", JpegEncode);
NODE_SET_PROTOTYPE_METHOD(t, "push", Push);
+ NODE_SET_PROTOTYPE_METHOD(t, "setQuality", SetQuality);
target->Set(String::NewSymbol("FixedJpegStack"), t->GetFunction());
}
-FixedJpegStack::FixedJpegStack(int wwidth, int hheight, int qquality, buffer_type bbuf_type) :
- width(wwidth), height(hheight), quality(qquality), buf_type(bbuf_type)
+FixedJpegStack::FixedJpegStack(int wwidth, int hheight, buffer_type bbuf_type) :
+ width(wwidth), height(hheight), quality(60), buf_type(bbuf_type)
{
data = (unsigned char *)calloc(width*height*3, sizeof(*data));
if (!data) throw "calloc in FixedJpegStack::FixedJpegStack failed!";
@@ -90,50 +91,58 @@ FixedJpegStack::Push(unsigned char *data_buf, int x, int y, int w, int h)
}
}
+void
+FixedJpegStack::SetQuality(int q)
+{
+ quality = q;
+}
+
Handle<Value>
FixedJpegStack::New(const Arguments &args)
{
HandleScope scope;
- if (args.Length() != 4)
- return VException("Four arguments required - width, height, quality and buffer type");
+ if (args.Length() < 2)
+ return VException("At least two arguments required - width, height, [and buffer type]");
if (!args[0]->IsInt32())
return VException("First argument must be integer width.");
if (!args[1]->IsInt32())
return VException("Second argument must be integer height.");
- if (!args[2]->IsInt32())
- return VException("Third argument must be integer quality.");
- if (!args[3]->IsString())
- return VException("Fourth argument must be a string. Either 'rgb', 'bgr', 'rgba' or 'bgra'.");
int w = args[0]->Int32Value();
int h = args[1]->Int32Value();
- int quality = args[2]->Int32Value();
- String::AsciiValue bt(args[3]->ToString());
if (w < 0)
return VException("Width can't be negative.");
if (h < 0)
return VException("Height can't be negative.");
- if (quality < 0 || quality > 100)
- return VException("Quality must be between 0 and 100 (inclusive).");
- if (!(str_eq(*bt, "rgb") || str_eq(*bt, "bgr") || str_eq(*bt, "rgba") || str_eq(*bt, "bgra")))
- return VException("Buffer type must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
-
- buffer_type buf_type;
- if (str_eq(*bt, "rgb"))
- buf_type = BUF_RGB;
- else if (str_eq(*bt, "bgr"))
- buf_type = BUF_BGR;
- else if (str_eq(*bt, "rgba"))
- buf_type = BUF_RGBA;
- else if (str_eq(*bt, "bgra"))
- buf_type = BUF_BGRA;
- else
- return VException("Buffer type wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
+
+ buffer_type buf_type = BUF_RGB;
+ if (args.Length() == 3) {
+ if (!args[2]->IsString())
+ return VException("Third argument must be a string. Either 'rgb', 'bgr', 'rgba' or 'bgra'.");
+
+ String::AsciiValue bt(args[2]->ToString());
+ if (!(str_eq(*bt, "rgb") || str_eq(*bt, "bgr") ||
+ str_eq(*bt, "rgba") || str_eq(*bt, "bgra")))
+ {
+ return VException("Buffer type must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ }
+
+ if (str_eq(*bt, "rgb"))
+ buf_type = BUF_RGB;
+ else if (str_eq(*bt, "bgr"))
+ buf_type = BUF_BGR;
+ else if (str_eq(*bt, "rgba"))
+ buf_type = BUF_RGBA;
+ else if (str_eq(*bt, "bgra"))
+ buf_type = BUF_BGRA;
+ else
+ return VException("Buffer type wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ }
try {
- FixedJpegStack *jpeg = new FixedJpegStack(w, h, quality, buf_type);
+ FixedJpegStack *jpeg = new FixedJpegStack(w, h, buf_type);
jpeg->Wrap(args.This());
return args.This();
}
@@ -195,3 +204,27 @@ FixedJpegStack::Push(const Arguments &args)
return Undefined();
}
+Handle<Value>
+FixedJpegStack::SetQuality(const Arguments &args)
+{
+ HandleScope scope;
+
+ if (args.Length() != 1)
+ return VException("One argument required - quality");
+
+ if (!args[0]->IsInt32())
+ return VException("First argument must be integer quality");
+
+ int q = args[0]->Int32Value();
+
+ if (q < 0)
+ return VException("Quality must be greater or equal to 0.");
+ if (q > 100)
+ return VException("Quality must be less than or equal to 100.");
+
+ FixedJpegStack *jpeg = ObjectWrap::Unwrap<FixedJpegStack>(args.This());
+ jpeg->SetQuality(q);
+
+ return Undefined();
+}
+
View
4 src/fixed_jpeg_stack.h
@@ -15,13 +15,15 @@ class FixedJpegStack : public node::ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
- FixedJpegStack(int wwidth, int hheight, int qquality, buffer_type bbuf_type);
+ FixedJpegStack(int wwidth, int hheight, buffer_type bbuf_type);
v8::Handle<v8::Value> JpegEncode();
void Push(unsigned char *data_buf, int x, int y, int w, int h);
+ void SetQuality(int q);
static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> JpegEncode(const v8::Arguments &args);
static v8::Handle<v8::Value> Push(const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetQuality(const v8::Arguments &args);
};
#endif
View
90 src/jpeg.cpp
@@ -19,11 +19,12 @@ Jpeg::Initialize(v8::Handle<v8::Object> target)
Local<FunctionTemplate> t = FunctionTemplate::New(New);
t->InstanceTemplate()->SetInternalFieldCount(1);
NODE_SET_PROTOTYPE_METHOD(t, "encode", JpegEncode);
+ NODE_SET_PROTOTYPE_METHOD(t, "setQuality", SetQuality);
target->Set(String::NewSymbol("Jpeg"), t->GetFunction());
}
-Jpeg::Jpeg(Buffer *ddata, int wwidth, int hheight, int qquality, buffer_type bbuf_type) :
- jpeg_encoder((unsigned char *)ddata->data(), wwidth, hheight, qquality, bbuf_type) {}
+Jpeg::Jpeg(Buffer *ddata, int wwidth, int hheight, buffer_type bbuf_type) :
+ jpeg_encoder((unsigned char *)ddata->data(), wwidth, hheight, 60, bbuf_type) {}
Handle<Value>
Jpeg::JpegEncode()
@@ -42,52 +43,60 @@ Jpeg::JpegEncode()
);
}
+void
+Jpeg::SetQuality(int q)
+{
+ jpeg_encoder.set_quality(q);
+}
+
Handle<Value>
Jpeg::New(const Arguments &args)
{
HandleScope scope;
- if (args.Length() != 5)
- return VException("Five arguments required - rgba/rgb buffer, width, height, quality, buffer type");
+ if (args.Length() < 3)
+ return VException("At least three arguments required - buffer, width, height, [and buffer type]");
if (!Buffer::HasInstance(args[0]))
return VException("First argument must be Buffer.");
if (!args[1]->IsInt32())
return VException("Second argument must be integer width.");
if (!args[2]->IsInt32())
return VException("Third argument must be integer height.");
- if (!args[3]->IsInt32())
- return VException("Fourth argument must be integer quality.");
- if (!args[4]->IsString())
- return VException("Fifth argument must be a string. Either 'rgb', 'bgr', 'rgba' or 'bgra'.");
int w = args[1]->Int32Value();
int h = args[2]->Int32Value();
- int quality = args[3]->Int32Value();
- String::AsciiValue bt(args[4]->ToString());
if (w < 0)
return VException("Width can't be negative.");
if (h < 0)
return VException("Height can't be negative.");
- if (quality < 0 || quality > 100)
- return VException("Quality must be between 0 and 100 (inclusive).");
- if (!(str_eq(*bt, "rgb") || str_eq(*bt, "bgr") || str_eq(*bt, "rgba") || str_eq(*bt, "bgra")))
- return VException("Buffer type must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
-
- buffer_type buf_type;
- if (str_eq(*bt, "rgb"))
- buf_type = BUF_RGB;
- else if (str_eq(*bt, "bgr"))
- buf_type = BUF_BGR;
- else if (str_eq(*bt, "rgba"))
- buf_type = BUF_RGBA;
- else if (str_eq(*bt, "bgra"))
- buf_type = BUF_BGRA;
- else
- return VException("Buffer type wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
+
+ buffer_type buf_type = BUF_RGB;
+ if (args.Length() == 4) {
+ if (!args[3]->IsString())
+ return VException("Fifth argument must be a string. Either 'rgb', 'bgr', 'rgba' or 'bgra'.");
+
+ String::AsciiValue bt(args[3]->ToString());
+ if (!(str_eq(*bt, "rgb") || str_eq(*bt, "bgr") ||
+ str_eq(*bt, "rgba") || str_eq(*bt, "bgra")))
+ {
+ return VException("Buffer type must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ }
+
+ if (str_eq(*bt, "rgb"))
+ buf_type = BUF_RGB;
+ else if (str_eq(*bt, "bgr"))
+ buf_type = BUF_BGR;
+ else if (str_eq(*bt, "rgba"))
+ buf_type = BUF_RGBA;
+ else if (str_eq(*bt, "bgra"))
+ buf_type = BUF_BGRA;
+ else
+ return VException("Buffer type wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ }
Buffer *data_buf = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
- Jpeg *jpeg = new Jpeg(data_buf, w, h, quality, buf_type);
+ Jpeg *jpeg = new Jpeg(data_buf, w, h, buf_type);
jpeg->Wrap(args.This());
return args.This();
}
@@ -96,7 +105,32 @@ Handle<Value>
Jpeg::JpegEncode(const Arguments &args)
{
HandleScope scope;
+
Jpeg *jpeg = ObjectWrap::Unwrap<Jpeg>(args.This());
- return jpeg->JpegEncode();
+ return scope.Close(jpeg->JpegEncode());
+}
+
+Handle<Value>
+Jpeg::SetQuality(const Arguments &args)
+{
+ HandleScope scope;
+
+ if (args.Length() != 1)
+ return VException("One argument required - quality");
+
+ if (!args[0]->IsInt32())
+ return VException("First argument must be integer quality");
+
+ int q = args[0]->Int32Value();
+
+ if (q < 0)
+ return VException("Quality must be greater or equal to 0.");
+ if (q > 100)
+ return VException("Quality must be less than or equal to 100.");
+
+ Jpeg *jpeg = ObjectWrap::Unwrap<Jpeg>(args.This());
+ jpeg->SetQuality(q);
+
+ return Undefined();
}
View
4 src/jpeg.h
@@ -11,11 +11,13 @@ class Jpeg : public node::ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
- Jpeg(node::Buffer *ddata, int wwidth, int hheight, int qquality, buffer_type bbuf_type);
+ Jpeg(node::Buffer *ddata, int wwidth, int hheight, buffer_type bbuf_type);
v8::Handle<v8::Value> JpegEncode();
+ void SetQuality(int q);
static v8::Handle<v8::Value> New(const v8::Arguments &args);
static v8::Handle<v8::Value> JpegEncode(const v8::Arguments &args);
+ static v8::Handle<v8::Value> SetQuality(const v8::Arguments &args);
};
#endif
View
6 src/jpeg_encoder.cpp
@@ -174,6 +174,12 @@ JpegEncoder::encode()
free(rgb_data);
}
+void
+JpegEncoder::set_quality(int q)
+{
+ quality = q;
+}
+
const unsigned char *
JpegEncoder::get_jpeg() const
{
View
1 src/jpeg_encoder.h
@@ -22,6 +22,7 @@ class JpegEncoder {
~JpegEncoder();
void encode();
+ void set_quality(int qquality);
const unsigned char *get_jpeg() const;
unsigned int get_jpeg_len() const;

0 comments on commit 9407c4b

Please sign in to comment.