Ported even more commands to Magick++

This commit is contained in:
TheEssem 2020-07-22 13:12:38 -05:00
parent 950ce00a80
commit 78dbeabd39
24 changed files with 468 additions and 65 deletions

View file

@ -7,8 +7,8 @@ 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(Napi::Function& callback, string in_path, bool sharp, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), sharp(sharp), type(type), delay(delay) {}
~BlurWorker() {}
void Execute() {
@ -20,7 +20,11 @@ class BlurWorker : public Napi::AsyncWorker {
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.blur(15);
if (sharp) {
image.sharpen(10, 3);
} else {
image.blur(15);
}
image.magick(type);
blurred.push_back(image);
}
@ -39,6 +43,7 @@ class BlurWorker : public Napi::AsyncWorker {
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
bool sharp;
};
Napi::Value Blur(const Napi::CallbackInfo &info)
@ -46,11 +51,12 @@ 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>();
bool sharp = info[1].As<Napi::Boolean>().Value();
string type = info[2].As<Napi::String>().Utf8Value();
int delay = info[3].As<Napi::Number>().Int32Value();
Napi::Function cb = info[4].As<Napi::Function>();
BlurWorker* blurWorker = new BlurWorker(cb, in_path, type, delay);
BlurWorker* blurWorker = new BlurWorker(cb, in_path, sharp, type, delay);
blurWorker->Queue();
return env.Undefined();
}

View file

@ -18,6 +18,12 @@
#include "magik.h"
#include "meme.h"
#include "mirror.h"
#include "motivate.h"
#include "resize.h"
#include "reverse.h"
#include "scott.h"
#include "speed.h"
#include "sonic.h"
#include "watermark.h"
Napi::Object Init(Napi::Env env, Napi::Object exports)
@ -41,6 +47,12 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "magik"), Napi::Function::New(env, Magik));
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, "resize"), Napi::Function::New(env, Resize));
exports.Set(Napi::String::New(env, "reverse"), Napi::Function::New(env, Reverse));
exports.Set(Napi::String::New(env, "scott"), Napi::Function::New(env, Scott));
exports.Set(Napi::String::New(env, "speed"), Napi::Function::New(env, Speed));
exports.Set(Napi::String::New(env, "sonic"), Napi::Function::New(env, Sonic));
exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark));
return exports;
}

92
natives/motivate.cc Normal file
View file

