Fix crop preprocessor

This commit is contained in:
Jason Miller
2020-12-09 11:22:43 -05:00
parent b8574b228a
commit e3b053db12

View File

@@ -13,51 +13,25 @@
import { Options } from '../shared/meta'; import { Options } from '../shared/meta';
export default async function crop( export default async function crop(
data: ImageData, { data, width, height }: ImageData,
opts: Options, { top, right, bottom, left }: Options,
): Promise<ImageData> { ): Promise<ImageData> {
const { left, top, right, bottom } = opts;
const source = data.data;
const { width, height } = data;
const cols = width * 4;
const newWidth = width - left - right; const newWidth = width - left - right;
const newHeight = height - top - bottom; const newHeight = height - top - bottom;
const len = newWidth * newHeight * 4;
const pixels = new Uint8ClampedArray(len);
const cols = width * 4;
const newCols = newWidth * 4;
const pixels = new Uint8ClampedArray(data.buffer, 0, newHeight * newCols);
for (let y = 0; y < newHeight; y++) { for (let y = 0; y < newHeight; y++) {
for (let x = 0; x < newWidth; x++) { const x = left * 4;
let i = y * cols + x * 4; const row = new Uint8ClampedArray(
let j = (top + y) * cols + (left + x) * 4; data.buffer,
pixels[i] = source[j]; (top + y) * cols + x,
pixels[i + 1] = source[j + 1]; newCols,
pixels[i + 2] = source[j + 2]; );
pixels[i + 3] = source[j + 3]; pixels.set(row, y * newCols);
}
} }
// let sourceX = left; return new ImageData(pixels.slice(), newWidth, newHeight);
// let sourceY = top;
// let x = 0;
// let y = 0;
// let i = 0;
// while (i < len) {
// let from = sourceY * cols + sourceX * 4;
// pixels[i++] = source[from++];
// pixels[i++] = source[from++];
// pixels[i++] = source[from++];
// pixels[i++] = source[from];
// if (++x === newWidth) {
// x = 0;
// y++;
// sourceX = left;
// sourceY++;
// }
// }
return new ImageData(pixels, newWidth, newHeight);
} }