Handle<Value> Png::New(const Arguments &args) { HandleScope scope; if (args.Length() < 3) return VException("At least three arguments required - data buffer, width, height, [and input 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."); buffer_type buf_type = BUF_RGB; if (args.Length() == 4) { if (!args[3]->IsString()) 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, "gray"))) { return VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'."); } if (str_eq(*bts, "rgb")) buf_type = BUF_RGB; else if (str_eq(*bts, "bgr")) buf_type = BUF_BGR; else if (str_eq(*bts, "rgba")) 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 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'."); } int w = args[1]->Int32Value(); int h = args[2]->Int32Value(); if (w < 0) return VException("Width smaller than 0."); if (h < 0) return VException("Height smaller than 0."); Png *png = new Png(w, h, buf_type); png->Wrap(args.This()); // Save buffer. png->handle_->SetHiddenValue(String::New("buffer"), args[0]); return args.This(); }
void Png::New(const FunctionCallbackInfo<Value> &args) { Isolate *isolate = Isolate::GetCurrent(); HandleScope scope(isolate); if (args.Length() < 3) args.GetReturnValue().Set(VException("At least three arguments required - data buffer, width, height, [and input buffer type]")); if (!Buffer::HasInstance(args[0])) args.GetReturnValue().Set(VException("First argument must be Buffer.")); if (!args[1]->IsInt32()) args.GetReturnValue().Set(VException("Second argument must be integer width.")); if (!args[2]->IsInt32()) args.GetReturnValue().Set(VException("Third argument must be integer height.")); buffer_type buf_type = BUF_RGB; if (args.Length() >= 4) { if (!args[3]->IsString()) args.GetReturnValue().Set(VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.")); String::Utf8Value bts(args[3]->ToString()); if (!(str_eq(*bts, "rgb") || str_eq(*bts, "bgr") || str_eq(*bts, "rgba") || str_eq(*bts, "bgra") || str_eq(*bts, "gray"))) { args.GetReturnValue().Set(VException("Fourth argument must be 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.")); } if (str_eq(*bts, "rgb")) buf_type = BUF_RGB; else if (str_eq(*bts, "bgr")) buf_type = BUF_BGR; else if (str_eq(*bts, "rgba")) 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 args.GetReturnValue().Set(VException("Fourth argument wasn't 'gray', 'rgb', 'bgr', 'rgba' or 'bgra'.")); } int bits = 8; if (args.Length() >= 5) { if(buf_type != BUF_GRAY) args.GetReturnValue().Set(VException("Pixel bit width option only valid for \"gray\" buffer type")); if(!args[4]->IsInt32()) args.GetReturnValue().Set(VException("Fifth argument must be 8 or 16")); if(args[4]->Int32Value() == 8) bits = 8; else if (args[4]->Int32Value() == 16) bits = 16; else args.GetReturnValue().Set(VException("Fifth arguments wasn't 8 or 16")); } int w = args[1]->Int32Value(); int h = args[2]->Int32Value(); if (w < 0) args.GetReturnValue().Set(VException("Width smaller than 0.")); if (h < 0) args.GetReturnValue().Set(VException("Height smaller than 0.")); Png *png = new Png(w, h, buf_type, bits); png->Wrap(args.This()); // Save buffer. png->handle()->SetHiddenValue(String::NewFromUtf8(isolate, "buffer"), args[0]); args.GetReturnValue().Set(args.This()); }