Even more Magick++ ports

This commit is contained in:
TheEssem 2020-07-22 19:54:58 -05:00
parent 78dbeabd39
commit e860aee986
21 changed files with 372 additions and 69 deletions

View file

@ -18,12 +18,16 @@
#include "magik.h"
#include "meme.h"
#include "mirror.h"
#include "misc.h"
#include "motivate.h"
#include "resize.h"
#include "reverse.h"
#include "scott.h"
#include "speed.h"
#include "sonic.h"
#include "spin.h"
#include "tile.h"
#include "trump.h"
#include "watermark.h"
Napi::Object Init(Napi::Env env, Napi::Object exports)
@ -53,6 +57,10 @@ Napi::Object Init(Napi::Env env, Napi::Object exports)
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, "spin"), Napi::Function::New(env, Spin));
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, "watermark"), Napi::Function::New(env, Watermark));
return exports;
}

56
natives/misc.cc Normal file
View file

@ -0,0 +1,56 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class SwirlWorker : public Napi::AsyncWorker {
public:
SwirlWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~SwirlWorker() {}
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.swirl(180);
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 Swirl(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>();
SwirlWorker* flopWorker = new SwirlWorker(cb, in_path, type, delay);
flopWorker->Queue();
return env.Undefined();
}

8
natives/misc.h Normal file
View file

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

View file

@ -7,8 +7,8 @@ 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(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() {}
void Execute() {
@ -20,8 +20,12 @@ class ResizeWorker : public Napi::AsyncWorker {
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.scale(Geometry("10%"));
image.scale(Geometry("1000%"));
if (stretch) {
image.resize(Geometry("512x512!"));
} else {
image.scale(Geometry("10%"));
image.scale(Geometry("1000%"));
}
image.magick(type);
blurred.push_back(image);
}
@ -40,6 +44,7 @@ class ResizeWorker : public Napi::AsyncWorker {
int delay, wordlength, i, n, amount;
size_t bytes, type_size;
Blob blob;
bool stretch;
};
Napi::Value Resize(const Napi::CallbackInfo &info)
@ -47,11 +52,12 @@ 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>();
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>();
ResizeWorker* explodeWorker = new ResizeWorker(cb, in_path, type, delay);
ResizeWorker* explodeWorker = new ResizeWorker(cb, in_path, stretch, type, delay);
explodeWorker->Queue();
return env.Undefined();
}

View file

@ -7,8 +7,8 @@ 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(Napi::Function& callback, string in_path, bool soos, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), soos(soos), delay(delay) {}
~ReverseWorker() {}
void Execute() {
@ -18,7 +18,13 @@ class ReverseWorker : public Napi::AsyncWorker {
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
coalesced.reverse();
if (soos) {
list<Image> copy = coalesced;
copy.reverse();
coalesced.insert(coalesced.end(), copy.begin(), copy.end());
} else {
coalesced.reverse();
}
optimizeImageLayers(&result, coalesced.begin(), coalesced.end());
if (delay != 0) for_each(result.begin(), result.end(), animationDelayImage(delay));
@ -34,6 +40,7 @@ class ReverseWorker : public Napi::AsyncWorker {
int delay, wordlength, i, n, amount;
size_t bytes, type_size;
Blob blob;
bool soos;
};
Napi::Value Reverse(const Napi::CallbackInfo &info)
@ -41,10 +48,11 @@ 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>();
bool soos = info[1].As<Napi::Boolean>().Value();
int delay = info[2].As<Napi::Number>().Int32Value();
Napi::Function cb = info[3].As<Napi::Function>();
ReverseWorker* explodeWorker = new ReverseWorker(cb, in_path, delay);
ReverseWorker* explodeWorker = new ReverseWorker(cb, in_path, soos, delay);
explodeWorker->Queue();
return env.Undefined();
}

View file

@ -1,5 +1,6 @@
#include <napi.h>
#include <list>
#include <iostream>
#include <Magick++.h>
using namespace std;
@ -16,7 +17,14 @@ class SpeedWorker : public Napi::AsyncWorker {
list<Image> blurred;
readImages(&frames, in_path);
for_each(frames.begin(), frames.end(), animationDelayImage(slow ? delay * 2 : delay / 2));
int new_delay = slow ? delay * 2 : delay / 2;
if (new_delay <= 1) {
new_delay = delay;
auto it = frames.begin();
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it);
} else {
for_each(frames.begin(), frames.end(), animationDelayImage(new_delay));
}
writeImages(frames.begin(), frames.end(), &blob);
}

73
natives/spin.cc Normal file
View file

@ -0,0 +1,73 @@
#include <napi.h>
#include <list>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class SpinWorker : public Napi::AsyncWorker {
public:
SpinWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~SpinWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> result;
readImages(&frames, in_path);
coalesceImages(&coalesced, frames.begin(), frames.end());
if (type != "GIF") {
list<Image>::iterator it = coalesced.begin();
for (int i = 0; i < 29; ++i) {
coalesced.push_back(*it);
}
}
int i = 0;
for (Image &image : coalesced) {
image.scale(Geometry("256x256"));
image.alphaChannel(Magick::SetAlphaChannel);
double rotation[1] = {360 * i / coalesced.size()};
image.distort(Magick::ScaleRotateTranslateDistortion, 1, rotation);
image.magick("GIF");
mid.push_back(image);
i++;
}
optimizeImageLayers(&result, mid.begin(), mid.end());
if (delay != 0) {
for_each(result.begin(), result.end(), animationDelayImage(delay));
} else if (type != "GIF") {
for_each(result.begin(), result.end(), animationDelayImage(5));
}
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, n;
size_t bytes, type_size;
Blob blob;
};
Napi::Value Spin(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>();
SpinWorker* blurWorker = new SpinWorker(cb, in_path, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/spin.h Normal file
View file

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

68
natives/tile.cc Normal file
View file

@ -0,0 +1,68 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class TileWorker : public Napi::AsyncWorker {
public:
TileWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~TileWorker() {}
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) {
list<Image> duplicated;
Image appended;
list<Image> montage;
Image frame;
image.magick(type);
for (int i = 0; i < 5; ++i) {
duplicated.push_back(image);
}
appendImages(&appended, duplicated.begin(), duplicated.end());
for (int i = 0; i < 5; ++i) {
montage.push_back(appended);
}
appendImages(&frame, montage.begin(), montage.end(), true);
frame.scale(Geometry("800x800>"));
mid.push_back(frame);
}
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 Tile(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>();
TileWorker* flopWorker = new TileWorker(cb, in_path, type, delay);
flopWorker->Queue();
return env.Undefined();
}

8
natives/tile.h Normal file
View file

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

65
natives/trump.cc Normal file
View file

@ -0,0 +1,65 @@
#include <napi.h>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
class TrumpWorker : public Napi::AsyncWorker {
public:
TrumpWorker(Napi::Function& callback, string in_path, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), type(type), delay(delay) {}
~TrumpWorker() {}
void Execute() {
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
list<Image> result;
Image watermark;
readImages(&frames, in_path);
watermark.read("./assets/images/trump.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("365x179!"));
double arguments[16] = {0, 0, 207, 268, 365, 0, 548, 271, 365, 179, 558, 450, 0, 179, 193, 450};
image.distort(Magick::PerspectiveDistortion, 16, arguments, true);
image.extent(Geometry("800x450"), Magick::CenterGravity);
watermark_new.composite(image, Geometry("-25+134"), Magick::DstOverCompositeOp);
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 Trump(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>();
TrumpWorker* blurWorker = new TrumpWorker(cb, in_path, type, delay);
blurWorker->Queue();
return env.Undefined();
}

8
natives/trump.h Normal file
View file

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