仰望星辰工作室

starmusic-mobile

starmusic-mobile/ scripts/gen-splash-images.js 7.1 KB · 214 行 原始文件
1// 生成 App 原生启动图(纯色背景 + 居中应用图标)
2// 运行:node scripts/gen-splash-images.js
3// 输出:static/splash/{hdpi,xhdpi,xxhdpi}.png
4//
5// 说明:uni-app 的原生启动图是 Android 启动主题的窗口背景,会一直显示到 WebView 渲染出首帧,
6// 因此这里做成「纯白底 + 居中图标」,与启动页(pages/splash)的浅色视觉自然衔接。
7const fs = require('fs')
8const path = require('path')
9const zlib = require('zlib')
10
11// ---------- PNG 编解码(仅支持 8bit RGBA,与应用图标格式一致) ----------
12const crcTable = (() => {
13 const table = new Int32Array(256)
14 for (let n = 0; n < 256; n++) {
15 let c = n
16 for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
17 table[n] = c
18 }
19 return table
20})()
21
22const crc32 = (buf) => {
23 let c = 0xffffffff
24 for (let i = 0; i < buf.length; i++) c = crcTable[(c ^ buf[i]) & 0xff] ^ (c >>> 8)
25 return (c ^ 0xffffffff) >>> 0
26}
27
28const chunk = (type, data) => {
29 const len = Buffer.alloc(4)
30 len.writeUInt32BE(data.length)
31 const typeBuf = Buffer.from(type, 'ascii')
32 const crcBuf = Buffer.alloc(4)
33 crcBuf.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])))
34 return Buffer.concat([len, typeBuf, data, crcBuf])
35}
36
37const decodePng = (buf) => {
38 let pos = 8
39 let width = 0
40 let height = 0
41 let colorType = 6
42 const idat = []
43 while (pos < buf.length) {
44 const len = buf.readUInt32BE(pos)
45 const type = buf.toString('ascii', pos + 4, pos + 8)
46 const data = buf.subarray(pos + 8, pos + 8 + len)
47 if (type === 'IHDR') {
48 width = data.readUInt32BE(0)
49 height = data.readUInt32BE(4)
50 colorType = data[9]
51 } else if (type === 'IDAT') {
52 idat.push(data)
53 } else if (type === 'IEND') {
54 break
55 }
56 pos += 12 + len
57 }
58 if (colorType !== 6) throw new Error('仅支持 8bit RGBA 的 PNG(colorType=6),当前为 ' + colorType)
59
60 const raw = zlib.inflateSync(Buffer.concat(idat))
61 const bpp = 4
62 const stride = width * bpp
63 const out = Buffer.alloc(stride * height)
64 let rp = 0
65 for (let y = 0; y < height; y++) {
66 const filter = raw[rp++]
67 const line = raw.subarray(rp, rp + stride)
68 rp += stride
69 const cur = out.subarray(y * stride, (y + 1) * stride)
70 const prev = y > 0 ? out.subarray((y - 1) * stride, y * stride) : null
71 for (let x = 0; x < stride; x++) {
72 const a = x >= bpp ? cur[x - bpp] : 0
73 const b = prev ? prev[x] : 0
74 const c = prev && x >= bpp ? prev[x - bpp] : 0
75 let v = line[x]
76 switch (filter) {
77 case 1:
78 v += a
79 break
80 case 2:
81 v += b
82 break
83 case 3:
84 v += (a + b) >> 1
85 break
86 case 4: {
87 const p = a + b - c
88 const pa = Math.abs(p - a)
89 const pb = Math.abs(p - b)
90 const pc = Math.abs(p - c)
91 v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c
92 break
93 }
94 }
95 cur[x] = v & 0xff
96 }
97 }
98 return { width, height, pixels: out }
99}
100
101const encodePng = (width, height, pixels) => {
102 const stride = width * 4
103 const raw = Buffer.alloc((stride + 1) * height)
104 for (let y = 0; y < height; y++) {
105 raw[y * (stride + 1)] = 0 // filter: None
106 pixels.copy(raw, y * (stride + 1) + 1, y * stride, (y + 1) * stride)
107 }
108 const ihdr = Buffer.alloc(13)
109 ihdr.writeUInt32BE(width, 0)
110 ihdr.writeUInt32BE(height, 4)
111 ihdr[8] = 8 // bit depth
112 ihdr[9] = 6 // color type: RGBA
113 return Buffer.concat([
114 Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
115 chunk('IHDR', ihdr),
116 chunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
117 chunk('IEND', Buffer.alloc(0)),
118 ])
119}
120
121// ---------- 双线性缩放 ----------
122const scaleRgba = (src, sw, sh, dw, dh) => {
123 const out = Buffer.alloc(dw * dh * 4)
124 for (let y = 0; y < dh; y++) {
125 const sy = ((y + 0.5) * sh) / dh - 0.5
126 const y0 = Math.max(0, Math.floor(sy))
127 const y1 = Math.min(sh - 1, y0 + 1)
128 const wy = Math.min(1, Math.max(0, sy - y0))
129 for (let x = 0; x < dw; x++) {
130 const sx = ((x + 0.5) * sw) / dw - 0.5
131 const x0 = Math.max(0, Math.floor(sx))
132 const x1 = Math.min(sw - 1, x0 + 1)
133 const wx = Math.min(1, Math.max(0, sx - x0))
134 const i00 = (y0 * sw + x0) * 4
135 const i01 = (y0 * sw + x1) * 4
136 const i10 = (y1 * sw + x0) * 4
137 const i11 = (y1 * sw + x1) * 4
138 const o = (y * dw + x) * 4
139 for (let c = 0; c < 4; c++) {
140 const top = src[i00 + c] * (1 - wx) + src[i01 + c] * wx
141 const bottom = src[i10 + c] * (1 - wx) + src[i11 + c] * wx
142 out[o + c] = Math.round(top * (1 - wy) + bottom * wy)
143 }
144 }
145 }
146 return out
147}
148
149// ---------- 合成:纯色画布 + 居中贴图(source-over 混合) ----------
150const compose = (canvasW, canvasH, bg, logo, logoW, logoH) => {
151 const canvas = Buffer.alloc(canvasW * canvasH * 4)
152 for (let i = 0; i < canvas.length; i += 4) {
153 canvas[i] = bg[0]
154 canvas[i + 1] = bg[1]
155 canvas[i + 2] = bg[2]
156 canvas[i + 3] = 255
157 }
158 const offsetX = Math.round((canvasW - logoW) / 2)
159 // 略高于几何中心(46%),视觉重心更舒服
160 const offsetY = Math.round(canvasH * 0.46 - logoH / 2)
161 for (let y = 0; y < logoH; y++) {
162 const cy = offsetY + y
163 if (cy < 0 || cy >= canvasH) continue
164 for (let x = 0; x < logoW; x++) {
165 const cx = offsetX + x
166 if (cx < 0 || cx >= canvasW) continue
167 const si = (y * logoW + x) * 4
168 const a = logo[si + 3] / 255
169 if (a === 0) continue
170 const di = (cy * canvasW + cx) * 4
171 canvas[di] = Math.round(logo[si] * a + canvas[di] * (1 - a))
172 canvas[di + 1] = Math.round(logo[si + 1] * a + canvas[di + 1] * (1 - a))
173 canvas[di + 2] = Math.round(logo[si + 2] * a + canvas[di + 2] * (1 - a))
174 }
175 }
176 return canvas
177}
178
179// ---------- 生成 ----------
180/** 纯白背景:与启动页 splash 的浅色渐变衔接,也最简洁 */
181const BG = [255, 255, 255]
182const LOGO_SOURCE = path.join(__dirname, '..', 'static', 'icon', 'xxxhdpi.png')
183const TARGETS = [
184 { file: 'hdpi.png', w: 480, h: 762, logo: 128 },
185 { file: 'xhdpi.png', w: 720, h: 1242, logo: 192 },
186 { file: 'xxhdpi.png', w: 1080, h: 1882, logo: 288 },
187]
188
189/**
190 * 应用图标自带浅米色底,直接贴到纯白启动图上会露出方形色块;
191 * 这里把接近底色的浅色像素归一到纯白,使图标与背景自然融合。
192 */
193const whitenNearWhite = (pixels) => {
194 for (let i = 0; i < pixels.length; i += 4) {
195 if (pixels[i] >= 240 && pixels[i + 1] >= 240 && pixels[i + 2] >= 232) {
196 pixels[i] = 255
197 pixels[i + 1] = 255
198 pixels[i + 2] = 255
199 }
200 }
201 return pixels
202}
203
204const srcIcon = decodePng(fs.readFileSync(LOGO_SOURCE))
205const outDir = path.join(__dirname, '..', 'static', 'splash')
206fs.mkdirSync(outDir, { recursive: true })
207
208for (const t of TARGETS) {
209 const logo = whitenNearWhite(scaleRgba(srcIcon.pixels, srcIcon.width, srcIcon.height, t.logo, t.logo))
210 const pixels = compose(t.w, t.h, BG, logo, t.logo, t.logo)
211 const out = path.join(outDir, t.file)
212 fs.writeFileSync(out, encodePng(t.w, t.h, pixels))
213 console.log(`generated ${t.file} ${t.w}x${t.h}(Logo ${t.logo}px,${(fs.statSync(out).size / 1024).toFixed(1)}KB)`)
214}