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,68 +1,54 @@
#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())
in = in.bandjoin(255); in = in.bandjoin(255);
string assetPath = basePath + "assets/images/gamexplain.png"; string assetPath = basePath + "assets/images/gamexplain.png";
VImage tmpl = VImage::new_from_file(assetPath.c_str()); VImage tmpl = VImage::new_from_file(assetPath.c_str());
int width = in.width(); int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image()); int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image()); int nPages = vips_image_get_n_pages(in.get_image());
vector<VImage> img; vector<VImage> img;
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 =
.resize(1181.0 / (double)width, img_frame
VImage::option()->set( .resize(1181.0 / (double)width,
"vscale", 571.0 / (double)pageHeight)) VImage::option()->set("vscale", 571.0 / (double)pageHeight))
.embed(10, 92, 1200, 675, .embed(10, 92, 1200, 675, VImage::option()->set("extend", "white"));
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);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, 675);
void *buf;
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 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();
} }
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, 675);
void *buf;
final.write_to_buffer(
("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
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,93 +1,79 @@
#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);
if (!in.has_alpha()) if (!in.has_alpha())
in = in.bandjoin(255); in = in.bandjoin(255);
int width = in.width(); int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image()); int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30; int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
double size = min(width, pageHeight); double size = min(width, pageHeight);
string diffPath = basePath + "assets/images/globediffuse.png"; string diffPath = basePath + "assets/images/globediffuse.png";
VImage diffuse = VImage diffuse =
VImage::new_from_file(diffPath.c_str()) VImage::new_from_file(diffPath.c_str())
.resize(size / 500.0, .resize(size / 500.0,
VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) / VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) /
255; 255;
string specPath = basePath + "assets/images/globespec.png"; string specPath = basePath + "assets/images/globespec.png";
VImage specular = VImage specular =
VImage::new_from_file(specPath.c_str()) VImage::new_from_file(specPath.c_str())
.resize(size / 500.0, .resize(size / 500.0,
VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)); VImage::option()->set("kernel", VIPS_KERNEL_CUBIC));
string distortPath = basePath + "assets/images/spheremap.png"; string distortPath = basePath + "assets/images/spheremap.png";
VImage distort = VImage distort =
(VImage::new_from_file(distortPath.c_str()) (VImage::new_from_file(distortPath.c_str())
.resize(size / 500.0, .resize(size / 500.0,
VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) / VImage::option()->set("kernel", VIPS_KERNEL_CUBIC)) /
65535) * 65535) *
size; size;
vector<VImage> img; vector<VImage> img;
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.resize( VImage resized = img_frame.resize(
size / (double)width, size / (double)width,
VImage::option()->set("vscale", size / (double)pageHeight)); VImage::option()->set("vscale", size / (double)pageHeight));
VImage rolled = img_frame.wrap( VImage rolled = img_frame.wrap(
VImage::option()->set("x", width * i / nPages)->set("y", 0)); VImage::option()->set("x", width * i / nPages)->set("y", 0));
VImage extracted = rolled.extract_band(0, VImage::option()->set("n", 3)); VImage extracted = rolled.extract_band(0, VImage::option()->set("n", 3));
VImage mapped = extracted.mapim(distort); VImage mapped = extracted.mapim(distort);
VImage composited = mapped * diffuse + specular; VImage composited = mapped * diffuse + specular;
VImage frame = composited.bandjoin(diffuse > 0.0); VImage frame = composited.bandjoin(diffuse > 0.0);
img.push_back(frame); img.push_back(frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, size);
if (type != "gif") {
vector<int> delay(30, 50);
final.set("delay", delay);
}
void *buf;
size_t length;
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();
} }
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, size);
if (type != "gif") {
vector<int> delay(30, 50);
final.set("delay", delay);
}
void *buf;
final.write_to_buffer(".gif", &buf, DataSize);
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,53 +1,40 @@
#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 { string assetPath = basePath + "assets/images/hbc.png";
Napi::Object obj = info[1].As<Napi::Object>(); VImage bg = VImage::new_from_file(assetPath.c_str());
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"; VImage text = VImage::text(
VImage bg = VImage::new_from_file(assetPath.c_str()); ".", VImage::option()->set("fontfile",
(basePath + "assets/fonts/hbc.ttf").c_str()));
text = VImage::text(
("<span letter_spacing=\"-5120\" color=\"white\">" + caption + "</span>")
.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", "PF Square Sans Pro, Twemoji Color Font 96")
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str()));
VImage text = VImage::text( VImage out = bg.composite2(text, VIPS_BLEND_MODE_OVER,
".", VImage::option()->set( VImage::option()
"fontfile", (basePath + "assets/fonts/hbc.ttf").c_str())); ->set("x", 400 - (text.width() / 2))
text = VImage::text( ->set("y", 300 - (text.height() / 2) - 8));
("<span letter_spacing=\"-5120\" color=\"white\">" + caption +
"</span>")
.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", "PF Square Sans Pro, Twemoji Color Font 96")
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str()));
VImage out = bg.composite2(text, VIPS_BLEND_MODE_OVER, void *buf;
VImage::option() out.write_to_buffer(".png", &buf, DataSize);
->set("x", 400 - (text.width() / 2))
->set("y", 300 - (text.height() / 2) - 8));
void *buf; *type = "png";
size_t length;
out.write_to_buffer(".png", &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
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,45 +1,31 @@
#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 { VOption *options = VImage::option()->set("access", "sequential");
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"); VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
VImage in = VImage noAlpha =
VImage::new_from_buffer(data.Data(), data.Length(), "", in.extract_band(0, VImage::option()->set("n", in.bands() - 1));
type == "gif" ? options->set("n", -1) : options) VImage inverted = noAlpha.invert();
.colourspace(VIPS_INTERPRETATION_sRGB); VImage out = inverted.bandjoin(in.extract_band(3));
if (!in.has_alpha()) in = in.bandjoin(255);
VImage noAlpha = void *buf;
in.extract_band(0, VImage::option()->set("n", in.bands() - 1)); out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
VImage inverted = noAlpha.invert();
VImage out = inverted.bandjoin(in.extract_band(3));
void *buf;
size_t length;
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,91 +1,70 @@
#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()) in = in.bandjoin(255);
in = in.bandjoin(255);
int width = in.width(); int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image()); int pageHeight = vips_image_get_page_height(in.get_image());
int totalHeight = in.height(); int totalHeight = in.height();
int nPages = vips_image_get_n_pages(in.get_image()); int nPages = vips_image_get_n_pages(in.get_image());
VImage final; VImage final;
if (totalHeight > 65500) { if (totalHeight > 65500) {
vector<VImage> img; vector<VImage> img;
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);
void *jpgBuf;
size_t jpgLength;
img_frame.write_to_buffer(
".jpg", &jpgBuf, &jpgLength,
VImage::option()->set("Q", quality)->set("strip", true));
VImage jpeged = VImage::new_from_buffer(jpgBuf, jpgLength, "");
jpeged.set(VIPS_META_PAGE_HEIGHT, pageHeight);
jpeged.set("delay", in.get_array_int("delay"));
img.push_back(jpeged);
}
final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
} else {
void *jpgBuf; void *jpgBuf;
size_t jpgLength; size_t jpgLength;
in.write_to_buffer( img_frame.write_to_buffer(
".jpg", &jpgBuf, &jpgLength, ".jpg", &jpgBuf, &jpgLength,
VImage::option()->set("Q", quality)->set("strip", true)); VImage::option()->set("Q", quality)->set("strip", true));
final = VImage::new_from_buffer(jpgBuf, jpgLength, ""); VImage jpeged = VImage::new_from_buffer(jpgBuf, jpgLength, "");
final.set(VIPS_META_PAGE_HEIGHT, pageHeight); jpeged.set(VIPS_META_PAGE_HEIGHT, pageHeight);
final.set("delay", in.get_array_int("delay")); jpeged.set("delay", in.get_array_int("delay"));
img.push_back(jpeged);
} }
final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
void *buf; final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
size_t length;
final.write_to_buffer(("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 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(), ""); void *jpgBuf;
void *buf; size_t jpgLength;
size_t length;
in.write_to_buffer( in.write_to_buffer(
".jpg", &buf, &length, ".jpg", &jpgBuf, &jpgLength,
VImage::option()->set("Q", quality)->set("strip", true)); VImage::option()->set("Q", quality)->set("strip", true));
final = VImage::new_from_buffer(jpgBuf, jpgLength, "");
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length)); final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
result.Set("type", "jpg"); final.set("delay", in.get_array_int("delay"));
} }
} catch (std::exception const &err) {
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException(); final.write_to_buffer(("." + type).c_str(), &buf, DataSize,
} catch (...) { type == "gif" ? VImage::option()->set("dither", 0)
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException(); : 0);
} else {
VImage in = VImage::new_from_buffer(BufferData, BufferLength, "");
in.write_to_buffer(".jpg", &buf, DataSize,
VImage::option()->set("Q", quality)->set("strip", true));
type = "jpg";
} }
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,160 +1,142 @@
#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");
string font = GetArgument<string>(Arguments, "font");
string basePath = GetArgument<string>(Arguments, "basePath");
try { VOption *options = VImage::option()->set("access", "sequential");
Napi::Object obj = info[1].As<Napi::Object>();
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"); VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
VImage in = int width = in.width();
VImage::new_from_buffer(data.Data(), data.Length(), "", int pageHeight = vips_image_get_page_height(in.get_image());
type == "gif" ? options->set("n", -1) : options) int nPages = vips_image_get_n_pages(in.get_image());
.colourspace(VIPS_INTERPRETATION_sRGB); int size = width / 9;
if (!in.has_alpha()) int dividedWidth = width / 1000;
in = in.bandjoin(255); int rad = 1;
vector<double> zeroVec = {0, 0, 0, 0};
int width = in.width(); string font_string =
int pageHeight = vips_image_get_page_height(in.get_image()); (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font " +
int nPages = vips_image_get_n_pages(in.get_image()); (font != "impact" ? "bold" : "normal") + " " + to_string(size);
int size = width / 9;
int dividedWidth = width / 1000;
int rad = 1;
vector<double> zeroVec = {0, 0, 0, 0};
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font " + VImage mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128;
(font != "impact" ? "bold" : "normal") + mask.draw_circle({255}, rad, rad, rad, VImage::option()->set("fill", true));
" " + to_string(size);
VImage mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128; VImage altMask;
mask.draw_circle({255}, rad, rad, rad, VImage::option()->set("fill", true));
VImage altMask; if (dividedWidth >= 1) {
altMask = VImage::black(dividedWidth * 2 + 1, dividedWidth * 2 + 1) + 128;
if (dividedWidth >= 1) { altMask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth,
altMask = VImage::black(dividedWidth * 2 + 1, dividedWidth * 2 + 1) + 128; VImage::option()->set("fill", true));
altMask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth,
VImage::option()->set("fill", true));
}
auto findResult = fontPaths.find(font);
if (findResult != fontPaths.end()) {
VImage::text(
".", VImage::option()->set("fontfile",
(basePath + findResult->second).c_str()));
}
VImage topText;
if (top != "") {
VImage topIn = VImage::text(
("<span foreground=\"white\">" + top + "</span>").c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", font_string.c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", width));
topIn = topIn.embed(rad + 10, rad + 10, (topIn.width() + 2 * rad) + 20,
(topIn.height() + 2 * rad) + 20);
VImage topOutline =
topIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
if (dividedWidth >= 1) {
topOutline =
topOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
}
topOutline = (topOutline == zeroVec);
VImage topInvert = topOutline.extract_band(3).invert();
topOutline =
topOutline
.extract_band(0,
VImage::option()->set("n", topOutline.bands() - 1))
.bandjoin(topInvert);
topText = topOutline.composite2(topIn, VIPS_BLEND_MODE_OVER);
}
VImage bottomText;
if (bottom != "") {
VImage bottomIn = VImage::text(
("<span foreground=\"white\">" + bottom + "</span>").c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", font_string.c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", width));
bottomIn =
bottomIn.embed(rad + 10, rad + 10, (bottomIn.width() + 2 * rad) + 20,
(bottomIn.height() + 2 * rad) + 20);
VImage bottomOutline =
bottomIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
if (dividedWidth >= 1) {
bottomOutline =
bottomOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
}
bottomOutline = (bottomOutline == zeroVec);
VImage bottomInvert = bottomOutline.extract_band(3).invert();
bottomOutline = bottomOutline
.extract_band(0, VImage::option()->set(
"n", bottomOutline.bands() - 1))
.bandjoin(bottomInvert);
bottomText = bottomOutline.composite2(bottomIn, VIPS_BLEND_MODE_OVER);
}
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
if (top != "") {
img_frame = img_frame.composite2(
topText, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", (width / 2) - (topText.width() / 2)));
}
if (bottom != "") {
img_frame = img_frame.composite2(
bottomText, VIPS_BLEND_MODE_OVER,
VImage::option()
->set("x", (width / 2) - (bottomText.width() / 2))
->set("y", pageHeight - bottomText.height()));
}
img.push_back(img_frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
void *buf;
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 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();
} }
auto findResult = fontPaths.find(font);
if (findResult != fontPaths.end()) {
VImage::text(".", VImage::option()->set(
"fontfile", (basePath + findResult->second).c_str()));
}
VImage topText;
if (top != "") {
VImage topIn = VImage::text(
("<span foreground=\"white\">" + top + "</span>").c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", font_string.c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", width));
topIn = topIn.embed(rad + 10, rad + 10, (topIn.width() + 2 * rad) + 20,
(topIn.height() + 2 * rad) + 20);
VImage topOutline =
topIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
if (dividedWidth >= 1) {
topOutline = topOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
}
topOutline = (topOutline == zeroVec);
VImage topInvert = topOutline.extract_band(3).invert();
topOutline =
topOutline
.extract_band(0, VImage::option()->set("n", topOutline.bands() - 1))
.bandjoin(topInvert);
topText = topOutline.composite2(topIn, VIPS_BLEND_MODE_OVER);
}
VImage bottomText;
if (bottom != "") {
VImage bottomIn = VImage::text(
("<span foreground=\"white\">" + bottom + "</span>").c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", font_string.c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", width));
bottomIn =
bottomIn.embed(rad + 10, rad + 10, (bottomIn.width() + 2 * rad) + 20,
(bottomIn.height() + 2 * rad) + 20);
VImage bottomOutline =
bottomIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
if (dividedWidth >= 1) {
bottomOutline =
bottomOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
}
bottomOutline = (bottomOutline == zeroVec);
VImage bottomInvert = bottomOutline.extract_band(3).invert();
bottomOutline =
bottomOutline
.extract_band(0,
VImage::option()->set("n", bottomOutline.bands() - 1))
.bandjoin(bottomInvert);
bottomText = bottomOutline.composite2(bottomIn, VIPS_BLEND_MODE_OVER);
}
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
if (top != "") {
img_frame = img_frame.composite2(
topText, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", (width / 2) - (topText.width() / 2)));
}
if (bottom != "") {
img_frame = img_frame.composite2(
bottomText, VIPS_BLEND_MODE_OVER,
VImage::option()
->set("x", (width / 2) - (bottomText.width() / 2))
->set("y", pageHeight - bottomText.height()));
}
img.push_back(img_frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
void *buf;
final.write_to_buffer(
("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
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,84 +1,67 @@
#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 { VOption *options = VImage::option()->set("access", "sequential");
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"); VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
VImage in = VImage out;
VImage::new_from_buffer(data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
VImage out; if (vertical) {
if (type == "gif") {
if (vertical) { // once again, libvips gif handling is both a blessing and a curse
if (type == "gif") { vector<VImage> img;
// once again, libvips gif handling is both a blessing and a curse int pageHeight = vips_image_get_page_height(in.get_image());
vector<VImage> img; int nPages = vips_image_get_n_pages(in.get_image());
int pageHeight = vips_image_get_page_height(in.get_image()); bool isOdd = pageHeight % 2;
int nPages = vips_image_get_n_pages(in.get_image()); for (int i = 0; i < nPages; i++) {
bool isOdd = pageHeight % 2; int x = (i * pageHeight) + (first ? 0 : (pageHeight / 2));
for (int i = 0; i < nPages; i++) { VImage cropped = in.crop(0, x, in.width(), pageHeight / 2);
int x = (i * pageHeight) + (first ? 0 : (pageHeight / 2));
VImage cropped = in.crop(0, x, in.width(), pageHeight / 2);
VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
VImage final = VImage::arrayjoin(
{first ? cropped : flipped, first ? flipped : cropped},
VImage::option()->set("across", 1));
img.push_back(final);
}
out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
out.set(VIPS_META_PAGE_HEIGHT, pageHeight - (isOdd ? 1 : 0));
} else {
VImage cropped = in.extract_area(0, 0, in.width(), in.height() / 2);
VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL); VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
out = VImage::arrayjoin({cropped, flipped}, VImage final = VImage::arrayjoin(
VImage::option()->set("across", 1)); {first ? cropped : flipped, first ? flipped : cropped},
VImage::option()->set("across", 1));
img.push_back(final);
} }
out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
out.set(VIPS_META_PAGE_HEIGHT, pageHeight - (isOdd ? 1 : 0));
} else { } else {
if (first) { VImage cropped = in.extract_area(0, 0, in.width(), in.height() / 2);
VImage cropped = in.extract_area(0, 0, in.width() / 2, in.height()); VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL); out = VImage::arrayjoin({cropped, flipped},
out = VImage::arrayjoin({cropped, flipped}); VImage::option()->set("across", 1));
} else { }
int size = in.width() / 2; } else {
VImage cropped = in.extract_area(size, 0, size, in.height()); if (first) {
VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL); VImage cropped = in.extract_area(0, 0, in.width() / 2, in.height());
out = VImage::arrayjoin({flipped, cropped}); VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL);
} out = VImage::arrayjoin({cropped, flipped});
} else {
int size = in.width() / 2;
VImage cropped = in.extract_area(size, 0, size, in.height());
VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL);
out = VImage::arrayjoin({flipped, cropped});
} }
void *buf;
size_t length;
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();
} }
void *buf;
out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
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,144 +1,126 @@
#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");
string font = GetArgument<string>(Arguments, "font");
string basePath = GetArgument<string>(Arguments, "basePath");
try { VOption *options = VImage::option()->set("access", "sequential");
Napi::Object obj = info[1].As<Napi::Object>();
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"); VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
VImage in = int width = in.width();
VImage::new_from_buffer(data.Data(), data.Length(), "", int size = width / 5;
type == "gif" ? options->set("n", -1) : options) int pageHeight = vips_image_get_page_height(in.get_image());
.colourspace(VIPS_INTERPRETATION_sRGB); int nPages = vips_image_get_n_pages(in.get_image());
if (!in.has_alpha()) int textWidth = width - ((width / 25) * 2);
in = in.bandjoin(255);
int width = in.width(); string font_string =
int size = width / 5; (font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font";
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image());
int textWidth = width - ((width / 25) * 2);
string font_string = auto findResult = fontPaths.find(font);
(font == "roboto" ? "Roboto Condensed" : font) + ", Twemoji Color Font"; if (findResult != fontPaths.end()) {
VImage::text(".", VImage::option()->set(
auto findResult = fontPaths.find(font); "fontfile", (basePath + findResult->second).c_str()));
if (findResult != fontPaths.end()) {
VImage::text(
".", VImage::option()->set("fontfile",
(basePath + findResult->second).c_str()));
}
VImage topImage;
if (top_text != "") {
string topText = "<span foreground=\"white\" background=\"black\">" +
top_text + "</span>";
topImage = VImage::text(
topText.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", (font_string + " " + to_string(size)).c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", textWidth));
}
VImage bottomImage;
if (bottom_text != "") {
string bottomText = "<span foreground=\"white\" background=\"black\">" +
bottom_text + "</span>";
bottomImage = VImage::text(
bottomText.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", (font_string + " " + to_string(size * 0.4)).c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", textWidth));
}
vector<VImage> img;
int height;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
int borderSize = max(2, width / 66);
int borderSize2 = borderSize * 0.5;
VImage bordered =
img_frame.embed(borderSize, borderSize, width + (borderSize * 2),
pageHeight + (borderSize * 2),
VImage::option()->set("extend", "black"));
VImage bordered2 = bordered.embed(
borderSize2, borderSize2, bordered.width() + (borderSize2 * 2),
bordered.height() + (borderSize2 * 2),
VImage::option()->set("extend", "white"));
int addition = width / 8;
int sideAddition = pageHeight * 0.4;
VImage bordered3 = bordered2.embed(
sideAddition / 2, addition / 2, bordered2.width() + sideAddition,
bordered2.height() + addition,
VImage::option()->set("extend", "black"));
VImage frame;
if (top_text != "") {
frame = bordered3.join(
topImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
topImage.height() + (size / 4),
VImage::option()->set("extend", "black")),
VIPS_DIRECTION_VERTICAL,
VImage::option()->set("background", 0x000000)->set("expand", true));
}
if (bottom_text != "") {
if (top_text == "")
frame = bordered3;
frame = frame.join(
bottomImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
bottomImage.height() + (size / 4),
VImage::option()->set("extend", "black")),
VIPS_DIRECTION_VERTICAL,
VImage::option()->set("background", 0x000000)->set("expand", true));
}
height = frame.height();
img.push_back(frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1))
.extract_band(0, VImage::option()->set("n", 3));
final.set(VIPS_META_PAGE_HEIGHT, height);
void *buf;
size_t length;
final.write_to_buffer(("." + type).c_str(), &buf, &length,
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();
} }
VImage topImage;
if (top_text != "") {
string topText = "<span foreground=\"white\" background=\"black\">" +
top_text + "</span>";
topImage = VImage::text(
topText.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", (font_string + " " + to_string(size)).c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", textWidth));
}
VImage bottomImage;
if (bottom_text != "") {
string bottomText = "<span foreground=\"white\" background=\"black\">" +
bottom_text + "</span>";
bottomImage = VImage::text(
bottomText.c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", (font_string + " " + to_string(size * 0.4)).c_str())
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", textWidth));
}
vector<VImage> img;
int height;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
int borderSize = max(2, width / 66);
int borderSize2 = borderSize * 0.5;
VImage bordered =
img_frame.embed(borderSize, borderSize, width + (borderSize * 2),
pageHeight + (borderSize * 2),
VImage::option()->set("extend", "black"));
VImage bordered2 = bordered.embed(borderSize2, borderSize2,
bordered.width() + (borderSize2 * 2),
bordered.height() + (borderSize2 * 2),
VImage::option()->set("extend", "white"));
int addition = width / 8;
int sideAddition = pageHeight * 0.4;
VImage bordered3 = bordered2.embed(
sideAddition / 2, addition / 2, bordered2.width() + sideAddition,
bordered2.height() + addition,
VImage::option()->set("extend", "black"));
VImage frame;
if (top_text != "") {
frame = bordered3.join(
topImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
topImage.height() + (size / 4),
VImage::option()->set("extend", "black")),
VIPS_DIRECTION_VERTICAL,
VImage::option()->set("background", 0x000000)->set("expand", true));
}
if (bottom_text != "") {
if (top_text == "")
frame = bordered3;
frame = frame.join(
bottomImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
bottomImage.height() + (size / 4),
VImage::option()->set("extend", "black")),
VIPS_DIRECTION_VERTICAL,
VImage::option()->set("background", 0x000000)->set("expand", true));
}
height = frame.height();
img.push_back(frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1))
.extract_band(0, VImage::option()->set("n", 3));
final.set(VIPS_META_PAGE_HEIGHT, height);
void *buf;
final.write_to_buffer(("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 1) : 0);
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,84 +1,70 @@
#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())
in = in.bandjoin(255); in = in.bandjoin(255);
string assetPath = basePath + "assets/images/reddit.png"; string assetPath = basePath + "assets/images/reddit.png";
VImage tmpl = VImage::new_from_file(assetPath.c_str()); VImage tmpl = VImage::new_from_file(assetPath.c_str());
int width = in.width(); int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image()); int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image()); int nPages = vips_image_get_n_pages(in.get_image());
string captionText = "<span foreground=\"white\">" + text + "</span>"; string captionText = "<span foreground=\"white\">" + text + "</span>";
VImage textImage = VImage::text( VImage textImage = VImage::text(
".", VImage::option()->set( ".", VImage::option()->set(
"fontfile", (basePath + "assets/fonts/reddit.ttf").c_str())); "fontfile", (basePath + "assets/fonts/reddit.ttf").c_str()));
textImage = VImage::text( textImage = VImage::text(
captionText.c_str(), captionText.c_str(),
VImage::option() VImage::option()
->set("rgba", true) ->set("rgba", true)
->set("font", "Roboto, Twemoji Color Font 62") ->set("font", "Roboto, Twemoji Color Font 62")
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str()) ->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("align", VIPS_ALIGN_LOW)); ->set("align", VIPS_ALIGN_LOW));
VImage composited = VImage composited =
tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER, tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", 375)->set( VImage::option()->set("x", 375)->set(
"y", (tmpl.height() - textImage.height()) - 64)); "y", (tmpl.height() - textImage.height()) - 64));
VImage watermark = VImage watermark =
composited.resize((double)width / (double)composited.width()); composited.resize((double)width / (double)composited.width());
vector<VImage> img; vector<VImage> img;
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 frame = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL, VImage frame = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL,
VImage::option()->set("expand", true)); VImage::option()->set("expand", true));
img.push_back(frame); img.push_back(frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + watermark.height());
void *buf;
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 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();
} }
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + watermark.height());
void *buf;
final.write_to_buffer(
("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
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,74 +1,56 @@
#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 { VOption *options = VImage::option()->set("access", "sequential");
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"); VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
VImage in = VImage out;
VImage::new_from_buffer(data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
VImage out; int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image());
int width = in.width(); int finalHeight;
int pageHeight = vips_image_get_page_height(in.get_image()); if (stretch) {
int nPages = vips_image_get_n_pages(in.get_image()); out =
in.resize(512.0 / (double)width,
int finalHeight; VImage::option()->set("vscale", 512.0 / (double)pageHeight));
if (stretch) { finalHeight = 512;
out = in.resize( } else if (wide) {
512.0 / (double)width, out = in.resize(9.5, VImage::option()->set("vscale", 0.5));
VImage::option()->set("vscale", 512.0 / (double)pageHeight)); finalHeight = pageHeight / 2;
finalHeight = 512; } else {
} else if (wide) { // Pain. Pain. Pain. Pain. Pain.
out = in.resize(9.5, VImage::option()->set("vscale", 0.5)); vector<VImage> img;
finalHeight = pageHeight / 2; for (int i = 0; i < nPages; i++) {
} else { VImage img_frame =
// Pain. Pain. Pain. Pain. Pain. type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
vector<VImage> img; VImage resized = img_frame.resize(0.1).resize(
for (int i = 0; i < nPages; i++) { 10, VImage::option()->set("kernel", VIPS_KERNEL_NEAREST));
VImage img_frame = img.push_back(resized);
type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in; finalHeight = resized.height();
VImage resized = img_frame.resize(0.1).resize(
10, VImage::option()->set("kernel", VIPS_KERNEL_NEAREST));
img.push_back(resized);
finalHeight = resized.height();
}
out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
} }
out.set(VIPS_META_PAGE_HEIGHT, finalHeight); out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
void *buf;
size_t length;
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();
} }
out.set(VIPS_META_PAGE_HEIGHT, finalHeight);
void *buf;
out.write_to_buffer(("." + type).c_str(), &buf, DataSize);
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,72 +1,61 @@
#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 { VOption *options =
Napi::Object obj = info[1].As<Napi::Object>(); VImage::option()->set("access", "sequential")->set("n", -1);
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 = VImage in = VImage::new_from_buffer(BufferData, BufferLength, "", options)
VImage::option()->set("access", "sequential")->set("n", -1); .colourspace(VIPS_INTERPRETATION_sRGB);
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", options) int width = in.width();
.colourspace(VIPS_INTERPRETATION_sRGB); int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image());
int width = in.width(); vector<VImage> split;
int pageHeight = vips_image_get_page_height(in.get_image()); // todo: find a better way of getting individual frames (or at least getting
int nPages = vips_image_get_n_pages(in.get_image()); // the frames in reverse order)
for (int i = 0; i < nPages; i++) {
vector<VImage> split; VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight);
// todo: find a better way of getting individual frames (or at least getting the frames in reverse order) split.push_back(img_frame);
for (int i = 0; i < nPages; i++) {
VImage img_frame = in.crop(0, i * pageHeight, width, pageHeight);
split.push_back(img_frame);
}
vector<int> delays = in.get_array_int("delay");
if (soos) {
vector<VImage> copy = split;
vector<int> copy2 = delays;
reverse(copy.begin(), copy.end());
reverse(copy2.begin(), copy2.end());
copy.pop_back();
copy2.pop_back();
copy.erase(copy.begin());
copy2.erase(copy2.begin());
split.insert(split.end(), copy.begin(), copy.end());
delays.insert(delays.end(), copy2.begin(), copy2.end());
} else {
reverse(split.begin(), split.end());
reverse(delays.begin(), delays.end());
}
VImage final = VImage::arrayjoin(split, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
final.set("delay", delays);
void *buf;
size_t length;
final.write_to_buffer(".gif", &buf, &length,
VImage::option()->set("dither", 0));
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();
} }
vector<int> delays = in.get_array_int("delay");
if (soos) {
vector<VImage> copy = split;
vector<int> copy2 = delays;
reverse(copy.begin(), copy.end());
reverse(copy2.begin(), copy2.end());
copy.pop_back();
copy2.pop_back();
copy.erase(copy.begin());
copy2.erase(copy2.begin());
split.insert(split.end(), copy.begin(), copy.end());
delays.insert(delays.end(), copy2.begin(), copy2.end());
} else {
reverse(split.begin(), split.end());
reverse(delays.begin(), delays.end());
}
VImage final = VImage::arrayjoin(split, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
final.set("delay", delays);
void *buf;
final.write_to_buffer(".gif", &buf, DataSize,
VImage::option()->set("dither", 0));
type = "gif";
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,50 +1,38 @@
#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 { string assetPath = basePath + "assets/images/sonic.jpg";
Napi::Object obj = info[1].As<Napi::Object>(); VImage bg = VImage::new_from_file(assetPath.c_str());
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"; VImage textImage =
VImage bg = VImage::new_from_file(assetPath.c_str()); VImage::text(
("<span foreground=\"white\">" + text + "</span>").c_str(),
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", "Verdana, Twemoji Color Font")
->set("fontfile", (basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", 542)
->set("height", 390))
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390);
VImage textImage = VImage out = bg.composite2(textImage, VIPS_BLEND_MODE_OVER,
VImage::text(("<span foreground=\"white\">" + text + "</span>").c_str(), VImage::option()->set("x", 391)->set("y", 84));
VImage::option()
->set("rgba", true)
->set("align", VIPS_ALIGN_CENTRE)
->set("font", "Verdana, Twemoji Color Font")
->set("fontfile",
(basePath + "assets/fonts/twemoji.otf").c_str())
->set("width", 542)
->set("height", 390))
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390);
VImage out = bg.composite2(textImage, VIPS_BLEND_MODE_OVER, void *buf;
VImage::option()->set("x", 391)->set("y", 84)); out.write_to_buffer(".png", &buf, DataSize);
void *buf; *type = "png";
size_t length;
out.write_to_buffer(".png", &buf, &length);
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
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);