| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- // 一次性探针:采样白母版的逐行轮廓(左右缘 x),用于拟合自绘 SVG 水滴路径。
- // 用法:node probe-contour.mjs <png>
- import { readFileSync } from 'node:fs'
- import { inflateSync } from 'node:zlib'
- function decodePng(path) {
- const buf = readFileSync(path)
- let pos = 8
- const idat = []
- let width = 0, height = 0, bitDepth = 0, colorType = 0, palette = null, trns = null
- while (pos < buf.length) {
- const len = buf.readUInt32BE(pos)
- const type = buf.toString('ascii', pos + 4, pos + 8)
- const data = buf.subarray(pos + 8, pos + 8 + len)
- if (type === 'IHDR') {
- width = data.readUInt32BE(0); height = data.readUInt32BE(4)
- bitDepth = data[8]; colorType = data[9]
- } else if (type === 'PLTE') palette = Buffer.from(data)
- else if (type === 'tRNS') trns = Buffer.from(data)
- else if (type === 'IDAT') idat.push(data)
- else if (type === 'IEND') break
- pos += 12 + len
- }
- const channels = colorType === 6 ? 4 : colorType === 2 ? 3 : 1
- const stride = width * channels
- const raw = inflateSync(Buffer.concat(idat))
- const img = Buffer.alloc(height * stride)
- for (let y = 0; y < height; y++) {
- const filter = raw[y * (stride + 1)]
- const row = raw.subarray(y * (stride + 1) + 1, (y + 1) * stride)
- const prev = y > 0 ? img.subarray((y - 1) * stride, y * stride) : null
- const out = img.subarray(y * stride, (y + 1) * stride)
- for (let x = 0; x < stride; x++) {
- const a = x >= channels ? out[x - channels] : 0
- const b = prev ? prev[x] : 0
- const c = prev && x >= channels ? prev[x - channels] : 0
- let v = row[x]
- if (filter === 1) v += a
- else if (filter === 2) v += b
- else if (filter === 3) v += (a + b) >> 1
- else if (filter === 4) {
- const p = a + b - c
- const pa = Math.abs(p - a), pb = Math.abs(p - b), pc = Math.abs(p - c)
- v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c
- }
- out[x] = v & 0xff
- }
- }
- const alphaAt = (x, y) => {
- if (colorType === 6) return img[y * stride + x * 4 + 3]
- if (colorType === 2) return 255
- const idx = img[y * stride + x]
- if (trns && idx < trns.length) return trns[idx]
- return 255
- }
- return { width, height, alphaAt }
- }
- const file = process.argv[2] || 'public/cockpit/assets/icons/T_MarkPoint_WhiteFull.png'
- const { width, height, alphaAt } = decodePng(file)
- const MINX = 67, MAXX = 444, MINY = 19, MAXY = 493
- const BW = MAXX - MINX + 1, BH = MAXY - MINY + 1
- console.log(`== ${file} 逐行轮廓(bbox 相对坐标,宽 = 左右缘跨度 / bbox 宽)==`)
- for (let i = 0; i <= 20; i++) {
- const t = i / 20 // 0=顶 1=底
- const y = Math.round(MINY + t * (MAXY - MINY))
- let lo = -1, hi = -1
- for (let x = MINX; x <= MAXX; x++) {
- if (alphaAt(x, y) > 8) { if (lo < 0) lo = x; hi = x }
- }
- if (lo < 0) { console.log(`t=${t.toFixed(2)} y=${y} (空)`); continue }
- const lx = ((lo - MINX) / BW).toFixed(3)
- const rx = ((hi - MINX) / BW).toFixed(3)
- const w = ((hi - lo + 1) / BW).toFixed(3)
- console.log(`t=${t.toFixed(2)} y=${y} 左${lx} 右${rx} 宽${w}`)
- }
- // 圆帽拟合验证:找最宽行与其 y,算帽心/半径(bbox 相对)
- let widest = 0, widestY = MINY
- for (let y = MINY; y <= MAXY; y++) {
- let lo = -1, hi = -1
- for (let x = MINX; x <= MAXX; x++) if (alphaAt(x, y) > 8) { if (lo < 0) lo = x; hi = x }
- if (lo >= 0 && hi - lo + 1 > widest) { widest = hi - lo + 1; widestY = y }
- }
- console.log(`\n最宽行 y=${widestY}(相对 ${(widestY - MINY) / BH})宽 ${widest} = bbox 宽 ${(widest / BW).toFixed(3)}`)
|