Commit 867cb6f6 by linjinhong

修改问题

parent d4a4c089
...@@ -12,7 +12,6 @@ const sharp = require("sharp"); ...@@ -12,7 +12,6 @@ const sharp = require("sharp");
*/ */
async function cropImageTransparentEdges(inputPath, outputPath, options = {}) { async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
const threshold = options.threshold || 10; const threshold = options.threshold || 10;
const blockSize = options.blockSize || 500; // 可调的块大小,根据内存调整
// 确保输出目录存在 // 确保输出目录存在
const outputDir = path.dirname(outputPath); const outputDir = path.dirname(outputPath);
...@@ -40,44 +39,17 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) { ...@@ -40,44 +39,17 @@ async function cropImageTransparentEdges(inputPath, outputPath, options = {}) {
let leftmost = width; let leftmost = width;
let rightmost = 0; let rightmost = 0;
// for (let y = 0; y < height; y++) { for (let y = 0; y < height; y++) {
// for (let x = 0; x < width; x++) { for (let x = 0; x < width; x++) {
// const alphaIdx = (y * width + x) * channels + channels - 1; const alphaIdx = (y * width + x) * channels + channels - 1;
// const alpha = data[alphaIdx]; const alpha = data[alphaIdx];
// // 如果像素不是透明的(根据阈值) // 如果像素不是透明的(根据阈值)
// if (alpha > threshold) { if (alpha > threshold) {
// topmost = Math.min(topmost, y); topmost = Math.min(topmost, y);
// bottommost = Math.max(bottommost, y); bottommost = Math.max(bottommost, y);
// leftmost = Math.min(leftmost, x); leftmost = Math.min(leftmost, x);
// rightmost = Math.max(rightmost, x); rightmost = Math.max(rightmost, x);
// }
// }
// }
const alphaIndexOffset = channels - 1; // alpha channel
// 分块扫描
for (let yStart = 0; yStart < height; yStart += blockSize) {
for (let xStart = 0; xStart < width; xStart += blockSize) {
const blockHeight = Math.min(blockSize, height - yStart);
const blockWidth = Math.min(blockSize, width - xStart);
for (let y = 0; y < blockHeight; y++) {
for (let x = 0; x < blockWidth; x++) {
const alphaIdx =
((yStart + y) * width + (xStart + x)) * channels +
alphaIndexOffset;
const alpha = data[alphaIdx];
// 判断透明度是否大于阈值
if (alpha > threshold) {
// 更新边界
topmost = Math.min(topmost, yStart + y);
bottommost = Math.max(bottommost, yStart + y);
leftmost = Math.min(leftmost, xStart + x);
rightmost = Math.max(rightmost, xStart + x);
}
}
} }
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment