Initial commit: Luminary photo catalog and RAW editor
Browser-based photo catalog with an optional team server (SQLite + LibRaw). Local database, uploaded photos, build output and dependencies are gitignored. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import type { RasterMask } from '../types';
|
||||
|
||||
export type AutoMaskKind = 'subject' | 'sky' | 'background';
|
||||
|
||||
/**
|
||||
* On-device mask estimation for the three selections that matter most in an
|
||||
* event edit: the subject, the sky and the background around the subject.
|
||||
*
|
||||
* The estimator works from a downsampled preview. It combines distance from
|
||||
* the border colour (the usual studio/event background) with a centre bias,
|
||||
* saturation and a vertical sky prior. This is intentionally lightweight: it
|
||||
* has no model download and produces a low-resolution raster that the normal
|
||||
* mask pipeline feathers and scales. The output is editable like any brush
|
||||
* mask, so a missed edge can be fixed without another model pass.
|
||||
*/
|
||||
export async function buildAutoMask(bitmap: ImageBitmap, kind: AutoMaskKind): Promise<RasterMask> {
|
||||
const width = Math.max(128, Math.min(320, Math.round(bitmap.width / 16)));
|
||||
const height = Math.max(48, Math.round((width * bitmap.height) / Math.max(1, bitmap.width)));
|
||||
const canvas = document.createElement('canvas');
|
||||
canvas.width = width;
|
||||
canvas.height = height;
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) throw new Error('Could not analyse the image for a mask');
|
||||
ctx.drawImage(bitmap, 0, 0, width, height);
|
||||
const image = ctx.getImageData(0, 0, width, height);
|
||||
const data = image.data;
|
||||
const count = width * height;
|
||||
|
||||
const subject = new Float32Array(count);
|
||||
const sky = new Float32Array(count);
|
||||
const border = borderColor(data, width, height);
|
||||
|
||||
for (let y = 0; y < height; y++) {
|
||||
const ny = y / Math.max(1, height - 1);
|
||||
for (let x = 0; x < width; x++) {
|
||||
const i = y * width + x;
|
||||
const o = i * 4;
|
||||
const r = data[o] / 255;
|
||||
const g = data[o + 1] / 255;
|
||||
const b = data[o + 2] / 255;
|
||||
const dist = Math.min(1, Math.sqrt((r - border[0]) ** 2 + (g - border[1]) ** 2 + (b - border[2]) ** 2) / 1.35);
|
||||
const center = 1 - Math.min(1, Math.sqrt(((x / width - 0.5) * 1.7) ** 2 + ((y / height - 0.5) * 1.35) ** 2));
|
||||
const maxc = Math.max(r, g, b);
|
||||
const minc = Math.min(r, g, b);
|
||||
const sat = maxc > 0.001 ? (maxc - minc) / maxc : 0;
|
||||
const luma = 0.2126 * r + 0.7152 * g + 0.0722 * b;
|
||||
subject[i] = dist * 0.58 + center * 0.27 + sat * 0.1 + (1 - Math.abs(luma - 0.48)) * 0.05;
|
||||
const blue = Math.max(0, b - (r + g) * 0.5);
|
||||
sky[i] = blue * 2.5 + Math.max(0, luma - 0.4) * 0.5 + (1 - ny) * 0.28 - (1 - sat) * 0.08;
|
||||
}
|
||||
}
|
||||
|
||||
const raw = kind === 'sky' ? sky : subject;
|
||||
const sorted = [...raw].sort((a, b) => a - b);
|
||||
const median = sorted[Math.floor(sorted.length * 0.5)] ?? 0;
|
||||
const high = sorted[Math.floor(sorted.length * 0.82)] ?? 1;
|
||||
const threshold = kind === 'sky' ? Math.max(0.35, median) : Math.max(0.36, median + (high - median) * 0.22);
|
||||
const mask = new Uint8Array(count);
|
||||
for (let i = 0; i < count; i++) {
|
||||
const on = raw[i] >= threshold;
|
||||
mask[i] = (kind === 'background' ? !on : on) ? 255 : 0;
|
||||
}
|
||||
smooth(mask, width, height);
|
||||
smooth(mask, width, height);
|
||||
|
||||
return { data: encodeBase64(mask), width, height };
|
||||
}
|
||||
|
||||
function borderColor(data: Uint8ClampedArray, width: number, height: number): [number, number, number] {
|
||||
let r = 0;
|
||||
let g = 0;
|
||||
let b = 0;
|
||||
let n = 0;
|
||||
const add = (x: number, y: number) => {
|
||||
const o = (y * width + x) * 4;
|
||||
r += data[o];
|
||||
g += data[o + 1];
|
||||
b += data[o + 2];
|
||||
n++;
|
||||
};
|
||||
for (let x = 0; x < width; x++) {
|
||||
add(x, 0);
|
||||
add(x, height - 1);
|
||||
}
|
||||
for (let y = 1; y < height - 1; y++) {
|
||||
add(0, y);
|
||||
add(width - 1, y);
|
||||
}
|
||||
return [r / n / 255, g / n / 255, b / n / 255];
|
||||
}
|
||||
|
||||
function smooth(mask: Uint8Array, width: number, height: number) {
|
||||
const copy = new Uint8Array(mask);
|
||||
for (let y = 1; y < height - 1; y++) {
|
||||
for (let x = 1; x < width - 1; x++) {
|
||||
const i = y * width + x;
|
||||
let sum = 0;
|
||||
for (let dy = -1; dy <= 1; dy++) {
|
||||
for (let dx = -1; dx <= 1; dx++) sum += copy[i + dy * width + dx];
|
||||
}
|
||||
mask[i] = sum / 9 >= 128 ? 255 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function encodeBase64(bytes: Uint8Array): string {
|
||||
let binary = '';
|
||||
const chunk = 0x8000;
|
||||
for (let i = 0; i < bytes.length; i += chunk) {
|
||||
binary += String.fromCharCode(...bytes.subarray(i, i + chunk));
|
||||
}
|
||||
return btoa(binary);
|
||||
}
|
||||
Reference in New Issue
Block a user