Native module migration wave 1, fixed issue with avatar

This commit is contained in:
TheEssem 2020-07-12 10:14:39 -05:00
parent a097312620
commit bdb15aee3f
28 changed files with 609 additions and 38 deletions

31
binding.gyp Normal file
View file

@ -0,0 +1,31 @@
{
"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"
],
"cflags!": [ "-fno-exceptions", "<!(pkg-config --cflags Magick++)" ],
"cflags_cc!": [ "-fno-exceptions", "<!(pkg-config --cflags Magick++)" ],
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")",
"/usr/include/ImageMagick-7",
"/usr/include/vips",
"/usr/include/glib-2.0",
"/usr/lib/glib-2.0/include"
],
"libraries": [
"<!(pkg-config --libs Magick++)",
],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
}
]
}

View file

@ -1,13 +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 add a 9GAG watermark!`;
const watermark = "./assets/images/9gag.png";
const buffer = await gm(image.path).coalesce().out("null:").out(watermark).gravity("East").out("-layers", "composite").bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.nineGag)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `9gag.${image.type}`

View file

@ -10,9 +10,9 @@ exports.run = async (message, args) => {
const member = message.channel.guild.members.find(element => {
return userRegex.test(element.nick) ? userRegex.test(element.nick) : userRegex.test(element.username);
});
return member ? member.dynamicAvatarURL(null, 1024) : message.author.dynamicAvatarURL(null, 1024);
return member ? member.user.dynamicAvatarURL(null, 1024) : message.author.user.dynamicAvatarURL(null, 1024);
} else {
return message.author.dynamicAvatarURL(null, 1024);
return message.author.user.dynamicAvatarURL(null, 1024);
}
};

View file

@ -1,13 +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 add a Bandicam watermark!`;
const watermark = "./assets/images/bandicam.png";
const buffer = await gm(image.path).coalesce().out("null:").out(watermark).gravity("North").scale(null, "%[fx:u.h]").out("-layers", "composite").bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.bandicam)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `bandicam.${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 blur!`;
const buffer = await gm(image.path).coalesce().blur(15).bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.blur)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `blur.${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 make blurple!`;
const buffer = await gm(image.path).coalesce().threshold(75, true).out("+level-colors").out("\"#7289DA\",white").bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.blurple)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `blurple.${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 add radial blur!`;
const buffer = await gm(image.path).coalesce().out("-rotational-blur", 10).bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.circle)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `circle.${image.type}`

View file

