@@ -2,7 +2,6 @@ import { Mask } from '..';
2
2
import { assert } from '../utils/validators/assert' ;
3
3
4
4
import { RoiMapManager } from './RoiMapManager' ;
5
- import { maxNumberRois , maxRoiId } from './utils/constants' ;
6
5
7
6
export interface FromMaskOptions {
8
7
/**
@@ -24,10 +23,10 @@ export function fromMask(
24
23
) : RoiMapManager {
25
24
const { allowCorners = false } = options ;
26
25
27
- const MAX_ARRAY = maxNumberRois - 1 ; // 65535 should be enough for most of the cases
26
+ const MAX_TODO_ARRAY_FILTER = 65535 ; // 65535 should be enough for most of the cases
28
27
29
- const maxPositiveId = maxRoiId - 1 ;
30
- const maxNegativeId = - maxRoiId ;
28
+ const MAX_POSITIVE_ID = 2 ** 31 - 1 ;
29
+ const MAX_NEGATIVE_ID = - ( 2 ** 31 - 1 ) ;
31
30
32
31
// based on a binary image we will create plenty of small images
33
32
const data = new Int32Array ( mask . size ) ; // maxValue: maxPositiveId, minValue: maxNegativeId
@@ -36,8 +35,8 @@ export function fromMask(
36
35
let positiveId = 0 ;
37
36
let negativeId = 0 ;
38
37
39
- const columnToProcess = new Uint16Array ( maxNumberRois ) ;
40
- const rowToProcess = new Uint16Array ( maxNumberRois ) ;
38
+ const columnToProcess = new Uint16Array ( MAX_TODO_ARRAY_FILTER + 1 ) ;
39
+ const rowToProcess = new Uint16Array ( MAX_TODO_ARRAY_FILTER + 1 ) ;
41
40
42
41
for ( let column = 0 ; column < mask . width ; column ++ ) {
43
42
for ( let row = 0 ; row < mask . height ; row ++ ) {
@@ -55,14 +54,14 @@ export function fromMask(
55
54
const targetState = mask . getBit ( column , row ) ;
56
55
const id = targetState ? ++ positiveId : -- negativeId ;
57
56
assert (
58
- positiveId <= maxPositiveId && negativeId >= maxNegativeId ,
57
+ positiveId <= MAX_POSITIVE_ID && negativeId >= MAX_NEGATIVE_ID ,
59
58
'too many regions of interest' ,
60
59
) ;
61
60
columnToProcess [ 0 ] = column ;
62
61
rowToProcess [ 0 ] = row ;
63
62
while ( from <= to ) {
64
- const currentColumn = columnToProcess [ from & MAX_ARRAY ] ;
65
- const currentRow = rowToProcess [ from & MAX_ARRAY ] ;
63
+ const currentColumn = columnToProcess [ from & MAX_TODO_ARRAY_FILTER ] ;
64
+ const currentRow = rowToProcess [ from & MAX_TODO_ARRAY_FILTER ] ;
66
65
data [ currentRow * mask . width + currentColumn ] = id ;
67
66
// need to check all around mask pixel
68
67
if (
@@ -72,9 +71,9 @@ export function fromMask(
72
71
) {
73
72
// LEFT
74
73
to ++ ;
75
- columnToProcess [ to & MAX_ARRAY ] = currentColumn - 1 ;
76
- rowToProcess [ to & MAX_ARRAY ] = currentRow ;
77
- data [ currentRow * mask . width + currentColumn - 1 ] = maxNegativeId ;
74
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn - 1 ;
75
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow ;
76
+ data [ currentRow * mask . width + currentColumn - 1 ] = MAX_NEGATIVE_ID ;
78
77
}
79
78
if (
80
79
currentRow > 0 &&
@@ -83,9 +82,9 @@ export function fromMask(
83
82
) {
84
83
// TOP
85
84
to ++ ;
86
- columnToProcess [ to & MAX_ARRAY ] = currentColumn ;
87
- rowToProcess [ to & MAX_ARRAY ] = currentRow - 1 ;
88
- data [ ( currentRow - 1 ) * mask . width + currentColumn ] = maxNegativeId ;
85
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn ;
86
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow - 1 ;
87
+ data [ ( currentRow - 1 ) * mask . width + currentColumn ] = MAX_NEGATIVE_ID ;
89
88
}
90
89
if (
91
90
currentColumn < mask . width - 1 &&
@@ -94,9 +93,9 @@ export function fromMask(
94
93
) {
95
94
// RIGHT
96
95
to ++ ;
97
- columnToProcess [ to & MAX_ARRAY ] = currentColumn + 1 ;
98
- rowToProcess [ to & MAX_ARRAY ] = currentRow ;
99
- data [ currentRow * mask . width + currentColumn + 1 ] = maxNegativeId ;
96
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn + 1 ;
97
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow ;
98
+ data [ currentRow * mask . width + currentColumn + 1 ] = MAX_NEGATIVE_ID ;
100
99
}
101
100
if (
102
101
currentRow < mask . height - 1 &&
@@ -105,9 +104,9 @@ export function fromMask(
105
104
) {
106
105
// BOTTOM
107
106
to ++ ;
108
- columnToProcess [ to & MAX_ARRAY ] = currentColumn ;
109
- rowToProcess [ to & MAX_ARRAY ] = currentRow + 1 ;
110
- data [ ( currentRow + 1 ) * mask . width + currentColumn ] = maxNegativeId ;
107
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn ;
108
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow + 1 ;
109
+ data [ ( currentRow + 1 ) * mask . width + currentColumn ] = MAX_NEGATIVE_ID ;
111
110
}
112
111
if ( allowCorners ) {
113
112
if (
@@ -118,10 +117,10 @@ export function fromMask(
118
117
) {
119
118
// TOP LEFT
120
119
to ++ ;
121
- columnToProcess [ to & MAX_ARRAY ] = currentColumn - 1 ;
122
- rowToProcess [ to & MAX_ARRAY ] = currentRow - 1 ;
120
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn - 1 ;
121
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow - 1 ;
123
122
data [ ( currentRow - 1 ) * mask . width + currentColumn - 1 ] =
124
- maxNegativeId ;
123
+ MAX_NEGATIVE_ID ;
125
124
}
126
125
if (
127
126
currentColumn < mask . width - 1 &&
@@ -131,10 +130,10 @@ export function fromMask(
131
130
) {
132
131
// TOP RIGHT
133
132
to ++ ;
134
- columnToProcess [ to & MAX_ARRAY ] = currentColumn + 1 ;
135
- rowToProcess [ to & MAX_ARRAY ] = currentRow - 1 ;
133
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn + 1 ;
134
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow - 1 ;
136
135
data [ ( currentRow - 1 ) * mask . width + currentColumn + 1 ] =
137
- maxNegativeId ;
136
+ MAX_NEGATIVE_ID ;
138
137
}
139
138
if (
140
139
currentColumn > 0 &&
@@ -144,10 +143,10 @@ export function fromMask(
144
143
) {
145
144
// BOTTOM LEFT
146
145
to ++ ;
147
- columnToProcess [ to & MAX_ARRAY ] = currentColumn - 1 ;
148
- rowToProcess [ to & MAX_ARRAY ] = currentRow + 1 ;
146
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn - 1 ;
147
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow + 1 ;
149
148
data [ ( currentRow + 1 ) * mask . width + currentColumn - 1 ] =
150
- maxNegativeId ;
149
+ MAX_NEGATIVE_ID ;
151
150
}
152
151
if (
153
152
currentColumn < mask . width - 1 &&
@@ -157,17 +156,17 @@ export function fromMask(
157
156
) {
158
157
// BOTTOM RIGHT
159
158
to ++ ;
160
- columnToProcess [ to & MAX_ARRAY ] = currentColumn + 1 ;
161
- rowToProcess [ to & MAX_ARRAY ] = currentRow + 1 ;
159
+ columnToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentColumn + 1 ;
160
+ rowToProcess [ to & MAX_TODO_ARRAY_FILTER ] = currentRow + 1 ;
162
161
data [ ( currentRow + 1 ) * mask . width + currentColumn + 1 ] =
163
- maxNegativeId ;
162
+ MAX_NEGATIVE_ID ;
164
163
}
165
164
}
166
165
167
166
from ++ ;
168
167
169
168
assert (
170
- to - from <= MAX_ARRAY ,
169
+ to - from <= MAX_TODO_ARRAY_FILTER ,
171
170
'fromMask can not finish, the array to manage internal data is not big enough.' +
172
171
'You could improve mask by changing MAX_ARRAY' ,
173
172
) ;
0 commit comments