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

Commit 8ca381f

Browse files
committed
fix: correct 16 bits image thresholding
Closes: #374
1 parent 6adde86 commit 8ca381f

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed
138 Bytes
Loading

src/operations/__tests__/threshold.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ImageColorModel, Image } from '../..';
12
import { computeThreshold, threshold } from '../threshold';
23

34
test('threshold with a fixed value of 100', () => {
@@ -94,3 +95,24 @@ test('error threshold out of range', () => {
9495
/threshold must be a value between 0 and 1/,
9596
);
9697
});
98+
99+
test('16 bits image simple', () => {
100+
const image = new Image(2, 2, {
101+
colorModel: ImageColorModel.GREY,
102+
bitDepth: 16,
103+
data: new Uint16Array([0, 100, 20000, 30000]),
104+
});
105+
const threshold = image.threshold();
106+
107+
expect(threshold).toMatchImageData([
108+
[0, 0],
109+
[1, 1],
110+
]);
111+
});
112+
113+
test('16 bits image', () => {
114+
const image = testUtils.load('formats/grey16.png');
115+
const threshold = image.threshold();
116+
117+
expect(threshold).toMatchImageSnapshot();
118+
});

src/operations/threshold.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export function computeThreshold(
8181
'threshold can only be computed on images with one channel',
8282
);
8383
}
84-
const histogram = image.histogram();
84+
const histogram = image.histogram({slots: 2**image.bitDepth});
8585

8686
switch (algorithm) {
8787
case 'huang':

0 commit comments

Comments
 (0)