@graysonlang/slim-webp-enc 1.0.0 → 1.1.0

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/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # slim-webp-enc
2
2
 
3
- A pure-TypeScript lossy WebP encoder with alpha support, purpose-built for **thumbnail generation** — no WASM, no loader complexity, zero runtime dependencies. Motivated by WebKit/Safari's lack of native `canvas.toBlob('image/webp')` support.
3
+ A pure-TypeScript lossy WebP encoder with alpha support — no WASM, no loader complexity, zero runtime dependencies. Motivated by WebKit/Safari's lack of native `canvas.toBlob('image/webp')` support. Not a full libwebp replacement (see the non-goals below), but for typical images it encodes in milliseconds at sizes and quality comparable to the browsers' native encoders.
4
4
 
5
- - **32 KB minified** (10.9 KB gzip) vs ~200–300 KB for a WASM libwebp build
6
- - Single-digit-millisecond encodes at thumbnail sizes (256² ≈ 4–6 ms)
7
- - Semi-lossy alpha (16-level reduction, 0/255 pinned exactly) compressed with a built-in mini-VP8L coder — typically **smaller total files than `cwebp -m 0`** on alpha-carrying thumbnails
5
+ - **33 KB minified** (11.5 KB gzip) vs ~200–300 KB for a WASM libwebp build
6
+ - Single-digit-millisecond encodes at common sizes (256² ≈ 4–6 ms)
7
+ - Semi-lossy alpha (16-level reduction, 0/255 pinned exactly) compressed with a built-in mini-VP8L coder — typically **smaller total files than `cwebp -m 0`** on alpha-carrying images
8
8
 
9
9
  ## Usage
10
10
 
