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
|
|
|
|
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>();
|
|
|
|
string path = obj.Get("path").As<Napi::String>().Utf8Value();
|
|
|
|
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;
|
|
|
|
|
|
|
|
Blob blob;
|
|
|
|
|
|
|
|
list<Image> frames;
|
|
|
|
readImages(&frames, path);
|
|
|
|
|
|
|
|
if (frame >= 0 && !loop) {
|
|
|
|
size_t frameSize = frames.size();
|
|
|
|
int framePos = clamp(frame, 0, (int)frameSize);
|
|
|
|
frames.resize(framePos + 1);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
|
|
|
|
Napi::Object result = Napi::Object::New(env);
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
|
|
|
blob.length()));
|
|
|
|
result.Set("type", type);
|
|
|
|
return result;
|
2021-05-03 21:01:48 +00:00
|
|
|
} catch (Napi::Error const &err) {
|
|
|
|
throw err;
|
2021-04-26 14:47:03 +00:00
|
|
|
} 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
|
|
|
}
|