MCPcopy
hub / github.com/donmccurdy/glTF-Transform / ktxdecompress

Function ktxdecompress

packages/cli/src/transforms/ktxdecompress.ts:30–107  ·  view source on GitHub ↗
(options: KTXDecompressOptions = KTX_DECOMPRESS_DEFAULTS)

Source from the content-addressed store, hash-verified

28};
29
30export const ktxdecompress = function (options: KTXDecompressOptions = KTX_DECOMPRESS_DEFAULTS): Transform {
31 options = { ...KTX_DECOMPRESS_DEFAULTS, ...options };
32
33 return createTransform('ktxdecompress', async (doc: Document): Promise<void> => {
34 const logger = doc.getLogger();
35
36 // Confirm recent version of KTX-Software is installed.
37 await checkKTXSoftware(logger);
38
39 // Create workspace. Avoid 'unsafeCleanup' and 'setGracefulCleanup', which
40 // are not working as expected and are slated for removal:
41 // https://github.com/raszi/node-tmp/pull/281
42 const batchPrefix = uuid();
43 const batchDir = tmp.dirSync({ prefix: 'gltf-transform-' });
44
45 const basisuExtension = doc.createExtension(KHRTextureBasisu);
46 const textures = doc.getRoot().listTextures();
47
48 await pLimit(textures, options.jobs!, async (texture, textureIndex) => {
49 const textureLabel =
50 texture.getURI() || texture.getName() || `${textureIndex + 1}/${doc.getRoot().listTextures().length}`;
51 const prefix = `ktx:texture(${textureLabel})`;
52
53 const srcMimeType = texture.getMimeType();
54 if (srcMimeType !== 'image/ktx2') return;
55
56 const srcImage = texture.getImage()!;
57 const srcExtension = texture.getURI()
58 ? FileUtils.extension(texture.getURI())
59 : ImageUtils.mimeTypeToExtension(srcMimeType);
60 const srcSize = texture.getSize();
61 const srcBytes = srcImage ? srcImage.byteLength : null;
62
63 if (!srcImage || !srcSize || !srcBytes) {
64 logger.warn(`${prefix}: Skipping, unreadable texture.`);
65 return;
66 }
67
68 // PREPARE: Create temporary in/out paths for the 'ktx' CLI tool, and determine
69 // necessary command-line flags.
70
71 const srcPath = join(batchDir.name, `${batchPrefix}_${textureIndex}.${srcExtension}`);
72 const dstPath = join(batchDir.name, `${batchPrefix}_${textureIndex}.png`);
73
74 await fs.writeFile(srcPath, srcImage);
75
76 // COMPRESS: Run `ktx create` CLI tool.
77 // eslint-disable-next-line @typescript-eslint/no-unused-vars
78 const [status, _stdout, stderr] = await waitExit(spawn('ktx', ['extract', srcPath, dstPath]));
79
80 if (status !== 0) {
81 logger.error(`${prefix}: Failed → \n\n${stderr.toString()}`);
82 } else {
83 // PACK: Replace image data in the glTF asset.
84 texture.setImage(await fs.readFile(dstPath)).setMimeType('image/png');
85 if (texture.getURI()) {
86 texture.setURI(FileUtils.basename(texture.getURI()) + '.png');
87 }

Callers 2

cli.tsFile · 0.85

Calls 15

createTransformFunction · 0.90
uuidFunction · 0.90
checkKTXSoftwareFunction · 0.85
pLimitFunction · 0.85
joinFunction · 0.85
getLoggerMethod · 0.80
createExtensionMethod · 0.80
listTexturesMethod · 0.80
getRootMethod · 0.80
getImageMethod · 0.80
mimeTypeToExtensionMethod · 0.80
setMimeTypeMethod · 0.80

Tested by

no test coverage detected