2021-12-24 05:12:08 +00:00
|
|
|
import { buf } from "crc-32";
|
|
|
|
import { Buffer } from "buffer";
|
2022-01-05 01:14:23 +00:00
|
|
|
import type { ImageProcessor } from "./main";
|
2021-12-24 05:12:08 +00:00
|
|
|
|
2022-01-13 09:24:59 +00:00
|
|
|
export type PNGChunk = [
|
|
|
|
string, // name
|
|
|
|
() => Promise<Buffer>, // data
|
|
|
|
() => Promise<number>, // crc
|
|
|
|
number];// offset
|
2021-12-24 05:12:08 +00:00
|
|
|
|
2022-01-12 06:58:46 +00:00
|
|
|
export class PNGDecoder {
|
2021-12-24 05:12:08 +00:00
|
|
|
repr: Buffer;
|
2022-01-01 18:52:50 +00:00
|
|
|
|
2021-12-24 05:12:08 +00:00
|
|
|
req = 8;
|
2022-01-01 18:52:50 +00:00
|
|
|
|
2021-12-24 05:12:08 +00:00
|
|
|
ptr = 8;
|
|
|
|
|
|
|
|
constructor(private reader: ReadableStreamDefaultReader<Uint8Array>) {
|
|
|
|
this.repr = Buffer.from([]);
|
|
|
|
}
|
|
|
|
|
|
|
|
async catchup() {
|
|
|
|
while (this.repr.byteLength < this.req) {
|
2022-01-01 18:52:50 +00:00
|
|
|
const chunk = await this.reader.read();
|
2022-01-04 15:36:43 +00:00
|
|
|
if (chunk.done) {
|
|
|
|
throw new Error(`Unexpected EOF, got ${this.repr.byteLength}, required ${this.req}, ${chunk.value}`);
|
|
|
|
}
|
|
|
|
this.repr = Buffer.concat([this.repr, chunk.value]);
|
2021-12-24 05:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async *chunks() {
|
|
|
|
while (true) {
|
|
|
|
this.req += 8; // req length and name
|
|
|
|
await this.catchup();
|
2022-01-01 18:52:50 +00:00
|
|
|
const length = this.repr.readUInt32BE(this.ptr);
|
|
|
|
const name = this.repr.slice(this.ptr + 4, this.ptr + 8).toString();
|
2021-12-24 05:12:08 +00:00
|
|
|
this.ptr += 4;
|
|
|
|
this.req += length + 4; // crc
|
2022-01-13 09:24:59 +00:00
|
|
|
//await this.catchup();
|
2022-01-04 15:36:43 +00:00
|
|
|
const pos = this.ptr;
|
2022-01-13 09:24:59 +00:00
|
|
|
yield [name,
|
|
|
|
async () => {
|
|
|
|
await this.catchup();
|
|
|
|
return this.repr.slice(pos, pos + length + 4);
|
|
|
|
},
|
|
|
|
async () => {
|
|
|
|
await this.catchup();
|
|
|
|
return this.repr.readUInt32BE(this.ptr + length + 4);
|
|
|
|
},
|
|
|
|
this.ptr] as PNGChunk;
|
2021-12-24 05:12:08 +00:00
|
|
|
this.ptr += length + 8;
|
|
|
|
if (name == 'IEND')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async dtor() {
|
2022-01-01 18:52:50 +00:00
|
|
|
//ugh
|
2021-12-24 05:12:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-12 06:58:46 +00:00
|
|
|
export class PNGEncoder {
|
2021-12-24 05:12:08 +00:00
|
|
|
writer: WritableStreamDefaultWriter<Buffer>;
|
|
|
|
|
|
|
|
constructor(bytes: WritableStream<Buffer>) {
|
|
|
|
this.writer = bytes.getWriter();
|
|
|
|
this.writer.write(Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]));
|
|
|
|
}
|
|
|
|
|
|
|
|
async insertchunk(chunk: PNGChunk) {
|
2022-01-01 18:52:50 +00:00
|
|
|
const b = Buffer.alloc(4);
|
2022-01-13 12:55:57 +00:00
|
|
|
const buff = await chunk[1]();
|
|
|
|
b.writeInt32BE(buff.length - 4, 0);
|
2021-12-24 05:12:08 +00:00
|
|
|
await this.writer.write(b); // write length
|
2022-01-13 12:55:57 +00:00
|
|
|
await this.writer.write(buff); // chunk includes chunkname
|
|
|
|
b.writeInt32BE(buf(buff), 0);
|
2021-12-24 05:12:08 +00:00
|
|
|
await this.writer.write(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
async dtor() {
|
|
|
|
this.writer.releaseLock();
|
|
|
|
await this.writer.close();
|
|
|
|
}
|
2022-01-01 18:52:50 +00:00
|
|
|
}
|
|
|
|
|
2022-01-03 22:29:28 +00:00
|
|
|
export const BufferWriteStream = () => {
|
2022-01-01 18:52:50 +00:00
|
|
|
let b = Buffer.from([]);
|
|
|
|
const ret = new WritableStream<Buffer>({
|
|
|
|
write(chunk) {
|
2022-01-04 15:36:43 +00:00
|
|
|
b = Buffer.concat([b, chunk]);
|
2022-01-01 18:52:50 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return [ret, () => b] as [WritableStream<Buffer>, () => Buffer];
|
|
|
|
};
|