Port invert, jpeg, and mirror
This commit is contained in:
		
							parent
							
								
									112f15e9f2
								
							
						
					
					
						commit
						4a3d880a61
					
				
					 3 changed files with 111 additions and 135 deletions
				
			
		| 
						 | 
					@ -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 Invert(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Invert(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -17,43 +17,29 @@ Napi::Value Invert(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)
 | 
				
			||||||
    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;
 | 
					 | 
				
			||||||
    } catch (Magick::Warning &warning) {
 | 
					 | 
				
			||||||
      cerr << "Warning: " << warning.what() << endl;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for_each(coalesced.begin(), coalesced.end(), negateImage());
 | 
					    VImage noAlpha =
 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					        in.extract_band(0, VImage::option()->set("n", in.bands() - 1));
 | 
				
			||||||
      image.negateChannel(Magick::AlphaChannel);
 | 
					    VImage inverted = noAlpha.invert();
 | 
				
			||||||
      mid.push_back(image);
 | 
					    VImage out = inverted.bandjoin(in.extract_band(3));
 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    // Magick::ChannelType(Magick::CompositeChannels ^ Magick::AlphaChannel)
 | 
					 | 
				
			||||||
    for_each(mid.begin(), mid.end(), magickImage(type));
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
					    if (delay) out.set("delay", delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (type == "gif") {
 | 
					    void *buf;
 | 
				
			||||||
      for (Image &image : mid) {
 | 
					    size_t length;
 | 
				
			||||||
        image.quantizeDitherMethod(FloydSteinbergDitherMethod);
 | 
					    out.write_to_buffer(("." + type).c_str(), &buf, &length);
 | 
				
			||||||
        image.quantize();
 | 
					 | 
				
			||||||
        if (delay != 0) image.animationDelay(delay);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    writeImages(mid.begin(), mid.end(), &blob);
 | 
					    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", 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 Jpeg(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Jpeg(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -13,61 +13,62 @@ Napi::Value Jpeg(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    Napi::Object obj = info[0].As<Napi::Object>();
 | 
					    Napi::Object obj = info[0].As<Napi::Object>();
 | 
				
			||||||
    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
					    Napi::Buffer<char> data = obj.Get("data").As<Napi::Buffer<char>>();
 | 
				
			||||||
    int quality =
 | 
					    int quality = obj.Has("quality")
 | 
				
			||||||
        obj.Has("quality") ? obj.Get("quality").As<Napi::Number>().Int32Value() : 0;
 | 
					                      ? obj.Get("quality").As<Napi::Number>().Int32Value()
 | 
				
			||||||
 | 
					                      : 0;
 | 
				
			||||||
    string type = obj.Get("type").As<Napi::String>().Utf8Value();
 | 
					    string type = obj.Get("type").As<Napi::String>().Utf8Value();
 | 
				
			||||||
    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;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Napi::Object result = Napi::Object::New(env);
 | 
					    Napi::Object result = Napi::Object::New(env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (type == "gif") {
 | 
					    if (type == "gif") {
 | 
				
			||||||
      list<Image> frames;
 | 
					      VImage in =
 | 
				
			||||||
      list<Image> coalesced;
 | 
					          VImage::new_from_buffer(
 | 
				
			||||||
      list<Image> jpeged;
 | 
					              data.Data(), data.Length(), "",
 | 
				
			||||||
      try {
 | 
					              VImage::option()->set("access", "sequential")->set("n", -1))
 | 
				
			||||||
        readImages(&frames, Blob(data.Data(), data.Length()));
 | 
					              .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
      } catch (Magick::WarningCoder &warning) {
 | 
					      if (!in.has_alpha()) in = in.bandjoin(255);
 | 
				
			||||||
        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());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      vector<VImage> img;
 | 
				
			||||||
 | 
					      for (int i = 0; i < n_pages; i++) {
 | 
				
			||||||
 | 
					        VImage img_frame = in.crop(0, i * page_height, width, page_height);
 | 
				
			||||||
 | 
					        void *buf;
 | 
				
			||||||
 | 
					        size_t length;
 | 
				
			||||||
 | 
					        img_frame.write_to_buffer(
 | 
				
			||||||
 | 
					            ".jpg", &buf, &length,
 | 
				
			||||||
 | 
					            VImage::option()->set("Q", quality)->set("strip", true));
 | 
				
			||||||
 | 
					        VImage jpeg = VImage::new_from_buffer(buf, length, "");
 | 
				
			||||||
 | 
					        img.push_back(jpeg);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
      coalesceImages(&coalesced, frames.begin(), frames.end());
 | 
					      VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					      final.set(VIPS_META_PAGE_HEIGHT, page_height);
 | 
				
			||||||
 | 
					      if (delay) final.set("delay", delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      for (Image &image : coalesced) {
 | 
					      void *buf;
 | 
				
			||||||
        Blob temp;
 | 
					      size_t length;
 | 
				
			||||||
        image.quality(quality);
 | 
					      final.write_to_buffer(("." + type).c_str(), &buf, &length,
 | 
				
			||||||
        image.magick("JPEG");
 | 
					                            VImage::option()->set("dither", 0));
 | 
				
			||||||
        image.write(&temp);
 | 
					 | 
				
			||||||
        Image newImage(temp);
 | 
					 | 
				
			||||||
        newImage.magick(type);
 | 
					 | 
				
			||||||
        newImage.animationDelay(delay == 0 ? image.animationDelay() : delay);
 | 
					 | 
				
			||||||
        jpeged.push_back(newImage);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
      optimizeTransparency(jpeged.begin(), jpeged.end());
 | 
					      vips_thread_shutdown();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      for (Image &image : jpeged) {
 | 
					      result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
				
			||||||
        image.quantizeDither(false);
 | 
					 | 
				
			||||||
        image.quantize();
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      writeImages(jpeged.begin(), jpeged.end(), &blob);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
      result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
 | 
					 | 
				
			||||||
                                                  blob.length()));
 | 
					 | 
				
			||||||
      result.Set("type", type);
 | 
					      result.Set("type", type);
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      Image image;
 | 
					      VImage in = VImage::new_from_buffer(data.Data(), data.Length(), "");
 | 
				
			||||||
      image.read(Blob(data.Data(), data.Length()));
 | 
					      void *buf;
 | 
				
			||||||
      image.quality(1);
 | 
					      size_t length;
 | 
				
			||||||
      image.magick("JPEG");
 | 
					      in.write_to_buffer(
 | 
				
			||||||
      image.write(&blob);
 | 
					          ".jpg", &buf, &length,
 | 
				
			||||||
 | 
					          VImage::option()->set("Q", quality)->set("strip", true));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      result.Set("data", Napi::Buffer<char>::Copy(env, (char *)blob.data(),
 | 
					      vips_thread_shutdown();
 | 
				
			||||||
                                                  blob.length()));
 | 
					
 | 
				
			||||||
 | 
					      result.Set("data", Napi::Buffer<char>::Copy(env, (char *)buf, length));
 | 
				
			||||||
      result.Set("type", "jpg");
 | 
					      result.Set("type", "jpg");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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 Mirror(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Mirror(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -22,74 +22,63 @@ Napi::Value Mirror(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)
 | 
				
			||||||
    MagickCore::GravityType gravity;
 | 
					            .colourspace(VIPS_INTERPRETATION_sRGB);
 | 
				
			||||||
    try {
 | 
					    if (!in.has_alpha()) in = in.bandjoin(255);
 | 
				
			||||||
      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());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (vertical && first) {
 | 
					    VImage out;
 | 
				
			||||||
      gravity = Magick::NorthGravity;
 | 
					
 | 
				
			||||||
    } else if (!vertical && first) {
 | 
					    if (vertical) {
 | 
				
			||||||
      gravity = Magick::WestGravity;
 | 
					      if (type == "gif") {
 | 
				
			||||||
    } else if (vertical && !first) {
 | 
					        // once again, libvips gif handling is both a blessing and a curse
 | 
				
			||||||
      gravity = Magick::SouthGravity;
 | 
					        vector<VImage> img;
 | 
				
			||||||
 | 
					        int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
 | 
					        int n_pages = vips_image_get_n_pages(in.get_image());
 | 
				
			||||||
 | 
					        bool isOdd = page_height % 2;
 | 
				
			||||||
 | 
					        for (int i = 0; i < n_pages; i++) {
 | 
				
			||||||
 | 
					          int x = (i * page_height) + (first ? 0 : (page_height / 2));
 | 
				
			||||||
 | 
					          VImage cropped = in.crop(0, x, in.width(), page_height / 2);
 | 
				
			||||||
 | 
					          VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
 | 
				
			||||||
 | 
					          VImage final = VImage::arrayjoin(
 | 
				
			||||||
 | 
					              {first ? cropped : flipped, first ? flipped : cropped},
 | 
				
			||||||
 | 
					              VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					          img.push_back(final);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        out = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					        out.set(VIPS_META_PAGE_HEIGHT, page_height - (isOdd ? 1 : 0));
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        VImage cropped = in.extract_area(0, 0, in.width(), in.height() / 2);
 | 
				
			||||||
 | 
					        VImage flipped = cropped.flip(VIPS_DIRECTION_VERTICAL);
 | 
				
			||||||
 | 
					        out = VImage::arrayjoin({cropped, flipped},
 | 
				
			||||||
 | 
					                                VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
      gravity = Magick::EastGravity;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					 | 
				
			||||||
      image.colorSpace(Magick::sRGBColorspace);
 | 
					 | 
				
			||||||
      list<Image> mirrored;
 | 
					 | 
				
			||||||
      Image final;
 | 
					 | 
				
			||||||
      image.extent(Geometry(to_string(vertical ? image.baseColumns()
 | 
					 | 
				
			||||||
                                               : image.baseColumns() / 2) +
 | 
					 | 
				
			||||||
                            "x" +
 | 
					 | 
				
			||||||
                            to_string(vertical ? image.baseRows() / 2
 | 
					 | 
				
			||||||
                                               : image.baseRows())),
 | 
					 | 
				
			||||||
                   gravity);
 | 
					 | 
				
			||||||
      mirrored.push_back(image);
 | 
					 | 
				
			||||||
      Image mirror = image;
 | 
					 | 
				
			||||||
      if (vertical) {
 | 
					 | 
				
			||||||
        mirror.flip();
 | 
					 | 
				
			||||||
      } else {
 | 
					 | 
				
			||||||
        mirror.flop();
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
      if (first) {
 | 
					      if (first) {
 | 
				
			||||||
        mirrored.push_back(mirror);
 | 
					        VImage cropped = in.extract_area(0, 0, in.width() / 2, in.height());
 | 
				
			||||||
 | 
					        VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL);
 | 
				
			||||||
 | 
					        out = VImage::arrayjoin({cropped, flipped});
 | 
				
			||||||
      } else {
 | 
					      } else {
 | 
				
			||||||
        mirrored.push_front(mirror);
 | 
					        int size = in.width() / 2;
 | 
				
			||||||
      }
 | 
					        VImage cropped = in.extract_area(size, 0, size, in.height());
 | 
				
			||||||
      appendImages(&final, mirrored.begin(), mirrored.end(), vertical);
 | 
					        VImage flipped = cropped.flip(VIPS_DIRECTION_HORIZONTAL);
 | 
				
			||||||
      final.repage();
 | 
					        out = VImage::arrayjoin({flipped, cropped});
 | 
				
			||||||
      final.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.quantizeDither(false);
 | 
					 | 
				
			||||||
        image.quantize();
 | 
					 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    writeImages(mid.begin(), mid.end(), &blob);
 | 
					    if (delay) out.set("delay", delay);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    void *buf;
 | 
				
			||||||
 | 
					    size_t length;
 | 
				
			||||||
 | 
					    out.write_to_buffer(("." + type).c_str(), &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", 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