Skip to content

Commit 3cf3ed8

Browse files
fix: replace sharp
1 parent 4018ab9 commit 3cf3ed8

4 files changed

Lines changed: 43 additions & 57 deletions

File tree

package-lock.json

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
"georaster": "^1.6.0",
7272
"georaster-layer-for-leaflet": "^4.1.2",
7373
"got": "^14.6.6",
74+
"jpeg-js": "^0.4.4",
7475
"konva": "^10.2.1",
7576
"ktx2-encoder": "^0.5.3",
7677
"leaflet": "^1.9.4",
@@ -90,7 +91,6 @@
9091
"react-dom": "^19.2.3",
9192
"react-konva": "^19.2.3",
9293
"react-router": "^7.13.1",
93-
"sharp": "^0.34.5",
9494
"stackblur-canvas": "^2.7.0",
9595
"svg-path-properties": "^1.3.0",
9696
"svg-transform-parser": "^0.0.1",

src/lib/export/gltf.js

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import {
1818
EXTMeshGPUInstancing,
1919
} from '@gltf-transform/extensions';
2020
import { PNG } from 'pngjs';
21-
import sharp from 'sharp';
2221

2322
import { hexToRGB01 } from '../colors';
2423
import { TEXTURE_MAP } from '../textures';
@@ -297,41 +296,6 @@ export async function write(filePath, project, meshData, imageData) {
297296
// Deduplicate any identical textures/materials/meshes
298297
await doc.transform(dedup());
299298

300-
// let textureCount = 0;
301-
// let totalTextures = 0;
302-
303-
// // Count first
304-
// doc.getRoot().listTextures().forEach(t => totalTextures++);
305-
306-
// await doc.transform(
307-
// ktx2({
308-
// // isUASTC: false,
309-
// isUASTC: true,
310-
// generateMipmap: true, // try without mipmaps first — halves encoding time
311-
312-
// imageDecoder: async (data) => {
313-
// const start = Date.now();
314-
// const { info, data: raw } = await sharp(Buffer.from(data))
315-
// .ensureAlpha()
316-
// .raw()
317-
// .toBuffer({ resolveWithObject: true });
318-
319-
// console.log(`[KTX2] Decoded ${++textureCount}/${totalTextures}: ${info.width}x${info.height} (${Date.now() - start}ms)`, info);
320-
// // Copy to a clean ArrayBuffer — don't use raw.buffer which is pooled
321-
// const pixels = new Uint8Array(raw.length);
322-
// pixels.set(raw);
323-
324-
// return {
325-
// width: info.width,
326-
// height: info.height,
327-
// // channels: 4,
328-
// // channels: info.channels,
329-
// data: pixels // new Uint8Array(raw.buffer, raw.byteOffset, raw.byteLength),
330-
// };
331-
// }
332-
// })
333-
// );
334-
335299

336300
const { sky } = openProject.scene;
337301

src/lib/workers/export.worker.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Observable } from "observable-fns"
44
import { Document, NodeIO } from '@gltf-transform/core';
55
import { KHRTextureBasisu } from '@gltf-transform/extensions';
66
import { ktx2 } from 'ktx2-encoder/gltf-transform';
7-
import sharp from 'sharp';
7+
// import sharp from 'sharp';
8+
import { PNG } from 'pngjs';
9+
import jpeg from 'jpeg-js';
810

911
import fs from "node:fs";
1012
import path from "node:path";
@@ -13,6 +15,17 @@ import { addOBJ } from "../../trees/lib/obj.js";
1315
import { addGLB } from "../../trees/lib/gltf.js";
1416

1517

18+
function decodeImage(data) {
19+
const buf = Buffer.from(data);
20+
// JPEG magic bytes: 0xFF 0xD8
21+
if (buf[0] === 0xFF && buf[1] === 0xD8) {
22+
const { width, height, data: pixels } = jpeg.decode(buf, { useTArray: true });
23+
return { width, height, data: new Uint8Array(pixels) };
24+
}
25+
const png = PNG.sync.read(buf);
26+
return { width: png.width, height: png.height, data: new Uint8Array(png.data) };
27+
}
28+
1629
const EXTENSIONS = [KHRTextureBasisu];
1730

1831
async function compressTextures(glbBuffer) {
@@ -34,13 +47,15 @@ async function compressTextures(glbBuffer) {
3447
ktx2({
3548
isUASTC: true,
3649
generateMipmap: true,
37-
imageDecoder: async (data) => {
38-
const { info, data: raw } = await sharp(Buffer.from(data))
39-
.ensureAlpha()
40-
.raw()
41-
.toBuffer({ resolveWithObject: true });
42-
const pixels = new Uint8Array(raw.length);
43-
pixels.set(raw);
50+
imageDecoder: decodeImage
51+
// imageDecoder: async (data) => {
52+
// return decodeImage(data);
53+
// const { info, data: raw } = await sharp(Buffer.from(data))
54+
// .ensureAlpha()
55+
// .raw()
56+
// .toBuffer({ resolveWithObject: true });
57+
// const pixels = new Uint8Array(raw.length);
58+
// pixels.set(raw);
4459

4560
// observer.next({
4661
// type: 'progress',
@@ -49,8 +64,8 @@ async function compressTextures(glbBuffer) {
4964
// current: ++textureCount
5065
// }
5166
// });
52-
return { width: info.width, height: info.height, data: pixels };
53-
}
67+
// return { width: info.width, height: info.height, data: pixels };
68+
// }
5469
})
5570
);
5671

@@ -115,15 +130,16 @@ export async function exportTreePackage(inputFiles, outputFile) {
115130
ktx2({
116131
isUASTC: true,
117132
generateMipmap: true,
118-
imageDecoder: async (data) => {
119-
const { info, data: raw } = await sharp(Buffer.from(data))
120-
.ensureAlpha()
121-
.raw()
122-
.toBuffer({ resolveWithObject: true });
123-
const pixels = new Uint8Array(raw.length);
124-
pixels.set(raw);
125-
return { width: info.width, height: info.height, data: pixels };
126-
}
133+
imageDecoder: decodeImage
134+
// imageDecoder: async (data) => {
135+
// const { info, data: raw } = await sharp(Buffer.from(data))
136+
// .ensureAlpha()
137+
// .raw()
138+
// .toBuffer({ resolveWithObject: true });
139+
// const pixels = new Uint8Array(raw.length);
140+
// pixels.set(raw);
141+
// return { width: info.width, height: info.height, data: pixels };
142+
// }
127143
})
128144
);
129145

0 commit comments

Comments
 (0)