2022-11-27 19:03:07 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
2020-07-12 15:14:39 +00:00
|
|
|
#include <napi.h>
|
2022-11-27 19:03:07 +00:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2022-11-27 20:52:40 +00:00
|
|
|
#include <iostream>
|
2022-11-27 19:03:07 +00:00
|
|
|
|
2020-07-12 15:14:39 +00:00
|
|
|
#include "blur.h"
|
2021-12-14 07:16:23 +00:00
|
|
|
#include "colors.h"
|
2020-07-23 20:49:34 +00:00
|
|
|
#include "caption.h"
|
|
|
|
#include "caption2.h"
|
2020-07-12 15:14:39 +00:00
|
|
|
#include "circle.h"
|
2020-07-23 22:43:39 +00:00
|
|
|
#include "crop.h"
|
2021-06-29 22:26:22 +00:00
|
|
|
#include "deepfry.h"
|
2020-07-12 15:14:39 +00:00
|
|
|
#include "explode.h"
|
2020-07-14 14:53:51 +00:00
|
|
|
#include "flag.h"
|
|
|
|
#include "flip.h"
|
|
|
|
#include "freeze.h"
|
2020-07-16 14:31:48 +00:00
|
|
|
#include "gamexplain.h"
|
|
|
|
#include "globe.h"
|
2020-07-21 14:03:08 +00:00
|
|
|
#include "homebrew.h"
|
2020-07-12 15:14:39 +00:00
|
|
|
#include "invert.h"
|
2020-07-20 22:10:04 +00:00
|
|
|
#include "jpeg.h"
|
|
|
|
#include "magik.h"
|
|
|
|
#include "meme.h"
|
|
|
|
#include "mirror.h"
|
2021-04-24 22:54:47 +00:00
|
|
|
#include "swirl.h"
|
2020-07-22 18:12:38 +00:00
|
|
|
#include "motivate.h"
|
2021-04-26 14:47:03 +00:00
|
|
|
#include "reddit.h"
|
2020-07-22 18:12:38 +00:00
|
|
|
#include "resize.h"
|
|
|
|
#include "reverse.h"
|
|
|
|
#include "scott.h"
|
2021-06-15 03:03:48 +00:00
|
|
|
#include "snapchat.h"
|
2020-07-22 18:12:38 +00:00
|
|
|
#include "sonic.h"
|
2022-10-07 18:26:12 +00:00
|
|
|
#include "speed.h"
|
2020-07-23 00:54:58 +00:00
|
|
|
#include "spin.h"
|
2022-10-07 18:26:12 +00:00
|
|
|
#include "squish.h"
|
2020-07-23 00:54:58 +00:00
|
|
|
#include "tile.h"
|
2022-06-25 17:40:52 +00:00
|
|
|
#include "togif.h"
|
2022-07-30 21:00:09 +00:00
|
|
|
#include "uncanny.h"
|
2021-05-04 21:28:34 +00:00
|
|
|
#include "uncaption.h"
|
2020-07-23 20:49:34 +00:00
|
|
|
#include "wall.h"
|
2020-07-14 14:53:51 +00:00
|
|
|
#include "watermark.h"
|
2021-07-12 21:02:00 +00:00
|
|
|
#include "whisper.h"
|
2021-12-03 00:05:52 +00:00
|
|
|
#include "zamn.h"
|
2020-07-12 15:14:39 +00:00
|
|
|
|
2021-12-31 05:08:06 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Magick++.h>
|
|
|
|
#endif
|
2022-02-12 21:09:59 +00:00
|
|
|
#include <vips/vips8>
|
2021-12-31 05:08:06 +00:00
|
|
|
|
2022-11-27 19:03:07 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2022-12-28 06:20:17 +00:00
|
|
|
std::map<std::string, char* (*)(string *type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize)> FunctionMap = {
|
2022-11-27 20:52:40 +00:00
|
|
|
{"blur", &Blur},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"caption", &Caption},
|
2022-11-27 20:52:40 +00:00
|
|
|
{"captionTwo", &CaptionTwo},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"circle", &Circle},
|
|
|
|
{"colors", &Colors},
|
|
|
|
{"crop", &Crop},
|
|
|
|
{"deepfry", &Deepfry},
|
|
|
|
{"explode", &Explode},
|
|
|
|
{"flag", &Flag},
|
|
|
|
{"flip", &Flip},
|
2022-12-12 18:13:03 +00:00
|
|
|
{"freeze", &Freeze},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"gamexplain", Gamexplain},
|
|
|
|
{"globe", Globe},
|
|
|
|
{"invert", Invert},
|
|
|
|
{"jpeg", Jpeg},
|
2022-12-28 06:20:17 +00:00
|
|
|
{"magik", Magik},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"meme", Meme},
|
|
|
|
{"mirror", Mirror},
|
|
|
|
{"motivate", Motivate},
|
|
|
|
{"reddit", Reddit},
|
|
|
|
{"resize", Resize},
|
|
|
|
{"reverse", Reverse},
|
|
|
|
{"scott", Scott},
|
|
|
|
{"snapchat", Snapchat},
|
2022-12-28 06:20:17 +00:00
|
|
|
{"speed", &Speed},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"spin", Spin},
|
|
|
|
{"swirl", Swirl},
|
|
|
|
{"tile", Tile},
|
|
|
|
{"togif", ToGif},
|
|
|
|
{"uncanny", Uncanny},
|
2022-12-28 06:20:17 +00:00
|
|
|
{"uncaption", &Uncaption},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"wall", Wall},
|
2022-12-28 06:20:17 +00:00
|
|
|
{"watermark", &Watermark},
|
2022-11-27 19:03:07 +00:00
|
|
|
{"whisper", Whisper},
|
|
|
|
{"zamn", Zamn}
|
|
|
|
};
|
|
|
|
|
2022-12-28 06:20:17 +00:00
|
|
|
std::map<std::string, char* (*)(string *type, ArgumentMap Arguments, size_t* DataSize)> NoInputFunctionMap = {
|
|
|
|
{"homebrew", Homebrew},
|
|
|
|
{"sonic", Sonic}
|
|
|
|
};
|
|
|
|
|
2022-11-27 20:52:40 +00:00
|
|
|
bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
|
|
|
|
return env.Global()
|
|
|
|
.Get("Number")
|
|
|
|
.ToObject()
|
|
|
|
.Get("isInteger")
|
|
|
|
.As<Napi::Function>()
|
|
|
|
.Call({num})
|
|
|
|
.ToBoolean()
|
|
|
|
.Value();
|
|
|
|
}
|
|
|
|
|
2022-12-26 06:28:56 +00:00
|
|
|
Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
|
2022-11-27 19:03:07 +00:00
|
|
|
Napi::Env env = info.Env();
|
|
|
|
Napi::Object result = Napi::Object::New(env);
|
|
|
|
|
|
|
|
try {
|
|
|
|
string command = info[0].As<Napi::String>().Utf8Value();
|
|
|
|
Napi::Object obj = info[1].As<Napi::Object>();
|
2022-12-26 06:28:56 +00:00
|
|
|
string type = obj.Has("type") ? obj.Get("type").As<Napi::String>().Utf8Value() : NULL;
|
2022-11-27 19:03:07 +00:00
|
|
|
|
|
|
|
Napi::Array properties = obj.GetPropertyNames();
|
|
|
|
|
2022-12-03 19:49:28 +00:00
|
|
|
ArgumentMap Arguments;
|
2022-11-27 19:03:07 +00:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < properties.Length(); i++) {
|
|
|
|
string property = properties.Get(uint32_t(i)).As<Napi::String>().Utf8Value();
|
|
|
|
|
|
|
|
if (property == "data") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-11-27 20:52:40 +00:00
|
|
|
auto val = obj.Get(property);
|
|
|
|
if (val.IsBoolean()) {
|
|
|
|
Arguments[property] = val.ToBoolean().Value();
|
|
|
|
} else if (val.IsString()) {
|
|
|
|
Arguments[property] = val.ToString().As<Napi::String>().Utf8Value();
|
|
|
|
} else if (val.IsNumber()) {
|
|
|
|
auto num = val.ToNumber();
|
|
|
|
if (isNapiValueInt(env, num)) {
|
|
|
|
Arguments[property] = num.Int32Value();
|
|
|
|
} else {
|
|
|
|
Arguments[property] = num.FloatValue();
|
|
|
|
}
|
2022-12-03 19:49:28 +00:00
|
|
|
} else {
|
|
|
|
throw "Unimplemented value type passed to image native.";
|
|
|
|
//Arguments[property] = val;
|
2022-11-27 20:52:40 +00:00
|
|
|
}
|
2022-11-27 19:03:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t length = 0;
|
2022-12-26 06:28:56 +00:00
|
|
|
char* buf;
|
|
|
|
if (input) {
|
|
|
|
Napi::Buffer<char> data = obj.Has("data") ? obj.Get("data").As<Napi::Buffer<char>>() : Napi::Buffer<char>::New(env, 0);
|
2022-12-28 06:20:17 +00:00
|
|
|
buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length);
|
2022-12-26 06:28:56 +00:00
|
|
|
} else {
|
|
|
|
buf = NoInputFunctionMap.at(command)(&type, Arguments, &length);
|
|
|
|
}
|
2022-11-27 19:03:07 +00:00
|
|
|
result.Set("data", Napi::Buffer<char>::New(env, buf, length, [](Napi::Env env, void* data) {
|
|
|
|
free(data);
|
|
|
|
}));
|
|
|
|
result.Set("type", type);
|
|
|
|
} catch (std::exception const &err) {
|
|
|
|
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
|
|
|
|
} catch (...) {
|
|
|
|
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Napi::Value ProcessImage(const Napi::CallbackInfo &info) { // janky solution for gradual adoption
|
|
|
|
Napi::Env env = info.Env();
|
|
|
|
|
|
|
|
string command = info[0].As<Napi::String>().Utf8Value();
|
|
|
|
|
|
|
|
if (MAP_HAS(FunctionMap, command)) {
|
2022-12-26 06:28:56 +00:00
|
|
|
return NewProcessImage(info, true);
|
|
|
|
} else if (MAP_HAS(NoInputFunctionMap, command)) {
|
|
|
|
return NewProcessImage(info, false);
|
2022-11-27 19:03:07 +00:00
|
|
|
} else {
|
|
|
|
Napi::Error::New(env, "Invalid command").ThrowAsJavaScriptException();
|
2022-11-27 23:23:57 +00:00
|
|
|
return env.Null();
|
2022-11-27 19:03:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Napi::Object Init(Napi::Env env, Napi::Object exports){
|
2021-12-31 05:08:06 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
Magick::InitializeMagick("");
|
|
|
|
#endif
|
2022-02-12 21:09:59 +00:00
|
|
|
if (vips_init(""))
|
|
|
|
vips_error_exit(NULL);
|
2022-11-27 19:03:07 +00:00
|
|
|
exports.Set(Napi::String::New(env, "image"), Napi::Function::New(env, ProcessImage)); // new function handler
|
|
|
|
|
2022-11-27 20:52:40 +00:00
|
|
|
Napi::Array arr = Napi::Array::New(env);
|
|
|
|
size_t i = 0;
|
|
|
|
for (auto const& imap: FunctionMap) {
|
|
|
|
Napi::HandleScope scope(env);
|
|
|
|
arr[i] = Napi::String::New(env, imap.first);
|
|
|
|
i++;
|
|
|
|
}
|
2022-12-26 06:28:56 +00:00
|
|
|
for (auto const& imap: NoInputFunctionMap) {
|
|
|
|
Napi::HandleScope scope(env);
|
|
|
|
arr[i] = Napi::String::New(env, imap.first);
|
|
|
|
i++;
|
|
|
|
}
|
2022-11-27 20:52:40 +00:00
|
|
|
|
|
|
|
exports.Set(Napi::String::New(env, "funcs"), arr);
|
|
|
|
|
2022-11-27 19:03:07 +00:00
|
|
|
return exports;
|
2020-07-12 15:14:39 +00:00
|
|
|
}
|
|
|
|
|
2022-02-12 21:09:59 +00:00
|
|
|
NODE_API_MODULE(addon, Init)
|