Skip to content

Commit e2e8618

Browse files
committed
wip: fix: resize with interpolationType nearest
Closes: #452
1 parent 8d81c2b commit e2e8618

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Diff for: src/geometry/__tests__/resize.test.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
1-
import { encodePng } from '../../save';
1+
import * as path from 'node:path';
22

3-
test.skip('compare result of resize with opencv (nearest)', () => {
3+
import { encodePng, write } from '../../save';
4+
5+
test('compare result of resize with opencv (nearest)', async () => {
46
const img = testUtils.load('opencv/test.png');
7+
const expectedImg = testUtils.load('opencv/testResizeNearest.png');
58

69
const resized = img.resize({
710
xFactor: 10,
811
yFactor: 10,
912
interpolationType: 'nearest',
1013
});
1114

15+
const substraction = expectedImg.clone().subtract(resized);
16+
await write(
17+
path.join(__dirname, 'resize_nearest_substraction.png'),
18+
substraction,
19+
);
20+
await write(path.join(__dirname, 'resize_nearest.png'), resized);
21+
1222
expect(resized).toMatchImage('opencv/testResizeNearest.png');
1323
});
1424

Diff for: src/geometry/resize.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -58,17 +58,21 @@ export function resize(image: Image, options: ResizeOptions): Image {
5858
borderType = 'constant',
5959
borderValue = 0,
6060
} = options;
61-
const { width, height } = checkOptions(image, options);
61+
const { width, height, xFactor, yFactor } = checkOptions(image, options);
6262
const newImage = Image.createFrom(image, { width, height });
6363
const interpolate = getInterpolationFunction(interpolationType);
6464
const interpolateBorder = getBorderInterpolation(borderType, borderValue);
6565
const clamp = getClamp(newImage);
66-
const intervalX = (image.width - 1) / (width - 1);
67-
const intervalY = (image.height - 1) / (height - 1);
66+
const wRatio = 1 / xFactor;
67+
const hRatio = 1 / yFactor;
68+
// const intervalX = (image.width - 1) / (width - 1);
69+
// const intervalY = (image.height - 1) / (height - 1);
6870
for (let row = 0; row < newImage.height; row++) {
6971
for (let column = 0; column < newImage.width; column++) {
70-
const nx = column * intervalX;
71-
const ny = row * intervalY;
72+
const nx = (column + 0.5) * wRatio;
73+
const ny = (row + 0.5) * hRatio;
74+
// const nx = column / xFactor;
75+
// const ny = row / yFactor;
7276
for (let channel = 0; channel < newImage.channels; channel++) {
7377
const newValue = interpolate(
7478
image,

Diff for: src/utils/interpolatePixel.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ function interpolateNearest(
6161
channel: number,
6262
interpolateBorder: BorderInterpolationFunction,
6363
): number {
64-
column = Math.round(column);
65-
row = Math.round(row);
64+
column = Math.floor(column);
65+
row = Math.floor(row);
6666

6767
return interpolateBorder(column, row, channel, image);
6868
}

0 commit comments

Comments
 (0)