diff --git a/Dockerfile.api b/Dockerfile.api index 650f803..5927ef8 100644 --- a/Dockerfile.api +++ b/Dockerfile.api @@ -46,6 +46,7 @@ WORKDIR /home/esmBot/.internal COPY ./assets/caption.otf /usr/share/fonts/caption.otf COPY ./assets/caption2.ttf /usr/share/fonts/caption2.ttf COPY ./assets/hbc.ttf /usr/share/fonts/hbc.ttf +COPY ./assets/reddit.ttf /usr/share/fonts/reddit.ttf RUN fc-cache -fv COPY --chown=node:node ./package.json package.json diff --git a/Dockerfile.bot b/Dockerfile.bot index b36bb98..3818dff 100644 --- a/Dockerfile.bot +++ b/Dockerfile.bot @@ -46,6 +46,7 @@ WORKDIR /home/esmBot/.internal COPY ./assets/caption.otf /usr/share/fonts/caption.otf COPY ./assets/caption2.ttf /usr/share/fonts/caption2.ttf COPY ./assets/hbc.ttf /usr/share/fonts/hbc.ttf +COPY ./assets/reddit.ttf /usr/share/fonts/reddit.ttf RUN fc-cache -fv COPY --chown=node:node ./package.json package.json diff --git a/assets/images/reddit.png b/assets/images/reddit.png new file mode 100644 index 0000000..f4e1e7a Binary files /dev/null and b/assets/images/reddit.png differ diff --git a/assets/reddit.ttf b/assets/reddit.ttf new file mode 100644 index 0000000..5e84294 Binary files /dev/null and b/assets/reddit.ttf differ diff --git a/commands/image-editing/reddit.js b/commands/image-editing/reddit.js new file mode 100644 index 0000000..8e85752 --- /dev/null +++ b/commands/image-editing/reddit.js @@ -0,0 +1,19 @@ +const ImageCommand = require("../../classes/imageCommand.js"); +const { random } = require("../../utils/misc.js"); +const names = ["esmBot", "me_irl", "dankmemes", "hmmm", "gaming", "wholesome", "chonkers", "memes", "funny", "pcmasterrace", "bellybros"]; + +class RedditCommand extends ImageCommand { + params(args) { + return { + caption: args.length === 0 ? random(names) : args.join(" ").replaceAll("\n", "").replaceAll(" ", "") + }; + } + + static description = "Adds a Reddit watermark to an image"; + static arguments = ["{text}"]; + + static noText = "you need to provide some text to add a Reddit watermark!"; + static command = "reddit"; +} + +module.exports = RedditCommand; \ No newline at end of file diff --git a/natives/blur.cc b/natives/blur.cc index a61ed8b..c1ca441 100644 --- a/natives/blur.cc +++ b/natives/blur.cc @@ -9,43 +9,47 @@ using namespace Magick; Napi::Value Blur(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + Blob blob; - list frames; - list coalesced; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 4d666ec..7711675 100644 --- a/natives/blurple.cc +++ b/natives/blurple.cc @@ -9,42 +9,46 @@ using namespace Magick; Napi::Value Blurple(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list blurpled; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 c234e37..72a5c47 100644 --- a/natives/caption.cc +++ b/natives/caption.cc @@ -9,62 +9,66 @@ using namespace Magick; Napi::Value Caption(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + Blob blob; - list frames; - list coalesced; - list captioned; - readImages(&frames, path); + 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); + 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()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 27acf61..c384adb 100644 --- a/natives/caption2.cc +++ b/natives/caption2.cc @@ -9,60 +9,64 @@ using namespace Magick; Napi::Value CaptionTwo(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + Blob blob; - list frames; - list coalesced; - list captioned; - Blob caption_blob; - readImages(&frames, path); + 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); + 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()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 def9f5d..0f9f1fc 100644 --- a/natives/circle.cc +++ b/natives/circle.cc @@ -9,41 +9,45 @@ using namespace Magick; Napi::Value Circle(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list blurred; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 f348793..1bbfccf 100644 --- a/natives/crop.cc +++ b/natives/crop.cc @@ -9,49 +9,53 @@ using namespace Magick; Napi::Value Crop(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 7d5a83c..ecbc2c3 100644 --- a/natives/explode.cc +++ b/natives/explode.cc @@ -9,42 +9,46 @@ using namespace Magick; Napi::Value Explode(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + Blob blob; - list frames; - list coalesced; - list blurred; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 4c47898..031e204 100644 --- a/natives/flag.cc +++ b/natives/flag.cc @@ -9,50 +9,54 @@ using namespace Magick; Napi::Value Flag(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + 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()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 82a25d4..33a9a93 100644 --- a/natives/flip.cc +++ b/natives/flip.cc @@ -9,43 +9,47 @@ using namespace Magick; Napi::Value Flip(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string path = obj.Get("path").As().Utf8Value(); - 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; + try { + Napi::Object obj = info[0].As(); + string path = obj.Get("path").As().Utf8Value(); + 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; - Blob blob; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 375ec38..5a429d3 100644 --- a/natives/freeze.cc +++ b/natives/freeze.cc @@ -9,37 +9,42 @@ using namespace Magick; Napi::Value Freeze(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string path = obj.Get("path").As().Utf8Value(); - 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; + try { + Napi::Object obj = info[0].As(); + string path = obj.Get("path").As().Utf8Value(); + 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; - Blob blob; + Blob blob; - list frames; - readImages(&frames, path); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - 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 883134a..15ec2fe 100644 --- a/natives/gamexplain.cc +++ b/natives/gamexplain.cc @@ -9,46 +9,50 @@ using namespace Magick; Napi::Value Gamexplain(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; + try { + 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; + 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()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 52fa98b..1912687 100644 --- a/natives/globe.cc +++ b/natives/globe.cc @@ -9,62 +9,67 @@ using namespace Magick; Napi::Value Globe(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; + try { + 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; + 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()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 2986d78..bd63133 100644 --- a/natives/homebrew.cc +++ b/natives/homebrew.cc @@ -9,25 +9,29 @@ using namespace Magick; Napi::Value Homebrew(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string caption = obj.Get("caption").As().Utf8Value(); + try { + Napi::Object obj = info[0].As(); + string caption = obj.Get("caption").As().Utf8Value(); - Blob blob; + 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); + 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; + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); + } } \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index aba1ede..0461b7f 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -21,6 +21,7 @@ #include "mirror.h" #include "swirl.h" #include "motivate.h" +#include "reddit.h" #include "resize.h" #include "retro.h" #include "reverse.h" @@ -56,6 +57,7 @@ Napi::Object Init(Napi::Env env, Napi::Object exports) exports.Set(Napi::String::New(env, "meme"), Napi::Function::New(env, Meme)); exports.Set(Napi::String::New(env, "mirror"), Napi::Function::New(env, Mirror)); exports.Set(Napi::String::New(env, "motivate"), Napi::Function::New(env, Motivate)); + exports.Set(Napi::String::New(env, "reddit"), Napi::Function::New(env, Reddit)); exports.Set(Napi::String::New(env, "resize"), Napi::Function::New(env, Resize)); exports.Set(Napi::String::New(env, "retro"), Napi::Function::New(env, Retro)); exports.Set(Napi::String::New(env, "reverse"), Napi::Function::New(env, Reverse)); diff --git a/natives/invert.cc b/natives/invert.cc index 9941216..0ee546c 100644 --- a/natives/invert.cc +++ b/natives/invert.cc @@ -9,43 +9,47 @@ using namespace Magick; Napi::Value Invert(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 05a906c..9c9114f 100644 --- a/natives/jpeg.cc +++ b/natives/jpeg.cc @@ -9,20 +9,24 @@ using namespace Magick; Napi::Value Jpeg(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string path = obj.Get("path").As().Utf8Value(); + try { + Napi::Object obj = info[0].As(); + string path = obj.Get("path").As().Utf8Value(); - Blob blob; + Blob blob; - Image image; - image.read(path); - image.quality(1); - image.magick("JPEG"); - image.write(&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; + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); + } } \ No newline at end of file diff --git a/natives/leak.cc b/natives/leak.cc index 055fa81..3c65a20 100644 --- a/natives/leak.cc +++ b/natives/leak.cc @@ -9,47 +9,51 @@ using namespace Magick; Napi::Value Leak(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; + try { + 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; + 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()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 a81986d..108f698 100644 --- a/natives/magik.cc +++ b/natives/magik.cc @@ -9,43 +9,47 @@ using namespace Magick; Napi::Value Magik(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list blurred; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 77d61ef..c23b6a3 100644 --- a/natives/meme.cc +++ b/natives/meme.cc @@ -9,79 +9,84 @@ using namespace Magick; Napi::Value Meme(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + 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))); + 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); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 a8c1ede..ac0ae7e 100644 --- a/natives/mirror.cc +++ b/natives/mirror.cc @@ -9,78 +9,82 @@ using namespace Magick; Napi::Value Mirror(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; - string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + try { + Napi::Object obj = info[0].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; + string type = obj.Get("type").As().Utf8Value(); + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - Blob blob; + Blob blob; - list frames; - list coalesced; - list mid; - MagickCore::GravityType gravity; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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(); + if (vertical && first) { + gravity = Magick::NorthGravity; + } else if (!vertical && first) { + gravity = Magick::WestGravity; + } else if (vertical && !first) { + gravity = Magick::SouthGravity; } else { - mirror.flop(); + gravity = Magick::EastGravity; } - if (first) { - mirrored.push_back(mirror); - } else { - mirrored.push_front(mirror); + + 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); } - 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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/motivate.cc b/natives/motivate.cc index f2e3efc..a3bee70 100644 --- a/natives/motivate.cc +++ b/natives/motivate.cc @@ -9,83 +9,87 @@ using namespace Magick; Napi::Value Motivate(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string path = obj.Get("path").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; + try { + Napi::Object obj = info[0].As(); + string path = obj.Get("path").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; - Blob blob; + Blob blob; - list frames; - list coalesced; - list mid; - Image top; - Image bottom; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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/reddit.cc b/natives/reddit.cc new file mode 100644 index 0000000..387eead --- /dev/null +++ b/natives/reddit.cc @@ -0,0 +1,74 @@ +#include +#include + +#include + +using namespace std; +using namespace Magick; + +Napi::Value Reddit(const Napi::CallbackInfo &info) { + Napi::Env env = info.Env(); + try { + Napi::Object obj = info[0].As(); + string path = obj.Get("path").As().Utf8Value(); + string text = obj.Get("caption").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; + Image watermark; + Image text_image; + readImages(&frames, path); + + watermark.read("./assets/images/reddit.png"); + text_image.textGravity(Magick::WestGravity); + text_image.font("Roboto"); + text_image.fontPointsize(47); + text_image.backgroundColor("none"); + text_image.read("pango:" + text + ""); + + watermark.composite(text_image, Geometry("+375+46"), + Magick::OverCompositeOp); + + string query(to_string(frames.front().baseColumns()) + "x"); + watermark.scale(Geometry(query)); + + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + Image final; + list to_append; + to_append.push_back(image); + to_append.push_back(watermark); + appendImages(&final, to_append.begin(), to_append.end(), true); + final.repage(); + image.magick(type); + final.animationDelay(delay == 0 ? image.animationDelay() : delay); + mid.push_back(final); + } + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } + } + + writeImages(mid.begin(), mid.end(), &blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), + blob.length())); + result.Set("type", type); + return result; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); + } +} \ No newline at end of file diff --git a/natives/reddit.h b/natives/reddit.h new file mode 100644 index 0000000..af9d1e5 --- /dev/null +++ b/natives/reddit.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_REDDIT_H_ +#define ESMBOT_NATIVES_REDDIT_H_ + +#include + +Napi::Value Reddit(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/resize.cc b/natives/resize.cc index d3ecc4b..344b447 100644 --- a/natives/resize.cc +++ b/natives/resize.cc @@ -9,54 +9,58 @@ using namespace Magick; Napi::Value Resize(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; - string type = obj.Get("type").As().Utf8Value(); - int delay = - obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; + try { + Napi::Object obj = info[0].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; + string type = obj.Get("type").As().Utf8Value(); + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - Blob blob; + Blob blob; - list frames; - list coalesced; - list blurred; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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%")); + 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); } - image.magick(type); - blurred.push_back(image); - } - optimizeTransparency(blurred.begin(), blurred.end()); + optimizeTransparency(blurred.begin(), blurred.end()); - if (type == "gif") { - for (Image &image : blurred) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); - if (delay != 0) image.animationDelay(delay); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 737bd97..69aea26 100644 --- a/natives/retro.cc +++ b/natives/retro.cc @@ -9,76 +9,80 @@ using namespace Magick; Napi::Value Retro(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string line1 = obj.Get("line1").As().Utf8Value(); - string line2 = obj.Get("line2").As().Utf8Value(); - string line3 = obj.Get("line3").As().Utf8Value(); + try { + Napi::Object obj = info[0].As(); + string line1 = obj.Get("line1").As().Utf8Value(); + string line2 = obj.Get("line2").As().Utf8Value(); + string line3 = obj.Get("line3").As().Utf8Value(); - Blob blob; + Blob blob; - Image image; - Image line1_text; - Image line2_text; - Image line3_text; + Image image; + Image line1_text; + Image line2_text; + Image line3_text; - image.read("./assets/images/retro.png"); + 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, + 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(line1_text, Geometry("+0-250"), Magick::OverCompositeOp); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 f569ae7..1ace38b 100644 --- a/natives/reverse.cc +++ b/natives/reverse.cc @@ -9,43 +9,47 @@ using namespace Magick; Napi::Value Reverse(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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; + try { + Napi::Object obj = info[0].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; - Blob blob; + Blob blob; - list frames; - list coalesced; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 cca5f37..978a7d5 100644 --- a/natives/scott.cc +++ b/natives/scott.cc @@ -9,52 +9,56 @@ using namespace Magick; Napi::Value Scott(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; + try { + 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; + 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()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 4253786..c867ebe 100644 --- a/natives/sonic.cc +++ b/natives/sonic.cc @@ -9,28 +9,32 @@ using namespace Magick; Napi::Value Sonic(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string text = obj.Get("text").As().Utf8Value(); + try { + Napi::Object obj = info[0].As(); + string text = obj.Get("text").As().Utf8Value(); - Blob blob; + 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); + 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; + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); + } } \ No newline at end of file diff --git a/natives/speed.cc b/natives/speed.cc index b0a28f3..8581e05 100644 --- a/natives/speed.cc +++ b/natives/speed.cc @@ -9,70 +9,76 @@ using namespace Magick; Napi::Value Speed(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].As(); - string path = obj.Get("path").As().Utf8Value(); - 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; + try { + Napi::Object obj = info[0].As(); + string path = obj.Get("path").As().Utf8Value(); + 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; - Blob blob; + Blob blob; - list frames; - readImages(&frames, path); + 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]); + // 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 (int i = 0; i < speed - 1; ++i) { - frames.remove_if( - [counter = 0](const auto x) mutable { return ++counter % 2 == 0; }); + 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); } - } - } 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; }); + + 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 { - for_each(frames.begin(), frames.end(), animationDelayImage(new_delay)); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 b356536..caf6854 100644 --- a/natives/spin.cc +++ b/natives/spin.cc @@ -9,59 +9,63 @@ using namespace Magick; Napi::Value Spin(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 index edbf559..e575813 100644 --- a/natives/swirl.cc +++ b/natives/swirl.cc @@ -9,41 +9,45 @@ 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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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/tile.cc b/natives/tile.cc index 63a839b..24c03ec 100644 --- a/natives/tile.cc +++ b/natives/tile.cc @@ -9,55 +9,59 @@ using namespace Magick; Napi::Value Tile(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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); } - appendImages(&appended, duplicated.begin(), duplicated.end()); - appended.repage(); - for (int i = 0; i < 5; ++i) { - montage.push_back(appended); + + optimizeTransparency(mid.begin(), mid.end()); + + if (type == "gif") { + for (Image &image : mid) { + image.quantizeDitherMethod(FloydSteinbergDitherMethod); + image.quantize(); + } } - appendImages(&frame, montage.begin(), montage.end(), true); - frame.repage(); - frame.scale(Geometry("800x800>")); - frame.animationDelay(delay == 0 ? image.animationDelay() : delay); - mid.push_back(frame); + + writeImages(mid.begin(), mid.end(), &blob); + + Napi::Object result = Napi::Object::New(env); + result.Set("data", Napi::Buffer::Copy(env, (char *)blob.data(), + blob.length())); + result.Set("type", type); + return result; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 59c6694..73febe6 100644 --- a/natives/trump.cc +++ b/natives/trump.cc @@ -9,52 +9,56 @@ using namespace Magick; Napi::Value Trump(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; + try { + 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; + 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()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 2c45d47..66bb13a 100644 --- a/natives/wall.cc +++ b/natives/wall.cc @@ -9,49 +9,53 @@ using namespace Magick; Napi::Value Wall(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; + try { + 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; + Blob blob; - list frames; - list coalesced; - list mid; - readImages(&frames, path); - coalesceImages(&coalesced, frames.begin(), frames.end()); + 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); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 84ca1d1..d86ad2a 100644 --- a/natives/watermark.cc +++ b/natives/watermark.cc @@ -10,74 +10,80 @@ using namespace Magick; Napi::Value Watermark(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); - Napi::Object obj = info[0].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 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; + try { + Napi::Object obj = info[0].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 mc = obj.Has("mc") ? obj.Get("mc").As().Value() : false; + string type = obj.Get("type").As().Utf8Value(); + int delay = + obj.Has("delay") ? obj.Get("delay").As().Int32Value() : 0; - Blob blob; + 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; + 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)); } - image.magick(type); - final.animationDelay(delay == 0 ? image.animationDelay() : delay); - mid.push_back(final); - } + coalesceImages(&coalesced, frames.begin(), frames.end()); - optimizeTransparency(mid.begin(), mid.end()); - - if (type == "gif") { - for (Image &image : mid) { - image.quantizeDitherMethod(FloydSteinbergDitherMethod); - image.quantize(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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 0bde2fa..7474537 100644 --- a/natives/wdt.cc +++ b/natives/wdt.cc @@ -9,46 +9,50 @@ using namespace Magick; Napi::Value Wdt(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; + try { + 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; + 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()); + 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(); + 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; + } catch (std::exception const &err) { + throw Napi::Error::New(env, err.what()); } - - 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