Skip to content
Browse files

Merge pull request #22 from awick/grayscale

Added grayscale support specified with 'gray'
  • Loading branch information...
2 parents 8a92ff2 + ebc9911 commit 9bd58f22513d93d24bf03e52406ab277e6f3cafd @pkrumins committed Apr 2, 2013
Showing with 27 additions and 11 deletions.
  1. +1 −1 readme.md
  2. +1 −1 src/common.h
  3. +7 −4 src/png.cpp
  4. +18 −5 src/png_encoder.cpp
View
2 readme.md
@@ -26,7 +26,7 @@ var png = new Png(buffer, width, height, buffer_type);
The first argument, `buffer`, is a node.js `Buffer` filled with RGB(A) values.
The second argument is integer width of the image.
The third argument is integer height of the image.
-The fourth argument is 'rgb', 'bgr', 'rgba or 'bgra'. Defaults to 'rgb'.
+The fourth argument is 'rgb', 'bgr', 'rgba', 'bgra', or 'gray'. Defaults to 'rgb'.
The constructed `png` object has the `encode` method that's asynchronous in nature.
You give it a callback and it will call your function with a node.js Buffer object
View
2 src/common.h
@@ -22,7 +22,7 @@ struct Rect {
bool str_eq(const char *s1, const char *s2);
-typedef enum { BUF_RGB, BUF_BGR, BUF_RGBA, BUF_BGRA } buffer_type;
+typedef enum { BUF_RGB, BUF_BGR, BUF_RGBA, BUF_BGRA, BUF_GRAY } buffer_type;
struct encode_request {
v8::Persistent<v8::Function> callback;
View
11 src/png.cpp
@@ -62,13 +62,14 @@ Png::New(const Arguments &args)
buffer_type buf_type = BUF_RGB;
if (args.Length() == 4) {
if (!args[3]->IsString())
- return VException("Fourth argument must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ return VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.");
String::AsciiValue bts(args[3]->ToString());
if (!(str_eq(*bts, "rgb") || str_eq(*bts, "bgr") ||
- str_eq(*bts, "rgba") || str_eq(*bts, "bgra")))
+ str_eq(*bts, "rgba") || str_eq(*bts, "bgra") ||
+ str_eq(*bts, "gray")))
{
- return VException("Fourth argument must be 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ return VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.");
}
if (str_eq(*bts, "rgb"))
@@ -79,8 +80,10 @@ Png::New(const Arguments &args)
buf_type = BUF_RGBA;
else if (str_eq(*bts, "bgra"))
buf_type = BUF_BGRA;
+ else if (str_eq(*bts, "gray"))
+ buf_type = BUF_GRAY;
else
- return VException("Fourth argument wasn't 'rgb', 'bgr', 'rgba' or 'bgra'.");
+ return VException("Fourth argument wasn't 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.");
}
View
23 src/png_encoder.cpp
@@ -46,10 +46,17 @@ PngEncoder::encode()
throw "png_create_info_struct failed.";
int color_type;
- if (buf_type == BUF_RGB || buf_type == BUF_BGR)
+ switch (buf_type) {
+ case BUF_RGB:
+ case BUF_BGR:
color_type = PNG_COLOR_TYPE_RGB;
- else
+ break;
+ case BUF_GRAY:
+ color_type = PNG_COLOR_TYPE_GRAY;
+ break;
+ default:
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ }
png_set_IHDR(png_ptr, info_ptr, width, height,
8, color_type, PNG_INTERLACE_NONE,
@@ -69,11 +76,17 @@ PngEncoder::encode()
if (!row_pointers)
throw "malloc failed in node-png (PngEncoder::encode).";
- if (buf_type == BUF_RGB || buf_type == BUF_BGR) {
+ switch (buf_type) {
+ case BUF_RGB:
+ case BUF_BGR:
for (int i=0; i<height; i++)
row_pointers[i] = data+3*i*width;
- }
- else {
+ break;
+ case BUF_GRAY:
+ for (int i=0; i<height; i++)
+ row_pointers[i] = data+i*width;
+ break;
+ default:
for (int i=0; i<height; i++)
row_pointers[i] = data+4*i*width;
}

0 comments on commit 9bd58f2

Please sign in to comment.
Something went wrong with that request. Please try again.