diff --git a/commands/image-editing/bounce.js b/commands/image-editing/bounce.js new file mode 100644 index 0000000..5075f9d --- /dev/null +++ b/commands/image-editing/bounce.js @@ -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; diff --git a/natives/bounce.cc b/natives/bounce.cc new file mode 100644 index 0000000..f179e72 --- /dev/null +++ b/natives/bounce.cc @@ -0,0 +1,52 @@ +#include "common.h" +#include + +#include + +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 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 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; +} \ No newline at end of file diff --git a/natives/bounce.h b/natives/bounce.h new file mode 100644 index 0000000..ab77b54 --- /dev/null +++ b/natives/bounce.h @@ -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); \ No newline at end of file diff --git a/natives/image.cc b/natives/image.cc index 617af5a..96371de 100644 --- a/natives/image.cc +++ b/natives/image.cc @@ -6,6 +6,7 @@ #include #include "blur.h" +#include "bounce.h" #include "colors.h" #include "caption.h" #include "caption2.h" @@ -53,6 +54,7 @@ using namespace std; std::map FunctionMap = { {"blur", &Blur}, + {"bounce", &Bounce}, {"caption", &Caption}, {"captionTwo", &CaptionTwo}, {"circle", &Circle},