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