how the hell did I manage to miss squish
This commit is contained in:
		
							parent
							
								
									47b2d9fcff
								
							
						
					
					
						commit
						89caa43068
					
				
					 3 changed files with 45 additions and 70 deletions
				
			
		| 
						 | 
					@ -78,6 +78,7 @@ std::map<std::string, char* (*)(string *type, char* BufferData, size_t BufferLen
 | 
				
			||||||
  {"snapchat", Snapchat},
 | 
					  {"snapchat", Snapchat},
 | 
				
			||||||
  {"speed", &Speed},
 | 
					  {"speed", &Speed},
 | 
				
			||||||
  {"spin", Spin},
 | 
					  {"spin", Spin},
 | 
				
			||||||
 | 
					  {"squish", Squish},
 | 
				
			||||||
  {"swirl", Swirl},
 | 
					  {"swirl", Swirl},
 | 
				
			||||||
  {"tile", Tile},
 | 
					  {"tile", Tile},
 | 
				
			||||||
  {"togif", ToGif},
 | 
					  {"togif", ToGif},
 | 
				
			||||||
| 
						 | 
					@ -105,7 +106,7 @@ bool isNapiValueInt(Napi::Env& env, Napi::Value& num) {
 | 
				
			||||||
      .Value();
 | 
					      .Value();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
 | 
					Napi::Value ProcessImage(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
  Napi::Object result = Napi::Object::New(env);
 | 
					  Napi::Object result = Napi::Object::New(env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -145,7 +146,7 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    size_t length = 0;
 | 
					    size_t length = 0;
 | 
				
			||||||
    char* buf;
 | 
					    char* buf;
 | 
				
			||||||
    if (input) {
 | 
					    if (obj.Has("data")) {
 | 
				
			||||||
      Napi::Buffer<char> data = obj.Has("data") ? obj.Get("data").As<Napi::Buffer<char>>() : Napi::Buffer<char>::New(env, 0);
 | 
					      Napi::Buffer<char> data = obj.Has("data") ? obj.Get("data").As<Napi::Buffer<char>>() : Napi::Buffer<char>::New(env, 0);
 | 
				
			||||||
      buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length);
 | 
					      buf = FunctionMap.at(command)(&type, data.Data(), data.Length(), Arguments, &length);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
| 
						 | 
					@ -164,21 +165,6 @@ Napi::Value NewProcessImage(const Napi::CallbackInfo &info, bool input) {
 | 
				
			||||||
  return result;
 | 
					  return result;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Napi::Value ProcessImage(const Napi::CallbackInfo &info) { // janky solution for gradual adoption
 | 
					 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  string command = info[0].As<Napi::String>().Utf8Value();
 | 
					 | 
				
			||||||
  
 | 
					 | 
				
			||||||
  if (MAP_HAS(FunctionMap, command)) {
 | 
					 | 
				
			||||||
    return NewProcessImage(info, true);
 | 
					 | 
				
			||||||
  } else if (MAP_HAS(NoInputFunctionMap, command)) {
 | 
					 | 
				
			||||||
    return NewProcessImage(info, false);
 | 
					 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    Napi::Error::New(env, "Invalid command").ThrowAsJavaScriptException();
 | 
					 | 
				
			||||||
    return env.Null();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
Napi::Object Init(Napi::Env env, Napi::Object exports){
 | 
					Napi::Object Init(Napi::Env env, Napi::Object exports){
 | 
				
			||||||
#ifdef _WIN32
 | 
					#ifdef _WIN32
 | 
				
			||||||
  Magick::InitializeMagick("");
 | 
					  Magick::InitializeMagick("");
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,26 +1,20 @@
 | 
				
			||||||
 | 
					#include "common.h"
 | 
				
			||||||
#include <math.h>
 | 
					#include <math.h>
 | 
				
			||||||
#include <napi.h>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <vips/vips8>
 | 
					#include <vips/vips8>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
using namespace vips;
 | 
					using namespace vips;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Napi::Value Squish(const Napi::CallbackInfo &info) {
 | 
					char *Squish(string *type, char *BufferData, size_t BufferLength,
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					             ArgumentMap Arguments, size_t *DataSize) {
 | 
				
			||||||
  Napi::Object result = Napi::Object::New(env);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  try {
 | 
					 | 
				
			||||||
    Napi::Object obj = info[1].As<Napi::Object>();
 | 
					 | 
				
			||||||
    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
					 | 
				
			||||||
    string type = obj.Get("type").As<Napi::String>().Utf8Value();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  VOption *options = VImage::option();
 | 
					  VOption *options = VImage::option();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  VImage in =
 | 
					  VImage in =
 | 
				
			||||||
      VImage::new_from_buffer(
 | 
					      VImage::new_from_buffer(
 | 
				
			||||||
            data.Data(), data.Length(), "",
 | 
					          BufferData, BufferLength, "",
 | 
				
			||||||
            type == "gif" ? options->set("n", -1)->set("access", "sequential")
 | 
					          *type == "gif" ? options->set("n", -1)->set("access", "sequential")
 | 
				
			||||||
                         : options)
 | 
					                         : options)
 | 
				
			||||||
          .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
					          .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
  if (!in.has_alpha())
 | 
					  if (!in.has_alpha())
 | 
				
			||||||
| 
						 | 
					@ -28,13 +22,13 @@ Napi::Value Squish(const Napi::CallbackInfo &info) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  int width = in.width();
 | 
					  int width = in.width();
 | 
				
			||||||
  int pageHeight = vips_image_get_page_height(in.get_image());
 | 
					  int pageHeight = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
    int nPages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
 | 
					  int nPages = *type == "gif" ? vips_image_get_n_pages(in.get_image()) : 30;
 | 
				
			||||||
  double mult = (2 * M_PI) / nPages;
 | 
					  double mult = (2 * M_PI) / nPages;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  vector<VImage> img;
 | 
					  vector<VImage> img;
 | 
				
			||||||
  for (int i = 0; i < nPages; i++) {
 | 
					  for (int i = 0; i < nPages; i++) {
 | 
				
			||||||
    VImage img_frame =
 | 
					    VImage img_frame =
 | 
				
			||||||
          type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
 | 
					        *type == "gif" ? in.crop(0, i * pageHeight, width, pageHeight) : in;
 | 
				
			||||||
    double newWidth = (sin(i * mult) / 4) + 0.75;
 | 
					    double newWidth = (sin(i * mult) / 4) + 0.75;
 | 
				
			||||||
    double newHeight = (cos(i * mult) / 4) + 0.75;
 | 
					    double newHeight = (cos(i * mult) / 4) + 0.75;
 | 
				
			||||||
    VImage resized =
 | 
					    VImage resized =
 | 
				
			||||||
| 
						 | 
					@ -44,24 +38,17 @@ Napi::Value Squish(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
					  VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
  final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
 | 
					  final.set(VIPS_META_PAGE_HEIGHT, pageHeight);
 | 
				
			||||||
    if (type != "gif") {
 | 
					  if (*type != "gif") {
 | 
				
			||||||
    vector<int> delay(30, 50);
 | 
					    vector<int> delay(30, 50);
 | 
				
			||||||
    final.set("delay", delay);
 | 
					    final.set("delay", delay);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  void *buf;
 | 
					  void *buf;
 | 
				
			||||||
    size_t length;
 | 
					  final.write_to_buffer(".gif", &buf, DataSize);
 | 
				
			||||||
    final.write_to_buffer(".gif", &buf, &length);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
					  *type = "gif";
 | 
				
			||||||
    result.Set("type", "gif");
 | 
					 | 
				
			||||||
  } catch (std::exception const &err) {
 | 
					 | 
				
			||||||
    Napi::Error::New(env, err.what()).ThrowAsJavaScriptException();
 | 
					 | 
				
			||||||
  } catch (...) {
 | 
					 | 
				
			||||||
    Napi::Error::New(env, "Unknown error").ThrowAsJavaScriptException();
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  vips_error_clear();
 | 
					  vips_error_clear();
 | 
				
			||||||
  vips_thread_shutdown();
 | 
					  vips_thread_shutdown();
 | 
				
			||||||
  return result;
 | 
					  return (char *)buf;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,5 +1,7 @@
 | 
				
			||||||
#pragma once
 | 
					#pragma once
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <napi.h>
 | 
					#include "common.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Napi::Value Squish(const Napi::CallbackInfo& info);
 | 
					using std::string;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					char* Squish(string* type, char* BufferData, size_t BufferLength, ArgumentMap Arguments, size_t* DataSize);
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue