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

Commit 646dc2b

Browse files
fix: correct hypothenuse implementation when custom channels are passed (#407)
1 parent 3a9bc79 commit 646dc2b

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/filters/__tests__/hypotenuse.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ test('different number of channels', () => {
4141
image.hypotenuse(otherImage);
4242
}).toThrow('both images must have the same number of channels');
4343
});
44+
45+
test('testing with a custom channel', () => {
46+
const image = testUtils.createRgbImage([[3, 5, 8]]);
47+
const otherImage = testUtils.createRgbImage([[4, 12, 15]]);
48+
49+
const expected = testUtils.createRgbImage([[3, 13, 8]]);
50+
expect(image.hypotenuse(otherImage, { channels: [1] })).toMatchImage(
51+
expected,
52+
);
53+
});
54+
55+
test('testing with custom channels', () => {
56+
const image = testUtils.createRgbaImage([[3, 5, 8, 0]]);
57+
const otherImage = testUtils.createRgbaImage([[4, 12, 15, 0]]);
58+
59+
const expected = testUtils.createRgbaImage([[5, 13, 17, 0]]);
60+
expect(image.hypotenuse(otherImage, { channels: [0, 1, 2] })).toMatchImage(
61+
expected,
62+
);
63+
});

src/filters/hypotenuse.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
import { BitDepth, Image } from '..';
1+
import { Image } from '..';
2+
import { getOutputImage } from '../utils/getOutputImage';
23
import checkProcessable from '../utils/validators/checkProcessable';
34
import { validateChannels } from '../utils/validators/validators';
45

56
export interface HypotenuseOptions {
6-
/**
7-
* Bit depth of the resulting image.
8-
* @default `image.bitDepth`
9-
*/
10-
bitDepth?: BitDepth;
117
/**
128
* To which channels to apply the filter. By default all but alpha.
139
*/
@@ -26,11 +22,9 @@ export function hypotenuse(
2622
otherImage: Image,
2723
options: HypotenuseOptions = {},
2824
): Image {
29-
const { bitDepth = image.bitDepth, channels = [] } = options;
30-
31-
for (let i = 0; i < image.components; i++) {
32-
channels.push(i);
33-
}
25+
const {
26+
channels = new Array(image.components).fill(0).map((value, index) => index),
27+
} = options;
3428

3529
checkProcessable(image, {
3630
bitDepth: [8, 16],
@@ -51,14 +45,15 @@ export function hypotenuse(
5145

5246
validateChannels(channels, image);
5347

54-
const newImage = Image.createFrom(image, { bitDepth });
48+
const newImage = getOutputImage(image, {}, { clone: true });
5549

5650
for (const channel of channels) {
5751
for (let i = 0; i < image.size; i++) {
5852
const value = Math.hypot(
5953
image.getValueByIndex(i, channel),
6054
otherImage.getValueByIndex(i, channel),
6155
);
56+
6257
newImage.setValueByIndex(
6358
i,
6459
channel,

0 commit comments

Comments
 (0)