@pbk20191/icodec 0.6.5 → 0.6.6
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/dist/pngquant_bg.wasm +0 -0
- package/lib/avif.d.ts +3 -3
- package/lib/avif.js +47 -0
- package/lib/common.d.ts +1 -0
- package/lib/common.js +64 -0
- package/lib/heic.js +43 -0
- package/lib/index.js +19 -0
- package/lib/jpeg.d.ts +3 -3
- package/lib/jpeg.js +34 -0
- package/lib/jxl.d.ts +4 -4
- package/lib/jxl.js +69 -0
- package/lib/png.js +45 -0
- package/lib/qoi.js +19 -0
- package/lib/webp.d.ts +7 -7
- package/lib/webp.js +74 -0
- package/lib/wp2.d.ts +5 -5
- package/lib/wp2.js +46 -0
- package/package.json +2 -2
package/dist/pngquant_bg.wasm
CHANGED
|
Binary file
|
package/lib/avif.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 enum Subsampling {
|
|
3
3
|
YUV444 = 1,
|
|
4
4
|
YUV422 = 2,
|
|
@@ -35,7 +35,7 @@ export interface Options {
|
|
|
35
35
|
*
|
|
36
36
|
* @default YUV420
|
|
37
37
|
*/
|
|
38
|
-
subsample?: Subsampling
|
|
38
|
+
subsample?: EnumValue<typeof Subsampling>;
|
|
39
39
|
/**
|
|
40
40
|
* If true, ignores `tileRowsLog2` and `tileColsLog2` and automatically chooses suitable tiling values.
|
|
41
41
|
*
|
|
@@ -72,7 +72,7 @@ export interface Options {
|
|
|
72
72
|
*
|
|
73
73
|
* @default AVIFTune.Auto
|
|
74
74
|
*/
|
|
75
|
-
tune?: AVIFTune
|
|
75
|
+
tune?: EnumValue<typeof AVIFTune>;
|
|
76
76
|
/**
|
|
77
77
|
* Use libsharpyuv for RGB->YUV conversion if needed.
|
|
78
78
|
*
|
package/lib/avif.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import wasmFactoryEnc from "../dist/avif-enc.js";
|
|
2
|
+
import wasmFactoryDec from "../dist/avif-dec.js";
|
|
3
|
+
import { check, encodeES, loadES } from "./common.js";
|
|
4
|
+
export var Subsampling;
|
|
5
|
+
(function (Subsampling) {
|
|
6
|
+
Subsampling[Subsampling["YUV444"] = 1] = "YUV444";
|
|
7
|
+
Subsampling[Subsampling["YUV422"] = 2] = "YUV422";
|
|
8
|
+
Subsampling[Subsampling["YUV420"] = 3] = "YUV420";
|
|
9
|
+
Subsampling[Subsampling["YUV400"] = 4] = "YUV400";
|
|
10
|
+
})(Subsampling || (Subsampling = {}));
|
|
11
|
+
export var AVIFTune;
|
|
12
|
+
(function (AVIFTune) {
|
|
13
|
+
AVIFTune[AVIFTune["Auto"] = 0] = "Auto";
|
|
14
|
+
AVIFTune[AVIFTune["PSNR"] = 1] = "PSNR";
|
|
15
|
+
AVIFTune[AVIFTune["SSIM"] = 2] = "SSIM";
|
|
16
|
+
})(AVIFTune || (AVIFTune = {}));
|
|
17
|
+
export const defaultOptions = {
|
|
18
|
+
quality: 50,
|
|
19
|
+
qualityAlpha: -1,
|
|
20
|
+
speed: 6,
|
|
21
|
+
subsample: Subsampling.YUV420,
|
|
22
|
+
autoTiling: false,
|
|
23
|
+
tileColsLog2: 0,
|
|
24
|
+
tileRowsLog2: 0,
|
|
25
|
+
chromaDeltaQ: false,
|
|
26
|
+
sharpness: 0,
|
|
27
|
+
denoiseLevel: 0,
|
|
28
|
+
tune: AVIFTune.Auto,
|
|
29
|
+
sharpYUV: false,
|
|
30
|
+
};
|
|
31
|
+
export const mimeType = "image/avif";
|
|
32
|
+
export const extension = "avif";
|
|
33
|
+
export const bitDepth = [8, 10, 12, 16];
|
|
34
|
+
let encoderWASM;
|
|
35
|
+
let decoderWASM;
|
|
36
|
+
export async function loadEncoder(input) {
|
|
37
|
+
return encoderWASM ??= await loadES(wasmFactoryEnc, input);
|
|
38
|
+
}
|
|
39
|
+
export async function loadDecoder(input) {
|
|
40
|
+
return decoderWASM ??= await loadES(wasmFactoryDec, input);
|
|
41
|
+
}
|
|
42
|
+
export function encode(image, options) {
|
|
43
|
+
return encodeES("AVIF Encode", encoderWASM, defaultOptions, image, options);
|
|
44
|
+
}
|
|
45
|
+
export function decode(input) {
|
|
46
|
+
return check(decoderWASM.decode(input), "AVIF Decode");
|
|
47
|
+
}
|
package/lib/common.d.ts
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* - If is BufferSource, it will be treated as the WASM bytes.
|
|
6
6
|
*/
|
|
7
7
|
export type WasmSource = string | BufferSource;
|
|
8
|
+
export type EnumValue<E> = E[Extract<keyof E, string>];
|
|
8
9
|
export declare function loadES(factory: any, source?: WasmSource): any;
|
|
9
10
|
export interface ImageDataLike {
|
|
10
11
|
width: number;
|
package/lib/common.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export function loadES(factory, source) {
|
|
2
|
+
return typeof source === "string"
|
|
3
|
+
? factory({ locateFile: () => source })
|
|
4
|
+
: factory({ wasmBinary: source });
|
|
5
|
+
}
|
|
6
|
+
export function toBitDepth(image, value) {
|
|
7
|
+
const { data, width, height, depth = 8 } = image;
|
|
8
|
+
if (value === depth) {
|
|
9
|
+
return image;
|
|
10
|
+
}
|
|
11
|
+
const pixels = width * height * 4;
|
|
12
|
+
const dist = value === 8
|
|
13
|
+
? new Uint8ClampedArray(pixels)
|
|
14
|
+
: new Uint16Array(pixels);
|
|
15
|
+
const view = depth === 8
|
|
16
|
+
? data
|
|
17
|
+
: new Uint16Array(data.buffer, data.byteOffset);
|
|
18
|
+
const from = (1 << depth) - 1;
|
|
19
|
+
const to = (1 << value) - 1;
|
|
20
|
+
for (let i = 0; i < pixels; i++) {
|
|
21
|
+
dist[i] = view[i] / from * to + 0.5;
|
|
22
|
+
}
|
|
23
|
+
const nd = new Uint8ClampedArray(dist.buffer, dist.byteOffset, dist.byteLength);
|
|
24
|
+
return _icodec_ImageData(nd, width, height, value);
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Node does not have `ImageData` class, so we define a pure version.
|
|
28
|
+
*/
|
|
29
|
+
export class PureImageData {
|
|
30
|
+
data;
|
|
31
|
+
width;
|
|
32
|
+
height;
|
|
33
|
+
depth;
|
|
34
|
+
constructor(data, width, height, depth = 8) {
|
|
35
|
+
this.data = data;
|
|
36
|
+
this.width = width;
|
|
37
|
+
this.height = height;
|
|
38
|
+
this.depth = depth;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// @ts-expect-error
|
|
42
|
+
PureImageData.prototype.colorSpace = "srgb";
|
|
43
|
+
export function encodeES(name, wasm, defaults, image, options) {
|
|
44
|
+
options = { ...defaults, ...options };
|
|
45
|
+
const { data, width, height } = image;
|
|
46
|
+
options.bitDepth = image.depth ?? 8;
|
|
47
|
+
const result = wasm.encode(data, width, height, options);
|
|
48
|
+
if (result instanceof Error) {
|
|
49
|
+
throw result;
|
|
50
|
+
}
|
|
51
|
+
return check(result, name);
|
|
52
|
+
}
|
|
53
|
+
export function check(value, hint) {
|
|
54
|
+
if (typeof value === "string") {
|
|
55
|
+
throw new Error(`${hint}: ${value}`);
|
|
56
|
+
}
|
|
57
|
+
if (value instanceof Error) {
|
|
58
|
+
throw value;
|
|
59
|
+
}
|
|
60
|
+
if (value)
|
|
61
|
+
return value;
|
|
62
|
+
else
|
|
63
|
+
throw new Error(hint + " failed");
|
|
64
|
+
}
|
package/lib/heic.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import wasmFactoryEnc from "../dist/heic-enc.js";
|
|
2
|
+
import wasmFactoryDec from "../dist/heic-dec.js";
|
|
3
|
+
import { check, encodeES, loadES } from "./common.js";
|
|
4
|
+
export const Presets = ["ultrafast", "superfast", "veryfast", "faster", "fast", "medium", "slow", "slower", "veryslow", "placebo"];
|
|
5
|
+
export const Subsampling = ["420", "422", "444"];
|
|
6
|
+
export const Tune = ["psnr", "ssim", "grain", "fastdecode"];
|
|
7
|
+
export const defaultOptions = {
|
|
8
|
+
quality: 50,
|
|
9
|
+
lossless: false,
|
|
10
|
+
preset: "slow",
|
|
11
|
+
tune: "ssim",
|
|
12
|
+
tuIntraDepth: 2,
|
|
13
|
+
complexity: 50,
|
|
14
|
+
chroma: "420",
|
|
15
|
+
sharpYUV: false,
|
|
16
|
+
};
|
|
17
|
+
export const mimeType = "image/heic";
|
|
18
|
+
export const extension = "heic";
|
|
19
|
+
export const bitDepth = [8, 10, 12];
|
|
20
|
+
let encoderWASM;
|
|
21
|
+
let decoderWASM;
|
|
22
|
+
export async function loadEncoder(input) {
|
|
23
|
+
return encoderWASM ??= await loadES(wasmFactoryEnc, input);
|
|
24
|
+
}
|
|
25
|
+
export async function loadDecoder(input) {
|
|
26
|
+
return decoderWASM ??= await loadES(wasmFactoryDec, input);
|
|
27
|
+
}
|
|
28
|
+
export function encode(image, options) {
|
|
29
|
+
const new_encode = (...args) => {
|
|
30
|
+
const holder = {};
|
|
31
|
+
encoderWASM.encode(...args, holder);
|
|
32
|
+
if (holder.error) {
|
|
33
|
+
return holder.error;
|
|
34
|
+
}
|
|
35
|
+
if (holder.success) {
|
|
36
|
+
return holder.success;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return encodeES("HEIC Encode", { encode: new_encode }, defaultOptions, image, options);
|
|
40
|
+
}
|
|
41
|
+
export function decode(input) {
|
|
42
|
+
return check(decoderWASM.decode(input), "HEIC Decode");
|
|
43
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PureImageData, toBitDepth } from "./common.js";
|
|
2
|
+
export { toBitDepth };
|
|
3
|
+
export * as avif from "./avif.js";
|
|
4
|
+
export * as png from "./png.js";
|
|
5
|
+
export * as jpeg from "./jpeg.js";
|
|
6
|
+
export * as jxl from "./jxl.js";
|
|
7
|
+
export * as webp from "./webp.js";
|
|
8
|
+
export * as heic from "./heic.js";
|
|
9
|
+
export * as qoi from "./qoi.js";
|
|
10
|
+
export * as wp2 from "./wp2.js";
|
|
11
|
+
globalThis._icodec_ImageData = (data, w, h, depth) => {
|
|
12
|
+
if (depth === 8) {
|
|
13
|
+
return new ImageDataEx(data, w, h);
|
|
14
|
+
}
|
|
15
|
+
return new PureImageData(data, w, h, depth);
|
|
16
|
+
};
|
|
17
|
+
class ImageDataEx extends ImageData {
|
|
18
|
+
depth = 8;
|
|
19
|
+
}
|
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
|
|
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
|
|
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";
|
package/lib/jxl.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
}
|
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.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
}
|
package/lib/webp.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { ImageDataLike, WasmSource } from "./common.js";
|
|
2
|
-
export declare
|
|
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
|
|
7
|
+
export declare enum AlphaFiltering {
|
|
8
8
|
None = 0,
|
|
9
9
|
Fast = 1,
|
|
10
10
|
Best = 2
|
|
11
11
|
}
|
|
12
|
-
export declare
|
|
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,5 @@ 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;
|
package/lib/webp.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
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
|
+
}
|
package/lib/wp2.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { ImageDataLike, WasmSource } from "./common.js";
|
|
2
|
-
export declare
|
|
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
|
|
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
|
*
|
package/lib/wp2.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pbk20191/icodec",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.6",
|
|
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"
|