Port flip, re-add top to caption2

This commit is contained in:
Essem 2022-02-23 15:01:24 -06:00
parent 9e4cdd7120
commit 0fa02a9e6a
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
3 changed files with 39 additions and 33 deletions

View file

@ -55,9 +55,12 @@ Napi::Value CaptionTwo(const Napi::CallbackInfo &info) {
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));
VImage frame =
(top ? captionImage : img_frame)
.join(top ? img_frame : 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));

View file

@ -33,7 +33,7 @@ Napi::Value Colors(const Napi::CallbackInfo &info) {
VImage out;
if (color == "blurple") {
out = in;
out = in; // TODO: figure out how to implement blurple
} else if (color == "grayscale") {
out = in.colourspace(VIPS_INTERPRETATION_B_W);
} else if (color == "sepia") {

View file

@ -3,9 +3,10 @@
#include <iostream>
#include <list>
#include <vips/vips8>
using namespace std;
using namespace Magick;
using namespace vips;
Napi::Value Flip(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
@ -19,41 +20,43 @@ Napi::Value Flip(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> mid;
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);
for (Image &image : coalesced) {
flop ? image.flop() : image.flip();
image.magick(type);
mid.push_back(image);
}
optimizeTransparency(mid.begin(), mid.end());
if (type == "gif") {
for (Image &image : mid) {
image.quantizeDither(false);
image.quantize();
if (delay != 0) image.animationDelay(delay);
VImage out;
if (flop) {
out = in.flip(VIPS_DIRECTION_HORIZONTAL);
} else if (type == "gif") {
// libvips gif handling is both a blessing and a curse
vector<VImage> img;
int page_height = vips_image_get_page_height(in.get_image());
int n_pages = vips_image_get_n_pages(in.get_image());
for (int i = 0; i < n_pages; i++) {
VImage img_frame = in.crop(0, i * page_height, in.width(), page_height);
VImage flipped = img_frame.flip(VIPS_DIRECTION_VERTICAL);
img.push_back(flipped);
}
out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
out.set(VIPS_META_PAGE_HEIGHT, page_height);
} else {
out = in.flip(VIPS_DIRECTION_VERTICAL);
}
writeImages(mid.begin(), mid.end(), &blob);
if (delay) out.set("delay", delay);
void *buf;
size_t length;
out.write_to_buffer(("." + type).c_str(), &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", type);
return result;
} catch (std::exception const &err) {