Added bounce

This commit is contained in:
Essem 2022-12-28 14:24:22 -06:00
parent 89caa43068
commit 5d8974d177
No known key found for this signature in database
GPG Key ID: 7D497397CC3A2A8C
4 changed files with 72 additions and 0 deletions

View File

@ -0,0 +1,11 @@
import ImageCommand from "../../classes/imageCommand.js";
class BounceCommand extends ImageCommand {
static description = "Makes an image bounce up and down";
static aliases = ["bouncy"];
static noImage = "You need to provide an image/GIF to bounce!";
static command = "bounce";
}
export default BounceCommand;

52
natives/bounce.cc Normal file
View File

@ -0,0 +1,52 @@
#include "common.h"
#include <math.h>
#include <vips/vips8>
using namespace std;
using namespace vips;
char *Bounce(string *type, char *BufferData, size_t BufferLength,
ArgumentMap Arguments, size_t *DataSize) {
VOption *options = VImage::option();
VImage in =
VImage::new_from_buffer(
BufferData, BufferLength, "",
*type == "gif" ? options->set("n", -1)->set("access", "sequential")
: options)
.colourspace(VIPS_INTERPRETATION_sRGB);
if (!in.has_alpha())
in = in.bandjoin(255);
int width = in.width();
int pageHeight = vips_image_get_page_height(in.get_image());
int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 15;
double mult = M_PI / nPages;
int halfHeight = pageHeight / 2;
vector<VImage> img;
for (int i = 0; i < nPages; i++) {
VImage img_frame =
*type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
double height = halfHeight * ((abs(sin(i * mult)) * -1) + 1);
VImage embedded = img_frame.embed(0, height, width, pageHeight + halfHeight);
img.push_back(embedded);
}
VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
final.set(VIPS_META_PAGE_HEIGHT, pageHeight + halfHeight);
if (*type != "gif") {
vector<int> delay(30, 50);
final.set("delay", delay);
}
void *buf;
final.write_to_buffer(".gif", &buf, DataSize);
*type = "gif";
vips_error_clear();
vips_thread_shutdown();
return (char *)buf;
}

7
natives/bounce.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include "common.h"
using std::string;
char* Bounce(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);

View File

@ -6,6 +6,7 @@
#include <iostream>
#include "blur.h"
#include "bounce.h"
#include "colors.h"
#include "caption.h"
#include "caption2.h"
@ -53,6 +54,7 @@ using namespace std;
std::map<std::string, char* (*)(string *type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize)> FunctionMap = {
{"blur", &Blur},
{"bounce", &Bounce},
{"caption", &Caption},
{"captionTwo", &CaptionTwo},
{"circle", &Circle},