-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentAnnotationIdentifier.js
More file actions
102 lines (99 loc) · 3.32 KB
/
Copy pathDocumentAnnotationIdentifier.js
File metadata and controls
102 lines (99 loc) · 3.32 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
var DocumentAnnotation = require('./DocumentAnnotation.js');
class DocumentAnnotationIdentifier {
// Returns pixel color based on annotation type
getColorByAnnotationType(annotationType) {
// Text input #00ff7e (r0, g255, b126)
if ( annotationType === "freetext") {
return {
r: 0,
g: 255,
b: 126,
a: 0
}
}
// Check box input #ff0000 (r255, g0, b0)
else if ( annotationType === "checkbox") {
return {
r: 255,
g: 0,
b: 0,
a: 0
}
}
}
// Returns line start and line end if scan is successfull, else null.
scanLine(imageBuffer, y, width, annotationType) {
var color = this.getColorByAnnotationType(annotationType);
var hasFoundStart = false;
var lineStart = 0;
var lineWidth = 0;
for (let x = 0; x < width; x++ ) {
var idx = (width * y + x) << 2;
var red = imageBuffer[idx];
var green = imageBuffer[idx + 1];
var blue = imageBuffer[idx + 2];
var alpha = imageBuffer[idx + 3];
if ( red === color.r && green === color.g && blue === color.b ) {
if (!hasFoundStart) {
hasFoundStart = true;
lineStart = x;
} else {
lineWidth++;
}
}
}
if (!hasFoundStart)
return null;
return {
line_start: lineStart,
line_width: lineWidth
};
}
// Returns an array of class DocumentAnnotation
// The big flaw here is that annotation boxes of same type cannot share Y-axis. Fix!
getAnnotations(imageBuffer, width, height, annotationType) {
// Iterates the buffer and checks for corresponding color codes, then
// populates an array of type DocumentAnnotation and returns the array.
var isSearching = false;
var annotations = [];
var currentAnnotationStartX = null;
var currentAnnotationStartY = null;
var currentAnnotationStopX = null;
var currentAnnotationStopY = null;
var previousTextScanResult = null;
// Iterate the Y-axis
for(let y = 0; y < height; y++ ) {
var freeTextScanResult = this.scanLine(imageBuffer, y, width, annotationType);
if ( freeTextScanResult ) {
var previousTextScanResult = freeTextScanResult;
if ( !isSearching ) {
// Beging annotation
isSearching = true;
currentAnnotationStartX = freeTextScanResult.line_start;
currentAnnotationStartY = y;
}
} else {
if (isSearching) {
// Close annotation
currentAnnotationStopX = previousTextScanResult.line_start + previousTextScanResult.line_width;
currentAnnotationStopY = y;
isSearching = false;
var annotation = new DocumentAnnotation(annotationType,
currentAnnotationStartX,
currentAnnotationStartY,
previousTextScanResult.line_width,
(currentAnnotationStopY - currentAnnotationStartY)
);
currentAnnotationStartX = null;
currentAnnotationStartY = null;
currentAnnotationStopX = null;
currentAnnotationStopY = null;
previousTextScanResult = null;
annotations.push(annotation);
}
}
}
return annotations;
}
}
module.exports = DocumentAnnotationIdentifier;