@ -1,13 +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 add a DeviantArt watermark!`;
const watermark = "./assets/images/deviantart.png";
const buffer = await gm(image.path).coalesce().out("null:").out(watermark).gravity("Center").scale(null, "%[fx:u.h]").out("-layers", "composite").bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.deviantart)(image.path, 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,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 explode!`;
const buffer = await gm(image.path).coalesce().implode([-2]).bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.explode)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `explode.${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 invert!`;
const buffer = await gm(image.path).coalesce().negative().bufferPromise(image.type, image.delay);
const buffer = await promisify(magick.invert)(image.path, image.type.toUpperCase(), image.delay ? (100 / image.delay.split("/")[0]) * image.delay.split("/")[1] : 0);
return {
file: buffer,
name: `invert.${image.type}`

View file

@ -5,7 +5,7 @@ exports.run = async (message) => {
const image = await require("../utils/imagedetect.js")(message);
if (image === undefined) return `${message.author.mention}, you need to provide an image with a QR code to read!`;
message.channel.sendTyping();
const rawData = await sharp(image.data).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const rawData = await sharp(image.path).ensureAlpha().raw().toBuffer({ resolveWithObject: true });
const qrBuffer = jsqr(rawData.data, rawData.info.width, rawData.info.height);
if (!qrBuffer) return `${message.author.mention}, I couldn't find a QR code!`;
return `\`\`\`\n${qrBuffer.data}\n\`\`\``;

58
natives/9gag.cc Normal file
View file

@ -0,0 +1,58 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class NineGagWorker : public Napi::AsyncWorker {
public:
NineGagWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~NineGagWorker() {}
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.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value NineGag(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
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();
return env.Undefined();
}

8
natives/9gag.h Normal file
View file

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

60
natives/bandicam.cc Normal file
View file

@ -0,0 +1,60 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class BandicamWorker : public Napi::AsyncWorker {
public:
BandicamWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~BandicamWorker() {}
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.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Bandicam(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
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();
return env.Undefined();
}

8
natives/bandicam.h Normal file
View file

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

56
natives/blur.cc Normal file
View file

@ -0,0 +1,56 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class BlurWorker : public Napi::AsyncWorker {
public:
BlurWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~BlurWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> blurred;
list<Image> result;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.blur(15);
image.magick(type);
blurred.push_back(image);
}
optimizeImageLayers(&result, blurred.begin(), blurred.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Blur(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
BlurWorker* blurWorker = new BlurWorker(cb, in_path, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/blur.h Normal file
View file

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

57
natives/blurple.cc Normal file
View file

@ -0,0 +1,57 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class BlurpleWorker : public Napi::AsyncWorker {
public:
BlurpleWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~BlurpleWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> blurpled;
list<Image> result;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.threshold(49151.25);
image.levelColors("#7289DA", "white");
image.magick(type);
blurpled.push_back(image);
}
optimizeImageLayers(&result, blurpled.begin(), blurpled.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Blurple(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
BlurpleWorker* blurpleWorker = new BlurpleWorker(cb, in_path, type, delay);
blurpleWorker->Queue();
return env.Undefined();
}

8
natives/blurple.h Normal file
View file

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

56
natives/circle.cc Normal file
View file

@ -0,0 +1,56 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class CircleWorker : public Napi::AsyncWorker {
public:
CircleWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~CircleWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> blurred;
list<Image> result;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.rotationalBlur(10);
image.magick(type);
blurred.push_back(image);
}
optimizeImageLayers(&result, blurred.begin(), blurred.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Circle(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
CircleWorker* circleWorker = new CircleWorker(cb, in_path, type, delay);
circleWorker->Queue();
return env.Undefined();
}

8
natives/circle.h Normal file
View file

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

60
natives/deviantart.cc Normal file
View file

@ -0,0 +1,60 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class DeviantartWorker : public Napi::AsyncWorker {
public:
DeviantartWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~DeviantartWorker() {}
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);
}
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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Deviantart(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
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();
return env.Undefined();
}

8
natives/deviantart.h Normal file
View file

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

56
natives/explode.cc Normal file
View file

@ -0,0 +1,56 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class ExplodeWorker : public Napi::AsyncWorker {
public:
ExplodeWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~ExplodeWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> blurred;
list<Image> result;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.implode(-1);
image.magick(type);
blurred.push_back(image);
}
optimizeImageLayers(&result, blurred.begin(), blurred.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Explode(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
ExplodeWorker* explodeWorker = new ExplodeWorker(cb, in_path, type, delay);
explodeWorker->Queue();
return env.Undefined();
}

8
natives/explode.h Normal file
View file

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

28
natives/image.cc Normal file
View file

@ -0,0 +1,28 @@
#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 "invert.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, "invert"), Napi::Function::New(env, Invert));
return exports;
}
NODE_API_MODULE(addon, Init);

56
natives/invert.cc Normal file
View file

@ -0,0 +1,56 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class InvertWorker : public Napi::AsyncWorker {
public:
InvertWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~InvertWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> inverted;
list<Image> result;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.negateChannel(Magick::ChannelType(Magick::CompositeChannels ^ Magick::AlphaChannel));
image.magick(type);
inverted.push_back(image);
}
optimizeImageLayers(&result, inverted.begin(), inverted.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, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Invert(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string type = info[1].As<Napi::String>().Utf8Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
InvertWorker* invertWorker = new InvertWorker(cb, in_path, type, delay);
invertWorker->Queue();
return env.Undefined();
}

8
natives/invert.h Normal file
View file

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