Line data Source code
1 : import type { Size2D } from '@comfyorg/shared-frontend-utils/geometry'
2 :
3 : interface RgbaImage extends Readonly<Size2D> {
4 : readonly data: Uint8ClampedArray
5 : }
6 :
7 30 : const PNG_SIGNATURE = new Uint8Array([
8 : 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a
9 : ])
10 :
11 30 : const CRC_TABLE = new Uint32Array(256)
12 30 : for (let n = 0; n < 256; n++) {
13 7680 : let c = n
14 7680 : for (let k = 0; k < 8; k++) {
15 61440 : c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
16 : }
17 7680 : CRC_TABLE[n] = c
18 : }
19 :
20 48 : function crc32(...parts: Uint8Array[]): number {
21 48 : let crc = 0xffffffff
22 48 : for (const part of parts) {
23 96 : for (const byte of part) {
24 203948 : crc = CRC_TABLE[(crc ^ byte) & 0xff] ^ (crc >>> 8)
25 : }
26 : }
27 48 : return (crc ^ 0xffffffff) >>> 0
28 : }
29 :
30 128 : function u32be(value: number): Uint8Array<ArrayBuffer> {
31 128 : return new Uint8Array([
32 : (value >>> 24) & 0xff,
33 : (value >>> 16) & 0xff,
34 : (value >>> 8) & 0xff,
35 : value & 0xff
36 : ])
37 : }
38 :
39 48 : function pngChunk(
40 : type: string,
41 : data: Uint8Array<ArrayBuffer>
42 : ): Uint8Array<ArrayBuffer>[] {
43 48 : const typeBytes = new TextEncoder().encode(type)
44 48 : return [u32be(data.length), typeBytes, data, u32be(crc32(typeBytes, data))]
45 : }
46 :
47 16 : async function zlibDeflate(
48 : bytes: Uint8Array<ArrayBuffer>
49 : ): Promise<Uint8Array<ArrayBuffer>> {
50 16 : const compressor = new CompressionStream('deflate')
51 16 : const writer = compressor.writable.getWriter()
52 16 : const writeDone = writer.write(bytes).then(() => writer.close())
53 :
54 16 : const chunks: Uint8Array[] = []
55 16 : const reader = compressor.readable.getReader()
56 16 : for (;;) {
57 48 : const { done, value } = await reader.read()
58 48 : if (done) break
59 32 : chunks.push(value)
60 : }
61 16 : await writeDone
62 :
63 16 : const total = chunks.reduce((sum, chunk) => sum + chunk.length, 0)
64 16 : const out = new Uint8Array(total)
65 16 : let offset = 0
66 16 : for (const chunk of chunks) {
67 32 : out.set(chunk, offset)
68 32 : offset += chunk.length
69 : }
70 16 : return out
71 : }
72 :
73 16 : export async function encodeRgbaAsPng(image: RgbaImage): Promise<Blob> {
74 16 : const { width, height, data } = image
75 16 : const bytesPerRow = width * 4
76 :
77 16 : const raw = new Uint8Array(height * (bytesPerRow + 1))
78 16 : for (let y = 0; y < height; y++) {
79 1024 : const rowStart = y * (bytesPerRow + 1)
80 1024 : raw[rowStart] = 0
81 1024 : raw.set(data.subarray(y * bytesPerRow, (y + 1) * bytesPerRow), rowStart + 1)
82 : }
83 :
84 16 : const ihdr = new Uint8Array(13)
85 16 : ihdr.set(u32be(width), 0)
86 16 : ihdr.set(u32be(height), 4)
87 16 : ihdr[8] = 8
88 16 : ihdr[9] = 6
89 :
90 16 : const idat = await zlibDeflate(raw)
91 :
92 16 : return new Blob(
93 : [
94 : PNG_SIGNATURE,
95 : ...pngChunk('IHDR', ihdr),
96 : ...pngChunk('IDAT', idat),
97 : ...pngChunk('IEND', new Uint8Array(0))
98 : ],
99 : { type: 'image/png' }
100 : )
101 : }
|