Skip to content

fix: fix bug with transform function and fullImage:true #487

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

Merged
merged 2 commits into from
Jul 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/geometry/__tests__/transform.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,24 @@ test('compare result of clockwise rotation with opencv', () => {
expect(transformed).toMatchImage('opencv/testClockwiseRot90.png');
});

test('compare result of clockwise rotation with opencv and fullImage:true', () => {
const img = testUtils.load('opencv/test.png');
const transformed = img.transform(
[
[0, -1, img.width + 1],
[1, 0, 0],
],
{
inverse: false,
fullImage: true,
borderType: 'constant',
borderValue: 0,
interpolationType: 'bilinear',
},
);
expect(transformed).toMatchImage('opencv/testClockwiseRot90.png');
});

test('compare result of anti-clockwise rotation with opencv', () => {
const img = testUtils.load('opencv/test.png');
const transformed = img.transform(
Expand Down
19 changes: 10 additions & 9 deletions src/geometry/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ export function transform(
fullImage,
} = options;
let { width = image.width, height = image.height } = options;

if (!isValidMatrix(transformMatrix)) {
throw new TypeError(
`transformation matrix must be 2x3 or 3x3. Received ${transformMatrix.length}x${transformMatrix[1].length}`,
);
}
if (transformMatrix.length === 2) {
transformMatrix.push([0, 0, 1]);
}

if (fullImage) {
transformMatrix = transformMatrix.map((row) => row.slice());
transformMatrix[0][2] = 0;
Expand Down Expand Up @@ -126,15 +136,6 @@ export function transform(
height = Math.round(height);
}

if (!isValidMatrix(transformMatrix)) {
throw new TypeError(
`transformation matrix must be 2x3 or 3x3. Received ${transformMatrix.length}x${transformMatrix[1].length}`,
);
}
if (transformMatrix.length === 2) {
transformMatrix.push([0, 0, 1]);
}

if (!options.inverse) {
transformMatrix = inverse(new Matrix(transformMatrix)).to2DArray();
}
Expand Down
Loading