Skip to content

Commit 1944341

Browse files
committed
fix: resize nearest
chirurgical fix, rework of #453 Refs: #452
1 parent 8d81c2b commit 1944341

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/geometry/__tests__/resize.test.ts

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
import { encodePng } from '../../save';
1+
import 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

15-
test.skip('compare result of resize with opencv (bilinear)', () => {
25+
test.skip('compare result of resize with opencv (bilinear)', async () => {
1626
const img = testUtils.load('opencv/test.png');
27+
const expectedImg = testUtils.load('opencv/testResizeBilinear.png');
1728

1829
const resized = img.resize({
1930
xFactor: 10,
2031
yFactor: 10,
2132
});
2233

34+
const substraction = expectedImg.clone().subtract(resized);
35+
await write(
36+
path.join(__dirname, 'resize_bilinear_substraction.png'),
37+
substraction,
38+
);
39+
await write(path.join(__dirname, 'resize_bilinear.png'), resized);
40+
2341
expect(resized).toMatchImage('opencv/testResizeBilinear.png');
2442
});
2543

src/geometry/resize.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Image } from '../Image';
22
import { getClamp } from '../utils/clamp';
3-
import { getBorderInterpolation, BorderType } from '../utils/interpolateBorder';
3+
import { BorderType, getBorderInterpolation } from '../utils/interpolateBorder';
44
import {
55
getInterpolationFunction,
66
InterpolationType,
77
} from '../utils/interpolatePixel';
88
import { assert } from '../utils/validators/assert';
99

10+
import { transform } from './transform';
11+
1012
export interface ResizeOptions {
1113
/**
1214
* Width of the output image.
@@ -58,7 +60,25 @@ export function resize(image: Image, options: ResizeOptions): Image {
5860
borderType = 'constant',
5961
borderValue = 0,
6062
} = options;
61-
const { width, height } = checkOptions(image, options);
63+
const { width, height, xFactor, yFactor } = checkOptions(image, options);
64+
65+
if (interpolationType === 'nearest') {
66+
return transform(
67+
image,
68+
[
69+
[xFactor, 0, xFactor / 2],
70+
[0, yFactor, yFactor / 2],
71+
],
72+
{
73+
interpolationType,
74+
borderType,
75+
borderValue,
76+
height,
77+
width,
78+
},
79+
);
80+
}
81+
6282
const newImage = Image.createFrom(image, { width, height });
6383
const interpolate = getInterpolationFunction(interpolationType);
6484
const interpolateBorder = getBorderInterpolation(borderType, borderValue);

0 commit comments

Comments
 (0)