#include #include #include #include using namespace std; using namespace Magick; Napi::Value Watermark(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); try { Napi::Object obj = info[0].As(); Napi::Buffer data = obj.Get("data").As>(); string water = obj.Get("water").As().Utf8Value(); Magick::GravityType gravity = Magick::GravityType(obj.Get("gravity").As().Int64Value()); bool resize = obj.Has("resize") ? obj.Get("resize").As().Value() : false; float yscale = obj.Has("yscale") ? obj.Get("yscale").As().FloatValue() : false; bool append = obj.Has("append") ? obj.Get("append").As().Value() : false; bool mc = obj.Has("mc") ? obj.Get("mc").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; Blob blob; list frames; list coalesced; list mid; Image watermark; try { readImages(&frames, Blob(data.Data(), data.Length())); } catch (Magick::WarningCoder &warning) { cerr << "Coder Warning: " << warning.what() << endl; } catch (Magick::Warning &warning) { cerr << "Warning: " << warning.what() << endl; } watermark.read(water); if (resize && append) { string query(to_string(frames.front().baseColumns()) + "x"); watermark.scale(Geometry(query)); } else if (resize && yscale) { string query(to_string(frames.front().baseColumns()) + "x" + to_string(frames.front().baseRows() * yscale) + "!"); watermark.resize(Geometry(query)); } else if (resize) { string query("x" + to_string(frames.front().baseRows())); watermark.scale(Geometry(query)); } coalesceImages(&coalesced, frames.begin(), frames.end()); for (Image &image : coalesced) { Image final; if (append) { list to_append; to_append.push_back(image); to_append.push_back(watermark); appendImages(&final, to_append.begin(), to_append.end(), true); final.repage(); } else if (mc) { image.backgroundColor("white"); image.extent(Geometry(image.columns(), image.rows() + 15)); image.composite(watermark, gravity, Magick::OverCompositeOp); final = image; } else { image.composite(watermark, gravity, Magick::OverCompositeOp); final = image; } image.magick(type); final.animationDelay(delay == 0 ? image.animationDelay() : delay); mid.push_back(final); } optimizeTransparency(mid.begin(), mid.end()); if (type == "gif") { for (Image &image : mid) { image.quantizeDitherMethod(FloydSteinbergDitherMethod); image.quantize(); } } writeImages(mid.begin(), mid.end(), &blob); Napi::Object result = Napi::Object::New(env); result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); result.Set("type", type); return result; } catch (std::exception const &err) { throw Napi::Error::New(env, err.what()); } catch (...) { throw Napi::Error::New(env, "Unknown error"); } }