File: HighGUI.cc

package info (click to toggle)
node-opencv 6.0.0%2Bgit20180416.cfc96ba0-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,632 kB
  • sloc: xml: 476,707; cpp: 5,950; makefile: 114; sh: 59; ansic: 20
file content (100 lines) | stat: -rw-r--r-- 2,548 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "HighGUI.h"
#include "OpenCV.h"
#include "Matrix.h"

#ifdef HAVE_OPENCV_HIGHGUI

Nan::Persistent<FunctionTemplate> NamedWindow::constructor;

void NamedWindow::Init(Local<Object> target) {
  Nan::HandleScope scope;

  // Constructor
  Local<FunctionTemplate> ctor = Nan::New<FunctionTemplate>(NamedWindow::New);
  constructor.Reset(ctor);
  ctor->InstanceTemplate()->SetInternalFieldCount(1);
  ctor->SetClassName(Nan::New("NamedWindow").ToLocalChecked());

  // Prototype
  Nan::SetPrototypeMethod(ctor, "show", Show);
  Nan::SetPrototypeMethod(ctor, "destroy", Destroy);
  Nan::SetPrototypeMethod(ctor, "blockingWaitKey", BlockingWaitKey);
  Nan::SetPrototypeMethod(ctor, "resizeWindow", ResizeWindow);

  target->Set(Nan::New("NamedWindow").ToLocalChecked(), ctor->GetFunction());
};

NAN_METHOD(NamedWindow::New) {
  Nan::HandleScope scope;

  if (info.This()->InternalFieldCount() == 0) {
    JSTHROW_TYPE("Cannot Instantiate without new")
  }

  NamedWindow* win;
  if (info.Length() == 1) {
    win = new NamedWindow(std::string(*Nan::Utf8String(info[0]->ToString())), 0);
  } else {  //if (info.Length() == 2){
    win = new NamedWindow(std::string(*Nan::Utf8String(info[0]->ToString())), 
            info[1]->IntegerValue());
  }

  win->Wrap(info.Holder());
  info.GetReturnValue().Set(info.Holder());
}

NamedWindow::NamedWindow(const std::string& name, int f) {
  winname = std::string(name);
  flags = f;
  cv::namedWindow(winname, flags);
}

NAN_METHOD(NamedWindow::Show) {
  SETUP_FUNCTION(NamedWindow)
  Matrix *im = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject());

  try {
    cv::imshow(self->winname, im->mat);
  } catch (cv::Exception& e) {
    const char* err_msg = e.what();
    Nan::ThrowError(err_msg);
  }

  info.GetReturnValue().Set(info.Holder());
}

NAN_METHOD(NamedWindow::Destroy) {
  SETUP_FUNCTION(NamedWindow)
  cv::destroyWindow(self->winname);
  info.GetReturnValue().Set(info.Holder());
}

NAN_METHOD(NamedWindow::BlockingWaitKey) {
  int time = 0;

  if (info.Length() > 1) {
    time = info[1]->IntegerValue();
  } else {
    if (info.Length() > 0) {
      time = info[0]->IntegerValue();
    }
  }

  int res = cv::waitKey(time);

  info.GetReturnValue().Set(Nan::New<Number>(res));
}

NAN_METHOD(NamedWindow::ResizeWindow) {
  SETUP_FUNCTION(NamedWindow)
  
  if (info.Length() != 2) {
    throw "expected 2 argurments: width, height";
  }//otherwise

  int width = info[0]->IntegerValue();
  int height = info[1]->IntegerValue();
  cv::resizeWindow(self->winname, width, height);
}

#endif