mirror of
https://github.com/fsolla/teqo.git
synced 2026-12-03 22:13:06 +00:00
116 lines
3.6 KiB
TypeScript
116 lines
3.6 KiB
TypeScript
'use client'
|
|
|
|
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
|
|
import { removeCardPhotoBackground } from '@/components/cards/cardCutout'
|
|
import { harmonizeCardCutout } from '@/components/cards/cardPhotoHarmonyCanvas'
|
|
import { TEAM_CARD_FACE_REFERENCE_SIZE, type CardRect } from '@/lib/cardModels'
|
|
import {
|
|
frameCardPhotoOnFace,
|
|
type CardAlphaBbox,
|
|
type CardFaceBox,
|
|
type CardPhotoTransform,
|
|
} from '@/lib/cardPhotoTransform'
|
|
|
|
export type CardCutoutState =
|
|
| { status: 'idle' }
|
|
| { status: 'processing'; phase: 'download' | 'process'; ratio: number }
|
|
| {
|
|
status: 'ready'
|
|
canvas: HTMLCanvasElement
|
|
/**
|
|
* S17 — tone-harmonized variant of `canvas` (same size/alpha). Equals
|
|
* `canvas` when the photo is already on tone; `null` when the reference
|
|
* could not be read (the composer then hides the control).
|
|
*/
|
|
harmonized: HTMLCanvasElement | null
|
|
width: number
|
|
height: number
|
|
/** Initial framing: cutout top on the slot top, face centered (S18). */
|
|
transform: CardPhotoTransform
|
|
/** S20 — silhouette and detected face, kept for the fine-tuning anchor. */
|
|
bbox: CardAlphaBbox
|
|
face: CardFaceBox | null
|
|
}
|
|
| { status: 'error'; reason: 'engine' | 'empty' }
|
|
|
|
/**
|
|
* S15 — owns the cutout lifecycle for the team model: engine import, progress,
|
|
* degenerate framing (fail-closed), retry with the same file and run-id guards
|
|
* so a stale inference or an unmount never writes state. The engine itself is a
|
|
* page-lifetime singleton in the adapter (re-creating the wasm session per open
|
|
* would cost seconds); each result's masks/bitmap are released there.
|
|
*
|
|
* S17 — the ready state also carries the harmonized variant, computed once
|
|
* before the first ready paint (default ON must not flash the raw photo).
|
|
*/
|
|
export const useCardCutout = (photoWindow: CardRect | undefined, harmonyReferenceSrc?: string) => {
|
|
const [state, setState] = useState<CardCutoutState>({ status: 'idle' })
|
|
const fileRef = useRef<File | null>(null)
|
|
const runRef = useRef(0)
|
|
|
|
useEffect(
|
|
() => () => {
|
|
runRef.current += 1
|
|
},
|
|
[],
|
|
)
|
|
|
|
const start = useCallback(
|
|
async (file: File) => {
|
|
runRef.current += 1
|
|
const run = runRef.current
|
|
fileRef.current = file
|
|
setState({ status: 'processing', phase: 'download', ratio: 0 })
|
|
|
|
const result = await removeCardPhotoBackground(file, (progress) => {
|
|
if (runRef.current !== run) return
|
|
setState({ status: 'processing', phase: progress.phase, ratio: progress.ratio })
|
|
})
|
|
if (runRef.current !== run) return
|
|
|
|
if (!result.ok) {
|
|
setState({ status: 'error', reason: result.reason })
|
|
return
|
|
}
|
|
|
|
const transform = photoWindow
|
|
? frameCardPhotoOnFace(
|
|
{ width: result.width, height: result.height },
|
|
photoWindow,
|
|
result.bbox,
|
|
result.face,
|
|
TEAM_CARD_FACE_REFERENCE_SIZE,
|
|
)
|
|
: null
|
|
if (!transform) {
|
|
setState({ status: 'error', reason: 'empty' })
|
|
return
|
|
}
|
|
|
|
const harmonized = harmonyReferenceSrc
|
|
? await harmonizeCardCutout(result.canvas, harmonyReferenceSrc)
|
|
: null
|
|
if (runRef.current !== run) return
|
|
|
|
setState({
|
|
status: 'ready',
|
|
canvas: result.canvas,
|
|
harmonized,
|
|
width: result.width,
|
|
height: result.height,
|
|
transform,
|
|
bbox: result.bbox,
|
|
face: result.face,
|
|
})
|
|
},
|
|
[photoWindow, harmonyReferenceSrc],
|
|
)
|
|
|
|
const retry = useCallback(() => {
|
|
const file = fileRef.current
|
|
if (file) void start(file)
|
|
}, [start])
|
|
|
|
return { state, start, retry }
|
|
}
|