Skip to content

Commit d80f4a7

Browse files
authoredMay 3, 2024··
fix: resize nearest (#454)
Refs: #452
1 parent 8d81c2b commit d80f4a7

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed
 

‎src/geometry/__tests__/resize.test.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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');
57

68
const resized = img.resize({
@@ -12,14 +14,22 @@ test.skip('compare result of resize with opencv (nearest)', () => {
1214
expect(resized).toMatchImage('opencv/testResizeNearest.png');
1315
});
1416

15-
test.skip('compare result of resize with opencv (bilinear)', () => {
17+
test.skip('compare result of resize with opencv (bilinear)', async () => {
1618
const img = testUtils.load('opencv/test.png');
19+
const expectedImg = testUtils.load('opencv/testResizeBilinear.png');
1720

1821
const resized = img.resize({
1922
xFactor: 10,
2023
yFactor: 10,
2124
});
2225

26+
const substraction = expectedImg.clone().subtract(resized);
27+
await write(
28+
path.join(__dirname, 'resize_bilinear_substraction.png'),
29+
substraction,
30+
);
31+
await write(path.join(__dirname, 'resize_bilinear.png'), resized);
32+
2333
expect(resized).toMatchImage('opencv/testResizeBilinear.png');
2434
});
2535

‎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)