mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
* feat(find-duplicates): upgrade to 128-bit dHash with metadata and thumbnails * feat(find-duplicates): add custom-results display mode and duplicate store * feat(find-duplicates): add results overview grid and detail comparison view * feat(find-duplicates): overhaul settings with sensitivity presets and download actions * feat(find-duplicates): update i18n description * chore: replace jsqr with zxing-wasm for barcode reading * feat(barcode-read): rewrite backend with zxing-wasm for all barcode types * feat(barcode-read): rewrite frontend with multi-file, results table, progress, export - Multi-file sequential processing with per-file progress - Structured results table with type badges and copy per-result - Copy All and Export CSV functionality - Thorough scan toggle (maps to tryHarder in zxing-wasm) - Before/after view shows annotated image with bounding boxes - Updated tool description in constants and i18n * feat(stitch): update tool name and description for redesign * feat(stitch): add grid layout, alignment, border, radius, quality, and new resize modes * feat(stitch): redesign settings UI with grid, alignment, border, radius, quality * test(stitch): add stitch to e2e tool navigation suite * feat(vectorize): redesign with dual-engine backend and preset-driven UI - Backend: potrace for B&W, VTracer (@neplex/vectorizer) for full-color vectorization - Frontend: 5 presets (logo, illustration, photo, sketch, custom) - Settings: color precision, gradient step, detail, smoothing, corner threshold, invert - Updated OpenAPI spec and i18n description * feat(border): redesign with presets, shadow, padding color, swatches - Add 8 one-click presets (Clean White, Gallery Black, Shadow, Rounded, Polaroid, Vintage, Minimal, Cinematic) - Implement proper shadow rendering with blur, offset X/Y, color, opacity - Add padding color control (was hardcoded white) - Add color swatches for quick color selection - Wrap in form for Enter key submission - Add smart validation (requires at least one effect active) - Align frontend/backend slider ranges - Organize UI with sections and collapsible shadow toggle * feat(split): overhaul image splitting with live grid overlay and tile preview - Add interactive-split display mode with SplitCanvas component - Live SVG grid overlay on uploaded image showing split boundaries - Two split modes: Grid (NxM) and Tile Size (px dimensions) - 9 grid presets (2x1, 1x2, 2x2, 3x1, 1x3, 3x3, 2x3, 3x2, 4x4) - Output format selection (original/PNG/JPG/WebP) with quality slider - Post-split tile preview thumbnails with individual download - Download All as ZIP button - HEIC/HEIF preview with loading spinner - Backend: tile-size mode, output format conversion, quality control - Zustand store for split state management * feat(split): rewrite backend and frontend settings Backend: tile-size mode, output format conversion, quality control. Frontend: split modes, presets, format selector, tile preview grid. * feat(border): add live CSS preview and remove before/after slider - Add imageWrapperStyle prop to ImageViewer for live border preview - Add onImageStyle callback through tool-page to settings components - Change border displayMode to no-comparison (no slider) - BorderControls sends live CSS styles (border, padding, radius, shadow) - Preview updates instantly as user adjusts sliders or clicks presets * fix: repair i18n file corrupted by formatter during merge conflict resolution * feat(border): enable live CSS preview in right pane as settings change * fix(border): keep CSS preview visible after processing for WYSIWYG consistency * chore(gif-tools): scaffold for SOTA upgrade - Add animated GIF test fixture (3 frames, 100x100) - Update tool description to reflect new capabilities - Add fflate dependency to API for ZIP creation * feat(gif-tools): rewrite backend with 6 processing modes Modes: resize (with percentage), optimize (colors/dither/effort), speed (delay manipulation), reverse (frame reorder), extract (single/range/all with ZIP), rotate (90/180/270 + flip). Adds /api/v1/tools/gif-tools/info metadata endpoint. * test(gif-tools): add integration tests for all 6 modes Tests metadata endpoint, resize (pixel + percentage), optimize, speed, reverse, extract (single/range/all), and rotate (angle + flip). Fix animated.gif fixture to be a real 3-frame animation (was a single 100x300 frame). Fix reverse and rotate modes to process frames individually and reassemble via GIF binary concatenation, since Sharp 0.33.x loses page-height metadata when reconstructing from raw pixel data. * feat(gif-tools): rewrite frontend with tabbed 6-mode UI - useGifInfo hook for metadata (frame count, dimensions, duration) - Info bar showing GIF properties - 3x2 mode grid: Resize, Optimize, Speed, Reverse, Extract, Rotate - Animation modes disabled for static images - Loop control (infinite/once/custom) - Batch processing support * test(gif-tools): add to representative tools in e2e suite --------- Co-authored-by: Siddharth Kumar Sah <siddharth123sk@gmail.com>
243 lines
7.3 KiB
TypeScript
243 lines
7.3 KiB
TypeScript
import { basename } from "node:path";
|
|
import type { FastifyInstance } from "fastify";
|
|
import sharp from "sharp";
|
|
import { autoOrient } from "../../lib/auto-orient.js";
|
|
import { ensureSharpCompat } from "../../lib/heic-converter.js";
|
|
|
|
const DEFAULT_THRESHOLD = 8;
|
|
const THUMBNAIL_WIDTH = 200;
|
|
|
|
/**
|
|
* Compute a 128-bit dHash (row + column) for perceptual duplicate detection.
|
|
* Row hash: resize to 9x8 grayscale, compare adjacent horizontal pixels (64 bits).
|
|
* Column hash: resize to 8x9 grayscale, compare adjacent vertical pixels (64 bits).
|
|
*/
|
|
async function computeDHash128(buffer: Buffer): Promise<string> {
|
|
// Row hash: 9 wide x 8 tall
|
|
const rowPixels = await sharp(buffer).resize(9, 8, { fit: "fill" }).grayscale().raw().toBuffer();
|
|
let hash = "";
|
|
for (let y = 0; y < 8; y++) {
|
|
for (let x = 0; x < 8; x++) {
|
|
hash += rowPixels[y * 9 + x] > rowPixels[y * 9 + x + 1] ? "1" : "0";
|
|
}
|
|
}
|
|
|
|
// Column hash: 8 wide x 9 tall
|
|
const colPixels = await sharp(buffer).resize(8, 9, { fit: "fill" }).grayscale().raw().toBuffer();
|
|
for (let y = 0; y < 8; y++) {
|
|
for (let x = 0; x < 8; x++) {
|
|
hash += colPixels[y * 8 + x] > colPixels[(y + 1) * 8 + x] ? "1" : "0";
|
|
}
|
|
}
|
|
|
|
return hash; // 128 characters
|
|
}
|
|
|
|
function hammingDistance(a: string, b: string): number {
|
|
let distance = 0;
|
|
for (let i = 0; i < a.length; i++) {
|
|
if (a[i] !== b[i]) distance++;
|
|
}
|
|
return distance;
|
|
}
|
|
|
|
interface FileData {
|
|
buffer: Buffer;
|
|
filename: string;
|
|
originalSize: number;
|
|
}
|
|
|
|
interface FileInfo {
|
|
filename: string;
|
|
hash: string;
|
|
width: number;
|
|
height: number;
|
|
fileSize: number;
|
|
format: string;
|
|
thumbnail: string | null;
|
|
}
|
|
|
|
async function extractFileInfo(file: FileData): Promise<FileInfo> {
|
|
const meta = await sharp(file.buffer).metadata();
|
|
const width = meta.width ?? 0;
|
|
const height = meta.height ?? 0;
|
|
const format = meta.format ?? "unknown";
|
|
|
|
// Generate 200px wide JPEG thumbnail as base64
|
|
let thumbnail: string | null = null;
|
|
try {
|
|
const thumbBuffer = await sharp(file.buffer)
|
|
.resize(THUMBNAIL_WIDTH, undefined, { withoutEnlargement: true })
|
|
.jpeg({ quality: 70 })
|
|
.toBuffer();
|
|
thumbnail = `data:image/jpeg;base64,${thumbBuffer.toString("base64")}`;
|
|
} catch {
|
|
// Non-fatal: some formats may fail thumbnail generation
|
|
}
|
|
|
|
return {
|
|
filename: file.filename,
|
|
hash: "",
|
|
width,
|
|
height,
|
|
fileSize: file.originalSize,
|
|
format,
|
|
thumbnail,
|
|
};
|
|
}
|
|
|
|
export function registerFindDuplicates(app: FastifyInstance) {
|
|
app.post("/api/v1/tools/find-duplicates", async (request, reply) => {
|
|
const files: FileData[] = [];
|
|
let threshold = DEFAULT_THRESHOLD;
|
|
|
|
try {
|
|
const parts = request.parts();
|
|
for await (const part of parts) {
|
|
if (part.type === "file") {
|
|
const chunks: Buffer[] = [];
|
|
for await (const chunk of part.file) {
|
|
chunks.push(chunk);
|
|
}
|
|
const buf = Buffer.concat(chunks);
|
|
if (buf.length > 0) {
|
|
files.push({
|
|
buffer: buf,
|
|
filename: basename(part.filename ?? `image-${files.length}`),
|
|
originalSize: buf.length,
|
|
});
|
|
}
|
|
} else if (part.type === "field" && part.fieldname === "threshold") {
|
|
const val = Number(part.value);
|
|
if (!Number.isNaN(val) && val >= 0 && val <= 20) {
|
|
threshold = val;
|
|
}
|
|
}
|
|
}
|
|
} catch (err) {
|
|
return reply.status(400).send({
|
|
error: "Failed to parse multipart request",
|
|
details: err instanceof Error ? err.message : String(err),
|
|
});
|
|
}
|
|
|
|
if (files.length < 2) {
|
|
return reply
|
|
.status(400)
|
|
.send({ error: "At least 2 images are required for duplicate detection" });
|
|
}
|
|
|
|
try {
|
|
// Decode HEIC/HEIF if needed, then normalize EXIF orientation
|
|
for (const file of files) {
|
|
file.buffer = await autoOrient(await ensureSharpCompat(file.buffer));
|
|
}
|
|
|
|
// Extract metadata, thumbnails, and compute hashes
|
|
const fileInfos: FileInfo[] = [];
|
|
for (const file of files) {
|
|
const info = await extractFileInfo(file);
|
|
info.hash = await computeDHash128(file.buffer);
|
|
fileInfos.push(info);
|
|
}
|
|
|
|
// Group duplicates by hamming distance
|
|
const assigned = new Set<number>();
|
|
const groups: Array<{
|
|
groupId: number;
|
|
files: Array<{
|
|
filename: string;
|
|
similarity: number;
|
|
width: number;
|
|
height: number;
|
|
fileSize: number;
|
|
format: string;
|
|
isBest: boolean;
|
|
thumbnail: string | null;
|
|
}>;
|
|
}> = [];
|
|
|
|
let groupCounter = 0;
|
|
|
|
for (let i = 0; i < fileInfos.length; i++) {
|
|
if (assigned.has(i)) continue;
|
|
|
|
const members: Array<{ index: number; similarity: number }> = [
|
|
{ index: i, similarity: 100 },
|
|
];
|
|
|
|
for (let j = i + 1; j < fileInfos.length; j++) {
|
|
if (assigned.has(j)) continue;
|
|
const dist = hammingDistance(fileInfos[i].hash, fileInfos[j].hash);
|
|
if (dist <= threshold) {
|
|
const similarity = Math.round((1 - dist / 128) * 10000) / 100;
|
|
members.push({ index: j, similarity });
|
|
assigned.add(j);
|
|
}
|
|
}
|
|
|
|
if (members.length > 1) {
|
|
assigned.add(i);
|
|
groupCounter++;
|
|
|
|
// Determine "best" image: highest pixel count, tie-break by file size
|
|
let bestIdx = 0;
|
|
for (let m = 1; m < members.length; m++) {
|
|
const curr = fileInfos[members[m].index];
|
|
const best = fileInfos[members[bestIdx].index];
|
|
const currPixels = curr.width * curr.height;
|
|
const bestPixels = best.width * best.height;
|
|
if (
|
|
currPixels > bestPixels ||
|
|
(currPixels === bestPixels && curr.fileSize > best.fileSize)
|
|
) {
|
|
bestIdx = m;
|
|
}
|
|
}
|
|
|
|
groups.push({
|
|
groupId: groupCounter,
|
|
files: members.map((m, idx) => ({
|
|
filename: fileInfos[m.index].filename,
|
|
similarity: m.similarity,
|
|
width: fileInfos[m.index].width,
|
|
height: fileInfos[m.index].height,
|
|
fileSize: fileInfos[m.index].fileSize,
|
|
format: fileInfos[m.index].format,
|
|
isBest: idx === bestIdx,
|
|
thumbnail: fileInfos[m.index].thumbnail,
|
|
})),
|
|
});
|
|
}
|
|
}
|
|
|
|
// Sort groups by highest similarity descending
|
|
groups.sort((a, b) => {
|
|
const maxA = Math.max(...a.files.map((f) => f.similarity));
|
|
const maxB = Math.max(...b.files.map((f) => f.similarity));
|
|
return maxB - maxA;
|
|
});
|
|
|
|
// Calculate space saveable (sum of non-best duplicate file sizes)
|
|
let spaceSaveable = 0;
|
|
for (const group of groups) {
|
|
for (const file of group.files) {
|
|
if (!file.isBest) spaceSaveable += file.fileSize;
|
|
}
|
|
}
|
|
|
|
return reply.send({
|
|
totalImages: files.length,
|
|
duplicateGroups: groups,
|
|
uniqueImages: files.length - assigned.size,
|
|
spaceSaveable,
|
|
});
|
|
} catch (err) {
|
|
return reply.status(422).send({
|
|
error: "Duplicate detection failed",
|
|
details: err instanceof Error ? err.message : "Unknown error",
|
|
});
|
|
}
|
|
});
|
|
}
|