Port motivate, globe (assets need work), and reverse
This commit is contained in:
		
							parent
							
								
									05795313c4
								
							
						
					
					
						commit
						0162997792
					
				
					 6 changed files with 174 additions and 155 deletions
				
			
		
							
								
								
									
										
											BIN
										
									
								
								assets/images/globediffuse.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/images/globediffuse.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 24 KiB  | 
							
								
								
									
										
											BIN
										
									
								
								assets/images/globespec.png
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								assets/images/globespec.png
									
										
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							| 
		 After Width: | Height: | Size: 27 KiB  | 
										
											Binary file not shown.
										
									
								
							| 
		 Before Width: | Height: | Size: 283 KiB After Width: | Height: | Size: 29 KiB  | 
							
								
								
									
										109
									
								
								natives/globe.cc
									
										
									
									
									
								
							
							
						
						
									
										109
									
								
								natives/globe.cc
									
										
									
									
									
								
							| 
						 | 
					@ -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 Globe(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Globe(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -15,66 +13,65 @@ Napi::Value Globe(const Napi::CallbackInfo &info) {
 | 
				
			||||||
    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
					    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
				
			||||||
    string type = obj.Get("type").As<Napi::String>().Utf8Value();
 | 
					    string type = obj.Get("type").As<Napi::String>().Utf8Value();
 | 
				
			||||||
    string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
 | 
					    string basePath = obj.Get("basePath").As<Napi::String>().Utf8Value();
 | 
				
			||||||
    int delay =
 | 
					 | 
				
			||||||
        obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Blob blob;
 | 
					    VOption *options = VImage::option();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    list<Image> frames;
 | 
					    VImage in =
 | 
				
			||||||
    list<Image> coalesced;
 | 
					        VImage::new_from_buffer(
 | 
				
			||||||
    list<Image> mid;
 | 
					            data.Data(), data.Length(), "",
 | 
				
			||||||
    Image distort;
 | 
					            type == "gif" ? options->set("n", -1)->set("access", "sequential")
 | 
				
			||||||
    Image overlay;
 | 
					                          : options)
 | 
				
			||||||
    try {
 | 
					            .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
      readImages(&frames, Blob(data.Data(), data.Length()));
 | 
					    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
				
			||||||
    } catch (Magick::WarningCoder &warning) {
 | 
					
 | 
				
			||||||
      cerr << "Coder Warning: " << warning.what() << endl;
 | 
					    int width = in.width();
 | 
				
			||||||
    } catch (Magick::Warning &warning) {
 | 
					    int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
      cerr << "Warning: " << warning.what() << endl;
 | 
					    int n_pages = type == "gif" ? vips_image_get_n_pages(in.get_image()) : 29;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    double size = min(width, page_height);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    string diffPath = basePath + "assets/images/globediffuse.png";
 | 
				
			||||||
 | 
					    VImage diffuse =
 | 
				
			||||||
 | 
					        VImage::new_from_file(diffPath.c_str()).resize(size / 500.0) / 255;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    string specPath = basePath + "assets/images/globespec.png";
 | 
				
			||||||
 | 
					    VImage specular =
 | 
				
			||||||
 | 
					        VImage::new_from_file(specPath.c_str()).resize(size / 500.0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    string distortPath = basePath + "assets/images/spheremap.png";
 | 
				
			||||||
 | 
					    VImage distort =
 | 
				
			||||||
 | 
					        (VImage::new_from_file(distortPath.c_str()).resize(size / 500.0) /
 | 
				
			||||||
 | 
					         255) *
 | 
				
			||||||
 | 
					        size;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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 resized = img_frame.resize(
 | 
				
			||||||
 | 
					          size / (double)width,
 | 
				
			||||||
 | 
					          VImage::option()->set("vscale", size / (double)page_height));
 | 
				
			||||||
 | 
					      VImage rolled = resized.wrap(
 | 
				
			||||||
 | 
					          VImage::option()->set("x", page_height * i / n_pages)->set("y", 0));
 | 
				
			||||||
 | 
					      VImage extracted = rolled.extract_band(0, VImage::option()->set("n", 3));
 | 
				
			||||||
 | 
					      VImage mapped = extracted.mapim(distort);
 | 
				
			||||||
 | 
					      VImage composited = mapped * diffuse + specular;
 | 
				
			||||||
 | 
					      VImage frame = composited.bandjoin(diffuse > 0.0);
 | 
				
			||||||
 | 
					      img.push_back(frame);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    distort.read(basePath + "assets/images/spheremap.png");
 | 
					    VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
    overlay.read(basePath + "assets/images/sphere_overlay.png");
 | 
					    final.set(VIPS_META_PAGE_HEIGHT, page_height);
 | 
				
			||||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (type != "gif") {
 | 
					    if (type != "gif") {
 | 
				
			||||||
      list<Image>::iterator it = coalesced.begin();
 | 
					      vector<int> delay(30, 50);
 | 
				
			||||||
      for (int i = 0; i < 29; ++i) {
 | 
					      final.set("delay", delay);
 | 
				
			||||||
        coalesced.push_back(*it);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    int i = 0;
 | 
					    void *buf;
 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					    size_t length;
 | 
				
			||||||
      image.scale(Geometry("500x500!"));
 | 
					    final.write_to_buffer(".gif", &buf, &length);
 | 
				
			||||||
      image.alphaChannel(Magick::SetAlphaChannel);
 | 
					 | 
				
			||||||
      size_t width = image.columns();
 | 
					 | 
				
			||||||
      image.roll(Geometry("+" + to_string(width * i / coalesced.size())));
 | 
					 | 
				
			||||||
      image.composite(overlay, Magick::CenterGravity,
 | 
					 | 
				
			||||||
                      Magick::HardLightCompositeOp);
 | 
					 | 
				
			||||||
      image.composite(distort, Magick::CenterGravity,
 | 
					 | 
				
			||||||
                      Magick::DistortCompositeOp);
 | 
					 | 
				
			||||||
      image.magick("GIF");
 | 
					 | 
				
			||||||
      mid.push_back(image);
 | 
					 | 
				
			||||||
      i++;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
					 | 
				
			||||||
    if (delay != 0) {
 | 
					 | 
				
			||||||
      for_each(mid.begin(), mid.end(), animationDelayImage(delay));
 | 
					 | 
				
			||||||
    } else if (type != "gif") {
 | 
					 | 
				
			||||||
      for_each(mid.begin(), mid.end(), animationDelayImage(5));
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    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", "gif");
 | 
					    result.Set("type", "gif");
 | 
				
			||||||
    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 Motivate(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Motivate(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -20,85 +18,102 @@ Napi::Value Motivate(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 top;
 | 
					            .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
    Image bottom;
 | 
					    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
				
			||||||
    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());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    top.size(Geometry("600"));
 | 
					    int width = in.width();
 | 
				
			||||||
    top.backgroundColor("black");
 | 
					    int size = width / 5;
 | 
				
			||||||
    top.font("Times");
 | 
					    int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
    top.textGravity(Magick::CenterGravity);
 | 
					    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
				
			||||||
    top.fontPointsize(56);
 | 
					    int textWidth = width - ((width / 25) * 2);
 | 
				
			||||||
    top.read("pango:<span font_family=\"" +
 | 
					 | 
				
			||||||
                       (font == "roboto" ? "Roboto Condensed" : font) +
 | 
					 | 
				
			||||||
                       "\" foreground='white'>" + top_text + "</span>");
 | 
					 | 
				
			||||||
    top.extent(Geometry(bottom_text != "" ? to_string(top.columns()) + "x" +
 | 
					 | 
				
			||||||
                                                to_string(top.rows())
 | 
					 | 
				
			||||||
                                          : to_string(top.columns()) + "x" +
 | 
					 | 
				
			||||||
                                                to_string(top.rows() + 20)),
 | 
					 | 
				
			||||||
               "black", Magick::NorthGravity);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    string font_string = font == "roboto" ? "Roboto Condensed" : font;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    string topText = "<span foreground=\"white\" background=\"black\">" +
 | 
				
			||||||
 | 
					                     top_text + "</span>";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    VImage topImage = VImage::text(
 | 
				
			||||||
 | 
					        topText.c_str(),
 | 
				
			||||||
 | 
					        VImage::option()
 | 
				
			||||||
 | 
					            ->set("rgba", true)
 | 
				
			||||||
 | 
					            ->set("align", VIPS_ALIGN_CENTRE)
 | 
				
			||||||
 | 
					            ->set("font", (font_string + " " + to_string(size)).c_str())
 | 
				
			||||||
 | 
					            ->set("width", textWidth));
 | 
				
			||||||
 | 
					    topImage.write_to_file("/home/esm/motivatetext2.png");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    VImage bottomImage;
 | 
				
			||||||
    if (bottom_text != "") {
 | 
					    if (bottom_text != "") {
 | 
				
			||||||
      bottom.size(Geometry("600"));
 | 
					      string bottomText = "<span foreground=\"white\" background=\"black\">" +
 | 
				
			||||||
      bottom.backgroundColor("black");
 | 
					                          bottom_text + "</span>";
 | 
				
			||||||
      bottom.font("Times");
 | 
					
 | 
				
			||||||
      bottom.textGravity(Magick::CenterGravity);
 | 
					      bottomImage = VImage::text(
 | 
				
			||||||
      bottom.fontPointsize(28);
 | 
					          bottomText.c_str(),
 | 
				
			||||||
      bottom.read("pango:<span font_family=\"" +
 | 
					          VImage::option()
 | 
				
			||||||
                       (font == "roboto" ? "Roboto Condensed" : font) +
 | 
					              ->set("rgba", true)
 | 
				
			||||||
                       "\" foreground='white'>" + bottom_text + "</span>");
 | 
					              ->set("align", VIPS_ALIGN_CENTRE)
 | 
				
			||||||
      bottom.extent(Geometry(to_string(bottom.columns()) + "x" +
 | 
					              ->set("font", (font_string + " " + to_string(size * 0.4)).c_str())
 | 
				
			||||||
                             to_string(bottom.rows() + 20)),
 | 
					              ->set("width", textWidth));
 | 
				
			||||||
                    "black", Magick::NorthGravity);
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					    vector<VImage> img;
 | 
				
			||||||
      Image final;
 | 
					    int height;
 | 
				
			||||||
      image.scale(Geometry(500, 500));
 | 
					    for (int i = 0; i < n_pages; i++) {
 | 
				
			||||||
      image.borderColor("black");
 | 
					      VImage img_frame =
 | 
				
			||||||
      image.border(Geometry(5, 5));
 | 
					          type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
 | 
				
			||||||
      image.borderColor("white");
 | 
					 | 
				
			||||||
      image.border(Geometry(3, 3));
 | 
					 | 
				
			||||||
      image.backgroundColor("black");
 | 
					 | 
				
			||||||
      image.extent(Geometry(600, image.rows() + 50), Magick::CenterGravity);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      list<Image> to_append;
 | 
					      int borderSize = max(2, width / 66);
 | 
				
			||||||
      to_append.push_back(image);
 | 
					      int borderSize2 = borderSize * 0.5;
 | 
				
			||||||
      to_append.push_back(top);
 | 
					      VImage bordered =
 | 
				
			||||||
      if (bottom_text != "") to_append.push_back(bottom);
 | 
					          img_frame.embed(borderSize, borderSize, width + (borderSize * 2),
 | 
				
			||||||
      appendImages(&final, to_append.begin(), to_append.end(), true);
 | 
					                          page_height + (borderSize * 2),
 | 
				
			||||||
      final.repage();
 | 
					                          VImage::option()->set("extend", "black"));
 | 
				
			||||||
      final.magick(type);
 | 
					      VImage bordered2 = bordered.embed(
 | 
				
			||||||
      final.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
					          borderSize2, borderSize2, bordered.width() + (borderSize2 * 2),
 | 
				
			||||||
      mid.push_back(final);
 | 
					          bordered.height() + (borderSize2 * 2),
 | 
				
			||||||
    }
 | 
					          VImage::option()->set("extend", "white"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
					      int addition = width / 8;
 | 
				
			||||||
 | 
					      int sideAddition = page_height * 0.4;
 | 
				
			||||||
    if (type == "gif") {
 | 
					
 | 
				
			||||||
      for (Image &image : mid) {
 | 
					      VImage bordered3 = bordered2.embed(
 | 
				
			||||||
        image.quantizeDither(false);
 | 
					          sideAddition / 2, addition / 2, bordered2.width() + sideAddition,
 | 
				
			||||||
        image.quantize();
 | 
					          bordered2.height() + addition,
 | 
				
			||||||
 | 
					          VImage::option()->set("extend", "black"));
 | 
				
			||||||
 | 
					      VImage frame = bordered3.join(
 | 
				
			||||||
 | 
					          topImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
 | 
				
			||||||
 | 
					                           topImage.height() + (size / 4),
 | 
				
			||||||
 | 
					                           VImage::option()->set("extend", "black")),
 | 
				
			||||||
 | 
					          VIPS_DIRECTION_VERTICAL,
 | 
				
			||||||
 | 
					          VImage::option()->set("background", 0x000000)->set("expand", true));
 | 
				
			||||||
 | 
					      if (bottom_text != "") {
 | 
				
			||||||
 | 
					        frame = frame.join(
 | 
				
			||||||
 | 
					            bottomImage.gravity(VIPS_COMPASS_DIRECTION_NORTH, bordered3.width(),
 | 
				
			||||||
 | 
					                                bottomImage.height() + (size / 4),
 | 
				
			||||||
 | 
					                                VImage::option()->set("extend", "black")),
 | 
				
			||||||
 | 
					            VIPS_DIRECTION_VERTICAL,
 | 
				
			||||||
 | 
					            VImage::option()->set("background", 0x000000)->set("expand", true));
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      height = frame.height();
 | 
				
			||||||
 | 
					      img.push_back(frame);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1))
 | 
				
			||||||
 | 
					                       .extract_band(0, VImage::option()->set("n", 3));
 | 
				
			||||||
 | 
					    final.set(VIPS_META_PAGE_HEIGHT, height);
 | 
				
			||||||
 | 
					    if (delay) final.set("delay", delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    writeImages(mid.begin(), mid.end(), &blob);
 | 
					    void *buf;
 | 
				
			||||||
 | 
					    size_t length;
 | 
				
			||||||
 | 
					    final.write_to_buffer(
 | 
				
			||||||
 | 
					        ("." + type).c_str(), &buf, &length,
 | 
				
			||||||
 | 
					        type == "gif" ? VImage::option()->set("dither", 1) : 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    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,11 @@
 | 
				
			||||||
#include <Magick++.h>
 | 
					 | 
				
			||||||
#include <napi.h>
 | 
					#include <napi.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <list>
 | 
					#include <list>
 | 
				
			||||||
 | 
					#include <vips/vips8>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
using namespace Magick;
 | 
					using namespace vips;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Napi::Value Reverse(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Reverse(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -15,47 +15,54 @@ Napi::Value Reverse(const Napi::CallbackInfo &info) {
 | 
				
			||||||
    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
					    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
				
			||||||
    bool soos =
 | 
					    bool soos =
 | 
				
			||||||
        obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
 | 
					        obj.Has("soos") ? obj.Get("soos").As<Napi::Boolean>().Value() : false;
 | 
				
			||||||
    int delay =
 | 
					 | 
				
			||||||
        obj.Has("delay") ? obj.Get("delay").As<Napi::Number>().Int32Value() : 0;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Blob blob;
 | 
					    VOption *options =
 | 
				
			||||||
 | 
					        VImage::option()->set("access", "sequential")->set("n", -1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    list<Image> frames;
 | 
					    VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "", options)
 | 
				
			||||||
    list<Image> coalesced;
 | 
					                    .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
    try {
 | 
					
 | 
				
			||||||
      readImages(&frames, Blob(data.Data(), data.Length()));
 | 
					    int width = in.width();
 | 
				
			||||||
    } catch (Magick::WarningCoder &warning) {
 | 
					    int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
      cerr << "Coder Warning: " << warning.what() << endl;
 | 
					    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
				
			||||||
    } catch (Magick::Warning &warning) {
 | 
					
 | 
				
			||||||
      cerr << "Warning: " << warning.what() << endl;
 | 
					    vector<VImage> split;
 | 
				
			||||||
 | 
					    // todo: find a better way of getting individual frames (or at least getting the frames in reverse order)
 | 
				
			||||||
 | 
					    for (int i = 0; i < n_pages; i++) {
 | 
				
			||||||
 | 
					      VImage img_frame = in.crop(0, i * page_height, width, page_height);
 | 
				
			||||||
 | 
					      split.push_back(img_frame);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    vector<int> delays = in.get_array_int("delay");
 | 
				
			||||||
    if (soos) {
 | 
					    if (soos) {
 | 
				
			||||||
      list<Image> copy = coalesced;
 | 
					      vector<VImage> copy = split;
 | 
				
			||||||
      copy.reverse();
 | 
					      vector<int> copy2 = delays;
 | 
				
			||||||
 | 
					      reverse(copy.begin(), copy.end());
 | 
				
			||||||
 | 
					      reverse(copy2.begin(), copy2.end());
 | 
				
			||||||
      copy.pop_back();
 | 
					      copy.pop_back();
 | 
				
			||||||
      copy.pop_front();
 | 
					      copy2.pop_back();
 | 
				
			||||||
      coalesced.insert(coalesced.end(), copy.begin(), copy.end());
 | 
					      copy.erase(copy.begin());
 | 
				
			||||||
 | 
					      copy2.erase(copy2.begin());
 | 
				
			||||||
 | 
					      split.insert(split.end(), copy.begin(), copy.end());
 | 
				
			||||||
 | 
					      delays.insert(delays.end(), copy2.begin(), copy2.end());
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      coalesced.reverse();
 | 
					      reverse(split.begin(), split.end());
 | 
				
			||||||
 | 
					      reverse(delays.begin(), delays.end());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for_each(coalesced.begin(), coalesced.end(), magickImage("GIF"));
 | 
					    VImage final = VImage::arrayjoin(split, VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					    final.set(VIPS_META_PAGE_HEIGHT, page_height);
 | 
				
			||||||
 | 
					    final.set("delay", delays);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    optimizeTransparency(coalesced.begin(), coalesced.end());
 | 
					    void *buf;
 | 
				
			||||||
 | 
					    size_t length;
 | 
				
			||||||
 | 
					    final.write_to_buffer(".gif", &buf, &length,
 | 
				
			||||||
 | 
					                          VImage::option()->set("dither", 0));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					    vips_thread_shutdown();
 | 
				
			||||||
      image.quantizeDither(false);
 | 
					 | 
				
			||||||
      image.quantize();
 | 
					 | 
				
			||||||
      if (delay != 0) image.animationDelay(delay);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    writeImages(coalesced.begin(), coalesced.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", "gif");
 | 
					    result.Set("type", "gif");
 | 
				
			||||||
    return result;
 | 
					    return result;
 | 
				
			||||||
  } catch (std::exception const &err) {
 | 
					  } catch (std::exception const &err) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue