Skip to content
This repository was archived by the owner on Jul 26, 2025. It is now read-only.

Commit afecc5f

Browse files
authored
feat: RoiMap could too easily contain over 2^15 rois (#388)
In order to solve this issue we are using Int32Array instead of Int16Array typed array
1 parent ecab15e commit afecc5f

File tree

6 files changed

+26
-16
lines changed

6 files changed

+26
-16
lines changed

src/roi/RoiMapManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface RoiMap {
2323
* Data of the ROIs. Each ROI is associated with a negative or a positive value,
2424
* depending if it derives from a zone made of zeros or ones in the original mask.
2525
*/
26-
data: Int16Array;
26+
data: Int32Array;
2727
/**
2828
* Number of distinct positive values in the ROI map.
2929
*

src/roi/__tests__/Roi.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ test('getMap', () => {
2424
// @ts-expect-error the map property is private
2525
const result = rois[0].map.data;
2626

27-
const expected = new Int16Array([-1, 1, -3, 1, 1, 1, -2, -2, -2]);
27+
const expected = new Int32Array([-1, 1, -3, 1, 1, 1, -2, -2, -2]);
2828
expect(result).toStrictEqual(expected);
2929
});

src/roi/__tests__/waterShed.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { computeThreshold } from '../..';
2-
import { createGreyImage, getInt16Array } from '../../../test/testUtils';
2+
import { createGreyImage, getInt32Array } from '../../../test/testUtils';
33
import { waterShed } from '../waterShed';
44

55
describe('Test WaterShed Roi generation', () => {
@@ -12,7 +12,7 @@ describe('Test WaterShed Roi generation', () => {
1212
[3, 3, 3, 3, 3],
1313
]);
1414
const roiMapManager = waterShed(image, {});
15-
const resultArray = testUtils.getInt16Array(`
15+
const resultArray = testUtils.getInt32Array(`
1616
-1, -1, -1, -1, -1,
1717
-1, -1, -1, -1, -1,
1818
-1, -1, -1, -1, -1,
@@ -47,7 +47,7 @@ describe('Test WaterShed Roi generation', () => {
4747

4848
const roiMapManager = waterShed(image, { threshold: 2 / 255 });
4949

50-
const resultArray = getInt16Array(`
50+
const resultArray = getInt32Array(`
5151
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
5252
0, 0,-2,-2,-2, 0, 0, 0, 0, 0,
5353
0, 0,-2,-2,-2,-2, 0, 0, 0, 0,
@@ -86,7 +86,7 @@ describe('Test WaterShed Roi generation', () => {
8686
const roiMapManager = waterShed(invertedImage, {
8787
threshold: 253 / image.maxValue,
8888
});
89-
const resultArray = getInt16Array(`
89+
const resultArray = getInt32Array(`
9090
0, 0, 0, 0, 0,
9191
0,-1,-1,-1, 0,
9292
0,-1,-1,-1, 0,
@@ -127,7 +127,7 @@ describe('Test WaterShed Roi generation', () => {
127127
const roiMapManager = waterShed(image, {
128128
threshold: threshold / image.maxValue,
129129
});
130-
const resultArray = getInt16Array(`
130+
const resultArray = getInt32Array(`
131131
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
132132
0, 0,-2,-2,-2, 0, 0, 0, 0, 0,
133133
0, 0,-2,-2,-2,-2, 0, 0, 0, 0,
@@ -167,7 +167,7 @@ describe('Test WaterShed Roi generation', () => {
167167

168168
const mask = image.threshold({ algorithm: 'otsu' });
169169
const roiMapManager = waterShed(image, { mask });
170-
const resultArray = getInt16Array(`
170+
const resultArray = getInt32Array(`
171171
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1,
172172
-2, -2, -2, -2, -2, -2, -2, 0, -1, -1,
173173
-2, -2, -2, -2, -2, -2, -2, -2, -1, -1,

src/roi/fromMask.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export function fromMask(
3030
const maxNegativeId = -maxRoiId;
3131

3232
// based on a binary image we will create plenty of small images
33-
const data = new Int16Array(mask.size); // maxValue: maxPositiveId, minValue: maxNegativeId
33+
const data = new Int32Array(mask.size); // maxValue: maxPositiveId, minValue: maxNegativeId
3434

3535
// split will always return an array of images
3636
let positiveId = 0;

src/roi/waterShed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function waterShed(
8181

8282
const maskExpectedValue = 0;
8383

84-
const data = new Int16Array(currentImage.size);
84+
const data = new Int32Array(currentImage.size);
8585
const width = currentImage.width;
8686
const height = currentImage.height;
8787
const toProcess = new PriorityQueue({

test/testUtils.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ import { TestImagePath } from './TestImagePath';
99
import { createImageFromData, CreateImageOptions } from './createImageFromData';
1010
import { createMask } from './createMask';
1111

12-
13-
14-
15-
1612
/**
1713
* Return the path to a given image.
1814
* @param name - Test image name.
@@ -142,6 +138,20 @@ export function getInt16Array(string: string) {
142138
.map(Number),
143139
);
144140
}
141+
142+
/**
143+
* Creates an Int32Array from string
144+
* @param string - represents Int32Array data
145+
* @returns Int32Array
146+
*/
147+
export function getInt32Array(string: string) {
148+
return new Int32Array(
149+
string
150+
.split(/[\n\r ,]+/)
151+
.filter(Boolean)
152+
.map(Number),
153+
);
154+
}
145155
declare global {
146156
// eslint-disable-next-line no-var
147157
var testUtils: {
@@ -157,8 +167,8 @@ declare global {
157167
makeTmpDir: typeof makeTmpDir;
158168
cleanTmpDir: typeof cleanTmpDir;
159169
getInt16Array: typeof getInt16Array;
170+
getInt32Array: typeof getInt32Array;
160171
};
161172
}
162173

163-
export {createMask} from './createMask';
164-
174+
export { createMask } from './createMask';

0 commit comments

Comments
 (0)