Port uncaption

This commit is contained in:
Essem 2022-05-19 15:46:32 -05:00
parent d191a69742
commit 2e71097763
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -1,11 +1,9 @@
#include <Magick++.h>
#include <napi.h> #include <napi.h>
#include <iostream> #include <vips/vips8>
#include <list>
using namespace std; using namespace std;
using namespace Magick; using namespace vips;
Napi::Value Uncaption(const Napi::CallbackInfo &info) { Napi::Value Uncaption(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env(); Napi::Env env = info.Env();
@ -13,61 +11,52 @@ Napi::Value Uncaption(const Napi::CallbackInfo &info) {
try { try {
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>>();
float tolerance = obj.Has("tolerance") ? obj.Get("tolerance").As<Napi::Number>().FloatValue() : 0.95; float tolerance = obj.Has("tolerance")
? obj.Get("tolerance").As<Napi::Number>().FloatValue()
: 0.5;
string type = obj.Get("type").As<Napi::String>().Utf8Value(); string type = obj.Get("type").As<Napi::String>().Utf8Value();
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; int width = in.width();
} catch (Magick::Warning &warning) { int page_height = vips_image_get_page_height(in.get_image());
cerr << "Warning: " << warning.what() << endl; int n_pages = vips_image_get_n_pages(in.get_image());
VImage first =
in.crop(0, 0, 3, page_height).colourspace(VIPS_INTERPRETATION_B_W) >
(255 * tolerance);
int top, captionWidth, captionHeight;
first.find_trim(&top, &captionWidth, &captionHeight);
vector<VImage> img;
for (int i = 0; i < n_pages; i++) {
VImage img_frame = type == "gif" ? in.crop(0, (i * page_height) + top,
width, page_height - top)
: in;
img.push_back(img_frame);
} }
coalesceImages(&coalesced, frames.begin(), frames.end()); VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, page_height - top);
if (delay) final.set("delay", delay);
Image firstImage = coalesced.front(); void *buf;
ssize_t columns = firstImage.columns(); size_t length;
ssize_t rows = firstImage.rows(); final.write_to_buffer(
ssize_t row; ("." + type).c_str(), &buf, &length,
for (row = 0; row < rows; ++row) { type == "gif" ? VImage::option()->set("dither", 0) : 0);
ColorGray color = firstImage.pixelColor(0, row);
if (color.shade() < tolerance) {
break;
}
}
Geometry geom = Geometry(columns, row == rows ? rows : rows - row, 0,
row == rows ? 0 : row);
for (Image &image : coalesced) { vips_thread_shutdown();
image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod);
image.backgroundColor("none");
image.extent(geom);
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);
}
}
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) {