Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (
transformMatrix.length !== 2 ||
transformMatrix[0].length !== 3 ||
transformMatrix[1].length !== 3
) {
throw new Error(
`transformation matrix must be 2x3, found ${transformMatrix.length}x${
transformMatrix[1].length
}`
);
}
if (!options.inverse) {
transformMatrix = [transformMatrix[0], transformMatrix[1], [0, 0, 1]];
transformMatrix = inverse(new Matrix(transformMatrix)).to2DArray();
}
const newImage = Image.createFrom(image, {
width,
height
});
const interpolateBorder = getBorderInterpolation(borderType, borderValue);
const clamp = getClamp(newImage);
const interpolate = getInterpolationFunction(interpolationType);
const hFactor = newImage.width * newImage.channels;
for (let y = 0; y < newImage.height; y++) {
const hOffset = hFactor * y;
for (let x = 0; x < newImage.width; x++) {
const wOffset = hOffset + x * image.channels;
const nx = transformPoint(transformMatrix[0], x, y);
let n31 = n3[0];
let n32 = n3[1];
let n33 = n3[2];
let f = (1.0 / (n23 * n33)) * ((n21 * n31 - (n21 * n33 + n23 * n31) * u0 + n23 * n33 * u0 * u0) + (n22 * n32 - (n22 * n33 + n23 * n32) * v0 + n23 * n33 * v0 * v0));
if (f >= 0) {
f = Math.sqrt(f);
} else {
f = Math.sqrt(-f);
}
let A = new Matrix([[f, 0, u0], [0, f, v0], [0, 0, 1]]);
let At = A.transpose();
let Ati = inverse(At);
let Ai = inverse(A);
let n2R = Matrix.rowVector(n2);
let n3R = Matrix.rowVector(n3);
let arReal = Math.sqrt(dotVect(n2R.mmul(Ati).mmul(Ai).to1DArray(), n2) / dotVect(n3R.mmul(Ati).mmul(Ai).to1DArray(), n3));
if (arReal === 0 || arVis === 0) {
finalW = Math.ceil(w);
finalH = Math.ceil(h);
} else if (arReal < arVis) {
finalW = Math.ceil(w);
finalH = Math.ceil(finalW / arReal);
} else {
finalH = Math.ceil(h);
finalW = Math.ceil(arReal * finalH);
let n31 = n3[0];
let n32 = n3[1];
let n33 = n3[2];
let f = (1.0 / (n23 * n33)) * ((n21 * n31 - (n21 * n33 + n23 * n31) * u0 + n23 * n33 * u0 * u0) + (n22 * n32 - (n22 * n33 + n23 * n32) * v0 + n23 * n33 * v0 * v0));
if (f >= 0) {
f = Math.sqrt(f);
} else {
f = Math.sqrt(-f);
}
let A = new Matrix([[f, 0, u0], [0, f, v0], [0, 0, 1]]);
let At = A.transpose();
let Ati = inverse(At);
let Ai = inverse(A);
let n2R = Matrix.rowVector(n2);
let n3R = Matrix.rowVector(n3);
let arReal = Math.sqrt(dotVect(n2R.mmul(Ati).mmul(Ai).to1DArray(), n2) / dotVect(n3R.mmul(Ati).mmul(Ai).to1DArray(), n3));
if (arReal === 0 || arVis === 0) {
finalW = Math.ceil(w);
finalH = Math.ceil(h);
} else if (arReal < arVis) {
finalW = Math.ceil(w);
finalH = Math.ceil(finalW / arReal);
} else {
finalH = Math.ceil(h);