Port watermark, homebrew, gamexplain, and flag
This commit is contained in:
parent
0fa02a9e6a
commit
112f15e9f2
5 changed files with 190 additions and 99 deletions
|
@ -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 Flag(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -19,50 +19,47 @@ Napi::Value Flag(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");
|
||||
|
||||
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;
|
||||
watermark.read(assetPath);
|
||||
watermark.alphaChannel(Magick::SetAlphaChannel);
|
||||
watermark.evaluate(Magick::AlphaChannel, Magick::MultiplyEvaluateOperator,
|
||||
0.5);
|
||||
string query(to_string(frames.front().baseColumns()) + "x" +
|
||||
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);
|
||||
VImage overlayImage = VImage::new_from_file(assetPath.c_str()).thumbnail_image(width, VImage::option()->set("height", page_height)->set("size", VIPS_SIZE_FORCE));
|
||||
if (!overlayImage.has_alpha()) {
|
||||
overlayImage = overlayImage.bandjoin(127);
|
||||
} else {
|
||||
overlayImage = overlayImage * vector<double>{ 1, 1, 1, 0.5 }; // this is a pretty cool line, just saying
|
||||
}
|
||||
|
||||
optimizeTransparency(mid.begin(), mid.end());
|
||||
|
||||
if (type == "gif") {
|
||||
for (Image &image : mid) {
|
||||
image.quantizeDither(false);
|
||||
image.quantize();
|
||||
if (delay != 0) image.animationDelay(delay);
|
||||
}
|
||||
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 composited = img_frame.composite2(overlayImage, VIPS_BLEND_MODE_OVER);
|
||||
img.push_back(composited);
|
||||
}
|
||||
|
||||
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);
|
||||
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,4 +1,3 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <iostream>
|
||||
|
|
|
@ -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 Gamexplain(const Napi::CallbackInfo &info) {
|
||||
Napi::Env env = info.Env();
|
||||
|
@ -18,47 +18,49 @@ Napi::Value Gamexplain(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");
|
||||
|
||||
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";
|
||||
watermark.read(assetPath);
|
||||
coalesceImages(&coalesced, frames.begin(), frames.end());
|
||||
VImage tmpl = VImage::new_from_file(assetPath.c_str());
|
||||
|
||||
for (Image &image : coalesced) {
|
||||
image.backgroundColor("white");
|
||||
image.scale(Geometry("1181x571!"));
|
||||
image.extent(Geometry("1200x675-10-92"));
|
||||
image.composite(watermark, Geometry("+0+0"), Magick::OverCompositeOp);
|
||||
image.magick(type);
|
||||
mid.push_back(image);
|
||||
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());
|
||||
|
||||
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 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") {
|
||||
for (Image &image : mid) {
|
||||
image.quantizeDither(false);
|
||||
image.quantize();
|
||||
if (delay != 0) image.animationDelay(delay);
|
||||
}
|
||||
}
|
||||
|
||||
writeImages(mid.begin(), mid.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,10 +1,10 @@
|
|||
#include <Magick++.h>
|
||||
#include <napi.h>
|
||||
|
||||
#include <list>
|
||||
#include <vips/vips8>
|
||||
|
||||
using namespace std;
|
||||
using namespace Magick;
|
||||
using namespace vips;
|
||||
|
||||
Napi::Value Homebrew(const Napi::CallbackInfo &info) {
|
||||
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 basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
|
||||
|
||||
Blob blob;
|
||||
|
||||
Image image;
|
||||
string assetPath = basePath + "assets/images/hbc.png";
|
||||
image.read(assetPath);
|
||||
image.textGravity(Magick::CenterGravity);
|
||||
image.font("./assets/hbc.ttf");
|
||||
image.textKerning(-5);
|
||||
image.fillColor("white");
|
||||
image.fontPointsize(96);
|
||||
image.draw(DrawableText(0, 0, caption));
|
||||
image.magick("PNG");
|
||||
image.write(&blob);
|
||||
VImage bg = VImage::new_from_file(assetPath.c_str());
|
||||
|
||||
VImage text =
|
||||
VImage::text(("<span letter_spacing=\"-5120\" color=\"white\">" +
|
||||
caption + "</span>")
|
||||
.c_str(),
|
||||
VImage::option()
|
||||
->set("rgba", true)
|
||||
->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);
|
||||
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", "png");
|
||||
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 Watermark(const Napi::CallbackInfo &info) {
|
||||
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::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
||||
string water = obj.Get("water").As<Napi::String>().Utf8Value();
|
||||
Magick::GravityType gravity =
|
||||
Magick::GravityType(obj.Get("gravity").As<Napi::Number>().Int64Value());
|
||||
int gravity = obj.Get("gravity").As<Napi::Number>().Int64Value();
|
||||
bool resize = obj.Has("resize")
|
||||
? obj.Get("resize").As<Napi::Boolean>().Value()
|
||||
: false;
|
||||
|
@ -31,9 +30,96 @@ Napi::Value Watermark(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;
|
||||
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> mid;
|
||||
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);
|
||||
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