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) {
|
||||
const newArgs = this.type === "classic" ? this.args.filter(item => !item.includes(url)).join(" ") : this.options.text;
|
||||
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,
|
||||
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 <iostream>
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
using namespace std;
|
||||
using namespace Magick;
|
||||
using namespace vips;
|
||||
|
||||
Napi::Value Blur(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -18,42 +18,29 @@ Napi::Value Blur(const Napi::CallbackInfo &info) {
|
|||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
Blob blob;
|
||||
VOption *options = VImage::option()->set("access", "sequential");
|
||||
|
||||
list<Image> frames;
|
||||
list<Image> coalesced;
|
||||
try {
|
||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
||||
} catch (Magick::WarningCoder &warning) {
|
||||
cerr << "Coder Warning: " << warning.what() << endl;
|
||||
} catch (Magick::Warning &warning) {
|
||||
cerr << "Warning: " << warning.what() << endl;
|
||||
}
|
||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
||||
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);
|
||||
|
||||
if (sharp) {
|
||||
for_each(coalesced.begin(), coalesced.end(), sharpenImage(0, 3));
|
||||
} else {
|
||||
for_each(coalesced.begin(), coalesced.end(), blurImage(15));
|
||||
}
|
||||
// TODO: find a better way to calculate the intensity for GIFs without
|
||||
// splitting frames
|
||||
VImage out = sharp ? in.sharpen(VImage::option()->set("sigma", 3))
|
||||
: 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") {
|
||||
for (Image &image : coalesced) {
|
||||
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
|
||||
image.quantize();
|
||||
if (delay != 0) image.animationDelay(delay);
|
||||
}
|
||||
}
|
||||
|
||||
writeImages(coalesced.begin(), coalesced.end(), &blob);
|
||||
vips_thread_shutdown();
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
||||
blob.length()));
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||
result.Set("type", type);
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <vips/vips8>
|
||||
#include <napi.h>
|
||||
|
||||
using namespace std;
|
||||
using namespace vips;
|
||||
|
@ -20,12 +21,13 @@ Napi::Value Caption(const Napi::CallbackInfo &info) {
|
|||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", VImage::option()
|
||||
->set("n", -1)
|
||||
->set("access", "sequential"))
|
||||
VOption *options = VImage::option()->set("access", "sequential");
|
||||
|
||||
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);
|
||||
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||
|
||||
int width = in.width();
|
||||
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 textWidth = width - ((width / 25) * 2);
|
||||
|
||||
//char font_string[12 + sizeof(size)];
|
||||
//sprintf(font_string, "futura bold %d", size);
|
||||
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " +
|
||||
(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(captionText.c_str(), VImage::option()
|
||||
|
@ -47,35 +47,35 @@ Napi::Value Caption(const Napi::CallbackInfo &info) {
|
|||
->set("align", VIPS_ALIGN_CENTRE)
|
||||
->set("font", font_string.c_str())
|
||||
->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)
|
||||
.gravity(VIPS_COMPASS_DIRECTION_CENTRE,
|
||||
width, text.height() + size, VImage::option()
|
||||
->set("extend", "white"));
|
||||
.gravity(VIPS_COMPASS_DIRECTION_CENTRE, 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 = captionImage.join(img_frame,
|
||||
VIPS_DIRECTION_VERTICAL, VImage::option()
|
||||
->set("background", 0xffffff)
|
||||
->set("expand", true));
|
||||
VImage img_frame =
|
||||
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
||||
VImage frame = captionImage.join(
|
||||
img_frame, 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);
|
||||
|
||||
void *buf;
|
||||
size_t length;
|
||||
final.write_to_buffer(("." + type).c_str(), &buf, &length, VImage::option()
|
||||
->set("dither", 0));
|
||||
final.write_to_buffer(
|
||||
("." + type).c_str(), &buf, &length,
|
||||
type == "gif" ? VImage::option()->set("dither", 0) : 0);
|
||||
|
||||
//vips_shutdown();
|
||||
vips_thread_shutdown();
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::New(env, (char *)buf,
|
||||
length));
|
||||
result.Set("data", Napi::Buffer<char>::New(env, (char *)buf, length));
|
||||
result.Set("type", type);
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
using namespace std;
|
||||
using namespace Magick;
|
||||
using namespace vips;
|
||||
|
||||
Napi::Value CaptionTwo(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -20,67 +20,60 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) {
|
|||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
Blob blob;
|
||||
VOption *options = VImage::option()->set("access", "sequential");
|
||||
|
||||
list<Image> frames;
|
||||
list<Image> coalesced;
|
||||
list<Image> captioned;
|
||||
Blob caption_blob;
|
||||
try {
|
||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
||||
} catch (Magick::WarningCoder &warning) {
|
||||
cerr << "Coder Warning: " << warning.what() << endl;
|
||||
} catch (Magick::Warning &warning) {
|
||||
cerr << "Warning: " << warning.what() << endl;
|
||||
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);
|
||||
|
||||
int width = in.width();
|
||||
int size = width / 13;
|
||||
int page_height = vips_image_get_page_height(in.get_image());
|
||||
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();
|
||||
string query(to_string(width - ((width / 25) * 2)) + "x");
|
||||
Image caption_image(Geometry(query), Color("white"));
|
||||
caption_image.fillColor("black");
|
||||
caption_image.font("Helvetica Neue");
|
||||
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);
|
||||
void *buf;
|
||||
size_t length;
|
||||
final.write_to_buffer(
|
||||
("." + type).c_str(), &buf, &length,
|
||||
type == "gif" ? VImage::option()->set("dither", 0) : 0);
|
||||
|
||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
||||
|
||||
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);
|
||||
vips_thread_shutdown();
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
||||
blob.length()));
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||
result.Set("type", type);
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
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::Env env = info.Env();
|
||||
|
@ -20,21 +23,24 @@ Napi::Value Colors(const Napi::CallbackInfo &info) {
|
|||
int delay =
|
||||
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
||||
|
||||
Blob blob;
|
||||
VOption *options = VImage::option()->set("access", "sequential");
|
||||
|
||||
list<Image> frames;
|
||||
list<Image> coalesced;
|
||||
list<Image> colored;
|
||||
try {
|
||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
||||
} catch (Magick::WarningCoder &warning) {
|
||||
cerr << "Coder Warning: " << warning.what() << endl;
|
||||
} catch (Magick::Warning &warning) {
|
||||
cerr << "Warning: " << warning.what() << endl;
|
||||
VImage in =
|
||||
VImage::new_from_buffer(data.Data(), data.Length(), "",
|
||||
type == "gif" ? options->set("n", -1) : options)
|
||||
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||
|
||||
VImage out;
|
||||
|
||||
if (color == "blurple") {
|
||||
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") {
|
||||
image.threshold(49151.25);
|
||||
image.levelColors(old ? "#7289DA" : "#5865F2", "white");
|
||||
|
@ -47,22 +53,18 @@ Napi::Value Colors(const Napi::CallbackInfo &info) {
|
|||
image.magick(type);
|
||||
image.animationDelay(delay == 0 ? image.animationDelay() : delay);
|
||||
colored.push_back(image);
|
||||
}
|
||||
}*/
|
||||
|
||||
optimizeTransparency(colored.begin(), colored.end());
|
||||
if (delay) out.set("delay", delay);
|
||||
|
||||
if (type == "gif") {
|
||||
for (Image &image : colored) {
|
||||
image.quantizeDitherMethod(FloydSteinbergDitherMethod);
|
||||
image.quantize();
|
||||
}
|
||||
}
|
||||
void *buf;
|
||||
size_t length;
|
||||
out.write_to_buffer(("." + type).c_str(), &buf, &length);
|
||||
|
||||
writeImages(colored.begin(), colored.end(), &blob);
|
||||
vips_thread_shutdown();
|
||||
|
||||
Napi::Object result = Napi::Object::New(env);
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
||||
blob.length()));
|
||||
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
||||
result.Set("type", type);
|
||||
return result;
|
||||
} catch (std::exception const &err) {
|
||||
|
|
Loading…
Reference in a new issue