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

Commit 1e2b09d

Browse files
committed
fix: move important line to histogram
1 parent 8ca381f commit 1e2b09d

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/compute/__tests__/histogram.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ test('binary image', () => {
3636
expect(histogram[255]).toBe(9);
3737
});
3838

39+
test('grey 16-bit image', () => {
40+
const image = createImageFromData(
41+
[
42+
[0, 0, 0, 0, 0],
43+
[0, 40000, 255, 255, 0],
44+
[0, 255, 255, 255, 0],
45+
[0, 255, 255, 255, 0],
46+
[0, 0, 0, 0, 0],
47+
],
48+
'GREY',
49+
{ bitDepth: 16 },
50+
);
51+
const histogram = image.histogram();
52+
53+
expect(histogram.length).toBe(2 ** 16);
54+
});
55+
3956
test('grey 16-bit image with 2 slots', () => {
4057
const image = createImageFromData(
4158
[

src/compute/histogram.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface HistogramOptions {
1111
channel?: number;
1212
/**
1313
* The number of slots that histogram can have.
14-
* @default `256`
14+
* @default 2 ** image.bitDepth
1515
*/
1616
slots?: number;
1717
}
@@ -27,7 +27,7 @@ export function histogram(
2727
options: HistogramOptions = {},
2828
): Uint32Array {
2929
let { channel } = options;
30-
const { slots = 256 } = options;
30+
const { slots = 2 ** image.bitDepth } = options;
3131
if (!(slots !== 0 && (slots & (slots - 1)) === 0)) {
3232
throw new RangeError(
3333
'slots must be a power of 2, for example: 64, 256, 1024',

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({slots: 2**image.bitDepth});
84+
const histogram = image.histogram();
8585

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

0 commit comments

Comments
 (0)