Port watermark, homebrew, gamexplain, and flag

This commit is contained in:
Essem 2022-02-24 13:03:36 -06:00
parent 0fa02a9e6a
commit 112f15e9f2
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
5 changed files with 190 additions and 99 deletions

View file

@ -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 Flag(const Napi::CallbackInfo &info) { Napi::Value Flag(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
@ -19,50 +19,47 @@ Napi::Value Flag(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");
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 page_height = vips_image_get_page_height(in.get_image());
int n_pages = vips_image_get_n_pages(in.get_image());
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
Image watermark;
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;
}
string assetPath = basePath + overlay; string assetPath = basePath + overlay;
watermark.read(assetPath); VImage overlayImage = VImage::new_from_file(assetPath.c_str()).thumbnail_image(width, VImage::option()->set("height", page_height)->set("size", VIPS_SIZE_FORCE));
watermark.alphaChannel(Magick::SetAlphaChannel); if (!overlayImage.has_alpha()) {
watermark.evaluate(Magick::AlphaChannel, Magick::MultiplyEvaluateOperator, overlayImage = overlayImage.bandjoin(127);
0.5); } else {
string query(to_string(frames.front().baseColumns()) + "x" + overlayImage = overlayImage * vector<double>{ 1, 1, 1, 0.5 }; // this is a pretty cool line, just saying
to_string(frames.front().baseRows()) + "!");
watermark.scale(Geometry(query));
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) {
image.composite(watermark, Magick::NorthGravity, Magick::OverCompositeOp);
image.magick(type);
mid.push_back(image);
} }
optimizeTransparency(mid.begin(), mid.end()); vector<VImage> img;
for (int i = 0; i < n_pages; i++) {
if (type == "gif") { VImage img_frame =
for (Image &image : mid) { type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
image.quantizeDither(false); VImage composited = img_frame.composite2(overlayImage, VIPS_BLEND_MODE_OVER);
image.quantize(); img.push_back(composited);
if (delay != 0) image.animationDelay(delay);
}
} }
writeImages(mid.begin(), mid.end(), &blob); VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, page_height);
if (delay) final.set("delay", delay);
void *buf;
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0) : 0);
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,
blob.length())); length));
result.Set("type", type); result.Set("type", type);
return result; return result;
} catch (std::exception const &err) { } catch (std::exception const &err) {

View file

@ -1,4 +1,3 @@
#include <Magick++.h>
#include <napi.h> #include <napi.h>
#include <iostream> #include <iostream>

View file

@ -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 Gamexplain(const Napi::CallbackInfo &info) { Napi::Value Gamexplain(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
@ -18,47 +18,49 @@ Napi::Value Gamexplain(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");
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);
list<Image> frames;
list<Image> coalesced;
list<Image> mid;
Image watermark;
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;
}
string assetPath = basePath + "assets/images/gamexplain.png"; string assetPath = basePath + "assets/images/gamexplain.png";
watermark.read(assetPath); VImage tmpl = VImage::new_from_file(assetPath.c_str());
coalesceImages(&coalesced, frames.begin(), frames.end());
for (Image &image : coalesced) { int width = in.width();
image.backgroundColor("white"); int page_height = vips_image_get_page_height(in.get_image());
image.scale(Geometry("1181x571!")); int n_pages = vips_image_get_n_pages(in.get_image());
image.extent(Geometry("1200x675-10-92"));
image.composite(watermark, Geometry("+0+0"), Magick::OverCompositeOp); vector<VImage> img;
image.magick(type); for (int i = 0; i < n_pages; i++) {
mid.push_back(image); VImage img_frame =
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
VImage resized =
img_frame
.thumbnail_image(1181, VImage::option()
->set("height", 571)
->set("size", VIPS_SIZE_FORCE))
.embed(10, 92, 1200, 675,
VImage::option()->set("extend", "white"));
VImage composited = resized.composite2(tmpl, VIPS_BLEND_MODE_OVER);
img.push_back(composited);
} }
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, 675);
if (delay) final.set("delay", delay);
optimizeTransparency(mid.begin(), mid.end()); void *buf;
size_t length;
final.write_to_buffer(
("." + type).c_str(), &buf, &length,
type == "gif" ? VImage::option()->set("dither", 0) : 0);
if (type == "gif") { vips_thread_shutdown();
for (Image &image : mid) {
image.quantizeDither(false);
image.quantize();
if (delay != 0) image.animationDelay(delay);
}
}
writeImages(mid.begin(), mid.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) {

View file

@ -1,10 +1,10 @@
#include <Magick++.h>
#include <napi.h> #include <napi.h>
#include <list> #include <list>
#include <vips/vips8>
using namespace std; using namespace std;
using namespace Magick; using namespace vips;
Napi::Value Homebrew(const Napi::CallbackInfo &info) { Napi::Value Homebrew(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
@ -14,23 +14,31 @@ Napi::Value Homebrew(const Napi::CallbackInfo &info) {
string caption = obj.Get("caption").As<Napi::String>().Utf8Value(); string caption = obj.Get("caption").As<Napi::String>().Utf8Value();
string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value(); string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
Blob blob;
Image image;
string assetPath = basePath + "assets/images/hbc.png"; string assetPath = basePath + "assets/images/hbc.png";
image.read(assetPath); VImage bg = VImage::new_from_file(assetPath.c_str());
image.textGravity(Magick::CenterGravity);
image.font("./assets/hbc.ttf"); VImage text =
image.textKerning(-5); VImage::text(("<span letter_spacing=\"-5120\" color=\"white\">" +
image.fillColor("white"); caption + "</span>")
image.fontPointsize(96); .c_str(),
image.draw(DrawableText(0, 0, caption)); VImage::option()
image.magick("PNG"); ->set("rgba", true)
image.write(&blob); ->set("align", VIPS_ALIGN_CENTRE)
->set("font", "PF Square Sans Pro 96"));
VImage out = bg.composite2(text, VIPS_BLEND_MODE_OVER,
VImage::option()
->set("x", 400 - (text.width() / 2))
->set("y", 300 - (text.height() / 2) - 8));
void *buf;
size_t length;
out.write_to_buffer(".png", &buf, &length);
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", "png"); result.Set("type", "png");
return result; return result;
} catch (std::exception const &err) { } catch (std::exception const &err) {

View file

@ -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 Watermark(const Napi::CallbackInfo &info) { Napi::Value Watermark(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
@ -14,8 +14,7 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
Napi::Object obj = info[0].As<Napi::Object>(); Napi::Object obj = info[0].As<Napi::Object>();
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>(); Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
string water = obj.Get("water").As<Napi::String>().Utf8Value(); string water = obj.Get("water").As<Napi::String>().Utf8Value();
Magick::GravityType gravity = int gravity = obj.Get("gravity").As<Napi::Number>().Int64Value();
Magick::GravityType(obj.Get("gravity").As<Napi::Number>().Int64Value());
bool resize = obj.Has("resize") bool resize = obj.Has("resize")
? obj.Get("resize").As<Napi::Boolean>().Value() ? obj.Get("resize").As<Napi::Boolean>().Value()
: false; : false;
@ -31,9 +30,96 @@ Napi::Value Watermark(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 =
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);
string merged = basePath + water;
VImage watermark = VImage::new_from_file(merged.c_str());
int width = in.width();
int page_height = vips_image_get_page_height(in.get_image());
int n_pages = vips_image_get_n_pages(in.get_image());
if (resize && append) {
watermark = watermark.thumbnail_image(width);
} else if (resize) {
watermark = watermark.thumbnail_image(
VIPS_MAX_COORD, VImage::option()->set("height", page_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;
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::option()->set("x", x)->set("y", y));
img.push_back(composited);
}
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, page_height + addedHeight);
if (delay) final.set("delay", delay);
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();
/*list<Image> frames;
list<Image> coalesced; list<Image> coalesced;
list<Image> mid; list<Image> mid;
Image watermark; Image watermark;
@ -90,11 +176,10 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
} }
} }
writeImages(mid.begin(), mid.end(), &blob); writeImages(mid.begin(), mid.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) {