@ -0,0 +1,92 @@
#include <napi.h>
#include <list>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class MotivateWorker : public Napi::AsyncWorker {
public:
MotivateWorker(Napi::Function& callback, string in_path, string top_text, string bottom_text, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), top_text(top_text), bottom_text(bottom_text), type(type), delay(delay) {}
~MotivateWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> result;
Image top;
Image bottom;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
top.size(Geometry("600"));
top.backgroundColor("black");
top.font("Times");
top.textGravity(Magick::CenterGravity);
top.fontPointsize(56);
top.read("pango:<span foreground='white'>" + top_text + "</span>");
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:<span foreground='white'>" + bottom_text + "</span>");
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<Image> 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.magick(type);
mid.push_back(final);
}
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, top_text, bottom_text;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Motivate(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
string top_text = info[1].As<Napi::String>().Utf8Value();
string bottom_text = info[2].As<Napi::String>().Utf8Value();
string type = info[3].As<Napi::String>().Utf8Value();
int delay = info[4].As<Napi::Number>().Int32Value();
Napi::Function cb = info[5].As<Napi::Function>();
MotivateWorker* blurWorker = new MotivateWorker(cb, in_path, top_text, bottom_text, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/motivate.h Normal file
View file

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

57
natives/resize.cc Normal file
View file

@ -0,0 +1,57 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class ResizeWorker : public Napi::AsyncWorker {
public:
ResizeWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~ResizeWorker() {}
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.scale(Geometry("10%"));
image.scale(Geometry("1000%"));
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, amount;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Resize(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>();
ResizeWorker* explodeWorker = new ResizeWorker(cb, in_path, type, delay);
explodeWorker->Queue();
return env.Undefined();
}

8
natives/resize.h Normal file
View file

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

50
natives/reverse.cc Normal file
View file

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

8
natives/reverse.h Normal file
View file

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

65
natives/scott.cc Normal file
View file

@ -0,0 +1,65 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class ScottWorker : public Napi::AsyncWorker {
public:
ScottWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~ScottWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> result;
Image watermark;
readImages(&frames, in_path);
watermark.read("./assets/images/scott.png");
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
Image watermark_new = watermark;
image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod);
image.backgroundColor("none");
image.scale(Geometry("415x234!"));
double arguments[16] = {0, 0, 129, 187, 415, 0, 517, 182, 415, 234, 517, 465, 0, 234, 132, 418};
image.distort(Magick::PerspectiveDistortion, 16, arguments, true);
image.extent(Geometry("864x481"), Magick::CenterGravity);
watermark_new.composite(image, Geometry("-110+83"), Magick::OverCompositeOp);
watermark_new.magick(type);
mid.push_back(watermark_new);
}
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 Scott(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>();
ScottWorker* blurWorker = new ScottWorker(cb, in_path, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/scott.h Normal file
View file

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

51
natives/sonic.cc Normal file
View file

@ -0,0 +1,51 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class SonicWorker : public Napi::AsyncWorker {
public:
SonicWorker(Napi::Function& callback, string text)
: Napi::AsyncWorker(callback), text(text) {}
~SonicWorker() {}
void Execute() {
Image image;
Image text_image;
text_image.backgroundColor("none");
text_image.fontPointsize(72);
text_image.textGravity(Magick::CenterGravity);
text_image.font("Bitstream Vera Sans");
text_image.read("pango:<span foreground='white'>" + text + "</span>");
text_image.resize(Geometry(474, 332));
text_image.extent(Geometry("1024x538-435-145"), Magick::CenterGravity);
image.read("./assets/images/sonic.jpg");
image.composite(text_image, Geometry("+160+10"), Magick::OverCompositeOp);
image.magick("PNG");
image.write(&blob);
}
void OnOK() {
Callback().Call({Env().Undefined(), Napi::Buffer<char>::Copy(Env(), (char *)blob.data(), blob.length())});
}
private:
string text, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Sonic(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string text = info[0].As<Napi::String>().Utf8Value();
Napi::Function cb = info[1].As<Napi::Function>();
SonicWorker* explodeWorker = new SonicWorker(cb, text);
explodeWorker->Queue();
return env.Undefined();
}

8
natives/sonic.h Normal file
View file

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

49
natives/speed.cc Normal file
View file

@ -0,0 +1,49 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class SpeedWorker : public Napi::AsyncWorker {
public:
SpeedWorker(Napi::Function& callback, string in_path, bool slow, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), slow(slow), type(type), delay(delay) {}
~SpeedWorker() {}
void Execute() {
list<Image> frames;
list<Image> blurred;
readImages(&frames, in_path);
for_each(frames.begin(), frames.end(), animationDelayImage(slow ? delay * 2 : delay / 2));
writeImages(frames.begin(), frames.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, amount;
size_t bytes, type_size;
Blob blob;
bool slow;
};
Napi::Value Speed(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string in_path = info[0].As<Napi::String>().Utf8Value();
bool slow = info[1].As<Napi::Boolean>().Value();
string type = info[2].As<Napi::String>().Utf8Value();
int delay = info[3].As<Napi::Number>().Int32Value();
Napi::Function cb = info[4].As<Napi::Function>();
SpeedWorker* explodeWorker = new SpeedWorker(cb, in_path, slow, type, delay);
explodeWorker->Queue();
return env.Undefined();
}

8
natives/speed.h Normal file
View file

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