Port snapchat and zamn
This commit is contained in:
		
							parent
							
								
									0162997792
								
							
						
					
					
						commit
						d191a69742
					
				
					 2 changed files with 88 additions and 82 deletions
				
			
		| 
						 | 
					@ -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 Snapchat(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Snapchat(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -20,58 +18,64 @@ Napi::Value Snapchat(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> captioned;
 | 
					                                type == "gif" ? options->set("n", -1) : options)
 | 
				
			||||||
    Blob caption_blob;
 | 
					            .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
    try {
 | 
					    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
				
			||||||
      readImages(&frames, Blob(data.Data(), data.Length()));
 | 
					
 | 
				
			||||||
    } catch (Magick::WarningCoder &warning) {
 | 
					    int width = in.width();
 | 
				
			||||||
      cerr << "Coder Warning: " << warning.what() << endl;
 | 
					    int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
    } catch (Magick::Warning &warning) {
 | 
					    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
				
			||||||
      cerr << "Warning: " << warning.what() << endl;
 | 
					    int size = width / 20;
 | 
				
			||||||
 | 
					    int textWidth = width - ((width / 25) * 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    string font_string = "Helvetica Neue " + to_string(size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    VImage textIn =
 | 
				
			||||||
 | 
					        VImage::text(("<span foreground=\"white\" background=\"#000000B2\">" +
 | 
				
			||||||
 | 
					                      caption + "</span>")
 | 
				
			||||||
 | 
					                         .c_str(),
 | 
				
			||||||
 | 
					                     VImage::option()
 | 
				
			||||||
 | 
					                         ->set("rgba", true)
 | 
				
			||||||
 | 
					                         ->set("align", VIPS_ALIGN_CENTRE)
 | 
				
			||||||
 | 
					                         ->set("font", font_string.c_str())
 | 
				
			||||||
 | 
					                         ->set("width", textWidth));
 | 
				
			||||||
 | 
					    int bgHeight = textIn.height() + (width / 25);
 | 
				
			||||||
 | 
					    textIn =
 | 
				
			||||||
 | 
					        ((textIn == (vector<double>){0, 0, 0, 0}).bandand())
 | 
				
			||||||
 | 
					            .ifthenelse({0, 0, 0, 178}, textIn)
 | 
				
			||||||
 | 
					            .embed((width / 2) - (textIn.width() / 2),
 | 
				
			||||||
 | 
					                   (bgHeight / 2) - (textIn.height() / 2), width, bgHeight,
 | 
				
			||||||
 | 
					                   VImage::option()
 | 
				
			||||||
 | 
					                       ->set("extend", "background")
 | 
				
			||||||
 | 
					                       ->set("background", (vector<double>){0, 0, 0, 178}));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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;
 | 
				
			||||||
 | 
					      img_frame = img_frame.composite2(
 | 
				
			||||||
 | 
					          textIn, VIPS_BLEND_MODE_OVER,
 | 
				
			||||||
 | 
					          VImage::option()->set("x", 0)->set("y", page_height * pos));
 | 
				
			||||||
 | 
					      img.push_back(img_frame);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					    final.set(VIPS_META_PAGE_HEIGHT, page_height);
 | 
				
			||||||
 | 
					    if (delay) final.set("delay", delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    size_t width = frames.front().baseColumns();
 | 
					    void *buf;
 | 
				
			||||||
    size_t height = frames.front().baseRows();
 | 
					    size_t length;
 | 
				
			||||||
    string query(to_string(width - ((width / 25) * 2)) + "x");
 | 
					    final.write_to_buffer(
 | 
				
			||||||
    Image caption_image(Geometry(query), Color("none"));
 | 
					        ("." + type).c_str(), &buf, &length,
 | 
				
			||||||
    caption_image.backgroundColor(Color("none"));
 | 
					        type == "gif" ? VImage::option()->set("dither", 0) : 0);
 | 
				
			||||||
    caption_image.fillColor("white");
 | 
					 | 
				
			||||||
    caption_image.font("Helvetica Neue");
 | 
					 | 
				
			||||||
    caption_image.fontPointsize(width / 25);
 | 
					 | 
				
			||||||
    caption_image.textGravity(Magick::CenterGravity);
 | 
					 | 
				
			||||||
    caption_image.read("pango:" + caption);
 | 
					 | 
				
			||||||
    caption_image.backgroundColor(Color("rgba(0, 0, 0, 0.7)"));
 | 
					 | 
				
			||||||
    caption_image.extent(Geometry(width, caption_image.rows() + (width / 25)),
 | 
					 | 
				
			||||||
                         Magick::CenterGravity);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
					    vips_thread_shutdown();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					 | 
				
			||||||
      list<Image> images;
 | 
					 | 
				
			||||||
      image.composite(caption_image, 0, height * pos, Magick::OverCompositeOp);
 | 
					 | 
				
			||||||
      image.magick(type);
 | 
					 | 
				
			||||||
      image.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
					 | 
				
			||||||
      captioned.push_back(image);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    optimizeTransparency(captioned.begin(), captioned.end());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    if (type == "gif") {
 | 
					 | 
				
			||||||
      for (Image &image : captioned) {
 | 
					 | 
				
			||||||
        image.quantizeDither(false);
 | 
					 | 
				
			||||||
        image.quantize();
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    writeImages(captioned.begin(), captioned.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,10 @@
 | 
				
			||||||
#include <Magick++.h>
 | 
					 | 
				
			||||||
#include <napi.h>
 | 
					#include <napi.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <list>
 | 
					#include <vips/vips8>
 | 
				
			||||||
 | 
					#include <gperftools/profiler.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
using namespace std;
 | 
					using namespace std;
 | 
				
			||||||
using namespace Magick;
 | 
					using namespace vips;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Napi::Value Zamn(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Zamn(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -17,42 +17,44 @@ Napi::Value Zamn(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);
 | 
				
			||||||
    readImages(&frames, Blob(data.Data(), data.Length()));
 | 
					    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
				
			||||||
    watermark.read(basePath + "assets/images/zamn.png");
 | 
					 | 
				
			||||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					    int width = in.width();
 | 
				
			||||||
      Image watermark_new = watermark;
 | 
					    int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
      image.backgroundColor("none");
 | 
					    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
				
			||||||
      image.scale(Geometry("303x438!"));
 | 
					
 | 
				
			||||||
      image.extent(Geometry("621x516-310-75"));
 | 
					    string assetPath = basePath + "assets/images/zamn.png";
 | 
				
			||||||
      watermark_new.composite(image, Magick::CenterGravity,
 | 
					    VImage tmpl = VImage::new_from_file(assetPath.c_str());
 | 
				
			||||||
                              Magick::OverCompositeOp);
 | 
					
 | 
				
			||||||
      watermark_new.magick(type);
 | 
					    vector<VImage> img;
 | 
				
			||||||
      watermark_new.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
					    for (int i = 0; i < n_pages; i++) {
 | 
				
			||||||
      mid.push_back(watermark_new);
 | 
					      VImage img_frame =
 | 
				
			||||||
 | 
					          type == "gif" ? in.crop(0, i * page_height, width, page_height) : in;
 | 
				
			||||||
 | 
					      VImage composited = tmpl.insert(
 | 
				
			||||||
 | 
					          img_frame.extract_band(0, VImage::option()->set("n", 3)).bandjoin(255).resize(
 | 
				
			||||||
 | 
					              303.0 / (double)width,
 | 
				
			||||||
 | 
					              VImage::option()->set("vscale", 438.0 / (double)page_height)),
 | 
				
			||||||
 | 
					          310, 76);
 | 
				
			||||||
 | 
					      img.push_back(composited);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					    final.set(VIPS_META_PAGE_HEIGHT, 516);
 | 
				
			||||||
 | 
					    if (delay) final.set("delay", delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
					    void *buf;
 | 
				
			||||||
 | 
					    size_t length;
 | 
				
			||||||
 | 
					    final.write_to_buffer(("." + type).c_str(), &buf, &length);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (type == "gif") {
 | 
					    vips_thread_shutdown();
 | 
				
			||||||
      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) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue