2021-04-24 22:54:47 +00:00
|
|
|
#include <Magick++.h>
|
2020-07-12 15:14:39 +00:00
|
|
|
#include <napi.h>
|
2021-04-24 22:54:47 +00:00
|
|
|
|
2021-10-23 18:58:19 +00:00
|
|
|
#include <iostream>
|
2020-07-12 15:14:39 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Magick;
|
|
|
|
|
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();
|
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
try {
|
|
|
|
Napi::Object obj = info[0].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 delay =
|
|
|
|
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
|
|
|
|
int frame = obj.Has("frame")
|
|
|
|
? obj.Get("frame").As<Napi::Number>().Int32Value()
|
|
|
|
: -1;
|
|
|
|
|
2021-05-13 22:11:13 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
|
|
|
|
|
|
|
char *fileData = data.Data();
|
|
|
|
char *match = "\x21\xFF\x0BNETSCAPE2.0\x03\x01";
|
|
|
|
char *descriptor = "\x2C\x00\x00\x00\x00";
|
|
|
|
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) {
|
|
|
|
Blob blob;
|
2021-04-26 14:47:03 +00:00
|
|
|
|
2021-05-13 22:11:13 +00:00
|
|
|
list<Image> frames;
|
2021-10-23 18:58:19 +00:00
|
|
|
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;
|
|
|
|
}
|
2021-04-26 14:47:03 +00:00
|
|
|
size_t frameSize = frames.size();
|
|
|
|
int framePos = clamp(frame, 0, (int)frameSize);
|
|
|
|
frames.resize(framePos + 1);
|
|
|
|
|
2021-05-13 22:11:13 +00:00
|
|
|
for_each(frames.begin(), frames.end(),
|
|
|
|
animationIterationsImage(loop ? 0 : 1));
|
|
|
|
for_each(frames.begin(), frames.end(), magickImage(type));
|
|
|
|
|
|
|
|
if (delay != 0)
|
|
|
|
for_each(frames.begin(), frames.end(), animationDelayImage(delay));
|
|
|
|
writeImages(frames.begin(), frames.end(), &blob);
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
|
|
|
blob.length()));
|
|
|
|
} 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);
|
|
|
|
return result;
|
|
|
|
} catch (std::exception const &err) {
|
|
|
|
throw Napi::Error::New(env, err.what());
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (...) {
|
|
|
|
throw Napi::Error::New(env, "Unknown error");
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2020-07-12 15:14:39 +00:00
|
|
|
}
|