@@ -27,7 +27,7 @@ if (!(await hasNativeWebPEncoder())) {
27
27
 
28
28
  | Option | Default | Notes |
29
29
  |---|---|---|
30
- | `quality` | `80` | 0–100, same scale as cwebp; default matches the browsers' native WebP encoder default |
30
+ | `quality` | `80` | 0–100, same scale as cwebp; default matches the browsers' native WebP encoder default. Follows the HTML canvas rule for invalid values: out-of-range, NaN, and ±Infinity encode at the default, as if omitted |
31
31
  | `alphaLevels` | `16` | 8 / 16 / 32 semi-lossy alpha levels; 0 and 255 always exact |
32
32
  | `alphaDither` | `1` | Ordered-dither strength 0–1 for the alpha level reduction. Hides banding on smooth alpha gradients at some alpha-payload cost; exact 0/255 pixels never dither. `0` disables |
33
33
  | `alphaAdaptive` | `true` | Pick alpha level values by Lloyd-Max (like libwebp's `-alpha_q`) instead of a uniform grid. Min/max stay exact; masks with ≤ `alphaLevels` distinct values pass through losslessly |
@@ -37,7 +37,7 @@ if (!(await hasNativeWebPEncoder())) {
37
37
 
38
38
  ## Scope
39
39
 
40
- Encodes a single lossy VP8 keyframe (16×16 intra modes, single segment, flat quantization, adaptive token probabilities) in a RIFF/VP8X/ALPH container. Alpha is level-reduced, prediction-filtered, and coded as a minimal VP8L stream (ALPH method 1). Flat-color images (≤ 256 distinct colors) are also tried as palette-based lossless VP8L and the smaller file is kept — `quality` is a floor, never a ceiling. Non-goals: animation, metadata, general-purpose lossless RGB (only the palette subset above), decode, rate control. Files run roughly 10–25 % larger than `cwebp -m 0` for opaque RGB at matched SSIM; alpha-heavy thumbnails usually come out *smaller* thanks to the semi-lossy alpha strategy.
40
+ Encodes a single lossy VP8 keyframe (16×16 intra modes, single segment, flat quantization, adaptive token probabilities) in a RIFF/VP8X/ALPH container. RGB→YUV conversion matches libwebp's import pipeline — gamma-corrected chroma averaging, alpha-weighted on mixed-transparency blocks, and block-local luma smoothing under the mask — so alpha-edge chroma matches what browsers' native encoders produce instead of washing out. Alpha is level-reduced, prediction-filtered, and coded as a minimal VP8L stream (ALPH method 1). Flat-color images (≤ 256 distinct colors) are also tried as palette-based lossless VP8L and the smaller file is kept — `quality` is a floor, never a ceiling. Non-goals: animation, metadata, general-purpose lossless RGB (only the palette subset above), decode, rate control. Files run roughly 10–25 % larger than `cwebp -m 0` for opaque RGB at matched SSIM; alpha-heavy images usually come out *smaller* thanks to the semi-lossy alpha strategy.
41
41
 
42
42
  ## Performance
43
43
 
@@ -52,7 +52,7 @@ Median encode times in headless Chrome (Apple Silicon, q80; `node harness/bench-
52
52
  | sprite (lossless path) | 256² | 4.5 ms | 2.3 ms | 1.1 ms |
53
53
  | sprite (lossless path) | 512² | 17.3 ms | 7.8 ms | 3.6 ms |
54
54
 
55
- Per encode: 1–2× native (faster than native on 512² alpha content), and 3–5× slower than WASM libwebp. The trade is the payload: WASM costs a **337 KB download plus ~24 ms compile/init** before the first encode, vs this library's 32 KB bundle with none — and our output was smaller than both in nearly every cell (sprite 512²: 1.6 KB vs 7–10 KB, thanks to the lossless path). Benchmark content is the harness corpus itself (`harness/content.ts`), so these rows measure the same images the quality metrics do. For a page encoding a handful of thumbnails, the WASM speed advantage never pays back its startup cost; sustained bulk encoding is where WASM wins.
55
+ Per encode: 1–2× native (faster than native on 512² alpha content), and 3–5× slower than WASM libwebp. The trade is the payload: WASM costs a **337 KB download plus ~24 ms compile/init** before the first encode, vs this library's 33 KB bundle with none — and our output was smaller than both in nearly every cell (sprite 512²: 1.6 KB vs 7–10 KB, thanks to the lossless path). Benchmark content is the harness corpus itself (`harness/content.ts`), so these rows measure the same images the quality metrics do. For a page encoding a handful of images, the WASM speed advantage never pays back its startup cost; sustained bulk encoding is where WASM wins.
56
56
 
57
57
  ## Example app
58
58
 
package/dist/index.d.ts CHANGED
@@ -7,10 +7,15 @@ export interface ImageDataLike {
7
7
  }
8
8
  export interface EncodeOptions {
9
9
  /**
10
- * 0–100, like cwebp. Default 80, matching the browsers' native encoder
11
- * default. Note: unlike `canvas.toBlob('image/webp', 1.0)`, which browsers
12
- * special-case to fully lossless output, 100 here is still lossy (finest
13
- * quantization, 4:2:0 chroma) use `lossless` for pixel-exact output.
10
+ * 0–100, like cwebp. Default 80 when omitted, matching the browsers'
11
+ * native encoder default. Follows the HTML canvas encoding rule (scaled
12
+ * to 0–100): a quality outside the valid range including NaN and
13
+ * ±Infinity is treated as if it had not been given and encodes at the
14
+ * default, exactly like the native canvas encoders slim fills in for.
15
+ * Note: unlike `canvas.toBlob('image/webp', 1.0)`, which browsers
16
+ * special-case to fully lossless output, 100 here is still lossy
17
+ * (finest quantization, 4:2:0 chroma) — use `lossless` for pixel-exact
18
+ * output.
14
19
  */
15
20
  quality?: number;
16
21
  /**
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- var g2=Object.defineProperty;var B2=(t,e,n)=>e in t?g2(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var O=(t,e,n)=>B2(t,typeof e!="symbol"?e+"":e,n);var $=null;async function x2(){return $!==null?$:typeof document>"u"?($=!1,$):($=await new Promise(t=>{try{let e=document.createElement("canvas");e.width=1,e.height=1,e.toBlob(n=>t(n?.type==="image/webp"),"image/webp")}catch{t(!1)}}),$)}var s5=[[[[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128]],[[253,136,254,255,228,219,128,128,128,128,128],[189,129,242,255,227,213,255,219,128,128,128],[106,126,227,252,214,209,255,255,128,128,128]],[[1,98,248,255,236,226,255,255,128,128,128],[181,133,238,254,221,234,255,154,128,128,128],[78,134,202,247,198,180,255,219,128,128,128]],[[1,185,249,255,243,255,128,128,128,128,128],[184,150,247,255,236,224,128,128,128,128,128],[77,110,216,255,236,230,128,128,128,128,128]],[[1,101,251,255,241,255,128,128,128,128,128],[170,139,241,252,236,209,255,255,128,128,128],[37,116,196,243,228,255,255,255,128,128,128]],[[1,204,254,255,245,255,128,128,128,128,128],[207,160,250,255,238,128,128,128,128,128,128],[102,103,231,255,211,171,128,128,128,128,128]],[[1,152,252,255,240,255,128,128,128,128,128],[177,135,243,255,234,225,128,128,128,128,128],[80,129,211,255,194,224,128,128,128,128,128]],[[1,1,255,128,128,128,128,128,128,128,128],[246,1,255,128,128,128,128,128,128,128,128],[255,128,128,128,128,128,128,128,128,128,128]]],[[[198,35,237,223,193,187,162,160,145,155,62],[131,45,198,221,172,176,220,157,252,221,1],[68,47,146,208,149,167,221,162,255,223,128]],[[1,149,241,255,221,224,255,255,128,128,128],[184,141,234,253,222,220,255,199,128,128,128],[81,99,181,242,176,190,249,202,255,255,128]],[[1,129,232,253,214,197,242,196,255,255,128],[99,121,210,250,201,198,255,202,128,128,128],[23,91,163,242,170,187,247,210,255,255,128]],[[1,200,246,255,234,255,128,128,128,128,128],[109,178,241,255,231,245,255,255,128,128,128],[44,130,201,253,205,192,255,255,128,128,128]],[[1,132,239,251,219,209,255,165,128,128,128],[94,136,225,251,218,190,255,255,128,128,128],[22,100,174,245,186,161,255,199,128,128,128]],[[1,182,249,255,232,235,128,128,128,128,128],[124,143,241,255,227,234,128,128,128,128,128],[35,77,181,251,193,211,255,205,128,128,128]],[[1,157,247,255,236,231,255,255,128,128,128],[121,141,235,255,225,227,255,255,128,128,128],[45,99,188,251,195,217,255,224,128,128,128]],[[1,1,251,255,213,255,128,128,128,128,128],[203,1,248,255,255,128,128,128,128,128,128],[137,1,177,255,224,255,128,128,128,128,128]]],[[[253,9,248,251,207,208,255,192,128,128,128],[175,13,224,243,193,185,249,198,255,255,128],[73,17,171,221,161,179,236,167,255,234,128]],[[1,95,247,253,212,183,255,255,128,128,128],[239,90,244,250,211,209,255,255,128,128,128],[155,77,195,248,188,195,255,255,128,128,128]],[[1,24,239,251,218,219,255,205,128,128,128],[201,51,219,255,196,186,128,128,128,128,128],[69,46,190,239,201,218,255,228,128,128,128]],[[1,191,251,255,255,128,128,128,128,128,128],[223,165,249,255,213,255,128,128,128,128,128],[141,124,248,255,255,128,128,128,128,128,128]],[[1,16,248,255,255,128,128,128,128,128,128],[190,36,230,255,236,255,128,128,128,128,128],[149,1,255,128,128,128,128,128,128,128,128]],[[1,226,255,128,128,128,128,128,128,128,128],[247,192,255,128,128,128,128,128,128,128,128],[240,128,255,128,128,128,128,128,128,128,128]],[[1,134,252,255,255,128,128,128,128,128,128],[213,62,250,255,255,128,128,128,128,128,128],[55,93,255,128,128,128,128,128,128,128,128]],[[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128]]],[[[202,24,213,235,186,191,220,160,240,175,255],[126,38,182,232,169,184,228,174,255,187,128],[61,46,138,219,151,178,240,170,255,216,128]],[[1,112,230,250,199,191,247,159,255,255,128],[166,109,228,252,211,215,255,174,128,128,128],[39,77,162,232,172,180,245,178,255,255,128]],[[1,52,220,246,198,199,249,220,255,255,128],[124,74,191,243,183,193,250,221,255,255,128],[24,71,130,219,154,170,243,182,255,255,128]],[[1,182,225,249,219,240,255,224,128,128,128],[149,150,226,252,216,205,255,171,128,128,128],[28,108,170,242,183,194,254,223,255,255,128]],[[1,81,230,252,204,203,255,192,128,128,128],[123,102,209,247,188,196,255,233,128,128,128],[20,95,153,243,164,173,255,203,128,128,128]],[[1,222,248,255,216,213,128,128,128,128,128],[168,175,246,252,235,205,255,255,128,128,128],[47,116,215,255,211,212,255,255,128,128,128]],[[1,121,236,253,212,214,255,255,128,128,128],[141,84,213,252,201,202,255,219,128,128,128],[42,80,160,240,162,185,255,205,128,128,128]],[[1,1,255,128,128,128,128,128,128,128,128],[244,1,255,128,128,128,128,128,128,128,128],[238,1,255,128,128,128,128,128,128,128,128]]]],C5=[[[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[176,246,255,255,255,255,255,255,255,255,255],[223,241,252,255,255,255,255,255,255,255,255],[249,253,253,255,255,255,255,255,255,255,255]],[[255,244,252,255,255,255,255,255,255,255,255],[234,254,254,255,255,255,255,255,255,255,255],[253,255,255,255,255,255,255,255,255,255,255]],[[255,246,254,255,255,255,255,255,255,255,255],[239,253,254,255,255,255,255,255,255,255,255],[254,255,254,255,255,255,255,255,255,255,255]],[[255,248,254,255,255,255,255,255,255,255,255],[251,255,254,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[251,254,254,255,255,255,255,255,255,255,255],[254,255,254,255,255,255,255,255,255,255,255]],[[255,254,253,255,254,255,255,255,255,255,255],[250,255,254,255,254,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[217,255,255,255,255,255,255,255,255,255,255],[225,252,241,253,255,255,254,255,255,255,255],[234,250,241,250,253,255,253,254,255,255,255]],[[255,254,255,255,255,255,255,255,255,255,255],[223,254,254,255,255,255,255,255,255,255,255],[238,253,254,254,255,255,255,255,255,255,255]],[[255,248,254,255,255,255,255,255,255,255,255],[249,254,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,255,255,255,255,255,255,255,255,255],[247,254,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[252,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,254,255,255,255,255,255,255,255,255],[253,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,253,255,255,255,255,255,255,255,255],[250,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[186,251,250,255,255,255,255,255,255,255,255],[234,251,244,254,255,255,255,255,255,255,255],[251,251,243,253,254,255,254,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[236,253,254,255,255,255,255,255,255,255,255],[251,253,253,254,254,255,255,255,255,255,255]],[[255,254,254,255,255,255,255,255,255,255,255],[254,254,254,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,255,255,255,255,255,255,255,255,255],[254,254,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[248,255,255,255,255,255,255,255,255,255,255],[250,254,252,254,255,255,255,255,255,255,255],[248,254,249,253,255,255,255,255,255,255,255]],[[255,253,253,255,255,255,255,255,255,255,255],[246,253,253,255,255,255,255,255,255,255,255],[252,254,251,254,254,255,255,255,255,255,255]],[[255,254,252,255,255,255,255,255,255,255,255],[248,254,253,255,255,255,255,255,255,255,255],[253,255,254,254,255,255,255,255,255,255,255]],[[255,251,254,255,255,255,255,255,255,255,255],[245,251,254,255,255,255,255,255,255,255,255],[253,253,254,255,255,255,255,255,255,255,255]],[[255,251,253,255,255,255,255,255,255,255,255],[252,253,254,255,255,255,255,255,255,255,255],[255,254,255,255,255,255,255,255,255,255,255]],[[255,252,255,255,255,255,255,255,255,255,255],[249,255,254,255,255,255,255,255,255,255,255],[255,255,254,255,255,255,255,255,255,255,255]],[[255,255,253,255,255,255,255,255,255,255,255],[250,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]]],u5=[4,5,6,7,8,9,10,10,11,12,13,14,15,16,17,17,18,19,20,20,21,21,22,22,23,23,24,25,25,26,27,28,29,30,31,32,33,34,35,36,37,37,38,39,40,41,42,43,44,45,46,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,76,77,78,79,80,81,82,83,84,85,86,87,88,89,91,93,95,96,98,100,101,102,104,106,108,110,112,114,116,118,122,124,126,128,130,132,134,136,138,140,143,145,148,151,154,157],K=[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,119,122,125,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,177,181,185,189,193,197,201,205,209,213,217,221,225,229,234,239,245,249,254,259,264,269,274,279,284],$5=[0,1,4,8,5,2,3,6,9,12,13,10,7,11,14,15],D=[0,1,2,3,6,4,5,6,6,6,6,6,6,6,6,7,0],G5=[173,148,140],Q5=[176,155,140,135],X5=[180,157,141,134,130],Z5=[254,254,243,230,196,177,153,140,133,130,129],P5=[96,73,55,39,23,13,5,1,255,255,255,255,255,255,255,255,101,78,58,42,26,16,8,2,0,3,9,17,27,43,59,79,102,86,62,46,32,20,10,6,4,7,11,21,33,47,63,87,105,90,70,52,37,28,18,14,12,15,19,29,38,53,71,91,110,99,82,66,48,35,30,24,22,25,31,36,49,67,83,100,115,108,94,76,64,50,44,40,34,41,45,51,65,77,95,109,118,113,103,92,80,68,60,56,54,57,61,69,81,93,104,114,119,116,111,106,97,88,84,74,72,75,85,89,98,107,112,117];var c5=class{constructor(){O(this,"buf",[]);O(this,"acc",0);O(this,"used",0)}putBits(e,n){for(this.acc|=e<<this.used,this.used+=n;this.used>=8;)this.buf.push(this.acc&255),this.acc>>>=8,this.used-=8}finish(){return this.used>0&&(this.buf.push(this.acc&255),this.acc=0,this.used=0),new Uint8Array(this.buf)}};function l5(t){if(t<=4)return{code:t-1,extraBits:0,extraValue:0};let e=t-1,n=31-Math.clz32(e),o=e>>n-1&1,s=n-1;return{code:2*n+o,extraBits:s,extraValue:e&(1<<s)-1}}function U2(t,e){let n=e/t|0,o=e-n*t;return o<=8&&n<8?P5[n*16+8-o]+1:o>t-8&&n<7?P5[(n+1)*16+8+(t-o)]+1:e+120}function G(t,e){let n=t.length,o=new Uint8Array(n),s=[];for(let r=0;r<n;r++)t[r]>0&&s.push(r);if(s.length===0)return o;if(s.length===1)return o[s[0]]=1,o;let i=Array.from(t);for(;;){let r=s.map(l=>({count:i[l],symbol:l,left:null,right:null})).sort((l,u)=>l.count-u.count);for(;r.length>1;){let l=r.shift(),u=r.shift(),m={count:l.count+u.count,symbol:-1,left:l,right:u},f=0,b=r.length;for(;f<b;){let x=f+b>>1;r[x].count<=m.count?f=x+1:b=x}r.splice(f,0,m)}let c=!1,a=(l,u)=>{if(l.symbol>=0){u>e&&(c=!0),o[l.symbol]=u;return}a(l.left,u+1),a(l.right,u+1)};if(a(r[0],0),!c)return o;i=i.map(l=>l>0?l+1>>1:0)}}function C2(t,e){let n=0;for(let o=0;o<e;o++)n=n<<1|t>>o&1;return n}function Q(t){let e=0;for(let n of t.lengths)n>0&&e++;return e!==1?t:{codes:t.codes,lengths:new Uint8Array(t.lengths.length)}}function X(t){let e=Math.max(0,...t),n=new Array(e+1).fill(0);for(let r of t)r>0&&n[r]++;let o=new Array(e+1).fill(0),s=0;for(let r=1;r<=e;r++)s=s+n[r-1]<<1,o[r]=s;let i=new Uint16Array(t.length);for(let r=0;r<t.length;r++)t[r]>0&&(i[r]=C2(o[t[r]]++,t[r]));return{codes:i,lengths:t}}var E5=[17,18,0,1,2,3,4,5,16,6,7,8,9,10,11,12,13,14,15];function j(t,e){let n=[];for(let l=0;l<e.length;l++)e[l]>0&&n.push(l);if(n.length===0){t.putBits(1,1),t.putBits(0,1),t.putBits(0,1),t.putBits(0,1);return}if(n.length<=2&&n[n.length-1]<256){t.putBits(1,1),t.putBits(n.length-1,1),n[0]<=1?(t.putBits(0,1),t.putBits(n[0],1)):(t.putBits(1,1),t.putBits(n[0],8)),n.length===2&&t.putBits(n[1],8);return}t.putBits(0,1);let o=[],s=8;for(let l=0;l<e.length;){let u=e[l],m=1;for(;l+m<e.length&&e[l+m]===u;)m++;if(u===0){let f=m;for(;f>=11;){let b=Math.min(f,138);o.push({symbol:18,extra:b-11,extraBits:7}),f-=b}for(f>=3&&(o.push({symbol:17,extra:f-3,extraBits:3}),f=0);f-- >0;)o.push({symbol:0,extra:0,extraBits:0})}else{let f=m;for(u!==s&&(o.push({symbol:u,extra:0,extraBits:0}),s=u,f--);f>=3;){let b=Math.min(f,6);o.push({symbol:16,extra:b-3,extraBits:2}),f-=b}for(;f-- >0;)o.push({symbol:u,extra:0,extraBits:0})}l+=m}let i=new Uint32Array(19);for(let l of o)i[l.symbol]++;let r=G(i,7),c=Q(X(r)),a=E5.length;for(;a>4&&r[E5[a-1]]===0;)a--;t.putBits(a-4,4);for(let l=0;l<a;l++)t.putBits(r[E5[l]],3);t.putBits(0,1);for(let l of o)t.putBits(c.codes[l.symbol],c.lengths[l.symbol]),l.extraBits>0&&t.putBits(l.extra,l.extraBits)}var P2=4,E2=4096;function L2(t,e){let n=[],o=t.length,s=0;for(;s<o;){let i=0,r=0;for(let c of s>=e?[1,e]:[1]){if(s<c)continue;let a=0,l=Math.min(E2,o-s);for(;a<l&&t[s+a]===t[s+a-c];)a++;a>i&&(i=a,r=c)}i>=P2?(n.push({length:i,distCode:U2(e,r)}),s+=i):(n.push({literal:t[s]}),s++)}return n}var L5=256,v2=24,I2=L5+v2;function v5(t,e,n,o){let s=L2(e,n),i=new Uint32Array(I2),r=new Uint32Array(256),c=new Uint32Array(256),a=new Uint32Array(256),l=new Uint32Array(40);for(let A of s)"literal"in A?(i[A.literal>>>8&255]++,r[A.literal>>>16&255]++,c[A.literal&255]++,a[A.literal>>>24]++):(i[L5+l5(A.length).code]++,l[l5(A.distCode).code]++);let u=G(i,15),m=G(r,15),f=G(c,15),b=G(a,15),x=G(l,15),g=Q(X(u)),p=Q(X(m)),h=Q(X(f)),C=Q(X(b)),v=Q(X(x));t.putBits(0,1),o&&t.putBits(0,1),j(t,u),j(t,m),j(t,f),j(t,b),j(t,x);let T=(A,_)=>{A.lengths[_]>0&&t.putBits(A.codes[_],A.lengths[_])};for(let A of s)if("literal"in A)T(g,A.literal>>>8&255),T(p,A.literal>>>16&255),T(h,A.literal&255),T(C,A.literal>>>24);else{let _=l5(A.length);T(g,L5+_.code),_.extraBits>0&&t.putBits(_.extraValue,_.extraBits);let H=l5(A.distCode);T(v,H.code),H.extraBits>0&&t.putBits(H.extraValue,H.extraBits)}}function K5(t,e,n){let o=new Uint32Array(t.length);for(let i=0;i<t.length;i++)o[i]=(4278190080|t[i]<<8)>>>0;let s=new c5;return s.putBits(0,1),v5(s,o,e,!0),s.finish()}function M2(t){return t<=2?3:t<=4?2:t<=16?1:0}function j5(t,e,n){let o=new Set,s=e*n,i=new Uint32Array(s);for(let p=0;p<s;p++){let h=(t[p*4+3]<<24|t[p*4]<<16|t[p*4+1]<<8|t[p*4+2])>>>0;if(i[p]=h,o.add(h),o.size>256)return null}let r=[...o].sort((p,h)=>p-h),c=new Map;r.forEach((p,h)=>{c.set(p,h)});let a=M2(r.length),l=8>>a,u=e+(1<<a)-1>>a,m=new Uint32Array(u*n);m.fill(4278190080);for(let p=0;p<n;p++)for(let h=0;h<e;h++){let C=c.get(i[p*e+h]),v=p*u+(h>>a);m[v]=(m[v]|C<<l*(h&(1<<a)-1)+8)>>>0}let f=!1;for(let p of r)if(p>>>24!==255){f=!0;break}let b=new c5;b.putBits(47,8),b.putBits(e-1,14),b.putBits(n-1,14),b.putBits(f?1:0,1),b.putBits(0,3),b.putBits(1,1),b.putBits(3,2),b.putBits(r.length-1,8);let x=new Uint32Array(r.length),g=0;for(let p=0;p<r.length;p++){let h=r[p];x[p]=((h>>>24)-(g>>>24)&255)*16777216+(((h>>>16)-(g>>>16)&255)<<16)+(((h>>>8)-(g>>>8)&255)<<8)+(h-g&255)>>>0,g=h}return v5(b,x,r.length,!1),b.putBits(0,1),v5(b,m,u,!0),b.finish()}var z5=[0,8,2,10,12,4,14,6,3,11,1,9,15,7,13,5];function w2(t,e,n=16,o=1){let s=255/(n-1),i=new Uint8Array(t.length);if(o<=0){let c=new Uint8Array(256);for(let a=0;a<256;a++)c[a]=Math.round(Math.round(a/s)*s);for(let a=0;a<t.length;a++)i[a]=c[t[a]];return i}let r=o>1?1:o;for(let c=0;c<t.length;c++){let a=c%e,l=c/e|0,u=((z5[(l&3)*4+(a&3)]+.5)/16-.5)*r,m=Math.round(t[c]/s+u),f=Math.round(m*s);i[c]=f<0?0:f>255?255:f}return i}function F2(t,e,n=16,o=1){let s=new Uint32Array(256),i=255,r=0,c=0;for(let p=0;p<t.length;p++){let h=t[p];s[h]===0&&c++,s[h]++,h<i&&(i=h),h>r&&(r=h)}if(c<=n)return t.slice();let a=new Float64Array(n);for(let p=0;p<n;p++)a[p]=i+(r-i)*p/(n-1);let l=1/0,u=new Uint8Array(256);for(let p=0;p<6;p++){let h=new Float64Array(n),C=new Float64Array(n),v=0;for(let A=i;A<=r;A++){for(;v<n-1&&2*A>a[v]+a[v+1];)v++;u[A]=v,h[v]+=A*s[A],C[v]+=s[A]}for(let A=1;A<n-1;A++)C[A]>0&&(a[A]=h[A]/C[A]);let T=0;for(let A=i;A<=r;A++){let _=A-a[u[A]];T+=s[A]*_*_}if(l-T<1e-4*t.length)break;l=T}let m=new Uint8Array(n);for(let p=0;p<n;p++)m[p]=Math.round(a[p]);m[0]=i,m[n-1]=r;let f=new Uint8Array(256),b=new Float64Array(256);{let p=0;for(let h=i;h<=r;h++){for(;p<n-2&&h>=m[p+1];)p++;f[h]=p;let C=m[p+1]-m[p];b[h]=C>0?(h-m[p])/C:0}}let x=o<=0?0:o>1?1:o,g=new Uint8Array(t.length);for(let p=0;p<t.length;p++){let h=t[p],C;if(x>0){let v=p%e,T=p/e|0,A=((z5[(T&3)*4+(v&3)]+.5)/16-.5)*x;C=f[h]+Math.round(b[h]+A)}else C=f[h]+Math.round(b[h]);g[p]=m[C<0?0:C>=n?n-1:C]}return g}var a5=0,J5=1,t2=2,R2=3;function T2(t){return t<0?0:t>255?255:t}function _2(t,e,n,o){if(o===a5)return t;let s=new Uint8Array(t.length);for(let i=0;i<n;i++)for(let r=0;r<e;r++){let c=i*e+r,a;r===0&&i===0?a=0:o===J5?a=r>0?t[c-1]:t[c-e]:o===t2?a=i>0?t[c-e]:t[c-1]:i===0?a=t[c-1]:r===0?a=t[c-e]:a=T2(t[c-1]+t[c-e]-t[c-e-1]),s[c]=t[c]-a&255}return s}function S2(t){let e=new Uint32Array(256),n=0;for(let s=0;s<t.length;s++)s>0&&t[s]===t[s-1]||(e[t[s]]++,n++);if(n===0)return 0;let o=0;for(let s=0;s<256;s++){if(e[s]===0)continue;let i=e[s]/n;o-=i*Math.log2(i)}return n*(o+2)}function n2(t,e,n,o=16,s=1,i=!0){let r=i?F2(t,e,o,s):w2(t,e,o,s),c=!0;for(let f=0;f<t.length;f++)if(r[f]!==t[f]){c=!1;break}let a=c?[{plane:t,preprocessing:0}]:[{plane:t,preprocessing:0},{plane:r,preprocessing:1}],l=null,u=a5,m=t;for(let{plane:f,preprocessing:b}of a){let x=[a5,J5,t2,R2].map(g=>({f:g,filtered:_2(f,e,n,g)})).map(g=>({...g,h:S2(g.filtered)})).sort((g,p)=>g.h-p.h).slice(0,2);for(let{f:g,filtered:p}of x){let h=K5(p,e,n);if(l===null||h.length+1<l.length){let C=new Uint8Array(1+h.length);C[0]=1|g<<2|b<<4,C.set(h,1),l=C,u=g,m=f}}}if(1+t.length<l.length){let f=new Uint8Array(1+t.length);return f[0]=0,f.set(t,1),{payload:f,filter:a5,quantized:t}}return{payload:l,filter:u,quantized:m}}function e2(t){let e=t.length/4,n=new Uint8Array(e);for(let o=0;o<e;o++)n[o]=t[o*4+3];return n}function z(t){return[t.charCodeAt(0),t.charCodeAt(1),t.charCodeAt(2),t.charCodeAt(3)]}function I5(t){return[t&255,t>>>8&255,t>>>16&255,t>>>24&255]}function r2(t){return[t&255,t>>>8&255,t>>>16&255]}function f5(t,e){let n=e.length+(e.length&1),o=new Uint8Array(8+n);return o.set(z(t),0),o.set(I5(e.length),4),o.set(e,8),o}function M5(t){let e=[];if(t.alph){let i=new Uint8Array(10);i[0]=16,i.set(r2(t.width-1),4),i.set(r2(t.height-1),7),e.push(f5("VP8X",i)),e.push(f5("ALPH",t.alph))}e.push(f5("VP8 ",t.vp8));let n=e.reduce((i,r)=>i+r.length,0),o=new Uint8Array(12+n);o.set(z("RIFF"),0),o.set(I5(4+n),4),o.set(z("WEBP"),8);let s=12;for(let i of e)o.set(i,s),s+=i.length;return o}function o2(t){let e=f5("VP8L",t),n=new Uint8Array(12+e.length);return n.set(z("RIFF"),0),n.set(I5(4+e.length),4),n.set(z("WEBP"),8),n.set(e,12),n}function k2(t,e,n){return 16839*t+33059*e+6420*n+32768+(16<<16)>>16}function i2(t){return t=t+32768+8388608>>16,t<0?0:t>255?255:t}function N2(t,e,n){return i2(-9719*t-19081*e+28800*n)}function V2(t,e,n){return i2(28800*t-24116*e-4684*n)}function s2(t,e,n){let o=e+15>>4,s=n+15>>4,i=o*16,r=o*8,c=new Uint8Array(i*s*16),a=new Uint8Array(r*s*8),l=new Uint8Array(r*s*8);for(let u=0;u<s*16;u++){let m=u<n?u:n-1;for(let f=0;f<i;f++){let b=f<e?f:e-1,x=(m*e+b)*4;c[u*i+f]=k2(t[x],t[x+1],t[x+2])}}for(let u=0;u<s*8;u++)for(let m=0;m<r;m++){let f=0,b=0,x=0;for(let g=0;g<2;g++)for(let p=0;p<2;p++){let h=m*2+p,C=u*2+g;h>=e&&(h=e-1),C>=n&&(C=n-1);let v=(C*e+h)*4;f+=t[v],b+=t[v+1],x+=t[v+2]}f=f+2>>2,b=b+2>>2,x=x+2>>2,a[u*r+m]=N2(f,b,x),l[u*r+m]=V2(f,b,x)}return{width:e,height:n,mbW:o,mbH:s,yStride:i,uvStride:r,y:c,u:a,v:l}}var O2=[7,6,6,5,5,5,5,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],u2=[127,127,191,127,159,191,223,127,143,159,175,191,207,223,239,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,247,127,131,135,139,143,147,151,155,159,163,167,171,175,179,183,187,191,195,199,203,207,211,215,219,223,227,231,235,239,243,247,251,127,129,131,133,135,137,139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,169,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199,201,203,205,207,209,211,213,215,217,219,221,223,225,227,229,231,233,235,237,239,241,243,245,247,249,251,253,127],J=class{constructor(){O(this,"range",254);O(this,"value",0);O(this,"run",0);O(this,"nbBits",-8);O(this,"buf",[])}flush(){let e=8+this.nbBits,n=this.value>>e;if(this.value-=n<<e,this.nbBits-=8,(n&255)!==255){if(n&256){let s=this.buf.length-1;s>=0&&this.buf[s]++}let o=n&256?0:255;for(;this.run>0;this.run--)this.buf.push(o);this.buf.push(n&255)}else this.run++}putBit(e,n){let o=e?1:0,s=this.range*n>>8;if(o?(this.value+=s+1,this.range-=s+1):this.range=s,this.range<127){let i=O2[this.range];this.range=u2[this.range],this.value<<=i,this.nbBits+=i,this.nbBits>0&&this.flush()}return!!o}putBitUniform(e){let n=e?1:0,o=this.range>>1;return n?(this.value+=o+1,this.range-=o+1):this.range=o,this.range<127&&(this.range=u2[this.range],this.value<<=1,this.nbBits+=1,this.nbBits>0&&this.flush()),!!n}putBits(e,n){for(let o=1<<n-1;o;o>>>=1)this.putBitUniform(e&o)}putSignedBits(e,n){this.putBitUniform(e!==0)&&(e<0?this.putBits(-e<<1|1,n+1):this.putBits(e<<1,n+1))}finish(){return this.putBits(0,9-this.nbBits),this.nbBits=0,this.flush(),new Uint8Array(this.buf)}};function m5(t){return(t*20091>>16)+t|0}function b5(t){return t*35468>>16|0}function p5(t){return t<0?0:t>255?255:t}var y=new Int32Array(16);function w5(t,e,n,o,s,i){for(let r=0;r<4;r++){let c=e+r*s,a=o+r*s,l=t[c]-n[a],u=t[c+1]-n[a+1],m=t[c+2]-n[a+2],f=t[c+3]-n[a+3],b=l+f,x=u+m,g=u-m,p=l-f;y[0+r*4]=(b+x)*8,y[1+r*4]=g*2217+p*5352+1812>>9,y[2+r*4]=(b-x)*8,y[3+r*4]=p*2217-g*5352+937>>9}for(let r=0;r<4;r++){let c=y[0+r]+y[12+r],a=y[4+r]+y[8+r],l=y[4+r]-y[8+r],u=y[0+r]-y[12+r];i[0+r]=c+a+7>>4,i[4+r]=(l*2217+u*5352+12e3>>16)+(u!==0?1:0),i[8+r]=c-a+7>>4,i[12+r]=u*2217-l*5352+51e3>>16}}function F5(t,e,n,o,s,i){for(let r=0;r<4;r++){let c=t[r]+t[8+r],a=t[r]-t[8+r],l=b5(t[4+r])-m5(t[12+r]),u=m5(t[4+r])+b5(t[12+r]);y[r*4+0]=c+u,y[r*4+1]=a+l,y[r*4+2]=a-l,y[r*4+3]=c-u}for(let r=0;r<4;r++){let c=y[0+r]+4,a=c+y[8+r],l=c-y[8+r],u=b5(y[4+r])-m5(y[12+r]),m=m5(y[4+r])+b5(y[12+r]),f=i+r*o,b=n+r*o;s[f]=p5(e[b]+(a+m>>3)),s[f+1]=p5(e[b+1]+(l+u>>3)),s[f+2]=p5(e[b+2]+(l-u>>3)),s[f+3]=p5(e[b+3]+(a-m>>3))}}function l2(t,e){for(let n=0;n<4;n++){let o=n*4,s=t[o]+t[o+2],i=t[o+1]+t[o+3],r=t[o+1]-t[o+3],c=t[o]-t[o+2];y[0+n*4]=s+i,y[1+n*4]=c+r,y[2+n*4]=c-r,y[3+n*4]=s-i}for(let n=0;n<4;n++){let o=y[0+n]+y[8+n],s=y[4+n]+y[12+n],i=y[4+n]-y[12+n],r=y[0+n]-y[8+n],c=o+s,a=r+i,l=r-i,u=o-s;e[0+n]=c>>1,e[4+n]=a>>1,e[8+n]=l>>1,e[12+n]=u>>1}}function c2(t,e){for(let n=0;n<4;n++){let o=t[0+n]+t[12+n],s=t[4+n]+t[8+n],i=t[4+n]-t[8+n],r=t[0+n]-t[12+n];y[0+n]=o+s,y[8+n]=o-s,y[4+n]=r+i,y[12+n]=r-i}for(let n=0;n<4;n++){let o=y[0+n*4]+3,s=o+y[3+n*4],i=y[1+n*4]+y[2+n*4],r=y[1+n*4]-y[2+n*4],c=o-y[3+n*4];e[n*4+0]=s+i>>3,e[n*4+1]=c+r>>3,e[n*4+2]=s-i>>3,e[n*4+3]=c-r>>3}}var a2=2047,y5=0,V5=1,O5=2,f2=3;function A5(t,e){let n=Math.round(t)|0;return n<0?0:n>e?e:n}function D2(t){let e=A5(t,127),n=K[e]*101581>>16;return{y1:{dc:u5[e],ac:K[e],dcBias:96,acBias:110},y2:{dc:u5[e]*2,ac:n<8?8:n,dcBias:96,acBias:108},uv:{dc:u5[A5(e,117)],ac:K[e],dcBias:110,acBias:115}}}function H2(t,e){let n=K[A5(t,127)]>>2,s=(n>63?63:n)*5*e/256|0;return s<1?0:s>63?63:s}function R5(t,e,n,o){let s=new Int16Array(16),i=-1;for(let r=n;r<16;r++){let c=$5[r],a=r===0?o.dc:o.ac,l=r===0?o.dcBias:o.acBias,u=t[c],f=((u<0?-u:u)*256+l*a)/(256*a)|0;f>a2&&(f=a2),s[r]=u<0?-f:f,e[c]=s[r]*a,f&&(i=r)}return{levels:s,last:i}}function m2(t,e,n,o){return(((t*8+e)*3+n)*11+o)*2}function h5(t,e){let n=t?255-e:e;return Math.round(-Math.log2((n<1?1:n)/256)*256)}function T5(t,e,n,o,s){let i=(a,l,u,m,f)=>{let b=m2(l,u,m,f);return t[b]+=a?1:0,t[b+1]++,!!a},r=o,c=D[r];if(!i(s.last>=0,n,c,e,0))return 0;for(;r<16;){let a=s.levels[r++],u=a<0?-a:a;if(!i(u!==0,n,c,e,1)){c=D[r],e=0;continue}if(i(u>1,n,c,e,2)?(i(u>4,n,c,e,3)?i(u>10,n,c,e,6)?(i(u>=35,n,c,e,8),u>=35?i(u>=67,n,c,e,10):i(u>=19,n,c,e,9)):i(u>6,n,c,e,7):i(u!==2,n,c,e,4)&&i(u===4,n,c,e,5),c=D[r],e=2):(c=D[r],e=1),r===16||!i(r<=s.last,n,c,e,0))return 1}return 1}function q2(t){let e=s5.map(n=>n.map(o=>o.map(s=>s.slice())));for(let n=0;n<4;n++)for(let o=0;o<8;o++)for(let s=0;s<3;s++)for(let i=0;i<11;i++){let r=m2(n,o,s,i),c=t[r],a=t[r+1];if(a===0)continue;let l=C5[n][o][s][i],u=s5[n][o][s][i],m=c?255-Math.floor(c*255/a):255,f=g=>c*h5(1,g)+(a-c)*h5(0,g),b=f(u)+h5(0,l),x=f(m)+h5(1,l)+8*256;b>x&&(e[n][o][s][i]=m)}return e}function _5(t,e,n,o,s){let i=o,r=n[i][e];if(!t.putBit(s.last>=0,r[0]))return 0;for(;i<16;){let c=s.levels[i++],a=c<0,l=a?-c:c;if(!t.putBit(l!==0,r[1])){r=n[D[i]][0];continue}if(!t.putBit(l>1,r[2]))r=n[D[i]][1];else{if(!t.putBit(l>4,r[3]))t.putBit(l!==2,r[4])&&t.putBit(l===4,r[5]);else if(!t.putBit(l>10,r[6]))t.putBit(l>6,r[7])?(t.putBit(l>=9,165),t.putBit(!(l&1),145)):t.putBit(l===6,159);else{let u,m;l<19?(t.putBit(0,r[8]),t.putBit(0,r[9]),l-=11,u=4,m=G5):l<35?(t.putBit(0,r[8]),t.putBit(1,r[9]),l-=19,u=8,m=Q5):l<67?(t.putBit(1,r[8]),t.putBit(0,r[10]),l-=35,u=16,m=X5):(t.putBit(1,r[8]),t.putBit(1,r[10]),l-=67,u=1024,m=Z5);let f=0;for(;u;)t.putBit(l&u,m[f++]),u>>=1}r=n[D[i]][2]}if(t.putBitUniform(a),i===16||!t.putBit(i<=s.last,r[0]))return 1}return 1}function W2(t){return t<0?0:t>255?255:t}function S5(t,e,n,o,s,i,r,c,a){if(t===y5){let u;if(r||c){let m=0;if(r)for(let b=0;b<i;b++)m+=e[(s-1)*n+o+b];if(c)for(let b=0;b<i;b++)m+=e[(s+b)*n+o-1];let f=(i===16?4:3)+(r&&c?1:0);u=m+(1<<f-1)>>f}else u=128;a.fill(u,0,i*i);return}if(t===V5){for(let u=0;u<i;u++){let m=r?e[(s-1)*n+o+u]:127;for(let f=0;f<i;f++)a[f*i+u]=m}return}if(t===O5){for(let u=0;u<i;u++){let m=c?e[(s+u)*n+o-1]:129;a.fill(m,u*i,u*i+i)}return}let l=r?c?e[(s-1)*n+o-1]:129:127;for(let u=0;u<i;u++){let m=c?e[(s+u)*n+o-1]:129;for(let f=0;f<i;f++){let b=r?e[(s-1)*n+o+f]:127;a[u*i+f]=W2(m+b-l)}}}function k5(t,e,n,o,s,i){let r=0;for(let c=0;c<s;c++){let a=(o+c)*e+n,l=c*s;for(let u=0;u<s;u++){let m=t[a+u]-i[l+u];r+=m<0?-m:m}}return r}function N5(t,e,n,o,s,i){for(let r=0;r<i;r++)e.set(t.subarray(r*i,r*i+i),(s+r)*n+o)}function b2(t,e){let{mbW:n,mbH:o,yStride:s,uvStride:i}=t,r=A5(e.qi,127),c=D2(r),a=H2(r,e.filterStrength??60),l=new Uint8Array(t.y.length),u=new Uint8Array(t.u.length),m=new Uint8Array(t.v.length),f=new Int16Array(16),b=new Int16Array(16),x=new Int16Array(16),g=new Int16Array(16),p=new Uint8Array(256),h=new Uint8Array(256),C=new Uint8Array(64),v=new Uint8Array(64),T=new Uint8Array(64),A=new Uint8Array(64),_=Array.from({length:16},()=>new Int16Array(16)),H=new Int16Array(16),d5=new Array(n*o),H5=0;for(let w=0;w<o;w++)for(let L=0;L<n;L++){let I=w>0,E=L>0,M=L*16,R=w*16,B=L*8,U=w*8,F=y5,V=1/0;for(let d=0;d<4;d++){S5(d,l,s,M,R,16,I,E,p);let k=k5(t.y,s,M,R,16,p);k<V&&(V=k,F=d,h.set(p))}N5(h,l,s,M,R,16);let q=new Array(16);for(let d=0;d<16;d++){let k=M+(d&3)*4,N=(R+(d>>2)*4)*s+k;w5(t.y,N,l,N,s,f),b[d]=f[0],q[d]=R5(f,_[d],1,c.y1)}l2(b,x);let o5=R5(x,g,0,c.y2);c2(g,b);for(let d=0;d<16;d++){let k=M+(d&3)*4,N=(R+(d>>2)*4)*s+k;_[d][0]=b[d],F5(_[d],l,N,s,l,N)}let W5=y5,Y5=1/0;for(let d=0;d<4;d++){S5(d,u,i,B,U,8,I,E,C),S5(d,m,i,B,U,8,I,E,v);let k=k5(t.u,i,B,U,8,C)+k5(t.v,i,B,U,8,v);k<Y5&&(Y5=k,W5=d,T.set(C),A.set(v))}N5(T,u,i,B,U,8),N5(A,m,i,B,U,8);let U5=new Array(8);for(let d=0;d<2;d++){let k=d===0?t.u:t.v,Z=d===0?u:m;for(let N=0;N<4;N++){let d2=B+(N&1)*4,i5=(U+(N>>1)*4)*i+d2;w5(k,i5,Z,i5,i,f),U5[d*4+N]=R5(f,H,0,c.uv),F5(H,Z,i5,i,Z,i5)}}let Y=o5.last<0;if(Y){for(let d of q)if(d.last>=0){Y=!1;break}}if(Y){for(let d of U5)if(d.last>=0){Y=!1;break}}Y&&H5++,d5[w*n+L]={yMode:F,uvMode:W5,skip:Y,y2:o5,luma:q,uv:U5}}let g5=n*o,B5=g5?(g5-H5)*255/g5|0:255,t5=B5<250,n5=new Uint32Array(1056*2);{let w=Array.from({length:n},()=>new Array(9).fill(0));for(let L=0;L<o;L++){let I=new Array(9).fill(0);for(let E=0;E<n;E++){let M=d5[L*n+E],R=w[E];if(t5&&M.skip){R.fill(0),I.fill(0);continue}R[8]=I[8]=T5(n5,R[8]+I[8],1,0,M.y2);for(let B=0;B<4;B++)for(let U=0;U<4;U++){let F=R[U]+I[B],V=T5(n5,F,0,1,M.luma[B*4+U]);R[U]=I[B]=V}for(let B=0;B<=2;B+=2)for(let U=0;U<2;U++)for(let F=0;F<2;F++){let V=R[4+B+F]+I[4+B+U],q=T5(n5,V,2,0,M.uv[(B>>1)*4+U*2+F]);R[4+B+F]=I[4+B+U]=q}}}}let e5=q2(n5),P=new J,r5=new J;P.putBitUniform(0),P.putBitUniform(0),P.putBitUniform(0),P.putBitUniform(0),P.putBits(a,6),P.putBits(0,3),P.putBitUniform(0),P.putBits(0,2),P.putBits(r,7),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putBitUniform(0);for(let w=0;w<4;w++)for(let L=0;L<8;L++)for(let I=0;I<3;I++)for(let E=0;E<11;E++){let M=e5[w][L][I][E],R=M!==s5[w][L][I][E];P.putBit(R,C5[w][L][I][E])&&P.putBits(M,8)}P.putBitUniform(t5)&&P.putBits(B5,8);let p2=e5[1],h2=e5[0],y2=e5[2],A2=Array.from({length:n},()=>new Array(9).fill(0));for(let w=0;w<o;w++){let L=new Array(9).fill(0);for(let I=0;I<n;I++){let E=d5[w*n+I];t5&&P.putBit(E.skip,B5),P.putBit(1,145),P.putBit(E.yMode===f2||E.yMode===O5,156)?P.putBit(E.yMode===f2,128):P.putBit(E.yMode===V5,163),P.putBit(E.uvMode!==y5,142)&&P.putBit(E.uvMode!==V5,114)&&P.putBit(E.uvMode!==O5,183);let M=A2[I];if(t5&&E.skip){M.fill(0),L.fill(0);continue}let R=_5(r5,M[8]+L[8],p2,0,E.y2);M[8]=L[8]=R;for(let B=0;B<4;B++)for(let U=0;U<4;U++){let F=M[U]+L[B],V=_5(r5,F,h2,1,E.luma[B*4+U]);M[U]=L[B]=V}for(let B=0;B<=2;B+=2)for(let U=0;U<2;U++)for(let F=0;F<2;F++){let V=M[4+B+F]+L[4+B+U],q=E.uv[(B>>1)*4+U*2+F],o5=_5(r5,V,y2,0,q);M[4+B+F]=L[4+B+U]=o5}}}let W=P.finish(),q5=r5.finish();if(W.length>=1<<19)throw new RangeError(`VP8: partition 0 is ${W.length} bytes, exceeding the bitstream's 2^19-1 limit \u2014 image too large/complex for a VP8 keyframe`);let x5=16|W.length<<5,S=new Uint8Array(10+W.length+q5.length);return S[0]=x5&255,S[1]=x5>>>8&255,S[2]=x5>>>16&255,S[3]=157,S[4]=1,S[5]=42,S[6]=t.width&255,S[7]=t.width>>8&63,S[8]=t.height&255,S[9]=t.height>>8&63,S.set(W,10),S.set(q5,10+W.length),S}var D5=16383;function Y2(t,e,n){if(!Number.isInteger(t)||!Number.isInteger(e))throw new RangeError(`encodeWebP: width and height must be integers (got ${t}\xD7${e})`);if(t<1||e<1)throw new RangeError(`encodeWebP: width and height must be \u2265 1 (got ${t}\xD7${e})`);if(t>D5||e>D5)throw new RangeError(`encodeWebP: width and height must be \u2264 ${D5} (got ${t}\xD7${e})`);let o=t*e*4;if(n<o)throw new RangeError(`encodeWebP: data too short \u2014 need ${o} bytes for a ${t}\xD7${e} RGBA image, got ${n}`)}function $2(t){if(t.quality!==void 0&&!Number.isFinite(t.quality))throw new RangeError(`encodeWebP: quality must be a finite number (got ${t.quality})`);if(t.alphaDither!==void 0&&!Number.isFinite(t.alphaDither))throw new RangeError(`encodeWebP: alphaDither must be a finite number (got ${t.alphaDither})`)}function G2(t){let e=(t<0?0:t>100?100:t)/100,n=e<.75?e*(2/3):2*e-1,o=Math.cbrt(n);return Math.round(127*(1-o))}function Q2(t){let e=0,n=0,o=0,s=0,i=0;for(let u=0;u<t.length;u+=4)t[u+3]===0?i++:(e+=t[u],n+=t[u+1],o+=t[u+2],s++);if(i===0)return t;let r=s?Math.round(e/s):255,c=s?Math.round(n/s):255,a=s?Math.round(o/s):255,l=t.slice();for(let u=0;u<l.length;u+=4)l[u+3]===0&&(l[u]=r,l[u+1]=c,l[u+2]=a);return l}function At(t,e={}){let{width:n,height:o}=t;Y2(n,o,t.data.length),$2(e);let s=(t.data instanceof Uint8Array?t.data:new Uint8Array(t.data.buffer,t.data.byteOffset,t.data.byteLength)).subarray(0,n*o*4),i=e2(s),r=!1;for(let b=0;b<i.length;b++)if(i[b]!==255){r=!0;break}let c=r?Q2(s):s,a=e.lossless??"auto",l=null;if(a!==!1){let b=j5(s,n,o);if(b&&(l=o2(b)),l&&a===!0)return l}let u=s2(c,n,o),m=b2(u,{qi:G2(e.quality??80)}),f;if(!r)f=M5({width:n,height:o,vp8:m});else{let b=n2(i,n,o,e.alphaLevels??16,e.alphaDither??1,e.alphaAdaptive??!0);f=M5({width:n,height:o,vp8:m,alph:b.payload})}return l&&l.length<=f.length?l:f}export{At as encodeWebP,x2 as hasNativeWebPEncoder};
1
+ var w2=Object.defineProperty;var S2=(t,e,n)=>e in t?w2(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var H=(t,e,n)=>S2(t,typeof e!="symbol"?e+"":e,n);var X=null;async function R2(){return X!==null?X:typeof document>"u"?(X=!1,X):(X=await new Promise(t=>{try{let e=document.createElement("canvas");e.width=1,e.height=1,e.toBlob(n=>t(n?.type==="image/webp"),"image/webp")}catch{t(!1)}}),X)}var l5=[[[[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128]],[[253,136,254,255,228,219,128,128,128,128,128],[189,129,242,255,227,213,255,219,128,128,128],[106,126,227,252,214,209,255,255,128,128,128]],[[1,98,248,255,236,226,255,255,128,128,128],[181,133,238,254,221,234,255,154,128,128,128],[78,134,202,247,198,180,255,219,128,128,128]],[[1,185,249,255,243,255,128,128,128,128,128],[184,150,247,255,236,224,128,128,128,128,128],[77,110,216,255,236,230,128,128,128,128,128]],[[1,101,251,255,241,255,128,128,128,128,128],[170,139,241,252,236,209,255,255,128,128,128],[37,116,196,243,228,255,255,255,128,128,128]],[[1,204,254,255,245,255,128,128,128,128,128],[207,160,250,255,238,128,128,128,128,128,128],[102,103,231,255,211,171,128,128,128,128,128]],[[1,152,252,255,240,255,128,128,128,128,128],[177,135,243,255,234,225,128,128,128,128,128],[80,129,211,255,194,224,128,128,128,128,128]],[[1,1,255,128,128,128,128,128,128,128,128],[246,1,255,128,128,128,128,128,128,128,128],[255,128,128,128,128,128,128,128,128,128,128]]],[[[198,35,237,223,193,187,162,160,145,155,62],[131,45,198,221,172,176,220,157,252,221,1],[68,47,146,208,149,167,221,162,255,223,128]],[[1,149,241,255,221,224,255,255,128,128,128],[184,141,234,253,222,220,255,199,128,128,128],[81,99,181,242,176,190,249,202,255,255,128]],[[1,129,232,253,214,197,242,196,255,255,128],[99,121,210,250,201,198,255,202,128,128,128],[23,91,163,242,170,187,247,210,255,255,128]],[[1,200,246,255,234,255,128,128,128,128,128],[109,178,241,255,231,245,255,255,128,128,128],[44,130,201,253,205,192,255,255,128,128,128]],[[1,132,239,251,219,209,255,165,128,128,128],[94,136,225,251,218,190,255,255,128,128,128],[22,100,174,245,186,161,255,199,128,128,128]],[[1,182,249,255,232,235,128,128,128,128,128],[124,143,241,255,227,234,128,128,128,128,128],[35,77,181,251,193,211,255,205,128,128,128]],[[1,157,247,255,236,231,255,255,128,128,128],[121,141,235,255,225,227,255,255,128,128,128],[45,99,188,251,195,217,255,224,128,128,128]],[[1,1,251,255,213,255,128,128,128,128,128],[203,1,248,255,255,128,128,128,128,128,128],[137,1,177,255,224,255,128,128,128,128,128]]],[[[253,9,248,251,207,208,255,192,128,128,128],[175,13,224,243,193,185,249,198,255,255,128],[73,17,171,221,161,179,236,167,255,234,128]],[[1,95,247,253,212,183,255,255,128,128,128],[239,90,244,250,211,209,255,255,128,128,128],[155,77,195,248,188,195,255,255,128,128,128]],[[1,24,239,251,218,219,255,205,128,128,128],[201,51,219,255,196,186,128,128,128,128,128],[69,46,190,239,201,218,255,228,128,128,128]],[[1,191,251,255,255,128,128,128,128,128,128],[223,165,249,255,213,255,128,128,128,128,128],[141,124,248,255,255,128,128,128,128,128,128]],[[1,16,248,255,255,128,128,128,128,128,128],[190,36,230,255,236,255,128,128,128,128,128],[149,1,255,128,128,128,128,128,128,128,128]],[[1,226,255,128,128,128,128,128,128,128,128],[247,192,255,128,128,128,128,128,128,128,128],[240,128,255,128,128,128,128,128,128,128,128]],[[1,134,252,255,255,128,128,128,128,128,128],[213,62,250,255,255,128,128,128,128,128,128],[55,93,255,128,128,128,128,128,128,128,128]],[[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128],[128,128,128,128,128,128,128,128,128,128,128]]],[[[202,24,213,235,186,191,220,160,240,175,255],[126,38,182,232,169,184,228,174,255,187,128],[61,46,138,219,151,178,240,170,255,216,128]],[[1,112,230,250,199,191,247,159,255,255,128],[166,109,228,252,211,215,255,174,128,128,128],[39,77,162,232,172,180,245,178,255,255,128]],[[1,52,220,246,198,199,249,220,255,255,128],[124,74,191,243,183,193,250,221,255,255,128],[24,71,130,219,154,170,243,182,255,255,128]],[[1,182,225,249,219,240,255,224,128,128,128],[149,150,226,252,216,205,255,171,128,128,128],[28,108,170,242,183,194,254,223,255,255,128]],[[1,81,230,252,204,203,255,192,128,128,128],[123,102,209,247,188,196,255,233,128,128,128],[20,95,153,243,164,173,255,203,128,128,128]],[[1,222,248,255,216,213,128,128,128,128,128],[168,175,246,252,235,205,255,255,128,128,128],[47,116,215,255,211,212,255,255,128,128,128]],[[1,121,236,253,212,214,255,255,128,128,128],[141,84,213,252,201,202,255,219,128,128,128],[42,80,160,240,162,185,255,205,128,128,128]],[[1,1,255,128,128,128,128,128,128,128,128],[244,1,255,128,128,128,128,128,128,128,128],[238,1,255,128,128,128,128,128,128,128,128]]]],L5=[[[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[176,246,255,255,255,255,255,255,255,255,255],[223,241,252,255,255,255,255,255,255,255,255],[249,253,253,255,255,255,255,255,255,255,255]],[[255,244,252,255,255,255,255,255,255,255,255],[234,254,254,255,255,255,255,255,255,255,255],[253,255,255,255,255,255,255,255,255,255,255]],[[255,246,254,255,255,255,255,255,255,255,255],[239,253,254,255,255,255,255,255,255,255,255],[254,255,254,255,255,255,255,255,255,255,255]],[[255,248,254,255,255,255,255,255,255,255,255],[251,255,254,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[251,254,254,255,255,255,255,255,255,255,255],[254,255,254,255,255,255,255,255,255,255,255]],[[255,254,253,255,254,255,255,255,255,255,255],[250,255,254,255,254,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[217,255,255,255,255,255,255,255,255,255,255],[225,252,241,253,255,255,254,255,255,255,255],[234,250,241,250,253,255,253,254,255,255,255]],[[255,254,255,255,255,255,255,255,255,255,255],[223,254,254,255,255,255,255,255,255,255,255],[238,253,254,254,255,255,255,255,255,255,255]],[[255,248,254,255,255,255,255,255,255,255,255],[249,254,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,255,255,255,255,255,255,255,255,255],[247,254,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[252,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,254,255,255,255,255,255,255,255,255],[253,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,253,255,255,255,255,255,255,255,255],[250,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[186,251,250,255,255,255,255,255,255,255,255],[234,251,244,254,255,255,255,255,255,255,255],[251,251,243,253,254,255,254,255,255,255,255]],[[255,253,254,255,255,255,255,255,255,255,255],[236,253,254,255,255,255,255,255,255,255,255],[251,253,253,254,254,255,255,255,255,255,255]],[[255,254,254,255,255,255,255,255,255,255,255],[254,254,254,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,254,255,255,255,255,255,255,255,255,255],[254,254,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]],[[[248,255,255,255,255,255,255,255,255,255,255],[250,254,252,254,255,255,255,255,255,255,255],[248,254,249,253,255,255,255,255,255,255,255]],[[255,253,253,255,255,255,255,255,255,255,255],[246,253,253,255,255,255,255,255,255,255,255],[252,254,251,254,254,255,255,255,255,255,255]],[[255,254,252,255,255,255,255,255,255,255,255],[248,254,253,255,255,255,255,255,255,255,255],[253,255,254,254,255,255,255,255,255,255,255]],[[255,251,254,255,255,255,255,255,255,255,255],[245,251,254,255,255,255,255,255,255,255,255],[253,253,254,255,255,255,255,255,255,255,255]],[[255,251,253,255,255,255,255,255,255,255,255],[252,253,254,255,255,255,255,255,255,255,255],[255,254,255,255,255,255,255,255,255,255,255]],[[255,252,255,255,255,255,255,255,255,255,255],[249,255,254,255,255,255,255,255,255,255,255],[255,255,254,255,255,255,255,255,255,255,255]],[[255,255,253,255,255,255,255,255,255,255,255],[250,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]],[[255,255,255,255,255,255,255,255,255,255,255],[254,255,255,255,255,255,255,255,255,255,255],[255,255,255,255,255,255,255,255,255,255,255]]]],c5=[4,5,6,7,8,9,10,10,11,12,13,14,15,16,17,17,18,19,20,20,21,21,22,22,23,23,24,25,25,26,27,28,29,30,31,32,33,34,35,36,37,37,38,39,40,41,42,43,44,45,46,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,76,77,78,79,80,81,82,83,84,85,86,87,88,89,91,93,95,96,98,100,101,102,104,106,108,110,112,114,116,118,122,124,126,128,130,132,134,136,138,140,143,145,148,151,154,157],J=[4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,60,62,64,66,68,70,72,74,76,78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,119,122,125,128,131,134,137,140,143,146,149,152,155,158,161,164,167,170,173,177,181,185,189,193,197,201,205,209,213,217,221,225,229,234,239,245,249,254,259,264,269,274,279,284],t2=[0,1,4,8,5,2,3,6,9,12,13,10,7,11,14,15],W=[0,1,2,3,6,4,5,6,6,6,6,6,6,6,6,7,0],n2=[173,148,140],e2=[176,155,140,135],r2=[180,157,141,134,130],o2=[254,254,243,230,196,177,153,140,133,130,129],v5=[96,73,55,39,23,13,5,1,255,255,255,255,255,255,255,255,101,78,58,42,26,16,8,2,0,3,9,17,27,43,59,79,102,86,62,46,32,20,10,6,4,7,11,21,33,47,63,87,105,90,70,52,37,28,18,14,12,15,19,29,38,53,71,91,110,99,82,66,48,35,30,24,22,25,31,36,49,67,83,100,115,108,94,76,64,50,44,40,34,41,45,51,65,77,95,109,118,113,103,92,80,68,60,56,54,57,61,69,81,93,104,114,119,116,111,106,97,88,84,74,72,75,85,89,98,107,112,117];var f5=class{constructor(){H(this,"buf",[]);H(this,"acc",0);H(this,"used",0)}putBits(e,n){for(this.acc|=e<<this.used,this.used+=n;this.used>=8;)this.buf.push(this.acc&255),this.acc>>>=8,this.used-=8}finish(){return this.used>0&&(this.buf.push(this.acc&255),this.acc=0,this.used=0),new Uint8Array(this.buf)}};function a5(t){if(t<=4)return{code:t-1,extraBits:0,extraValue:0};let e=t-1,n=31-Math.clz32(e),o=e>>n-1&1,s=n-1;return{code:2*n+o,extraBits:s,extraValue:e&(1<<s)-1}}function F2(t,e){let n=e/t|0,o=e-n*t;return o<=8&&n<8?v5[n*16+8-o]+1:o>t-8&&n<7?v5[(n+1)*16+8+(t-o)]+1:e+120}function Q(t,e){let n=t.length,o=new Uint8Array(n),s=[];for(let r=0;r<n;r++)t[r]>0&&s.push(r);if(s.length===0)return o;if(s.length===1)return o[s[0]]=1,o;let i=Array.from(t);for(;;){let r=s.map(l=>({count:i[l],symbol:l,left:null,right:null})).sort((l,c)=>l.count-c.count);for(;r.length>1;){let l=r.shift(),c=r.shift(),a={count:l.count+c.count,symbol:-1,left:l,right:c},f=0,p=r.length;for(;f<p;){let g=f+p>>1;r[g].count<=a.count?f=g+1:p=g}r.splice(f,0,a)}let u=!1,m=(l,c)=>{if(l.symbol>=0){c>e&&(u=!0),o[l.symbol]=c;return}m(l.left,c+1),m(l.right,c+1)};if(m(r[0],0),!u)return o;i=i.map(l=>l>0?l+1>>1:0)}}function k2(t,e){let n=0;for(let o=0;o<e;o++)n=n<<1|t>>o&1;return n}function Z(t){let e=0;for(let n of t.lengths)n>0&&e++;return e!==1?t:{codes:t.codes,lengths:new Uint8Array(t.lengths.length)}}function K(t){let e=Math.max(0,...t),n=new Array(e+1).fill(0);for(let r of t)r>0&&n[r]++;let o=new Array(e+1).fill(0),s=0;for(let r=1;r<=e;r++)s=s+n[r-1]<<1,o[r]=s;let i=new Uint16Array(t.length);for(let r=0;r<t.length;r++)t[r]>0&&(i[r]=k2(o[t[r]]++,t[r]));return{codes:i,lengths:t}}var _5=[17,18,0,1,2,3,4,5,16,6,7,8,9,10,11,12,13,14,15];function z(t,e){let n=[];for(let l=0;l<e.length;l++)e[l]>0&&n.push(l);if(n.length===0){t.putBits(1,1),t.putBits(0,1),t.putBits(0,1),t.putBits(0,1);return}if(n.length<=2&&n[n.length-1]<256){t.putBits(1,1),t.putBits(n.length-1,1),n[0]<=1?(t.putBits(0,1),t.putBits(n[0],1)):(t.putBits(1,1),t.putBits(n[0],8)),n.length===2&&t.putBits(n[1],8);return}t.putBits(0,1);let o=[],s=8;for(let l=0;l<e.length;){let c=e[l],a=1;for(;l+a<e.length&&e[l+a]===c;)a++;if(c===0){let f=a;for(;f>=11;){let p=Math.min(f,138);o.push({symbol:18,extra:p-11,extraBits:7}),f-=p}for(f>=3&&(o.push({symbol:17,extra:f-3,extraBits:3}),f=0);f-- >0;)o.push({symbol:0,extra:0,extraBits:0})}else{let f=a;for(c!==s&&(o.push({symbol:c,extra:0,extraBits:0}),s=c,f--);f>=3;){let p=Math.min(f,6);o.push({symbol:16,extra:p-3,extraBits:2}),f-=p}for(;f-- >0;)o.push({symbol:c,extra:0,extraBits:0})}l+=a}let i=new Uint32Array(19);for(let l of o)i[l.symbol]++;let r=Q(i,7),u=Z(K(r)),m=_5.length;for(;m>4&&r[_5[m-1]]===0;)m--;t.putBits(m-4,4);for(let l=0;l<m;l++)t.putBits(r[_5[l]],3);t.putBits(0,1);for(let l of o)t.putBits(u.codes[l.symbol],u.lengths[l.symbol]),l.extraBits>0&&t.putBits(l.extra,l.extraBits)}var N2=4,V2=4096;function O2(t,e){let n=[],o=t.length,s=0;for(;s<o;){let i=0,r=0;for(let u of s>=e?[1,e]:[1]){if(s<u)continue;let m=0,l=Math.min(V2,o-s);for(;m<l&&t[s+m]===t[s+m-u];)m++;m>i&&(i=m,r=u)}i>=N2?(n.push({length:i,distCode:F2(e,r)}),s+=i):(n.push({literal:t[s]}),s++)}return n}var T5=256,D2=24,H2=T5+D2;function w5(t,e,n,o){let s=O2(e,n),i=new Uint32Array(H2),r=new Uint32Array(256),u=new Uint32Array(256),m=new Uint32Array(256),l=new Uint32Array(40);for(let h of s)"literal"in h?(i[h.literal>>>8&255]++,r[h.literal>>>16&255]++,u[h.literal&255]++,m[h.literal>>>24]++):(i[T5+a5(h.length).code]++,l[a5(h.distCode).code]++);let c=Q(i,15),a=Q(r,15),f=Q(u,15),p=Q(m,15),g=Q(l,15),d=Z(K(c)),b=Z(K(a)),A=Z(K(f)),x=Z(K(p)),E=Z(K(g));t.putBits(0,1),o&&t.putBits(0,1),z(t,c),z(t,a),z(t,f),z(t,p),z(t,g);let I=(h,F)=>{h.lengths[F]>0&&t.putBits(h.codes[F],h.lengths[F])};for(let h of s)if("literal"in h)I(d,h.literal>>>8&255),I(b,h.literal>>>16&255),I(A,h.literal&255),I(x,h.literal>>>24);else{let F=a5(h.length);I(d,T5+F.code),F.extraBits>0&&t.putBits(F.extraValue,F.extraBits);let Y=a5(h.distCode);I(E,Y.code),Y.extraBits>0&&t.putBits(Y.extraValue,Y.extraBits)}}function i2(t,e,n){let o=new Uint32Array(t.length);for(let i=0;i<t.length;i++)o[i]=(4278190080|t[i]<<8)>>>0;let s=new f5;return s.putBits(0,1),w5(s,o,e,!0),s.finish()}function W2(t){return t<=2?3:t<=4?2:t<=16?1:0}function s2(t,e,n){let o=new Set,s=e*n,i=new Uint32Array(s);for(let b=0;b<s;b++){let A=(t[b*4+3]<<24|t[b*4]<<16|t[b*4+1]<<8|t[b*4+2])>>>0;if(i[b]=A,o.add(A),o.size>256)return null}let r=[...o].sort((b,A)=>b-A),u=new Map;r.forEach((b,A)=>{u.set(b,A)});let m=W2(r.length),l=8>>m,c=e+(1<<m)-1>>m,a=new Uint32Array(c*n);a.fill(4278190080);for(let b=0;b<n;b++)for(let A=0;A<e;A++){let x=u.get(i[b*e+A]),E=b*c+(A>>m);a[E]=(a[E]|x<<l*(A&(1<<m)-1)+8)>>>0}let f=!1;for(let b of r)if(b>>>24!==255){f=!0;break}let p=new f5;p.putBits(47,8),p.putBits(e-1,14),p.putBits(n-1,14),p.putBits(f?1:0,1),p.putBits(0,3),p.putBits(1,1),p.putBits(3,2),p.putBits(r.length-1,8);let g=new Uint32Array(r.length),d=0;for(let b=0;b<r.length;b++){let A=r[b];g[b]=((A>>>24)-(d>>>24)&255)*16777216+(((A>>>16)-(d>>>16)&255)<<16)+(((A>>>8)-(d>>>8)&255)<<8)+(A-d&255)>>>0,d=A}return w5(p,g,r.length,!1),p.putBits(0,1),w5(p,a,c,!0),p.finish()}var u2=[0,8,2,10,12,4,14,6,3,11,1,9,15,7,13,5];function Y2(t,e,n=16,o=1){let s=255/(n-1),i=new Uint8Array(t.length);if(o<=0){let u=new Uint8Array(256);for(let m=0;m<256;m++)u[m]=Math.round(Math.round(m/s)*s);for(let m=0;m<t.length;m++)i[m]=u[t[m]];return i}let r=o>1?1:o;for(let u=0;u<t.length;u++){let m=u%e,l=u/e|0,c=((u2[(l&3)*4+(m&3)]+.5)/16-.5)*r,a=Math.round(t[u]/s+c),f=Math.round(a*s);i[u]=f<0?0:f>255?255:f}return i}function G2(t,e,n=16,o=1){let s=new Uint32Array(256),i=255,r=0,u=0;for(let b=0;b<t.length;b++){let A=t[b];s[A]===0&&u++,s[A]++,A<i&&(i=A),A>r&&(r=A)}if(u<=n)return t.slice();let m=new Float64Array(n);for(let b=0;b<n;b++)m[b]=i+(r-i)*b/(n-1);let l=1/0,c=new Uint8Array(256);for(let b=0;b<6;b++){let A=new Float64Array(n),x=new Float64Array(n),E=0;for(let h=i;h<=r;h++){for(;E<n-1&&2*h>m[E]+m[E+1];)E++;c[h]=E,A[E]+=h*s[h],x[E]+=s[h]}for(let h=1;h<n-1;h++)x[h]>0&&(m[h]=A[h]/x[h]);let I=0;for(let h=i;h<=r;h++){let F=h-m[c[h]];I+=s[h]*F*F}if(l-I<1e-4*t.length)break;l=I}let a=new Uint8Array(n);for(let b=0;b<n;b++)a[b]=Math.round(m[b]);a[0]=i,a[n-1]=r;let f=new Uint8Array(256),p=new Float64Array(256);{let b=0;for(let A=i;A<=r;A++){for(;b<n-2&&A>=a[b+1];)b++;f[A]=b;let x=a[b+1]-a[b];p[A]=x>0?(A-a[b])/x:0}}let g=o<=0?0:o>1?1:o,d=new Uint8Array(t.length);for(let b=0;b<t.length;b++){let A=t[b],x;if(g>0){let E=b%e,I=b/e|0,h=((u2[(I&3)*4+(E&3)]+.5)/16-.5)*g;x=f[A]+Math.round(p[A]+h)}else x=f[A]+Math.round(p[A]);d[b]=a[x<0?0:x>=n?n-1:x]}return d}var m5=0,l2=1,c2=2,q2=3;function $2(t){return t<0?0:t>255?255:t}function X2(t,e,n,o){if(o===m5)return t;let s=new Uint8Array(t.length);for(let i=0;i<n;i++)for(let r=0;r<e;r++){let u=i*e+r,m;r===0&&i===0?m=0:o===l2?m=r>0?t[u-1]:t[u-e]:o===c2?m=i>0?t[u-e]:t[u-1]:i===0?m=t[u-1]:r===0?m=t[u-e]:m=$2(t[u-1]+t[u-e]-t[u-e-1]),s[u]=t[u]-m&255}return s}function Q2(t){let e=new Uint32Array(256),n=0;for(let s=0;s<t.length;s++)s>0&&t[s]===t[s-1]||(e[t[s]]++,n++);if(n===0)return 0;let o=0;for(let s=0;s<256;s++){if(e[s]===0)continue;let i=e[s]/n;o-=i*Math.log2(i)}return n*(o+2)}function a2(t,e,n,o=16,s=1,i=!0){let r=i?G2(t,e,o,s):Y2(t,e,o,s),u=!0;for(let f=0;f<t.length;f++)if(r[f]!==t[f]){u=!1;break}let m=u?[{plane:t,preprocessing:0}]:[{plane:t,preprocessing:0},{plane:r,preprocessing:1}],l=null,c=m5,a=t;for(let{plane:f,preprocessing:p}of m){let g=[m5,l2,c2,q2].map(d=>({f:d,filtered:X2(f,e,n,d)})).map(d=>({...d,h:Q2(d.filtered)})).sort((d,b)=>d.h-b.h).slice(0,2);for(let{f:d,filtered:b}of g){let A=i2(b,e,n);if(l===null||A.length+1<l.length){let x=new Uint8Array(1+A.length);x[0]=1|d<<2|p<<4,x.set(A,1),l=x,c=d,a=f}}}if(1+t.length<l.length){let f=new Uint8Array(1+t.length);return f[0]=0,f.set(t,1),{payload:f,filter:m5,quantized:t}}return{payload:l,filter:c,quantized:a}}function f2(t){let e=t.length/4,n=new Uint8Array(e);for(let o=0;o<e;o++)n[o]=t[o*4+3];return n}function t5(t){return[t.charCodeAt(0),t.charCodeAt(1),t.charCodeAt(2),t.charCodeAt(3)]}function S5(t){return[t&255,t>>>8&255,t>>>16&255,t>>>24&255]}function m2(t){return[t&255,t>>>8&255,t>>>16&255]}function b5(t,e){let n=e.length+(e.length&1),o=new Uint8Array(8+n);return o.set(t5(t),0),o.set(S5(e.length),4),o.set(e,8),o}function R5(t){let e=[];if(t.alph){let i=new Uint8Array(10);i[0]=16,i.set(m2(t.width-1),4),i.set(m2(t.height-1),7),e.push(b5("VP8X",i)),e.push(b5("ALPH",t.alph))}e.push(b5("VP8 ",t.vp8));let n=e.reduce((i,r)=>i+r.length,0),o=new Uint8Array(12+n);o.set(t5("RIFF"),0),o.set(S5(4+n),4),o.set(t5("WEBP"),8);let s=12;for(let i of e)o.set(i,s),s+=i.length;return o}function b2(t){let e=b5("VP8L",t),n=new Uint8Array(12+e.length);return n.set(t5("RIFF"),0),n.set(S5(4+e.length),4),n.set(t5("WEBP"),8),n.set(e,12),n}function Z2(t,e,n){return 16839*t+33059*e+6420*n+32768+(16<<16)>>16}var h2=12,A5=7,h5=1<<A5,K2=h5>>1,y2=1<<h2-A5,p2=(1<<h2)-1,R=new Uint16Array(256),V5=new Int32Array(y2+1);{for(let e=0;e<=255;e++)R[e]=Math.floor(Math.pow(e/255,.8)*p2+.5);let t=h5/p2;for(let e=0;e<=y2;e++)V5[e]=Math.floor(255*Math.pow(t*e,1/.8)+.5)}function p5(t){let e=t>>A5+2,n=t&(h5<<2)-1;return V5[e+1]*n+V5[e]*((h5<<2)-n)+K2>>A5}function A2(t){return t=t+131072+(128<<18)>>18,t<0?0:t>255?255:t}function d2(t,e,n,o){let s=e+15>>4,i=n+15>>4,r=s*16,u=s*8,m=new Uint8Array(r*i*16),l=new Uint8Array(u*i*8),c=new Uint8Array(u*i*8);for(let f=0;f<i*16;f++){let p=f<n?f:n-1;for(let g=0;g<r;g++){let d=g<e?g:e-1,b=(p*e+d)*4;m[f*r+g]=Z2(t[b],t[b+1],t[b+2])}}let a=[0,0,0,0];for(let f=0;f<i*8;f++)for(let p=0;p<u;p++){for(let x=0;x<4;x++){let E=p*2+(x&1),I=f*2+(x>>1);E>=e&&(E=e-1),I>=n&&(I=n-1),a[x]=I*e+E}let g=1020;o&&(g=o[a[0]]+o[a[1]]+o[a[2]]+o[a[3]]);let d,b,A;if(g===0||g===1020)d=p5(R[t[a[0]*4]]+R[t[a[1]*4]]+R[t[a[2]*4]]+R[t[a[3]*4]]),b=p5(R[t[a[0]*4+1]]+R[t[a[1]*4+1]]+R[t[a[2]*4+1]]+R[t[a[3]*4+1]]),A=p5(R[t[a[0]*4+2]]+R[t[a[1]*4+2]]+R[t[a[2]*4+2]]+R[t[a[3]*4+2]]);else{let x=o,E=I=>{let h=x[a[0]]*R[t[a[0]*4+I]]+x[a[1]]*R[t[a[1]*4+I]]+x[a[2]]*R[t[a[2]*4+I]]+x[a[3]]*R[t[a[3]*4+I]];return p5(4*h/g|0)};d=E(0),b=E(1),A=E(2)}l[f*u+p]=A2(-9719*d-19081*b+28800*A),c[f*u+p]=A2(28800*d-24116*b-4684*A)}return{width:e,height:n,mbW:s,mbH:i,yStride:r,uvStride:u,y:m,u:l,v:c}}var N=8;function F5(t,e,n,o,s,i){let{y:r,yStride:u,width:m}=t,l=0,c=0;for(let a=0;a<i;a++)for(let f=0;f<s;f++)e[(o+a)*m+n+f]!==0&&(c++,l+=r[(o+a)*u+n+f]);if(c>0&&c<s*i){let a=l/c|0;for(let f=0;f<i;f++)for(let p=0;p<s;p++)e[(o+f)*m+n+p]===0&&(r[(o+f)*u+n+p]=a)}return c===0}function k5(t,e,n,o,s,i){for(let r=0;r<i;r++)t.fill(s,(o+r)*e+n,(o+r)*e+n+i)}function B2(t,e){let{width:n,height:o,yStride:s,uvStride:i}=t,r=0;for(;r+N<=o;r+=N){let u=!0,m=0,l=0,c=0,a=0;for(;a+N<=n;a+=N)F5(t,e,a,r,N,N)?(u&&(m=t.y[r*s+a],l=t.u[(r>>1)*i+(a>>1)],c=t.v[(r>>1)*i+(a>>1)],u=!1),k5(t.y,s,a,r,m,N),k5(t.u,i,a>>1,r>>1,l,N/2),k5(t.v,i,a>>1,r>>1,c,N/2)):u=!0;a<n&&F5(t,e,a,r,n-a,N)}if(r<o)for(let u=0;u<n;u+=N)F5(t,e,u,r,Math.min(N,n-u),o-r);N5(t.y,s,n,o,t.mbH*16),N5(t.u,i,n+1>>1,o+1>>1,t.mbH*8),N5(t.v,i,n+1>>1,o+1>>1,t.mbH*8)}function N5(t,e,n,o,s){if(n<e)for(let i=0;i<o;i++)t.fill(t[i*e+n-1],i*e+n,(i+1)*e);for(let i=o;i<s;i++)t.copyWithin(i*e,(o-1)*e,o*e)}var j2=[7,6,6,5,5,5,5,4,4,4,4,4,4,4,4,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0],x2=[127,127,191,127,159,191,223,127,143,159,175,191,207,223,239,127,135,143,151,159,167,175,183,191,199,207,215,223,231,239,247,127,131,135,139,143,147,151,155,159,163,167,171,175,179,183,187,191,195,199,203,207,211,215,219,223,227,231,235,239,243,247,251,127,129,131,133,135,137,139,141,143,145,147,149,151,153,155,157,159,161,163,165,167,169,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199,201,203,205,207,209,211,213,215,217,219,221,223,225,227,229,231,233,235,237,239,241,243,245,247,249,251,253,127],n5=class{constructor(){H(this,"range",254);H(this,"value",0);H(this,"run",0);H(this,"nbBits",-8);H(this,"buf",[])}flush(){let e=8+this.nbBits,n=this.value>>e;if(this.value-=n<<e,this.nbBits-=8,(n&255)!==255){if(n&256){let s=this.buf.length-1;s>=0&&this.buf[s]++}let o=n&256?0:255;for(;this.run>0;this.run--)this.buf.push(o);this.buf.push(n&255)}else this.run++}putBit(e,n){let o=e?1:0,s=this.range*n>>8;if(o?(this.value+=s+1,this.range-=s+1):this.range=s,this.range<127){let i=j2[this.range];this.range=x2[this.range],this.value<<=i,this.nbBits+=i,this.nbBits>0&&this.flush()}return!!o}putBitUniform(e){let n=e?1:0,o=this.range>>1;return n?(this.value+=o+1,this.range-=o+1):this.range=o,this.range<127&&(this.range=x2[this.range],this.value<<=1,this.nbBits+=1,this.nbBits>0&&this.flush()),!!n}putBits(e,n){for(let o=1<<n-1;o;o>>>=1)this.putBitUniform(e&o)}putSignedBits(e,n){this.putBitUniform(e!==0)&&(e<0?this.putBits(-e<<1|1,n+1):this.putBits(e<<1,n+1))}finish(){return this.putBits(0,9-this.nbBits),this.nbBits=0,this.flush(),new Uint8Array(this.buf)}};function y5(t){return(t*20091>>16)+t|0}function d5(t){return t*35468>>16|0}function B5(t){return t<0?0:t>255?255:t}var y=new Int32Array(16);function O5(t,e,n,o,s,i){for(let r=0;r<4;r++){let u=e+r*s,m=o+r*s,l=t[u]-n[m],c=t[u+1]-n[m+1],a=t[u+2]-n[m+2],f=t[u+3]-n[m+3],p=l+f,g=c+a,d=c-a,b=l-f;y[0+r*4]=(p+g)*8,y[1+r*4]=d*2217+b*5352+1812>>9,y[2+r*4]=(p-g)*8,y[3+r*4]=b*2217-d*5352+937>>9}for(let r=0;r<4;r++){let u=y[0+r]+y[12+r],m=y[4+r]+y[8+r],l=y[4+r]-y[8+r],c=y[0+r]-y[12+r];i[0+r]=u+m+7>>4,i[4+r]=(l*2217+c*5352+12e3>>16)+(c!==0?1:0),i[8+r]=u-m+7>>4,i[12+r]=c*2217-l*5352+51e3>>16}}function D5(t,e,n,o,s,i){for(let r=0;r<4;r++){let u=t[r]+t[8+r],m=t[r]-t[8+r],l=d5(t[4+r])-y5(t[12+r]),c=y5(t[4+r])+d5(t[12+r]);y[r*4+0]=u+c,y[r*4+1]=m+l,y[r*4+2]=m-l,y[r*4+3]=u-c}for(let r=0;r<4;r++){let u=y[0+r]+4,m=u+y[8+r],l=u-y[8+r],c=d5(y[4+r])-y5(y[12+r]),a=y5(y[4+r])+d5(y[12+r]),f=i+r*o,p=n+r*o;s[f]=B5(e[p]+(m+a>>3)),s[f+1]=B5(e[p+1]+(l+c>>3)),s[f+2]=B5(e[p+2]+(l-c>>3)),s[f+3]=B5(e[p+3]+(m-a>>3))}}function U2(t,e){for(let n=0;n<4;n++){let o=n*4,s=t[o]+t[o+2],i=t[o+1]+t[o+3],r=t[o+1]-t[o+3],u=t[o]-t[o+2];y[0+n*4]=s+i,y[1+n*4]=u+r,y[2+n*4]=u-r,y[3+n*4]=s-i}for(let n=0;n<4;n++){let o=y[0+n]+y[8+n],s=y[4+n]+y[12+n],i=y[4+n]-y[12+n],r=y[0+n]-y[8+n],u=o+s,m=r+i,l=r-i,c=o-s;e[0+n]=u>>1,e[4+n]=m>>1,e[8+n]=l>>1,e[12+n]=c>>1}}function g2(t,e){for(let n=0;n<4;n++){let o=t[0+n]+t[12+n],s=t[4+n]+t[8+n],i=t[4+n]-t[8+n],r=t[0+n]-t[12+n];y[0+n]=o+s,y[8+n]=o-s,y[4+n]=r+i,y[12+n]=r-i}for(let n=0;n<4;n++){let o=y[0+n*4]+3,s=o+y[3+n*4],i=y[1+n*4]+y[2+n*4],r=y[1+n*4]-y[2+n*4],u=o-y[3+n*4];e[n*4+0]=s+i>>3,e[n*4+1]=u+r>>3,e[n*4+2]=s-i>>3,e[n*4+3]=u-r>>3}}var C2=2047,U5=0,X5=1,Q5=2,P2=3;function g5(t,e){let n=Math.round(t)|0;return n<0?0:n>e?e:n}function J2(t){let e=g5(t,127),n=J[e]*101581>>16;return{y1:{dc:c5[e],ac:J[e],dcBias:96,acBias:110},y2:{dc:c5[e]*2,ac:n<8?8:n,dcBias:96,acBias:108},uv:{dc:c5[g5(e,117)],ac:J[e],dcBias:110,acBias:115}}}function z2(t,e){let n=J[g5(t,127)]>>2,s=(n>63?63:n)*5*e/256|0;return s<1?0:s>63?63:s}function H5(t,e,n,o){let s=new Int16Array(16),i=-1;for(let r=n;r<16;r++){let u=t2[r],m=r===0?o.dc:o.ac,l=r===0?o.dcBias:o.acBias,c=t[u],f=((c<0?-c:c)*256+l*m)/(256*m)|0;f>C2&&(f=C2),s[r]=c<0?-f:f,e[u]=s[r]*m,f&&(i=r)}return{levels:s,last:i}}function M2(t,e,n,o){return(((t*8+e)*3+n)*11+o)*2}function x5(t,e){let n=t?255-e:e;return Math.round(-Math.log2((n<1?1:n)/256)*256)}function W5(t,e,n,o,s){let i=(m,l,c,a,f)=>{let p=M2(l,c,a,f);return t[p]+=m?1:0,t[p+1]++,!!m},r=o,u=W[r];if(!i(s.last>=0,n,u,e,0))return 0;for(;r<16;){let m=s.levels[r++],c=m<0?-m:m;if(!i(c!==0,n,u,e,1)){u=W[r],e=0;continue}if(i(c>1,n,u,e,2)?(i(c>4,n,u,e,3)?i(c>10,n,u,e,6)?(i(c>=35,n,u,e,8),c>=35?i(c>=67,n,u,e,10):i(c>=19,n,u,e,9)):i(c>6,n,u,e,7):i(c!==2,n,u,e,4)&&i(c===4,n,u,e,5),u=W[r],e=2):(u=W[r],e=1),r===16||!i(r<=s.last,n,u,e,0))return 1}return 1}function t1(t){let e=l5.map(n=>n.map(o=>o.map(s=>s.slice())));for(let n=0;n<4;n++)for(let o=0;o<8;o++)for(let s=0;s<3;s++)for(let i=0;i<11;i++){let r=M2(n,o,s,i),u=t[r],m=t[r+1];if(m===0)continue;let l=L5[n][o][s][i],c=l5[n][o][s][i],a=u?255-Math.floor(u*255/m):255,f=d=>u*x5(1,d)+(m-u)*x5(0,d),p=f(c)+x5(0,l),g=f(a)+x5(1,l)+8*256;p>g&&(e[n][o][s][i]=a)}return e}function Y5(t,e,n,o,s){let i=o,r=n[i][e];if(!t.putBit(s.last>=0,r[0]))return 0;for(;i<16;){let u=s.levels[i++],m=u<0,l=m?-u:u;if(!t.putBit(l!==0,r[1])){r=n[W[i]][0];continue}if(!t.putBit(l>1,r[2]))r=n[W[i]][1];else{if(!t.putBit(l>4,r[3]))t.putBit(l!==2,r[4])&&t.putBit(l===4,r[5]);else if(!t.putBit(l>10,r[6]))t.putBit(l>6,r[7])?(t.putBit(l>=9,165),t.putBit(!(l&1),145)):t.putBit(l===6,159);else{let c,a;l<19?(t.putBit(0,r[8]),t.putBit(0,r[9]),l-=11,c=4,a=n2):l<35?(t.putBit(0,r[8]),t.putBit(1,r[9]),l-=19,c=8,a=e2):l<67?(t.putBit(1,r[8]),t.putBit(0,r[10]),l-=35,c=16,a=r2):(t.putBit(1,r[8]),t.putBit(1,r[10]),l-=67,c=1024,a=o2);let f=0;for(;c;)t.putBit(l&c,a[f++]),c>>=1}r=n[W[i]][2]}if(t.putBitUniform(m),i===16||!t.putBit(i<=s.last,r[0]))return 1}return 1}function n1(t){return t<0?0:t>255?255:t}function G5(t,e,n,o,s,i,r,u,m){if(t===U5){let c;if(r||u){let a=0;if(r)for(let p=0;p<i;p++)a+=e[(s-1)*n+o+p];if(u)for(let p=0;p<i;p++)a+=e[(s+p)*n+o-1];let f=(i===16?4:3)+(r&&u?1:0);c=a+(1<<f-1)>>f}else c=128;m.fill(c,0,i*i);return}if(t===X5){for(let c=0;c<i;c++){let a=r?e[(s-1)*n+o+c]:127;for(let f=0;f<i;f++)m[f*i+c]=a}return}if(t===Q5){for(let c=0;c<i;c++){let a=u?e[(s+c)*n+o-1]:129;m.fill(a,c*i,c*i+i)}return}let l=r?u?e[(s-1)*n+o-1]:129:127;for(let c=0;c<i;c++){let a=u?e[(s+c)*n+o-1]:129;for(let f=0;f<i;f++){let p=r?e[(s-1)*n+o+f]:127;m[c*i+f]=n1(a+p-l)}}}function q5(t,e,n,o,s,i){let r=0;for(let u=0;u<s;u++){let m=(o+u)*e+n,l=u*s;for(let c=0;c<s;c++){let a=t[m+c]-i[l+c];r+=a<0?-a:a}}return r}function $5(t,e,n,o,s,i){for(let r=0;r<i;r++)e.set(t.subarray(r*i,r*i+i),(s+r)*n+o)}function E2(t,e){let{mbW:n,mbH:o,yStride:s,uvStride:i}=t,r=g5(e.qi,127),u=J2(r),m=z2(r,e.filterStrength??60),l=new Uint8Array(t.y.length),c=new Uint8Array(t.u.length),a=new Uint8Array(t.v.length),f=new Int16Array(16),p=new Int16Array(16),g=new Int16Array(16),d=new Int16Array(16),b=new Uint8Array(256),A=new Uint8Array(256),x=new Uint8Array(64),E=new Uint8Array(64),I=new Uint8Array(64),h=new Uint8Array(64),F=Array.from({length:16},()=>new Int16Array(16)),Y=new Int16Array(16),C5=new Array(n*o),K5=0;for(let T=0;T<o;T++)for(let L=0;L<n;L++){let v=T>0,M=L>0,_=L*16,S=T*16,U=L*8,C=T*8,w=U5,D=1/0;for(let B=0;B<4;B++){G5(B,l,s,_,S,16,v,M,b);let V=q5(t.y,s,_,S,16,b);V<D&&(D=V,w=B,A.set(b))}$5(A,l,s,_,S,16);let G=new Array(16);for(let B=0;B<16;B++){let V=_+(B&3)*4,O=(S+(B>>2)*4)*s+V;O5(t.y,O,l,O,s,f),p[B]=f[0],G[B]=H5(f,F[B],1,u.y1)}U2(p,g);let s5=H5(g,d,0,u.y2);g2(d,p);for(let B=0;B<16;B++){let V=_+(B&3)*4,O=(S+(B>>2)*4)*s+V;F[B][0]=p[B],D5(F[B],l,O,s,l,O)}let J5=U5,z5=1/0;for(let B=0;B<4;B++){G5(B,c,i,U,C,8,v,M,x),G5(B,a,i,U,C,8,v,M,E);let V=q5(t.u,i,U,C,8,x)+q5(t.v,i,U,C,8,E);V<z5&&(z5=V,J5=B,I.set(x),h.set(E))}$5(I,c,i,U,C,8),$5(h,a,i,U,C,8);let I5=new Array(8);for(let B=0;B<2;B++){let V=B===0?t.u:t.v,j=B===0?c:a;for(let O=0;O<4;O++){let T2=U+(O&1)*4,u5=(C+(O>>1)*4)*i+T2;O5(V,u5,j,u5,i,f),I5[B*4+O]=H5(f,Y,0,u.uv),D5(Y,j,u5,i,j,u5)}}let $=s5.last<0;if($){for(let B of G)if(B.last>=0){$=!1;break}}if($){for(let B of I5)if(B.last>=0){$=!1;break}}$&&K5++,C5[T*n+L]={yMode:w,uvMode:J5,skip:$,y2:s5,luma:G,uv:I5}}let P5=n*o,M5=P5?(P5-K5)*255/P5|0:255,e5=M5<250,r5=new Uint32Array(1056*2);{let T=Array.from({length:n},()=>new Array(9).fill(0));for(let L=0;L<o;L++){let v=new Array(9).fill(0);for(let M=0;M<n;M++){let _=C5[L*n+M],S=T[M];if(e5&&_.skip){S.fill(0),v.fill(0);continue}S[8]=v[8]=W5(r5,S[8]+v[8],1,0,_.y2);for(let U=0;U<4;U++)for(let C=0;C<4;C++){let w=S[C]+v[U],D=W5(r5,w,0,1,_.luma[U*4+C]);S[C]=v[U]=D}for(let U=0;U<=2;U+=2)for(let C=0;C<2;C++)for(let w=0;w<2;w++){let D=S[4+U+w]+v[4+U+C],G=W5(r5,D,2,0,_.uv[(U>>1)*4+C*2+w]);S[4+U+w]=v[4+U+C]=G}}}}let o5=t1(r5),P=new n5,i5=new n5;P.putBitUniform(0),P.putBitUniform(0),P.putBitUniform(0),P.putBitUniform(0),P.putBits(m,6),P.putBits(0,3),P.putBitUniform(0),P.putBits(0,2),P.putBits(r,7),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putSignedBits(0,4),P.putBitUniform(0);for(let T=0;T<4;T++)for(let L=0;L<8;L++)for(let v=0;v<3;v++)for(let M=0;M<11;M++){let _=o5[T][L][v][M],S=_!==l5[T][L][v][M];P.putBit(S,L5[T][L][v][M])&&P.putBits(_,8)}P.putBitUniform(e5)&&P.putBits(M5,8);let I2=o5[1],L2=o5[0],v2=o5[2],_2=Array.from({length:n},()=>new Array(9).fill(0));for(let T=0;T<o;T++){let L=new Array(9).fill(0);for(let v=0;v<n;v++){let M=C5[T*n+v];e5&&P.putBit(M.skip,M5),P.putBit(1,145),P.putBit(M.yMode===P2||M.yMode===Q5,156)?P.putBit(M.yMode===P2,128):P.putBit(M.yMode===X5,163),P.putBit(M.uvMode!==U5,142)&&P.putBit(M.uvMode!==X5,114)&&P.putBit(M.uvMode!==Q5,183);let _=_2[v];if(e5&&M.skip){_.fill(0),L.fill(0);continue}let S=Y5(i5,_[8]+L[8],I2,0,M.y2);_[8]=L[8]=S;for(let U=0;U<4;U++)for(let C=0;C<4;C++){let w=_[C]+L[U],D=Y5(i5,w,L2,1,M.luma[U*4+C]);_[C]=L[U]=D}for(let U=0;U<=2;U+=2)for(let C=0;C<2;C++)for(let w=0;w<2;w++){let D=_[4+U+w]+L[4+U+C],G=M.uv[(U>>1)*4+C*2+w],s5=Y5(i5,D,v2,0,G);_[4+U+w]=L[4+U+C]=s5}}}let q=P.finish(),j5=i5.finish();if(q.length>=1<<19)throw new RangeError(`VP8: partition 0 is ${q.length} bytes, exceeding the bitstream's 2^19-1 limit \u2014 image too large/complex for a VP8 keyframe`);let E5=16|q.length<<5,k=new Uint8Array(10+q.length+j5.length);return k[0]=E5&255,k[1]=E5>>>8&255,k[2]=E5>>>16&255,k[3]=157,k[4]=1,k[5]=42,k[6]=t.width&255,k[7]=t.width>>8&63,k[8]=t.height&255,k[9]=t.height>>8&63,k.set(q,10),k.set(j5,10+q.length),k}var Z5=16383;function e1(t,e,n){if(!Number.isInteger(t)||!Number.isInteger(e))throw new RangeError(`encodeWebP: width and height must be integers (got ${t}\xD7${e})`);if(t<1||e<1)throw new RangeError(`encodeWebP: width and height must be \u2265 1 (got ${t}\xD7${e})`);if(t>Z5||e>Z5)throw new RangeError(`encodeWebP: width and height must be \u2264 ${Z5} (got ${t}\xD7${e})`);let o=t*e*4;if(n<o)throw new RangeError(`encodeWebP: data too short \u2014 need ${o} bytes for a ${t}\xD7${e} RGBA image, got ${n}`)}function r1(t){if(t.alphaDither!==void 0&&!Number.isFinite(t.alphaDither))throw new RangeError(`encodeWebP: alphaDither must be a finite number (got ${t.alphaDither})`)}function o1(t){return t!==void 0&&t>=0&&t<=100?t:80}function i1(t){let e=(t<0?0:t>100?100:t)/100,n=e<.75?e*(2/3):2*e-1,o=Math.cbrt(n);return Math.round(127*(1-o))}function v1(t,e={}){let{width:n,height:o}=t;e1(n,o,t.data.length),r1(e);let s=(t.data instanceof Uint8Array?t.data:new Uint8Array(t.data.buffer,t.data.byteOffset,t.data.byteLength)).subarray(0,n*o*4),i=f2(s),r=!1;for(let f=0;f<i.length;f++)if(i[f]!==255){r=!0;break}let u=e.lossless??"auto",m=null;if(u!==!1){let f=s2(s,n,o);if(f&&(m=b2(f)),m&&u===!0)return m}let l=d2(s,n,o,r?i:void 0);r&&B2(l,i);let c=E2(l,{qi:i1(o1(e.quality))}),a;if(!r)a=R5({width:n,height:o,vp8:c});else{let f=a2(i,n,o,e.alphaLevels??16,e.alphaDither??1,e.alphaAdaptive??!0);a=R5({width:n,height:o,vp8:c,alph:f.payload})}return m&&m.length<=a.length?m:a}export{v1 as encodeWebP,R2 as hasNativeWebPEncoder};
package/dist/yuv.d.ts CHANGED
@@ -10,7 +10,14 @@ export interface YuvImage {
10
10
  v: Uint8Array;
11
11
  }
12
12
  /**
13
- * Convert interleaved RGBA to padded YUV420. Alpha is ignored (the alpha
14
- * plane travels separately in the ALPH chunk).
13
+ * Convert interleaved RGBA to padded YUV420. When `alpha` (a width*height
14
+ * plane) is provided, 2x2 chroma blocks with mixed transparency are averaged
15
+ * with per-pixel alpha weights so fully transparent pixels contribute nothing.
15
16
  */
16
- export declare function rgbaToYuv420(rgba: Uint8Array | Uint8ClampedArray, width: number, height: number): YuvImage;
17
+ export declare function rgbaToYuv420(rgba: Uint8Array | Uint8ClampedArray, width: number, height: number, alpha?: Uint8Array): YuvImage;
18
+ /**
19
+ * Smooth luma under transparent pixels (8x8-block-local visible average) and
20
+ * flatten fully transparent 8x8 blocks, then refresh the replicated padding.
21
+ * Call after rgbaToYuv420 whenever the image has transparency.
22
+ */
23
+ export declare function smoothTransparentAreas(yuv: YuvImage, alpha: Uint8Array): void;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@graysonlang/slim-webp-enc",
3
- "version": "1.0.0",
4
- "description": "Pure-TypeScript lossy WebP thumbnail encoder with alpha support",
3
+ "version": "1.1.0",
4
+ "description": "Pure-TypeScript lossy WebP encoder with alpha support",
5
5
  "keywords": [
6
6
  "webp",
7
7
  "webp-encoder",