-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeno-parallel-encode.js
More file actions
213 lines (184 loc) · 5.17 KB
/
deno-parallel-encode.js
File metadata and controls
213 lines (184 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// Reference:
// https://github.com/DavidBuchanan314/parallel-png-proposal
import {
ColorType,
FilterMethod,
colorTypeToChannels,
encodeChunk,
encodeHeader,
ChunkType,
encode_IHDR,
colorTypeToString,
flattenBuffers,
} from "../index.js";
import { splitPixels } from "./util/pixels.js";
import { MultiProgressBar } from "https://deno.land/x/progress@v1.4.9/mod.ts";
import { adler32_combine } from "./util/adler32.js";
const output = Deno.args[0];
if (!output)
throw new Error(
"Must specify an output, example:\n deno run deno-parallel-encode.js myfile.png"
);
const width = 16000;
const height = 16000;
const colorType = ColorType.RGB;
const depth = 8;
const channels = colorTypeToChannels(colorType);
const filter = FilterMethod.Up;
const pageCount = 16;
console.log("Workers:", pageCount);
const ArrType = depth === 16 ? Uint16Array : Uint8ClampedArray;
const maxValue = depth === 16 ? 0xffff : 0xff;
const data = new ArrType(width * height * channels);
// quickly generate some image data
const tileSize = Math.floor(width * 0.1);
for (let y = 0; y < tileSize; y++) {
for (let x = 0; x < width; x++) {
for (let c = 0; c < channels; c++) {
const idx = x + y * height;
const px = Math.floor(x / tileSize);
const py = Math.floor(y / (tileSize / 2));
const v = (px + py) % 2 === 0 ? maxValue : 0x00;
data[idx * channels + c] = v;
}
}
}
// copy data across rest of buffer
const tileChunkSize = tileSize * width * channels;
let i = tileChunkSize;
while (i < data.length) {
data.copyWithin(i, 0, tileChunkSize);
i += tileChunkSize;
}
// our image options
const options = {
width,
height,
depth,
colorType,
filter,
};
console.log(`Image Size: %s x %s px`, width, height);
console.log(`Depth: %s bpp`, depth);
console.log(`Color Type: %s`, colorTypeToString(colorType));
// show progress
const progressBar = new MultiProgressBar({ title: "encoding" });
const file = await Deno.open(output, {
create: true,
write: true,
truncate: true,
});
const fileWriter = file.writable.getWriter();
await fileWriter.ready;
console.time("encode");
async function writeChunk(chunk) {
return fileWriter.write(encodeChunk(chunk));
}
// encode PNG header
await fileWriter.write(encodeHeader());
// encode metadata
await writeChunk({
type: ChunkType.IHDR,
data: encode_IHDR(options),
});
// number of pages i.e. number of threads that will be run
// await progressBar.render(Array(pageCount));
const deflateOptions = { level: 3 };
const results = await processWorkers(
data,
options,
pageCount,
deflateOptions,
(progresses) =>
progressBar.render(
progresses.map((p) => ({
completed: p * 100,
total: 100,
}))
)
);
let adler;
for (let i = 0; i < results.length; i++) {
const { result, adler: chunkAdler, size } = results[i];
adler = adler32_combine(adler, chunkAdler, size);
let compressed = result;
if (i === results.length - 1) {
// last chunk, concat with adler32
const adlerBytes = new Uint8Array(4);
const dv = new DataView(adlerBytes.buffer);
dv.setUint32(0, adler);
compressed = flattenBuffers([result, adlerBytes]);
}
// encode the current IDAT chunk
await writeChunk({ type: ChunkType.IDAT, data: compressed });
}
// write ending chunk
await writeChunk({ type: ChunkType.IEND });
// stop progress
await progressBar.render(results.map((p) => ({ completed: 100, total: 100 })));
await progressBar.end();
// // end stream
await fileWriter.close();
console.timeEnd("encode");
async function processWorkers(
data,
options,
pageCount,
deflateOptions,
progress = () => {}
) {
const { width, height, colorType = ColorType.RGBA } = options;
const channels = colorTypeToChannels(colorType);
const workerResults = Array(pageCount).fill(null);
let remaining = pageCount;
return new Promise((resolve) => {
// split whole stream into smaller sections
for (let { index, view, isFirst, isLast } of splitPixels(
data,
width,
height,
channels,
pageCount
)) {
const worker = new Worker(
new URL("./util/parallel-encode-worker.js", import.meta.url),
{
type: "module",
}
);
// we need to create a slice to pass it off to the worker
// otherwise subarray view gets detached
const sliced = view.slice();
worker.postMessage(
{
...options, // image encoding options and data
view: sliced,
index,
isFirst,
isLast,
deflateOptions,
},
[sliced.buffer]
);
const handler = async (ev) => {
const r = ev.data;
workerResults[r.index] = r;
if (r.result) {
worker.removeEventListener("message", handler);
worker.terminate();
remaining--;
const progresses = workerResults.map((r) => {
return r ? r.progress || 0 : 0;
});
await progress(progresses);
if (remaining === 0) {
resolve(workerResults);
} else if (remaining < 0) {
throw new Error("Worker received too many events");
}
}
};
worker.addEventListener("message", handler);
}
});
}