2020-07-12 15:14:39 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2022-03-09 19:10:10 +00:00
|
|
|
#include <vips/vips8>
|
2020-07-12 15:14:39 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2022-03-09 19:10:10 +00:00
|
|
|
using namespace vips;
|
2020-07-12 15:14:39 +00:00
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Freeze(const Napi::CallbackInfo &info) {
|
2020-07-12 15:14:39 +00:00
|
|
|
Napi::Env env = info.Env();
|
2022-06-28 03:50:53 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
2020-07-12 15:14:39 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
2022-11-27 19:03:07 +00:00
|
|
|
Napi::Object obj = info[1].As<Napi::Object>();
|
2021-05-11 19:25:02 +00:00
|
|
|
Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
|
2021-04-26 14:47:03 +00:00
|
|
|
bool loop =
|
|
|
|
obj.Has("loop") ? obj.Get("loop").As<Napi::Boolean>().Value() : false;
|
|
|
|
string type = obj.Get("type").As<Napi::String>().Utf8Value();
|
|
|
|
int frame = obj.Has("frame")
|
|
|
|
? obj.Get("frame").As<Napi::Number>().Int32Value()
|
|
|
|
: -1;
|
|
|
|
|
2021-05-13 22:11:13 +00:00
|
|
|
char *fileData = data.Data();
|
2022-01-15 06:04:34 +00:00
|
|
|
char *match = (char *)"\x21\xFF\x0BNETSCAPE2.0\x03\x01";
|
|
|
|
char *descriptor = (char *)"\x2C\x00\x00\x00\x00";
|
2021-05-13 22:11:13 +00:00
|
|
|
char *lastPos;
|
|
|
|
|
|
|
|
bool none = true;
|
|
|
|
|
|
|
|
if (loop) {
|
|
|
|
char *newData = (char *)malloc(data.Length() + 19);
|
|
|
|
memcpy(newData, fileData, data.Length());
|
|
|
|
lastPos = (char *)memchr(newData, '\x2C', data.Length());
|
|
|
|
while (lastPos != NULL) {
|
|
|
|
if (memcmp(lastPos, descriptor, 5) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x2C',
|
|
|
|
(data.Length() - (lastPos - newData)) - 1);
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2021-05-13 22:11:13 +00:00
|
|
|
memcpy(lastPos + 19, lastPos, (data.Length() - (lastPos - newData)));
|
|
|
|
memcpy(lastPos, match, 16);
|
|
|
|
memcpy(lastPos + 16, "\x00\x00\x00", 3);
|
|
|
|
result.Set("data",
|
|
|
|
Napi::Buffer<char>::Copy(env, newData, data.Length() + 19));
|
|
|
|
none = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (none)
|
|
|
|
result.Set("data",
|
|
|
|
Napi::Buffer<char>::Copy(env, newData, data.Length()));
|
|
|
|
} else if (frame >= 0 && !loop) {
|
2022-03-09 19:10:10 +00:00
|
|
|
VOption *options = VImage::option()->set("access", "sequential");
|
|
|
|
|
2022-11-27 20:52:40 +00:00
|
|
|
VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "",
|
|
|
|
type == "gif" ? options->set("n", -1)
|
|
|
|
: options)
|
2022-03-09 19:10:10 +00:00
|
|
|
.colourspace(VIPS_INTERPRETATION_sRGB);
|
2022-11-27 20:52:40 +00:00
|
|
|
if (!in.has_alpha())
|
|
|
|
in = in.bandjoin(255);
|
2022-03-09 19:10:10 +00:00
|
|
|
|
2022-06-20 06:05:06 +00:00
|
|
|
int pageHeight = vips_image_get_page_height(in.get_image());
|
2022-06-22 16:00:31 +00:00
|
|
|
int nPages = vips_image_get_n_pages(in.get_image());
|
|
|
|
int framePos = clamp(frame, 0, (int)nPages);
|
2022-06-20 06:05:06 +00:00
|
|
|
VImage out = in.crop(0, 0, in.width(), pageHeight * (framePos + 1));
|
|
|
|
out.set(VIPS_META_PAGE_HEIGHT, pageHeight);
|
2022-03-09 19:10:10 +00:00
|
|
|
out.set("loop", loop ? 0 : 1);
|
|
|
|
|
|
|
|
void *buf;
|
|
|
|
size_t length;
|
|
|
|
out.write_to_buffer(("." + type).c_str(), &buf, &length);
|
|
|
|
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
|
2021-05-13 22:11:13 +00:00
|
|
|
} else {
|
|
|
|
lastPos = (char *)memchr(fileData, '\x21', data.Length());
|
|
|
|
while (lastPos != NULL) {
|
|
|
|
if (memcmp(lastPos, match, 16) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x21',
|
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(lastPos, lastPos + 19,
|
|
|
|
(data.Length() - (lastPos - fileData)) - 19);
|
|
|
|
result.Set("data",
|
|
|
|
Napi::Buffer<char>::Copy(env, fileData, data.Length() - 19));
|
|
|
|
none = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (none)
|
|
|
|
result.Set("data",
|
|
|
|
Napi::Buffer<char>::Copy(env, fileData, data.Length()));
|
|
|
|
}
|
2021-04-26 14:47:03 +00:00
|
|
|
|
|
|
|
result.Set("type", type);
|
|
|
|
} catch (std::exception const &err) {
|
2022-06-28 03:50:53 +00:00
|
|
|
Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
2022-06-28 03:50:53 +00:00
|
|
|
Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2022-06-28 03:50:53 +00:00
|
|
|
|
|
|
|
vips_error_clear();
|
|
|
|
vips_thread_shutdown();
|
|
|
|
return result;
|
2022-01-15 06:04:34 +00:00
|
|
|
}
|