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++) { for (int i = 0; i < n_pages; i++) {
VImage img_frame = VImage img_frame =
type == "gif" ? in.crop(0, i * page_height, width, page_height) : in; type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
VImage frame = img_frame.join( VImage frame =
captionImage, VIPS_DIRECTION_VERTICAL, (top ? captionImage : img_frame)
VImage::option()->set("background", 0xffffff)->set("expand", true)); .join(top ? img_frame : captionImage, VIPS_DIRECTION_VERTICAL,
VImage::option()
->set("background", 0xffffff)
->set("expand", true));
img.push_back(frame); img.push_back(frame);
} }
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1)); 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; VImage out;
if (color == "blurple") { if (color == "blurple") {
out = in; out = in; // TODO: figure out how to implement blurple
} else if (color == "grayscale") { } else if (color == "grayscale") {
out = in.colourspace(VIPS_INTERPRETATION_B_W); out = in.colourspace(VIPS_INTERPRETATION_B_W);
} else if (color == "sepia") { } else if (color == "sepia") {

View file

@ -3,9 +3,10 @@
#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 Flip(const Napi::CallbackInfo &info) { Napi::Value Flip(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
@ -19,41 +20,43 @@ Napi::Value Flip(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)
try { .colourspace(VIPS_INTERPRETATION_sRGB);
readImages(&frames, Blob(data.Data(), data.Length())); if (!in.has_alpha()) in = in.bandjoin(255);
} 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());
for (Image &image : coalesced) { VImage out;
flop ? image.flop() : image.flip(); if (flop) {
image.magick(type); out = in.flip(VIPS_DIRECTION_HORIZONTAL);
mid.push_back(image); } else if (type == "gif") {
} // libvips gif handling is both a blessing and a curse
vector<VImage> img;
optimizeTransparency(mid.begin(), mid.end()); int page_height = vips_image_get_page_height(in.get_image());
int n_pages = vips_image_get_n_pages(in.get_image());
if (type == "gif") { for (int i = 0; i < n_pages; i++) {
for (Image &image : mid) { VImage img_frame = in.crop(0, i * page_height, in.width(), page_height);
image.quantizeDither(false); VImage flipped = img_frame.flip(VIPS_DIRECTION_VERTICAL);
image.quantize(); img.push_back(flipped);
if (delay != 0) image.animationDelay(delay);
} }
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); 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) {