2020-07-14 14:53:51 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-02-24 19:03:36 +00:00
|
|
|
#include <vips/vips8>
|
2020-07-14 14:53:51 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-02-24 19:03:36 +00:00
|
|
|
using namespace vips;
|
2020-07-14 14:53:51 +00:00
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Watermark(const Napi::CallbackInfo &info) {
|
|
|
|
Napi::Env env = info.Env();
|
2020-07-14 14:53:51 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
|
|
|
Napi::Object obj = info[0].As<Napi::Object>();
|
2021-05-11 19:25:02 +00:00
|
|
|
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
2021-04-26 14:47:03 +00:00
|
|
|
string water = obj.Get("water").As<Napi::String>().Utf8Value();
|
2022-02-24 19:03:36 +00:00
|
|
|
int gravity = obj.Get("gravity").As<Napi::Number>().Int64Value();
|
2021-04-26 14:47:03 +00:00
|
|
|
bool resize = obj.Has("resize")
|
|
|
|
? obj.Get("resize").As<Napi::Boolean>().Value()
|
|
|
|
: false;
|
2022-03-08 17:03:25 +00:00
|
|
|
float yscale = obj.Has("yscale")
|
2022-03-09 19:30:18 +00:00
|
|
|
? obj.Get("yscale").As<Napi::Number>().FloatValue()
|
|
|
|
: false;
|
2021-04-26 14:47:03 +00:00
|
|
|
bool append = obj.Has("append")
|
|
|
|
? obj.Get("append").As<Napi::Boolean>().Value()
|
|
|
|
: false;
|
2022-06-07 21:27:47 +00:00
|
|
|
bool alpha =
|
|
|
|
obj.Has("alpha") ? obj.Get("alpha").As<Napi::Boolean>().Value() : false;
|
2021-04-26 14:47:03 +00:00
|
|
|
bool mc = obj.Has("mc") ? obj.Get("mc").As<Napi::Boolean>().Value() : false;
|
2022-04-17 15:40:56 +00:00
|
|
|
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
|
2021-04-26 14:47:03 +00:00
|
|
|
string type = obj.Get("type").As<Napi::String>().Utf8Value();
|
2021-02-25 21:09:53 +00:00
|
|
|
|
2022-02-24 19:03:36 +00:00
|
|
|
VOption *options = VImage::option()->set("access", "sequential");
|
2021-02-25 21:09:53 +00:00
|
|
|
|
2022-02-24 19:03:36 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
string merged = basePath + water;
|
|
|
|
VImage watermark = VImage::new_from_file(merged.c_str());
|
|
|
|
|
|
|
|
int width = in.width();
|
|
|
|
int page_height = vips_image_get_page_height(in.get_image());
|
|
|
|
int n_pages = vips_image_get_n_pages(in.get_image());
|
|
|
|
|
|
|
|
if (resize && append) {
|
2022-04-25 04:23:21 +00:00
|
|
|
watermark = watermark.resize((double)width / (double)watermark.width());
|
2022-03-09 19:55:19 +00:00
|
|
|
} else if (resize && yscale) {
|
2022-04-25 04:23:21 +00:00
|
|
|
watermark = watermark.resize(
|
|
|
|
(double)width / (double)watermark.width(),
|
|
|
|
VImage::option()->set("vscale", (double)(page_height * yscale) /
|
|
|
|
(double)watermark.height()));
|
2022-02-24 19:03:36 +00:00
|
|
|
} else if (resize) {
|
2022-04-25 04:23:21 +00:00
|
|
|
watermark =
|
|
|
|
watermark.resize((double)page_height / (double)watermark.height());
|
2022-02-24 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 21:27:47 +00:00
|
|
|
int x = 0, y = 0;
|
|
|
|
switch (gravity) {
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
x = (width / 2) - (watermark.width() / 2);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
x = width - watermark.width();
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
x = (width / 2) - (watermark.width() / 2);
|
|
|
|
y = (page_height / 2) - (watermark.height() / 2);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
x = width - watermark.width();
|
|
|
|
y = (page_height / 2) - (watermark.height() / 2);
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
x = (width / 2) - (watermark.width() / 2);
|
|
|
|
y = page_height - watermark.height();
|
|
|
|
break;
|
|
|
|
case 9:
|
|
|
|
x = width - watermark.width();
|
|
|
|
y = page_height - watermark.height();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-24 19:03:36 +00:00
|
|
|
vector<VImage> img;
|
|
|
|
int addedHeight = 0;
|
2022-06-07 21:27:47 +00:00
|
|
|
VImage contentAlpha;
|
|
|
|
VImage frameAlpha;
|
|
|
|
VImage bg;
|
|
|
|
VImage frame;
|
2022-02-24 19:03:36 +00:00
|
|
|
for (int i = 0; i < n_pages; i++) {
|
|
|
|
VImage img_frame =
|
|
|
|
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
|
|
|
if (append) {
|
|
|
|
VImage appended = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL,
|
|
|
|
VImage::option()->set("expand", true));
|
|
|
|
addedHeight = watermark.height();
|
|
|
|
img.push_back(appended);
|
|
|
|
} else if (mc) {
|
|
|
|
VImage padded =
|
|
|
|
img_frame.embed(0, 0, width, page_height + 15,
|
|
|
|
VImage::option()->set("background", 0xffffff));
|
|
|
|
VImage composited =
|
|
|
|
padded.composite2(watermark, VIPS_BLEND_MODE_OVER,
|
|
|
|
VImage::option()
|
|
|
|
->set("x", width - 190)
|
|
|
|
->set("y", padded.height() - 22));
|
|
|
|
addedHeight = 15;
|
|
|
|
img.push_back(composited);
|
|
|
|
} else {
|
2022-06-07 21:27:47 +00:00
|
|
|
VImage composited;
|
|
|
|
if (alpha) {
|
|
|
|
if (i == 0) {
|
|
|
|
contentAlpha = watermark.extract_band(0).embed(
|
|
|
|
x, y, width, page_height,
|
|
|
|
VImage::option()->set("extend", "white"));
|
|
|
|
frameAlpha = watermark.extract_band(1).embed(
|
|
|
|
x, y, width, page_height,
|
|
|
|
VImage::option()->set("extend", "black"));
|
|
|
|
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") {
|
|
|
|
type = "png";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VImage content =
|
|
|
|
img_frame.extract_band(0, VImage::option()->set("n", 3))
|
|
|
|
.bandjoin(contentAlpha & img_frame.extract_band(3));
|
|
|
|
|
|
|
|
composited =
|
|
|
|
content.composite2(frame, VIPS_BLEND_MODE_OVER,
|
2022-02-24 19:03:36 +00:00
|
|
|
VImage::option()->set("x", x)->set("y", y));
|
2022-06-07 21:27:47 +00:00
|
|
|
} else {
|
|
|
|
composited =
|
|
|
|
img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER,
|
|
|
|
VImage::option()->set("x", x)->set("y", y));
|
|
|
|
}
|
2022-02-24 19:03:36 +00:00
|
|
|
img.push_back(composited);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
|
|
|
final.set(VIPS_META_PAGE_HEIGHT, page_height + addedHeight);
|
|
|
|
|
|
|
|
void *buf;
|
|
|
|
size_t length;
|
|
|
|
final.write_to_buffer(
|
|
|
|
("." + type).c_str(), &buf, &length,
|
|
|
|
type == "gif" ? VImage::option()->set("dither", 0) : 0);
|
|
|
|
|
|
|
|
vips_thread_shutdown();
|
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
2022-02-24 19:03:36 +00:00
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
2021-04-26 14:47:03 +00:00
|
|
|
result.Set("type", type);
|
|
|
|
return result;
|
|
|
|
} catch (std::exception const &err) {
|
|
|
|
throw Napi::Error::New(env, err.what());
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
|
|
|
throw Napi::Error::New(env, "Unknown error");
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2022-03-08 17:03:25 +00:00
|
|
|
}
|