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 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 {
water: this.specialArgs.alpha ? "assets/images/speech.png" : "assets/images/speechbubble.png",
gravity: "north", gravity: "north",
resize: true, resize: true,
yscale: 0.2, 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,28 +57,6 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
watermark.resize((double)page_height / (double)watermark.height()); watermark.resize((double)page_height / (double)watermark.height());
} }
vector<VImage> img;
int addedHeight = 0;
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 {
int x = 0, y = 0; int x = 0, y = 0;
switch (gravity) { switch (gravity) {
case 1: case 1:
@ -104,9 +84,62 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
y = page_height - watermark.height(); y = page_height - watermark.height();
break; 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;
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 = 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 {
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, img_frame.composite2(watermark, VIPS_BLEND_MODE_OVER,
VImage::option()->set("x", x)->set("y", y)); VImage::option()->set("x", x)->set("y", y));
}
img.push_back(composited); img.push_back(composited);
} }
} }