mrmBot-Matrix/natives/reverse.cc

51 lines
1.3 KiB
C++
Raw Normal View History

#include <Magick++.h>
2020-07-22 18:12:38 +00:00
#include <napi.h>
2020-07-22 18:12:38 +00:00
#include <list>
using namespace std;
using namespace Magick;
Napi::Value Reverse(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::Object obj = info[0].As<Napi::Object>();
string path = obj.Get("path").As<Napi::String>().Utf8Value();
bool soos =
obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
int delay =
obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
2021-02-25 21:09:53 +00:00
Blob blob;
2021-02-25 21:09:53 +00:00
list<Image> frames;
list<Image> coalesced;
readImages(&frames, path);
coalesceImages(&coalesced, frames.begin(), frames.end());
if (soos) {
list<Image> copy = coalesced;
copy.reverse();
coalesced.insert(coalesced.end(), copy.begin(), copy.end());
} else {
coalesced.reverse();
2020-07-22 18:12:38 +00:00
}
for_each(coalesced.begin(), coalesced.end(), magickImage("GIF"));
2020-07-22 18:12:38 +00:00
optimizeTransparency(coalesced.begin(), coalesced.end());
2020-07-22 18:12:38 +00:00
for (Image &image : coalesced) {
image.quantizeDither(false);
image.quantize();
if (delay != 0) image.animationDelay(delay);
}
2020-07-22 18:12:38 +00:00
writeImages(coalesced.begin(), coalesced.end(), &blob);
2020-07-22 18:12:38 +00:00
Napi::Object result = Napi::Object::New(env);
result.Set("data",
Napi::Buffer<char>::Copy(env, (char *)blob.data(), blob.length()));
result.Set("type", "gif");
return result;
2020-07-22 18:12:38 +00:00
}