Port meme
This commit is contained in:
parent
d5811503da
commit
7a31e063f8
1 changed files with 93 additions and 73 deletions
166
natives/meme.cc
166
natives/meme.cc
|
@ -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 Meme(const Napi::CallbackInfo &info) {
|
Napi::Value Meme(const Napi::CallbackInfo &info) {
|
||||||
Napi::Env env = info.Env();
|
Napi::Env env = info.Env();
|
||||||
|
@ -20,88 +20,108 @@ Napi::Value Meme(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> mid;
|
type == "gif" ? options->set("n", -1) : options)
|
||||||
Image top_text;
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
||||||
Image bottom_text;
|
if (!in.has_alpha()) in = in.bandjoin(255);
|
||||||
try {
|
|
||||||
readImages(&frames, Blob(data.Data(), data.Length()));
|
int width = in.width();
|
||||||
} catch (Magick::WarningCoder &warning) {
|
int page_height = vips_image_get_page_height(in.get_image());
|
||||||
cerr << "Coder Warning: " << warning.what() << endl;
|
int n_pages = vips_image_get_n_pages(in.get_image());
|
||||||
} catch (Magick::Warning &warning) {
|
int size = width / 9;
|
||||||
cerr << "Warning: " << warning.what() << endl;
|
int rad = 1;
|
||||||
|
|
||||||
|
string font_string = (font == "roboto" ? "Roboto Condensed" : font) + " " +
|
||||||
|
(font != "impact" ? "bold" : "normal") + " " +
|
||||||
|
to_string(size);
|
||||||
|
|
||||||
|
VImage mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128;
|
||||||
|
mask.draw_circle({255}, rad, rad, rad, VImage::option()->set("fill", true));
|
||||||
|
|
||||||
|
VImage topText;
|
||||||
|
if (top != "") {
|
||||||
|
VImage topIn = VImage::text(
|
||||||
|
("<span foreground=\"white\">" + top + "</span>").c_str(),
|
||||||
|
VImage::option()
|
||||||
|
->set("rgba", true)
|
||||||
|
->set("align", VIPS_ALIGN_CENTRE)
|
||||||
|
->set("font", font_string.c_str())
|
||||||
|
->set("width", width));
|
||||||
|
|
||||||
|
topIn = topIn.embed(rad + 10, rad + 10, (topIn.width() + 2 * rad) + 20,
|
||||||
|
(topIn.height() + 2 * rad) + 20);
|
||||||
|
|
||||||
|
VImage topOutline =
|
||||||
|
topIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
|
||||||
|
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
|
||||||
|
topOutline = (topOutline == (vector<double>){0, 0, 0, 0});
|
||||||
|
VImage topInvert = topOutline.extract_band(3).invert();
|
||||||
|
topOutline = topOutline
|
||||||
|
.extract_band(0, VImage::option()->set(
|
||||||
|
"n", topOutline.bands() - 1))
|
||||||
|
.bandjoin(topInvert);
|
||||||
|
topText = topOutline.composite2(topIn, VIPS_BLEND_MODE_OVER);
|
||||||
}
|
}
|
||||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
|
||||||
|
|
||||||
int width = coalesced.front().columns();
|
|
||||||
int dividedWidth = width / 1000;
|
|
||||||
|
|
||||||
top_text.size(Geometry(to_string(width)));
|
|
||||||
top_text.backgroundColor("none");
|
|
||||||
top_text.font("Impact");
|
|
||||||
top_text.fontPointsize(width / 12);
|
|
||||||
top_text.textGravity(Magick::CenterGravity);
|
|
||||||
top_text.read("pango:<span font_family=\"" +
|
|
||||||
(font == "roboto" ? "Roboto Condensed" : font) +
|
|
||||||
"\" weight=\"" + (font != "impact" ? "bold" : "normal") +
|
|
||||||
"\" foreground='white'>" + top + "</span>");
|
|
||||||
Image top_text_fill = top_text;
|
|
||||||
top_text_fill.channel(Magick::AlphaChannel);
|
|
||||||
top_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon");
|
|
||||||
top_text_fill.backgroundColor("black");
|
|
||||||
top_text_fill.alphaChannel(Magick::ShapeAlphaChannel);
|
|
||||||
if (dividedWidth > 1)
|
|
||||||
top_text_fill.morphology(Magick::DilateMorphology, "Octagon",
|
|
||||||
dividedWidth);
|
|
||||||
top_text.composite(top_text_fill, Magick::CenterGravity,
|
|
||||||
Magick::DstOverCompositeOp);
|
|
||||||
|
|
||||||
|
VImage bottomText;
|
||||||
if (bottom != "") {
|
if (bottom != "") {
|
||||||
bottom_text.size(Geometry(to_string(coalesced.front().columns())));
|
VImage bottomIn = VImage::text(
|
||||||
bottom_text.backgroundColor("none");
|
("<span foreground=\"white\">" + bottom + "</span>").c_str(),
|
||||||
bottom_text.font("Impact");
|
VImage::option()
|
||||||
bottom_text.fontPointsize(width / 12);
|
->set("rgba", true)
|
||||||
bottom_text.textGravity(Magick::CenterGravity);
|
->set("align", VIPS_ALIGN_CENTRE)
|
||||||
bottom_text.read("pango:<span foreground='white'>" + bottom + "</span>");
|
->set("font", font_string.c_str())
|
||||||
Image bottom_text_fill = bottom_text;
|
->set("width", width));
|
||||||
bottom_text_fill.channel(Magick::AlphaChannel);
|
bottomIn =
|
||||||
bottom_text_fill.morphology(Magick::EdgeOutMorphology, "Octagon");
|
bottomIn.embed(rad + 10, rad + 10, (bottomIn.width() + 2 * rad) + 20,
|
||||||
bottom_text_fill.backgroundColor("black");
|
(bottomIn.height() + 2 * rad) + 20);
|
||||||
bottom_text_fill.alphaChannel(Magick::ShapeAlphaChannel);
|
VImage bottomOutline =
|
||||||
if (dividedWidth > 1)
|
bottomIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
|
||||||
bottom_text_fill.morphology(Magick::DilateMorphology, "Octagon",
|
.gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
|
||||||
dividedWidth);
|
bottomOutline = (bottomOutline == (vector<double>){0, 0, 0, 0});
|
||||||
bottom_text.composite(bottom_text_fill, Magick::CenterGravity,
|
VImage bottomInvert = bottomOutline.extract_band(3).invert();
|
||||||
Magick::DstOverCompositeOp);
|
bottomOutline = bottomOutline
|
||||||
|
.extract_band(0, VImage::option()->set(
|
||||||
|
"n", bottomOutline.bands() - 1))
|
||||||
|
.bandjoin(bottomInvert);
|
||||||
|
bottomText = bottomOutline.composite2(bottomIn, VIPS_BLEND_MODE_OVER);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Image &image : coalesced) {
|
vector<VImage> img;
|
||||||
image.composite(top_text, Magick::NorthGravity, Magick::OverCompositeOp);
|
for (int i = 0; i < n_pages; i++) {
|
||||||
if (bottom != "")
|
VImage img_frame =
|
||||||
image.composite(bottom_text, Magick::SouthGravity,
|
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
|
||||||
Magick::OverCompositeOp);
|
if (top != "") {
|
||||||
image.magick(type);
|
img_frame = img_frame.composite2(
|
||||||
mid.push_back(image);
|
topText, VIPS_BLEND_MODE_OVER,
|
||||||
}
|
VImage::option()->set("x", (width / 2) - (topText.width() / 2)));
|
||||||
|
|
||||||
optimizeTransparency(mid.begin(), mid.end());
|
|
||||||
|
|
||||||
if (type == "gif") {
|
|
||||||
for (Image &image : mid) {
|
|
||||||
image.quantizeDither(false);
|
|
||||||
image.quantize();
|
|
||||||
if (delay != 0) image.animationDelay(delay);
|
|
||||||
}
|
}
|
||||||
|
if (bottom != "") {
|
||||||
|
img_frame = img_frame.composite2(
|
||||||
|
bottomText, VIPS_BLEND_MODE_OVER,
|
||||||
|
VImage::option()
|
||||||
|
->set("x", (width / 2) - (bottomText.width() / 2))
|
||||||
|
->set("y", page_height - bottomText.height()));
|
||||||
|
}
|
||||||
|
img.push_back(img_frame);
|
||||||
}
|
}
|
||||||
|
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
|
||||||
|
final.set(VIPS_META_PAGE_HEIGHT, page_height);
|
||||||
|
if (delay) final.set("delay", delay);
|
||||||
|
|
||||||
writeImages(mid.begin(), mid.end(), &blob);
|
void *buf;
|
||||||
|
size_t length;
|
||||||
|
final.write_to_buffer(
|
||||||
|
("." + type).c_str(), &buf, &length,
|
||||||
|
type == "gif" ? VImage::option()->set("dither", 0) : 0);
|
||||||
|
|
||||||
|
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