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";
class SpeechBubbleCommand extends ImageCommand {
params = {
water: "assets/images/speechbubble.png",
gravity: "north",
resize: true,
yscale: 0.2,
};
params() {
return {
water: this.specialArgs.alpha ? "assets/images/speech.png" : "assets/images/speechbubble.png",
gravity: "north",
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 aliases = ["speech", "sb"];

View File

@ -22,6 +22,8 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
bool append = obj.Has("append")
? obj.Get("append").As<Napi::Boolean>().Value()
: 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;
string basePath = obj.Get("basePath").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());
}
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;
int addedHeight = 0;
VImage contentAlpha;
VImage frameAlpha;
VImage bg;
VImage frame;
for (int i = 0; i < n_pages; i++) {
VImage img_frame =
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;
img.push_back(composited);
} else {
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;
}
VImage composited =
img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER,
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,
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);
}
}