Final Magick++ ports, removed gm module

This commit is contained in:
TheEssem 2020-07-23 15:49:34 -05:00
parent e860aee986
commit 9555406229
30 changed files with 349 additions and 218 deletions

76
natives/caption.cc Normal file
View file

@ -0,0 +1,76 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class CaptionWorker : public Napi::AsyncWorker {
public:
CaptionWorker(Napi::Function& callback, string caption, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), caption(caption), in_path(in_path), type(type), delay(delay) {}
~CaptionWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> captioned;
list<Image> result;
Blob caption_blob;
readImages(&frames, in_path);
size_t width = frames.front().baseColumns();
size_t height = frames.front().baseRows();
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("./assets/caption.otf");
caption_image.fontPointsize(width / 10);
caption_image.textGravity(Magick::CenterGravity);
caption_image.read("caption:" + caption);
caption_image.extent(Geometry(width, caption_image.rows() + (width / 10)), Magick::CenterGravity);
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
Image appended;
list<Image> images;
image.backgroundColor("white");
images.push_back(caption_image);
images.push_back(image);
appendImages(&appended, images.begin(), images.end(), true);
appended.magick(type);
captioned.push_back(appended);
}
optimizeImageLayers(&result, captioned.begin(), captioned.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 caption, in_path, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Caption(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string caption = info[0].As<Napi::String>().Utf8Value();
string in_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>();
CaptionWorker* captionWorker = new CaptionWorker(cb, caption, in_path, type, delay);
captionWorker->Queue();
return env.Undefined();
}

8
natives/caption.h Normal file
View file

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

75
natives/caption2.cc Normal file
View file

@ -0,0 +1,75 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class CaptionTwoWorker : public Napi::AsyncWorker {
public:
CaptionTwoWorker(Napi::Function& callback, string caption, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), caption(caption), in_path(in_path), type(type), delay(delay) {}
~CaptionTwoWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> captioned;
list<Image> result;
Blob caption_blob;
readImages(&frames, in_path);
size_t width = frames.front().baseColumns();
size_t height = frames.front().baseRows();
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());
int iterator = 0;
for (Image &image : coalesced) {
Image appended;
list<Image> images;
image.backgroundColor("white");
images.push_back(image);
images.push_back(caption_image);
appendImages(&appended, images.begin(), images.end(), true);
appended.magick(type);
captioned.push_back(appended);
}
optimizeImageLayers(&result, captioned.begin(), captioned.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 caption, in_path, type;
int delay, wordlength, i, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value CaptionTwo(const Napi::CallbackInfo &info)
{
Napi::Env env = info.Env();
string caption = info[0].As<Napi::String>().Utf8Value();
string in_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>();
CaptionTwoWorker* captionTwoWorker = new CaptionTwoWorker(cb, caption, in_path, type, delay);
captionTwoWorker->Queue();
return env.Undefined();
}

8
natives/caption2.h Normal file
View file

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

View file

@ -1,8 +1,8 @@
#include <napi.h>
#include "blur.h"
#include "blurple.h"
//#include "caption.h"
//#include "caption2.h"
#include "caption.h"
#include "caption2.h"
#include "circle.h"
#include "explode.h"
#include "flag.h"
@ -28,14 +28,16 @@
#include "spin.h"
#include "tile.h"
#include "trump.h"
#include "wall.h"
#include "wdt.h"
#include "watermark.h"
Napi::Object Init(Napi::Env env, Napi::Object exports)
{
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, "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, "explode"), Napi::Function::New(env, Explode));
exports.Set(Napi::String::New(env, "flag"), Napi::Function::New(env, Flag));
@ -61,6 +63,8 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
exports.Set(Napi::String::New(env, "swirl"), Napi::Function::New(env, Swirl));
exports.Set(Napi::String::New(env, "tile"), Napi::Function::New(env, Tile));
exports.Set(Napi::String::New(env, "trump"), Napi::Function::New(env, Trump));
exports.Set(Napi::String::New(env, "wall"), Napi::Function::New(env, Wall));
exports.Set(Napi::String::New(env, "wdt"), Napi::Function::New(env, Wdt));
exports.Set(Napi::String::New(env, "watermark"), Napi::Function::New(env, Watermark));
return exports;
}

View file

@ -7,8 +7,8 @@ using namespace Magick;
class ResizeWorker : public Napi::AsyncWorker {
public:
ResizeWorker(Napi::Function& callback, string in_path, bool stretch, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), stretch(stretch), type(type), delay(delay) {}
ResizeWorker(Napi::Function& callback, string in_path, bool stretch, bool wide, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), stretch(stretch), wide(wide), type(type), delay(delay) {}
~ResizeWorker() {}
void Execute() {
@ -22,6 +22,8 @@ class ResizeWorker : public Napi::AsyncWorker {
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%"));
@ -44,7 +46,7 @@ class ResizeWorker : public Napi::AsyncWorker {
int delay, wordlength, i, n, amount;
size_t bytes, type_size;
Blob blob;
bool stretch;
bool stretch, wide;
};
Napi::Value Resize(const Napi::CallbackInfo &info)
@ -53,11 +55,12 @@ Napi::Value Resize(const Napi::CallbackInfo &info)
string in_path = info[0].As<Napi::String>().Utf8Value();
bool stretch = 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>();
bool wide = info[2].As<Napi::Boolean>().Value();
string type = info[3].As<Napi::String>().Utf8Value();
int delay = info[4].As<Napi::Number>().Int32Value();
Napi::Function cb = info[5].As<Napi::Function>();
ResizeWorker* explodeWorker = new ResizeWorker(cb, in_path, stretch, type, delay);
ResizeWorker* explodeWorker = new ResizeWorker(cb, in_path, stretch, wide, type, delay);
explodeWorker->Queue();
return env.Undefined();
}

62
natives/wall.cc Normal file
View file

@ -0,0 +1,62 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class WallWorker : public Napi::AsyncWorker {
public:
WallWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~WallWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> result;
readImages(&frames, in_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>"));
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 Wall(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>();
WallWorker* flopWorker = new WallWorker(cb, in_path, type, delay);
flopWorker->Queue();
return env.Undefined();
}

8
natives/wall.h Normal file
View file

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

60
natives/wdt.cc Normal file
View file

@ -0,0 +1,60 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class WdtWorker : public Napi::AsyncWorker {
public:
WdtWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~WdtWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> result;
Image watermark;
readImages(&frames, in_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);
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 Wdt(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>();
WdtWorker* blurWorker = new WdtWorker(cb, in_path, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/wdt.h Normal file
View file

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