-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspritedownsampler.html
More file actions
398 lines (396 loc) · 14.5 KB
/
Copy pathspritedownsampler.html
File metadata and controls
398 lines (396 loc) · 14.5 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sprite downsampler</title>
<script src="gzip.js"></script>
<style>
.warning::before {
display: inline-block;
content: "!";
background-color: #ddaa00;
clip-path: polygon(50% 0%, 100% 100%, 0% 100%);
--background: conic-gradient(at 50% 0%, transparent 150deg, #cc8800 210deg, #cc8800 210deg, transparent 210deg);
width: 1em;
height: 1em;
text-align: center;
color: white;
}
progress::before {
content: attr(value) "/" attr(max);
margin-top: 1lh;
display: block;
}
</style>
</head>
<body>
<h1>Sprite downsampler</h1>
<p>Downscales all costumes in the given sprites to 1/2 size, and changes the blocks so that the costumes appear the same size as before. In the resulting code the (size) block gets substituted with something that doesn't always rounds to integers. Because of a Scratch bug, the behaviour of the <touching ...?> blocks can change even if the costume looks the same after downsampling.</p>
<div>sb3 file: <input type="file" id="fi"/><button type="button" id="fopenbtn">open</button></div>
<div>Select sprites:</div>
<div><select multiple id="sps" disabled></select></div>
<div>or load sprite list (list of names in JSON format): <input type="file" id="filist" disabled/></div>
<div><button type="button" id="OKbtn" disabled>OK</button><progress hidden id="p"></progress><a id="a"></a></div>
<div id="logs"></div>
<script>
var fi=document.getElementById("fi");
var filist=document.getElementById("filist");
var fopenbtn=document.getElementById("fopenbtn");
var OKbtn=document.getElementById("OKbtn");
var sps=document.getElementById("sps");
var p=document.getElementById("p");
var a=document.getElementById("a");
var ctx=document.createElement("canvas").getContext("2d", {willReadFrequently: true});
var zip, project, targets, sprites, zipout, jsonFilename, file;
fopenbtn.onclick = async function() {
controlsDisabled(true);
try {
file = fi.files[0];
zip = new Zip(file);
await zip.loadAllFiles();
var decoder = new TextDecoder();
if(zip.hasFile("project.json")) project = JSON.parse(decoder.decode(zip.getFileContent(jsonFilename="project.json")));
else if(zip.hasFile("sprite.json")) project = JSON.parse(decoder.decode(zip.getFileContent(jsonFilename="sprite.json")));
else throw new Error("Can't find project.json");
targets = project.targets ? [...project.targets] : [project];
sprites = targets.filter(x=>x && !x.isStage);
sps.size=Math.min(sprites.length,20);
sps.innerHTML="";
for(let i=0; i<sprites.length; i++) sps.appendChild(new Option(sprites[i].name, i));
}
catch(err) {
console.error(err);
alert(err);
}
finally {
controlsDisabled(false);
}
};
function controlsDisabled(bool) {
fopenbtn.disabled=bool;
OKbtn.disabled=bool;
sps.disabled=bool;
filist.disabled=bool;
}
OKbtn.onclick = async function() {
controlsDisabled(true);
try {
p.removeAttribute("value");
p.hidden=false;
a.hidden=true;
let blob = await downscaleSelected(p);
let url = URL.createObjectURL(blob);
a.href=url;
a.download="small-"+file.name;
a.innerText="download "+a.download;
a.hidden=false;
}
catch(err) {
console.error(err);
alert(err);
}
finally {
fopenbtn.disabled=false;
//controlsDisabled(false);
}
};
filist.onchange=async function() {
controlsDisabled(true);
try {
let json = JSON.parse(await filist.files[0].text());
json = [...json];
for(let option of sps.options) option.selected=false;
for(let sprite of json) {
let selection = {name: null, idx: null};
if(typeof sprite === "string") selection.name=sprite;
if(typeof sprite === "number") selection.idx=sprite;
if(typeof sprite === "object") selection=sprite;
if(
selection.idx != null && !isNaN(+selection.idx) &&
(sprites[+selection.idx].name==selection.name || !selection.name)
) {
sps.options[+selection.idx].selected=true;
}
else {
let idx = sprites.findIndex(sprite=>sprite.name==selection.name);
if(idx >= 0) sps.options[idx].selected=true;
}
}
}
catch(err) {
console.error(err);
alert(err);
}
finally {
controlsDisabled(false);
}
};
async function downscaleSelected(progress) {
zipout = new ZipMaker();
let downscaledSpriteNames = new Set();
let downscaledCostumes = {__proto__: null};
let assetIds = new Set();
for(let i=0; i<targets.length; i++) {
if(targets[i]&&Array.isArray(targets[i].costumes)) {
for(let costume of targets[i].costumes) assetIds.add(costume.assetId);
}
}
let idCounter = 0;
function nextAssetId() {
let assetId;
do {
assetId = idCounter.toString(16).padStart(32, "0");
idCounter += 1;
} while(assetIds.has(assetId));
return assetId;
}
let globalVarIDs = new Set();
for(let target of targets) {
if(target && target.isStage) getVarIds(target, globalVarIDs);
}
let N=0;
for(let i=0; i<sps.selectedOptions.length; i++) {
let sprite=sprites[sps.selectedOptions[i].value];
if(sprite && Array.isArray(sprite.costumes)) N+=sprite.costumes.length;
}
progress.max=N;
progress.value=0;
for(let i=0; i<sps.selectedOptions.length; i++) {
let sprite=sprites[sps.selectedOptions[i].value];
downscaledSpriteNames.add(sprite.name);
for(let i=0; i<sprite.costumes.length; i++) {
let assetId = sprite.costumes[i].assetId;
let fileExt;
if(typeof sprite.costumes[i].md5ext === "string") {
fileExt = (sprite.costumes[i].md5ext.match("\.[^\.]*$") || [""])[0];
}
else if(typeof sprite.costumes[i].dataFormat === "string") {
fileExt = "."+sprite.costumes[i].dataFormat;
}
let oldfname = assetId+fileExt;
if(!(oldfname in downscaledCostumes)) {
let newfname = downscaledCostumes[oldfname] = nextAssetId()+fileExt;
if(zip.hasFile(oldfname)) {
await zipout.addFile(await downscaleImage(await zip.getFileContent(oldfname), fileExt), newfname);
}
else {
console.warn("Can't find %o", oldfname);
}
}
let newfname = downscaledCostumes[oldfname];
sprite.costumes[i].assetId = newfname.slice(0,32);
if(sprite.costumes[i].md5ext) sprite.costumes[i].md5ext=newfname;
if(sprite.costumes[i].rotationCenterX) sprite.costumes[i].rotationCenterX/=2;
if(sprite.costumes[i].rotationCenterY) sprite.costumes[i].rotationCenterY/=2;
progress.value+=1;
}
replaceSizeBlocks(sprite, globalVarIDs);
sprite.size*=2;
}
for(let i=0; i<targets.length; i++) {
replaceSensingOfBlocks(targets[i], globalVarIDs, downscaledSpriteNames);
}
let resultJSONFile = new Blob([JSON.stringify(project)], {type: "application/json"});
await zipout.addFile(resultJSONFile, jsonFilename);
await zipout.copyFiles(zip, zip.listDirectoryContent("").filter(x=>x&&!x.endsWith(".json")), progress);
return await zipout.generateZip();
}
const svgNamespaceURI="http://www.w3.org/2000/svg";
async function downscaleImage(buffer, fileExt) {
if(fileExt === ".svg") {
let svgstring = new TextDecoder().decode(buffer);
let svgdoc = new DOMParser().parseFromString(svgstring, "image/svg+xml");
let svg=svgdoc.documentElement;
let width = svg.hasAttribute("width")?svg.width.baseVal.value:svg.viewBox.baseVal.width;
if(!isNaN(width)) svg.width.baseVal.value=width/2;
let height = svg.hasAttribute("height")?svg.height.baseVal.value:svg.viewBox.baseVal.height;
if(!isNaN(height)) svg.height.baseVal.value=height/2;
let scaler = svgdoc.createElementNS(svgNamespaceURI, "g");
scaler.style.transform="scale(0.5)";
scaler.style.transformOrigin="0 0";
while(svg.firstChild) scaler.appendChild(svg.firstChild);
svg.appendChild(scaler);
let hasViewBox = svg.hasAttribute("viewBox");
svg.viewBox.baseVal.width=hasViewBox?svg.viewBox.baseVal.width/2:width/2;
svg.viewBox.baseVal.height=hasViewBox?svg.viewBox.baseVal.height/2:height/2;
svg.viewBox.baseVal.x/=2;
svg.viewBox.baseVal.y/=2;
let resultstr = new XMLSerializer().serializeToString(svgdoc);
return new Blob([resultstr],{type: "image/svg+xml"});
}
else {
let img = await createImageBitmap(new Blob([buffer], {type: "image/"+fileExt.slice(1)}));
ctx.canvas.width=Math.ceil(img.width/2);
ctx.canvas.height=Math.ceil(img.height/2);
ctx.imageSmoothingEnabled=false;
ctx.drawImage(img, 0, 0, ctx.canvas.width, ctx.canvas.height);
img.close();
return new Promise((res,err)=>
ctx.canvas.toBlob(
blob=>blob?res(blob):err(new Error("Couldn't save PNG: canvas.toBlob() failed")),
"image/png"
)
);
}
}
function getVarIds(sprite, set) {
if(sprite && sprite.variables && typeof(sprite.variables)==="object") {
for(let varID in sprite.variables) set.add(varID);
}
if(sprite && sprite.lists && typeof(sprite.lists)==="object") {
for(let varID in sprite.lists) set.add(varID);
}
if(sprite && sprite.broadcasts && typeof(sprite.broadcasts)==="object") {
for(let varID in sprite.broadcasts) set.add(varID);
}
}
function replaceSizeBlocks(sprite, globalVarIDs) {
if(!sprite || !sprite.blocks || typeof(sprite.blocks)!=="object") return;
let localVarIDs = new Set();
getVarIds(sprite, localVarIDs);
let nextId = 0;
function createBlockId() {
let res;
do {
res = "x"+nextId.toString(36);
nextId+=1;
} while((res in sprite.blocks) || localVarIDs.has(res) || globalVarIDs.has(res));
return res;
}
// Object.keys won't include the new blocks that we add
for(let blockId of Object.keys(sprite.blocks)) {
let block=sprite.blocks[blockId];
if(!block) continue;
switch(block.opcode) {
// set size larger to compensate downscaled costumes
case "looks_setsizeto":
case "looks_changesizeby":
let inputName = block.opcode=="looks_setsizeto"?"SIZE":"CHANGE";
if(!block.inputs || !block.inputs[inputName]) break;
// multiply by 2 to cancel the downscaled costumes
let mult = {
opcode: "operator_multiply",
parent: blockId,
inputs: {NUM1: [...block.inputs[inputName]], NUM2: [1,[4,2]]},
next: null,
fields: {},
shadow: false,
topLevel: false
};
let multId=createBlockId();
sprite.blocks[multId]=mult;
block.inputs[inputName][1]=multId;
break;
// divide the value by 2 as we set sprite size larger
case "looks_size":
let sizeId = createBlockId();
sprite.blocks[sizeId]=block;
let div = {
opcode: "operator_divide",
parent: block.parent,
inputs: {NUM1: [3,sizeId,[4,0]], NUM2: [1,[4,2]]},
next: null,
fields: {},
shadow: false,
topLevel: block.topLevel
};
block.parent=blockId;
block.topLevel=false;
if("x" in block) div.x=block.x;
if("y" in block) div.y=block.y;
sprite.blocks[blockId]=div;
break;
// we don't change any more blocks here
// sensing_of is handled in a different function that is called for more sprites
}
}
}
function replaceSensingOfBlocks(sprite, globalVarIDs, downscaledSprites) {
let hasProblem=false;
if(!sprite || !sprite.blocks || typeof(sprite.blocks)!=="object") return;
let localVarIDs = new Set();
getVarIds(sprite, localVarIDs);
let nextId = 0;
function createBlockId() {
let res;
do {
res = "x"+nextId.toString(36);
nextId+=1;
} while((res in sprite.blocks) || localVarIDs.has(res) || globalVarIDs.has(res));
return res;
}
// Object.keys won't include the new blocks that we add
for(let blockId of Object.keys(sprite.blocks)) {
let block=sprite.blocks[blockId];
if(!block || block.opcode !== "sensing_of") continue;
// check if the block will read the size of the target
let maybeNotSize = false;
if(block.inputs && block.inputs.PROPERTY) {
maybeNotSize = true;
}
else if(!block.fields || !block.fields.PROPERTY || block.fields.PROPERTY[0] != "size") {
// certainly won't read the size of something
continue;
}
// check if the block will read from a downscaled sprite
let maybeNotDownscaled = false;
if(block.inputs && block.inputs.OBJECT) {
if(
Array.isArray(block.inputs.OBJECT[1]) &&
block.inputs.OBJECT[1][0] < 11 &&
!downscaledSprites.has(block.inputs.OBJECT[1][1])
) {
// constant input, not a downscaled sprite
continue;
}
else if(
typeof(block.inputs.OBJECT[1])==="string" &&
sprite.blocks[block.inputs.OBJECT[1]] &&
sprite.blocks[block.inputs.OBJECT[1]].opcode === "sensing_of_object_menu" &&
(!sprite.blocks[block.inputs.OBJECT[1]].inputs || !sprite.blocks[block.inputs.OBJECT[1]].inputs.OBJECT)
) {
// constant input
if(!sprite.blocks[block.inputs.OBJECT[1]].fields || !downscaledSprites.has(sprite.blocks[block.inputs.OBJECT[1]].fields.OBJECT[0])) {
// isn't a downscaled sprite
continue;
}
}
else {
// can't prove constant
maybeNotDownscaled = true;
}
}
if(maybeNotDownscaled || maybeNotSize) hasProblem=true;
else {
// proved that the block reads the size of a downscaled sprite
// divide the value by 2 as we set sprite size larger when compensating downscaled costumes
let sizeId = createBlockId();
sprite.blocks[sizeId]=block;
let div = {
opcode: "operator_divide",
parent: block.parent,
inputs: {NUM1: [3,sizeId,[4,0]], NUM2: [1,[4,2]]},
next: null,
fields: {},
shadow: false,
topLevel: block.topLevel
};
block.parent=blockId;
block.topLevel=false;
if("x" in block) div.x=block.x;
if("y" in block) div.y=block.y;
sprite.blocks[blockId]=div;
}
}
if(hasProblem) {
let msg = `In the ${JSON.stringify(sprite.name)} sprite, couldn't decide if some ([size v] of (...)) blocks can get the size of downscaled sprites, in which case it will return 2x larger number than before.`;
let div=document.createElement("div");
div.classList.add("warning");
div.innerText=msg;
document.getElementById("logs").appendChild(div);
}
return;
}
</script>
</body>
</html>