(buf: Buffer)
| 157 | * Returns a Uint8Array of width*height*4 bytes in RGBA order. |
| 158 | */ |
| 159 | export function decodePng(buf: Buffer): { width: number; height: number; data: Uint8Array } { |
| 160 | const { width, height, bitDepth, colorType, rawPixels } = decodePngRaw(buf, "decodePng"); |
| 161 | |
| 162 | if (bitDepth !== 8) { |
| 163 | throw new Error(`decodePng: unsupported bit depth ${bitDepth} (expected 8)`); |
| 164 | } |
| 165 | |
| 166 | const output = new Uint8Array(width * height * 4); |
| 167 | |
| 168 | if (colorType === 6) { |
| 169 | // RGBA — copy directly |
| 170 | output.set(rawPixels); |
| 171 | } else { |
| 172 | // RGB → RGBA: set alpha to 255 |
| 173 | for (let i = 0; i < width * height; i++) { |
| 174 | output[i * 4 + 0] = rawPixels[i * 3 + 0] ?? 0; |
| 175 | output[i * 4 + 1] = rawPixels[i * 3 + 1] ?? 0; |
| 176 | output[i * 4 + 2] = rawPixels[i * 3 + 2] ?? 0; |
| 177 | output[i * 4 + 3] = 255; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return { width, height, data: output }; |
| 182 | } |
| 183 | |
| 184 | // ── 16-bit PNG decoder ──────────────────────────────────────────────────────── |
| 185 |
no test coverage detected