-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwebarkitJsfeat.cpp
More file actions
121 lines (102 loc) · 3.69 KB
/
Copy pathwebarkitJsfeat.cpp
File metadata and controls
121 lines (102 loc) · 3.69 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <AR/ar.h>
#include <AR2/config.h>
#include <AR2/imageFormat.h>
#include <AR2/util.h>
#include <WebARKit/WebARKitLog.h>
#include <emscripten.h>
#include <jsfeat.h>
#include <stdio.h>
#include <string>
#include <vector>
using namespace jsfeat;
extern "C" {
emscripten::val load_jpeg(const char* filename) {
char* ext;
char buf1[512], buf2[512];
AR2JpegImageT* jpegImage;
if (!filename) return emscripten::val::null();
ext = arUtilGetFileExtensionFromPath(filename, 1);
if (!ext) {
webarkitLOGe(
"Error: unable to determine extension of file '%s'. Exiting.\n",
filename);
}
if (strcmp(ext, "jpeg") == 0 || strcmp(ext, "jpg") == 0 ||
strcmp(ext, "jpe") == 0) {
webarkitLOGi("Waiting for the jpeg...");
webarkitLOGi("Reading JPEG file...");
ar2UtilDivideExt(filename, buf1, buf2);
jpegImage = ar2ReadJpegImage(buf1, buf2);
if (jpegImage == NULL) {
webarkitLOGe(
"Error: unable to read JPEG image from file '%s'. Exiting.\n",
filename);
}
webarkitLOGi(" Done.");
if (jpegImage->nc != 1 && jpegImage->nc != 3) {
ARLOGe(
"Error: Input JPEG image is in neither RGB nor grayscale format. "
"%d bytes/pixel %sformat is unsupported. Exiting.\n",
jpegImage->nc, (jpegImage->nc == 4 ? "(possibly CMYK) " : ""));
}
webarkitLOGi("JPEG image number of channels: '%d'", jpegImage->nc);
webarkitLOGi("JPEG image width is: '%d'", jpegImage->xsize);
webarkitLOGi("JPEG image height is: '%d'", jpegImage->ysize);
webarkitLOGi("JPEG image, dpi is: '%d'", jpegImage->dpi);
if (jpegImage->dpi == 0.0f) {
webarkitLOGw(
"JPEG image '%s' does not contain embedded resolution data, and no "
"resolution specified on command-line.",
filename);
}
} else if (strcmp(ext, "png") == 0) {
webarkitLOGe(
"Error: file has extension '%s', which is not supported for "
"reading. Exiting.\n",
ext);
free(ext);
}
auto jpegSize = jpegImage->xsize * jpegImage->ysize * jpegImage->nc;
emscripten::val obj = emscripten::val::object();
emscripten::val view{
emscripten::typed_memory_view(jpegSize, jpegImage->image)};
auto result = emscripten::val::global("Uint8Array").new_(jpegSize);
result.call<void>("set", view);
obj.set("width", jpegImage->xsize);
obj.set("height", jpegImage->ysize);
obj.set("dpi", jpegImage->dpi);
obj.set("data", result);
free(ext);
free(jpegImage);
return obj;
};
emscripten::val load_jpeg_data(std::string filename) {
auto out = load_jpeg(filename.c_str());
return out;
};
emscripten::val yape06_detect(emscripten::val inputSrc, int w, int h) {
auto src = emscripten::convertJSArrayToNumberVector<u_char>(inputSrc);
Imgproc imgproc;
Yape06 yape;
std::unique_ptr<KeyPoints> keypoints = std::make_unique<KeyPoints>(w * h);
std::unique_ptr<Matrix_t> lev0_img = std::make_unique<Matrix_t>(w, h, ComboTypes::U8C1_t);
imgproc.grayscale_internal<u_char, Matrix_t>(src.data(), w, h, lev0_img.get(), Colors::COLOR_RGBA2GRAY);
imgproc.gaussian_blur_internal(lev0_img.get(), lev0_img.get(), 5, 2);
auto obj = yape.detect_internal(lev0_img.get(), keypoints.get(), 17);
emscripten::val outObj = emscripten::val::object();
emscripten::val pointsArr = emscripten::val::array();
KPoint_t pt;
for (auto i = 0; i < obj.pts.kpoints.size(); i++) {
pt.x = obj.pts.kpoints[i].x;
pt.y = obj.pts.kpoints[i].y;
pt.level = obj.pts.kpoints[i].level;
pt.score = obj.pts.kpoints[i].score;
pt.angle = obj.pts.kpoints[i].angle;
pointsArr.call<void>("push", pt);
}
outObj.set("count", obj.count);
outObj.set("points", pointsArr);
return outObj;
};
}
#include "bindings.cpp"