diff --git a/messages.json b/messages.json index 3aa0bbc..78c58cb 100644 --- a/messages.json +++ b/messages.json @@ -2,7 +2,9 @@ "emotes": [ "", "", - "" + "", + "", + "" ], "messages": [ "with your sanity", diff --git a/natives/blur.cc b/natives/blur.cc index 5158a51..a61ed8b 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -1,66 +1,51 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class BlurWorker : public Napi::AsyncWorker { - public: - BlurWorker(Napi::Function& callback, string in_path, bool sharp, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), sharp(sharp), type(type), delay(delay) {} - ~BlurWorker() {} - - void Execute() { - list frames; - list coalesced; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - if (sharp) { - for_each(coalesced.begin(), coalesced.end(), sharpenImage(10, 3)); - } else { - for_each(coalesced.begin(), coalesced.end(), blurImage(15)); - } - - for_each(coalesced.begin(), coalesced.end(), magickImage(type)); - - optimizeTransparency(coalesced.begin(), coalesced.end()); - - if (type == "gif") { - for (Image &image : coalesced) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(coalesced.begin(), coalesced.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; - bool sharp; -}; - -Napi::Value Blur(const Napi::CallbackInfo &info) -{ +Napi::Value Blur(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); bool sharp = obj.Get("sharp").As().Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - BlurWorker* blurWorker = new BlurWorker(cb, path, sharp, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + if (sharp) { + for_each(coalesced.begin(), coalesced.end(), sharpenImage(10, 3)); + } else { + for_each(coalesced.begin(), coalesced.end(), blurImage(15)); + } + + for_each(coalesced.begin(), coalesced.end(), magickImage(type)); + + optimizeTransparency(coalesced.begin(), coalesced.end()); + + if (type == "gif") { + for (Image &image : coalesced) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + writeImages(coalesced.begin(), coalesced.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; } \ No newline at end of file diff --git a/natives/blurple.cc b/natives/blurple.cc index c737a4a..4d666ec 100644 --- a/natives/blurple.cc +++ b/natives/blurple.cc @@ -1,65 +1,50 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class BlurpleWorker : public Napi::AsyncWorker { - public: - BlurpleWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~BlurpleWorker() {} - - void Execute() { - list frames; - list coalesced; - list blurpled; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.threshold(49151.25); - image.levelColors("#7289DA", "white"); - image.magick(type); - image.animationDelay(delay == 0 ? image.animationDelay() : delay); - blurpled.push_back(image); - } - - optimizeTransparency(blurpled.begin(), blurpled.end()); - - if (type == "gif") { - for (Image &image : blurpled) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - - writeImages(blurpled.begin(), blurpled.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Blurple(const Napi::CallbackInfo &info) -{ +Napi::Value Blurple(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - BlurpleWorker* blurpleWorker = new BlurpleWorker(cb, path, type, delay); - blurpleWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list blurpled; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.threshold(49151.25); + image.levelColors("#7289DA", "white"); + image.magick(type); + image.animationDelay(delay == 0 ? image.animationDelay() : delay); + blurpled.push_back(image); + } + + optimizeTransparency(blurpled.begin(), blurpled.end()); + + if (type == "gif") { + for (Image &image : blurpled) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(blurpled.begin(), blurpled.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; } \ No newline at end of file diff --git a/natives/caption.cc b/natives/caption.cc index 60654ee..c234e37 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -1,85 +1,70 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class CaptionWorker : public Napi::AsyncWorker { - public: - CaptionWorker(Napi::Function& callback, string caption, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), caption(caption), in_path(in_path), type(type), delay(delay) {} - ~CaptionWorker() {} - - void Execute() { - list frames; - list coalesced; - list captioned; - list result; - readImages(&frames, in_path); - - size_t width = frames.front().baseColumns(); - string query(to_string(width - ((width / 25) * 2)) + "x"); - Image caption_image(Geometry(query), Color("white")); - caption_image.fillColor("black"); - caption_image.alpha(true); - caption_image.font("Futura"); - caption_image.fontPointsize(width / 13); - caption_image.textGravity(Magick::CenterGravity); - caption_image.read("pango:" + caption); - caption_image.extent(Geometry(width, caption_image.rows() + (width / 13)), Magick::CenterGravity); - - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - Image appended; - list images; - image.backgroundColor("white"); - images.push_back(caption_image); - images.push_back(image); - appendImages(&appended, images.begin(), images.end(), true); - appended.repage(); - appended.magick(type); - appended.animationDelay(delay == 0 ? image.animationDelay() : delay); - appended.gifDisposeMethod(Magick::BackgroundDispose); - captioned.push_back(appended); - } - - optimizeTransparency(captioned.begin(), captioned.end()); - - if (type == "gif") { - for (Image &image : captioned) { - image.quantizeDither(false); - image.quantize(); - } - } - - - writeImages(captioned.begin(), captioned.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string caption, in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Caption(const Napi::CallbackInfo &info) -{ +Napi::Value Caption(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string caption = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - CaptionWorker* captionWorker = new CaptionWorker(cb, caption, path, type, delay); - captionWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list captioned; + readImages(&frames, path); + + size_t width = frames.front().baseColumns(); + string query(to_string(width - ((width / 25) * 2)) + "x"); + Image caption_image(Geometry(query), Color("white")); + caption_image.fillColor("black"); + caption_image.alpha(true); + caption_image.font("Futura"); + caption_image.fontPointsize(width / 13); + caption_image.textGravity(Magick::CenterGravity); + caption_image.read("pango:" + caption); + caption_image.extent(Geometry(width, caption_image.rows() + (width / 13)), + Magick::CenterGravity); + + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + Image appended; + list images; + image.backgroundColor("white"); + images.push_back(caption_image); + images.push_back(image); + appendImages(&appended, images.begin(), images.end(), true); + appended.repage(); + appended.magick(type); + appended.animationDelay(delay == 0 ? image.animationDelay() : delay); + appended.gifDisposeMethod(Magick::BackgroundDispose); + captioned.push_back(appended); + } + + optimizeTransparency(captioned.begin(), captioned.end()); + + if (type == "gif") { + for (Image &image : captioned) { + image.quantizeDither(false); + image.quantize(); + } + } + + writeImages(captioned.begin(), captioned.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; } \ No newline at end of file diff --git a/natives/caption2.cc b/natives/caption2.cc index c089cc4..27acf61 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -1,81 +1,68 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class CaptionTwoWorker : public Napi::AsyncWorker { - public: - CaptionTwoWorker(Napi::Function& callback, string caption, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), caption(caption), in_path(in_path), type(type), delay(delay) {} - ~CaptionTwoWorker() {} - - void Execute() { - list frames; - list coalesced; - list captioned; - Blob caption_blob; - readImages(&frames, in_path); - - size_t width = frames.front().baseColumns(); - string query(to_string(width - ((width / 25) * 2)) + "x"); - Image caption_image(Geometry(query), Color("white")); - caption_image.fillColor("black"); - caption_image.font("Helvetica Neue"); - caption_image.fontPointsize(width / 17); - caption_image.read("pango:" + caption); - caption_image.extent(Geometry(width, caption_image.rows() + (width / 25)), Magick::CenterGravity); - - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - Image appended; - list images; - image.backgroundColor("white"); - images.push_back(image); - images.push_back(caption_image); - appendImages(&appended, images.begin(), images.end(), true); - appended.repage(); - appended.magick(type); - appended.animationDelay(delay == 0 ? image.animationDelay() : delay); - captioned.push_back(appended); - } - - optimizeTransparency(captioned.begin(), captioned.end()); - - if (type == "gif") { - for (Image &image : captioned) { - image.quantizeDither(false); - image.quantize(); - } - } - - writeImages(captioned.begin(), captioned.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string caption, in_path, type; - int delay; - Blob blob; -}; - -Napi::Value CaptionTwo(const Napi::CallbackInfo &info) -{ +Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string caption = obj.Get("caption").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - CaptionTwoWorker* captionTwoWorker = new CaptionTwoWorker(cb, caption, path, type, delay); - captionTwoWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list captioned; + Blob caption_blob; + readImages(&frames, path); + + size_t width = frames.front().baseColumns(); + string query(to_string(width - ((width / 25) * 2)) + "x"); + Image caption_image(Geometry(query), Color("white")); + caption_image.fillColor("black"); + caption_image.font("Helvetica Neue"); + caption_image.fontPointsize(width / 17); + caption_image.read("pango:" + caption); + caption_image.extent(Geometry(width, caption_image.rows() + (width / 25)), + Magick::CenterGravity); + + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + Image appended; + list images; + image.backgroundColor("white"); + images.push_back(image); + images.push_back(caption_image); + appendImages(&appended, images.begin(), images.end(), true); + appended.repage(); + appended.magick(type); + appended.animationDelay(delay == 0 ? image.animationDelay() : delay); + captioned.push_back(appended); + } + + optimizeTransparency(captioned.begin(), captioned.end()); + + if (type == "gif") { + for (Image &image : captioned) { + image.quantizeDither(false); + image.quantize(); + } + } + + writeImages(captioned.begin(), captioned.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; } \ No newline at end of file diff --git a/natives/circle.cc b/natives/circle.cc index 7e1cf3d..def9f5d 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -1,63 +1,49 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class CircleWorker : public Napi::AsyncWorker { - public: - CircleWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~CircleWorker() {} - - void Execute() { - list frames; - list coalesced; - list blurred; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.rotationalBlur(10); - image.magick(type); - blurred.push_back(image); - } - - optimizeTransparency(blurred.begin(), blurred.end()); - - if (type == "gif") { - for (Image &image : blurred) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(blurred.begin(), blurred.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Circle(const Napi::CallbackInfo &info) -{ +Napi::Value Circle(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - CircleWorker* circleWorker = new CircleWorker(cb, path, type, delay); - circleWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list blurred; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.rotationalBlur(10); + image.magick(type); + blurred.push_back(image); + } + + optimizeTransparency(blurred.begin(), blurred.end()); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + writeImages(blurred.begin(), blurred.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; } \ No newline at end of file diff --git a/natives/crop.cc b/natives/crop.cc index 2df3539..f348793 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -1,64 +1,57 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class CropWorker : public Napi::AsyncWorker { - public: - CropWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~CropWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.extent(Geometry(to_string(image.columns() / image.rows() >= 1 ? image.rows() : image.columns()) + "x"), Magick::CenterGravity); - image.extent(Geometry("x" + to_string(image.columns() / image.rows() <= 1 ? image.columns() : image.rows())), Magick::CenterGravity); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Crop(const Napi::CallbackInfo &info) -{ +Napi::Value Crop(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - CropWorker* blurWorker = new CropWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.extent(Geometry(to_string(image.columns() / image.rows() >= 1 + ? image.rows() + : image.columns()) + + "x"), + Magick::CenterGravity); + image.extent(Geometry("x" + to_string(image.columns() / image.rows() <= 1 + ? image.columns() + : image.rows())), + Magick::CenterGravity); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/explode.cc b/natives/explode.cc index cf2f6cd..7d5a83c 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -1,64 +1,50 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class ExplodeWorker : public Napi::AsyncWorker { - public: - ExplodeWorker(Napi::Function& callback, string in_path, int amount, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), amount(amount), type(type), delay(delay) {} - ~ExplodeWorker() {} - - void Execute() { - list frames; - list coalesced; - list blurred; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.implode(amount); - image.magick(type); - blurred.push_back(image); - } - - optimizeTransparency(blurred.begin(), blurred.end()); - - if (type == "gif") { - for (Image &image : blurred) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(blurred.begin(), blurred.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay, amount; - Blob blob; -}; - -Napi::Value Explode(const Napi::CallbackInfo &info) -{ +Napi::Value Explode(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); int amount = obj.Get("amount").As().Int32Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - ExplodeWorker* explodeWorker = new ExplodeWorker(cb, path, amount, type, delay); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list blurred; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.implode(amount); + image.magick(type); + blurred.push_back(image); + } + + optimizeTransparency(blurred.begin(), blurred.end()); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + writeImages(blurred.begin(), blurred.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; } \ No newline at end of file diff --git a/natives/flag.cc b/natives/flag.cc index 59fc225..4c47898 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -1,70 +1,58 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class FlagWorker : public Napi::AsyncWorker { - public: - FlagWorker(Napi::Function& callback, string in_path, string overlay_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), overlay_path(overlay_path), type(type), delay(delay) {} - ~FlagWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_path); - watermark.read(overlay_path); - watermark.alphaChannel(Magick::SetAlphaChannel); - watermark.evaluate(Magick::AlphaChannel, Magick::MultiplyEvaluateOperator, 0.5); - string query(to_string(frames.front().baseColumns()) + "x" + to_string(frames.front().baseRows()) + "!"); - watermark.scale(Geometry(query)); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.composite(watermark, Magick::NorthGravity, Magick::OverCompositeOp); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, overlay_path, type; - int delay; - Blob blob; -}; - -Napi::Value Flag(const Napi::CallbackInfo &info) -{ +Napi::Value Flag(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string overlay = obj.Get("overlay").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - FlagWorker* flagWorker = new FlagWorker(cb, path, overlay, type, delay); - flagWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; + readImages(&frames, path); + watermark.read(overlay); + watermark.alphaChannel(Magick::SetAlphaChannel); + watermark.evaluate(Magick::AlphaChannel, Magick::MultiplyEvaluateOperator, + 0.5); + string query(to_string(frames.front().baseColumns()) + "x" + + to_string(frames.front().baseRows()) + "!"); + watermark.scale(Geometry(query)); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.composite(watermark, Magick::NorthGravity, Magick::OverCompositeOp); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/flip.cc b/natives/flip.cc index 54fea85..82a25d4 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -1,65 +1,51 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class FlipWorker : public Napi::AsyncWorker { - public: - FlipWorker(Napi::Function& callback, string in_path, bool flop, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), flop(flop), type(type), delay(delay) {} - ~FlipWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - flop ? image.flop() : image.flip(); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - bool flop; - int delay; - Blob blob; -}; - -Napi::Value Flip(const Napi::CallbackInfo &info) -{ +Napi::Value Flip(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - bool flop = obj.Has("flop") ? obj.Get("flop").As().Value() : false; + bool flop = + obj.Has("flop") ? obj.Get("flop").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - FlipWorker* flipWorker = new FlipWorker(cb, path, flop, type, delay); - flipWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + flop ? image.flop() : image.flip(); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/freeze.cc b/natives/freeze.cc index cb458f9..375ec38 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -1,56 +1,45 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class FreezeWorker : public Napi::AsyncWorker { - public: - FreezeWorker(Napi::Function& callback, string in_path, bool loop, int frame, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), loop(loop), frame(frame), type(type), delay(delay) {} - ~FreezeWorker() {} - - void Execute() { - list frames; - readImages(&frames, in_path); - - if (frame >= 0 && !loop) { - size_t frameSize = frames.size(); - int framePos = clamp(frame, 0, (int)frameSize); - frames.resize(framePos + 1); - } - for_each(frames.begin(), frames.end(), animationIterationsImage(loop ? 0 : 1)); - for_each(frames.begin(), frames.end(), magickImage(type)); - - if (delay != 0) for_each(frames.begin(), frames.end(), animationDelayImage(delay)); - writeImages(frames.begin(), frames.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int frame, delay; - Blob blob; - bool loop; -}; - -Napi::Value Freeze(const Napi::CallbackInfo &info) -{ +Napi::Value Freeze(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - bool loop = obj.Has("loop") ? obj.Get("loop").As().Value() : false; + bool loop = + obj.Has("loop") ? obj.Get("loop").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - int frame = obj.Has("frame") ? obj.Get("frame").As().Int32Value() : -1; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int frame = + obj.Has("frame") ? obj.Get("frame").As().Int32Value() : -1; - FreezeWorker* freezeWorker = new FreezeWorker(cb, path, loop, frame, type, delay); - freezeWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + readImages(&frames, path); + + if (frame >= 0 && !loop) { + size_t frameSize = frames.size(); + int framePos = clamp(frame, 0, (int)frameSize); + frames.resize(framePos + 1); + } + for_each(frames.begin(), frames.end(), + animationIterationsImage(loop ? 0 : 1)); + for_each(frames.begin(), frames.end(), magickImage(type)); + + if (delay != 0) + for_each(frames.begin(), frames.end(), animationDelayImage(delay)); + writeImages(frames.begin(), frames.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; } \ No newline at end of file diff --git a/natives/gamexplain.cc b/natives/gamexplain.cc index 5d6ae2a..883134a 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -1,68 +1,54 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class GamexplainWorker : public Napi::AsyncWorker { - public: - GamexplainWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~GamexplainWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_path); - watermark.read("./assets/images/gamexplain.png"); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.backgroundColor("white"); - image.scale(Geometry("1181x571!")); - image.extent(Geometry("1200x675-10-92")); - image.composite(watermark, Geometry("+0+0"), Magick::OverCompositeOp); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Gamexplain(const Napi::CallbackInfo &info) -{ +Napi::Value Gamexplain(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - GamexplainWorker* blurWorker = new GamexplainWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; + readImages(&frames, path); + watermark.read("./assets/images/gamexplain.png"); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.backgroundColor("white"); + image.scale(Geometry("1181x571!")); + image.extent(Geometry("1200x675-10-92")); + image.composite(watermark, Geometry("+0+0"), Magick::OverCompositeOp); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/globe.cc b/natives/globe.cc index 9e14a75..52fa98b 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -1,83 +1,70 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class GlobeWorker : public Napi::AsyncWorker { - public: - GlobeWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~GlobeWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image distort; - Image overlay; - readImages(&frames, in_path); - distort.read("./assets/images/spheremap.png"); - overlay.read("./assets/images/sphere_overlay.png"); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - if (type != "gif") { - list ::iterator it = coalesced.begin(); - for (int i = 0; i < 29; ++i) { - coalesced.push_back(*it); - } - } - - int i = 0; - for (Image &image : coalesced) { - image.scale(Geometry("500x500!")); - image.alphaChannel(Magick::SetAlphaChannel); - size_t width = image.columns(); - image.roll(Geometry("+" + to_string(width * i / coalesced.size()))); - image.composite(overlay, Magick::CenterGravity, Magick::HardLightCompositeOp); - image.composite(distort, Magick::CenterGravity, Magick::DistortCompositeOp); - image.magick("GIF"); - mid.push_back(image); - i++; - } - - optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) { - for_each(mid.begin(), mid.end(), animationDelayImage(delay)); - } else if (type != "gif") { - for_each(mid.begin(), mid.end(), animationDelayImage(5)); - } - - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "gif")}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Globe(const Napi::CallbackInfo &info) -{ +Napi::Value Globe(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - GlobeWorker* blurWorker = new GlobeWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image distort; + Image overlay; + readImages(&frames, path); + distort.read("./assets/images/spheremap.png"); + overlay.read("./assets/images/sphere_overlay.png"); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + if (type != "gif") { + list::iterator it = coalesced.begin(); + for (int i = 0; i < 29; ++i) { + coalesced.push_back(*it); + } + } + + int i = 0; + for (Image &image : coalesced) { + image.scale(Geometry("500x500!")); + image.alphaChannel(Magick::SetAlphaChannel); + size_t width = image.columns(); + image.roll(Geometry("+" + to_string(width * i / coalesced.size()))); + image.composite(overlay, Magick::CenterGravity, + Magick::HardLightCompositeOp); + image.composite(distort, Magick::CenterGravity, Magick::DistortCompositeOp); + image.magick("GIF"); + mid.push_back(image); + i++; + } + + optimizeTransparency(mid.begin(), mid.end()); + if (delay != 0) { + for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + } else if (type != "gif") { + for_each(mid.begin(), mid.end(), animationDelayImage(5)); + } + + 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; } \ No newline at end of file diff --git a/natives/homebrew.cc b/natives/homebrew.cc index ef87a37..2986d78 100644 --- a/natives/homebrew.cc +++ b/natives/homebrew.cc @@ -1,47 +1,33 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class HomebrewWorker : public Napi::AsyncWorker { - public: - HomebrewWorker(Napi::Function& callback, string text) - : Napi::AsyncWorker(callback), text(text) {} - ~HomebrewWorker() {} - - void Execute() { - Image image; - image.read("./assets/images/hbc.png"); - image.textGravity(Magick::CenterGravity); - image.font("./assets/hbc.ttf"); - image.textKerning(-5); - image.fillColor("white"); - image.fontPointsize(96); - image.draw(DrawableText(0, 0, text)); - image.magick("PNG"); - image.write(&blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "png")}); - } - - private: - string text; - Blob blob; -}; - -Napi::Value Homebrew(const Napi::CallbackInfo &info) -{ +Napi::Value Homebrew(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string caption = obj.Get("caption").As().Utf8Value(); - HomebrewWorker* explodeWorker = new HomebrewWorker(cb, caption); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + Image image; + image.read("./assets/images/hbc.png"); + image.textGravity(Magick::CenterGravity); + image.font("./assets/hbc.ttf"); + image.textKerning(-5); + image.fillColor("white"); + image.fontPointsize(96); + image.draw(DrawableText(0, 0, caption)); + image.magick("PNG"); + image.write(&blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", + Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); + result.Set("type", "png"); + return result; } \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index bfda3ea..aba1ede 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -19,7 +19,7 @@ #include "magik.h" #include "meme.h" #include "mirror.h" -#include "misc.h" +#include "swirl.h" #include "motivate.h" #include "resize.h" #include "retro.h" diff --git a/natives/invert.cc b/natives/invert.cc index a33346f..9941216 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -1,65 +1,51 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class InvertWorker : public Napi::AsyncWorker { - public: - InvertWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~InvertWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for_each(coalesced.begin(), coalesced.end(), negateImage()); - for (Image &image : coalesced) { - image.negateChannel(Magick::AlphaChannel); - mid.push_back(image); - } - // Magick::ChannelType(Magick::CompositeChannels ^ Magick::AlphaChannel) - for_each(mid.begin(), mid.end(), magickImage(type)); - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Invert(const Napi::CallbackInfo &info) -{ +Napi::Value Invert(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - InvertWorker* invertWorker = new InvertWorker(cb, path, type, delay); - invertWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for_each(coalesced.begin(), coalesced.end(), negateImage()); + for (Image &image : coalesced) { + image.negateChannel(Magick::AlphaChannel); + mid.push_back(image); + } + // Magick::ChannelType(Magick::CompositeChannels ^ Magick::AlphaChannel) + for_each(mid.begin(), mid.end(), magickImage(type)); + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/jpeg.cc b/natives/jpeg.cc index b10031d..05a906c 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -1,42 +1,28 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class JpegWorker : public Napi::AsyncWorker { - public: - JpegWorker(Napi::Function& callback, string in_path) - : Napi::AsyncWorker(callback), in_path(in_path) {} - ~JpegWorker() {} - - void Execute() { - Image image; - image.read(in_path); - image.quality(1); - image.magick("JPEG"); - image.write(&blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "jpg")}); - } - - private: - string in_path; - Blob blob; -}; - -Napi::Value Jpeg(const Napi::CallbackInfo &info) -{ +Napi::Value Jpeg(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - JpegWorker* explodeWorker = new JpegWorker(cb, path); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + Image image; + image.read(path); + image.quality(1); + image.magick("JPEG"); + image.write(&blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", + Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); + result.Set("type", "jpg"); + return result; } \ No newline at end of file diff --git a/natives/leak.cc b/natives/leak.cc index 145e345..055fa81 100644 --- a/natives/leak.cc +++ b/natives/leak.cc @@ -1,69 +1,55 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class LeakWorker : public Napi::AsyncWorker { - public: - LeakWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~LeakWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_path); - watermark.read("./assets/images/leak.png"); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.backgroundColor("white"); - image.scale(Geometry("640x360!")); - image.rotate(15); - image.extent(Geometry("1280x720-700+100")); - image.composite(watermark, Geometry("+0+0"), Magick::OverCompositeOp); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Leak(const Napi::CallbackInfo &info) -{ +Napi::Value Leak(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - LeakWorker* blurWorker = new LeakWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; + readImages(&frames, path); + watermark.read("./assets/images/leak.png"); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.backgroundColor("white"); + image.scale(Geometry("640x360!")); + image.rotate(15); + image.extent(Geometry("1280x720-700+100")); + image.composite(watermark, Geometry("+0+0"), Magick::OverCompositeOp); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/magik.cc b/natives/magik.cc index 068bdbf..a81986d 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -1,66 +1,51 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class MagikWorker : public Napi::AsyncWorker { - public: - MagikWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~MagikWorker() {} - - void Execute() { - list frames; - list coalesced; - list blurred; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.scale(Geometry("350x350")); - image.liquidRescale(Geometry("175x175")); - image.liquidRescale(Geometry("350x350")); - image.magick(type); - blurred.push_back(image); - } - - optimizeTransparency(blurred.begin(), blurred.end()); - - if (type == "gif") { - for (Image &image : blurred) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(blurred.begin(), blurred.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Magik(const Napi::CallbackInfo &info) -{ +Napi::Value Magik(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + Blob blob; - MagikWorker* explodeWorker = new MagikWorker(cb, path, type, delay); - explodeWorker->Queue(); - return env.Undefined(); + list frames; + list coalesced; + list blurred; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.scale(Geometry("350x350")); + image.liquidRescale(Geometry("175x175")); + image.liquidRescale(Geometry("350x350")); + image.magick(type); + blurred.push_back(image); + } + + optimizeTransparency(blurred.begin(), blurred.end()); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + writeImages(blurred.begin(), blurred.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; } \ No newline at end of file diff --git a/natives/meme.cc b/natives/meme.cc index cb7aac6..77d61ef 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -1,97 +1,87 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class MemeWorker : public Napi::AsyncWorker { - public: - MemeWorker(Napi::Function& callback, string in_path, string text_top, string text_bottom, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), text_top(text_top), text_bottom(text_bottom), type(type), delay(delay) {} - ~MemeWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image top_text; - Image bottom_text; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - for_each(coalesced.begin(), coalesced.end(), scaleImage(Geometry(600, 600))); - - top_text.size(Geometry(to_string(coalesced.front().columns()))); - top_text.backgroundColor("none"); - top_text.font("Impact"); - top_text.fontPointsize(40); - top_text.textGravity(Magick::CenterGravity); - top_text.read("pango:" + text_top + ""); - Image top_text_fill = top_text; - top_text_fill.channel(Magick::AlphaChannel); - top_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon"); - top_text_fill.backgroundColor("black"); - top_text_fill.alphaChannel(Magick::ShapeAlphaChannel); - top_text.composite(top_text_fill, Magick::CenterGravity, Magick::DstOverCompositeOp); - - if (text_bottom != "") { - bottom_text.size(Geometry(to_string(coalesced.front().columns()))); - bottom_text.backgroundColor("none"); - bottom_text.font("Impact"); - bottom_text.fontPointsize(40); - bottom_text.textGravity(Magick::CenterGravity); - bottom_text.read("pango:" + text_bottom + ""); - Image bottom_text_fill = bottom_text; - bottom_text_fill.channel(Magick::AlphaChannel); - bottom_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon"); - bottom_text_fill.backgroundColor("black"); - bottom_text_fill.alphaChannel(Magick::ShapeAlphaChannel); - bottom_text.composite(bottom_text_fill, Magick::CenterGravity, Magick::DstOverCompositeOp); - } - - for (Image &image : coalesced) { - image.composite(top_text, Magick::NorthGravity, Magick::OverCompositeOp); - if (text_bottom != "") image.composite(bottom_text, Magick::SouthGravity, Magick::OverCompositeOp); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type, text_top, text_bottom; - int delay; - Blob blob; -}; - -Napi::Value Meme(const Napi::CallbackInfo &info) -{ +Napi::Value Meme(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string top = obj.Get("top").As().Utf8Value(); string bottom = obj.Get("bottom").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - MemeWorker* blurWorker = new MemeWorker(cb, path, top, bottom, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image top_text; + Image bottom_text; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + for_each(coalesced.begin(), coalesced.end(), scaleImage(Geometry(600, 600))); + + top_text.size(Geometry(to_string(coalesced.front().columns()))); + top_text.backgroundColor("none"); + top_text.font("Impact"); + top_text.fontPointsize(40); + top_text.textGravity(Magick::CenterGravity); + top_text.read("pango:" + top + ""); + Image top_text_fill = top_text; + top_text_fill.channel(Magick::AlphaChannel); + top_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon"); + top_text_fill.backgroundColor("black"); + top_text_fill.alphaChannel(Magick::ShapeAlphaChannel); + top_text.composite(top_text_fill, Magick::CenterGravity, + Magick::DstOverCompositeOp); + + if (bottom != "") { + bottom_text.size(Geometry(to_string(coalesced.front().columns()))); + bottom_text.backgroundColor("none"); + bottom_text.font("Impact"); + bottom_text.fontPointsize(40); + bottom_text.textGravity(Magick::CenterGravity); + bottom_text.read("pango:" + bottom + ""); + Image bottom_text_fill = bottom_text; + bottom_text_fill.channel(Magick::AlphaChannel); + bottom_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon"); + bottom_text_fill.backgroundColor("black"); + bottom_text_fill.alphaChannel(Magick::ShapeAlphaChannel); + bottom_text.composite(bottom_text_fill, Magick::CenterGravity, + Magick::DstOverCompositeOp); + } + + for (Image &image : coalesced) { + image.composite(top_text, Magick::NorthGravity, Magick::OverCompositeOp); + if (bottom != "") + image.composite(bottom_text, Magick::SouthGravity, + Magick::OverCompositeOp); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/mirror.cc b/natives/mirror.cc index b360f43..a8c1ede 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -1,93 +1,86 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class MirrorWorker : public Napi::AsyncWorker { - public: - MirrorWorker(Napi::Function& callback, string in_path, bool vertical, bool first, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), vertical(vertical), first(first), type(type), delay(delay) {} - ~MirrorWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - MagickCore::GravityType gravity; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - if (vertical && first) { - gravity = Magick::NorthGravity; - } else if (!vertical && first) { - gravity = Magick::WestGravity; - } else if (vertical && !first) { - gravity = Magick::SouthGravity; - } else { - gravity = Magick::EastGravity; - } - - for (Image &image : coalesced) { - list mirrored; - Image final; - image.extent(Geometry(to_string(vertical ? image.baseColumns() : image.baseColumns() / 2) + "x" + to_string(vertical ? image.baseRows() / 2 : image.baseRows())), gravity); - mirrored.push_back(image); - Image mirror = image; - if (vertical) { - mirror.flip(); - } else { - mirror.flop(); - } - if (first) { - mirrored.push_back(mirror); - } else { - mirrored.push_front(mirror); - } - appendImages(&final, mirrored.begin(), mirrored.end(), vertical); - final.repage(); - final.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.quantizeDither(false); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - bool vertical, first; - Blob blob; -}; - -Napi::Value Mirror(const Napi::CallbackInfo &info) -{ +Napi::Value Mirror(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - bool vertical = obj.Has("vertical") ? obj.Get("vertical").As().Value() : false; - bool first = obj.Has("first") ? obj.Get("first").As().Value() : false; + bool vertical = obj.Has("vertical") + ? obj.Get("vertical").As().Value() + : false; + bool first = + obj.Has("first") ? obj.Get("first").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - MirrorWorker* mirrorWorker = new MirrorWorker(cb, path, vertical, first, type, delay); - mirrorWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + MagickCore::GravityType gravity; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + if (vertical && first) { + gravity = Magick::NorthGravity; + } else if (!vertical && first) { + gravity = Magick::WestGravity; + } else if (vertical && !first) { + gravity = Magick::SouthGravity; + } else { + gravity = Magick::EastGravity; + } + + for (Image &image : coalesced) { + list mirrored; + Image final; + image.extent( + Geometry(to_string(vertical ? image.baseColumns() + : image.baseColumns() / 2) + + "x" + + to_string(vertical ? image.baseRows() / 2 : image.baseRows())), + gravity); + mirrored.push_back(image); + Image mirror = image; + if (vertical) { + mirror.flip(); + } else { + mirror.flop(); + } + if (first) { + mirrored.push_back(mirror); + } else { + mirrored.push_front(mirror); + } + appendImages(&final, mirrored.begin(), mirrored.end(), vertical); + final.repage(); + final.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.quantizeDither(false); + 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; } \ No newline at end of file diff --git a/natives/misc.cc b/natives/misc.cc deleted file mode 100644 index e688da3..0000000 --- a/natives/misc.cc +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include -#include - -using namespace std; -using namespace Magick; - -class SwirlWorker : public Napi::AsyncWorker { - public: - SwirlWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~SwirlWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.swirl(180); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Swirl(const Napi::CallbackInfo &info) -{ - Napi::Env env = info.Env(); - - Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); - string path = obj.Get("path").As().Utf8Value(); - string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - - SwirlWorker* flopWorker = new SwirlWorker(cb, path, type, delay); - flopWorker->Queue(); - return env.Undefined(); -} \ No newline at end of file diff --git a/natives/motivate.cc b/natives/motivate.cc index fb5cc6f..f2e3efc 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -1,99 +1,91 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class MotivateWorker : public Napi::AsyncWorker { - public: - MotivateWorker(Napi::Function& callback, string in_path, string top_text, string bottom_text, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), top_text(top_text), bottom_text(bottom_text), type(type), delay(delay) {} - ~MotivateWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image top; - Image bottom; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - top.size(Geometry("600")); - top.backgroundColor("black"); - top.font("Times"); - top.textGravity(Magick::CenterGravity); - top.fontPointsize(56); - top.read("pango:" + top_text + ""); - top.extent(Geometry(bottom_text != "" ? to_string(top.columns()) + "x" + to_string(top.rows()) : to_string(top.columns()) + "x" + to_string(top.rows() + 20)), "black", Magick::NorthGravity); - - if (bottom_text != "") { - bottom.size(Geometry("600")); - bottom.backgroundColor("black"); - bottom.font("Times"); - bottom.textGravity(Magick::CenterGravity); - bottom.fontPointsize(28); - bottom.read("pango:" + bottom_text + ""); - bottom.extent(Geometry(to_string(bottom.columns()) + "x" + to_string(bottom.rows() + 20)), "black", Magick::NorthGravity); - } - - for (Image &image : coalesced) { - Image final; - image.scale(Geometry(500, 500)); - image.borderColor("black"); - image.border(Geometry(5, 5)); - image.borderColor("white"); - image.border(Geometry(3, 3)); - image.backgroundColor("black"); - image.extent(Geometry(600, image.rows() + 50), Magick::CenterGravity); - - list to_append; - to_append.push_back(image); - to_append.push_back(top); - if (bottom_text != "") to_append.push_back(bottom); - appendImages(&final, to_append.begin(), to_append.end(), true); - final.repage(); - final.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.quantizeDither(false); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type, top_text, bottom_text; - int delay; - Blob blob; -}; - -Napi::Value Motivate(const Napi::CallbackInfo &info) -{ +Napi::Value Motivate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - string top = obj.Get("top").As().Utf8Value(); - string bottom = obj.Get("bottom").As().Utf8Value(); + string top_text = obj.Get("top").As().Utf8Value(); + string bottom_text = obj.Get("bottom").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - MotivateWorker* blurWorker = new MotivateWorker(cb, path, top, bottom, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image top; + Image bottom; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + top.size(Geometry("600")); + top.backgroundColor("black"); + top.font("Times"); + top.textGravity(Magick::CenterGravity); + top.fontPointsize(56); + top.read("pango:" + top_text + ""); + top.extent(Geometry(bottom_text != "" ? to_string(top.columns()) + "x" + + to_string(top.rows()) + : to_string(top.columns()) + "x" + + to_string(top.rows() + 20)), + "black", Magick::NorthGravity); + + if (bottom_text != "") { + bottom.size(Geometry("600")); + bottom.backgroundColor("black"); + bottom.font("Times"); + bottom.textGravity(Magick::CenterGravity); + bottom.fontPointsize(28); + bottom.read("pango:" + bottom_text + ""); + bottom.extent(Geometry(to_string(bottom.columns()) + "x" + + to_string(bottom.rows() + 20)), + "black", Magick::NorthGravity); + } + + for (Image &image : coalesced) { + Image final; + image.scale(Geometry(500, 500)); + image.borderColor("black"); + image.border(Geometry(5, 5)); + image.borderColor("white"); + image.border(Geometry(3, 3)); + image.backgroundColor("black"); + image.extent(Geometry(600, image.rows() + 50), Magick::CenterGravity); + + list to_append; + to_append.push_back(image); + to_append.push_back(top); + if (bottom_text != "") to_append.push_back(bottom); + appendImages(&final, to_append.begin(), to_append.end(), true); + final.repage(); + final.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.quantizeDither(false); + 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; } \ No newline at end of file diff --git a/natives/resize.cc b/natives/resize.cc index 6086a48..d3ecc4b 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -1,73 +1,62 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class ResizeWorker : public Napi::AsyncWorker { - public: - ResizeWorker(Napi::Function& callback, string in_path, bool stretch, bool wide, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), stretch(stretch), wide(wide), type(type), delay(delay) {} - ~ResizeWorker() {} - - void Execute() { - list frames; - list coalesced; - list blurred; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - if (stretch) { - image.resize(Geometry("512x512!")); - } else if (wide) { - image.resize(Geometry(to_string((image.baseColumns() * 19) / 2) + "x" + to_string(image.baseRows() / 2) + "!")); - } else { - image.scale(Geometry("10%")); - image.scale(Geometry("1000%")); - } - image.magick(type); - blurred.push_back(image); - } - - optimizeTransparency(blurred.begin(), blurred.end()); - - if (type == "gif") { - for (Image &image : blurred) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(blurred.begin(), blurred.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay, amount; - Blob blob; - bool stretch, wide; -}; - -Napi::Value Resize(const Napi::CallbackInfo &info) -{ +Napi::Value Resize(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - bool stretch = obj.Has("stretch") ? obj.Get("stretch").As().Value() : false; - bool wide = obj.Has("wide") ? obj.Get("wide").As().Value() : false; + bool stretch = obj.Has("stretch") + ? obj.Get("stretch").As().Value() + : false; + bool wide = + obj.Has("wide") ? obj.Get("wide").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - ResizeWorker* explodeWorker = new ResizeWorker(cb, path, stretch, wide, type, delay); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list blurred; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + if (stretch) { + image.resize(Geometry("512x512!")); + } else if (wide) { + image.resize(Geometry(to_string((image.baseColumns() * 19) / 2) + "x" + + to_string(image.baseRows() / 2) + "!")); + } else { + image.scale(Geometry("10%")); + image.scale(Geometry("1000%")); + } + image.magick(type); + blurred.push_back(image); + } + + optimizeTransparency(blurred.begin(), blurred.end()); + + if (type == "gif") { + for (Image &image : blurred) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + writeImages(blurred.begin(), blurred.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; } \ No newline at end of file diff --git a/natives/retro.cc b/natives/retro.cc index de1b1db..737bd97 100644 --- a/natives/retro.cc +++ b/natives/retro.cc @@ -1,94 +1,84 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class RetroWorker : public Napi::AsyncWorker { - public: - RetroWorker(Napi::Function& callback, string line1, string line2, string line3) - : Napi::AsyncWorker(callback), line1(line1), line2(line2), line3(line3) {} - ~RetroWorker() {} - - void Execute() { - Image image; - Image line1_text; - Image line2_text; - Image line3_text; - - image.read("./assets/images/retro.png"); - - line2_text.backgroundColor("none"); - line2_text.fontPointsize(128); - line2_text.textGravity(Magick::CenterGravity); - line2_text.font("Comic Sans MS"); - line2_text.read("pango:" + (line2 == "" ? line1 : line2) + ""); - line2_text.extent(Geometry("1260x859+0+0"), Magick::CenterGravity); - Image line2_text_fill = line2_text; - line2_text_fill.channel(Magick::AlphaChannel); - line2_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon:10"); - line2_text_fill.backgroundColor("gray"); - line2_text_fill.alphaChannel(Magick::ShapeAlphaChannel); - line2_text.composite(line2_text_fill, Magick::CenterGravity, Magick::DstOverCompositeOp); - image.composite(line2_text, Geometry("+0-100"), Magick::OverCompositeOp); - - if (line2 != "") { - line1_text.backgroundColor("none"); - line1_text.fontPointsize(64); - line1_text.textGravity(Magick::CenterGravity); - line1_text.font("Comic Sans MS"); - line1_text.read("pango:" + line1 + ""); - line1_text.extent(Geometry("1260x859+0+0"), Magick::CenterGravity); - Image line1_text_fill = line1_text; - line1_text_fill.channel(Magick::AlphaChannel); - line1_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon:10"); - line1_text_fill.backgroundColor("gray"); - line1_text_fill.alphaChannel(Magick::ShapeAlphaChannel); - line1_text.composite(line1_text_fill, Magick::CenterGravity, Magick::DstOverCompositeOp); - image.composite(line1_text, Geometry("+0-250"), Magick::OverCompositeOp); - } - - if (line3 != "") { - line3_text.backgroundColor("none"); - line3_text.fontPointsize(64); - line3_text.textGravity(Magick::CenterGravity); - line3_text.font("Comic Sans MS"); - line3_text.read("pango:" + line3 + ""); - line3_text.extent(Geometry("1260x859+0+0"), Magick::CenterGravity); - Image line3_text_fill = line3_text; - line3_text_fill.channel(Magick::AlphaChannel); - line3_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon:10"); - line3_text_fill.backgroundColor("gray"); - line3_text_fill.alphaChannel(Magick::ShapeAlphaChannel); - line3_text.composite(line3_text_fill, Magick::CenterGravity, Magick::DstOverCompositeOp); - image.composite(line3_text, Geometry("+0+50"), Magick::OverCompositeOp); - } - - image.magick("PNG"); - image.write(&blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "png")}); - } - - private: - string line1, line2, line3; - Blob blob; -}; - -Napi::Value Retro(const Napi::CallbackInfo &info) -{ +Napi::Value Retro(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string line1 = obj.Get("line1").As().Utf8Value(); string line2 = obj.Get("line2").As().Utf8Value(); string line3 = obj.Get("line3").As().Utf8Value(); - RetroWorker* retroWorker = new RetroWorker(cb, line1, line2, line3); - retroWorker->Queue(); - return env.Undefined(); + Blob blob; + + Image image; + Image line1_text; + Image line2_text; + Image line3_text; + + image.read("./assets/images/retro.png"); + + line2_text.backgroundColor("none"); + line2_text.fontPointsize(128); + line2_text.textGravity(Magick::CenterGravity); + line2_text.font("Comic Sans MS"); + line2_text.read("pango:" + + (line2 == "" ? line1 : line2) + ""); + line2_text.extent(Geometry("1260x859+0+0"), Magick::CenterGravity); + Image line2_text_fill = line2_text; + line2_text_fill.channel(Magick::AlphaChannel); + line2_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon:10"); + line2_text_fill.backgroundColor("gray"); + line2_text_fill.alphaChannel(Magick::ShapeAlphaChannel); + line2_text.composite(line2_text_fill, Magick::CenterGravity, + Magick::DstOverCompositeOp); + image.composite(line2_text, Geometry("+0-100"), Magick::OverCompositeOp); + + if (line2 != "") { + line1_text.backgroundColor("none"); + line1_text.fontPointsize(64); + line1_text.textGravity(Magick::CenterGravity); + line1_text.font("Comic Sans MS"); + line1_text.read("pango:" + line1 + ""); + line1_text.extent(Geometry("1260x859+0+0"), Magick::CenterGravity); + Image line1_text_fill = line1_text; + line1_text_fill.channel(Magick::AlphaChannel); + line1_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon:10"); + line1_text_fill.backgroundColor("gray"); + line1_text_fill.alphaChannel(Magick::ShapeAlphaChannel); + line1_text.composite(line1_text_fill, Magick::CenterGravity, + Magick::DstOverCompositeOp); + image.composite(line1_text, Geometry("+0-250"), Magick::OverCompositeOp); + } + + if (line3 != "") { + line3_text.backgroundColor("none"); + line3_text.fontPointsize(64); + line3_text.textGravity(Magick::CenterGravity); + line3_text.font("Comic Sans MS"); + line3_text.read("pango:" + line3 + ""); + line3_text.extent(Geometry("1260x859+0+0"), Magick::CenterGravity); + Image line3_text_fill = line3_text; + line3_text_fill.channel(Magick::AlphaChannel); + line3_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon:10"); + line3_text_fill.backgroundColor("gray"); + line3_text_fill.alphaChannel(Magick::ShapeAlphaChannel); + line3_text.composite(line3_text_fill, Magick::CenterGravity, + Magick::DstOverCompositeOp); + image.composite(line3_text, Geometry("+0+50"), Magick::OverCompositeOp); + } + + image.magick("PNG"); + image.write(&blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", + Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); + result.Set("type", "png"); + return result; } \ No newline at end of file diff --git a/natives/reverse.cc b/natives/reverse.cc index 9cac1f0..f569ae7 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -1,65 +1,51 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class ReverseWorker : public Napi::AsyncWorker { - public: - ReverseWorker(Napi::Function& callback, string in_path, bool soos, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), soos(soos), delay(delay) {} - ~ReverseWorker() {} - - void Execute() { - list frames; - list coalesced; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - if (soos) { - list copy = coalesced; - copy.reverse(); - coalesced.insert(coalesced.end(), copy.begin(), copy.end()); - } else { - coalesced.reverse(); - } - - for_each(coalesced.begin(), coalesced.end(), magickImage("GIF")); - - optimizeTransparency(coalesced.begin(), coalesced.end()); - - for (Image &image : coalesced) { - image.quantizeDither(false); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - - writeImages(coalesced.begin(), coalesced.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "gif")}); - } - - private: - string in_path; - int delay; - Blob blob; - bool soos; -}; - -Napi::Value Reverse(const Napi::CallbackInfo &info) -{ +Napi::Value Reverse(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - bool soos = obj.Has("soos") ? obj.Get("soos").As().Value() : false; - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + bool soos = + obj.Has("soos") ? obj.Get("soos").As().Value() : false; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - ReverseWorker* explodeWorker = new ReverseWorker(cb, path, soos, delay); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + if (soos) { + list copy = coalesced; + copy.reverse(); + coalesced.insert(coalesced.end(), copy.begin(), copy.end()); + } else { + coalesced.reverse(); + } + + for_each(coalesced.begin(), coalesced.end(), magickImage("GIF")); + + optimizeTransparency(coalesced.begin(), coalesced.end()); + + for (Image &image : coalesced) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + + writeImages(coalesced.begin(), coalesced.end(), &blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", + Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); + result.Set("type", "gif"); + return result; } \ No newline at end of file diff --git a/natives/scott.cc b/natives/scott.cc index 91fdfa5..cca5f37 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -1,72 +1,60 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class ScottWorker : public Napi::AsyncWorker { - public: - ScottWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~ScottWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_path); - watermark.read("./assets/images/scott.png"); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - Image watermark_new = watermark; - image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); - image.backgroundColor("none"); - image.scale(Geometry("415x234!")); - double arguments[16] = {0, 0, 129, 187, 415, 0, 517, 182, 415, 234, 517, 465, 0, 234, 132, 418}; - image.distort(Magick::PerspectiveDistortion, 16, arguments, true); - image.extent(Geometry("864x481"), Magick::CenterGravity); - watermark_new.composite(image, Geometry("-110+83"), Magick::OverCompositeOp); - watermark_new.magick(type); - watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); - mid.push_back(watermark_new); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Scott(const Napi::CallbackInfo &info) -{ +Napi::Value Scott(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - ScottWorker* blurWorker = new ScottWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; + readImages(&frames, path); + watermark.read("./assets/images/scott.png"); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + Image watermark_new = watermark; + image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); + image.backgroundColor("none"); + image.scale(Geometry("415x234!")); + double arguments[16] = {0, 0, 129, 187, 415, 0, 517, 182, + 415, 234, 517, 465, 0, 234, 132, 418}; + image.distort(Magick::PerspectiveDistortion, 16, arguments, true); + image.extent(Geometry("864x481"), Magick::CenterGravity); + watermark_new.composite(image, Geometry("-110+83"), + Magick::OverCompositeOp); + watermark_new.magick(type); + watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); + mid.push_back(watermark_new); + } + + 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; } \ No newline at end of file diff --git a/natives/sonic.cc b/natives/sonic.cc index 5646cc4..4253786 100644 --- a/natives/sonic.cc +++ b/natives/sonic.cc @@ -1,50 +1,36 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class SonicWorker : public Napi::AsyncWorker { - public: - SonicWorker(Napi::Function& callback, string text) - : Napi::AsyncWorker(callback), text(text) {} - ~SonicWorker() {} - - void Execute() { - Image image; - Image text_image; - text_image.backgroundColor("none"); - text_image.fontPointsize(72); - text_image.textGravity(Magick::CenterGravity); - text_image.font("Bitstream Vera Sans"); - text_image.read("pango:" + text + ""); - text_image.resize(Geometry(474, 332)); - text_image.extent(Geometry("1024x538-435-145"), Magick::CenterGravity); - image.read("./assets/images/sonic.jpg"); - image.composite(text_image, Geometry("+160+10"), Magick::OverCompositeOp); - image.magick("PNG"); - image.write(&blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "png")}); - } - - private: - string text; - Blob blob; -}; - -Napi::Value Sonic(const Napi::CallbackInfo &info) -{ +Napi::Value Sonic(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string text = obj.Get("text").As().Utf8Value(); - SonicWorker* explodeWorker = new SonicWorker(cb, text); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + Image image; + Image text_image; + text_image.backgroundColor("none"); + text_image.fontPointsize(72); + text_image.textGravity(Magick::CenterGravity); + text_image.font("Bitstream Vera Sans"); + text_image.read("pango:" + text + ""); + text_image.resize(Geometry(474, 332)); + text_image.extent(Geometry("1024x538-435-145"), Magick::CenterGravity); + image.read("./assets/images/sonic.jpg"); + image.composite(text_image, Geometry("+160+10"), Magick::OverCompositeOp); + image.magick("PNG"); + image.write(&blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", + Napi::Buffer::Copy(env, (char *)blob.data(), blob.length())); + result.Set("type", "png"); + return result; } \ No newline at end of file diff --git a/natives/speed.cc b/natives/speed.cc index a3484ad..b0a28f3 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -1,93 +1,78 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class SpeedWorker : public Napi::AsyncWorker { - public: - SpeedWorker(Napi::Function& callback, string in_path, bool slow, int speed, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), slow(slow), speed(speed), type(type), delay(delay) {} - ~SpeedWorker() {} - - void Execute() { - list frames; - readImages(&frames, in_path); - - // if passed a delay, use that. otherwise use the average frame delay. - if (delay == 0) { - vector old_delays; - bool removeFrames = false; - for (Image &image : frames) { - int animation_delay = image.animationDelay(); - old_delays.push_back(animation_delay); - } - - for (Image &image : frames) { - int old_delay = image.animationDelay(); - int new_delay = slow ? old_delay * speed : old_delay / speed; - if (!slow && new_delay <= 1) { - removeFrames = true; - break; - } - image.animationDelay(new_delay); - } - - if (removeFrames) { - for (list ::iterator i = frames.begin(); i != frames.end(); ++i) { - int index = distance(frames.begin(), i); - i->animationDelay(old_delays[index]); - } - - for (int i = 0; i < speed - 1; ++i) { - frames.remove_if([counter = 0](const auto x) mutable { - return ++counter % 2 == 0; - }); - } - } - } else { - int new_delay = slow ? delay * speed : delay / speed; - if (!slow && new_delay <= 1) { - for (int i = 0; i < speed - 1; ++i) { - frames.remove_if([counter = 0](const auto x) mutable { - return ++counter % 2 == 0; - }); - } - } else { - for_each(frames.begin(), frames.end(), animationDelayImage(new_delay)); - } - } - - for_each(frames.begin(), frames.end(), magickImage(type)); - - writeImages(frames.begin(), frames.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - bool slow; - string in_path, type; - int speed, delay; - Blob blob; -}; - -Napi::Value Speed(const Napi::CallbackInfo &info) -{ +Napi::Value Speed(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); - bool slow = obj.Has("slow") ? obj.Get("slow").As().Value() : false; + bool slow = + obj.Has("slow") ? obj.Get("slow").As().Value() : false; string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - int speed = obj.Has("speed") ? obj.Get("speed").As().Int32Value() : 2; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int speed = + obj.Has("speed") ? obj.Get("speed").As().Int32Value() : 2; - SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, speed, type, delay); - explodeWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + readImages(&frames, path); + + // if passed a delay, use that. otherwise use the average frame delay. + if (delay == 0) { + vector old_delays; + bool removeFrames = false; + for (Image &image : frames) { + int animation_delay = image.animationDelay(); + old_delays.push_back(animation_delay); + } + + for (Image &image : frames) { + int old_delay = image.animationDelay(); + int new_delay = slow ? old_delay * speed : old_delay / speed; + if (!slow && new_delay <= 1) { + removeFrames = true; + break; + } + image.animationDelay(new_delay); + } + + if (removeFrames) { + for (list::iterator i = frames.begin(); i != frames.end(); ++i) { + int index = distance(frames.begin(), i); + i->animationDelay(old_delays[index]); + } + + for (int i = 0; i < speed - 1; ++i) { + frames.remove_if( + [counter = 0](const auto x) mutable { return ++counter % 2 == 0; }); + } + } + } else { + int new_delay = slow ? delay * speed : delay / speed; + if (!slow && new_delay <= 1) { + for (int i = 0; i < speed - 1; ++i) { + frames.remove_if( + [counter = 0](const auto x) mutable { return ++counter % 2 == 0; }); + } + } else { + for_each(frames.begin(), frames.end(), animationDelayImage(new_delay)); + } + } + + for_each(frames.begin(), frames.end(), magickImage(type)); + + writeImages(frames.begin(), frames.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; } \ No newline at end of file diff --git a/natives/spin.cc b/natives/spin.cc index ff707c1..b356536 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -1,80 +1,67 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class SpinWorker : public Napi::AsyncWorker { - public: - SpinWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~SpinWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - if (type != "gif") { - list ::iterator it = coalesced.begin(); - for (int i = 0; i < 29; ++i) { - coalesced.push_back(*it); - } - } - - int i = 0; - for (Image &image : coalesced) { - image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); - image.scale(Geometry("256x256")); - image.alphaChannel(Magick::SetAlphaChannel); - double rotation[1] = {360 * i / coalesced.size()}; - image.distort(Magick::ScaleRotateTranslateDistortion, 1, rotation); - image.magick("GIF"); - mid.push_back(image); - i++; - } - - for_each(mid.begin(), mid.end(), gifDisposeMethodImage(Magick::BackgroundDispose)); - - optimizeTransparency(mid.begin(), mid.end()); - if (delay != 0) { - for_each(mid.begin(), mid.end(), animationDelayImage(delay)); - } else if (type != "gif") { - for_each(mid.begin(), mid.end(), animationDelayImage(5)); - } - - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), "gif")}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Spin(const Napi::CallbackInfo &info) -{ +Napi::Value Spin(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - SpinWorker* blurWorker = new SpinWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + if (type != "gif") { + list::iterator it = coalesced.begin(); + for (int i = 0; i < 29; ++i) { + coalesced.push_back(*it); + } + } + + int i = 0; + for (Image &image : coalesced) { + image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); + image.scale(Geometry("256x256")); + image.alphaChannel(Magick::SetAlphaChannel); + double rotation[1] = {360 * i / coalesced.size()}; + image.distort(Magick::ScaleRotateTranslateDistortion, 1, rotation); + image.magick("GIF"); + mid.push_back(image); + i++; + } + + for_each(mid.begin(), mid.end(), + gifDisposeMethodImage(Magick::BackgroundDispose)); + + optimizeTransparency(mid.begin(), mid.end()); + if (delay != 0) { + for_each(mid.begin(), mid.end(), animationDelayImage(delay)); + } else if (type != "gif") { + for_each(mid.begin(), mid.end(), animationDelayImage(5)); + } + + 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", "gif"); + return result; } \ No newline at end of file diff --git a/natives/swirl.cc b/natives/swirl.cc new file mode 100644 index 0000000..edbf559 --- /dev/null +++ b/natives/swirl.cc @@ -0,0 +1,49 @@ +#include +#include + +#include + +using namespace std; +using namespace Magick; + +Napi::Value Swirl(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + + Napi::Object obj = info[0].As(); + string path = obj.Get("path").As().Utf8Value(); + 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; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.swirl(180); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDither(false); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; +} \ No newline at end of file diff --git a/natives/misc.h b/natives/swirl.h similarity index 50% rename from natives/misc.h rename to natives/swirl.h index 3e8b360..3bd7b31 100644 --- a/natives/misc.h +++ b/natives/swirl.h @@ -1,5 +1,5 @@ -#ifndef ESMBOT_NATIVES_MISC_H_ -#define ESMBOT_NATIVES_MISC_H_ +#ifndef ESMBOT_NATIVES_SWIRL_H_ +#define ESMBOT_NATIVES_SWIRL_H_ #include diff --git a/natives/tile.cc b/natives/tile.cc index 0d61da3..63a839b 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -1,77 +1,63 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class TileWorker : public Napi::AsyncWorker { - public: - TileWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~TileWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - list duplicated; - Image appended; - list montage; - Image frame; - image.magick(type); - for (int i = 0; i < 5; ++i) { - duplicated.push_back(image); - } - appendImages(&appended, duplicated.begin(), duplicated.end()); - appended.repage(); - for (int i = 0; i < 5; ++i) { - montage.push_back(appended); - } - appendImages(&frame, montage.begin(), montage.end(), true); - frame.repage(); - frame.scale(Geometry("800x800>")); - frame.animationDelay(delay == 0 ? image.animationDelay() : delay); - mid.push_back(frame); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Tile(const Napi::CallbackInfo &info) -{ +Napi::Value Tile(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - TileWorker* flopWorker = new TileWorker(cb, path, type, delay); - flopWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + list duplicated; + Image appended; + list montage; + Image frame; + image.magick(type); + for (int i = 0; i < 5; ++i) { + duplicated.push_back(image); + } + appendImages(&appended, duplicated.begin(), duplicated.end()); + appended.repage(); + for (int i = 0; i < 5; ++i) { + montage.push_back(appended); + } + appendImages(&frame, montage.begin(), montage.end(), true); + frame.repage(); + frame.scale(Geometry("800x800>")); + frame.animationDelay(delay == 0 ? image.animationDelay() : delay); + mid.push_back(frame); + } + + 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; } \ No newline at end of file diff --git a/natives/trump.cc b/natives/trump.cc index ea9a853..59c6694 100644 --- a/natives/trump.cc +++ b/natives/trump.cc @@ -1,72 +1,60 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class TrumpWorker : public Napi::AsyncWorker { - public: - TrumpWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~TrumpWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_path); - watermark.read("./assets/images/trump.png"); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - Image watermark_new = watermark; - image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); - image.backgroundColor("none"); - image.scale(Geometry("365x179!")); - double arguments[16] = {0, 0, 207, 268, 365, 0, 548, 271, 365, 179, 558, 450, 0, 179, 193, 450}; - image.distort(Magick::PerspectiveDistortion, 16, arguments, true); - image.extent(Geometry("800x450"), Magick::CenterGravity); - watermark_new.composite(image, Geometry("-25+134"), Magick::DstOverCompositeOp); - watermark_new.magick(type); - watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); - mid.push_back(watermark_new); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Trump(const Napi::CallbackInfo &info) -{ +Napi::Value Trump(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - TrumpWorker* blurWorker = new TrumpWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; + readImages(&frames, path); + watermark.read("./assets/images/trump.png"); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + Image watermark_new = watermark; + image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod); + image.backgroundColor("none"); + image.scale(Geometry("365x179!")); + double arguments[16] = {0, 0, 207, 268, 365, 0, 548, 271, + 365, 179, 558, 450, 0, 179, 193, 450}; + image.distort(Magick::PerspectiveDistortion, 16, arguments, true); + image.extent(Geometry("800x450"), Magick::CenterGravity); + watermark_new.composite(image, Geometry("-25+134"), + Magick::DstOverCompositeOp); + watermark_new.magick(type); + watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay); + mid.push_back(watermark_new); + } + + 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; } \ No newline at end of file diff --git a/natives/wall.cc b/natives/wall.cc index 7dc6964..2c45d47 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -1,70 +1,57 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class WallWorker : public Napi::AsyncWorker { - public: - WallWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~WallWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - readImages(&frames, in_path); - coalesceImages(&coalesced, frames.begin(), frames.end()); - - for (Image &image : coalesced) { - image.resize(Geometry("128x128")); - image.virtualPixelMethod(Magick::TileVirtualPixelMethod); - image.matteColor("none"); - image.backgroundColor("none"); - image.scale(Geometry("512x512")); - double arguments[16] = {0, 0, 57, 42, 0, 128, 63, 130, 128, 0, 140, 60, 128, 128, 140, 140}; - image.distort(Magick::PerspectiveDistortion, 16, arguments); - image.scale(Geometry("800x800>")); - image.magick(type); - mid.push_back(image); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Wall(const Napi::CallbackInfo &info) -{ +Napi::Value Wall(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - WallWorker* flopWorker = new WallWorker(cb, path, type, delay); - flopWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + readImages(&frames, path); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.resize(Geometry("128x128")); + image.virtualPixelMethod(Magick::TileVirtualPixelMethod); + image.matteColor("none"); + image.backgroundColor("none"); + image.scale(Geometry("512x512")); + double arguments[16] = {0, 0, 57, 42, 0, 128, 63, 130, + 128, 0, 140, 60, 128, 128, 140, 140}; + image.distort(Magick::PerspectiveDistortion, 16, arguments); + image.scale(Geometry("800x800>")); + image.magick(type); + mid.push_back(image); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + if (delay != 0) image.animationDelay(delay); + } + } + + 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; } \ No newline at end of file diff --git a/natives/watermark.cc b/natives/watermark.cc index 42451c4..84ca1d1 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -1,94 +1,83 @@ -#include -#include -#include #include +#include + +#include +#include using namespace std; using namespace Magick; -class WatermarkWorker : public Napi::AsyncWorker { - public: - WatermarkWorker(Napi::Function& callback, string in_path, string water_path, int gravity, string type, int delay, bool resize, bool append, bool mc) - : Napi::AsyncWorker(callback), in_path(in_path), water_path(water_path), gravity(gravity), type(type), delay(delay), resize(resize), append(append), mc(mc) {} - ~WatermarkWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_path); - watermark.read(water_path); - if (resize && append) { - string query(to_string(frames.front().baseColumns()) + "x"); - watermark.scale(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, Magick::GravityType(gravity), Magick::OverCompositeOp); - final = image; - } else { - image.composite(watermark, Magick::GravityType(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); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, water_path, type; - int delay, gravity; - Blob blob; - bool resize, append, mc; -}; - -Napi::Value Watermark(const Napi::CallbackInfo &info) -{ +Napi::Value Watermark(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string water = obj.Get("water").As().Utf8Value(); int gravity = obj.Get("gravity").As().Int32Value(); - bool resize = obj.Has("resize") ? obj.Get("resize").As().Value() : false; - bool append = obj.Has("append") ? obj.Get("append").As().Value() : false; + bool resize = + obj.Has("resize") ? obj.Get("resize").As().Value() : 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; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - WatermarkWorker* watermarkWorker = new WatermarkWorker(cb, path, water, gravity, type, delay, resize, append, mc); - watermarkWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list mid; + Image watermark; + readImages(&frames, path); + watermark.read(water); + if (resize && append) { + string query(to_string(frames.front().baseColumns()) + "x"); + watermark.scale(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, Magick::GravityType(gravity), + Magick::OverCompositeOp); + final = image; + } else { + image.composite(watermark, Magick::GravityType(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; } \ No newline at end of file diff --git a/natives/wdt.cc b/natives/wdt.cc index 6e2a8d1..0bde2fa 100644 --- a/natives/wdt.cc +++ b/natives/wdt.cc @@ -1,67 +1,54 @@ -#include -#include #include +#include + +#include using namespace std; using namespace Magick; -class WdtWorker : public Napi::AsyncWorker { - public: - WdtWorker(Napi::Function& callback, string in_path, string type, int delay) - : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~WdtWorker() {} - - void Execute() { - list frames; - list coalesced; - list mid; - Image watermark; - readImages(&frames, in_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); - } - - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - } - } - - writeImages(mid.begin(), mid.end(), &blob); - } - - void OnOK() { - Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length()), Napi::String::From(Env(), type)}); - } - - private: - string in_path, type; - int delay; - Blob blob; -}; - -Napi::Value Wdt(const Napi::CallbackInfo &info) -{ +Napi::Value Wdt(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); Napi::Object obj = info[0].As(); - Napi::Function cb = info[1].As(); string path = obj.Get("path").As().Utf8Value(); string type = obj.Get("type").As().Utf8Value(); - int delay = obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - WdtWorker* blurWorker = new WdtWorker(cb, path, type, delay); - blurWorker->Queue(); - return env.Undefined(); + Blob blob; + + list frames; + list coalesced; + list 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); + } + + 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; } \ No newline at end of file diff --git a/utils/image-runner.js b/utils/image-runner.js index 04b7941..f45260f 100644 --- a/utils/image-runner.js +++ b/utils/image-runner.js @@ -15,14 +15,16 @@ exports.run = object => { // If no image type is given (say, the command generates its own image), make it a PNG. const fileExtension = object.type ? object.type.split("/")[1] : "png"; const objectWithFixedType = Object.assign({}, object, {type: fileExtension}); - magick[object.cmd](objectWithFixedType, (error, data, type) => { - if (error) reject(error); + try { + const result = magick[object.cmd](objectWithFixedType); const returnObject = { - buffer: data, - fileExtension: type + buffer: result.data, + fileExtension: result.type }; resolve(returnObject); - }); + } catch (e) { + reject(e); + } }); }; diff --git a/utils/image.js b/utils/image.js index b8b71ef..f9002c0 100644 --- a/utils/image.js +++ b/utils/image.js @@ -105,7 +105,7 @@ exports.disconnect = async () => { connection.destroy(); } for (const uuid of Object.keys(jobs)) { - jobs[uuid].emit("error", new Error("Job ended prematurely (not really an error; just run your image job again)")); + jobs[uuid].emit("error", "Job ended prematurely (not really an error; just run your image job again)"); } this.connections = []; return;