diff --git a/binding.gyp b/binding.gyp index 8a13752..634f514 100644 --- a/binding.gyp +++ b/binding.gyp @@ -2,17 +2,7 @@ "targets": [ { "target_name": "image", - "sources": [ - "./natives/9gag.cc", - "./natives/bandicam.cc", - "./natives/blur.cc", - "./natives/blurple.cc", - "./natives/circle.cc", - "./natives/deviantart.cc", - "./natives/explode.cc", - "./natives/image.cc", - "./natives/invert.cc" - ], + "sources": [ "'natives/'+f).join(' ')\")" ], "cflags!": [ "-fno-exceptions", " { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a 9GAG watermark!`; - const buffer = await promisify(magick.nineGag)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + const buffer = await promisify(magick.watermark)(image.path, "./assets/images/9gag.png", 6, false, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `9gag.${image.type}` diff --git a/commands/bandicam.js b/commands/bandicam.js index a890a92..ceb3586 100644 --- a/commands/bandicam.js +++ b/commands/bandicam.js @@ -5,7 +5,7 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a Bandicam watermark!`; - const buffer = await promisify(magick.bandicam)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + const buffer = await promisify(magick.watermark)(image.path, "./assets/images/bandicam.png", 2, true, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `bandicam.${image.type}` diff --git a/commands/deviantart.js b/commands/deviantart.js index 0b1fe3f..88c84d5 100644 --- a/commands/deviantart.js +++ b/commands/deviantart.js @@ -5,7 +5,7 @@ exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to add a DeviantArt watermark!`; - const buffer = await promisify(magick.deviantart)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); + const buffer = await promisify(magick.watermark)(image.path, "./assets/images/deviantart.png", 5, true, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `deviantart.${image.type}` diff --git a/commands/flag.js b/commands/flag.js index baad461..32786d9 100644 --- a/commands/flag.js +++ b/commands/flag.js @@ -1,6 +1,5 @@ -const gm = require("gm").subClass({ - imageMagick: true -}); +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); const emojiRegex = require("emoji-regex"); const emoji = require("node-emoji"); @@ -12,7 +11,7 @@ exports.run = async (message, args) => { const flag = emoji.unemojify(args[0]).replace(/:/g, "").replace("flag-", ""); let path = `./assets/images/region-flags/png/${flag.toUpperCase()}.png`; if (flag === "🏴‍☠️") path = "./assets/images/pirateflag.png"; - const buffer = await gm(image.path).coalesce().out("null:").out("(", path, "-alpha", "set", "-channel", "A", "-evaluate", "multiply", "0.5", "+channel", ")").gravity("North").scale("%[fx:u.w]x%[fx:u.h]!").out("-layers", "composite").bufferPromise(image.type, image.delay); + const buffer = await promisify(magick.flag)(image.path, path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `flag.${image.type}` diff --git a/commands/flip.js b/commands/flip.js index 250283c..cd0f0a0 100644 --- a/commands/flip.js +++ b/commands/flip.js @@ -1,12 +1,11 @@ -const gm = require("gm").subClass({ - imageMagick: true -}); +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to flip!`; - const buffer = await gm(image.path).flip().bufferPromise(image.type, image.delay); + const buffer = await promisify(magick.flip)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `flip.${image.type}` diff --git a/commands/flop.js b/commands/flop.js index e82686e..bb6c0dd 100644 --- a/commands/flop.js +++ b/commands/flop.js @@ -1,12 +1,11 @@ -const gm = require("gm").subClass({ - imageMagick: true -}); +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide an image to flop!`; - const buffer = await gm(image.path).flop().bufferPromise(image.type, image.delay); + const buffer = await promisify(magick.flop)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `flop.${image.type}` diff --git a/commands/freeze.js b/commands/freeze.js index 6b2358b..e3f5974 100644 --- a/commands/freeze.js +++ b/commands/freeze.js @@ -1,13 +1,12 @@ -const gm = require("gm").subClass({ - imageMagick: true -}); +const magick = require("../build/Release/image.node"); +const { promisify } = require("util"); exports.run = async (message) => { message.channel.sendTyping(); const image = await require("../utils/imagedetect.js")(message); if (image === undefined) return `${message.author.mention}, you need to provide a GIF to freeze!`; if (image.type !== "gif") return `${message.author.mention}, that isn't a GIF!`; - const buffer = await gm(image.path).loop(1).bufferPromise(image.type, image.delay); + const buffer = await promisify(magick.freeze)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0); return { file: buffer, name: `freeze.${image.type}` diff --git a/natives/9gag.h b/natives/9gag.h deleted file mode 100644 index 6a57337..0000000 --- a/natives/9gag.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef ESMBOT_NATIVES_9GAG_H_ -#define ESMBOT_NATIVES_9GAG_H_ - -#include - -Napi::Value NineGag(const Napi::CallbackInfo& info); - -#endif \ No newline at end of file diff --git a/natives/bandicam.h b/natives/bandicam.h deleted file mode 100644 index 95e777e..0000000 --- a/natives/bandicam.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef ESMBOT_NATIVES_BANDICAM_H_ -#define ESMBOT_NATIVES_BANDICAM_H_ - -#include - -Napi::Value Bandicam(const Napi::CallbackInfo& info); - -#endif \ No newline at end of file diff --git a/natives/deviantart.h b/natives/deviantart.h deleted file mode 100644 index e7bd6e4..0000000 --- a/natives/deviantart.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef ESMBOT_NATIVES_DEVIANTART_H_ -#define ESMBOT_NATIVES_DEVIANTART_H_ - -#include - -Napi::Value Deviantart(const Napi::CallbackInfo& info); - -#endif \ No newline at end of file diff --git a/natives/flag.cc b/natives/flag.cc new file mode 100644 index 0000000..18c439a --- /dev/null +++ b/natives/flag.cc @@ -0,0 +1,63 @@ +#include +#include +#include + +using namespace std; +using namespace Magick; + +class FlagWorker : public Napi::AsyncWorker { + public: + FlagWorker(Napi::Function& callback, string in_path, string overlay_path, string type, int delay) + : Napi::AsyncWorker(callback), in_path(in_path), overlay_path(overlay_path), type(type), delay(delay) {} + ~FlagWorker() {} + + void Execute() { + list frames; + list coalesced; + list mid; + list result; + Image watermark; + readImages(&frames, in_path); + watermark.read(overlay_path); + watermark.alphaChannel(Magick::SetAlphaChannel); + watermark.evaluate(Magick::AlphaChannel, Magick::MultiplyEvaluateOperator, 0.5); + string query(to_string(frames.front().baseColumns()) + "x" + to_string(frames.front().baseRows()) + "!"); + watermark.scale(Geometry(query)); + coalesceImages(&coalesced, frames.begin(), frames.end()); + + for (Image &image : coalesced) { + image.composite(watermark, Magick::NorthGravity, Magick::OverCompositeOp); + image.magick(type); + mid.push_back(image); + } + + optimizeImageLayers(&result, mid.begin(), mid.end()); + if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); + writeImages(result.begin(), result.end(), &blob); + } + + void OnOK() { + Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length())}); + } + + private: + string in_path, overlay_path, type; + int delay, wordlength, i, n; + size_t bytes, type_size; + Blob blob; +}; + +Napi::Value Flag(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + + string in_path = info[0].As().Utf8Value(); + string overlay_path = info[1].As().Utf8Value(); + string type = info[2].As().Utf8Value(); + int delay = info[3].As().Int32Value(); + Napi::Function cb = info[4].As(); + + FlagWorker* flagWorker = new FlagWorker(cb, in_path, overlay_path, type, delay); + flagWorker->Queue(); + return env.Undefined(); +} \ No newline at end of file diff --git a/natives/flag.h b/natives/flag.h new file mode 100644 index 0000000..55d6e5e --- /dev/null +++ b/natives/flag.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_FLAG_H_ +#define ESMBOT_NATIVES_FLAG_H_ + +#include + +Napi::Value Flag(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/9gag.cc b/natives/flip.cc similarity index 72% rename from natives/9gag.cc rename to natives/flip.cc index 94d14f3..c39afd1 100644 --- a/natives/9gag.cc +++ b/natives/flip.cc @@ -5,24 +5,22 @@ using namespace std; using namespace Magick; -class NineGagWorker : public Napi::AsyncWorker { +class FlipWorker : public Napi::AsyncWorker { public: - NineGagWorker(Napi::Function& callback, string in_path, string type, int delay) + FlipWorker(Napi::Function& callback, string in_path, string type, int delay) : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~NineGagWorker() {} + ~FlipWorker() {} void Execute() { list frames; list coalesced; list mid; list result; - Image watermark; readImages(&frames, in_path); - watermark.read("./assets/images/9gag.png"); coalesceImages(&coalesced, frames.begin(), frames.end()); for (Image &image : coalesced) { - image.composite(watermark, Magick::EastGravity, Magick::OverCompositeOp); + image.flip(); image.magick(type); mid.push_back(image); } @@ -43,7 +41,7 @@ class NineGagWorker : public Napi::AsyncWorker { Blob blob; }; -Napi::Value NineGag(const Napi::CallbackInfo &info) +Napi::Value Flip(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); @@ -52,7 +50,7 @@ Napi::Value NineGag(const Napi::CallbackInfo &info) int delay = info[2].As().Int32Value(); Napi::Function cb = info[3].As(); - NineGagWorker* ninegagWorker = new NineGagWorker(cb, in_path, type, delay); - ninegagWorker->Queue(); + FlipWorker* flipWorker = new FlipWorker(cb, in_path, type, delay); + flipWorker->Queue(); return env.Undefined(); } \ No newline at end of file diff --git a/natives/flip.h b/natives/flip.h new file mode 100644 index 0000000..47c22b1 --- /dev/null +++ b/natives/flip.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_FLIP_H_ +#define ESMBOT_NATIVES_FLIP_H_ + +#include + +Napi::Value Flip(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/bandicam.cc b/natives/flop.cc similarity index 68% rename from natives/bandicam.cc rename to natives/flop.cc index 9f0d7e7..1aad4b2 100644 --- a/natives/bandicam.cc +++ b/natives/flop.cc @@ -5,26 +5,22 @@ using namespace std; using namespace Magick; -class BandicamWorker : public Napi::AsyncWorker { +class FlopWorker : public Napi::AsyncWorker { public: - BandicamWorker(Napi::Function& callback, string in_path, string type, int delay) + FlopWorker(Napi::Function& callback, string in_path, string type, int delay) : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~BandicamWorker() {} + ~FlopWorker() {} void Execute() { list frames; list coalesced; list mid; list result; - Image watermark; readImages(&frames, in_path); - watermark.read("./assets/images/bandicam.png"); - string query("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.flop(); image.magick(type); mid.push_back(image); } @@ -45,7 +41,7 @@ class BandicamWorker : public Napi::AsyncWorker { Blob blob; }; -Napi::Value Bandicam(const Napi::CallbackInfo &info) +Napi::Value Flop(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); @@ -54,7 +50,7 @@ Napi::Value Bandicam(const Napi::CallbackInfo &info) int delay = info[2].As().Int32Value(); Napi::Function cb = info[3].As(); - BandicamWorker* bandicamWorker = new BandicamWorker(cb, in_path, type, delay); - bandicamWorker->Queue(); + FlopWorker* flopWorker = new FlopWorker(cb, in_path, type, delay); + flopWorker->Queue(); return env.Undefined(); } \ No newline at end of file diff --git a/natives/flop.h b/natives/flop.h new file mode 100644 index 0000000..db82779 --- /dev/null +++ b/natives/flop.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_FLOP_H_ +#define ESMBOT_NATIVES_FLOP_H_ + +#include + +Napi::Value Flop(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/deviantart.cc b/natives/freeze.cc similarity index 53% rename from natives/deviantart.cc rename to natives/freeze.cc index 3e87f5a..d0e8b83 100644 --- a/natives/deviantart.cc +++ b/natives/freeze.cc @@ -5,31 +5,20 @@ using namespace std; using namespace Magick; -class DeviantartWorker : public Napi::AsyncWorker { +class FreezeWorker : public Napi::AsyncWorker { public: - DeviantartWorker(Napi::Function& callback, string in_path, string type, int delay) + FreezeWorker(Napi::Function& callback, string in_path, string type, int delay) : Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {} - ~DeviantartWorker() {} + ~FreezeWorker() {} void Execute() { list frames; - list coalesced; - list mid; list result; - Image watermark; readImages(&frames, in_path); - watermark.read("./assets/images/deviantart.png"); - string query("x" + to_string(frames.front().baseRows())); - watermark.scale(Geometry(query)); - coalesceImages(&coalesced, frames.begin(), frames.end()); - for (Image &image : coalesced) { - image.composite(watermark, Magick::CenterGravity, Magick::OverCompositeOp); - image.magick(type); - mid.push_back(image); - } + for_each(frames.begin(), frames.end(), animationIterationsImage(1)); - optimizeImageLayers(&result, mid.begin(), mid.end()); + optimizeImageLayers(&result, frames.begin(), frames.end()); if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); writeImages(result.begin(), result.end(), &blob); } @@ -45,7 +34,7 @@ class DeviantartWorker : public Napi::AsyncWorker { Blob blob; }; -Napi::Value Deviantart(const Napi::CallbackInfo &info) +Napi::Value Freeze(const Napi::CallbackInfo &info) { Napi::Env env = info.Env(); @@ -54,7 +43,7 @@ Napi::Value Deviantart(const Napi::CallbackInfo &info) int delay = info[2].As().Int32Value(); Napi::Function cb = info[3].As(); - DeviantartWorker* deviantartWorker = new DeviantartWorker(cb, in_path, type, delay); - deviantartWorker->Queue(); + FreezeWorker* blurWorker = new FreezeWorker(cb, in_path, type, delay); + blurWorker->Queue(); return env.Undefined(); } \ No newline at end of file diff --git a/natives/freeze.h b/natives/freeze.h new file mode 100644 index 0000000..2e3f331 --- /dev/null +++ b/natives/freeze.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_FREEZE_H_ +#define ESMBOT_NATIVES_FREEZE_H_ + +#include + +Napi::Value Freeze(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index 8918b6b..b17080d 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -1,27 +1,31 @@ #include -#include "9gag.h" -#include "bandicam.h" #include "blur.h" #include "blurple.h" //#include "caption.h" //#include "caption2.h" #include "circle.h" -#include "deviantart.h" #include "explode.h" +#include "flag.h" +#include "flip.h" +#include "flop.h" +#include "freeze.h" #include "invert.h" +#include "watermark.h" Napi::Object Init(Napi::Env env, Napi::Object exports) { - exports.Set(Napi::String::New(env, "nineGag"), Napi::Function::New(env, NineGag)); - exports.Set(Napi::String::New(env, "bandicam"), Napi::Function::New(env, Bandicam)); exports.Set(Napi::String::New(env, "blur"), Napi::Function::New(env, Blur)); exports.Set(Napi::String::New(env, "blurple"), Napi::Function::New(env, Blurple)); //exports.Set(Napi::String::New(env, "caption"), Napi::Function::New(env, Caption)); //exports.Set(Napi::String::New(env, "captionTwo"), Napi::Function::New(env, CaptionTwo)); exports.Set(Napi::String::New(env, "circle"), Napi::Function::New(env, Circle)); - exports.Set(Napi::String::New(env, "deviantart"), Napi::Function::New(env, Deviantart)); exports.Set(Napi::String::New(env, "explode"), Napi::Function::New(env, Explode)); + exports.Set(Napi::String::New(env, "flag"), Napi::Function::New(env, Flag)); + exports.Set(Napi::String::New(env, "flip"), Napi::Function::New(env, Flip)); + exports.Set(Napi::String::New(env, "flop"), Napi::Function::New(env, Flop)); + exports.Set(Napi::String::New(env, "freeze"), Napi::Function::New(env, Freeze)); exports.Set(Napi::String::New(env, "invert"), Napi::Function::New(env, Invert)); + exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark)); return exports; } diff --git a/natives/watermark.cc b/natives/watermark.cc new file mode 100644 index 0000000..8e07502 --- /dev/null +++ b/natives/watermark.cc @@ -0,0 +1,66 @@ +#include +#include +#include + +using namespace std; +using namespace Magick; + +class WatermarkWorker : public Napi::AsyncWorker { + public: + WatermarkWorker(Napi::Function& callback, string in_path, string water_path, int gravity, bool resize, string type, int delay) + : Napi::AsyncWorker(callback), in_path(in_path), water_path(water_path), gravity(gravity), resize(resize), type(type), delay(delay) {} + ~WatermarkWorker() {} + + void Execute() { + list frames; + list coalesced; + list mid; + list result; + Image watermark; + readImages(&frames, in_path); + watermark.read(water_path); + 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.composite(watermark, Magick::GravityType(gravity), Magick::OverCompositeOp); + image.magick(type); + mid.push_back(image); + } + + optimizeImageLayers(&result, mid.begin(), mid.end()); + if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay)); + writeImages(result.begin(), result.end(), &blob); + } + + void OnOK() { + Callback().Call({Env().Undefined(), Napi::Buffer::Copy(Env(), (char *)blob.data(), blob.length())}); + } + + private: + string in_path, water_path, type; + int delay, wordlength, i, n, gravity; + size_t bytes, type_size; + Blob blob; + bool resize; +}; + +Napi::Value Watermark(const Napi::CallbackInfo &info) +{ + Napi::Env env = info.Env(); + + string in_path = info[0].As().Utf8Value(); + string water = info[1].As().Utf8Value(); + int gravity = info[2].As().Int32Value(); + bool resize = info[3].As().Value(); + string type = info[4].As().Utf8Value(); + int delay = info[5].As().Int32Value(); + Napi::Function cb = info[6].As(); + + WatermarkWorker* watermarkWorker = new WatermarkWorker(cb, in_path, water, gravity, resize, type, delay); + watermarkWorker->Queue(); + return env.Undefined(); +} \ No newline at end of file diff --git a/natives/watermark.h b/natives/watermark.h new file mode 100644 index 0000000..d43ecd3 --- /dev/null +++ b/natives/watermark.h @@ -0,0 +1,8 @@ +#ifndef ESMBOT_NATIVES_WATERMARK_H_ +#define ESMBOT_NATIVES_WATERMARK_H_ + +#include + +Napi::Value Watermark(const Napi::CallbackInfo& info); + +#endif \ No newline at end of file