Port deepfry, reddit, resize, and sonic, replace thumbnail with resize, cleanup
This commit is contained in:
		
							parent
							
								
									7486d5c170
								
							
						
					
					
						commit
						38233af2d7
					
				
					 20 changed files with 174 additions and 305 deletions
				
			
		| 
						 | 
				
			
			@ -1,11 +1,11 @@
 | 
			
		|||
import wrap from "../../utils/wrap.js";
 | 
			
		||||
//import wrap from "../../utils/wrap.js";
 | 
			
		||||
import ImageCommand from "../../classes/imageCommand.js";
 | 
			
		||||
 | 
			
		||||
class SonicCommand extends ImageCommand {
 | 
			
		||||
  params() {
 | 
			
		||||
    const cleanedMessage = (this.type === "classic" ? this.args.join(" ") : this.options.text).replaceAll("&", "\\&").replaceAll(">", "\\>").replaceAll("<", "\\<").replaceAll("\"", "\\"").replaceAll("'", "\\'").replaceAll("%", "\\%");
 | 
			
		||||
    return {
 | 
			
		||||
      text: wrap(cleanedMessage, {width: 15, indent: ""})
 | 
			
		||||
      text: cleanedMessage
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,15 +1,10 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
using namespace vips;
 | 
			
		||||
 | 
			
		||||
/*void finalizer(Napi::Env env, char* data) {
 | 
			
		||||
    free(data);
 | 
			
		||||
}*/
 | 
			
		||||
 | 
			
		||||
Napi::Value Caption(const Napi::CallbackInfo &info) {
 | 
			
		||||
  Napi::Env env = info.Env();
 | 
			
		||||
  try {
 | 
			
		||||
| 
						 | 
				
			
			@ -79,7 +74,6 @@ Napi::Value Caption(const Napi::CallbackInfo &info) {
 | 
			
		|||
    result.Set("type", type);
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (std::exception const &err) {
 | 
			
		||||
    cerr << "Error: " << err.what() << endl;
 | 
			
		||||
    throw Napi::Error::New(env, err.what());
 | 
			
		||||
  } catch (...) {
 | 
			
		||||
    throw Napi::Error::New(env, "Unknown error");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -33,28 +31,12 @@ Napi::Value Colors(const Napi::CallbackInfo &info) {
 | 
			
		|||
    VImage out;
 | 
			
		||||
 | 
			
		||||
    if (color == "blurple") {
 | 
			
		||||
      out = in; // TODO: figure out how to implement blurple
 | 
			
		||||
      out = in;
 | 
			
		||||
    } else if (color == "grayscale") {
 | 
			
		||||
      out = in.colourspace(VIPS_INTERPRETATION_B_W);
 | 
			
		||||
    } else if (color == "sepia") {
 | 
			
		||||
      out = in.flatten().recomb(sepia);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /*for (Image &image : coalesced) {
 | 
			
		||||
      if (color == "blurple") {
 | 
			
		||||
        image.threshold(49151.25);
 | 
			
		||||
        image.levelColors(old ? "#7289DA" : "#5865F2", "white");
 | 
			
		||||
      } else if (color == "grayscale") {
 | 
			
		||||
        image.quantizeColorSpace(GRAYColorspace);
 | 
			
		||||
        image.quantizeColors(256);
 | 
			
		||||
      } else if (color == "sepia") {
 | 
			
		||||
        image.sepiaTone(49151.25);
 | 
			
		||||
      }
 | 
			
		||||
      image.magick(type);
 | 
			
		||||
      image.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
			
		||||
      colored.push_back(image);
 | 
			
		||||
    }*/
 | 
			
		||||
 | 
			
		||||
    if (delay) out.set("delay", delay);
 | 
			
		||||
 | 
			
		||||
    void *buf;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -37,8 +35,10 @@ Napi::Value Crop(const Napi::CallbackInfo &info) {
 | 
			
		|||
      int frameHeight = img_frame.height();
 | 
			
		||||
      bool widthOrHeight = frameWidth / frameHeight >= 1;
 | 
			
		||||
      int size = widthOrHeight ? frameHeight : frameWidth;
 | 
			
		||||
      //img_frame.crop(frameWidth - size, frameHeight - size, size, size);
 | 
			
		||||
      VImage result = img_frame.smartcrop(size, size, VImage::option()->set("interesting", VIPS_INTERESTING_CENTRE));
 | 
			
		||||
      // img_frame.crop(frameWidth - size, frameHeight - size, size, size);
 | 
			
		||||
      VImage result = img_frame.smartcrop(
 | 
			
		||||
          size, size,
 | 
			
		||||
          VImage::option()->set("interesting", VIPS_INTERESTING_CENTRE));
 | 
			
		||||
      finalHeight = size;
 | 
			
		||||
      img.push_back(result);
 | 
			
		||||
    }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,9 @@
 | 
			
		|||
#include <Magick++.h>
 | 
			
		||||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
using namespace Magick;
 | 
			
		||||
using namespace vips;
 | 
			
		||||
 | 
			
		||||
Napi::Value Deepfry(const Napi::CallbackInfo &info) {
 | 
			
		||||
  Napi::Env env = info.Env();
 | 
			
		||||
| 
						 | 
				
			
			@ -17,47 +15,33 @@ Napi::Value Deepfry(const Napi::CallbackInfo &info) {
 | 
			
		|||
    int delay =
 | 
			
		||||
        obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
 | 
			
		||||
 | 
			
		||||
    Blob blob;
 | 
			
		||||
 | 
			
		||||
    list<Image> frames;
 | 
			
		||||
    list<Image> coalesced;
 | 
			
		||||
    list<Image> blurred;
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
			
		||||
 | 
			
		||||
    for (Image &image : coalesced) {
 | 
			
		||||
      Blob temp;
 | 
			
		||||
      image.colorSpace(Magick::sRGBColorspace);
 | 
			
		||||
      image.level(16383.75, 39321);
 | 
			
		||||
      image.quality(1);
 | 
			
		||||
      image.magick("JPEG");
 | 
			
		||||
      image.write(&temp);
 | 
			
		||||
      Image newImage(temp);
 | 
			
		||||
      newImage.magick(type);
 | 
			
		||||
      newImage.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
			
		||||
      blurred.push_back(newImage);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    optimizeTransparency(blurred.begin(), blurred.end());
 | 
			
		||||
 | 
			
		||||
    if (type == "gif") {
 | 
			
		||||
      for (Image &image : blurred) {
 | 
			
		||||
        image.quantizeDitherMethod(FloydSteinbergDitherMethod);
 | 
			
		||||
        image.quantize();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    writeImages(blurred.begin(), blurred.end(), &blob);
 | 
			
		||||
 | 
			
		||||
    Napi::Object result = Napi::Object::New(env);
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
 | 
			
		||||
                                                blob.length()));
 | 
			
		||||
 | 
			
		||||
    VImage in = VImage::new_from_buffer(
 | 
			
		||||
                    data.Data(), data.Length(), "",
 | 
			
		||||
                    VImage::option()->set("access", "sequential")->set("n", -1))
 | 
			
		||||
                    .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
			
		||||
    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
			
		||||
 | 
			
		||||
    int page_height = vips_image_get_page_height(in.get_image());
 | 
			
		||||
 | 
			
		||||
    VImage fried = (in * 1.3 - (255.0 * 1.3 - 255.0)) * 1.5;
 | 
			
		||||
    void *jpgBuf;
 | 
			
		||||
    size_t jpgLength;
 | 
			
		||||
    fried.write_to_buffer(".jpg", &jpgBuf, &jpgLength,
 | 
			
		||||
                          VImage::option()->set("Q", 1)->set("strip", true));
 | 
			
		||||
    VImage final = VImage::new_from_buffer(jpgBuf, jpgLength, "");
 | 
			
		||||
    final.set(VIPS_META_PAGE_HEIGHT, page_height);
 | 
			
		||||
    if (delay) final.set("delay", delay);
 | 
			
		||||
 | 
			
		||||
    void *buf;
 | 
			
		||||
    size_t length;
 | 
			
		||||
    final.write_to_buffer(("." + type).c_str(), &buf, &length,
 | 
			
		||||
                          VImage::option()->set("dither", 0));
 | 
			
		||||
 | 
			
		||||
    vips_thread_shutdown();
 | 
			
		||||
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
			
		||||
    result.Set("type", type);
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (std::exception const &err) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -32,18 +30,24 @@ Napi::Value Flag(const Napi::CallbackInfo &info) {
 | 
			
		|||
    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
			
		||||
 | 
			
		||||
    string assetPath = basePath + overlay;
 | 
			
		||||
    VImage overlayImage = VImage::new_from_file(assetPath.c_str()).thumbnail_image(width, VImage::option()->set("height", page_height)->set("size", VIPS_SIZE_FORCE));
 | 
			
		||||
    VImage overlayInput = VImage::new_from_file(assetPath.c_str());
 | 
			
		||||
    VImage overlayImage = overlayInput.resize(
 | 
			
		||||
        (double)width / (double)overlayInput.width(),
 | 
			
		||||
        VImage::option()->set(
 | 
			
		||||
            "vscale", (double)page_height / (double)overlayInput.height()));
 | 
			
		||||
    if (!overlayImage.has_alpha()) {
 | 
			
		||||
      overlayImage = overlayImage.bandjoin(127);
 | 
			
		||||
    } else {
 | 
			
		||||
      overlayImage = overlayImage * vector<double>{ 1, 1, 1, 0.5 }; // this is a pretty cool line, just saying
 | 
			
		||||
      // this is a pretty cool line, just saying
 | 
			
		||||
      overlayImage = overlayImage * vector<double>{1, 1, 1, 0.5};
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    vector<VImage> img;
 | 
			
		||||
    for (int i = 0; i < n_pages; i++) {
 | 
			
		||||
      VImage img_frame =
 | 
			
		||||
          type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
 | 
			
		||||
      VImage composited = img_frame.composite2(overlayImage, VIPS_BLEND_MODE_OVER);
 | 
			
		||||
      VImage composited =
 | 
			
		||||
          img_frame.composite2(overlayImage, VIPS_BLEND_MODE_OVER);
 | 
			
		||||
      img.push_back(composited);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -58,8 +62,7 @@ Napi::Value Flag(const Napi::CallbackInfo &info) {
 | 
			
		|||
        type == "gif" ? VImage::option()->set("dither", 0) : 0);
 | 
			
		||||
 | 
			
		||||
    Napi::Object result = Napi::Object::New(env);
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf,
 | 
			
		||||
                                                length));
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
			
		||||
    result.Set("type", type);
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (std::exception const &err) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -37,11 +35,10 @@ Napi::Value Gamexplain(const Napi::CallbackInfo &info) {
 | 
			
		|||
    for (int i = 0; i < n_pages; i++) {
 | 
			
		||||
      VImage img_frame =
 | 
			
		||||
          type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
 | 
			
		||||
      VImage resized =
 | 
			
		||||
          img_frame
 | 
			
		||||
              .thumbnail_image(1181, VImage::option()
 | 
			
		||||
                                         ->set("height", 571)
 | 
			
		||||
                                         ->set("size", VIPS_SIZE_FORCE))
 | 
			
		||||
      VImage resized = img_frame
 | 
			
		||||
                           .resize(1181.0 / (double)width,
 | 
			
		||||
                                   VImage::option()->set(
 | 
			
		||||
                                       "vscale", 571.0 / (double)page_height))
 | 
			
		||||
                           .embed(10, 92, 1200, 675,
 | 
			
		||||
                                  VImage::option()->set("extend", "white"));
 | 
			
		||||
      VImage composited = resized.composite2(tmpl, VIPS_BLEND_MODE_OVER);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,6 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -30,22 +28,14 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) {
 | 
			
		|||
              .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
			
		||||
      if (!in.has_alpha()) in = in.bandjoin(255);
 | 
			
		||||
 | 
			
		||||
      int width = in.width();
 | 
			
		||||
      int page_height = vips_image_get_page_height(in.get_image());
 | 
			
		||||
      int n_pages = vips_image_get_n_pages(in.get_image());
 | 
			
		||||
 | 
			
		||||
      vector<VImage> img;
 | 
			
		||||
      for (int i = 0; i < n_pages; i++) {
 | 
			
		||||
        VImage img_frame = in.crop(0, i * page_height, width, page_height);
 | 
			
		||||
        void *buf;
 | 
			
		||||
        size_t length;
 | 
			
		||||
        img_frame.write_to_buffer(
 | 
			
		||||
            ".jpg", &buf, &length,
 | 
			
		||||
      void *jpgBuf;
 | 
			
		||||
      size_t jpgLength;
 | 
			
		||||
      in.write_to_buffer(
 | 
			
		||||
          ".jpg", &jpgBuf, &jpgLength,
 | 
			
		||||
          VImage::option()->set("Q", quality)->set("strip", true));
 | 
			
		||||
        VImage jpeg = VImage::new_from_buffer(buf, length, "");
 | 
			
		||||
        img.push_back(jpeg);
 | 
			
		||||
      }
 | 
			
		||||
      VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
			
		||||
      VImage final = VImage::new_from_buffer(jpgBuf, jpgLength, "");
 | 
			
		||||
      final.set(VIPS_META_PAGE_HEIGHT, page_height);
 | 
			
		||||
      if (delay) final.set("delay", delay);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -46,7 +44,8 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
 | 
			
		|||
 | 
			
		||||
    if (dividedWidth >= 1) {
 | 
			
		||||
      altMask = VImage::black(dividedWidth * 2 + 1, dividedWidth * 2 + 1) + 128;
 | 
			
		||||
      altMask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth, VImage::option()->set("fill", true));
 | 
			
		||||
      altMask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth,
 | 
			
		||||
                          VImage::option()->set("fill", true));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    VImage topText;
 | 
			
		||||
| 
						 | 
				
			
			@ -66,7 +65,8 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
 | 
			
		|||
          topIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
 | 
			
		||||
              .gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
 | 
			
		||||
      if (dividedWidth >= 1) {
 | 
			
		||||
        topOutline = topOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
 | 
			
		||||
        topOutline =
 | 
			
		||||
            topOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
 | 
			
		||||
      }
 | 
			
		||||
      topOutline = (topOutline == (vector<double>){0, 0, 0, 0});
 | 
			
		||||
      VImage topInvert = topOutline.extract_band(3).invert();
 | 
			
		||||
| 
						 | 
				
			
			@ -93,7 +93,8 @@ Napi::Value Meme(const Napi::CallbackInfo &info) {
 | 
			
		|||
          bottomIn.morph(mask, VIPS_OPERATION_MORPHOLOGY_DILATE)
 | 
			
		||||
              .gaussblur(0.5, VImage::option()->set("min_ampl", 0.1));
 | 
			
		||||
      if (dividedWidth >= 1) {
 | 
			
		||||
        bottomOutline = bottomOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
 | 
			
		||||
        bottomOutline =
 | 
			
		||||
            bottomOutline.morph(altMask, VIPS_OPERATION_MORPHOLOGY_DILATE);
 | 
			
		||||
      }
 | 
			
		||||
      bottomOutline = (bottomOutline == (vector<double>){0, 0, 0, 0});
 | 
			
		||||
      VImage bottomInvert = bottomOutline.extract_band(3).invert();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,9 @@
 | 
			
		|||
#include <Magick++.h>
 | 
			
		||||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
using namespace Magick;
 | 
			
		||||
using namespace vips;
 | 
			
		||||
 | 
			
		||||
Napi::Value Reddit(const Napi::CallbackInfo &info) {
 | 
			
		||||
  Napi::Env env = info.Env();
 | 
			
		||||
| 
						 | 
				
			
			@ -18,62 +16,58 @@ Napi::Value Reddit(const Napi::CallbackInfo &info) {
 | 
			
		|||
    int delay =
 | 
			
		||||
        obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
 | 
			
		||||
 | 
			
		||||
    Blob blob;
 | 
			
		||||
    VOption *options = VImage::option()->set("access", "sequential");
 | 
			
		||||
 | 
			
		||||
    list<Image> frames;
 | 
			
		||||
    list<Image> coalesced;
 | 
			
		||||
    list<Image> mid;
 | 
			
		||||
    Image watermark;
 | 
			
		||||
    Image text_image;
 | 
			
		||||
    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;
 | 
			
		||||
    VImage in =
 | 
			
		||||
        VImage::new_from_buffer(data.Data(), data.Length(), "",
 | 
			
		||||
                                type == "gif" ? options->set("n", -1) : options)
 | 
			
		||||
            .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
			
		||||
    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
			
		||||
 | 
			
		||||
    string assetPath = basePath + "assets/images/reddit.png";
 | 
			
		||||
    VImage tmpl = VImage::new_from_file(assetPath.c_str());
 | 
			
		||||
 | 
			
		||||
    int width = in.width();
 | 
			
		||||
    int page_height = vips_image_get_page_height(in.get_image());
 | 
			
		||||
    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
			
		||||
 | 
			
		||||
    string captionText = "<span foreground=\"white\">" + text + "</span>";
 | 
			
		||||
 | 
			
		||||
    VImage textImage =
 | 
			
		||||
        VImage::text(captionText.c_str(), VImage::option()
 | 
			
		||||
                                              ->set("rgba", true)
 | 
			
		||||
                                              ->set("font", "Roboto 62")
 | 
			
		||||
                                              ->set("align", VIPS_ALIGN_LOW));
 | 
			
		||||
 | 
			
		||||
    VImage composited =
 | 
			
		||||
        tmpl.composite2(textImage, VIPS_BLEND_MODE_OVER,
 | 
			
		||||
                        VImage::option()->set("x", 375)->set(
 | 
			
		||||
                            "y", (tmpl.height() - textImage.height()) - 64));
 | 
			
		||||
    VImage watermark =
 | 
			
		||||
        composited.resize((double)width / (double)composited.width());
 | 
			
		||||
 | 
			
		||||
    vector<VImage> img;
 | 
			
		||||
    for (int i = 0; i < n_pages; i++) {
 | 
			
		||||
      VImage img_frame =
 | 
			
		||||
          type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
 | 
			
		||||
      VImage frame = img_frame.join(watermark, VIPS_DIRECTION_VERTICAL,
 | 
			
		||||
                                    VImage::option()->set("expand", true));
 | 
			
		||||
      img.push_back(frame);
 | 
			
		||||
    }
 | 
			
		||||
    VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
			
		||||
    final.set(VIPS_META_PAGE_HEIGHT, page_height + watermark.height());
 | 
			
		||||
    if (delay) final.set("delay", delay);
 | 
			
		||||
 | 
			
		||||
    watermark.read(basePath + "assets/images/reddit.png");
 | 
			
		||||
    text_image.textGravity(Magick::WestGravity);
 | 
			
		||||
    text_image.font("Roboto");
 | 
			
		||||
    text_image.fontPointsize(47);
 | 
			
		||||
    text_image.backgroundColor("none");
 | 
			
		||||
    text_image.read("pango:<span foreground='white'>" + text + "</span>");
 | 
			
		||||
    void *buf;
 | 
			
		||||
    size_t length;
 | 
			
		||||
    final.write_to_buffer(
 | 
			
		||||
        ("." + type).c_str(), &buf, &length,
 | 
			
		||||
        type == "gif" ? VImage::option()->set("dither", 0) : 0);
 | 
			
		||||
 | 
			
		||||
    watermark.composite(text_image, Geometry("+375+46"),
 | 
			
		||||
                        Magick::OverCompositeOp);
 | 
			
		||||
 | 
			
		||||
    string query(to_string(frames.front().baseColumns()) + "x");
 | 
			
		||||
    watermark.scale(Geometry(query));
 | 
			
		||||
 | 
			
		||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
			
		||||
 | 
			
		||||
    for (Image &image : coalesced) {
 | 
			
		||||
      Image final;
 | 
			
		||||
      list<Image> to_append;
 | 
			
		||||
      to_append.push_back(image);
 | 
			
		||||
      to_append.push_back(watermark);
 | 
			
		||||
      appendImages(&final, to_append.begin(), to_append.end(), true);
 | 
			
		||||
      final.repage();
 | 
			
		||||
      image.magick(type);
 | 
			
		||||
      final.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
			
		||||
      mid.push_back(final);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
			
		||||
 | 
			
		||||
    if (type == "gif") {
 | 
			
		||||
      for (Image &image : mid) {
 | 
			
		||||
        image.quantizeDitherMethod(FloydSteinbergDitherMethod);
 | 
			
		||||
        image.quantize();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    writeImages(mid.begin(), mid.end(), &blob);
 | 
			
		||||
    vips_thread_shutdown();
 | 
			
		||||
 | 
			
		||||
    Napi::Object result = Napi::Object::New(env);
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
 | 
			
		||||
                                                blob.length()));
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
			
		||||
    result.Set("type", type);
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (std::exception const &err) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,11 +1,9 @@
 | 
			
		|||
#include <Magick++.h>
 | 
			
		||||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
using namespace Magick;
 | 
			
		||||
using namespace vips;
 | 
			
		||||
 | 
			
		||||
Napi::Value Resize(const Napi::CallbackInfo &info) {
 | 
			
		||||
  Napi::Env env = info.Env();
 | 
			
		||||
| 
						 | 
				
			
			@ -22,49 +20,43 @@ Napi::Value Resize(const Napi::CallbackInfo &info) {
 | 
			
		|||
    int delay =
 | 
			
		||||
        obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
 | 
			
		||||
 | 
			
		||||
    Blob blob;
 | 
			
		||||
    VOption *options = VImage::option()->set("access", "sequential");
 | 
			
		||||
 | 
			
		||||
    list<Image> frames;
 | 
			
		||||
    list<Image> coalesced;
 | 
			
		||||
    list<Image> blurred;
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
			
		||||
    VImage in =
 | 
			
		||||
        VImage::new_from_buffer(data.Data(), data.Length(), "",
 | 
			
		||||
                                type == "gif" ? options->set("n", -1) : options)
 | 
			
		||||
            .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
			
		||||
 | 
			
		||||
    for (Image &image : coalesced) {
 | 
			
		||||
    VImage out;
 | 
			
		||||
 | 
			
		||||
    int width = in.width();
 | 
			
		||||
    int page_height = vips_image_get_page_height(in.get_image());
 | 
			
		||||
 | 
			
		||||
    int finalHeight;
 | 
			
		||||
    if (stretch) {
 | 
			
		||||
        image.resize(Geometry("512x512!"));
 | 
			
		||||
      out = in.resize(
 | 
			
		||||
          512.0 / (double)width,
 | 
			
		||||
          VImage::option()->set("vscale", 512.0 / (double)page_height));
 | 
			
		||||
      finalHeight = 512;
 | 
			
		||||
    } else if (wide) {
 | 
			
		||||
        image.resize(Geometry(to_string((image.baseColumns() * 19) / 2) + "x" +
 | 
			
		||||
                              to_string(image.baseRows() / 2) + "!"));
 | 
			
		||||
      out = in.resize(9.5, VImage::option()->set("vscale", 0.5));
 | 
			
		||||
      finalHeight = page_height / 2;
 | 
			
		||||
    } else {
 | 
			
		||||
        image.scale(Geometry("10%"));
 | 
			
		||||
        image.scale(Geometry("1000%"));
 | 
			
		||||
      }
 | 
			
		||||
      image.magick(type);
 | 
			
		||||
      blurred.push_back(image);
 | 
			
		||||
      out = in.resize(0.1).resize(
 | 
			
		||||
          10, VImage::option()->set("kernel", VIPS_KERNEL_NEAREST));
 | 
			
		||||
      finalHeight = page_height;
 | 
			
		||||
    }
 | 
			
		||||
    out.set(VIPS_META_PAGE_HEIGHT, finalHeight);
 | 
			
		||||
    if (delay) out.set("delay", delay);
 | 
			
		||||
 | 
			
		||||
    optimizeTransparency(blurred.begin(), blurred.end());
 | 
			
		||||
    void *buf;
 | 
			
		||||
    size_t length;
 | 
			
		||||
    out.write_to_buffer(("." + type).c_str(), &buf, &length);
 | 
			
		||||
 | 
			
		||||
    if (type == "gif") {
 | 
			
		||||
      for (Image &image : blurred) {
 | 
			
		||||
        image.quantizeDitherMethod(FloydSteinbergDitherMethod);
 | 
			
		||||
        image.quantize();
 | 
			
		||||
        if (delay != 0) image.animationDelay(delay);
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    writeImages(blurred.begin(), blurred.end(), &blob);
 | 
			
		||||
    vips_thread_shutdown();
 | 
			
		||||
 | 
			
		||||
    Napi::Object result = Napi::Object::New(env);
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
 | 
			
		||||
                                                blob.length()));
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
			
		||||
    result.Set("type", type);
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (std::exception const &err) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,10 +1,9 @@
 | 
			
		|||
#include <Magick++.h>
 | 
			
		||||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
using namespace Magick;
 | 
			
		||||
using namespace vips;
 | 
			
		||||
 | 
			
		||||
Napi::Value Sonic(const Napi::CallbackInfo &info) {
 | 
			
		||||
  Napi::Env env = info.Env();
 | 
			
		||||
| 
						 | 
				
			
			@ -14,25 +13,30 @@ Napi::Value Sonic(const Napi::CallbackInfo &info) {
 | 
			
		|||
    string text = obj.Get("text").As<Napi::String>().Utf8Value();
 | 
			
		||||
    string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
 | 
			
		||||
 | 
			
		||||
    Blob blob;
 | 
			
		||||
    string assetPath = basePath + "assets/images/sonic.jpg";
 | 
			
		||||
    VImage bg = VImage::new_from_file(assetPath.c_str());
 | 
			
		||||
 | 
			
		||||
    Image image;
 | 
			
		||||
    Image text_image;
 | 
			
		||||
    text_image.backgroundColor("none");
 | 
			
		||||
    text_image.fontPointsize(72);
 | 
			
		||||
    text_image.textGravity(Magick::CenterGravity);
 | 
			
		||||
    text_image.font("Bitstream Vera Sans");
 | 
			
		||||
    text_image.read("pango:<span foreground='white'>" + text + "</span>");
 | 
			
		||||
    text_image.resize(Geometry(474, 332));
 | 
			
		||||
    text_image.extent(Geometry("1024x538-435-145"), Magick::CenterGravity);
 | 
			
		||||
    image.read(basePath + "assets/images/sonic.jpg");
 | 
			
		||||
    image.composite(text_image, Geometry("+160+10"), Magick::OverCompositeOp);
 | 
			
		||||
    image.magick("PNG");
 | 
			
		||||
    image.write(&blob);
 | 
			
		||||
    VImage textImage =
 | 
			
		||||
        VImage::text(("<span foreground=\"white\">" + text + "</span>").c_str(),
 | 
			
		||||
                     VImage::option()
 | 
			
		||||
                         ->set("rgba", true)
 | 
			
		||||
                         ->set("align", VIPS_ALIGN_CENTRE)
 | 
			
		||||
                         ->set("font", "Bitstream Vera Sans")
 | 
			
		||||
                         ->set("width", 542)
 | 
			
		||||
                         ->set("height", 390))
 | 
			
		||||
            .gravity(VIPS_COMPASS_DIRECTION_CENTRE, 542, 390);
 | 
			
		||||
 | 
			
		||||
    VImage out = bg.composite2(textImage, VIPS_BLEND_MODE_OVER,
 | 
			
		||||
                               VImage::option()->set("x", 391)->set("y", 84));
 | 
			
		||||
 | 
			
		||||
    void *buf;
 | 
			
		||||
    size_t length;
 | 
			
		||||
    out.write_to_buffer(".png", &buf, &length);
 | 
			
		||||
 | 
			
		||||
    vips_thread_shutdown();
 | 
			
		||||
 | 
			
		||||
    Napi::Object result = Napi::Object::New(env);
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
 | 
			
		||||
                                                blob.length()));
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
			
		||||
    result.Set("type", "png");
 | 
			
		||||
    return result;
 | 
			
		||||
  } catch (std::exception const &err) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -46,12 +44,15 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
 | 
			
		|||
    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
			
		||||
 | 
			
		||||
    if (resize && append) {
 | 
			
		||||
      watermark = watermark.thumbnail_image(width);
 | 
			
		||||
      watermark = watermark.resize((double)width / (double)watermark.width());
 | 
			
		||||
    } else if (resize && yscale) {
 | 
			
		||||
      watermark = watermark.thumbnail_image(width, VImage::option()->set("height", page_height * yscale)->set("size", VIPS_SIZE_FORCE));
 | 
			
		||||
      watermark = watermark.resize(
 | 
			
		||||
          (double)width / (double)watermark.width(),
 | 
			
		||||
          VImage::option()->set("vscale", (double)(page_height * yscale) /
 | 
			
		||||
                                              (double)watermark.height()));
 | 
			
		||||
    } else if (resize) {
 | 
			
		||||
      watermark = watermark.thumbnail_image(
 | 
			
		||||
          VIPS_MAX_COORD, VImage::option()->set("height", page_height));
 | 
			
		||||
      watermark =
 | 
			
		||||
          watermark.resize((double)page_height / (double)watermark.height());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    vector<VImage> img;
 | 
			
		||||
| 
						 | 
				
			
			@ -121,65 +122,6 @@ Napi::Value Watermark(const Napi::CallbackInfo &info) {
 | 
			
		|||
 | 
			
		||||
    vips_thread_shutdown();
 | 
			
		||||
 | 
			
		||||
    /*list<Image> frames;
 | 
			
		||||
    list<Image> coalesced;
 | 
			
		||||
    list<Image> mid;
 | 
			
		||||
    Image watermark;
 | 
			
		||||
    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;
 | 
			
		||||
    }
 | 
			
		||||
    string merged = basePath + water;
 | 
			
		||||
    watermark.read(merged);
 | 
			
		||||
    if (resize && append) {
 | 
			
		||||
      string query(to_string(frames.front().baseColumns()) + "x");
 | 
			
		||||
      watermark.scale(Geometry(query));
 | 
			
		||||
    } else if (resize && yscale) {
 | 
			
		||||
      string query(to_string(frames.front().baseColumns()) + "x" +
 | 
			
		||||
                   to_string(frames.front().baseRows() * yscale) + "!");
 | 
			
		||||
      watermark.resize(Geometry(query));
 | 
			
		||||
    } else if (resize) {
 | 
			
		||||
      string query("x" + to_string(frames.front().baseRows()));
 | 
			
		||||
      watermark.scale(Geometry(query));
 | 
			
		||||
    }
 | 
			
		||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
			
		||||
 | 
			
		||||
    for (Image &image : coalesced) {
 | 
			
		||||
      Image final;
 | 
			
		||||
      if (append) {
 | 
			
		||||
        list<Image> to_append;
 | 
			
		||||
        to_append.push_back(image);
 | 
			
		||||
        to_append.push_back(watermark);
 | 
			
		||||
        appendImages(&final, to_append.begin(), to_append.end(), true);
 | 
			
		||||
        final.repage();
 | 
			
		||||
      } else if (mc) {
 | 
			
		||||
        image.backgroundColor("white");
 | 
			
		||||
        image.extent(Geometry(image.columns(), image.rows() + 15));
 | 
			
		||||
        image.composite(watermark, gravity, Magick::OverCompositeOp);
 | 
			
		||||
        final = image;
 | 
			
		||||
      } else {
 | 
			
		||||
        image.composite(watermark, gravity, Magick::OverCompositeOp);
 | 
			
		||||
        final = image;
 | 
			
		||||
      }
 | 
			
		||||
      image.magick(type);
 | 
			
		||||
      final.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
			
		||||
      mid.push_back(final);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
			
		||||
 | 
			
		||||
    if (type == "gif") {
 | 
			
		||||
      for (Image &image : mid) {
 | 
			
		||||
        image.quantizeDitherMethod(FloydSteinbergDitherMethod);
 | 
			
		||||
        image.quantize();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    writeImages(mid.begin(), mid.end(), &blob);*/
 | 
			
		||||
 | 
			
		||||
    Napi::Object result = Napi::Object::New(env);
 | 
			
		||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
			
		||||
    result.Set("type", type);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,7 +1,5 @@
 | 
			
		|||
#include <napi.h>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
#include <list>
 | 
			
		||||
#include <vips/vips8>
 | 
			
		||||
 | 
			
		||||
using namespace std;
 | 
			
		||||
| 
						 | 
				
			
			@ -40,7 +38,6 @@ Napi::Value Whisper(const Napi::CallbackInfo &info) {
 | 
			
		|||
      mask = VImage::black(dividedWidth * 2 + 1, dividedWidth * 2 + 1) + 128;
 | 
			
		||||
      mask.draw_circle({255}, dividedWidth, dividedWidth, dividedWidth,
 | 
			
		||||
                       VImage::option()->set("fill", true));
 | 
			
		||||
      mask.write_to_file("/home/esm/mask.png");
 | 
			
		||||
    } else {
 | 
			
		||||
      mask = VImage::black(rad * 2 + 1, rad * 2 + 1) + 128;
 | 
			
		||||
      mask.draw_circle({255}, rad, rad, rad,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue