Skip to content

Commit

Permalink
[cli][functions] Replace @squoosh/lib with sharp (#752)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jan 8, 2023
1 parent c296525 commit 5946e8e
Show file tree
Hide file tree
Showing 17 changed files with 553 additions and 509 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@types/ndarray": "1.0.11",
"@types/node": "18.11.18",
"@types/node-gzip": "1.1.0",
"@types/sharp": "^0.31.0",
"@types/tape": "4.13.2",
"@typescript-eslint/eslint-plugin": "5.47.1",
"@typescript-eslint/parser": "5.47.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"@gltf-transform/core": "^2.5.1",
"@gltf-transform/extensions": "^2.5.1",
"@gltf-transform/functions": "^2.5.1",
"@squoosh/lib": "^0.4.0",
"@types/inquirer": "^8.2.1",
"@types/language-tags": "^1.0.0",
"@types/micromatch": "^4.0.2",
Expand All @@ -48,6 +47,7 @@
"node-gzip": "^1.1.2",
"p-limit": "3.1.0",
"semver": "^7.3.7",
"sharp": "^0.31.2",
"spdx-correct": "^3.1.1",
"tmp": "^0.2.1"
},
Expand All @@ -60,5 +60,5 @@
"package.json",
"package-lock.json"
],
"gitHead": "5cc8d10b5de17633b6a63a4fa5e10e0e60c71e84"
"gitHead": "381ba367afd6179de695be1e4a771b337ccdabad"
}
61 changes: 14 additions & 47 deletions packages/cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,20 @@ import { gzip } from 'node-gzip';
import { program } from '@caporal/core';
import { Logger, NodeIO, PropertyType, VertexLayout, vec2 } from '@gltf-transform/core';
import { ALL_EXTENSIONS } from '@gltf-transform/extensions';
import { CenterOptions, InstanceOptions, PartitionOptions, PruneOptions, QUANTIZE_DEFAULTS, ResampleOptions, SequenceOptions, TEXTURE_RESIZE_DEFAULTS, TextureResizeFilter, UnweldOptions, WeldOptions, center, dedup, instance, metalRough, partition, prune, quantize, resample, sequence, tangents, textureResize, unweld, weld, reorder, dequantize, oxipng, mozjpeg, webp, unlit, meshopt, DRACO_DEFAULTS, draco, DracoOptions, simplify, SIMPLIFY_DEFAULTS, WELD_DEFAULTS } from '@gltf-transform/functions';
import { CenterOptions, InstanceOptions, PartitionOptions, PruneOptions, QUANTIZE_DEFAULTS, ResampleOptions, SequenceOptions, TEXTURE_RESIZE_DEFAULTS, TextureResizeFilter, UnweldOptions, WeldOptions, center, dedup, instance, metalRough, partition, prune, quantize, resample, sequence, tangents, textureResize, unweld, weld, reorder, dequantize, unlit, meshopt, DRACO_DEFAULTS, draco, DracoOptions, simplify, SIMPLIFY_DEFAULTS, WELD_DEFAULTS, textureCompress, TEXTURE_COMPRESS_DEFAULTS } from '@gltf-transform/functions';
import { InspectFormat, inspect } from './inspect';
import { ETC1S_DEFAULTS, Filter, Mode, UASTC_DEFAULTS, ktxfix, merge, toktx, XMPOptions, xmp } from './transforms';
import { formatBytes, MICROMATCH_OPTIONS, underline } from './util';
import { Session } from './session';
import { ValidateOptions, validate } from './validate';
import * as squoosh from '@squoosh/lib';

let io: NodeIO;

// Use require() so microbundle doesn't compile these.
const fetch = require('node-fetch');
const draco3d = require('draco3dgltf');
const mikktspace = require('mikktspace');
const sharp = require('sharp');
const { MeshoptDecoder, MeshoptEncoder, MeshoptSimplifier } = require('meshoptimizer');

const programReady = new Promise<void>((resolve) => {
Expand Down Expand Up @@ -1009,31 +1009,19 @@ affects only the container metadata.`.trim())
.transform(ktxfix())
);

const SQUOOSH_SUMMARY = `
Compresses textures with {VARIANT}, using @squoosh/lib. Reduces transmitted file
const TEXTURE_COMPRESS_SUMMARY = `
Compresses textures with {VARIANT}, using sharp. Reduces transmitted file
size. Compared to GPU texture compression like KTX/Basis, PNG/JPEG/WebP must
be fully decompressed in GPU memory — this makes texture GPU upload much
slower, and may consume 4-8x more GPU memory. However, the PNG/JPEG/WebP
compression methods are typically more forgiving than GPU texture compression,
and require less tuning to achieve good visual and filesize results.
The experimental auto-optimization mode, --auto, can be used to iteratively
refine and optimize encoder settings against perceptual metrics (Butteraugli
distance). Slower, with typically higher-quality and larger images compared to
the default encoder settings.
${underline('NOTICE')}: Only a small subset of the available @squoosh/lib
encoder configuration options are currently exposed by this commandline. If any
contributors would like to recommend, test, and document additional encoder
options, please open a pull request.
See: https://github.com/GoogleChromeLabs/squoosh/blob/dev/libsquoosh/src/codecs.ts
`.trim();

// WEBP
program
.command('webp', 'WebP texture compression')
.help(SQUOOSH_SUMMARY.replace(/{VARIANT}/g, 'WebP'))
.help(TEXTURE_COMPRESS_SUMMARY.replace(/{VARIANT}/g, 'WebP'))
.argument('<input>', INPUT_DESC)
.argument('<output>', OUTPUT_DESC)
.option(
Expand All @@ -1046,24 +1034,17 @@ program
'Texture slots to include (glob)',
{validator: program.STRING, default: '*'}
)
.option(
'--auto',
'Enables experimental auto-optimization with perceptual metrics.'
+ ' Slower, with typically higher-quality and larger images'
+ ' compared to the default encoder settings.',
{validator: program.BOOLEAN, default: false}
)
.action(({args, options, logger}) => {
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
return Session.create(io, logger, args.input, args.output)
.transform(webp({...options, formats, slots, squoosh}));
.transform(textureCompress({targetFormat: 'webp', encoder: sharp, formats, slots}));
});

// OXIPNG
// PNG
program
.command('oxipng', 'OxiPNG texture compression')
.help(SQUOOSH_SUMMARY.replace(/{VARIANT}/g, 'OxiPNG'))
.command('png', 'PNG texture compression')
.help(TEXTURE_COMPRESS_SUMMARY.replace(/{VARIANT}/g, 'PNG'))
.argument('<input>', INPUT_DESC)
.argument('<output>', OUTPUT_DESC)
.option(
Expand All @@ -1076,24 +1057,17 @@ program
'Texture slots to include (glob)',
{validator: program.STRING, default: '*'}
)
.option(
'--auto',
'Enables experimental auto-optimization with perceptual metrics.'
+ ' Slower, with typically higher-quality and larger images'
+ ' compared to the default encoder settings.',
{validator: program.BOOLEAN, default: false}
)
.action(({args, options, logger}) => {
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
return Session.create(io, logger, args.input, args.output)
.transform(oxipng({...options, formats, slots, squoosh}));
.transform(textureCompress({targetFormat: 'png', encoder: sharp, formats, slots}));
});

// MOZJPEG
// JPEG
program
.command('mozjpeg', 'MozJPEG texture compression')
.help(SQUOOSH_SUMMARY.replace(/{VARIANT}/g, 'MozJPEG'))
.command('jpeg', 'JPEG texture compression')
.help(TEXTURE_COMPRESS_SUMMARY.replace(/{VARIANT}/g, 'JPEG'))
.argument('<input>', INPUT_DESC)
.argument('<output>', OUTPUT_DESC)
.option(
Expand All @@ -1106,18 +1080,11 @@ program
'Texture slots to include (glob)',
{validator: program.STRING, default: '*'}
)
.option(
'--auto',
'Enables experimental auto-optimization with perceptual metrics.'
+ ' Slower, with typically higher-quality and larger images'
+ ' compared to the default encoder settings.',
{validator: program.BOOLEAN, default: false}
)
.action(({args, options, logger}) => {
const formats = micromatch.makeRe(String(options.formats), MICROMATCH_OPTIONS);
const slots = micromatch.makeRe(String(options.slots), MICROMATCH_OPTIONS);
return Session.create(io, logger, args.input, args.output)
.transform(mozjpeg({...options, formats, slots, squoosh}));
.transform(textureCompress({targetFormat: 'jpeg', encoder: sharp, formats, slots}));
});

program.command('', '\n\n⏯ ANIMATION ────────────────────────────────────────');
Expand Down
3 changes: 0 additions & 3 deletions packages/cli/src/types/global.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
// See: https://github.com/KhronosGroup/glTF-Validator/issues/114
declare module 'gltf-validator';

// See: https://github.com/GoogleChromeLabs/squoosh/issues/1211
declare module '@squoosh/lib';
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@
"mangle": {
"regex": "^_"
},
"gitHead": "5cc8d10b5de17633b6a63a4fa5e10e0e60c71e84"
"gitHead": "381ba367afd6179de695be1e4a771b337ccdabad"
}
3 changes: 2 additions & 1 deletion packages/core/src/properties/texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ export class Texture extends ExtensibleProperty<ITexture> {
*/
public setURI(uri: string): this {
this.set('uri', uri);
this.set('mimeType', ImageUtils.extensionToMimeType(FileUtils.extension(uri)));
const mimeType = ImageUtils.extensionToMimeType(FileUtils.extension(uri));
if (mimeType) this.set('mimeType', mimeType);
return this;
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/image-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export class ImageUtils {
/** Returns the MIME type for the given file extension. */
public static extensionToMimeType(extension: string): string {
if (extension === 'jpg') return 'image/jpeg';
if (!extension) return '';
return `image/${extension}`;
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/extensions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@
"README.md",
"package.json"
],
"gitHead": "5cc8d10b5de17633b6a63a4fa5e10e0e60c71e84"
"gitHead": "381ba367afd6179de695be1e4a771b337ccdabad"
}
2 changes: 1 addition & 1 deletion packages/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@
"publishConfig": {
"access": "public"
},
"gitHead": "5cc8d10b5de17633b6a63a4fa5e10e0e60c71e84"
"gitHead": "381ba367afd6179de695be1e4a771b337ccdabad"
}
2 changes: 1 addition & 1 deletion packages/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export * from './reorder';
export * from './sequence';
export * from './simplify';
export * from './sort-primitive-weights';
export * from './squoosh';
export * from './texture-compress';
export * from './tangents';
export * from './texture-resize';
export * from './transform-mesh';
Expand Down
Loading

0 comments on commit 5946e8e

Please sign in to comment.