Port uncaption
This commit is contained in:
		
							parent
							
								
									d191a69742
								
							
						
					
					
						commit
						2e71097763
					
				
					 1 changed files with 38 additions and 49 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 Uncaption(const Napi::CallbackInfo &info) {
 | 
					Napi::Value Uncaption(const Napi::CallbackInfo &info) {
 | 
				
			||||||
  Napi::Env env = info.Env();
 | 
					  Napi::Env env = info.Env();
 | 
				
			||||||
| 
						 | 
					@ -13,61 +11,52 @@ Napi::Value Uncaption(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>>();
 | 
				
			||||||
    float tolerance = obj.Has("tolerance") ? obj.Get("tolerance").As<Napi::Number>().FloatValue() : 0.95;
 | 
					    float tolerance = obj.Has("tolerance")
 | 
				
			||||||
 | 
					                          ? obj.Get("tolerance").As<Napi::Number>().FloatValue()
 | 
				
			||||||
 | 
					                          : 0.5;
 | 
				
			||||||
    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;
 | 
					    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());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Image firstImage = coalesced.front();
 | 
					    int width = in.width();
 | 
				
			||||||
    ssize_t columns = firstImage.columns();
 | 
					    int page_height = vips_image_get_page_height(in.get_image());
 | 
				
			||||||
    ssize_t rows = firstImage.rows();
 | 
					    int n_pages = vips_image_get_n_pages(in.get_image());
 | 
				
			||||||
    ssize_t row;
 | 
					 | 
				
			||||||
    for (row = 0; row < rows; ++row) {
 | 
					 | 
				
			||||||
      ColorGray color = firstImage.pixelColor(0, row);
 | 
					 | 
				
			||||||
      if (color.shade() < tolerance) {
 | 
					 | 
				
			||||||
        break;
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
    Geometry geom = Geometry(columns, row == rows ? rows : rows - row, 0,
 | 
					 | 
				
			||||||
                             row == rows ? 0 : row);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (Image &image : coalesced) {
 | 
					    VImage first =
 | 
				
			||||||
      image.virtualPixelMethod(Magick::TransparentVirtualPixelMethod);
 | 
					        in.crop(0, 0, 3, page_height).colourspace(VIPS_INTERPRETATION_B_W) >
 | 
				
			||||||
      image.backgroundColor("none");
 | 
					        (255 * tolerance);
 | 
				
			||||||
      image.extent(geom);
 | 
					    int top, captionWidth, captionHeight;
 | 
				
			||||||
      image.magick(type);
 | 
					    first.find_trim(&top, &captionWidth, &captionHeight);
 | 
				
			||||||
      mid.push_back(image);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    optimizeTransparency(mid.begin(), mid.end());
 | 
					    vector<VImage> img;
 | 
				
			||||||
 | 
					    for (int i = 0; i < n_pages; i++) {
 | 
				
			||||||
    if (type == "gif") {
 | 
					      VImage img_frame = type == "gif" ? in.crop(0, (i * page_height) + top,
 | 
				
			||||||
      for (Image &image : mid) {
 | 
					                                                 width, page_height - top)
 | 
				
			||||||
        image.quantizeDither(false);
 | 
					                                       : in;
 | 
				
			||||||
        image.quantize();
 | 
					      img.push_back(img_frame);
 | 
				
			||||||
        if (delay != 0) image.animationDelay(delay);
 | 
					 | 
				
			||||||
      }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    VImage final = VImage::arrayjoin(img, VImage::option()->set("across", 1));
 | 
				
			||||||
 | 
					    final.set(VIPS_META_PAGE_HEIGHT, page_height - top);
 | 
				
			||||||
 | 
					    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", 0) : 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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