From b1959e977d45a8b043b753aadac59d92812c382b Mon Sep 17 00:00:00 2001 From: SnapOtter Date: Tue, 5 May 2026 23:07:14 +0800 Subject: [PATCH] fix: improve defringe quality and add defensive guards in transparency-fixer --- apps/api/src/routes/tools/transparency-fixer.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/api/src/routes/tools/transparency-fixer.ts b/apps/api/src/routes/tools/transparency-fixer.ts index 88d64b4a..efc5db53 100644 --- a/apps/api/src/routes/tools/transparency-fixer.ts +++ b/apps/api/src/routes/tools/transparency-fixer.ts @@ -50,7 +50,7 @@ async function applyDefringe(buffer: Buffer, intensity: number): Promise } // Blur the alpha channel - const blurRadius = Math.max(1, Math.round(intensity / 20)); + const blurRadius = Math.max(0.3, Math.round(intensity / 20)); const blurredAlphaRaw = await sharp(alpha, { raw: { width: info.width, height: info.height, channels: 1 }, }) @@ -63,6 +63,9 @@ async function applyDefringe(buffer: Buffer, intensity: number): Promise const result = Buffer.from(data); for (let i = 0; i < pixelCount; i++) { if (alpha[i] > 0 && blurredAlphaRaw[i] < threshold) { + result[i * 4] = 0; + result[i * 4 + 1] = 0; + result[i * 4 + 2] = 0; result[i * 4 + 3] = 0; } } @@ -96,6 +99,9 @@ async function processTransparencyFix( const isOom = err instanceof Error && err.message.includes("out of memory"); if (!isOom) throw err; + // removeBackground has its own internal u2net fallback on OOM. + // This route-level fallback provides an intermediate quality step + // (birefnet-general) before that kicks in on a second OOM. onProgress?.(5, `Retrying with fallback model (${FALLBACK_MODEL})`); resultBuffer = await removeBackground( inputBuffer,