/* * 扫描 assets/images,重算每张图的尺寸与 alpha 有效区域, * 并回写 script.js 中的 photos 数组。 * 新增/删除图片后运行:node update-photos.js(或直接双击 update-photos.bat) */ const fs = require("fs"); const path = require("path"); const zlib = require("zlib"); const imageDir = path.join(__dirname, "assets", "images"); const scriptPath = path.join(__dirname, "script.js"); const extensions = new Set([".png", ".jpg", ".jpeg"]); function readPngMeta(buf) { let pos = 8; let idat = []; let w, h, depth, ctype, interlace; while (pos < buf.length) { const len = buf.readUInt32BE(pos); const type = buf.toString("ascii", pos + 4, pos + 8); const chunk = buf.subarray(pos + 8, pos + 8 + len); pos += 12 + len; if (type === "IHDR") { w = chunk.readUInt32BE(0); h = chunk.readUInt32BE(4); depth = chunk[8]; ctype = chunk[9]; interlace = chunk[12]; } else if (type === "IDAT") { idat.push(chunk); } else if (type === "IEND") { break; } } const full = { x: 0, y: 0, w, h }; if (depth !== 8 || interlace !== 0 || (ctype !== 6 && ctype !== 2)) { return { w, h, bounds: full }; } const bpp = ctype === 6 ? 4 : 3; const alphaAt = ctype === 6 ? 3 : -1; const raw = zlib.inflateSync(Buffer.concat(idat)); const stride = w * bpp; const rows = []; let off = 0; let prev = Buffer.alloc(stride); for (let y = 0; y < h; y++) { const ft = raw[off++]; const line = Buffer.from(raw.subarray(off, off + stride)); off += stride; for (let x = 0; x < stride; x++) { const a = x >= bpp ? line[x - bpp] : 0; const b = prev[x]; const c = x >= bpp ? prev[x - bpp] : 0; let v = 0; if (ft === 1) v = a; else if (ft === 2) v = b; else if (ft === 3) v = (a + b) >> 1; else if (ft === 4) { const p = a + b - c; const pa = Math.abs(p - a); const pb = Math.abs(p - b); const pc = Math.abs(p - c); v = pa <= pb && pa <= pc ? a : pb <= pc ? b : c; } line[x] = (line[x] + v) & 0xff; } rows.push(line); prev = line; } if (alphaAt < 0) return { w, h, bounds: full }; let minX = w; let minY = h; let maxX = -1; let maxY = -1; for (let y = 0; y < h; y++) { const row = rows[y]; for (let x = 0; x < w; x++) { if (row[x * bpp + alphaAt] > 8) { if (x < minX) minX = x; if (x > maxX) maxX = x; if (y < minY) minY = y; if (y > maxY) maxY = y; } } } if (maxX < 0) return { w, h, bounds: full }; return { w, h, bounds: { x: minX, y: minY, w: maxX - minX + 1, h: maxY - minY + 1 }, }; } function readJpegSize(buf) { let i = 2; while (i < buf.length - 9) { if (buf[i] !== 0xff) { i++; continue; } const marker = buf[i + 1]; if (marker >= 0xc0 && marker <= 0xcf && ![0xc4, 0xc8, 0xcc].includes(marker)) { return { w: buf.readUInt16BE(i + 7), h: buf.readUInt16BE(i + 5) }; } i += 2 + buf.readUInt16BE(i + 2); } return null; } const items = fs .readdirSync(imageDir) .filter((name) => extensions.has(path.extname(name).toLowerCase())) .sort() .map((name) => { const buf = fs.readFileSync(path.join(imageDir, name)); const meta = path.extname(name).toLowerCase() === ".png" ? readPngMeta(buf) : (() => { const size = readJpegSize(buf) || { w: 0, h: 0 }; return { w: size.w, h: size.h, bounds: { x: 0, y: 0, w: size.w, h: size.h } }; })(); return { src: `assets/images/${name}`, width: meta.w, height: meta.h, bounds: meta.bounds, }; }) .filter((it) => it.width && it.height); const lines = items.map( (it) => [ " {", ` src: ${JSON.stringify(it.src)},`, ` width: ${it.width},`, ` height: ${it.height},`, ` bounds: { x: ${it.bounds.x}, y: ${it.bounds.y}, w: ${it.bounds.w}, h: ${it.bounds.h} },`, " },", ].join("\n") ); const script = fs.readFileSync(scriptPath, "utf8"); const eol = script.includes("\r\n") ? "\r\n" : "\n"; const block = (" var photos = [\n" + lines.join("\n") + "\n ];\n").replace(/\n/g, eol); const start = script.indexOf(" var photos = ["); const end = script.indexOf(" /* PHOTOS_END */"); if (start < 0 || end < 0) { console.error("script.js 中未找到 photos 数组标记"); process.exit(1); } fs.writeFileSync(scriptPath, script.slice(0, start) + block + script.slice(end), "utf8"); console.log(`script.js updated: ${items.length} images`);