Some fixes and whatnot

This commit is contained in:
Essem 2022-11-27 14:52:40 -06:00
parent 330dd67063
commit 7ee571dd2b
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
30 changed files with 699 additions and 589 deletions

View file

@ -11,7 +11,7 @@ import { createRequire } from "module";
import EventEmitter from "events";
const nodeRequire = createRequire(import.meta.url);
const magick = nodeRequire(`../build/${process.env.DEBUG && process.env.DEBUG === "true" ? "Debug" : "Release"}/image.node`);
const img = nodeRequire(`../build/${process.env.DEBUG && process.env.DEBUG === "true" ? "Debug" : "Release"}/image.node`);
const Rerror = 0x01;
const Tqueue = 0x02;
@ -92,7 +92,7 @@ wss.on("connection", (ws, request) => {
const cur = Buffer.alloc(2);
cur.writeUInt16LE(jobAmount);
const formats = {};
for (const cmd of Object.keys(magick)) {
for (const cmd of img.funcs) {
formats[cmd] = ["image/png", "image/gif", "image/jpeg", "image/webp"];
}
const init = Buffer.concat([Buffer.from([Rinit]), Buffer.from([0x00, 0x00]), num, cur, Buffer.from(JSON.stringify(formats))]);

View file

@ -1,16 +1,15 @@
#include "common.h"
#include <vips/vips8>
#include <string>
#include <map>
#include <string>
#include <vips/vips8>
using namespace std;
using namespace vips;
char* Blur(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
string caption = Arguments["caption"];
string font = MAP_GET(Arguments, "font");
bool sharp = MAP_GET(Arguments, "sharp") == "true";
char *Blur(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
bool sharp = MAP_GET(Arguments, "sharp", bool);
VOption *options = VImage::option()->set("access", "sequential");
VImage in =
@ -18,12 +17,13 @@ char* Blur(string type, char* BufferData, size_t BufferLength, map<string, strin
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
if (!in.has_alpha())
in = in.bandjoin(255);
// TODO: find a better way to calculate the intensity for GIFs without
// splitting frames
VImage out = sharp ? in.sharpen(VImage::option()->set("sigma", 3))
: in.gaussblur(15);
VImage out =
sharp ? in.sharpen(VImage::option()->set("sigma", 3)) : in.gaussblur(15);
void *buf;
out.write_to_buffer(("." + type).c_str(), &buf, DataSize);

View file

@ -1,8 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
using std::string;
using std::any;
using std::map;
using std::string;
char* Blur(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Blur(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -7,10 +7,12 @@
using namespace std;
using namespace vips;
char* Caption(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Caption(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
string caption = Arguments["caption"];
string font = MAP_GET(Arguments, "font");
string caption = MAP_GET(Arguments, "caption", string);
string font = MAP_GET(Arguments, "font", string);
string basePath = MAP_GET(Arguments, "basePath", string);
VOption *options = VImage::option()->set("access", "sequential");
@ -34,11 +36,20 @@ char* Caption(string type, char* BufferData, size_t BufferLength, map<string, st
string captionText = "<span background=\"white\">" + caption + "</span>";
VImage text =
VImage::text(captionText.c_str(), VImage::option()
VImage text;
auto findResult = fontPaths.find(font);
if (findResult != fontPaths.end()) {
text = VImage::text(
".", VImage::option()->set("fontfile",
(basePath + findResult->second).c_str()));
}
text = VImage::text(
captionText.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", textWidth));
VImage captionImage =
((text == (vector<double>){0, 0, 0, 0}).bandand())

View file

@ -1,7 +1,11 @@
#pragma once
#include <any>
#include <map>
#include <string>
#include <napi.h>
char* Caption(std::string type, char* BufferData, size_t BufferLength, std::map<std::string, std::string> Arguments, size_t* DataSize);
using std::any;
using std::map;
using std::string;
char* Caption(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,19 +1,19 @@
#include "common.h"
#include "common.h"
#include <string>
#include <map>
#include <string>
#include <vips/vips8>
using namespace std;
using namespace vips;
char* CaptionTwo(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *CaptionTwo(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
bool top = MAP_GET(Arguments, "top") == "true";
string caption = Arguments["caption"];
string font = MAP_GET(Arguments, "font");
string basePath = MAP_GET(Arguments, "basePath");
bool top = MAP_GET(Arguments, "top", bool);
string caption = MAP_GET(Arguments, "caption", string);
string font = MAP_GET(Arguments, "font", string);
string basePath = MAP_GET(Arguments, "basePath", string);
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -1,7 +1,11 @@
#pragma once
#include <any>
#include <map>
#include <string>
#include <napi.h>
char* CaptionTwo(std::string type, char* BufferData, size_t BufferLength, std::map<std::string, std::string> Arguments, size_t* DataSize);
using std::any;
using std::map;
using std::string;
char* CaptionTwo(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,15 +1,17 @@
#include "common.h"
#include <Magick++.h>
#include <napi.h>
#include <cstring>
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <map>
#include <string>
using namespace std;
using namespace Magick;
char* Circle(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Circle(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
Blob blob;
@ -44,5 +46,8 @@ char* Circle(string type, char* BufferData, size_t BufferLength, map<string, str
*DataSize = blob.length();
return (char*) blob.data();
// workaround because the data is tied to the blob
char *data = (char *)malloc(*DataSize);
memcpy(data, blob.data(), *DataSize);
return data;
}

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <string>
#include <any>
#include <map>
#include <string>
using std::string;
using std::any;
using std::map;
using std::string;
char* Circle(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Circle(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,8 +1,8 @@
#include <napi.h>
#include "common.h"
#include <vips/vips8>
#include <map>
#include <string>
#include <vips/vips8>
using namespace std;
using namespace vips;
@ -10,9 +10,10 @@ using namespace vips;
VImage sepia = VImage::new_matrixv(3, 3, 0.3588, 0.7044, 0.1368, 0.2990, 0.5870,
0.1140, 0.2392, 0.4696, 0.0912);
char* Colors(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Colors(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
string color = Arguments["color"];
string color = MAP_GET(Arguments, "color", string);
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
using std::any;
using std::map;
using std::string;
char* Colors(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Colors(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,10 +1,12 @@
#pragma once
#include <any>
#include <string>
#include <unordered_map>
#define MAP_HAS(ARRAY, KEY) (ARRAY.count(KEY) > 0)
#define MAP_GET(ARRAY, KEY) (MAP_HAS(ARRAY, KEY) ? ARRAY.at(KEY) : NULL) // C++ has forced my hand
#define MAP_GET(ARRAY, KEY, TYPE) (MAP_HAS(ARRAY, KEY) ? any_cast<TYPE>(ARRAY.at(KEY)) : NULL) // C++ has forced my hand
#define MAP_GET_FALLBACK(ARRAY, KEY, TYPE, FALLBACK) (MAP_HAS(ARRAY, KEY) ? any_cast<TYPE>(ARRAY.at(KEY)) : FALLBACK)
const std::unordered_map<std::string, std::string> fontPaths {
{"futura", "assets/fonts/caption.otf"},

View file

@ -1,4 +1,4 @@
#include <napi.h>
#include "common.h"
#include <map>
#include <string>
#include <vips/vips8>
@ -6,7 +6,8 @@
using namespace std;
using namespace vips;
char* Crop(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Crop(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential");

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
using std::any;
using std::map;
using std::string;
char* Crop(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Crop(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,10 +1,12 @@
#include "common.h"
#include <map>
#include <vips/vips8>
using namespace std;
using namespace vips;
char* Deepfry(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Deepfry(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
VOption *options = VImage::option()->set("access", "sequential");
@ -53,8 +55,7 @@ char* Deepfry(string type, char* BufferData, size_t BufferLength, map<string, st
void *buf;
final.write_to_buffer(("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)
: 0);
type == "gif" ? VImage::option()->set("dither", 0) : 0);
vips_error_clear();
vips_thread_shutdown();

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
using std::any;
using std::map;
using std::string;
char* Deepfry(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Deepfry(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -2,18 +2,20 @@
#include <Magick++.h>
#include <cstring>
#include <iostream>
#include <string>
#include <map>
#include <list>
#include <map>
#include <string>
using namespace std;
using namespace Magick;
char* Explode(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Explode(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
int amount = stoi(Arguments.at("amount"));
int delay = MAP_HAS(Arguments, "delay") ? stoi(Arguments.at("delay")) : 0;
int amount = MAP_GET(Arguments, "amount", int);
int delay = MAP_GET_FALLBACK(Arguments, "delay", int, 0);
Blob blob;
@ -49,5 +51,8 @@ char* Explode(string type, char* BufferData, size_t BufferLength, map<string, st
*DataSize = blob.length();
return (char*) blob.data();
// workaround because the data is tied to the blob
char *data = (char *)malloc(*DataSize);
memcpy(data, blob.data(), *DataSize);
return data;
}

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <string>
#include <any>
#include <map>
#include <string>
using std::any;
using std::map;
using std::string;
char* Explode(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Explode(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,16 +1,17 @@
#include <napi.h>
#include "common.h"
#include <vips/vips8>
#include <string>
#include <map>
#include <string>
#include <vips/vips8>
using namespace std;
using namespace vips;
char* Flag(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Flag(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
string overlay = Arguments["overlay"];
string basePath = Arguments["basePath"];
string overlay = MAP_GET(Arguments, "overlay", string);
string basePath = MAP_GET(Arguments, "basePath", string);
VOption *options = VImage::option()->set("access", "sequential");
@ -19,7 +20,8 @@ char* Flag(string type, char* BufferData, size_t BufferLength, map<string, strin
type == "gif" ? options->set("n", -1) : options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
if (!in.has_alpha())
in = in.bandjoin(255);
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
@ -29,8 +31,8 @@ char* Flag(string type, char* BufferData, size_t BufferLength, map<string, strin
VImage overlayInput = VImage::new_from_file(assetPath.c_str());
VImage overlayImage = overlayInput.resize(
(double)width / (double)overlayInput.width(),
VImage::option()->set(
"vscale", (double)pageHeight / (double)overlayInput.height()));
VImage::option()->set("vscale", (double)pageHeight /
(double)overlayInput.height()));
if (!overlayImage.has_alpha()) {
overlayImage = overlayImage.bandjoin(127);
} else {
@ -53,7 +55,8 @@ char* Flag(string type, char* BufferData, size_t BufferLength, map<string, strin
void *buf;
final.write_to_buffer(
("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0);
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
vips_error_clear();
vips_thread_shutdown();

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
using std::any;
using std::map;
using std::string;
char* Flag(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Flag(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -7,17 +7,19 @@
using namespace std;
using namespace vips;
char* Flip(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Flip(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
bool flop = MAP_GET(Arguments, "flop") == "true";
bool flop = MAP_GET(Arguments, "flop", bool);
VOption *options = VImage::option()->set("access", "sequential");
VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? VImage::option()->set("n", -1)->set("access", "sequential") : 0)
VImage in = VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif"
? VImage::option()->set("n", -1)->set(
"access", "sequential")
: 0)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
if (!in.has_alpha())
in = in.bandjoin(255);
VImage out;
if (flop) {

View file

@ -1,10 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
using std::any;
using std::map;
using std::string;
char* Flip(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
char* Flip(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -51,11 +51,12 @@ Napi::Value Freeze(const Napi::CallbackInfo &info) {
} else if (frame >= 0 && !loop) {
VOption *options = VImage::option()->set("access", "sequential");
VImage in = VImage::new_from_buffer(
data.Data(), data.Length(), "",
type == "gif" ? options->set("n", -1) : options)
VImage in = 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);
if (!in.has_alpha())
in = in.bandjoin(255);
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = vips_image_get_n_pages(in.get_image());

View file

@ -21,7 +21,8 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) {
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);
if (!in.has_alpha())
in = in.bandjoin(255);
string assetPath = basePath + "assets/images/gamexplain.png";
VImage tmpl = VImage::new_from_file(assetPath.c_str());
@ -50,7 +51,8 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) {
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0);
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);

View file

@ -23,7 +23,8 @@ Napi::Value Globe(const Napi::CallbackInfo &info) {
type == "gif" ? options->set("n", -1)->set("access", "sequential")
: options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
if (!in.has_alpha())
in = in.bandjoin(255);
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());

View file

@ -3,6 +3,8 @@
#include <napi.h>
#include <map>
#include <string>
#include <iostream>
#include <any>
#include "blur.h"
#include "colors.h"
@ -42,7 +44,6 @@
#include "watermark.h"
#include "whisper.h"
#include "zamn.h"
#include <iostream>
#ifdef _WIN32
#include <Magick++.h>
@ -51,10 +52,10 @@
using namespace std;
std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize)> FunctionMap = {
{"caption", &Caption},
{"caption2", &CaptionTwo},
std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize)> FunctionMap = {
{"blur", &Blur},
{"caption", &Caption},
{"captionTwo", &CaptionTwo},
{"circle", &Circle},
{"colors", &Colors},
{"crop", &Crop},
@ -62,8 +63,8 @@ std::map<std::string, char* (*)(string type, char* BufferData, size_t BufferLeng
{"explode", &Explode},
{"flag", &Flag},
{"flip", &Flip},
{"watermark", &Watermark},
{"uncaption", &Uncaption}
{"uncaption", &Uncaption},
{"watermark", &Watermark}
};
std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFunctionMap = {
@ -94,6 +95,17 @@ std::map<std::string, Napi::Value (*)(const Napi::CallbackInfo &info)> OldFuncti
{"zamn", Zamn}
};
bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
return env.Global()
.Get("Number")
.ToObject()
.Get("isInteger")
.As<Napi::Function>()
.Call({num})
.ToBoolean()
.Value();
}
Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object result = Napi::Object::New(env);
@ -106,7 +118,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
Napi::Array properties = obj.GetPropertyNames();
std::map<string, string> Arguments;
std::map<string, any> Arguments;
for (unsigned int i = 0; i < properties.Length(); i++) {
string property = properties.Get(uint32_t(i)).As<Napi::String>().Utf8Value();
@ -115,7 +127,21 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info) {
continue;
}
Arguments[property] = obj.Get(property).ToString().As<Napi::String>().Utf8Value();
auto val = obj.Get(property);
if (val.IsBoolean()) {
Arguments[property] = val.ToBoolean().Value();
} else if (val.IsString()) {
Arguments[property] = val.ToString().As<Napi::String>().Utf8Value();
} else if (val.IsNumber()) {
auto num = val.ToNumber();
if (isNapiValueInt(env, num)) {
Arguments[property] = num.Int32Value();
} else {
Arguments[property] = num.FloatValue();
}
} else {
Arguments[property] = val;
}
}
size_t length = 0;
@ -159,6 +185,21 @@ Napi::Object Init(Napi::Env env, Napi::Object exports){
vips_error_exit(NULL);
exports.Set(Napi::String::New(env, "image"), Napi::Function::New(env, ProcessImage)); // new function handler
Napi::Array arr = Napi::Array::New(env);
size_t i = 0;
for (auto const& imap: FunctionMap) {
Napi::HandleScope scope(env);
arr[i] = Napi::String::New(env, imap.first);
i++;
}
for(auto const& imap: OldFunctionMap) {
Napi::HandleScope scope(env);
arr[i] = Napi::String::New(env, imap.first);
i++;
}
exports.Set(Napi::String::New(env, "funcs"), arr);
return exports;
}

View file

@ -1,24 +1,26 @@
#include "common.h"
#include <vips/vips8>
#include <map>
#include <vips/vips8>
using namespace std;
using namespace vips;
char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Uncaption(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
float tolerance = MAP_HAS(Arguments, "tolerance")
? stof(Arguments["tolerance"])
: 0.5;
float tolerance = MAP_GET_FALLBACK(Arguments, "tolerance", float, 0.5);
VOption *options = VImage::option();
VImage in =
VImage::new_from_buffer(BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1)->set("access", "sequential") : options)
VImage::new_from_buffer(
BufferData, BufferLength, "",
type == "gif" ? options->set("n", -1)->set("access", "sequential")
: options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha()) in = in.bandjoin(255);
if (!in.has_alpha())
in = in.bandjoin(255);
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
@ -37,8 +39,7 @@ char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string,
top = 0;
}
for (int i = 0; i < nPages; i++) {
VImage img_frame =
in.crop(0, (i * pageHeight) + top, width, newHeight);
VImage img_frame = in.crop(0, (i * pageHeight) + top, width, newHeight);
img.push_back(img_frame);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
@ -47,8 +48,8 @@ char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string,
void *buf;
final.write_to_buffer(
("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0);
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
vips_error_clear();
vips_thread_shutdown();

View file

@ -1,5 +1,11 @@
#pragma once
#include <any>
#include <map>
#include <string>
char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
using std::any;
using std::map;
using std::string;
char* Uncaption(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);

View file

@ -1,27 +1,29 @@
#include "common.h"
#include <vips/vips8>
#include <map>
#include <vips/vips8>
using namespace std;
using namespace vips;
char* Watermark(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize) {
char *Watermark(string type, char *BufferData, size_t BufferLength,
map<string, any> Arguments, size_t *DataSize) {
string water = Arguments["water"];
int gravity = stoi(Arguments["gravity"]);
string water = MAP_GET(Arguments, "water", string);
int gravity = MAP_GET(Arguments, "gravity", int);
bool resize = MAP_HAS(Arguments, "resize") ? Arguments["resize"] == "true" : false;;
float yscale = MAP_HAS(Arguments, "yscale") ? stof(Arguments["yscale"]) : false;
bool resize = MAP_GET_FALLBACK(Arguments, "resize", bool, false);
;
float yscale = MAP_GET_FALLBACK(Arguments, "yscale", float, false);
bool append = MAP_HAS(Arguments, "append") ? Arguments["append"] == "true" : false;
bool append = MAP_GET_FALLBACK(Arguments, "append", bool, false);
bool alpha = MAP_HAS(Arguments, "alpha") ? Arguments["alpha"] == "true" : false;
bool flip = MAP_HAS(Arguments, "flip") ? Arguments["flip"] == "true" : false;
bool alpha = MAP_GET_FALLBACK(Arguments, "alpha", bool, false);
bool flip = MAP_GET_FALLBACK(Arguments, "flip", bool, false);
bool mc = MAP_HAS(Arguments, "mc");
string basePath = Arguments["basePath"];
string basePath = MAP_GET(Arguments, "basePath", string);
VOption *options = VImage::option()->set("access", "sequential");
@ -29,7 +31,8 @@ char* Watermark(string type, char* BufferData, size_t BufferLength, map<string,
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);
if (!in.has_alpha())
in = in.bandjoin(255);
string merged = basePath + water;
VImage watermark = VImage::new_from_file(merged.c_str());
@ -117,8 +120,7 @@ char* Watermark(string type, char* BufferData, size_t BufferLength, map<string,
frameAlpha = watermark.extract_band(1).embed(
x, y, width, pageHeight,
VImage::option()->set("extend", "black"));
bg =
frameAlpha.new_from_image({0, 0, 0}).copy(VImage::option()->set(
bg = frameAlpha.new_from_image({0, 0, 0}).copy(VImage::option()->set(
"interpretation", VIPS_INTERPRETATION_sRGB));
frame = bg.bandjoin(frameAlpha);
if (type == "jpg" || type == "jpeg") {
@ -146,7 +148,8 @@ char* Watermark(string type, char* BufferData, size_t BufferLength, map<string,
void *buf;
final.write_to_buffer(
("." + type).c_str(), &buf, DataSize,
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1) : 0);
type == "gif" ? VImage::option()->set("dither", 0)->set("reoptimise", 1)
: 0);
vips_error_clear();
vips_thread_shutdown();

View file

@ -1,5 +1,11 @@
#pragma once
#include <napi.h>
#include <any>
#include <map>
#include <string>
char* Watermark(string type, char* BufferData, size_t BufferLength, map<string, string> Arguments, size_t* DataSize);
using std::any;
using std::map;
using std::string;
char* Watermark(string type, char* BufferData, size_t BufferLength, map<string, any> Arguments, size_t* DataSize);