diff --git a/src/artifacts/gpuAsset/GpuAssetLayouts.test.ts b/src/artifacts/gpuAsset/GpuAssetLayouts.test.ts index 3c8775be12b..3fff59195a5 100644 --- a/src/artifacts/gpuAsset/GpuAssetLayouts.test.ts +++ b/src/artifacts/gpuAsset/GpuAssetLayouts.test.ts @@ -150,6 +150,35 @@ describe("GLB image bufferView and buffer resolution", () => { ]); }); + it("does not count data URI images or data URI buffers as external", async () => { + const layout = await readGlb( + glbWithJson( + JSON.stringify({ + asset: { version: "2.0" }, + buffers: [ + { + uri: `data:application/octet-stream;base64,${GLB_IMAGE.toString("base64")}`, + byteLength: GLB_IMAGE.length, + }, + ], + bufferViews: [{ buffer: 0, byteLength: GLB_IMAGE.length }], + images: [ + { + uri: `data:image/png;base64,${GLB_IMAGE.toString("base64")}`, + }, + { bufferView: 0, mimeType: "image/png" }, + { uri: "cesium.png" }, + ], + }), + Buffer.alloc(0), + ), + ); + expect(layout.regionsExamined).toBe("complete"); + expect(layout.limitations).toEqual([]); + expect(layout.metadata.external_images).toBe(1); + expect(layout.regions.map(({ path }) => path)).toEqual(["chunks/0.json"]); + }); + it.each([ [ "a non-object image element before a valid sibling", diff --git a/src/artifacts/gpuAsset/GpuAssetLayouts.ts b/src/artifacts/gpuAsset/GpuAssetLayouts.ts index 1c71f368a08..4b81a106b3f 100644 --- a/src/artifacts/gpuAsset/GpuAssetLayouts.ts +++ b/src/artifacts/gpuAsset/GpuAssetLayouts.ts @@ -311,6 +311,8 @@ const indexOf = (value: unknown): number | undefined => Number.isSafeInteger(value) && (value as number) >= 0 ? (value as number) : undefined; +const isDataUri = (value: unknown): boolean => + typeof value === "string" && /^data:/iu.test(value); const readGlbJsonChunk = async ( read: AssetRead, @@ -454,11 +456,12 @@ export const glbLayout = async ( unresolvedImages++; return; } - // An image named by URI is outside the GLB, not one of its regions. + // A data URI carries the image in the JSON chunk; any other URI names + // a companion file outside the GLB, not one of its regions. if (imageRecord["bufferView"] === undefined) { - // Only a URI string establishes an external source. - if (typeof imageRecord["uri"] === "string") externalImages++; - else unresolvedImages++; + if (typeof imageRecord["uri"] === "string") { + if (!isDataUri(imageRecord["uri"])) externalImages++; + } else unresolvedImages++; return; } const viewIndex = indexOf(imageRecord["bufferView"]); @@ -472,9 +475,10 @@ export const glbLayout = async ( const buffer = bufferIndex === undefined ? undefined : asRecord(buffers[bufferIndex]); // glTF 2.0 GLB: only buffers[0] without a uri is the BIN chunk. A buffer - // with a uri is external, so its images are not GLB regions either. + // with a data URI is embedded in the JSON chunk; any other uri is + // external, so its images are not GLB regions either. if (buffer !== undefined && typeof buffer["uri"] === "string") { - externalImages++; + if (!isDataUri(buffer["uri"])) externalImages++; return; } const start =