2021-04-24 22:54:47 +00:00
|
|
|
#include <Magick++.h>
|
2020-07-22 18:12:38 +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-22 18:12:38 +00:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Magick;
|
|
|
|
|
2021-05-12 04:05:38 +00:00
|
|
|
void *memset16(void *m, uint16_t val, size_t count) {
|
|
|
|
uint16_t *buf = (uint16_t *)m;
|
|
|
|
|
|
|
|
while (count--) *buf++ = val;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
2021-04-24 22:54:47 +00:00
|
|
|
Napi::Value Speed(const Napi::CallbackInfo &info) {
|
|
|
|
Napi::Env env = info.Env();
|
2020-07-22 18:12:38 +00:00
|
|
|
|
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 slow =
|
|
|
|
obj.Has("slow") ? obj.Get("slow").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 speed =
|
|
|
|
obj.Has("speed") ? obj.Get("speed").As<Napi::Number>().Int32Value() : 2;
|
2021-01-27 02:30:04 +00:00
|
|
|
|
2021-05-12 04:05:38 +00:00
|
|
|
Napi::Object result = Napi::Object::New(env);
|
|
|
|
|
|
|
|
char *fileData = data.Data();
|
2021-04-20 01:15:32 +00:00
|
|
|
|
2022-01-15 06:04:34 +00:00
|
|
|
char *match = (char *)"\x00\x21\xF9\x04";
|
2021-04-20 01:15:32 +00:00
|
|
|
|
2021-05-12 04:05:38 +00:00
|
|
|
// if passed a delay, use that. otherwise iterate over every frame.
|
2021-04-26 14:47:03 +00:00
|
|
|
if (delay == 0) {
|
2021-05-12 04:05:38 +00:00
|
|
|
vector<uint16_t> old_delays;
|
2021-04-26 14:47:03 +00:00
|
|
|
bool removeFrames = false;
|
2021-05-12 04:05:38 +00:00
|
|
|
char *lastPos;
|
|
|
|
|
|
|
|
int amount = 0;
|
|
|
|
|
2021-05-18 04:44:28 +00:00
|
|
|
lastPos = (char *)memchr(fileData, '\x00', data.Length());
|
2021-05-12 04:05:38 +00:00
|
|
|
while (lastPos != NULL) {
|
2021-05-18 04:44:28 +00:00
|
|
|
if (memcmp(lastPos, match, 4) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
++amount;
|
|
|
|
uint16_t old_delay;
|
2021-05-18 04:44:28 +00:00
|
|
|
memcpy(&old_delay, lastPos + 5, 2);
|
2021-05-12 04:05:38 +00:00
|
|
|
old_delays.push_back(old_delay);
|
2021-05-18 04:44:28 +00:00
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
2021-04-19 16:04:24 +00:00
|
|
|
}
|
2020-07-22 18:12:38 +00:00
|
|
|
|
2021-05-12 04:05:38 +00:00
|
|
|
int currentFrame = 0;
|
2021-05-18 04:44:28 +00:00
|
|
|
lastPos = (char *)memchr(fileData, '\x00', data.Length());
|
2021-05-12 04:05:38 +00:00
|
|
|
while (lastPos != NULL) {
|
2021-05-18 04:44:28 +00:00
|
|
|
if (memcmp(lastPos, match, 4) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint16_t new_delay = slow ? old_delays[currentFrame] * speed
|
|
|
|
: old_delays[currentFrame] / speed;
|
2021-04-26 14:47:03 +00:00
|
|
|
if (!slow && new_delay <= 1) {
|
|
|
|
removeFrames = true;
|
|
|
|
break;
|
|
|
|
}
|
2021-05-18 04:44:28 +00:00
|
|
|
memset16(lastPos + 5, new_delay, 1);
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
++currentFrame;
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2020-07-22 18:12:38 +00:00
|
|
|
|
2021-05-12 04:05:38 +00:00
|
|
|
result.Set("data",
|
|
|
|
Napi::Buffer<char>::Copy(env, fileData, data.Length()));
|
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
if (removeFrames) {
|
2021-05-12 04:05:38 +00:00
|
|
|
Blob blob;
|
|
|
|
|
|
|
|
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-05-12 04:05:38 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
for (list<Image>::iterator i = frames.begin(); i != frames.end(); ++i) {
|
|
|
|
int index = distance(frames.begin(), i);
|
|
|
|
i->animationDelay(old_delays[index]);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < speed - 1; ++i) {
|
2022-01-15 06:04:34 +00:00
|
|
|
auto it = frames.begin();
|
|
|
|
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it);
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2021-05-12 04:05:38 +00:00
|
|
|
|
|
|
|
for_each(frames.begin(), frames.end(), magickImage(type));
|
|
|
|
writeImages(frames.begin(), frames.end(), &blob);
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
|
|
|
blob.length()));
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-05-12 04:05:38 +00:00
|
|
|
char *lastPos;
|
|
|
|
|
|
|
|
bool removeFrames = false;
|
|
|
|
|
2021-05-18 04:44:28 +00:00
|
|
|
lastPos = (char *)memchr(fileData, '\x00', data.Length());
|
2021-05-12 04:05:38 +00:00
|
|
|
while (lastPos != NULL) {
|
2021-05-18 04:44:28 +00:00
|
|
|
if (memcmp(lastPos, match, 4) != 0) {
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint16_t old_delay;
|
2021-05-18 04:44:28 +00:00
|
|
|
memcpy(&old_delay, lastPos + 5, 2);
|
2021-05-12 04:05:38 +00:00
|
|
|
int new_delay = slow ? delay * speed : delay / speed;
|
|
|
|
if (!slow && new_delay <= 1) {
|
|
|
|
removeFrames = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (removeFrames) {
|
|
|
|
Blob blob;
|
|
|
|
|
|
|
|
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-05-12 04:05:38 +00:00
|
|
|
|
2021-04-26 14:47:03 +00:00
|
|
|
for (int i = 0; i < speed - 1; ++i) {
|
2022-01-15 06:04:34 +00:00
|
|
|
auto it = frames.begin();
|
|
|
|
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it);
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2021-05-12 04:05:38 +00:00
|
|
|
|
|
|
|
for_each(frames.begin(), frames.end(), magickImage(type));
|
|
|
|
writeImages(frames.begin(), frames.end(), &blob);
|
|
|
|
result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
|
|
|
|
blob.length()));
|
2021-04-26 14:47:03 +00:00
|
|
|
} else {
|
2021-05-12 04:05:38 +00:00
|
|
|
while (lastPos != NULL) {
|
2021-05-18 04:44:28 +00:00
|
|
|
if (memcmp(lastPos, match, 4) != 0) {
|
2021-05-12 04:05:38 +00:00
|
|
|
lastPos =
|
2021-05-18 04:44:28 +00:00
|
|
|
(char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
uint16_t old_delay;
|
2021-05-18 04:44:28 +00:00
|
|
|
memcpy(&old_delay, lastPos + 5, 2);
|
2021-05-12 04:05:38 +00:00
|
|
|
int new_delay = slow ? delay * speed : delay / speed;
|
2021-05-18 04:44:28 +00:00
|
|
|
memset16(lastPos + 5, new_delay, 1);
|
|
|
|
lastPos = (char *)memchr(lastPos + 1, '\x00',
|
2021-05-12 04:05:38 +00:00
|
|
|
(data.Length() - (lastPos - fileData)) - 1);
|
|
|
|
}
|
2021-04-26 14:47:03 +00:00
|
|
|
}
|
2021-04-24 22:54:47 +00:00
|
|
|
}
|
2020-07-22 18:12:38 +00:00
|
|
|
|
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-26 14:47:03 +00:00
|
|
|
}
|
2022-01-15 06:04:34 +00:00
|
|
|
}
|