Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

463 add pad function #465

Merged
merged 9 commits into from
May 27, 2024
2 changes: 1 addition & 1 deletion src/filters/convolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
} from 'ml-convolution';

import { Image } from '../Image';
import { extendBorders } from '../operations/extendBorders';
import { getClamp } from '../utils/clamp';
import { extendBorders } from '../utils/extendBorders';
import { getIndex } from '../utils/getIndex';
import { getOutputImage } from '../utils/getOutputImage';
import { BorderType, getBorderInterpolation } from '../utils/interpolateBorder';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
import { extendBorders } from '../extendBorders';

test('grey image with basic value', () => {
const image = testUtils.createGreyImage(`
1 2
3 4
5 6
7 8
`);

const newImage = extendBorders(image, {
horizontal: 1,
vertical: 1,
borderType: 'constant',
});
expect(newImage).toMatchImageData(`
0 0 0 0
0 1 2 0
0 3 4 0
0 5 6 0
0 7 8 0
0 0 0 0
`);
});

test('grey image with wrap', () => {
const image = testUtils.createGreyImage(`
1 2 3 4
Expand All @@ -8,26 +31,24 @@ test('grey image with wrap', () => {
4 5 6 7
5 6 7 8
`);

const newImage = extendBorders(image, {
horizontal: 2,
vertical: 3,
borderType: 'wrap',
});

expect(newImage).toMatchImageData(`
5 6 3 4 5 6 3 4
6 7 4 5 6 7 4 5
7 8 5 6 7 8 5 6
3 4 1 2 3 4 1 2
4 5 2 3 4 5 2 3
5 6 3 4 5 6 3 4
6 7 4 5 6 7 4 5
7 8 5 6 7 8 5 6
3 4 1 2 3 4 1 2
4 5 2 3 4 5 2 3
5 6 3 4 5 6 3 4
`);
5 6 3 4 5 6 3 4
6 7 4 5 6 7 4 5
7 8 5 6 7 8 5 6
3 4 1 2 3 4 1 2
4 5 2 3 4 5 2 3
5 6 3 4 5 6 3 4
6 7 4 5 6 7 4 5
7 8 5 6 7 8 5 6
3 4 1 2 3 4 1 2
4 5 2 3 4 5 2 3
5 6 3 4 5 6 3 4
`);
});

test('rgb image with default border type', () => {
Expand Down
19 changes: 16 additions & 3 deletions src/utils/extendBorders.ts → src/operations/extendBorders.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { Image } from '../Image';
import { BorderType, getBorderInterpolation } from '../utils/interpolateBorder';

import { BorderType, getBorderInterpolation } from './interpolateBorder';

interface ExtendBordersOptions {
export interface ExtendBordersOptions {
/**
* Left and right border thickness.
*/
horizontal: number;
/**
*Top and bottom border thickness.
*/
vertical: number;
/**
* Specify how the borders should be handled.
* @default `'reflect101'`
*/
borderType?: BorderType;
/**
* Value of the border if BorderType is 'constant'.
* @default `0`
*/
borderValue?: number;
}

Expand Down
1 change: 1 addition & 0 deletions src/operations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ export * from './operations.types';
export * from './paintMaskOnImage';
export * from './paintMaskOnMask';
export * from './correctBackground';
export * from './extendBorders';
Loading