@pbk20191/icodec 0.6.5 → 0.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/jpeg.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ImageDataLike, WasmSource } from "./common.js";
1
+ import { EnumValue, ImageDataLike, WasmSource } from "./common.js";
2
2
  export declare const enum ColorSpace {
3
3
  GRAYSCALE = 1,
4
4
  RGB = 2,
@@ -55,13 +55,13 @@ export interface Options {
55
55
  /**
56
56
  * @default ColorSpace.YCbCr
57
57
  */
58
- colorSpace?: ColorSpace;
58
+ colorSpace?: EnumValue<typeof ColorSpace>;
59
59
  /**
60
60
  * Select the predefined quantization table to use.
61
61
  *
62
62
  * @default Quantization.ImageMagick
63
63
  */
64
- quantTable?: Quantization;
64
+ quantTable?: EnumValue<typeof Quantization>;
65
65
  /**
66
66
  * use scans in trellis optimization.
67
67
  *
package/lib/jpeg.js ADDED
@@ -0,0 +1,34 @@
1
+ import wasmFactoryEnc from "../dist/mozjpeg.js";
2
+ import { check, encodeES, loadES } from "./common.js";
3
+ export const defaultOptions = {
4
+ quality: 75,
5
+ baseline: false,
6
+ arithmetic: false,
7
+ progressive: true,
8
+ optimizeCoding: true,
9
+ smoothing: 0,
10
+ colorSpace: 3 /* ColorSpace.YCbCr */,
11
+ quantTable: 3 /* Quantization.ImageMagick */,
12
+ trellisMultipass: false,
13
+ trellisOptZero: false,
14
+ trellisOptTable: false,
15
+ trellisLoops: 1,
16
+ autoSubsample: true,
17
+ chromaSubsample: 2,
18
+ separateChromaQuality: false,
19
+ chromaQuality: 75,
20
+ };
21
+ export const bitDepth = [8];
22
+ export const mimeType = "image/jpeg";
23
+ export const extension = "jpg";
24
+ let codecWASM;
25
+ export async function loadEncoder(input) {
26
+ return codecWASM = await loadES(wasmFactoryEnc, input);
27
+ }
28
+ export const loadDecoder = loadEncoder;
29
+ export function encode(image, options) {
30
+ return encodeES("JPEG Encode", codecWASM, defaultOptions, image, options);
31
+ }
32
+ export function decode(input) {
33
+ return check(codecWASM.decode(input), "JPEG Decode");
34
+ }
package/lib/jxl.d.ts CHANGED
@@ -1,10 +1,10 @@
1
- import { ImageDataLike, WasmSource } from "./common.js";
2
- export declare const enum Override {
1
+ import { EnumValue, ImageDataLike, WasmSource } from "./common.js";
2
+ export declare enum Override {
3
3
  Default = -1,
4
4
  False = 0,
5
5
  True = 1
6
6
  }
7
- export declare const enum Predictor {
7
+ export declare enum Predictor {
8
8
  Default = -1,
9
9
  Zero = 0,
10
10
  Left = 1,
@@ -167,7 +167,7 @@ export interface Options {
167
167
  *
168
168
  * @default Predictor.Default,
169
169
  */
170
- modularPredictor?: Predictor;
170
+ modularPredictor?: EnumValue<typeof Predictor>;
171
171
  }
172
172
  export declare const defaultOptions: Required<Options>;
173
173
  export declare const mimeType = "image/jxl";
@@ -177,3 +177,5 @@ export declare function loadEncoder(input?: WasmSource): Promise<any>;
177
177
  export declare function loadDecoder(input?: WasmSource): Promise<any>;
178
178
  export declare function encode(image: ImageDataLike, options?: Options): Uint8Array<ArrayBufferLike>;
179
179
  export declare function decode(input: BufferSource): ImageData;
180
+ export declare function unloadDecoder(): void;
181
+ export declare function unloadEncoder(): void;
package/lib/jxl.js ADDED
@@ -0,0 +1,75 @@
1
+ import wasmFactoryEnc from "../dist/jxl-enc.js";
2
+ import wasmFactoryDec from "../dist/jxl-dec.js";
3
+ import { check, encodeES, loadES } from "./common.js";
4
+ // Tristate bool value, `Default` means encoder chooses.
5
+ export var Override;
6
+ (function (Override) {
7
+ Override[Override["Default"] = -1] = "Default";
8
+ Override[Override["False"] = 0] = "False";
9
+ Override[Override["True"] = 1] = "True";
10
+ })(Override || (Override = {}));
11
+ export var Predictor;
12
+ (function (Predictor) {
13
+ Predictor[Predictor["Default"] = -1] = "Default";
14
+ Predictor[Predictor["Zero"] = 0] = "Zero";
15
+ Predictor[Predictor["Left"] = 1] = "Left";
16
+ Predictor[Predictor["Top"] = 2] = "Top";
17
+ Predictor[Predictor["Average0"] = 3] = "Average0";
18
+ Predictor[Predictor["Select"] = 4] = "Select";
19
+ Predictor[Predictor["Gradient"] = 5] = "Gradient";
20
+ Predictor[Predictor["Weighted"] = 6] = "Weighted";
21
+ Predictor[Predictor["TopRight"] = 7] = "TopRight";
22
+ Predictor[Predictor["TopLeft"] = 8] = "TopLeft";
23
+ Predictor[Predictor["LeftLeft"] = 9] = "LeftLeft";
24
+ Predictor[Predictor["Average1"] = 10] = "Average1";
25
+ Predictor[Predictor["Average2"] = 11] = "Average2";
26
+ Predictor[Predictor["Average3"] = 12] = "Average3";
27
+ Predictor[Predictor["Average4"] = 13] = "Average4";
28
+ // The following predictors are encoder-only.
29
+ Predictor[Predictor["Best"] = 14] = "Best";
30
+ Predictor[Predictor["Variable"] = 15] = "Variable";
31
+ })(Predictor || (Predictor = {}));
32
+ export const defaultOptions = {
33
+ lossless: false,
34
+ quality: 75,
35
+ alphaQuality: 100,
36
+ effort: 7,
37
+ brotliEffort: -1,
38
+ epf: -1,
39
+ gaborish: -1,
40
+ responsive: -1,
41
+ progressiveDC: -1,
42
+ progressiveAC: -1,
43
+ qProgressiveAC: -1,
44
+ decodingSpeed: 0,
45
+ photonNoiseIso: 0,
46
+ modular: false,
47
+ lossyPalette: false,
48
+ paletteColors: -1,
49
+ iterations: -1,
50
+ modularColorspace: -1,
51
+ modularPredictor: Predictor.Default,
52
+ };
53
+ export const mimeType = "image/jxl";
54
+ export const extension = "jxl";
55
+ export const bitDepth = [8, 9, 10, 11, 12, 13, 14, 15, 16];
56
+ let encoderWASM;
57
+ let decoderWASM;
58
+ export async function loadEncoder(input) {
59
+ return encoderWASM ??= await loadES(wasmFactoryEnc, input);
60
+ }
61
+ export async function loadDecoder(input) {
62
+ return decoderWASM ??= await loadES(wasmFactoryDec, input);
63
+ }
64
+ export function encode(image, options) {
65
+ return encodeES("JXL Encode", encoderWASM, defaultOptions, image, options);
66
+ }
67
+ export function decode(input) {
68
+ return check(decoderWASM.decode(input), "JXL Decode");
69
+ }
70
+ export function unloadDecoder() {
71
+ decoderWASM = undefined;
72
+ }
73
+ export function unloadEncoder() {
74
+ encoderWASM = undefined;
75
+ }
package/lib/png.js ADDED
@@ -0,0 +1,45 @@
1
+ import wasmFactory, { optimize, png_to_rgba, quantize } from "../dist/pngquant.js";
2
+ import { toBitDepth } from "./common.js";
3
+ export const defaultOptions = {
4
+ speed: 4,
5
+ quality: 75,
6
+ colors: 256,
7
+ dithering: 1,
8
+ level: 3,
9
+ interlace: false,
10
+ quantize: true,
11
+ bit_depth: 8,
12
+ };
13
+ export const bitDepth = [8, 16];
14
+ export const mimeType = "image/png";
15
+ export const extension = "png";
16
+ export const loadEncoder = (module_or_path) => wasmFactory({ module_or_path });
17
+ export const loadDecoder = loadEncoder;
18
+ /**
19
+ * Reduces the colors used in the image at a slight loss, using a combination
20
+ * of vector quantization algorithms.
21
+ *
22
+ * Can be used before other compression algorithm to boost compression ratio.
23
+ */
24
+ export function reduceColors(image, options) {
25
+ options = { ...defaultOptions, ...options };
26
+ const { data, width, height } = toBitDepth(image, 8);
27
+ return quantize(data, width, height, { ...defaultOptions, ...options });
28
+ }
29
+ export function encode(image, options) {
30
+ options = { ...defaultOptions, ...options };
31
+ if (options.quantize) {
32
+ image = toBitDepth(image, 8);
33
+ }
34
+ const { data, width, height, depth } = image;
35
+ options.bit_depth = depth;
36
+ return optimize(data, width, height, { ...defaultOptions, ...options });
37
+ }
38
+ export function decode(input) {
39
+ const [data, width, depth] = png_to_rgba(input);
40
+ let height = data.byteLength / width / 4;
41
+ if (depth === 16) {
42
+ height /= 2;
43
+ }
44
+ return _icodec_ImageData(data, width, height, depth);
45
+ }
package/lib/qoi.d.ts CHANGED
@@ -11,3 +11,4 @@ export declare function loadEncoder(input?: WasmSource): Promise<any>;
11
11
  export declare const loadDecoder: typeof loadEncoder;
12
12
  export declare function encode(image: ImageDataLike): Uint8Array<ArrayBufferLike>;
13
13
  export declare function decode(input: BufferSource): ImageData;
14
+ export declare function unloadCoder(): void;
package/lib/qoi.js ADDED
@@ -0,0 +1,22 @@
1
+ import wasmFactory from "../dist/qoi.js";
2
+ import { check, loadES } from "./common.js";
3
+ export const defaultOptions = undefined;
4
+ export const bitDepth = [8];
5
+ export const mimeType = "image/qoi";
6
+ export const extension = "qoi";
7
+ let codecWASM;
8
+ export async function loadEncoder(input) {
9
+ return codecWASM = await loadES(wasmFactory, input);
10
+ }
11
+ export const loadDecoder = loadEncoder;
12
+ export function encode(image) {
13
+ const { data, width, height } = image;
14
+ const result = codecWASM.encode(data, width, height, undefined);
15
+ return check(result, "QOI Encode");
16
+ }
17
+ export function decode(input) {
18
+ return check(codecWASM.decode(input), "QOI Decode");
19
+ }
20
+ export function unloadCoder() {
21
+ codecWASM = undefined;
22
+ }
package/lib/webp.d.ts CHANGED
@@ -1,15 +1,15 @@
1
- import { ImageDataLike, WasmSource } from "./common.js";
2
- export declare const enum Preprocess {
1
+ import { EnumValue, ImageDataLike, WasmSource } from "./common.js";
2
+ export declare enum Preprocess {
3
3
  None = 0,
4
4
  SegmentSmooth = 1,
5
5
  Dithering = 2
6
6
  }
7
- export declare const enum AlphaFiltering {
7
+ export declare enum AlphaFiltering {
8
8
  None = 0,
9
9
  Fast = 1,
10
10
  Best = 2
11
11
  }
12
- export declare const enum WebPPreset {
12
+ export declare enum WebPPreset {
13
13
  Default = 0,
14
14
  Picture = 1,
15
15
  Photo = 2,
@@ -110,7 +110,7 @@ export interface Options {
110
110
  *
111
111
  * @default Preprocess.None
112
112
  */
113
- preprocessing?: Preprocess;
113
+ preprocessing?: EnumValue<typeof Preprocess>;
114
114
  /**
115
115
  * Turns auto-filter on. This algorithm will spend additional time optimizing the
116
116
  * filtering strength to reach a well-balanced quality.
@@ -161,7 +161,7 @@ export interface Options {
161
161
  *
162
162
  * @default AlphaFiltering.Fast
163
163
  */
164
- alphaFiltering?: AlphaFiltering;
164
+ alphaFiltering?: EnumValue<typeof AlphaFiltering>;
165
165
  /**
166
166
  * Use more accurate and sharper RGB->YUV conversion if needed.
167
167
  * Note that this process is slower than the default conversion.
@@ -224,5 +224,7 @@ export declare const extension = "webp";
224
224
  export declare function loadEncoder(input?: WasmSource): Promise<any>;
225
225
  export declare function loadDecoder(input?: WasmSource): Promise<any>;
226
226
  export declare function encode(image: ImageDataLike, options?: Options): Uint8Array<ArrayBufferLike>;
227
- export declare function preset(config: Options, preset: WebPPreset): Options | null;
227
+ export declare function preset(config: Options, preset: EnumValue<typeof WebPPreset>): Options | null;
228
228
  export declare function decode(input: BufferSource): ImageData;
229
+ export declare function unloadDecoder(): void;
230
+ export declare function unloadEncoder(): void;
package/lib/webp.js ADDED
@@ -0,0 +1,80 @@
1
+ import wasmFactoryEnc from "../dist/webp-enc.js";
2
+ import wasmFactoryDec from "../dist/webp-dec.js";
3
+ import { check, encodeES, loadES } from "./common.js";
4
+ export var Preprocess;
5
+ (function (Preprocess) {
6
+ Preprocess[Preprocess["None"] = 0] = "None";
7
+ Preprocess[Preprocess["SegmentSmooth"] = 1] = "SegmentSmooth";
8
+ Preprocess[Preprocess["Dithering"] = 2] = "Dithering";
9
+ })(Preprocess || (Preprocess = {}));
10
+ export var AlphaFiltering;
11
+ (function (AlphaFiltering) {
12
+ AlphaFiltering[AlphaFiltering["None"] = 0] = "None";
13
+ AlphaFiltering[AlphaFiltering["Fast"] = 1] = "Fast";
14
+ AlphaFiltering[AlphaFiltering["Best"] = 2] = "Best";
15
+ })(AlphaFiltering || (AlphaFiltering = {}));
16
+ export var WebPPreset;
17
+ (function (WebPPreset) {
18
+ WebPPreset[WebPPreset["Default"] = 0] = "Default";
19
+ WebPPreset[WebPPreset["Picture"] = 1] = "Picture";
20
+ WebPPreset[WebPPreset["Photo"] = 2] = "Photo";
21
+ WebPPreset[WebPPreset["Drawing"] = 3] = "Drawing";
22
+ WebPPreset[WebPPreset["Icon"] = 4] = "Icon";
23
+ WebPPreset[WebPPreset["Text"] = 5] = "Text";
24
+ })(WebPPreset || (WebPPreset = {}));
25
+ export const defaultOptions = {
26
+ lossless: false,
27
+ nearLossless: 100,
28
+ quality: 75,
29
+ targetSize: 0,
30
+ targetPSNR: 0,
31
+ method: 4,
32
+ snsStrength: 50,
33
+ filterStrength: 60,
34
+ filterSharpness: 0,
35
+ filterType: true,
36
+ segments: 4,
37
+ pass: 1,
38
+ sharpYUV: false,
39
+ preprocessing: Preprocess.None,
40
+ autofilter: false,
41
+ partitionLimit: 0,
42
+ alphaCompression: 1,
43
+ alphaFiltering: AlphaFiltering.Fast,
44
+ alphaQuality: 100,
45
+ exact: false,
46
+ emulateJpegSize: false,
47
+ lowMemory: false,
48
+ // Undocumented options, only for compatibility.
49
+ partitions: 0,
50
+ showCompressed: 0,
51
+ imageHint: 0,
52
+ threadLevel: 0,
53
+ useDeltaPalette: 0,
54
+ };
55
+ export const bitDepth = [8];
56
+ export const mimeType = "image/webp";
57
+ export const extension = "webp";
58
+ let encoderWASM;
59
+ let decoderWASM;
60
+ export async function loadEncoder(input) {
61
+ return encoderWASM ??= await loadES(wasmFactoryEnc, input);
62
+ }
63
+ export async function loadDecoder(input) {
64
+ return decoderWASM ??= await loadES(wasmFactoryDec, input);
65
+ }
66
+ export function encode(image, options) {
67
+ return encodeES("Webp Encode", encoderWASM, defaultOptions, image, options);
68
+ }
69
+ export function preset(config, preset) {
70
+ return encoderWASM.WebPConfigPreset(config, preset);
71
+ }
72
+ export function decode(input) {
73
+ return check(decoderWASM.decode(input), "Webp Decode");
74
+ }
75
+ export function unloadDecoder() {
76
+ decoderWASM = undefined;
77
+ }
78
+ export function unloadEncoder() {
79
+ encoderWASM = undefined;
80
+ }
package/lib/wp2.d.ts CHANGED
@@ -1,11 +1,11 @@
1
- import { ImageDataLike, WasmSource } from "./common.js";
2
- export declare const enum UVMode {
1
+ import { EnumValue, ImageDataLike, WasmSource } from "./common.js";
2
+ export declare enum UVMode {
3
3
  UVAdapt = 0,
4
4
  UV420 = 1,
5
5
  UV444 = 2,
6
6
  UVAuto = 3
7
7
  }
8
- export declare const enum Csp {
8
+ export declare enum Csp {
9
9
  YCoCg = 0,
10
10
  YCbCr = 1,
11
11
  Custom = 2,
@@ -51,8 +51,8 @@ export interface Options {
51
51
  * @default 50.0
52
52
  */
53
53
  sns?: number;
54
- uvMode?: UVMode;
55
- cspType?: Csp;
54
+ uvMode?: EnumValue<typeof UVMode>;
55
+ cspType?: EnumValue<typeof Csp>;
56
56
  /**
57
57
  * error diffusion strength [0=off, 100=max]
58
58
  *
@@ -69,3 +69,5 @@ export declare function loadEncoder(input?: WasmSource): Promise<any>;
69
69
  export declare function loadDecoder(input?: WasmSource): Promise<any>;
70
70
  export declare function encode(image: ImageDataLike, options?: Options): Uint8Array<ArrayBufferLike>;
71
71
  export declare function decode(input: BufferSource): ImageData;
72
+ export declare function unloadDecoder(): void;
73
+ export declare function unloadEncoder(): void;
package/lib/wp2.js ADDED
@@ -0,0 +1,52 @@
1
+ import { check, encodeES, loadES } from "./common.js";
2
+ import wasmFactoryEnc from "../dist/wp2-enc.js";
3
+ import wasmFactoryDec from "../dist/wp2-dec.js";
4
+ export var UVMode;
5
+ (function (UVMode) {
6
+ UVMode[UVMode["UVAdapt"] = 0] = "UVAdapt";
7
+ UVMode[UVMode["UV420"] = 1] = "UV420";
8
+ UVMode[UVMode["UV444"] = 2] = "UV444";
9
+ UVMode[UVMode["UVAuto"] = 3] = "UVAuto";
10
+ })(UVMode || (UVMode = {}));
11
+ export var Csp;
12
+ (function (Csp) {
13
+ Csp[Csp["YCoCg"] = 0] = "YCoCg";
14
+ Csp[Csp["YCbCr"] = 1] = "YCbCr";
15
+ Csp[Csp["Custom"] = 2] = "Custom";
16
+ Csp[Csp["YIQ"] = 3] = "YIQ";
17
+ })(Csp || (Csp = {}));
18
+ export const defaultOptions = {
19
+ quality: 75,
20
+ alphaQuality: 100,
21
+ effort: 5,
22
+ pass: 1,
23
+ sns: 50,
24
+ uvMode: UVMode.UVAuto,
25
+ cspType: Csp.YCoCg,
26
+ errorDiffusion: 0,
27
+ useRandomMatrix: false,
28
+ };
29
+ export const bitDepth = [8];
30
+ // WebP 2 will not be released as an image format, but wee need define these properties.
31
+ export const mimeType = "image/webp2";
32
+ export const extension = "wp2";
33
+ let encoderWASM;
34
+ let decoderWASM;
35
+ export async function loadEncoder(input) {
36
+ return encoderWASM ??= await loadES(wasmFactoryEnc, input);
37
+ }
38
+ export async function loadDecoder(input) {
39
+ return decoderWASM ??= await loadES(wasmFactoryDec, input);
40
+ }
41
+ export function encode(image, options) {
42
+ return encodeES("Webp2 Encode", encoderWASM, defaultOptions, image, options);
43
+ }
44
+ export function decode(input) {
45
+ return check(decoderWASM.decode(input), "Webp2 Decode");
46
+ }
47
+ export function unloadDecoder() {
48
+ decoderWASM = undefined;
49
+ }
50
+ export function unloadEncoder() {
51
+ encoderWASM = undefined;
52
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbk20191/icodec",
3
- "version": "0.6.5",
3
+ "version": "0.6.7",
4
4
  "private": false,
5
5
  "description": "Image encoders & decoders built with WebAssembly",
6
6
  "license": "MIT",
@@ -12,7 +12,7 @@
12
12
  "type": "module",
13
13
  "scripts": {
14
14
  "wasm": "node scripts/build.js",
15
- "types": "tsc --emitDeclarationOnly -p tsconfig.json",
15
+ "types": "tsc -p tsconfig.json --emitDeclarationOnly && tsc -p tsconfig.json",
16
16
  "build": "npm run wasm && npm run types",
17
17
  "test-convert": "tsx test/test-convert.js",
18
18
  "demo": "node scripts/start-demo.js"