nanoodle 0.14.0 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -3
- package/package.json +7 -5
- package/src/graph.mjs +6 -0
- package/src/local-media.mjs +5 -94
- package/src/mp4cat.mjs +40 -8
- package/src/png-decoder.mjs +226 -0
- package/src/sprite-avif.mjs +12 -0
- package/src/sprite-bitmap.mjs +233 -0
- package/src/sprite-image.mjs +73 -0
- package/src/sprite-model.mjs +18 -8
- package/src/sprite-orientation.mjs +65 -0
- package/src/sprite-projection.mjs +71 -36
- package/src/sprite-stack.mjs +42 -69
- package/src/sprite-video.mjs +262 -0
- package/src/zlib.mjs +23 -6
- package/src/sprite-cpu.mjs +0 -89
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
// BMP files and classic ICO entries store the same device-independent bitmap.
|
|
2
|
+
// A BMP supplies the pixel offset in a file header. ICO follows its color table
|
|
3
|
+
// immediately with XOR pixels and a separate 1-bit transparency plane.
|
|
4
|
+
function valid(condition, reason) {
|
|
5
|
+
if (!condition) throw new Error("Invalid BMP/ICO: " + reason);
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
function maskChannel(mask, depth) {
|
|
9
|
+
if (!mask) return null;
|
|
10
|
+
const shift = 31 - Math.clz32(mask & -mask);
|
|
11
|
+
const max = mask >>> shift;
|
|
12
|
+
valid((max & (max + 1)) === 0 && (depth === 32 || mask < 2 ** depth), "invalid color mask");
|
|
13
|
+
return { shift, max };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function valueFor(word, field) {
|
|
17
|
+
return Math.round(((((word >>> field.shift) & field.max) >>> 0) * 255) / field.max);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function writePalette(data, dst, palette, index) {
|
|
21
|
+
const p = index * 3;
|
|
22
|
+
valid(p + 2 < palette.length, "palette index out of range");
|
|
23
|
+
data[dst] = palette[p];
|
|
24
|
+
data[dst + 1] = palette[p + 1];
|
|
25
|
+
data[dst + 2] = palette[p + 2];
|
|
26
|
+
data[dst + 3] = 255;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function rlePixels(bytes, offset, width, height, depth, palette, data) {
|
|
30
|
+
let x = 0,
|
|
31
|
+
row = 0;
|
|
32
|
+
while (offset + 2 <= bytes.length) {
|
|
33
|
+
const count = bytes[offset++],
|
|
34
|
+
code = bytes[offset++];
|
|
35
|
+
if (!count) {
|
|
36
|
+
if (code === 1) return offset;
|
|
37
|
+
if (code === 0) {
|
|
38
|
+
x = 0;
|
|
39
|
+
row++;
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
if (code === 2) {
|
|
43
|
+
valid(offset + 2 <= bytes.length, "truncated RLE delta");
|
|
44
|
+
x += bytes[offset++];
|
|
45
|
+
row += bytes[offset++];
|
|
46
|
+
valid(x <= width && row < height, "RLE delta out of bounds");
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const length = count || code;
|
|
51
|
+
valid(row < height && x + length <= width, "RLE run out of bounds");
|
|
52
|
+
const packed = Math.ceil((length * depth) / 8);
|
|
53
|
+
if (!count) valid(offset + ((packed + 1) & ~1) <= bytes.length, "truncated absolute RLE run");
|
|
54
|
+
let dst = ((height - row - 1) * width + x) * 4;
|
|
55
|
+
for (let n = 0; n < length; n++, dst += 4) {
|
|
56
|
+
const value = count ? code : bytes[offset + (depth === 8 ? n : n >> 1)];
|
|
57
|
+
const index = depth === 8 ? value : (value >> (n % 2 ? 0 : 4)) & 15;
|
|
58
|
+
writePalette(data, dst, palette, index);
|
|
59
|
+
}
|
|
60
|
+
if (!count) offset += (packed + 1) & ~1;
|
|
61
|
+
x += length;
|
|
62
|
+
}
|
|
63
|
+
throw new Error("Invalid BMP/ICO: missing RLE end marker");
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function dib(bytes, pixelOffset, icon) {
|
|
67
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
68
|
+
const u16 = (p) => view.getUint16(p, true);
|
|
69
|
+
const u32 = (p) => view.getUint32(p, true);
|
|
70
|
+
const s32 = (p) => view.getInt32(p, true);
|
|
71
|
+
valid(bytes.length >= 12, "truncated bitmap header");
|
|
72
|
+
const header = u32(0);
|
|
73
|
+
const core = header === 12;
|
|
74
|
+
const os2 = core || header === 16 || header === 64;
|
|
75
|
+
valid(
|
|
76
|
+
[12, 16, 40, 52, 56, 64, 108, 124].includes(header) && header <= bytes.length,
|
|
77
|
+
"unsupported bitmap header",
|
|
78
|
+
);
|
|
79
|
+
const width = core ? u16(4) : s32(4);
|
|
80
|
+
const storedHeight = core ? u16(6) : s32(8);
|
|
81
|
+
const planes = u16(core ? 8 : 12),
|
|
82
|
+
depth = u16(core ? 10 : 14);
|
|
83
|
+
const topDown = storedHeight < 0;
|
|
84
|
+
const height = Math.abs(storedHeight) / (icon ? 2 : 1);
|
|
85
|
+
valid(planes === 1 && [1, 2, 4, 8, 16, 24, 32].includes(depth), "pixel format");
|
|
86
|
+
valid(
|
|
87
|
+
width > 0 &&
|
|
88
|
+
height > 0 &&
|
|
89
|
+
Number.isInteger(height) &&
|
|
90
|
+
height <= 8192 &&
|
|
91
|
+
width <= (height <= 128 ? 16384 : 8192),
|
|
92
|
+
"bitmap dimensions",
|
|
93
|
+
);
|
|
94
|
+
if (icon) valid(width === icon.width && height === icon.height, "icon dimensions disagree");
|
|
95
|
+
const compression = header >= 20 ? u32(16) : 0;
|
|
96
|
+
const bitfields = compression === 3 || compression === 6;
|
|
97
|
+
const rle = compression === 1 || compression === 2;
|
|
98
|
+
valid(
|
|
99
|
+
compression === 0 ||
|
|
100
|
+
(bitfields && !os2 && (depth === 16 || depth === 32)) ||
|
|
101
|
+
(rle && !topDown && ((depth === 8 && compression === 1) || (depth === 4 && compression === 2))),
|
|
102
|
+
"compression",
|
|
103
|
+
);
|
|
104
|
+
|
|
105
|
+
let offset = header;
|
|
106
|
+
let masks = depth === 16 ? [0x7c00, 0x03e0, 0x001f, 0] : [0xff0000, 0xff00, 0xff, 0];
|
|
107
|
+
if (bitfields) {
|
|
108
|
+
const count = compression === 6 || header >= 56 ? 4 : 3;
|
|
109
|
+
valid(
|
|
110
|
+
40 + 4 * count <= bytes.length && (header === 40 || 40 + 4 * count <= header),
|
|
111
|
+
"truncated bitfields",
|
|
112
|
+
);
|
|
113
|
+
masks = [u32(40), u32(44), u32(48), count === 4 ? u32(52) : 0];
|
|
114
|
+
if (header === 40) offset += 4 * count;
|
|
115
|
+
valid(masks[0] && masks[1] && masks[2], "missing RGB mask");
|
|
116
|
+
} else if (!os2 && header >= 56 && depth >= 16) {
|
|
117
|
+
masks[3] = (u32(52) & (depth === 32 ? 0xffffffff : 2 ** depth - 1)) >>> 0;
|
|
118
|
+
}
|
|
119
|
+
if (icon && depth === 32 && !bitfields && !masks[3]) masks[3] = 0xff000000;
|
|
120
|
+
let fields;
|
|
121
|
+
if (depth === 16 || depth === 32 || masks[3]) {
|
|
122
|
+
let used = 0;
|
|
123
|
+
for (const mask of masks) {
|
|
124
|
+
valid(!(used & mask), "overlapping color masks");
|
|
125
|
+
used |= mask;
|
|
126
|
+
}
|
|
127
|
+
fields = masks.map((mask) => maskChannel(mask, depth));
|
|
128
|
+
}
|
|
129
|
+
const usedColors = header >= 36 ? u32(32) : 0;
|
|
130
|
+
const paletteSize = depth <= 8 ? usedColors || 2 ** depth : usedColors;
|
|
131
|
+
const entrySize = core ? 3 : 4;
|
|
132
|
+
valid(
|
|
133
|
+
(depth > 8 || paletteSize <= 2 ** depth) && offset + paletteSize * entrySize <= bytes.length,
|
|
134
|
+
"truncated palette",
|
|
135
|
+
);
|
|
136
|
+
let palette;
|
|
137
|
+
if (depth <= 8) {
|
|
138
|
+
palette = new Uint8Array(paletteSize * 3);
|
|
139
|
+
for (let n = 0; n < paletteSize; n++) {
|
|
140
|
+
const b = offset + n * entrySize;
|
|
141
|
+
palette[n * 3] = bytes[b + 2];
|
|
142
|
+
palette[n * 3 + 1] = bytes[b + 1];
|
|
143
|
+
palette[n * 3 + 2] = bytes[b];
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
offset += paletteSize * entrySize;
|
|
147
|
+
if (!icon) {
|
|
148
|
+
valid(pixelOffset >= offset && pixelOffset <= bytes.length, "invalid pixel offset");
|
|
149
|
+
offset = pixelOffset;
|
|
150
|
+
}
|
|
151
|
+
const stride = 4 * Math.ceil((width * depth) / 32);
|
|
152
|
+
valid(rle || offset + height * stride <= bytes.length, "truncated pixel rows");
|
|
153
|
+
const data = new Uint8ClampedArray(width * height * 4);
|
|
154
|
+
let populatedAlpha = false;
|
|
155
|
+
if (rle) {
|
|
156
|
+
// Delta moves and short rows leave opaque black pixels in browser BMPs.
|
|
157
|
+
for (let i = 3; i < data.length; i += 4) data[i] = 255;
|
|
158
|
+
offset = rlePixels(bytes, offset, width, height, depth, palette, data);
|
|
159
|
+
} else {
|
|
160
|
+
for (let y = 0; y < height; y++, offset += stride) {
|
|
161
|
+
let dst = (topDown ? y : height - 1 - y) * width * 4;
|
|
162
|
+
for (let x = 0; x < width; x++, dst += 4) {
|
|
163
|
+
if (depth <= 8) {
|
|
164
|
+
const bit = x * depth;
|
|
165
|
+
const index = (bytes[offset + (bit >> 3)] >> (8 - depth - (bit & 7))) & ((1 << depth) - 1);
|
|
166
|
+
writePalette(data, dst, palette, index);
|
|
167
|
+
} else if (depth === 24 || (depth === 32 && !bitfields && !masks[3])) {
|
|
168
|
+
const p = offset + x * (depth / 8);
|
|
169
|
+
data[dst] = bytes[p + 2];
|
|
170
|
+
data[dst + 1] = bytes[p + 1];
|
|
171
|
+
data[dst + 2] = bytes[p];
|
|
172
|
+
data[dst + 3] = 255;
|
|
173
|
+
} else {
|
|
174
|
+
const word = depth === 16 ? u16(offset + x * 2) : u32(offset + x * 4);
|
|
175
|
+
data[dst] = valueFor(word, fields[0]);
|
|
176
|
+
data[dst + 1] = valueFor(word, fields[1]);
|
|
177
|
+
data[dst + 2] = valueFor(word, fields[2]);
|
|
178
|
+
data[dst + 3] = fields[3] ? valueFor(word, fields[3]) : 255;
|
|
179
|
+
if (fields[3] && data[dst + 3]) populatedAlpha = true;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// A populated 32-bit icon alpha channel supersedes the legacy AND mask.
|
|
185
|
+
if (icon && !populatedAlpha) {
|
|
186
|
+
const maskStride = 4 * Math.ceil(width / 32);
|
|
187
|
+
valid(offset + maskStride * height <= bytes.length, "truncated icon transparency mask");
|
|
188
|
+
for (let y = 0; y < height; y++)
|
|
189
|
+
for (let x = 0; x < width; x++) {
|
|
190
|
+
const mask = bytes[offset + y * maskStride + (x >> 3)] & (128 >> (x & 7));
|
|
191
|
+
const dst = ((topDown ? y : height - 1 - y) * width + x) * 4;
|
|
192
|
+
if (mask) data.fill(0, dst, dst + 4);
|
|
193
|
+
else data[dst + 3] = 255;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return { width, height, data };
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function decodeBitmap(bytes) {
|
|
200
|
+
valid(bytes.length >= 26 && bytes[0] === 66 && bytes[1] === 77, "invalid BMP file header");
|
|
201
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
202
|
+
return dib(bytes.subarray(14), view.getUint32(10, true) - 14, null);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export async function decodeIcon(bytes, decodePng) {
|
|
206
|
+
valid(bytes.length >= 6, "truncated icon header");
|
|
207
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
208
|
+
const count = view.getUint16(4, true);
|
|
209
|
+
const kind = view.getUint16(2, true);
|
|
210
|
+
valid(
|
|
211
|
+
view.getUint16(0, true) === 0 && (kind === 1 || kind === 2) && count && 6 + count * 16 <= bytes.length,
|
|
212
|
+
"invalid icon directory",
|
|
213
|
+
);
|
|
214
|
+
let best;
|
|
215
|
+
for (let n = 0, p = 6; n < count; n++, p += 16) {
|
|
216
|
+
const width = bytes[p] || 256,
|
|
217
|
+
height = bytes[p + 1] || 256;
|
|
218
|
+
const depth = (kind === 1 && view.getUint16(p + 6, true)) || Math.ceil(Math.log2(bytes[p + 2] || 256));
|
|
219
|
+
const size = view.getUint32(p + 8, true),
|
|
220
|
+
offset = view.getUint32(p + 12, true);
|
|
221
|
+
valid(offset >= 6 + count * 16 && size && offset + size <= bytes.length, "icon entry out of bounds");
|
|
222
|
+
if (!best || width * height > best.area || (width * height === best.area && depth > best.depth)) {
|
|
223
|
+
best = { width, height, depth, area: width * height, offset, size };
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const image = bytes.subarray(best.offset, best.offset + best.size);
|
|
227
|
+
if (image[0] === 137 && image[1] === 80) {
|
|
228
|
+
const rgba = await decodePng(image);
|
|
229
|
+
valid(rgba.width === best.width && rgba.height === best.height, "icon PNG dimensions disagree");
|
|
230
|
+
return rgba;
|
|
231
|
+
}
|
|
232
|
+
return dib(image, 0, best);
|
|
233
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { decodeBitmap, decodeIcon } from "./sprite-bitmap.mjs";
|
|
2
|
+
import { MAX_IMAGE_DIM } from "./local-media.mjs";
|
|
3
|
+
import { decodePngPixels } from "./png-decoder.mjs";
|
|
4
|
+
import { pngOrientation, orientImage } from "./sprite-orientation.mjs";
|
|
5
|
+
|
|
6
|
+
// ftyp lists a major brand, a minor version, then compatible brands. AVIF
|
|
7
|
+
// assets can use mif1 as the major brand and advertise avif only in the list.
|
|
8
|
+
function isAvifImage(bytes) {
|
|
9
|
+
if (bytes.length < 16 || bytes[4] !== 102 || bytes[5] !== 116 || bytes[6] !== 121 || bytes[7] !== 112)
|
|
10
|
+
return false;
|
|
11
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
12
|
+
let end = view.getUint32(0), start = 8;
|
|
13
|
+
if (end === 1) {
|
|
14
|
+
if (bytes.length < 24 || view.getUint32(8) !== 0) return false;
|
|
15
|
+
end = view.getUint32(12);
|
|
16
|
+
start = 16;
|
|
17
|
+
}
|
|
18
|
+
end = Math.min(end || bytes.length, bytes.length);
|
|
19
|
+
for (let i = start; i + 4 <= end; i += 4) {
|
|
20
|
+
if (i === start + 4) continue;
|
|
21
|
+
if (bytes[i] === 97 && bytes[i + 1] === 118 && bytes[i + 2] === 105 &&
|
|
22
|
+
(bytes[i + 3] === 102 || bytes[i + 3] === 115)) return true;
|
|
23
|
+
}
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Node loads the maintained decoder only for formats that need it. Its native
|
|
28
|
+
// binaries are installed with the npm package. The editor uses native bitmaps.
|
|
29
|
+
async function decodeRaster(bytes, format) {
|
|
30
|
+
const { default: sharp } = await import("sharp");
|
|
31
|
+
try {
|
|
32
|
+
const limitInputPixels = format === "JPEG" ? 16_777_216 : MAX_IMAGE_DIM ** 2;
|
|
33
|
+
const image = sharp(bytes, { pages: 1, failOn: "error", limitInputPixels });
|
|
34
|
+
const meta = await image.metadata();
|
|
35
|
+
if (meta.height > MAX_IMAGE_DIM || meta.width > (meta.height <= 128 ? 16384 : MAX_IMAGE_DIM))
|
|
36
|
+
throw new Error("dimensions exceed the sprite image limit");
|
|
37
|
+
// libavif interpolates 4:2:0 chroma the way the editor does. Metadata is
|
|
38
|
+
// bounded above before the WebAssembly decoder allocates its pixel buffer.
|
|
39
|
+
if (format === "AVIF") {
|
|
40
|
+
const { decodeAvif } = await import("./sprite-avif.mjs");
|
|
41
|
+
return await decodeAvif(bytes);
|
|
42
|
+
}
|
|
43
|
+
// JPEG respects EXIF orientation. The editor displays WebP in stored orientation.
|
|
44
|
+
if (format === "JPEG") image.autoOrient();
|
|
45
|
+
const { info, data } = await image.toColourspace("srgb").ensureAlpha().raw().toBuffer({ resolveWithObject: true });
|
|
46
|
+
return { width: info.width, height: info.height, data: new Uint8ClampedArray(data.buffer, data.byteOffset, data.byteLength) };
|
|
47
|
+
} catch (error) {
|
|
48
|
+
throw new Error("Invalid " + format + " sprite image: " + error.message, { cause: error });
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// All sprite inputs (textures, palettes and the renderer's strip) use the same
|
|
53
|
+
// straight-alpha pixel layout, independent of their on-disk encoding.
|
|
54
|
+
export async function decodeSpriteBytes(input) {
|
|
55
|
+
const bytes = input instanceof Uint8Array ? input : new Uint8Array(input);
|
|
56
|
+
if (bytes[0] === 66 && bytes[1] === 77) return decodeBitmap(bytes);
|
|
57
|
+
if (bytes[0] === 0 && bytes[1] === 0 && (bytes[2] === 1 || bytes[2] === 2) && bytes[3] === 0)
|
|
58
|
+
return decodeIcon(bytes, decodeSpriteBytes);
|
|
59
|
+
if (bytes[0] === 137 && bytes[1] === 80) {
|
|
60
|
+
const { w, h, rgba } = await decodePngPixels(bytes, {
|
|
61
|
+
maxDim: MAX_IMAGE_DIM,
|
|
62
|
+
allowSpriteStrip: true,
|
|
63
|
+
firstAnimationFrame: true,
|
|
64
|
+
});
|
|
65
|
+
return orientImage({ width: w, height: h, data: rgba }, pngOrientation(bytes));
|
|
66
|
+
}
|
|
67
|
+
if (bytes[0] === 255 && bytes[1] === 216) return decodeRaster(bytes, "JPEG");
|
|
68
|
+
if (bytes[0] === 71 && bytes[1] === 73 && bytes[2] === 70) return decodeRaster(bytes, "GIF");
|
|
69
|
+
if (isAvifImage(bytes)) return decodeRaster(bytes, "AVIF");
|
|
70
|
+
if (bytes[0] === 82 && bytes[1] === 73 && bytes[2] === 70 && bytes[3] === 70 &&
|
|
71
|
+
bytes[8] === 87 && bytes[9] === 69 && bytes[10] === 66 && bytes[11] === 80) return decodeRaster(bytes, "WebP");
|
|
72
|
+
throw new Error("Unsupported sprite image encoding. Expected PNG, JPEG, GIF, WebP, AVIF, BMP, ICO or CUR.");
|
|
73
|
+
}
|
package/src/sprite-model.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { dataUrlBytes } from "./media.mjs";
|
|
1
|
+
import { dataUrlBytes, sniffMime } from "./media.mjs";
|
|
2
2
|
|
|
3
3
|
import { ident, mul, trs, xform, unpackGlb } from "./gltf-scene.mjs";
|
|
4
4
|
|
|
@@ -34,8 +34,8 @@ function embedded(uri) {
|
|
|
34
34
|
export async function triangleModel(bytes, imagePixels) {
|
|
35
35
|
const { json, bin } = unpack(bytes);
|
|
36
36
|
if (json?.asset?.version !== "2.0") throw new Error("Expected glTF 2.0.");
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
const unsupported = (json.extensionsRequired || []).filter((name) => name !== "EXT_texture_webp");
|
|
38
|
+
if (unsupported.length) throw new Error("Unsupported required glTF extensions: " + unsupported.join(", "));
|
|
39
39
|
const buffers = (json.buffers || []).map((b, i) => {
|
|
40
40
|
const data = b.uri ? embedded(b.uri).bytes : i === 0 && bin;
|
|
41
41
|
if (!data || data.length < b.byteLength) throw new Error("Missing or truncated glTF buffer.");
|
|
@@ -88,15 +88,25 @@ export async function triangleModel(bytes, imagePixels) {
|
|
|
88
88
|
}
|
|
89
89
|
const textures = new Map();
|
|
90
90
|
async function texture(info) {
|
|
91
|
-
const tex = json.textures?.[info.index]
|
|
92
|
-
|
|
91
|
+
const tex = json.textures?.[info.index];
|
|
92
|
+
const useWebp = Object.hasOwn(tex?.extensions || {}, "EXT_texture_webp");
|
|
93
|
+
const source = useWebp ? tex.extensions.EXT_texture_webp?.source : tex?.source;
|
|
94
|
+
const img = json.images?.[source];
|
|
95
|
+
if (useWebp && (!Number.isSafeInteger(source) || source < 0 || !img))
|
|
96
|
+
throw new Error("Invalid EXT_texture_webp image source.");
|
|
93
97
|
if (!img || info.extensions?.KHR_texture_transform)
|
|
94
98
|
throw new Error("Unsupported base-color texture encoding or transform.");
|
|
95
|
-
if (!
|
|
99
|
+
if (useWebp && !img.uri && img.mimeType !== "image/webp")
|
|
100
|
+
throw new Error("EXT_texture_webp bufferView requires image/webp.");
|
|
101
|
+
if (!textures.has(source)) {
|
|
96
102
|
const image = img.uri ? embedded(img.uri) : { bytes: bufferView(img.bufferView), mime: img.mimeType };
|
|
97
|
-
|
|
103
|
+
const webp = sniffMime(image.bytes) === "image/webp";
|
|
104
|
+
if (useWebp && !webp) throw new Error("EXT_texture_webp source must contain a WebP image.");
|
|
105
|
+
textures.set(source, { pixels: await imagePixels(image.bytes, image.mime), webp });
|
|
98
106
|
}
|
|
99
|
-
|
|
107
|
+
const cached = textures.get(source);
|
|
108
|
+
if (useWebp && !cached.webp) throw new Error("EXT_texture_webp source must contain a WebP image.");
|
|
109
|
+
return { pixels: cached.pixels, sampler: json.samplers?.[tex.sampler] || {} };
|
|
100
110
|
}
|
|
101
111
|
const meshes = [];
|
|
102
112
|
async function emit(index, world) {
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Camera orientation is tag 0x0112 in TIFF IFD0 (a single SHORT). An invalid
|
|
2
|
+
// optional metadata block leaves the decoded image in its stored orientation.
|
|
3
|
+
function orientation(tiff) {
|
|
4
|
+
if (tiff.length < 8) return 1;
|
|
5
|
+
const view = new DataView(tiff.buffer, tiff.byteOffset, tiff.byteLength);
|
|
6
|
+
const order = view.getUint16(0);
|
|
7
|
+
if (order !== 0x4949 && order !== 0x4d4d) return 1;
|
|
8
|
+
const little = order === 0x4949;
|
|
9
|
+
if (view.getUint16(2, little) !== 42) return 1;
|
|
10
|
+
const start = view.getUint32(4, little);
|
|
11
|
+
if (start < 8 || start + 2 > tiff.length) return 1;
|
|
12
|
+
const count = view.getUint16(start, little);
|
|
13
|
+
if (start + 2 + count * 12 > tiff.length) return 1;
|
|
14
|
+
for (let i = 0, p = start + 2; i < count; i++, p += 12) {
|
|
15
|
+
if (view.getUint16(p, little) !== 0x0112) continue;
|
|
16
|
+
if (view.getUint16(p + 2, little) !== 3 || view.getUint32(p + 4, little) !== 1) return 1;
|
|
17
|
+
const n = view.getUint16(p + 8, little);
|
|
18
|
+
return n >= 1 && n <= 8 ? n : 1;
|
|
19
|
+
}
|
|
20
|
+
return 1;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function pngOrientation(bytes) {
|
|
24
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
25
|
+
for (let p = 8; p + 12 <= bytes.length; ) {
|
|
26
|
+
const size = view.getUint32(p);
|
|
27
|
+
if (p + size + 12 > bytes.length) break;
|
|
28
|
+
const tag = view.getUint32(p + 4);
|
|
29
|
+
if (tag === 0x65584966) return orientation(bytes.subarray(p + 8, p + 8 + size)); // eXIf
|
|
30
|
+
if (tag === 0x49454e44) break;
|
|
31
|
+
p += size + 12;
|
|
32
|
+
}
|
|
33
|
+
return 1;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function orientImage(image, tag) {
|
|
37
|
+
if (tag === 1) return image;
|
|
38
|
+
const { width: w, height: h, data } = image;
|
|
39
|
+
const transpose = tag >= 5;
|
|
40
|
+
const width = transpose ? h : w,
|
|
41
|
+
height = transpose ? w : h;
|
|
42
|
+
const output = new Uint8ClampedArray(data.length);
|
|
43
|
+
const to = new Uint32Array(output.buffer);
|
|
44
|
+
const from = new Uint32Array(data.buffer, data.byteOffset, data.length / 4);
|
|
45
|
+
// Destination position of source (0,0) and the increments for source x/y.
|
|
46
|
+
// Multiplying the row count by dy places each source row in its destination.
|
|
47
|
+
const steps = [
|
|
48
|
+
null,
|
|
49
|
+
null,
|
|
50
|
+
[w - 1, -1, w],
|
|
51
|
+
[w * h - 1, -1, -w],
|
|
52
|
+
[(h - 1) * w, 1, -w],
|
|
53
|
+
[0, h, 1],
|
|
54
|
+
[h - 1, h, -1],
|
|
55
|
+
[w * h - 1, -h, -1],
|
|
56
|
+
[(w - 1) * h, -h, 1],
|
|
57
|
+
];
|
|
58
|
+
const [origin, dx, dy] = steps[tag];
|
|
59
|
+
let pos = 0;
|
|
60
|
+
for (let y = 0; y < h; y++) {
|
|
61
|
+
let dst = origin + y * dy;
|
|
62
|
+
for (let x = 0; x < w; x++, dst += dx) to[dst] = from[pos++];
|
|
63
|
+
}
|
|
64
|
+
return { width, height, data: output };
|
|
65
|
+
}
|
|
@@ -24,45 +24,80 @@ export function viewPlan(sheet, fields) {
|
|
|
24
24
|
return { tile, layers, scale, rise, squash, w, h };
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
//
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
bottom = Math.min(h, Math.ceil(cy + radius * squash));
|
|
42
|
-
for (let y = top; y < bottom; y++) {
|
|
43
|
-
const dy = (y + 0.5 - cy) / (scale * squash);
|
|
44
|
-
for (let x = left; x < right; x++) {
|
|
45
|
-
const dx = (x + 0.5 - w / 2) / scale;
|
|
46
|
-
const u = Math.floor(c * dx + s * dy + tile / 2),
|
|
47
|
-
v = Math.floor(-s * dx + c * dy + tile / 2);
|
|
48
|
-
if (u < 0 || v < 0 || u >= tile || v >= tile) continue;
|
|
49
|
-
const src = (v * sheet.width + l * tile + u) * 4,
|
|
50
|
-
dst = (y * w + x) * 4,
|
|
51
|
-
alpha = sheet.data[src + 3];
|
|
52
|
-
if (!alpha) continue;
|
|
53
|
-
if (alpha === 255 || !data[dst + 3]) {
|
|
54
|
-
data.set(sheet.data.subarray(src, src + 4), dst);
|
|
55
|
-
} else {
|
|
56
|
-
const under = data[dst + 3] * (1 - alpha / 255),
|
|
57
|
-
a = alpha + under;
|
|
58
|
-
for (let k = 0; k < 3; k++)
|
|
59
|
-
data[dst + k] = (sheet.data[src + k] * alpha + data[dst + k] * under) / a;
|
|
60
|
-
data[dst + 3] = a;
|
|
61
|
-
}
|
|
27
|
+
// Scan transparency once per sheet. Empty sky and empty layers are common in a sliced mesh.
|
|
28
|
+
function occupiedLayers(sheet, plan) {
|
|
29
|
+
const { tile, layers, rise, scale, h } = plan;
|
|
30
|
+
const result = [];
|
|
31
|
+
for (let layer = 0; layer < layers; layer++) {
|
|
32
|
+
let left = tile, top = tile, right = 0, bottom = 0;
|
|
33
|
+
for (let y = 0; y < tile; y++) {
|
|
34
|
+
let alpha = (y * sheet.width + layer * tile) * 4 + 3;
|
|
35
|
+
for (let x = 0; x < tile; x++, alpha += 4) {
|
|
36
|
+
if (!sheet.data[alpha]) continue;
|
|
37
|
+
left = Math.min(left, x);
|
|
38
|
+
right = Math.max(right, x + 1);
|
|
39
|
+
top = Math.min(top, y);
|
|
40
|
+
bottom = y + 1;
|
|
62
41
|
}
|
|
63
42
|
}
|
|
43
|
+
if (right === 0) continue;
|
|
44
|
+
result.push({
|
|
45
|
+
offset: layer * tile,
|
|
46
|
+
x: (left + right - tile) / 2, y: (top + bottom - tile) / 2,
|
|
47
|
+
halfWidth: (right - left) / 2, halfHeight: (bottom - top) / 2,
|
|
48
|
+
cy: h / 2 + ((layers - 1) / 2 - layer) * rise * scale,
|
|
49
|
+
});
|
|
64
50
|
}
|
|
65
|
-
return
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Invert the orthographic transform at each output pixel center, then composite
|
|
55
|
+
// layers in ground-to-top order. draw() can be reused for every turntable angle.
|
|
56
|
+
export function softwareView(sheet, fields) {
|
|
57
|
+
const plan = viewPlan(sheet, fields);
|
|
58
|
+
const { tile, scale, squash, w, h } = plan;
|
|
59
|
+
const layers = occupiedLayers(sheet, plan);
|
|
60
|
+
return function draw(yaw) {
|
|
61
|
+
const data = new Uint8ClampedArray(w * h * 4);
|
|
62
|
+
const angle = (yaw * Math.PI) / 180, c = Math.cos(angle), s = Math.sin(angle);
|
|
63
|
+
const cosX = new Float64Array(w), sinX = new Float64Array(w);
|
|
64
|
+
for (let x = 0; x < w; x++) {
|
|
65
|
+
const dx = (x + 0.5 - w / 2) / scale;
|
|
66
|
+
cosX[x] = c * dx;
|
|
67
|
+
sinX[x] = -s * dx;
|
|
68
|
+
}
|
|
69
|
+
for (const layer of layers) {
|
|
70
|
+
const cx = w / 2 + scale * (c * layer.x - s * layer.y);
|
|
71
|
+
const cy = layer.cy + scale * squash * (s * layer.x + c * layer.y);
|
|
72
|
+
const rx = scale * (Math.abs(c) * layer.halfWidth + Math.abs(s) * layer.halfHeight);
|
|
73
|
+
const ry = scale * squash * (Math.abs(s) * layer.halfWidth + Math.abs(c) * layer.halfHeight);
|
|
74
|
+
const left = Math.max(0, Math.floor(cx - rx)), right = Math.min(w, Math.ceil(cx + rx));
|
|
75
|
+
const top = Math.max(0, Math.floor(cy - ry)), bottom = Math.min(h, Math.ceil(cy + ry));
|
|
76
|
+
for (let y = top; y < bottom; y++) {
|
|
77
|
+
const dy = (y + 0.5 - layer.cy) / (scale * squash);
|
|
78
|
+
const sinY = s * dy, cosY = c * dy;
|
|
79
|
+
for (let x = left; x < right; x++) {
|
|
80
|
+
const u = Math.floor(cosX[x] + sinY + tile / 2), v = Math.floor(sinX[x] + cosY + tile / 2);
|
|
81
|
+
if (u < 0 || u >= tile || v < 0 || v >= tile) continue;
|
|
82
|
+
const src = (v * sheet.width + layer.offset + u) * 4, alpha = sheet.data[src + 3];
|
|
83
|
+
if (!alpha) continue;
|
|
84
|
+
const dst = (y * w + x) * 4;
|
|
85
|
+
if (alpha === 255 || !data[dst + 3]) {
|
|
86
|
+
data[dst] = sheet.data[src];
|
|
87
|
+
data[dst + 1] = sheet.data[src + 1];
|
|
88
|
+
data[dst + 2] = sheet.data[src + 2];
|
|
89
|
+
data[dst + 3] = alpha;
|
|
90
|
+
} else {
|
|
91
|
+
const under = data[dst + 3] * (1 - alpha / 255), a = alpha + under;
|
|
92
|
+
for (let k = 0; k < 3; k++)
|
|
93
|
+
data[dst + k] = (sheet.data[src + k] * alpha + data[dst + k] * under) / a;
|
|
94
|
+
data[dst + 3] = a;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return { width: w, height: h, data };
|
|
100
|
+
};
|
|
66
101
|
}
|
|
67
102
|
|
|
68
103
|
export function flag(value, fallback) {
|