Port caption2, start blur/colors ports
This commit is contained in:
parent
50ef9e51be
commit
8b59e70fe5
5 changed files with 129 additions and 147 deletions
|
@ -6,7 +6,7 @@ class CaptionTwoCommand extends ImageCommand {
|
||||||
params(url) {
|
params(url) {
|
||||||
const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text;
|
const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text;
|
||||||
return {
|
return {
|
||||||
caption: newArgs && newArgs.trim() ? newArgs.replaceAll("&", "\\&").replaceAll(">", "\\>").replaceAll("<", "\\<").replaceAll("\"", "\\"").replaceAll("'", "\\'").replaceAll("%", "\\%") : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" "),
|
caption: newArgs && newArgs.trim() ? newArgs.replaceAll("&", "&").replaceAll(">", ">").replaceAll("<", "<").replaceAll("\"", """).replaceAll("'", "'").replaceAll("%", "%") : words.sort(() => 0.5 - Math.random()).slice(0, Math.floor(Math.random() * words.length + 1)).join(" "),
|
||||||
top: !!this.specialArgs.top,
|
top: !!this.specialArgs.top,
|
||||||
font: this.specialArgs.font && allowedFonts.includes(this.specialArgs.font.toLowerCase()) ? this.specialArgs.font.toLowerCase() : "helvetica"
|
font: this.specialArgs.font && allowedFonts.includes(this.specialArgs.font.toLowerCase()) ? this.specialArgs.font.toLowerCase() : "helvetica"
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include <Magick++.h>
|
|
||||||
#include <napi.h>
|
#include <napi.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <vips/vips8>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Magick;
|
using namespace vips;
|
||||||
|
|
||||||
Napi::Value Blur(const Napi::CallbackInfo &info) {
|
Napi::Value Blur(const Napi::CallbackInfo &info) {
|
||||||
Napi::Env env = info.Env();
|
Napi::Env env = info.Env();
|
||||||
|
@ -18,42 +18,29 @@ Napi::Value Blur(const Napi::CallbackInfo &info) {
|
||||||
int delay =
|
int delay =
|
||||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||||
|
|
||||||
Blob blob;
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
list<Image> frames;
|
VImage in =
|
||||||
list<Image> coalesced;
|
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
||||||
try {
|
type == "gif" ? options->set("n", -1) : options)
|
||||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||||
} catch (Magick::WarningCoder &warning) {
|
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||||
cerr << "Coder Warning: " << warning.what() << endl;
|
|
||||||
} catch (Magick::Warning &warning) {
|
|
||||||
cerr << "Warning: " << warning.what() << endl;
|
|
||||||
}
|
|
||||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
|
||||||
|
|
||||||
if (sharp) {
|
// TODO: find a better way to calculate the intensity for GIFs without
|
||||||
for_each(coalesced.begin(), coalesced.end(), sharpenImage(0, 3));
|
// splitting frames
|
||||||
} else {
|
VImage out = sharp ? in.sharpen(VImage::option()->set("sigma", 3))
|
||||||
for_each(coalesced.begin(), coalesced.end(), blurImage(15));
|
: in.gaussblur(15);
|
||||||
}
|
|
||||||
|
|
||||||
for_each(coalesced.begin(), coalesced.end(), magickImage(type));
|
if (delay) out.set("delay", delay);
|
||||||
|
|
||||||
optimizeTransparency(coalesced.begin(), coalesced.end());
|
void *buf;
|
||||||
|
size_t length;
|
||||||
|
out.write_to_buffer(("." + type).c_str(), &buf, &length);
|
||||||
|
|
||||||
if (type == "gif") {
|
vips_thread_shutdown();
|
||||||
for (Image &image : coalesced) {
|
|
||||||
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
|
|
||||||
image.quantize();
|
|
||||||
if (delay != 0) image.animationDelay(delay);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
writeImages(coalesced.begin(), coalesced.end(), &blob);
|
|
||||||
|
|
||||||
Napi::Object result = Napi::Object::New(env);
|
Napi::Object result = Napi::Object::New(env);
|
||||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||||
blob.length()));
|
|
||||||
result.Set("type", type);
|
result.Set("type", type);
|
||||||
return result;
|
return result;
|
||||||
} catch (std::exception const &err) {
|
} catch (std::exception const &err) {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
|
#include <napi.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vips/vips8>
|
#include <vips/vips8>
|
||||||
#include <napi.h>
|
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace vips;
|
using namespace vips;
|
||||||
|
@ -20,12 +21,13 @@ Napi::Value Caption(const Napi::CallbackInfo &info) {
|
||||||
int delay =
|
int delay =
|
||||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||||
|
|
||||||
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::option()
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
->set("n", -1)
|
|
||||||
->set("access", "sequential"))
|
VImage in =
|
||||||
|
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
||||||
|
type == "gif" ? options->set("n", -1) : options)
|
||||||
.colourspace(VIPS_INTERPRETATION_sRGB);
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||||
if (!in.has_alpha())
|
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||||
in = in.bandjoin(255);
|
|
||||||
|
|
||||||
int width = in.width();
|
int width = in.width();
|
||||||
int size = width / 10;
|
int size = width / 10;
|
||||||
|
@ -33,13 +35,11 @@ Napi::Value Caption(const Napi::CallbackInfo &info) {
|
||||||
int n_pages = vips_image_get_n_pages(in.get_image());
|
int n_pages = vips_image_get_n_pages(in.get_image());
|
||||||
int textWidth = width - ((width / 25) * 2);
|
int textWidth = width - ((width / 25) * 2);
|
||||||
|
|
||||||
//char font_string[12 + sizeof(size)];
|
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " +
|
||||||
//sprintf(font_string, "futura bold %d", size);
|
(font != "impact" ? "bold" : "normal") + " " +
|
||||||
|
to_string(size);
|
||||||
|
|
||||||
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " + (font != "impact" ? "bold" : "normal") + " " + to_string(size);
|
string captionText = "<span background=\"white\">" + caption + "</span>";
|
||||||
|
|
||||||
string captionText =
|
|
||||||
"<span background=\"white\">" + caption + "</span>";
|
|
||||||
|
|
||||||
VImage text =
|
VImage text =
|
||||||
VImage::text(captionText.c_str(), VImage::option()
|
VImage::text(captionText.c_str(), VImage::option()
|
||||||
|
@ -47,35 +47,35 @@ Napi::Value Caption(const Napi::CallbackInfo &info) {
|
||||||
->set("align", VIPS_ALIGN_CENTRE)
|
->set("align", VIPS_ALIGN_CENTRE)
|
||||||
->set("font", font_string.c_str())
|
->set("font", font_string.c_str())
|
||||||
->set("width", textWidth));
|
->set("width", textWidth));
|
||||||
VImage captionImage = ((text == (vector<double>) {0, 0, 0, 0}).bandand())
|
VImage captionImage =
|
||||||
|
((text == (vector<double>){0, 0, 0, 0}).bandand())
|
||||||
.ifthenelse(255, text)
|
.ifthenelse(255, text)
|
||||||
.gravity(VIPS_COMPASS_DIRECTION_CENTRE,
|
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, width, text.height() + size,
|
||||||
width, text.height() + size, VImage::option()
|
VImage::option()->set("extend", "white"));
|
||||||
->set("extend", "white"));
|
|
||||||
|
|
||||||
vector<VImage> img;
|
vector<VImage> img;
|
||||||
for (int i = 0; i < n_pages; i++) {
|
for (int i = 0; i < n_pages; i++) {
|
||||||
VImage img_frame = type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
VImage img_frame =
|
||||||
VImage frame = captionImage.join(img_frame,
|
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
||||||
VIPS_DIRECTION_VERTICAL, VImage::option()
|
VImage frame = captionImage.join(
|
||||||
->set("background", 0xffffff)
|
img_frame, VIPS_DIRECTION_VERTICAL,
|
||||||
->set("expand", true));
|
VImage::option()->set("background", 0xffffff)->set("expand", true));
|
||||||
img.push_back(frame);
|
img.push_back(frame);
|
||||||
}
|
}
|
||||||
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
||||||
final.set(VIPS_META_PAGE_HEIGHT, page_height + captionImage.height());
|
final.set(VIPS_META_PAGE_HEIGHT, page_height + captionImage.height());
|
||||||
|
if (delay) final.set("delay", delay);
|
||||||
|
|
||||||
void *buf;
|
void *buf;
|
||||||
size_t length;
|
size_t length;
|
||||||
final.write_to_buffer(("." + type).c_str(), &buf, &length, VImage::option()
|
final.write_to_buffer(
|
||||||
->set("dither", 0));
|
("." + type).c_str(), &buf, &length,
|
||||||
|
type == "gif" ? VImage::option()->set("dither", 0) : 0);
|
||||||
|
|
||||||
//vips_shutdown();
|
|
||||||
vips_thread_shutdown();
|
vips_thread_shutdown();
|
||||||
|
|
||||||
Napi::Object result = Napi::Object::New(env);
|
Napi::Object result = Napi::Object::New(env);
|
||||||
result.Set("data", Napi::Buffer<char>::New(env, (char *)buf,
|
result.Set("data", Napi::Buffer<char>::New(env, (char *)buf, length));
|
||||||
length));
|
|
||||||
result.Set("type", type);
|
result.Set("type", type);
|
||||||
return result;
|
return result;
|
||||||
} catch (std::exception const &err) {
|
} catch (std::exception const &err) {
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#include <Magick++.h>
|
|
||||||
#include <napi.h>
|
#include <napi.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <vips/vips8>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Magick;
|
using namespace vips;
|
||||||
|
|
||||||
Napi::Value CaptionTwo(const Napi::CallbackInfo &info) {
|
Napi::Value CaptionTwo(const Napi::CallbackInfo &info) {
|
||||||
Napi::Env env = info.Env();
|
Napi::Env env = info.Env();
|
||||||
|
@ -20,67 +20,60 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) {
|
||||||
int delay =
|
int delay =
|
||||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||||
|
|
||||||
Blob blob;
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
list<Image> frames;
|
VImage in =
|
||||||
list<Image> coalesced;
|
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
||||||
list<Image> captioned;
|
type == "gif" ? options->set("n", -1) : options)
|
||||||
Blob caption_blob;
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||||
try {
|
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
|
||||||
} catch (Magick::WarningCoder &warning) {
|
int width = in.width();
|
||||||
cerr << "Coder Warning: " << warning.what() << endl;
|
int size = width / 13;
|
||||||
} catch (Magick::Warning &warning) {
|
int page_height = vips_image_get_page_height(in.get_image());
|
||||||
cerr << "Warning: " << warning.what() << endl;
|
int n_pages = vips_image_get_n_pages(in.get_image());
|
||||||
|
int textWidth = width - ((width / 25) * 2);
|
||||||
|
|
||||||
|
string font_string =
|
||||||
|
(font == "roboto" ? "Roboto Condensed" : font) + " " + to_string(size);
|
||||||
|
|
||||||
|
string captionText = "<span background=\"white\">" + caption + "</span>";
|
||||||
|
|
||||||
|
VImage text =
|
||||||
|
VImage::text(captionText.c_str(), VImage::option()
|
||||||
|
->set("rgba", true)
|
||||||
|
->set("font", font_string.c_str())
|
||||||
|
->set("align", VIPS_ALIGN_LOW)
|
||||||
|
->set("width", textWidth));
|
||||||
|
VImage captionImage =
|
||||||
|
((text == (vector<double>){0, 0, 0, 0}).bandand())
|
||||||
|
.ifthenelse(255, text)
|
||||||
|
.embed(width / 25, width / 25, width, text.height() + size,
|
||||||
|
VImage::option()->set("extend", "white"));
|
||||||
|
|
||||||
|
vector<VImage> img;
|
||||||
|
for (int i = 0; i < n_pages; i++) {
|
||||||
|
VImage img_frame =
|
||||||
|
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
||||||
|
VImage frame = img_frame.join(
|
||||||
|
captionImage, VIPS_DIRECTION_VERTICAL,
|
||||||
|
VImage::option()->set("background", 0xffffff)->set("expand", true));
|
||||||
|
img.push_back(frame);
|
||||||
}
|
}
|
||||||
|
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
||||||
|
final.set(VIPS_META_PAGE_HEIGHT, page_height + captionImage.height());
|
||||||
|
if (delay) final.set("delay", delay);
|
||||||
|
|
||||||
size_t width = frames.front().baseColumns();
|
void *buf;
|
||||||
string query(to_string(width - ((width / 25) * 2)) + "x");
|
size_t length;
|
||||||
Image caption_image(Geometry(query), Color("white"));
|
final.write_to_buffer(
|
||||||
caption_image.fillColor("black");
|
("." + type).c_str(), &buf, &length,
|
||||||
caption_image.font("Helvetica Neue");
|
type == "gif" ? VImage::option()->set("dither", 0) : 0);
|
||||||
caption_image.fontPointsize(width / 17);
|
|
||||||
caption_image.read("pango:<span font_family=\"" +
|
|
||||||
(font == "roboto" ? "Roboto Condensed" : font) +
|
|
||||||
"\">" + caption + "</span>");
|
|
||||||
caption_image.extent(Geometry(width, caption_image.rows() + (width / 25)),
|
|
||||||
Magick::CenterGravity);
|
|
||||||
|
|
||||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
vips_thread_shutdown();
|
||||||
|
|
||||||
for (Image &image : coalesced) {
|
|
||||||
Image appended;
|
|
||||||
list<Image> images;
|
|
||||||
image.backgroundColor("white");
|
|
||||||
if (top) {
|
|
||||||
images.push_back(caption_image);
|
|
||||||
images.push_back(image);
|
|
||||||
} else {
|
|
||||||
images.push_back(image);
|
|
||||||
images.push_back(caption_image);
|
|
||||||
}
|
|
||||||
appendImages(&appended, images.begin(), images.end(), true);
|
|
||||||
appended.repage();
|
|
||||||
appended.magick(type);
|
|
||||||
appended.animationDelay(delay == 0 ? image.animationDelay() : delay);
|
|
||||||
appended.gifDisposeMethod(Magick::BackgroundDispose);
|
|
||||||
captioned.push_back(appended);
|
|
||||||
}
|
|
||||||
|
|
||||||
optimizeTransparency(captioned.begin(), captioned.end());
|
|
||||||
|
|
||||||
if (type == "gif") {
|
|
||||||
for (Image &image : captioned) {
|
|
||||||
image.quantizeDither(false);
|
|
||||||
image.quantize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
writeImages(captioned.begin(), captioned.end(), &blob);
|
|
||||||
|
|
||||||
Napi::Object result = Napi::Object::New(env);
|
Napi::Object result = Napi::Object::New(env);
|
||||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||||
blob.length()));
|
|
||||||
result.Set("type", type);
|
result.Set("type", type);
|
||||||
return result;
|
return result;
|
||||||
} catch (std::exception const &err) {
|
} catch (std::exception const &err) {
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
#include <Magick++.h>
|
|
||||||
#include <napi.h>
|
#include <napi.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <list>
|
#include <list>
|
||||||
|
#include <vips/vips8>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
using namespace Magick;
|
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);
|
||||||
|
|
||||||
Napi::Value Colors(const Napi::CallbackInfo &info) {
|
Napi::Value Colors(const Napi::CallbackInfo &info) {
|
||||||
Napi::Env env = info.Env();
|
Napi::Env env = info.Env();
|
||||||
|
@ -20,21 +23,24 @@ Napi::Value Colors(const Napi::CallbackInfo &info) {
|
||||||
int delay =
|
int delay =
|
||||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||||
|
|
||||||
Blob blob;
|
VOption *options = VImage::option()->set("access", "sequential");
|
||||||
|
|
||||||
list<Image> frames;
|
VImage in =
|
||||||
list<Image> coalesced;
|
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
||||||
list<Image> colored;
|
type == "gif" ? options->set("n", -1) : options)
|
||||||
try {
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
|
||||||
} catch (Magick::WarningCoder &warning) {
|
VImage out;
|
||||||
cerr << "Coder Warning: " << warning.what() << endl;
|
|
||||||
} catch (Magick::Warning &warning) {
|
if (color == "blurple") {
|
||||||
cerr << "Warning: " << warning.what() << endl;
|
out = in;
|
||||||
|
} else if (color == "grayscale") {
|
||||||
|
out = in.colourspace(VIPS_INTERPRETATION_B_W);
|
||||||
|
} else if (color == "sepia") {
|
||||||
|
out = in.flatten().recomb(sepia);
|
||||||
}
|
}
|
||||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
|
||||||
|
|
||||||
for (Image &image : coalesced) {
|
/*for (Image &image : coalesced) {
|
||||||
if (color == "blurple") {
|
if (color == "blurple") {
|
||||||
image.threshold(49151.25);
|
image.threshold(49151.25);
|
||||||
image.levelColors(old ? "#7289DA" : "#5865F2", "white");
|
image.levelColors(old ? "#7289DA" : "#5865F2", "white");
|
||||||
|
@ -47,22 +53,18 @@ Napi::Value Colors(const Napi::CallbackInfo &info) {
|
||||||
image.magick(type);
|
image.magick(type);
|
||||||
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
|
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
|
||||||
colored.push_back(image);
|
colored.push_back(image);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
optimizeTransparency(colored.begin(), colored.end());
|
if (delay) out.set("delay", delay);
|
||||||
|
|
||||||
if (type == "gif") {
|
void *buf;
|
||||||
for (Image &image : colored) {
|
size_t length;
|
||||||
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
|
out.write_to_buffer(("." + type).c_str(), &buf, &length);
|
||||||
image.quantize();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
writeImages(colored.begin(), colored.end(), &blob);
|
vips_thread_shutdown();
|
||||||
|
|
||||||
Napi::Object result = Napi::Object::New(env);
|
Napi::Object result = Napi::Object::New(env);
|
||||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||||
blob.length()));
|
|
||||||
result.Set("type", type);
|
result.Set("type", type);
|
||||||
return result;
|
return result;
|
||||||
} catch (std::exception const &err) {
|
} catch (std::exception const &err) {
|
||||||
|
|
Loading…
Reference in a new issue