Portablized more natives

This commit is contained in:
Essem 2022-12-26 00:28:56 -06:00
parent a01e2ae77b
commit e1a31d6ddc
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
25 changed files with 698 additions and 843 deletions

View file

@ -1,24 +1,19 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Gamexplain(const Napi::CallbackInfo &info) { char *Gamexplain(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env);
try { string basePath = GetArgument<string>(Arguments, "basePath");
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) if (!in.has_alpha())
@ -35,12 +30,11 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) {
for (int i = 0; i < nPages; i++) { for (int i = 0; i < nPages; i++) {
VImage img_frame = VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
VImage resized = img_frame VImage resized =
img_frame
.resize(1181.0 / (double)width, .resize(1181.0 / (double)width,
VImage::option()->set( VImage::option()->set("vscale", 571.0 / (double)pageHeight))
"vscale", 571.0 / (double)pageHeight)) .embed(10, 92, 1200, 675, VImage::option()->set("extend", "white"));
.embed(10, 92, 1200, 675,
VImage::option()->set("extend", "white"));
VImage composited = resized.composite2(tmpl, VIPS_BLEND_MODE_OVER); VImage composited = resized.composite2(tmpl, VIPS_BLEND_MODE_OVER);
img.push_back(composited); img.push_back(composited);
} }
@ -48,21 +42,13 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) {
final.set(VIPS_META_PAGE_HEIGHT, 675); final.set(VIPS_META_PAGE_HEIGHT, 675);
void *buf; void *buf;
size_t length;
final.write_to_buffer( final.write_to_buffer(
("." + type).c_str(), &buf, &length, ("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0); : 0);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result;
return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Gamexplain(const Napi::CallbackInfo& info); using std::string;
char* Gamexplain(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,25 +1,20 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Globe(const Napi::CallbackInfo &info) { char *Globe(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env);
try { string basePath = GetArgument<string>(Arguments, "basePath");
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
VOption *options = VImage::option(); VOption *options = VImage::option();
VImage in = VImage in =
VImage::new_from_buffer( VImage::new_from_buffer(
data.Data(), data.Length(), "", BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1)->set("access", "sequential") type == "gif" ? options->set("n", -1)->set("access", "sequential")
: options) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
@ -76,18 +71,9 @@ Napi::Value Globe(const Napi::CallbackInfo &info) {
} }
void *buf; void *buf;
size_t length; final.write_to_buffer(".gif", &buf, DataSize);
final.write_to_buffer(".gif", &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", "gif");
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Globe(const Napi::CallbackInfo& info); using std::string;
char* Globe(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,28 +1,22 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Homebrew(const Napi::CallbackInfo &info) { char *Homebrew(string *type, ArgumentMap Arguments, size_t *DataSize) {
Napi::Env env = info.Env(); string caption = GetArgument<string>(Arguments, "caption");
Napi::Object result = Napi::Object::New(env); string basePath = GetArgument<string>(Arguments, "basePath");
try {
Napi::Object obj = info[1].As<Napi::Object>();
string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
string assetPath = basePath + "assets/images/hbc.png"; string assetPath = basePath + "assets/images/hbc.png";
VImage bg = VImage::new_from_file(assetPath.c_str()); VImage bg = VImage::new_from_file(assetPath.c_str());
VImage text = VImage::text( VImage text = VImage::text(
".", VImage::option()->set( ".", VImage::option()->set("fontfile",
"fontfile", (basePath + "assets/fonts/hbc.ttf").c_str())); (basePath + "assets/fonts/hbc.ttf").c_str()));
text = VImage::text( text = VImage::text(
("<span letter_spacing=\"-5120\" color=\"white\">" + caption + ("<span letter_spacing=\"-5120\" color=\"white\">" + caption + "</span>")
"</span>")
.c_str(), .c_str(),
VImage::option() VImage::option()
->set("rgba", true) ->set("rgba", true)
@ -36,18 +30,11 @@ Napi::Value Homebrew(const Napi::CallbackInfo &info) {
->set("y", 300 - (text.height() / 2) - 8)); ->set("y", 300 - (text.height() / 2) - 8));
void *buf; void *buf;
size_t length; out.write_to_buffer(".png", &buf, DataSize);
out.write_to_buffer(".png", &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length)); *type = "png";
result.Set("type", "png");
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Homebrew(const Napi::CallbackInfo& info); using std::string;
char* Homebrew(string *type, ArgumentMap Arguments, size_t *DataSize);

View file

@ -63,27 +63,30 @@ std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLeng
{"flag", &Flag}, {"flag", &Flag},
{"flip", &Flip}, {"flip", &Flip},
{"freeze", &Freeze}, {"freeze", &Freeze},
{"speed", &Speed},
{"uncaption", &Uncaption},
{"watermark", &Watermark}
};
std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFunctionMap = {
{"gamexplain", Gamexplain}, {"gamexplain", Gamexplain},
{"globe", Globe}, {"globe", Globe},
{"homebrew", Homebrew},
{"invert", Invert}, {"invert", Invert},
{"jpeg", Jpeg}, {"jpeg", Jpeg},
{"magik", Magik},
{"meme", Meme}, {"meme", Meme},
{"mirror", Mirror}, {"mirror", Mirror},
{"motivate", Motivate}, {"motivate", Motivate},
{"reddit", Reddit}, {"reddit", Reddit},
{"resize", Resize}, {"resize", Resize},
{"reverse", Reverse}, {"reverse", Reverse},
{"speed", &Speed},
{"uncaption", &Uncaption},
{"watermark", &Watermark}
};
std::map<std::string, char* (*)(string *type, ArgumentMap Arguments, size_t* DataSize)> NoInputFunctionMap = {
{"homebrew", Homebrew},
{"sonic", Sonic}
};
std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFunctionMap = {
{"magik", Magik},
{"scott", Scott}, {"scott", Scott},
{"snapchat", Snapchat}, {"snapchat", Snapchat},
{"sonic", Sonic},
{"spin", Spin}, {"spin", Spin},
{"swirl", Swirl}, {"swirl", Swirl},
{"tile", Tile}, {"tile", Tile},
@ -105,15 +108,14 @@ bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
.Value(); .Value();
} }
Napi::Value NewProcessImage(const Napi::CallbackInfo &info) { Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env); Napi::Object result = Napi::Object::New(env);
try { try {
string command = info[0].As<Napi::String>().Utf8Value(); string command = info[0].As<Napi::String>().Utf8Value();
Napi::Object obj = info[1].As<Napi::Object>(); Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>(); string type = obj.Has("type") ? obj.Get("type").As<Napi::String>().Utf8Value() : NULL;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
Napi::Array properties = obj.GetPropertyNames(); Napi::Array properties = obj.GetPropertyNames();
@ -145,7 +147,13 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
} }
size_t length = 0; size_t length = 0;
char* buf = FunctionMap.at(command)(type, data.Data(), data.Length(), Arguments, &length); char* buf;
if (input) {
Napi::Buffer<char> data = obj.Has("data") ? obj.Get("data").As<Napi::Buffer<char>>() : Napi::Buffer<char>::New(env, 0);
buf = FunctionMap.at(command)(type, data.Data(), data.Length(), Arguments, &length);
} else {
buf = NoInputFunctionMap.at(command)(&type, Arguments, &length);
}
result.Set("data", Napi::Buffer<char>::New(env, buf, length, [](Napi::Env env, void* data) { result.Set("data", Napi::Buffer<char>::New(env, buf, length, [](Napi::Env env, void* data) {
free(data); free(data);
})); }));
@ -169,7 +177,9 @@ Napi::Value ProcessImage(const Napi::CallbackInfo &info) { // janky solution for
string command = info[0].As<Napi::String>().Utf8Value(); string command = info[0].As<Napi::String>().Utf8Value();
if (MAP_HAS(FunctionMap, command)) { if (MAP_HAS(FunctionMap, command)) {
return NewProcessImage(info); return NewProcessImage(info, true);
} else if (MAP_HAS(NoInputFunctionMap, command)) {
return NewProcessImage(info, false);
} else if (MAP_HAS(OldFunctionMap, command)) { } else if (MAP_HAS(OldFunctionMap, command)) {
return OldProcessImage(command, info); return OldProcessImage(command, info);
} else { } else {
@ -193,6 +203,11 @@ Napi::Object Init(Napi::Env env, Napi::Object exports){
arr[i] = Napi::String::New(env, imap.first); arr[i] = Napi::String::New(env, imap.first);
i++; i++;
} }
for (auto const& imap: NoInputFunctionMap) {
Napi::HandleScope scope(env);
arr[i] = Napi::String::New(env, imap.first);
i++;
}
for(auto const& imap: OldFunctionMap) { for(auto const& imap: OldFunctionMap) {
Napi::HandleScope scope(env); Napi::HandleScope scope(env);
arr[i] = Napi::String::New(env, imap.first); arr[i] = Napi::String::New(env, imap.first);

View file

@ -1,26 +1,21 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Invert(const Napi::CallbackInfo &info) { char *Invert(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env);
try {
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255); if (!in.has_alpha())
in = in.bandjoin(255);
VImage noAlpha = VImage noAlpha =
in.extract_band(0, VImage::option()->set("n", in.bands() - 1)); in.extract_band(0, VImage::option()->set("n", in.bands() - 1));
@ -28,18 +23,9 @@ Napi::Value Invert(const Napi::CallbackInfo &info) {
VImage out = inverted.bandjoin(in.extract_band(3)); VImage out = inverted.bandjoin(in.extract_band(3));
void *buf; void *buf;
size_t length; out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
out.write_to_buffer(("." + type).c_str(), &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Invert(const Napi::CallbackInfo& info); using std::string;
char* Invert(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,26 +1,19 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Jpeg(const Napi::CallbackInfo &info) { char *Jpeg(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env); int quality = GetArgumentWithFallback<int>(Arguments, "quality", 0);
try { void *buf;
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
int quality = obj.Has("quality")
? obj.Get("quality").As<Napi::Number>().Int32Value()
: 0;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
if (type == "gif") { if (type == "gif") {
VImage in = VImage in = VImage::new_from_buffer(
VImage::new_from_buffer( BufferData, BufferLength, "",
data.Data(), data.Length(), "",
VImage::option()->set("access", "sequential")->set("n", -1)) VImage::option()->set("access", "sequential")->set("n", -1))
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) if (!in.has_alpha())
@ -60,32 +53,18 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) {
final.set("delay", in.get_array_int("delay")); final.set("delay", in.get_array_int("delay"));
} }
void *buf; final.write_to_buffer(("." + type).c_str(), &buf, DataSize,
size_t length;
final.write_to_buffer(("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0) type == "gif" ? VImage::option()->set("dither", 0)
: 0); : 0);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} else { } else {
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), ""); VImage in = VImage::new_from_buffer(BufferData, BufferLength, "");
void *buf; in.write_to_buffer(".jpg", &buf, DataSize,
size_t length;
in.write_to_buffer(
".jpg", &buf, &length,
VImage::option()->set("Q", quality)->set("strip", true)); VImage::option()->set("Q", quality)->set("strip", true));
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length)); type = "jpg";
result.Set("type", "jpg");
}
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
} }
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Jpeg(const Napi::CallbackInfo& info); using std::string;
char* Jpeg(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,28 +1,21 @@
#include "common.h" #include "common.h"
#include <napi.h>
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Meme(const Napi::CallbackInfo &info) { char *Meme(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env); string top = GetArgument<string>(Arguments, "top");
string bottom = GetArgument<string>(Arguments, "bottom");
try { string font = GetArgument<string>(Arguments, "font");
Napi::Object obj = info[1].As<Napi::Object>(); string basePath = GetArgument<string>(Arguments, "basePath");
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string top = obj.Get("top").As<Napi::String>().Utf8Value();
string bottom = obj.Get("bottom").As<Napi::String>().Utf8Value();
string font = obj.Get("font").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) if (!in.has_alpha())
@ -36,9 +29,9 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
int rad = 1; int rad = 1;
vector<double> zeroVec = {0, 0, 0, 0}; vector<double> zeroVec = {0, 0, 0, 0};
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font " + string font_string =
(font != "impact" ? "bold" : "normal") + (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font " +
" " + to_string(size); (font != "impact" ? "bold" : "normal") + " " + to_string(size);
VImage mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128; VImage mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128;
mask.draw_circle({255}, rad, rad, rad, VImage::option()->set("fill", true)); mask.draw_circle({255}, rad, rad, rad, VImage::option()->set("fill", true));
@ -53,9 +46,8 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
auto findResult = fontPaths.find(font); auto findResult = fontPaths.find(font);
if (findResult != fontPaths.end()) { if (findResult != fontPaths.end()) {
VImage::text( VImage::text(".", VImage::option()->set(
".", VImage::option()->set("fontfile", "fontfile", (basePath + findResult->second).c_str()));
(basePath + findResult->second).c_str()));
} }
VImage topText; VImage topText;
@ -76,15 +68,13 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
topIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE) topIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1)); .gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
if (dividedWidth >= 1) { if (dividedWidth >= 1) {
topOutline = topOutline = topOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
topOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
} }
topOutline = (topOutline == zeroVec); topOutline = (topOutline == zeroVec);
VImage topInvert = topOutline.extract_band(3).invert(); VImage topInvert = topOutline.extract_band(3).invert();
topOutline = topOutline =
topOutline topOutline
.extract_band(0, .extract_band(0, VImage::option()->set("n", topOutline.bands() - 1))
VImage::option()->set("n", topOutline.bands() - 1))
.bandjoin(topInvert); .bandjoin(topInvert);
topText = topOutline.composite2(topIn, VIPS_BLEND_MODE_OVER); topText = topOutline.composite2(topIn, VIPS_BLEND_MODE_OVER);
} }
@ -111,9 +101,10 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
} }
bottomOutline = (bottomOutline == zeroVec); bottomOutline = (bottomOutline == zeroVec);
VImage bottomInvert = bottomOutline.extract_band(3).invert(); VImage bottomInvert = bottomOutline.extract_band(3).invert();
bottomOutline = bottomOutline bottomOutline =
.extract_band(0, VImage::option()->set( bottomOutline
"n", bottomOutline.bands() - 1)) .extract_band(0,
VImage::option()->set("n", bottomOutline.bands() - 1))
.bandjoin(bottomInvert); .bandjoin(bottomInvert);
bottomText = bottomOutline.composite2(bottomIn, VIPS_BLEND_MODE_OVER); bottomText = bottomOutline.composite2(bottomIn, VIPS_BLEND_MODE_OVER);
} }
@ -140,21 +131,12 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
final.set(VIPS_META_PAGE_HEIGHT, pageHeight); final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
void *buf; void *buf;
size_t length;
final.write_to_buffer( final.write_to_buffer(
("." + type).c_str(), &buf, &length, ("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0); : 0);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Meme(const Napi::CallbackInfo& info); using std::string;
char* Meme(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,31 +1,23 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Mirror(const Napi::CallbackInfo &info) { char *Mirror(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env); bool vertical = GetArgumentWithFallback<bool>(Arguments, "vertical", false);
bool first = GetArgumentWithFallback<bool>(Arguments, "first", false);
try {
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
bool vertical = obj.Has("vertical")
? obj.Get("vertical").As<Napi::Boolean>().Value()
: false;
bool first =
obj.Has("first") ? obj.Get("first").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255); if (!in.has_alpha())
in = in.bandjoin(255);
VImage out; VImage out;
@ -67,18 +59,9 @@ Napi::Value Mirror(const Napi::CallbackInfo &info) {
} }
void *buf; void *buf;
size_t length; out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
out.write_to_buffer(("." + type).c_str(), &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Mirror(const Napi::CallbackInfo& info); using std::string;
char* Mirror(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,28 +1,21 @@
#include "common.h" #include "common.h"
#include <napi.h>
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Motivate(const Napi::CallbackInfo &info) { char *Motivate(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env); string top_text = GetArgument<string>(Arguments, "top");
string bottom_text = GetArgument<string>(Arguments, "bottom");
try { string font = GetArgument<string>(Arguments, "font");
Napi::Object obj = info[1].As<Napi::Object>(); string basePath = GetArgument<string>(Arguments, "basePath");
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string top_text = obj.Get("top").As<Napi::String>().Utf8Value();
string bottom_text = obj.Get("bottom").As<Napi::String>().Utf8Value();
string font = obj.Get("font").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) if (!in.has_alpha())
@ -39,9 +32,8 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) {
auto findResult = fontPaths.find(font); auto findResult = fontPaths.find(font);
if (findResult != fontPaths.end()) { if (findResult != fontPaths.end()) {
VImage::text( VImage::text(".", VImage::option()->set(
".", VImage::option()->set("fontfile", "fontfile", (basePath + findResult->second).c_str()));
(basePath + findResult->second).c_str()));
} }
VImage topImage; VImage topImage;
@ -86,8 +78,8 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) {
img_frame.embed(borderSize, borderSize, width + (borderSize * 2), img_frame.embed(borderSize, borderSize, width + (borderSize * 2),
pageHeight + (borderSize * 2), pageHeight + (borderSize * 2),
VImage::option()->set("extend", "black")); VImage::option()->set("extend", "black"));
VImage bordered2 = bordered.embed( VImage bordered2 = bordered.embed(borderSize2, borderSize2,
borderSize2, borderSize2, bordered.width() + (borderSize2 * 2), bordered.width() + (borderSize2 * 2),
bordered.height() + (borderSize2 * 2), bordered.height() + (borderSize2 * 2),
VImage::option()->set("extend", "white")); VImage::option()->set("extend", "white"));
@ -125,20 +117,10 @@ Napi::Value Motivate(const Napi::CallbackInfo &info) {
final.set(VIPS_META_PAGE_HEIGHT, height); final.set(VIPS_META_PAGE_HEIGHT, height);
void *buf; void *buf;
size_t length; final.write_to_buffer(("." + type).c_str(), &buf, DataSize,
final.write_to_buffer(("." + type).c_str(), &buf, &length, type == "gif" ? VImage::option()->set("dither", 1) : 0);
type == "gif" ? VImage::option()->set("dither", 1)
: 0);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Motivate(const Napi::CallbackInfo& info); using std::string;
char* Motivate(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,25 +1,20 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Reddit(const Napi::CallbackInfo &info) { char *Reddit(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env);
try { string text = GetArgument<string>(Arguments, "text");
Napi::Object obj = info[1].As<Napi::Object>(); string basePath = GetArgument<string>(Arguments, "basePath");
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string text = obj.Get("caption").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) if (!in.has_alpha())
@ -64,21 +59,12 @@ Napi::Value Reddit(const Napi::CallbackInfo &info) {
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + watermark.height()); final.set(VIPS_META_PAGE_HEIGHT, pageHeight + watermark.height());
void *buf; void *buf;
size_t length;
final.write_to_buffer( final.write_to_buffer(
("." + type).c_str(), &buf, &length, ("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0); : 0);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Reddit(const Napi::CallbackInfo& info); using std::string;
char* Reddit(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,28 +1,19 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Resize(const Napi::CallbackInfo &info) { char *Resize(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env); bool stretch = GetArgumentWithFallback<bool>(Arguments, "stretch", false);
bool wide = GetArgumentWithFallback<bool>(Arguments, "wide", false);
try {
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
bool stretch = obj.Has("stretch")
? obj.Get("stretch").As<Napi::Boolean>().Value()
: false;
bool wide =
obj.Has("wide") ? obj.Get("wide").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value();
VOption *options = VImage::option()->set("access", "sequential"); VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage in =
VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options) type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
@ -34,8 +25,8 @@ Napi::Value Resize(const Napi::CallbackInfo &info) {
int finalHeight; int finalHeight;
if (stretch) { if (stretch) {
out = in.resize( out =
512.0 / (double)width, in.resize(512.0 / (double)width,
VImage::option()->set("vscale", 512.0 / (double)pageHeight)); VImage::option()->set("vscale", 512.0 / (double)pageHeight));
finalHeight = 512; finalHeight = 512;
} else if (wide) { } else if (wide) {
@ -57,18 +48,9 @@ Napi::Value Resize(const Napi::CallbackInfo &info) {
out.set(VIPS_META_PAGE_HEIGHT, finalHeight); out.set(VIPS_META_PAGE_HEIGHT, finalHeight);
void *buf; void *buf;
size_t length; out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
out.write_to_buffer(("." + type).c_str(), &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
result.Set("type", type);
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Resize(const Napi::CallbackInfo& info); using std::string;
char* Resize(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,24 +1,19 @@
#include <napi.h> #include "common.h"
#include <algorithm>
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Reverse(const Napi::CallbackInfo &info) { char *Reverse(string type, char *BufferData, size_t BufferLength,
Napi::Env env = info.Env(); ArgumentMap Arguments, size_t *DataSize) {
Napi::Object result = Napi::Object::New(env); bool soos = GetArgumentWithFallback<bool>(Arguments, "soos", false);
try {
Napi::Object obj = info[1].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
bool soos =
obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
VOption *options = VOption *options =
VImage::option()->set("access", "sequential")->set("n", -1); VImage::option()->set("access", "sequential")->set("n", -1);
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", options) VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", options)
.colourspace(VIPS_INTERPRETATION_sRGB); .colourspace(VIPS_INTERPRETATION_sRGB);
int width = in.width(); int width = in.width();
@ -26,7 +21,8 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) {
int nPages = vips_image_get_n_pages(in.get_image()); int nPages = vips_image_get_n_pages(in.get_image());
vector<VImage> split; vector<VImage> split;
// todo: find a better way of getting individual frames (or at least getting the frames in reverse order) // todo: find a better way of getting individual frames (or at least getting
// the frames in reverse order)
for (int i = 0; i < nPages; i++) { for (int i = 0; i < nPages; i++) {
VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight); VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight);
split.push_back(img_frame); split.push_back(img_frame);
@ -54,19 +50,12 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) {
final.set("delay", delays); final.set("delay", delays);
void *buf; void *buf;
size_t length; final.write_to_buffer(".gif", &buf, DataSize,
final.write_to_buffer(".gif", &buf, &length,
VImage::option()->set("dither", 0)); VImage::option()->set("dither", 0));
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length)); type = "gif";
result.Set("type", "gif");
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Reverse(const Napi::CallbackInfo& info); using std::string;
char* Reverse(string type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View file

@ -1,30 +1,25 @@
#include <napi.h> #include "common.h"
#include <vips/vips8> #include <vips/vips8>
using namespace std; using namespace std;
using namespace vips; using namespace vips;
Napi::Value Sonic(const Napi::CallbackInfo &info) { char *Sonic(string *type, ArgumentMap Arguments, size_t *DataSize) {
Napi::Env env = info.Env(); string text = GetArgument<string>(Arguments, "text");
Napi::Object result = Napi::Object::New(env); string basePath = GetArgument<string>(Arguments, "basePath");
try {
Napi::Object obj = info[1].As<Napi::Object>();
string text = obj.Get("text").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
string assetPath = basePath + "assets/images/sonic.jpg"; string assetPath = basePath + "assets/images/sonic.jpg";
VImage bg = VImage::new_from_file(assetPath.c_str()); VImage bg = VImage::new_from_file(assetPath.c_str());
VImage textImage = VImage textImage =
VImage::text(("<span foreground=\"white\">" + text + "</span>").c_str(), VImage::text(
("<span foreground=\"white\">" + text + "</span>").c_str(),
VImage::option() VImage::option()
->set("rgba", true) ->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE) ->set("align", VIPS_ALIGN_CENTRE)
->set("font", "Verdana, Twemoji Color Font") ->set("font", "Verdana, Twemoji Color Font")
->set("fontfile", ->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
(basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", 542) ->set("width", 542)
->set("height", 390)) ->set("height", 390))
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390); .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390);
@ -33,18 +28,11 @@ Napi::Value Sonic(const Napi::CallbackInfo &info) {
VImage::option()->set("x", 391)->set("y", 84)); VImage::option()->set("x", 391)->set("y", 84));
void *buf; void *buf;
size_t length; out.write_to_buffer(".png", &buf, DataSize);
out.write_to_buffer(".png", &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length)); *type = "png";
result.Set("type", "png");
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
} catch (...) {
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
}
vips_error_clear(); vips_error_clear();
vips_thread_shutdown(); vips_thread_shutdown();
return result; return (char *)buf;
} }

View file

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <napi.h> #include "common.h"
Napi::Value Sonic(const Napi::CallbackInfo& info); using std::string;
char* Sonic(string *type, ArgumentMap Arguments, size_t *DataSize);