2021-04-24 22:54:47 +00:00
|
|
|
#include <Magick++.h>
|
2020-07-23 20:49:34 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2020-07-23 20:49:34 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Magick;
|
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Wdt(const Napi::CallbackInfo &info) {
|
|
|
|
Napi::Env env = info.Env();
|
2021-02-25 21:09:53 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
|
|
|
Napi::Object obj = info[0].As<Napi::Object>();
|
|
|
|
string path = obj.Get("path").As<Napi::String>().Utf8Value();
|
|
|
|
string type = obj.Get("type").As<Napi::String>().Utf8Value();
|
|
|
|
int delay =
|
|
|
|
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
|
|
|
|
|
|
|
Blob blob;
|
|
|
|
|
|
|
|
list<Image> frames;
|
|
|
|
list<Image> coalesced;
|
|
|
|
list<Image> mid;
|
|
|
|
Image watermark;
|
|
|
|
readImages(&frames, path);
|
|
|
|
watermark.read("./assets/images/whodidthis.png");
|
|
|
|
coalesceImages(&coalesced, frames.begin(), frames.end());
|
|
|
|
|
|
|
|
for (Image &image : coalesced) {
|
|
|
|
Image watermark_new = watermark;
|
|
|
|
image.scale(Geometry("374x374>"));
|
|
|
|
watermark_new.composite(image, Magick::CenterGravity,
|
|
|
|
Magick::OverCompositeOp);
|
|
|
|
watermark_new.magick(type);
|
|
|
|
watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay);
|
|
|
|
mid.push_back(watermark_new);
|
|
|
|
}
|
2020-07-23 20:49:34 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
optimizeTransparency(mid.begin(), mid.end());
|
2020-07-23 20:49:34 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
if (type == "gif") {
|
|
|
|
for (Image &image : mid) {
|
|
|
|
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
|
|
|
|
image.quantize();
|
|
|
|
}
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2020-07-23 20:49:34 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
writeImages(mid.begin(), mid.end(), &blob);
|
2020-07-23 20:49:34 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
|
|
|
blob.length()));
|
|
|
|
result.Set("type", type);
|
|
|
|
return result;
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (Napi::Error const &err) {
|
|
|
|
throw err;
|
2021-04-26 14:47:03 +00:00
|
|
|
} catch (std::exception const &err) {
|
|
|
|
throw Napi::Error::New(env, err.what());
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
|
|
|
throw Napi::Error::New(env, "Unknown error");
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2020-07-23 20:49:34 +00:00
|
|
|
}
|