Added argument support to slow/speed, made video detection also detect images

This commit is contained in:
TheEssem 2021-04-19 20:15:32 -05:00
parent e00671f0d5
commit 7db1aa880c
4 changed files with 52 additions and 18 deletions

View file

@ -1,12 +1,17 @@
const ImageCommand = require("../../classes/imageCommand.js"); const ImageCommand = require("../../classes/imageCommand.js");
class SlowCommand extends ImageCommand { class SlowCommand extends ImageCommand {
params = { params(args) {
slow: true const speed = parseInt(args[0]);
return {
slow: true,
speed: isNaN(speed) ? 2 : speed
}; };
}
static description = "Makes an image sequence slower"; static description = "Makes an image sequence slower";
static aliases = ["slowdown", "slower", "gifspeed2"]; static aliases = ["slowdown", "slower", "gifspeed2"];
static arguments = ["{multiplier}"];
static requiresGIF = true; static requiresGIF = true;
static noImage = "you need to provide an image to slow down!"; static noImage = "you need to provide an image to slow down!";

View file

@ -1,8 +1,16 @@
const ImageCommand = require("../../classes/imageCommand.js"); const ImageCommand = require("../../classes/imageCommand.js");
class SpeedCommand extends ImageCommand { class SpeedCommand extends ImageCommand {
params(args) {
const speed = parseInt(args[0]);
return {
speed: isNaN(speed) ? 2 : speed
};
}
static description = "Makes an image sequence faster"; static description = "Makes an image sequence faster";
static aliases = ["speedup", "fast", "gifspeed", "faster"]; static aliases = ["speedup", "fast", "gifspeed", "faster"];
static arguments = ["{multiplier}"];
static requiresGIF = true; static requiresGIF = true;
static noImage = "you need to provide an image to speed up!"; static noImage = "you need to provide an image to speed up!";

View file

@ -7,8 +7,8 @@ using namespace Magick;
class SpeedWorker : public Napi::AsyncWorker { class SpeedWorker : public Napi::AsyncWorker {
public: public:
SpeedWorker(Napi::Function& callback, string in_path, bool slow, string type, int delay) SpeedWorker(Napi::Function& callback, string in_path, bool slow, int speed, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), slow(slow), type(type), delay(delay) {} : Napi::AsyncWorker(callback), in_path(in_path), slow(slow), speed(speed), type(type), delay(delay) {}
~SpeedWorker() {} ~SpeedWorker() {}
void Execute() { void Execute() {
@ -17,23 +17,43 @@ class SpeedWorker : public Napi::AsyncWorker {
// if passed a delay, use that. otherwise use the average frame delay. // if passed a delay, use that. otherwise use the average frame delay.
if (delay == 0) { if (delay == 0) {
vector<int> old_delays;
bool removeFrames = false;
for (Image &image : frames) {
int animation_delay = image.animationDelay();
old_delays.push_back(animation_delay);
}
for (Image &image : frames) { for (Image &image : frames) {
int old_delay = image.animationDelay(); int old_delay = image.animationDelay();
int new_delay = slow ? old_delay * 2 : old_delay / 2; int new_delay = slow ? old_delay * speed : old_delay / speed;
if (!slow && new_delay <= 1) { if (!slow && new_delay <= 1) {
new_delay = delay; removeFrames = true;
auto it = frames.begin(); break;
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it); }
} else {
image.animationDelay(new_delay); image.animationDelay(new_delay);
} }
if (removeFrames) {
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) {
frames.remove_if([counter = 0](const auto x) mutable {
return ++counter % 2 == 0;
});
}
} }
} else { } else {
int new_delay = slow ? delay * 2 : delay / 2; int new_delay = slow ? delay * speed : delay / speed;
if (!slow && new_delay <= 1) { if (!slow && new_delay <= 1) {
new_delay = delay; for (int i = 0; i < speed - 1; ++i) {
auto it = frames.begin(); frames.remove_if([counter = 0](const auto x) mutable {
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it); return ++counter % 2 == 0;
});
}
} else { } else {
for_each(frames.begin(), frames.end(), animationDelayImage(new_delay)); for_each(frames.begin(), frames.end(), animationDelayImage(new_delay));
} }
@ -51,7 +71,7 @@ class SpeedWorker : public Napi::AsyncWorker {
private: private:
bool slow; bool slow;
string in_path, type; string in_path, type;
int delay, amount; int speed, delay;
Blob blob; Blob blob;
}; };
@ -65,8 +85,9 @@ Napi::Value Speed(const Napi::CallbackInfo &info)
bool slow = obj.Has("slow") ? obj.Get("slow").As<Napi::Boolean>().Value() : false; bool slow = obj.Has("slow") ? obj.Get("slow").As<Napi::Boolean>().Value() : false;
string type = obj.Get("type").As<Napi::String>().Utf8Value(); string type = obj.Get("type").As<Napi::String>().Utf8Value();
int delay = obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0; 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;
SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, type, delay); SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, speed, type, delay);
explodeWorker->Queue(); explodeWorker->Queue();
return env.Undefined(); return env.Undefined();
} }

View file

@ -59,7 +59,7 @@ const getImage = async (image, image2, video, gifv = false) => {
payload.type = "image/gif"; payload.type = "image/gif";
} else if (video) { } else if (video) {
payload.type = await getType(payload.path); payload.type = await getType(payload.path);
if (!payload.type || !videoFormats.includes(payload.type)) return; if (!payload.type || (!videoFormats.includes(payload.type) && !imageFormats.includes(payload.type))) return;
} else { } else {
payload.type = await getType(payload.path); payload.type = await getType(payload.path);
if (!payload.type || !imageFormats.includes(payload.type)) return; if (!payload.type || !imageFormats.includes(payload.type)) return;