feat(photo): CPU-обработка фото с пайплайном sharp
- новые эндпоинты: /api/entries/:id/photo/cpu-analyze, cpu-preview, enhance-cpu - серверный пайплайн sharp: Lanczos ×2, median+blur, CLAHE, modulate, tint, linear, sharpen, mozjpeg - авто-подбор параметров по статистике фото, превью ограничено 1400px - worker: CPU-постобработка поверх результата photo-ai, no-op при нейтральных значениях - модалка #cpuEnhanceModal: сравнение оригинал/результат, зум, 8 ползунков, режимы auto/ai/cpu, горячие клавиши - индикатор прогресса фото-ИИ остаётся видимым до загрузки результата
This commit is contained in:
@@ -282,10 +282,80 @@ function createPhotoEnhanceWorker({ pool, getSetting, logAudit, invalidateEntrie
|
||||
};
|
||||
}
|
||||
|
||||
function cpuPostParams(job) {
|
||||
const p = job && job.params && typeof job.params === 'object' ? job.params.cpu : null;
|
||||
if (!p || typeof p !== 'object') return null;
|
||||
const num = (v, min, max, def) => {
|
||||
const n = Number(v);
|
||||
return Number.isFinite(n) ? Math.min(max, Math.max(min, n)) : def;
|
||||
};
|
||||
return {
|
||||
clarity: num(p.clarity, 0, 100, 0),
|
||||
denoise: num(p.denoise, 0, 100, 0),
|
||||
sharp: num(p.sharp, 0, 100, 0),
|
||||
brightness: num(p.brightness, 40, 200, 100),
|
||||
contrast: num(p.contrast, 40, 200, 100),
|
||||
saturate: num(p.saturate, 0, 200, 100),
|
||||
warmth: num(p.warmth, -100, 100, 0),
|
||||
quality: num(p.quality, 70, 100, 92),
|
||||
scale: num(p.scale, 1, 2, 1),
|
||||
};
|
||||
}
|
||||
|
||||
function cpuPostIsNoop(c) {
|
||||
if (!c) return true;
|
||||
return c.clarity === 0 && c.denoise === 0 && c.sharp === 0
|
||||
&& c.brightness === 100 && c.contrast === 100 && c.saturate === 100
|
||||
&& c.warmth === 0 && c.quality >= 92 && c.scale === 1;
|
||||
}
|
||||
|
||||
async function cpuFinishBuffer(buf, c) {
|
||||
if (!sharp || !c || cpuPostIsNoop(c)) return buf;
|
||||
let img = sharp(buf);
|
||||
const meta = await img.metadata();
|
||||
if (c.scale > 1 && (meta.width || 0) > 0) {
|
||||
img = img.resize({
|
||||
width: Math.round((meta.width || 1) * c.scale),
|
||||
height: Math.round((meta.height || 1) * c.scale),
|
||||
kernel: 'lanczos3',
|
||||
fit: 'fill',
|
||||
});
|
||||
}
|
||||
if (c.denoise > 0) {
|
||||
img = img.median(c.denoise > 70 ? 5 : 3);
|
||||
img = img.blur(c.denoise > 70 ? 0.6 : 0.4);
|
||||
}
|
||||
if (c.clarity > 0) {
|
||||
try {
|
||||
img = img.clahe({
|
||||
width: Math.max(64, Math.round((meta.width || 800) / 8)),
|
||||
height: Math.max(64, Math.round((meta.height || 600) / 8)),
|
||||
maxSlope: Math.max(1, Math.min(100, Math.round(1 + (c.clarity / 100) * 2.2))),
|
||||
});
|
||||
} catch (e) {}
|
||||
}
|
||||
img = img.modulate({ brightness: c.brightness / 100, saturation: c.saturate / 100 });
|
||||
if (Math.abs(c.warmth) > 0.5) {
|
||||
const t = c.warmth / 100;
|
||||
img = t > 0
|
||||
? img.modulate({ brightness: c.brightness / 100, saturation: c.saturate / 100, tint: Math.min(1, t * 0.25) })
|
||||
: img.tint({ r: 255, g: Math.round(255 - t * 12), b: Math.round(255 + t * 22) });
|
||||
}
|
||||
if (c.contrast !== 100) {
|
||||
const a = c.contrast / 100;
|
||||
img = img.linear(a, 128 * (1 - a));
|
||||
}
|
||||
if (c.sharp > 0) {
|
||||
img = img.sharpen({ sigma: 0.4 + (c.sharp / 100) * 1.4, m1: 0, m2: 1 + c.sharp / 60 });
|
||||
}
|
||||
return img.jpeg({ quality: Math.round(c.quality), mozjpeg: true }).toBuffer();
|
||||
}
|
||||
|
||||
async function enhanceWithAi(srcKey, job) {
|
||||
const { out, meta } = await runAiEnhance(srcKey, job);
|
||||
const finalBuf = await cpuFinishBuffer(out, cpuPostParams(job));
|
||||
const newName = crypto.randomBytes(12).toString('hex') + '.jpg';
|
||||
await storage.put(newName, out);
|
||||
await storage.put(newName, finalBuf);
|
||||
return { path: `/uploads/${newName}`, meta };
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user