-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnoizuclient.js
More file actions
186 lines (149 loc) · 4.37 KB
/
Copy pathnoizuclient.js
File metadata and controls
186 lines (149 loc) · 4.37 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
// ----------------------------
// CONFIG
// ----------------------------
const DB_NAME = "noizu_cache";
const STORE_CHUNKS = "chunks";
const STORE_META = "meta";
// ----------------------------
// INIT DB
// ----------------------------
function openDB() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME, 1);
req.onupgradeneeded = () => {
const db = req.result;
if (!db.objectStoreNames.contains(STORE_CHUNKS)) {
db.createObjectStore(STORE_CHUNKS);
}
if (!db.objectStoreNames.contains(STORE_META)) {
db.createObjectStore(STORE_META);
}
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
// ----------------------------
// SAVE CHUNK
// ----------------------------
async function saveChunk(id, blob) {
const db = await openDB();
const tx = db.transaction(STORE_CHUNKS, "readwrite");
tx.objectStore(STORE_CHUNKS).put(blob, id);
return tx.complete;
}
function renderHybrid(data, ctx) {
// 1. fast pass
data.encoding.forEach(c => {
if (c.type === "ref") drawChunk(c, ctx);
});
// 2. patch pass
data.patches.forEach(p => renderPatch(p, ctx));
}
// ----------------------------
// LOAD CHUNK
// ----------------------------
async function loadChunk(id) {
const db = await openDB();
const tx = db.transaction(STORE_CHUNKS, "readonly");
return tx.objectStore(STORE_CHUNKS).get(id);
}
// ----------------------------
// DOWNLOAD + CACHE PACK
// ----------------------------
async function downloadChunkPack(url = "/download-chunk-pack") {
const res = await fetch(url);
const blob = await res.blob();
const zip = await unzip(blob);
const index = JSON.parse(await zip["index.json"].text());
for (const entry of index) {
const file = zip[entry.path];
const chunkBlob = await file.blob();
await saveChunk(entry.id, chunkBlob);
}
console.log("[Noizu] Chunk pack cached:", index.length);
}
function renderPatch(patch, ctx) {
fetch(`/patch/${patch.id}`)
.then(r => r.blob())
.then(blob => createImageBitmap(blob))
.then(img => {
ctx.drawImage(img, patch.x, patch.y);
});
}
// ----------------------------
// SIMPLE ZIP READER (JSZip)
// ----------------------------
async function unzip(blob) {
const JSZip = await import("https://cdn.jsdelivr.net/npm/jszip/+esm");
const zip = await JSZip.default.loadAsync(blob);
const files = {};
for (const name of Object.keys(zip.files)) {
files[name] = zip.files[name];
}
return files;
}
// ----------------------------
// RECONSTRUCTION
// ----------------------------
async function reconstructImage(encoding, width, height) {
const canvas = document.createElement("canvas");
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext("2d");
for (const chunk of encoding) {
const { x, y } = chunk;
if (chunk.type === "ref") {
const blob = await loadChunk(chunk.id);
if (!blob) continue;
const img = await createImageBitmap(blob);
ctx.drawImage(img, x, y);
} else if (chunk.type === "raw") {
const img = await base64ToImage(chunk.data);
ctx.drawImage(img, x, y);
}
}
return canvas;
}
// ----------------------------
// BASE64 → IMAGE
// ----------------------------
function base64ToImage(base64) {
return new Promise((resolve) => {
const img = new Image();
img.src = "data:image/png;base64," + base64;
img.onload = () => resolve(img);
});
}
// ----------------------------
// FETCH + AUTO DECIDE
// ----------------------------
async function fetchNoizuImage(file) {
const formData = new FormData();
formData.append("file", file);
const res = await fetch("/process-image", {
method: "POST",
body: formData
});
if (res.headers.get("content-type").includes("application/json")) {
const data = await res.json();
if (data.mode === "noizu") {
console.log("[Noizu] Using procedural reconstruction");
const canvas = await reconstructImage(
data.encoding,
512, // TODO: pass actual size
512
);
return canvas;
}
}
// fallback image
const blob = await res.blob();
const img = await createImageBitmap(blob);
const canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
return canvas;
}