Ported more commands to Magick++, cleaned up a bit

This commit is contained in:
TheEssem 2020-07-14 09:53:51 -05:00
parent bdb15aee3f
commit bf14371b52
22 changed files with 217 additions and 99 deletions

View file

@ -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": [ "<!@(node -p \"require('fs').readdirSync('./natives').map(f=>'natives/'+f).join(' ')\")" ],
"cflags!": [ "-fno-exceptions", "<!(pkg-config --cflags Magick++)" ],
"cflags_cc!": [ "-fno-exceptions", "<!(pkg-config --cflags Magick++)" ],
"include_dirs": [

View file

@ -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 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}`

View file

@ -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}`

View file

@ -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}`

View file

@ -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}`

View file

@ -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}`

View file

@ -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}`

View file

@ -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}`

View file

@ -1,8 +0,0 @@
#ifndef ESMBOT_NATIVES_9GAG_H_
#define ESMBOT_NATIVES_9GAG_H_
#include <napi.h>
Napi::Value NineGag(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +0,0 @@
#ifndef ESMBOT_NATIVES_BANDICAM_H_
#define ESMBOT_NATIVES_BANDICAM_H_
#include <napi.h>
Napi::Value Bandicam(const Napi::CallbackInfo& info);
#endif

View file

@ -1,8 +0,0 @@
#ifndef ESMBOT_NATIVES_DEVIANTART_H_
#define ESMBOT_NATIVES_DEVIANTART_H_
#include <napi.h>
Napi::Value Deviantart(const Napi::CallbackInfo& info);
#endif

63
natives/flag.cc Normal file
View file

@ -0,0 +1,63 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
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<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> 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<char>::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<Napi::String>().Utf8Value();
string overlay_path = info[1].As<Napi::String>().Utf8Value();
string type = info[2].As<Napi::String>().Utf8Value();
int delay = info[3].As<Napi::Number>().Int32Value();
Napi::Function cb = info[4].As<Napi::Function>();
FlagWorker* flagWorker = new FlagWorker(cb, in_path, overlay_path, type, delay);
flagWorker->Queue();
return env.Undefined();
}

8
natives/flag.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ESMBOT_NATIVES_FLAG_H_
#define ESMBOT_NATIVES_FLAG_H_
#include <napi.h>
Napi::Value Flag(const Napi::CallbackInfo& info);
#endif

View file

@ -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<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> 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<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
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();
}

8
natives/flip.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ESMBOT_NATIVES_FLIP_H_
#define ESMBOT_NATIVES_FLIP_H_
#include <napi.h>
Napi::Value Flip(const Napi::CallbackInfo& info);
#endif

View file

@ -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<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> 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<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
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();
}

8
natives/flop.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ESMBOT_NATIVES_FLOP_H_
#define ESMBOT_NATIVES_FLOP_H_
#include <napi.h>
Napi::Value Flop(const Napi::CallbackInfo& info);
#endif

View file

@ -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<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> 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<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
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();
}

8
natives/freeze.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ESMBOT_NATIVES_FREEZE_H_
#define ESMBOT_NATIVES_FREEZE_H_
#include <napi.h>
Napi::Value Freeze(const Napi::CallbackInfo& info);
#endif

View file

@ -1,27 +1,31 @@
#include <napi.h>
#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;
}

66
natives/watermark.cc Normal file
View file

@ -0,0 +1,66 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
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<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> 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<char>::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<Napi::String>().Utf8Value();
string water = info[1].As<Napi::String>().Utf8Value();
int gravity = info[2].As<Napi::Number>().Int32Value();
bool resize = info[3].As<Napi::Boolean>().Value();
string type = info[4].As<Napi::String>().Utf8Value();
int delay = info[5].As<Napi::Number>().Int32Value();
Napi::Function cb = info[6].As<Napi::Function>();
WatermarkWorker* watermarkWorker = new WatermarkWorker(cb, in_path, water, gravity, resize, type, delay);
watermarkWorker->Queue();
return env.Undefined();
}

8
natives/watermark.h Normal file
View file

@ -0,0 +1,8 @@
#ifndef ESMBOT_NATIVES_WATERMARK_H_
#define ESMBOT_NATIVES_WATERMARK_H_
#include <napi.h>
Napi::Value Watermark(const Napi::CallbackInfo& info);
#endif