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");
class SlowCommand extends ImageCommand {
params = {
slow: true
};
params(args) {
const speed = parseInt(args[0]);
return {
slow: true,
speed: isNaN(speed) ? 2 : speed
};
}
static description = "Makes an image sequence slower";
static aliases = ["slowdown", "slower", "gifspeed2"];
static arguments = ["{multiplier}"];
static requiresGIF = true;
static noImage = "you need to provide an image to slow down!";

View File

@ -1,8 +1,16 @@
const ImageCommand = require("../../classes/imageCommand.js");
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 aliases = ["speedup", "fast", "gifspeed", "faster"];
static arguments = ["{multiplier}"];
static requiresGIF = true;
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 {
public:
SpeedWorker(Napi::Function& callback, string in_path, bool slow, string type, int delay)
: Napi::AsyncWorker(callback), in_path(in_path), slow(slow), type(type), delay(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), speed(speed), type(type), delay(delay) {}
~SpeedWorker() {}
void Execute() {
@ -17,23 +17,43 @@ class SpeedWorker : public Napi::AsyncWorker {
// if passed a delay, use that. otherwise use the average frame delay.
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) {
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) {
new_delay = delay;
auto it = frames.begin();
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it);
} else {
image.animationDelay(new_delay);
removeFrames = true;
break;
}
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 {
int new_delay = slow ? delay * 2 : delay / 2;
int new_delay = slow ? delay * speed : delay / speed;
if (!slow && new_delay <= 1) {
new_delay = delay;
auto it = frames.begin();
while(it != frames.end() && ++it != frames.end()) it = frames.erase(it);
for (int i = 0; i < speed - 1; ++i) {
frames.remove_if([counter = 0](const auto x) mutable {
return ++counter % 2 == 0;
});
}
} else {
for_each(frames.begin(), frames.end(), animationDelayImage(new_delay));
}
@ -51,7 +71,7 @@ class SpeedWorker : public Napi::AsyncWorker {
private:
bool slow;
string in_path, type;
int delay, amount;
int speed, delay;
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;
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;
SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, type, delay);
SpeedWorker* explodeWorker = new SpeedWorker(cb, path, slow, speed, type, delay);
explodeWorker->Queue();
return env.Undefined();
}

View File

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