Add speechbubble alpha option

This commit is contained in:
Essem 2022-06-07 16:27:47 -05:00
parent 4f66bc51cd
commit f47a18495d
No known key found for this signature in database
GPG Key ID: 7D497397CC3A2A8C
3 changed files with 81 additions and 35 deletions

BIN
assets/images/speech.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

View File

@ -1,12 +1,25 @@
import ImageCommand from "../../classes/imageCommand.js"; import ImageCommand from "../../classes/imageCommand.js";
class SpeechBubbleCommand extends ImageCommand { class SpeechBubbleCommand extends ImageCommand {
params = { params() {
water: "assets/images/speechbubble.png", return {
gravity: "north", water: this.specialArgs.alpha ? "assets/images/speech.png" : "assets/images/speechbubble.png",
resize: true, gravity: "north",
yscale: 0.2, resize: true,
}; yscale: 0.2,
alpha: this.specialArgs.alpha
};
}
static init() {
super.init();
this.flags.push({
name: "alpha",
description: "Make the top of the speech bubble transparent",
type: 5
});
return this;
}
static description = "Adds a speech bubble to an image"; static description = "Adds a speech bubble to an image";
static aliases = ["speech", "sb"]; static aliases = ["speech", "sb"];

View File

@ -22,6 +22,8 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
bool append = obj.Has("append") bool append = obj.Has("append")
? obj.Get("append").As<Napi::Boolean>().Value() ? obj.Get("append").As<Napi::Boolean>().Value()
: false; : false;
bool alpha =
obj.Has("alpha") ? obj.Get("alpha").As<Napi::Boolean>().Value() : false;
bool mc = obj.Has("mc") ? obj.Get("mc").As<Napi::Boolean>().Value() : false; bool mc = obj.Has("mc") ? obj.Get("mc").As<Napi::Boolean>().Value() : false;
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value(); string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
string type = obj.Get("type").As<Napi::String>().Utf8Value(); string type = obj.Get("type").As<Napi::String>().Utf8Value();
@ -55,8 +57,40 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
watermark.resize((double)page_height / (double)watermark.height()); watermark.resize((double)page_height / (double)watermark.height());
} }
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;
}
vector<VImage> img; vector<VImage> img;
int addedHeight = 0; int addedHeight = 0;
VImage contentAlpha;
VImage frameAlpha;
VImage bg;
VImage frame;
for (int i = 0; i < n_pages; i++) { for (int i = 0; i < n_pages; i++) {
VImage img_frame = VImage img_frame =
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in; type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
@ -77,36 +111,35 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
addedHeight = 15; addedHeight = 15;
img.push_back(composited); img.push_back(composited);
} else { } else {
int x = 0, y = 0; VImage composited;
switch (gravity) { if (alpha) {
case 1: if (i == 0) {
break; contentAlpha = watermark.extract_band(0).embed(
case 2: x, y, width, page_height,
x = (width / 2) - (watermark.width() / 2); VImage::option()->set("extend", "white"));
break; frameAlpha = watermark.extract_band(1).embed(
case 3: x, y, width, page_height,
x = width - watermark.width(); VImage::option()->set("extend", "black"));
break; bg =
case 5: frameAlpha.new_from_image({0, 0, 0}).copy(VImage::option()->set(
x = (width / 2) - (watermark.width() / 2); "interpretation", VIPS_INTERPRETATION_sRGB));
y = (page_height / 2) - (watermark.height() / 2); frame = bg.bandjoin(frameAlpha);
break; if (type == "jpg" || type == "jpeg") {
case 6: type = "png";
x = width - watermark.width(); }
y = (page_height / 2) - (watermark.height() / 2); }
break; VImage content =
case 8: img_frame.extract_band(0, VImage::option()->set("n", 3))
x = (width / 2) - (watermark.width() / 2); .bandjoin(contentAlpha & img_frame.extract_band(3));
y = page_height - watermark.height();
break; composited =
case 9: content.composite2(frame, VIPS_BLEND_MODE_OVER,
x = width - watermark.width();
y = page_height - watermark.height();
break;
}
VImage composited =
img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", x)->set("y", y)); VImage::option()->set("x", x)->set("y", y));
} else {
composited =
img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", x)->set("y", y));
}
img.push_back(composited); img.push_back(composited);
} }
} }