@@ -4,7 +4,9 @@ import { Observable } from "observable-fns"
44import { Document , NodeIO } from '@gltf-transform/core' ;
55import { KHRTextureBasisu } from '@gltf-transform/extensions' ;
66import { 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
911import fs from "node:fs" ;
1012import path from "node:path" ;
@@ -13,6 +15,17 @@ import { addOBJ } from "../../trees/lib/obj.js";
1315import { 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+
1629const EXTENSIONS = [ KHRTextureBasisu ] ;
1730
1831async 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