chore(image-vectorize): 存档未发布插件(描摹质量不达标,已下线)
用户实测描摹效果达不到可交付线(中文笔画断裂 / 抗锯齿边缘毛刺 / 浅色块碎层), 裁定不做这个插件。线上已清理:KV 清单移除、字节 GC 删除、宿主 `wasm-unsafe-eval` 收回。 代码本身是好的(VTracer wasm 跑通、5 预设 + 全参数面板 + 空格对比预览 + SVG/PNG 双输出、 本地 harness 27 项 + 真站 e2e 13 项全绿),问题在算法定位:VTracer 适合 logo / 线稿 / 纯平色块,不适合照片与带抗锯齿的合成图。留档备查,README 顶部已标注「已下线,未发布」。
This commit is contained in:
1 parent
3080916c62
commit
6f3a4676bb
16 files changed
+4033
No files matched your search
@@ -0,0 +1,102 @@
|
||||
/* VTracer 引擎封装。
|
||||
*
|
||||
* 为什么这么薄:vtracer-wasm 只有 `to_svg(pixels, w, h, config)` 一个入口,
|
||||
* 参数语义在实测中与官方 CLI 有明显偏差(见 CONFIG_LIMITS),所以把它全部收在这里,
|
||||
* 上层(ui / trace)只跟「业务参数」打交道。
|
||||
*
|
||||
* 实测得出的三条硬约束(2026-11-05,Chromium + 800×800 合成图扫描):
|
||||
* 1. colorPrecision 的语义与 vtracer CLI 相反:wasm 侧直接把它当「颜色相似度阈值」
|
||||
* (is_same_color_a),值越大越容易把不同颜色并成一团。≥6 时整张图会塌成一个色块,
|
||||
* =8 直接 wasm abort(unreachable)。故夹在 1–5。
|
||||
* 2. wasm abort 后实例不可再用,且 __wbg_init 见到已有实例会直接返回坏的,
|
||||
* 所以 panic 后必须 __reset_wasm() 再重建,否则整个插件从此报错。
|
||||
* 3. 像素必须是 RGBA 四通道(ImageData.data 原样),长度 w*h*4。
|
||||
*/
|
||||
'use strict'
|
||||
|
||||
import init, { to_svg, __reset_wasm } from './vendor/vtracer.js'
|
||||
import wasmB64 from './vendor/wasm-b64.js'
|
||||
|
||||
// colorPrecision 安全区间(见文件头第 1 条)
|
||||
export const COLOR_PRECISION_MIN = 1
|
||||
export const COLOR_PRECISION_MAX = 5
|
||||
|
||||
let wasmBytes = null
|
||||
let initPromise = null
|
||||
|
||||
function decodeWasm() {
|
||||
if (wasmBytes) return wasmBytes
|
||||
// 182KB base64 → 137KB 字节;只在首次描摹时解码,避免拖慢插件加载
|
||||
const bin = atob(wasmB64)
|
||||
const out = new Uint8Array(bin.length)
|
||||
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i)
|
||||
wasmBytes = out
|
||||
return out
|
||||
}
|
||||
|
||||
function ensureEngine() {
|
||||
if (!initPromise) {
|
||||
initPromise = init({ module_or_path: decodeWasm() }).catch(function (error) {
|
||||
initPromise = null
|
||||
throw new Error('描摹引擎初始化失败:' + (error && error.message ? error.message : String(error)))
|
||||
})
|
||||
}
|
||||
return initPromise
|
||||
}
|
||||
|
||||
function clampInt(value, min, max, fallback) {
|
||||
const n = Math.round(Number(value))
|
||||
if (!Number.isFinite(n)) return fallback
|
||||
return Math.max(min, Math.min(max, n))
|
||||
}
|
||||
|
||||
/** 业务参数 → wasm config(字段名 camelCase,枚举小写,与结构体字段一一对应) */
|
||||
export function toEngineConfig(p) {
|
||||
return {
|
||||
binary: Boolean(p.binary),
|
||||
mode: p.mode === 'polygon' ? 'polygon' : 'spline',
|
||||
hierarchical: p.hierarchical === 'cutout' ? 'cutout' : 'stacked',
|
||||
cornerThreshold: Math.max(0, Math.min(180, Number(p.cornerThreshold) || 60)),
|
||||
lengthThreshold: Math.max(0.1, Math.min(10, Number(p.lengthThreshold) || 3.5)),
|
||||
// maxIterations 是曲线拟合的迭代次数。上游 CLI 默认 10,但实测只要 ≥3 就会过拟合抗锯齿边缘:
|
||||
// 一个正圆被拉成水滴形、圆角矩形的角被吃掉。2 是实测最稳的值(圆保持圆、直角保持直)。
|
||||
maxIterations: clampInt(p.maxIterations, 1, 10, 2),
|
||||
spliceThreshold: 45,
|
||||
filterSpeckle: clampInt(p.filterSpeckle, 0, 128, 4),
|
||||
// 黑白模式没有颜色聚类,这两个字段只是结构体必填,给安全值即可
|
||||
colorPrecision: clampInt(p.colorPrecision, COLOR_PRECISION_MIN, COLOR_PRECISION_MAX, 2),
|
||||
layerDifference: clampInt(p.layerDifference, 0, 64, 16),
|
||||
pathPrecision: 8,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 描摹一次。
|
||||
* @param {Uint8ClampedArray|Uint8Array} rgba 长度必须等于 w*h*4
|
||||
* @returns {Promise<{svg: string, ms: number}>}
|
||||
*/
|
||||
export async function tracePixels(rgba, width, height, params) {
|
||||
const w = Math.round(width)
|
||||
const h = Math.round(height)
|
||||
if (!(w > 0) || !(h > 0)) throw new Error('图片尺寸无效')
|
||||
if (!rgba || rgba.length !== w * h * 4) throw new Error('像素数据长度与尺寸不符')
|
||||
|
||||
await ensureEngine()
|
||||
const config = toEngineConfig(params)
|
||||
const pixels = rgba instanceof Uint8Array ? rgba : new Uint8Array(rgba.buffer, rgba.byteOffset, rgba.length)
|
||||
const started = (typeof performance !== 'undefined' ? performance.now() : Date.now())
|
||||
|
||||
try {
|
||||
const svg = to_svg(pixels, w, h, config)
|
||||
const ms = Math.round((typeof performance !== 'undefined' ? performance.now() : Date.now()) - started)
|
||||
return { svg, ms }
|
||||
} catch (error) {
|
||||
// wasm abort 后实例已损坏,必须复位,否则下一次调用还是同一个坏实例
|
||||
initPromise = null
|
||||
try { __reset_wasm() } catch (e) { /* 复位本身失败也只能继续抛原始错误 */ }
|
||||
throw new Error(
|
||||
'描摹失败(引擎内部异常):' + (error && error.message ? error.message : String(error)) +
|
||||
'。请调大「颜色精度」的容错或换「黑白」模式重试'
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user