imagepressor 0.2.3 → 0.2.4
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 +13 -8
- package/dist/canvas.d.cts +19 -0
- package/dist/canvas.d.ts +14 -1
- package/dist/canvas.d.ts.map +1 -1
- package/dist/index.cjs +52 -13
- package/dist/index.cjs.map +8 -8
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +7 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +52 -13
- package/dist/index.js.map +8 -8
- package/dist/jpeg.d.cts +54 -0
- package/dist/jpeg.d.ts +26 -4
- package/dist/jpeg.d.ts.map +1 -1
- package/dist/types.d.cts +40 -0
- package/dist/types.d.ts +0 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils.d.cts +41 -0
- package/dist/utils.d.ts +18 -3
- package/dist/utils.d.ts.map +1 -1
- package/dist/webp.d.cts +37 -0
- package/dist/webp.d.ts +1 -1
- package/dist/webp.d.ts.map +1 -1
- package/dist/worker.d.cts +13 -0
- package/dist/worker.d.ts +8 -1
- package/dist/worker.d.ts.map +1 -1
- package/package.json +16 -6
package/README.md
CHANGED
|
@@ -14,11 +14,12 @@
|
|
|
14
14
|
## Highlights
|
|
15
15
|
|
|
16
16
|
- **Off-Main-Thread Processing:** Compresses and resizes images inside a dedicated Web Worker via `createImageBitmap` and `OffscreenCanvas`. The UI remains smooth and responsive at 60 fps, even during 4K or 30MB+ camera photo uploads.
|
|
17
|
-
- **
|
|
18
|
-
- **Bi-Directional EXIF &
|
|
17
|
+
- **CSP-Aware Inline Worker:** Uses a self-contained inline `blob:` Web Worker with no external CDN scripts (requires `worker-src blob:` or `child-src blob:` under strict CSP), automatically falling back to main-thread execution if workers are blocked.
|
|
18
|
+
- **Bi-Directional EXIF & ICC Color Profile Preservation (JPEG & WebP):**
|
|
19
19
|
- **JPEG:** Non-destructively preserves and re-injects raw APP1 (EXIF) and APP2 (ICC profile) segments.
|
|
20
20
|
- **WebP:** Built-in pure TypeScript WebP RIFF muxer upgrades simple VP8 bitstreams into extended `VP8X` containers with dedicated `EXIF` and `ICCP` chunks (no WASM, no Node APIs).
|
|
21
|
-
-
|
|
21
|
+
- Requests a wide-gamut `display-p3` 2D canvas when the source image carries a Display P3 ICC profile, and avoids re-attaching mismatched wide-gamut ICC profiles onto sRGB-clamped canvas pixels so colors stay faithful on wide-gamut monitors.
|
|
22
|
+
- Preserves camera make/model, focal length, ISO, shutter speed, and capture timestamps without washed-out or oversaturated colors on iPhones, MacBooks, and wide-gamut displays.
|
|
22
23
|
- **Non-Destructive Orientation Normalization:** Normalizes EXIF orientation tags in place to `1` (Standard), preventing double-rotation issues in external viewers.
|
|
23
24
|
- **Zero-Memory Chunk-Hopping Inspector:** `getImageMetadata()` reads RIFF headers and hops past multi-megabyte image pixel frames directly to the metadata chunks, keeping memory footprint minimal.
|
|
24
25
|
- **Smart Anti-Expansion Guards & Strict Mode:**
|
|
@@ -163,7 +164,7 @@ console.log(metadata.orientation); // 1
|
|
|
163
164
|
| `mimeType` | `"image/jpeg" \| "image/webp"` | input type | Target output format (`"image/jpeg"`, `"image/webp"`). Defaults to input MIME type (WebP/PNG $\rightarrow$ WebP, JPEG $\rightarrow$ JPEG). |
|
|
164
165
|
| `preserveMetadata` | `boolean` | `true` | When `true`, extracts and re-injects camera EXIF and the embedded ICC color profile for both JPEG and WebP outputs. Best-effort: corrupt metadata never fails a compression. |
|
|
165
166
|
| `strict` | `boolean` | `true` (same format) | When `true`, returns the original image if compression output turns out larger than the original image without dimension downscaling. Ignored when `maxSizeMB` is set: the smallest attempt is returned so the requested ceiling wins. |
|
|
166
|
-
| `useWebWorker` | `boolean` | `true` | Runs off the main thread in an inline Web Worker. Automatically falls back to main-thread processing if workers or `OffscreenCanvas` are unavailable. |
|
|
167
|
+
| `useWebWorker` | `boolean` | `true` | Runs off the main thread in an inline `blob:` Web Worker (requires `worker-src blob:` under strict CSP). Automatically falls back to main-thread processing if workers or `OffscreenCanvas` are blocked or unavailable. |
|
|
167
168
|
| `signal` | `AbortSignal` | `undefined` | An `AbortSignal` to cancel compression immediately and clean up worker memory. |
|
|
168
169
|
| `onProgress` | `(progress: number, stage: string) => void` | `undefined` | Callback receiving `(percent: number, stage: string)`. |
|
|
169
170
|
|
|
@@ -180,16 +181,20 @@ console.log(metadata.orientation); // 1
|
|
|
180
181
|
| `spliceWebpMetadata(blob, segments, w, h)` | Muxes binary metadata segments into an extended WebP container. |
|
|
181
182
|
| `insertWebPExif(webpBytes, rawExif, rawIcc, w, h)` | Low-level pure TypeScript WebP RIFF muxer that upgrades simple VP8 WebP to extended VP8X. |
|
|
182
183
|
| `normalizeExifOrientation(segment)` | Normalizes EXIF orientation tag in-place to `1` (Standard). |
|
|
184
|
+
| `detectInputMime(fileOrBlob, fileName)` | Infers `"image/jpeg"`, `"image/webp"`, or `"image/png"` from MIME type or filename. |
|
|
185
|
+
| `compressWithWorker(...)` / `compressWithMainThread(...)` | Low-level single-pass encoding helpers. |
|
|
186
|
+
|
|
187
|
+
> Deprecated aliases `getJpegMetadata` and `JpegMetadata` remain exported for backwards compatibility; prefer `getImageMetadata` and `ImageMetadata`.
|
|
183
188
|
|
|
184
189
|
---
|
|
185
190
|
|
|
186
191
|
## Browser Support
|
|
187
192
|
|
|
188
|
-
`imagepressor`
|
|
193
|
+
`imagepressor` targets Baseline 2023+ Web APIs (`OffscreenCanvas`, `createImageBitmap`, `AbortSignal.throwIfAborted()`, `Array.prototype.toSorted()`):
|
|
189
194
|
|
|
190
|
-
- **Chrome / Edge / Chromium:**
|
|
191
|
-
- **Firefox:**
|
|
192
|
-
- **Safari / iOS Safari:** 16.4+ (
|
|
195
|
+
- **Chrome / Edge / Chromium:** 112+ (full Web Worker + `OffscreenCanvas` + `display-p3` canvas support)
|
|
196
|
+
- **Firefox:** 115+
|
|
197
|
+
- **Safari / iOS Safari:** 16.4+ (Note: WebKit decodes WebP, but `canvas.toBlob` / `OffscreenCanvas.convertToBlob` requests for `"image/webp"` currently fall back to `"image/png"` in Safari. `imagepressor` inspects the actual output format, skips invalid WebP muxing, and returns original WebP/PNG inputs when fallback output would expand file size.)
|
|
193
198
|
- **Node.js / SSR:** Safely passes through input files without crashing during server-side rendering.
|
|
194
199
|
|
|
195
200
|
---
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { CompressionResult } from "./types.cjs";
|
|
2
|
+
export type CanvasColorSpace = "srgb" | "display-p3";
|
|
3
|
+
export interface EncodedFrame extends CompressionResult {
|
|
4
|
+
colorSpace?: CanvasColorSpace;
|
|
5
|
+
}
|
|
6
|
+
export interface CompressionSession {
|
|
7
|
+
encode(maxWidth: number | undefined, maxHeight: number | undefined, quality: number, mimeType: string, signal?: AbortSignal): Promise<EncodedFrame>;
|
|
8
|
+
dispose(): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a reusable main-thread encoding session that decodes `ImageBitmap`
|
|
12
|
+
* once across multiple `maxSizeMB` retry passes when `OffscreenCanvas` is available.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createMainThreadEncoder(blob: Blob, colorSpace?: CanvasColorSpace): CompressionSession;
|
|
15
|
+
/**
|
|
16
|
+
* Main-thread image compressor fallback.
|
|
17
|
+
* Uses OffscreenCanvas if available, otherwise falls back to HTMLCanvasElement.
|
|
18
|
+
*/
|
|
19
|
+
export declare function compressWithMainThread(blob: Blob, maxWidth: number | undefined, maxHeight: number | undefined, quality: number, mimeType: string, signal?: AbortSignal): Promise<CompressionResult>;
|
package/dist/canvas.d.ts
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import type { CompressionResult } from "./types";
|
|
1
|
+
import type { CompressionResult } from "./types.js";
|
|
2
|
+
export type CanvasColorSpace = "srgb" | "display-p3";
|
|
3
|
+
export interface EncodedFrame extends CompressionResult {
|
|
4
|
+
colorSpace?: CanvasColorSpace;
|
|
5
|
+
}
|
|
6
|
+
export interface CompressionSession {
|
|
7
|
+
encode(maxWidth: number | undefined, maxHeight: number | undefined, quality: number, mimeType: string, signal?: AbortSignal): Promise<EncodedFrame>;
|
|
8
|
+
dispose(): void;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Creates a reusable main-thread encoding session that decodes `ImageBitmap`
|
|
12
|
+
* once across multiple `maxSizeMB` retry passes when `OffscreenCanvas` is available.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createMainThreadEncoder(blob: Blob, colorSpace?: CanvasColorSpace): CompressionSession;
|
|
2
15
|
/**
|
|
3
16
|
* Main-thread image compressor fallback.
|
|
4
17
|
* Uses OffscreenCanvas if available, otherwise falls back to HTMLCanvasElement.
|
package/dist/canvas.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"canvas.d.ts","sourceRoot":"","sources":["../src/canvas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD;;;GAGG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,iBAAiB,CAAC,
|
|
1
|
+
{"version":3,"file":"canvas.d.ts","sourceRoot":"","sources":["../src/canvas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAEjD,MAAM,MAAM,gBAAgB,GAAG,MAAM,GAAG,YAAY,CAAC;AACrD,MAAM,WAAW,YAAa,SAAQ,iBAAiB;IACrD,UAAU,CAAC,EAAE,gBAAgB,CAAC;CAC/B;AACD,MAAM,WAAW,kBAAkB;IACjC,MAAM,CACJ,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,YAAY,CAAC,CAAC;IACzB,OAAO,IAAI,IAAI,CAAC;CACjB;AAmHD;;;GAGG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,IAAI,EACV,UAAU,CAAC,EAAE,gBAAgB,GAC5B,kBAAkB,CA+DpB;AACD;;;GAGG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,IAAI,EACV,QAAQ,EAAE,MAAM,GAAG,SAAS,EAC5B,SAAS,EAAE,MAAM,GAAG,SAAS,EAC7B,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,MAAM,CAAC,EAAE,WAAW,GACnB,OAAO,CAAC,iBAAiB,CAAC,CAc5B"}
|
package/dist/index.cjs
CHANGED
|
@@ -1,27 +1,66 @@
|
|
|
1
|
-
var{defineProperty:V,getOwnPropertyNames:ge,getOwnPropertyDescriptor:me}=Object,he=Object.prototype.hasOwnProperty;function xe(e){return this[e]}var be=(e)=>{var t=(K??=new WeakMap).get(e),r;if(t)return t;if(t=V({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var n of ge(e))if(!he.call(t,n))V(t,n,{get:xe.bind(e,n),enumerable:!(r=me(e,n))||r.enumerable})}return K.set(e,t),t},K;var ye=(e)=>e;function we(e,t){this[e]=ye.bind(null,t)}var Ce=(e,t)=>{for(var r in t)V(e,r,{get:t[r],enumerable:!0,configurable:!0,set:we.bind(t,r)})};var _e={};Ce(_e,{compressImage:()=>ue,compressWithMainThread:()=>y,compressWithWorker:()=>D,default:()=>ve,detectInputMime:()=>W,extractMetadataSegments:()=>R,getImageMetadata:()=>X,getJpegMetadata:()=>te,insertWebPExif:()=>j,normalizeExifOrientation:()=>S,spliceMetadataIntoBlob:()=>_,spliceWebpMetadata:()=>B});module.exports=be(_e);function W(e,t){let r=e.type;if(r==="image/jpeg"||r==="image/webp"||r==="image/png")return r;let n=t??(e instanceof File?e.name:"");if(/\.(jpe?g)$/i.test(n))return"image/jpeg";if(/\.webp$/i.test(n))return"image/webp";if(/\.png$/i.test(n))return"image/png";return}async function Q(e){if(e.size<4)return null;let t=await e.slice(0,12).arrayBuffer(),r=new DataView(t);if(r.byteLength>=2&&r.getUint16(0)===65496)return"jpeg";if(r.byteLength>=12&&r.getUint32(0,!1)===1380533830&&r.getUint32(8,!1)===1464156752)return"webp";return null}function F(e,t,r,n){if(r&&e>r)t=Math.max(1,Math.round(t*r/e)),e=Math.max(1,Math.round(r));if(n&&t>n)e=Math.max(1,Math.round(e*n/t)),t=Math.max(1,Math.round(n));return{width:e,height:t}}function A(e){e?.throwIfAborted()}async function y(e,t,r,n,a,i){if(A(i),typeof OffscreenCanvas<"u"&&typeof createImageBitmap<"u"){let s=await createImageBitmap(e),{width:u,height:c}=F(s.width,s.height,t,r),o=new OffscreenCanvas(u,c),f=o.getContext("2d");if(!f)throw Error("OffscreenCanvas 2D context unavailable");return f.drawImage(s,0,0,u,c),s.close(),{blob:await o.convertToBlob({type:a,quality:n}),width:u,height:c}}return new Promise((s,u)=>{if(typeof window>"u"||typeof document>"u"){s({blob:e,width:0,height:0});return}let c=new Image,o=URL.createObjectURL(e),f=!1,p=()=>{if(f)return;f=!0,URL.revokeObjectURL(o),u(new DOMException("Compression aborted","AbortError"))},d=(l)=>{if(f)return;f=!0,i?.removeEventListener("abort",p),l()};if(i){if(i.aborted){p();return}i.addEventListener("abort",p,{once:!0})}c.onload=()=>{if(f)return;URL.revokeObjectURL(o);let{width:l,height:m}=F(c.naturalWidth,c.naturalHeight,t,r),g=document.createElement("canvas");g.width=l,g.height=m;let w=g.getContext("2d");if(!w){d(()=>u(Error("Canvas 2D context unavailable")));return}w.drawImage(c,0,0,l,m),g.toBlob((C)=>{if(C)d(()=>s({blob:C,width:l,height:m}));else d(()=>u(Error("Canvas compression failed to produce blob")))},a,n)},c.onerror=()=>{URL.revokeObjectURL(o),d(()=>u(Error("Failed to load image for compression")))},c.src=o})}var I=[69,120,105,102,0,0],U=[73,67,67,95,80,82,79,70,73,76,69,0],N=65533-U.length-2;function z(e,t,r=0){if(e.length<r+t.length)return!1;for(let n=0;n<t.length;n++)if(e[r+n]!==t[n])return!1;return!0}function Y(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function Ae(e,t,r){return new Uint8Array(e.buffer.slice(e.byteOffset+t,e.byteOffset+r))}async function*Ie(e,t){let r=0,n=t,a=2;while(!0){if(a+4>n.byteLength){let o=r+a;if(o+4>e.size)return;let f=await e.slice(o,o+262144).arrayBuffer();if(f.byteLength<4)return;r=o,n=new DataView(f),a=0;continue}let i=n.getUint16(a);if(i===65498||i===65497||(i&65280)!==65280)return;let s=n.getUint16(a+2);if(s<2)return;let u=a+2+s,c=r+u;if(c>e.size)return;if(i===65505||i===65506){let o=u<=n.byteLength?Ae(n,a,u):new Uint8Array(await e.slice(r+a,c).arrayBuffer());yield{kind:i===65505?"jpeg-app1":"jpeg-app2",bytes:o}}a=u}}async function*Ue(e,t){let r=e.size,n=12;while(n+8<=r){let a,i;if(n+8<=t.byteLength)a=t.getUint32(n,!1),i=t.getUint32(n+4,!0);else{let f=await e.slice(n,n+8).arrayBuffer();if(f.byteLength<8)return;let p=new DataView(f);a=p.getUint32(0,!1),i=p.getUint32(4,!0)}let s=i+(i&1),u=a===1163413830,c=a===1229144912;if(u&&i>=6||c){let f=n+8+i;if(f>r)return;if(i>0){let p=new Uint8Array(await e.slice(n+8,f).arrayBuffer());yield{kind:u?"webp-exif":"webp-iccp",bytes:p}}}let o=n+8+s;if(o<=n)return;n=o}}async function*ee(e){let t=Math.min(e.size,262144),r=await e.slice(0,t).arrayBuffer(),n=new DataView(r);if(n.byteLength<4)return;if(n.getUint16(0)===65496){yield*Ie(e,n);return}if(n.byteLength>=12&&n.getUint32(0,!1)===1380533830&&n.getUint32(8,!1)===1464156752)yield*Ue(e,n)}function Ee(e){let t=z(e,I)?e.subarray(I.length):e,r=2+I.length+t.length,n=new Uint8Array(2+r);return n[0]=255,n[1]=225,n[2]=r>>8&255,n[3]=r&255,n.set(I,4),n.set(t,4+I.length),n}function Me(e){let t=Math.max(1,Math.ceil(e.length/N));if(t>255)return[];let r=[];for(let n=0;n<t;n++){let a=e.subarray(n*N,(n+1)*N),i=2+U.length+2+a.length,s=new Uint8Array(2+i);s[0]=255,s[1]=226,s[2]=i>>8&255,s[3]=i&255,s.set(U,4),s[4+U.length]=n+1,s[4+U.length+1]=t,s.set(a,4+U.length+2),r.push(s)}return r}async function R(e){let t=[];for await(let r of ee(e))switch(r.kind){case"jpeg-app1":{S(r.bytes),t.push(r.bytes);break}case"jpeg-app2":{t.push(r.bytes);break}case"webp-exif":{let n=Ee(r.bytes);S(n),t.push(n);break}case"webp-iccp":{t.push(...Me(r.bytes));break}}return t}function S(e){if(e.length<14)return!1;if(!(e[0]===255&&e[1]===225&&e[4]===69&&e[5]===120&&e[6]===105&&e[7]===102&&e[8]===0&&e[9]===0))return!1;let r=10,n=new DataView(e.buffer,e.byteOffset,e.byteLength),a=n.getUint16(r)===18761;if(!a&&n.getUint16(r)!==19789)return!1;if(n.getUint16(r+2,a)!==42)return!1;let i=n.getUint32(r+4,a),s=r+i;if(s+2>e.length)return!1;let u=n.getUint16(s,a);for(let c=0;c<u;c++){let o=s+2+c*12;if(o+12>e.length)break;if(n.getUint16(o,a)===274)return n.setUint16(o+8,1,a),!0}return!1}async function _(e,t){if(t.length===0)return e;let r=await e.slice(0,32).arrayBuffer(),n=new DataView(r);if(n.byteLength<4||n.getUint16(0)!==65496)return e;let a=2;if(n.byteLength>=6&&n.getUint16(2)===65504)a=4+n.getUint16(4);let i=e.slice(0,a),s=e.slice(a),u=t.map((c)=>c.buffer.slice(c.byteOffset,c.byteOffset+c.byteLength));return new Blob([i,...u,s],{type:e.type||"image/jpeg"})}async function X(e){let t={hasExif:!1,hasIccProfile:!1,colorSpace:"sRGB (Standard)"};for await(let r of ee(e))if(r.kind==="jpeg-app1"){if(z(r.bytes,I,4))t.hasExif=!0,q(Y(r.bytes),10,t)}else if(r.kind==="webp-exif")t.hasExif=!0,q(Y(r.bytes),z(r.bytes,I)?I.length:0,t);else if(r.kind==="webp-iccp"||r.bytes.length>=18&&z(r.bytes,U,4))t.hasIccProfile=!0,t.colorSpace="ICC profile embedded";return t}var te=X;function q(e,t,r){if(t+8>e.byteLength)return;let n=e.getUint16(t)===18761;if(!n&&e.getUint16(t)!==19789)return;if(e.getUint16(t+2,n)!==42)return;let a=e.getUint32(t+4,n),i=t+a;if(i+2>e.byteLength)return;let s,u=e.getUint16(i,n);for(let c=0;c<u;c++){let o=i+2+c*12;if(o+12>e.byteLength)break;let f=e.getUint16(o,n),p=e.getUint16(o+2,n),d=e.getUint32(o+4,n);if(f===274)r.orientation=e.getUint16(o+8,n);else if(f===271&&p===2)r.make=k(e,t,o,d,n);else if(f===272&&p===2)r.model=k(e,t,o,d,n);else if(f===306&&p===2)r.dateTime=k(e,t,o,d,n);else if(f===34665&&(p===3||p===4))s=e.getUint32(o+8,n)}if(s){let c=t+s;if(c+2<=e.byteLength){let o=e.getUint16(c,n);for(let f=0;f<o;f++){let p=c+2+f*12;if(p+12>e.byteLength)break;let d=e.getUint16(p,n),l=e.getUint16(p+2,n),m=e.getUint32(p+4,n);if((d===36867||d===36868)&&l===2&&!r.dateTime)r.dateTime=k(e,t,p,m,n)}}}}function k(e,t,r,n,a){if(n<=4){let o="";for(let f=0;f<n;f++){let p=e.getUint8(r+8+f);if(p===0)break;o+=String.fromCharCode(p)}return o.trim()}let i=e.getUint32(r+8,a),s=t+i;if(s>=e.byteLength)return"";let u=Math.min(n,e.byteLength-s),c="";for(let o=0;o<u;o++){let f=e.getUint8(s+o);if(f===0)break;c+=String.fromCharCode(f)}return c.trim()}var ne=new Set(["VP8 ","VP8L","ALPH","ANIM","ANMF"]);function O(e,t){return String.fromCharCode(e[t],e[t+1],e[t+2],e[t+3])}function re(e,t){return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(t,!0)}function ae(e,t,r){new DataView(e.buffer,e.byteOffset,e.byteLength).setUint32(t,r,!0)}function ie(e,t){return e[t]|e[t+1]<<8|e[t+2]<<16}function oe(e,t,r){e[t]=r&255,e[t+1]=r>>>8&255,e[t+2]=r>>>16&255}function Fe(e){if(e.length<12)throw Error("Invalid WebP: file is too small");if(O(e,0)!=="RIFF"||O(e,8)!=="WEBP")throw Error("Invalid WebP: missing RIFF/WEBP header");let t=re(e,4);if(Math.abs(t+8-e.length)>1&&t+8>e.length)throw Error("Invalid WebP: RIFF size mismatch");let r=[],n=12;while(n<e.length){if(n+8>e.length)break;let a=O(e,n),i=re(e,n+4),s=n+8,u=s+i,c=u+(i&1);if(u>e.length)break;r.push({fourCC:a,data:e.subarray(s,u)}),n=Math.min(c,e.length)}return r}function Pe(e){let t=e.find((a)=>a.fourCC==="VP8X");if(t&&t.data.length>=10){let a=1+ie(t.data,4),i=1+ie(t.data,7),s=(t.data[0]&16)!==0;return{width:a,height:i,alpha:s}}let r=e.find((a)=>a.fourCC==="VP8 "),n=e.find((a)=>a.fourCC==="VP8L");if(r){let a=r.data;if(a.length>=10&&a[3]===157&&a[4]===1&&a[5]===42){let i=(a[6]|a[7]<<8)&16383,s=(a[8]|a[9]<<8)&16383;if(i&&s)return{width:i,height:s,alpha:!1}}}if(n){let a=n.data;if(a.length>=5&&a[0]===47){let i=a[1]|a[2]<<8|a[3]<<16|a[4]<<24,s=(i&16383)+1,u=(i>>>14&16383)+1,c=(i>>>28&1)!==0;return{width:s,height:u,alpha:c}}}return null}function Le(e,t,r,n,a){let i=Pe(e),s=i?.width??n??1,u=i?.height??a??1,c=i?.alpha??!1,o=0;if(t)o|=8;if(r||e.some((p)=>p.fourCC==="ICCP"))o|=32;if(c||e.some((p)=>p.fourCC==="ALPH"))o|=16;if(e.some((p)=>p.fourCC==="XMP "))o|=4;if(e.some((p)=>p.fourCC==="ANIM"||p.fourCC==="ANMF"))o|=2;let f=new Uint8Array(10);return f[0]=o,oe(f,4,Math.max(0,s-1)),oe(f,7,Math.max(0,u-1)),{fourCC:"VP8X",data:f}}var se=[69,120,105,102,0,0],fe=[73,67,67,95,80,82,79,70,73,76,69,0];function v(e,t,r){if(e.length<t+r.length)return!1;for(let n=0;n<r.length;n++)if(e[t+n]!==r[n])return!1;return!0}function We(e){if(e.length===0)throw Error("EXIF data cannot be empty");let t=e;if(t.length>=4&&t[0]===255&&t[1]===225)t=t.subarray(4);let r=v(t,0,se)?t.subarray(6):t;if(r.length<8)throw Error("Invalid EXIF TIFF header");let n=r[0]===73&&r[1]===73,a=r[0]===77&&r[1]===77;if(!n&&!a)throw Error("Invalid EXIF byte order");if((n?r[2]|r[3]<<8:r[2]<<8|r[3])!==42)throw Error("Invalid EXIF TIFF magic");return r}function ke(e){let t=e;if(t.length>=4&&t[0]===255&&t[1]===226)t=t.subarray(4);if(t.length>=14&&v(t,0,fe))return t.subarray(14);return t}function ze(e){let t=12;for(let a of e)t+=8+a.data.length+(a.data.length&1);if(t-8>4294967295)throw Error("WebP file exceeds RIFF size limit");let r=new Uint8Array(t);r.set([82,73,70,70],0),ae(r,4,t-8),r.set([87,69,66,80],8);let n=12;for(let a of e){for(let i=0;i<4;i++)r[n+i]=a.fourCC.charCodeAt(i);if(ae(r,n+4,a.data.length),r.set(a.data,n+8),n+=8+a.data.length,a.data.length&1)r[n]=0,n++}return r}function j(e,t,r,n,a){if(!(e instanceof Uint8Array))throw TypeError("webp must be a Uint8Array");let i=t?We(t):void 0,s=r?ke(r):void 0,u=Fe(e),c=u.find((g)=>g.fourCC==="EXIF"),o=u.find((g)=>g.fourCC==="ICCP"),f=i?{fourCC:"EXIF",data:i}:c,p=s?{fourCC:"ICCP",data:s}:o,d=u.filter((g)=>g.fourCC!=="VP8X"&&g.fourCC!=="EXIF"&&g.fourCC!=="ICCP"),l=[];if(p)l.push(p);if(l.push(...d.filter((g)=>ne.has(g.fourCC))),f)l.push(f);l.push(...d.filter((g)=>!ne.has(g.fourCC)));let m=Le(l,Boolean(f),Boolean(p),n,a);return ze([m,...l])}async function B(e,t,r,n){let a,i=[];for(let o of t)if(!a&&o.length>=14&&o[0]===255&&o[1]===225&&v(o,4,se))a=o;else if(o.length>=18&&o[0]===255&&o[1]===226&&v(o,4,fe))i.push(o);let s;if(i.length===1)s=i[0];else if(i.length>1){let o=i.toSorted((l,m)=>l[16]-m[16]),f=o.reduce((l,m)=>l+(m.length-18),0),p=new Uint8Array(f),d=0;for(let l of o)p.set(l.subarray(18),d),d+=l.length-18;s=p}if(!a&&!s)return e;let u=new Uint8Array(await e.arrayBuffer()),c=j(u,a,s,r,n);return new Blob([c.buffer],{type:"image/webp"})}var Se=`
|
|
2
|
-
|
|
1
|
+
var{defineProperty:q,getOwnPropertyNames:Re,getOwnPropertyDescriptor:ze}=Object,De=Object.prototype.hasOwnProperty;function je(e){return this[e]}var Oe=(e)=>{var t=(fe??=new WeakMap).get(e),r;if(t)return t;if(t=q({},"__esModule",{value:!0}),e&&typeof e==="object"||typeof e==="function"){for(var n of Re(e))if(!De.call(t,n))q(t,n,{get:je.bind(e,n),enumerable:!(r=ze(e,n))||r.enumerable})}return fe.set(e,t),t},fe;var _e=(e)=>e;function Ne(e,t){this[e]=_e.bind(null,t)}var Ve=(e,t)=>{for(var r in t)q(e,r,{get:t[r],enumerable:!0,configurable:!0,set:Ne.bind(t,r)})};var ft={};Ve(ft,{compressImage:()=>We,compressWithMainThread:()=>pe,compressWithWorker:()=>Fe,default:()=>ut,detectInputMime:()=>N,extractMetadataSegments:()=>Ie,getImageMetadata:()=>Ae,insertWebPExif:()=>O,normalizeExifOrientation:()=>X,spliceMetadataIntoBlob:()=>H,spliceWebpMetadata:()=>Le});module.exports=Oe(ft);var y=[69,120,105,102,0,0],U=[73,67,67,95,80,82,79,70,73,76,69,0];function C(e,t,r){if(e.length<t+r.length)return!1;for(let n=0;n<r.length;n++)if(e[t+n]!==r[n])return!1;return!0}function k(e){return new DataView(e.buffer,e.byteOffset,e.byteLength)}function D(e,t,r=!0){if(t+8>e.byteLength)return null;let n=e.getUint16(t),a=n===18761;if(!a&&n!==19789)return null;if(e.getUint16(t+2,a)!==42)return null;let o=e.getUint32(t+4,a),i=t+o;if(r&&i+2>e.byteLength)return null;return{isLittleEndian:a,ifdStart:i}}function ue(e){return e==="image/webp"||e==="image/png"?"image/webp":"image/jpeg"}function N(e,t){let r=e.type;if(r==="image/jpeg"||r==="image/webp"||r==="image/png")return r;let n=t??(e instanceof File?e.name:"");if(/\.(jpe?g)$/i.test(n))return"image/jpeg";if(/\.webp$/i.test(n))return"image/webp";if(/\.png$/i.test(n))return"image/png";return}function Z(e){if(e.byteLength>=2&&e.getUint16(0)===65496)return"jpeg";if(e.byteLength>=12&&e.getUint32(0,!1)===1380533830&&e.getUint32(8,!1)===1464156752)return"webp";return null}async function de(e){if(e.size<4)return null;let t=await e.slice(0,12).arrayBuffer();return Z(new DataView(t))}function Q(e,t,r,n){if(r!==void 0&&Number.isFinite(r)&&r>0&&e>r)t=Math.max(1,Math.round(t*r/e)),e=Math.max(1,Math.round(r));if(n!==void 0&&Number.isFinite(n)&&n>0&&t>n)e=Math.max(1,Math.round(e*n/t)),t=Math.max(1,Math.round(n));return{width:e,height:t}}function P(e){e?.throwIfAborted()}function j(e){return e?.reason??new DOMException("Compression aborted","AbortError")}function le(e,t){let r=t==="display-p3"?{colorSpace:"display-p3"}:void 0,n=e.getContext("2d",r);if(!n)return null;let a=n,o=typeof a.getContextAttributes==="function"?a.getContextAttributes().colorSpace:void 0;return{ctx:n,actualColorSpace:o}}function Xe(e,t,r,n,a,o,i){return new Promise((d,u)=>{if(typeof window>"u"||typeof document>"u"){d({blob:e,width:0,height:0});return}let c=new Image,f=URL.createObjectURL(e),s=!1,l=()=>{if(s)return;s=!0,URL.revokeObjectURL(f),u(j(i))},g=(p)=>{if(s)return;s=!0,i?.removeEventListener("abort",l),p()};if(i){if(i.aborted){l();return}i.addEventListener("abort",l,{once:!0})}c.onload=()=>{if(s)return;URL.revokeObjectURL(f);let{width:p,height:h}=Q(c.naturalWidth,c.naturalHeight,t,r),m=document.createElement("canvas");m.width=p,m.height=h;let B=le(m,o);if(!B){g(()=>u(Error("Canvas 2D context unavailable")));return}B.ctx.drawImage(c,0,0,p,h),m.toBlob((M)=>{if(i?.aborted){l();return}if(M)g(()=>d({blob:M,width:p,height:h,colorSpace:B.actualColorSpace}));else g(()=>u(Error("Canvas compression failed to produce blob")))},a,n)},c.onerror=()=>{URL.revokeObjectURL(f),g(()=>u(Error("Failed to load image for compression")))},c.src=f})}function z(e,t){if(!(typeof OffscreenCanvas<"u"&&typeof createImageBitmap<"u"))return{encode:(o,i,d,u,c)=>Xe(e,o,i,d,u,t,c),dispose:()=>{}};let n=null,a=!1;return{async encode(o,i,d,u,c){if(P(c),!n)n=createImageBitmap(e);let f=await n;if(a)throw f.close(),j(c);P(c);let{width:s,height:l}=Q(f.width,f.height,o,i),g=new OffscreenCanvas(s,l),p=le(g,t);if(!p)throw Error("OffscreenCanvas 2D context unavailable");p.ctx.drawImage(f,0,0,s,l);let h=await g.convertToBlob({type:u,quality:d});return P(c),{blob:h,width:s,height:l,colorSpace:p.actualColorSpace}},dispose(){if(a=!0,n)n.then((o)=>o.close()).catch(()=>{}),n=null}}}async function pe(e,t,r,n,a,o){let i=z(e);try{let{blob:d,width:u,height:c}=await i.encode(t,r,n,a,o);return{blob:d,width:u,height:c}}finally{i.dispose()}}var me=262144,Y=65533-U.length-2,he=new TextDecoder("ascii"),He=new TextDecoder("utf-16be");function be(e,t,r){return new Uint8Array(e.buffer.slice(e.byteOffset+t,e.byteOffset+r))}async function*Ge(e,t){let r=0,n=t,a=2;while(!0){if(a+4>n.byteLength){let c=r+a;if(c+4>e.size)return;let f=await e.slice(c,c+me).arrayBuffer();if(f.byteLength<4)return;r=c,n=new DataView(f),a=0;continue}let o=n.getUint16(a);if(o===65498||o===65497||(o&65280)!==65280)return;let i=n.getUint16(a+2);if(i<2)return;let d=a+2+i,u=r+d;if(u>e.size)return;if(o===65505||o===65506){let c=d<=n.byteLength?be(n,a,d):new Uint8Array(await e.slice(r+a,u).arrayBuffer());yield{kind:o===65505?"jpeg-app1":"jpeg-app2",bytes:c}}a=d}}async function*Je(e,t){let r=e.size,n=12;while(n+8<=r){let a,o;if(n+8<=t.byteLength)a=t.getUint32(n,!1),o=t.getUint32(n+4,!0);else{let f=await e.slice(n,n+8).arrayBuffer();if(f.byteLength<8)return;let s=new DataView(f);a=s.getUint32(0,!1),o=s.getUint32(4,!0)}let i=o+(o&1),d=a===1163413830,u=a===1229144912;if(d&&o>=6||u){let f=n+8,s=f+o;if(s>r)return;if(o>0){let l=s<=t.byteLength?be(t,f,s):new Uint8Array(await e.slice(f,s).arrayBuffer());yield{kind:d?"webp-exif":"webp-iccp",bytes:l}}}let c=n+8+i;if(c<=n)return;n=c}}async function*te(e){let t=Math.min(e.size,me),r=await e.slice(0,t).arrayBuffer(),n=new DataView(r),a=Z(n);if(a==="jpeg")yield*Ge(e,n);else if(a==="webp")yield*Je(e,n)}function xe(e){let t=C(e,0,y)?e.subarray(y.length):e,r=2+y.length+t.length;if(r>65535)return null;let n=new Uint8Array(2+r);return n[0]=255,n[1]=225,n[2]=r>>8&255,n[3]=r&255,n.set(y,4),n.set(t,4+y.length),n}function ye(e){let t=Math.max(1,Math.ceil(e.length/Y));if(t>255)return[];let r=[];for(let n=0;n<t;n++){let a=e.subarray(n*Y,(n+1)*Y),o=2+U.length+2+a.length,i=new Uint8Array(2+o);i[0]=255,i[1]=226,i[2]=o>>8&255,i[3]=o&255,i.set(U,4),i[4+U.length]=n+1,i[4+U.length+1]=t,i.set(a,4+U.length+2),r.push(i)}return r}function Ke(e){if(e.length===0)return;let t=e.length===1?e:e.toSorted((o,i)=>o[16]-i[16]),r=t.reduce((o,i)=>o+Math.max(0,i.length-18),0);if(r===0)return;let n=new Uint8Array(r),a=0;for(let o of t){if(o.length<=18)continue;n.set(o.subarray(18),a),a+=o.length-18}return n}function ee(e,t){let r=D(e,t);if(!r)return!1;let{isLittleEndian:n,ifdStart:a}=r,o=e.getUint16(a,n);for(let i=0;i<o;i++){let d=a+2+i*12;if(d+12>e.byteLength)break;let u=e.getUint16(d,n),c=e.getUint16(d+2,n),f=e.getUint32(d+4,n);if(u===274&&c===3&&f===1)return e.setUint16(d+8,1,n),!0}return!1}async function Ce(e){let t={extraJpegSegments:[]},r=[];for await(let n of te(e))switch(n.kind){case"jpeg-app1":{if(C(n.bytes,4,y)&&!t.exifTiff){let a=n.bytes.slice(10);ee(k(a),0),t.exifTiff=a}else t.extraJpegSegments.push(n.bytes);break}case"jpeg-app2":{if(n.bytes.length>=18&&C(n.bytes,4,U))r.push(n.bytes);else t.extraJpegSegments.push(n.bytes);break}case"webp-exif":{if(!t.exifTiff){let a=C(n.bytes,0,y)?n.bytes.slice(y.length):n.bytes;ee(k(a),0),t.exifTiff=a}break}case"webp-iccp":{if(!t.iccProfile)t.iccProfile=n.bytes;break}}if(!t.iccProfile&&r.length>0)t.iccProfile=Ke(r);return t}function we(e,t=!0){let r=[];if(e.exifTiff){let n=xe(e.exifTiff);if(n)r.push(n)}if(t&&e.iccProfile)r.push(...ye(e.iccProfile));return r.push(...e.extraJpegSegments),r}function Ue(e){if(e.length<132)return"unknown";let t=k(e);if(t.getUint32(36,!1)!==1633907568)return"unknown";if(t.getUint32(16,!1)!==1380401696)return"other";let r=Math.min(t.getUint32(128,!1),200);for(let n=0;n<r;n++){let a=132+n*12;if(a+12>e.length)break;if(t.getUint32(a,!1)!==1684370275)continue;let o=t.getUint32(a+4,!1),i=t.getUint32(a+8,!1);if(i<12||o+i>e.length)break;let d=t.getUint32(o,!1),u="";if(d===1684370275){let f=t.getUint32(o+8,!1),s=Math.min(o+12+f,o+i);if(s>o+12){let l=e.subarray(o+12,s),g=l.indexOf(0);u=he.decode(g===-1?l:l.subarray(0,g))}}else if(d===1835824483&&o+28<=e.length){let f=t.getUint32(o+8,!1),s=t.getUint32(o+12,!1);if(f>=1&&s>=12){let l=t.getUint32(o+20,!1),g=t.getUint32(o+24,!1),p=o+g,h=Math.min(p+l,o+i);if(p>=o&&h>p)u=He.decode(e.subarray(p,h))}}let c=u.trim().toLowerCase();if(c.includes("display p3")||c.includes("display-p3")||/\bp3\b/.test(c))return"display-p3";if(c.includes("srgb"))return"srgb";if(c.length>0)return"other";break}return"unknown"}async function Ie(e){let t=[];for await(let r of te(e))switch(r.kind){case"jpeg-app1":{X(r.bytes),t.push(r.bytes);break}case"jpeg-app2":{t.push(r.bytes);break}case"webp-exif":{let n=xe(r.bytes);if(n)X(n),t.push(n);break}case"webp-iccp":{t.push(...ye(r.bytes));break}}return t}function X(e){if(e.length<14||e[0]!==255||e[1]!==225||!C(e,4,y))return!1;return ee(k(e),10)}async function H(e,t){if(t.length===0)return e;let r=Math.min(e.size,65536),n=await e.slice(0,r).arrayBuffer(),a=new Uint8Array(n),o=new DataView(n);if(o.byteLength<4||o.getUint16(0)!==65496)return e;let i=2;if(o.byteLength>=6&&o.getUint16(2)===65504){let s=o.getUint16(4);if(s>=2&&4+s<=e.size)i=4+s}let d=t.some((s)=>s.length>=18&&s[0]===255&&s[1]===226&&C(s,4,U)),u=[e.slice(0,i)],c=t.map((s)=>s.buffer.slice(s.byteOffset,s.byteOffset+s.byteLength));u.push(...c);let f=i;if(d&&f+4<=o.byteLength){let s=f,l=[],g=!1;while(s+4<=o.byteLength){let p=o.getUint16(s);if(p<65504||p>65519)break;let h=o.getUint16(s+2);if(h<2||s+2+h>o.byteLength)break;let m=s+2+h;if(p===65506&&h>=2+U.length&&C(a,s+4,U))g=!0;else l.push(n.slice(s,m));s=m}if(g)u.push(...l),f=s}return u.push(e.slice(f)),new Blob(u,{type:e.type||"image/jpeg"})}async function Ae(e){let t={hasExif:!1,hasIccProfile:!1,colorSpace:"sRGB (Standard)"};for await(let r of te(e))if(r.kind==="jpeg-app1"){if(C(r.bytes,4,y))t.hasExif=!0,ge(k(r.bytes),10,t)}else if(r.kind==="webp-exif")t.hasExif=!0,ge(k(r.bytes),C(r.bytes,0,y)?y.length:0,t);else if(r.kind==="webp-iccp"||r.bytes.length>=18&&C(r.bytes,4,U))t.hasIccProfile=!0,t.colorSpace="ICC profile embedded";return t}function ge(e,t,r){let n=D(e,t);if(!n)return;let{isLittleEndian:a,ifdStart:o}=n,i,d=e.getUint16(o,a);for(let u=0;u<d;u++){let c=o+2+u*12;if(c+12>e.byteLength)break;let f=e.getUint16(c,a),s=e.getUint16(c+2,a),l=e.getUint32(c+4,a);if(f===274&&s===3&&l>=1)r.orientation=e.getUint16(c+8,a);else if(f===271&&s===2)r.make=V(e,t,c,l,a);else if(f===272&&s===2)r.model=V(e,t,c,l,a);else if(f===306&&s===2)r.dateTime=V(e,t,c,l,a);else if(f===34665&&(s===3||s===4))i=s===3?e.getUint16(c+8,a):e.getUint32(c+8,a)}if(i){let u=t+i;if(u+2<=e.byteLength){let c=e.getUint16(u,a);for(let f=0;f<c;f++){let s=u+2+f*12;if(s+12>e.byteLength)break;let l=e.getUint16(s,a),g=e.getUint16(s+2,a),p=e.getUint32(s+4,a);if((l===36867||l===36868)&&g===2&&!r.dateTime)r.dateTime=V(e,t,s,p,a)}}}}function V(e,t,r,n,a){if(n===0)return"";let o,i;if(n<=4)o=r+8,i=Math.min(n,e.byteLength-o);else{let c=e.getUint32(r+8,a);if(o=t+c,o<t||o>=e.byteLength)return"";i=Math.min(n,e.byteLength-o)}if(i<=0)return"";let d=new Uint8Array(e.buffer,e.byteOffset+o,i),u=d.indexOf(0);return he.decode(u===-1?d:d.subarray(0,u)).trim()}var G=12,$e=10,qe=4294967295,Ze=8,ke=16,Qe=32,Ye=4,et=2,Ee=new Set(["VP8 ","VP8L","ALPH","ANIM","ANMF"]);function ne(e,t){return String.fromCharCode(e[t],e[t+1],e[t+2],e[t+3])}function Me(e,t){return e[t]|e[t+1]<<8|e[t+2]<<16}function Se(e,t,r){e[t]=r&255,e[t+1]=r>>>8&255,e[t+2]=r>>>16&255}function tt(e){if(e.length<G)throw Error("Invalid WebP: file is too small");if(ne(e,0)!=="RIFF"||ne(e,8)!=="WEBP")throw Error("Invalid WebP: missing RIFF/WEBP header");let t=k(e),r=t.getUint32(4,!0);if(Math.abs(r+8-e.length)>1&&r+8>e.length)throw Error("Invalid WebP: RIFF size mismatch");let n=[],a=G;while(a<e.length){if(a+8>e.length)break;let o=ne(e,a),i=t.getUint32(a+4,!0),d=a+8,u=d+i,c=u+(i&1);if(u>e.length)break;n.push({fourCC:o,data:e.subarray(d,u)}),a=Math.min(c,e.length)}return n}function Pe(e){let t=e.find((a)=>a.fourCC==="VP8X");if(t&&t.data.length>=10){let a=1+Me(t.data,4),o=1+Me(t.data,7),i=(t.data[0]&ke)!==0;return{width:a,height:o,alpha:i}}let r=e.find((a)=>a.fourCC==="VP8 "),n=e.find((a)=>a.fourCC==="VP8L");if(r){let a=r.data;if(a.length>=10&&a[3]===157&&a[4]===1&&a[5]===42){let o=(a[6]|a[7]<<8)&16383,i=(a[8]|a[9]<<8)&16383;if(o&&i)return{width:o,height:i,alpha:!1}}}if(n){let a=n.data;if(a.length>=5&&a[0]===47){let o=a[1]|a[2]<<8|a[3]<<16|a[4]<<24,i=(o&16383)+1,d=(o>>>14&16383)+1,u=(o>>>28&1)!==0;return{width:i,height:d,alpha:u}}}return null}function nt(e,t,r,n,a,o=!1){let i=Pe(e),d=i?.width??n??1,u=i?.height??a??1,c=i?.alpha??o,f=0;if(t)f|=Ze;if(r||e.some((l)=>l.fourCC==="ICCP"))f|=Qe;if(c||e.some((l)=>l.fourCC==="ALPH"))f|=ke;if(e.some((l)=>l.fourCC==="XMP "))f|=Ye;if(e.some((l)=>l.fourCC==="ANIM"||l.fourCC==="ANMF"))f|=et;let s=new Uint8Array($e);return s[0]=f,Se(s,4,Math.max(0,d-1)),Se(s,7,Math.max(0,u-1)),{fourCC:"VP8X",data:s}}function rt(e){if(e.length===0)throw Error("EXIF data cannot be empty");let t=e;if(t.length>=4&&t[0]===255&&t[1]===225)t=t.subarray(4);let r=C(t,0,y)?t.subarray(6):t;if(!D(k(r),0,!1))throw Error("Invalid EXIF TIFF header");return r}function at(e){let t=e;if(t.length>=4&&t[0]===255&&t[1]===226)t=t.subarray(4);if(t.length>=14&&C(t,0,U))return t.subarray(14);return t}function ot(e){let t=G;for(let o of e)t+=8+o.data.length+(o.data.length&1);if(t-8>qe)throw Error("WebP file exceeds RIFF size limit");let r=new Uint8Array(t),n=k(r);r.set([82,73,70,70],0),n.setUint32(4,t-8,!0),r.set([87,69,66,80],8);let a=G;for(let o of e){for(let i=0;i<4;i++)r[a+i]=o.fourCC.charCodeAt(i);if(n.setUint32(a+4,o.data.length,!0),r.set(o.data,a+8),a+=8+o.data.length,o.data.length&1)r[a]=0,a++}return r}function O(e,t,r,n,a){if(!(e instanceof Uint8Array))throw TypeError("webp must be a Uint8Array");let o=t?rt(t):void 0,i=r?at(r):void 0,d=tt(e),u=Pe(d),c=d.find((m)=>m.fourCC==="EXIF"),f=d.find((m)=>m.fourCC==="ICCP"),s=o?{fourCC:"EXIF",data:o}:c,l=i?{fourCC:"ICCP",data:i}:f,g=d.filter((m)=>m.fourCC!=="VP8X"&&m.fourCC!=="EXIF"&&m.fourCC!=="ICCP"),p=[];if(l)p.push(l);if(p.push(...g.filter((m)=>Ee.has(m.fourCC))),s)p.push(s);p.push(...g.filter((m)=>!Ee.has(m.fourCC)));let h=nt(p,Boolean(s),Boolean(l),u?.width??n,u?.height??a,u?.alpha??!1);return ot([h,...p])}async function Le(e,t,r,n){let a,o=[];for(let c of t)if(!a&&c.length>=14&&c[0]===255&&c[1]===225&&C(c,4,y))a=c;else if(c.length>=18&&c[0]===255&&c[1]===226&&C(c,4,U))o.push(c);let i;if(o.length===1)i=o[0];else if(o.length>1){let c=o.toSorted((g,p)=>g[16]-p[16]),f=c.reduce((g,p)=>g+(p.length-18),0),s=new Uint8Array(f),l=0;for(let g of c)s.set(g.subarray(18),l),l+=g.length-18;i=s}if(!a&&!i)return e;let d=new Uint8Array(await e.arrayBuffer()),u=O(d,a,i,r,n);return new Blob([u.buffer],{type:"image/webp"})}var it=`
|
|
2
|
+
let cachedBitmap = null;
|
|
3
|
+
function computeTargetSize(width, height, maxWidth, maxHeight) {
|
|
4
|
+
if (typeof maxWidth === "number" && Number.isFinite(maxWidth) && maxWidth > 0 && width > maxWidth) {
|
|
5
|
+
height = Math.max(1, Math.round((height * maxWidth) / width));
|
|
6
|
+
width = Math.max(1, Math.round(maxWidth));
|
|
7
|
+
}
|
|
8
|
+
if (typeof maxHeight === "number" && Number.isFinite(maxHeight) && maxHeight > 0 && height > maxHeight) {
|
|
9
|
+
width = Math.max(1, Math.round((width * maxHeight) / height));
|
|
10
|
+
height = Math.max(1, Math.round(maxHeight));
|
|
11
|
+
}
|
|
12
|
+
return { width, height };
|
|
13
|
+
}
|
|
3
14
|
self.onmessage = async (e) => {
|
|
4
|
-
const
|
|
15
|
+
const data = e.data;
|
|
16
|
+
if (data.type === "dispose") {
|
|
17
|
+
cachedBitmap?.close();
|
|
18
|
+
cachedBitmap = null;
|
|
19
|
+
self.close();
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const { id, blob, maxWidth, maxHeight, quality, mimeType, colorSpace } = data;
|
|
5
23
|
try {
|
|
6
|
-
|
|
24
|
+
if (blob) {
|
|
25
|
+
cachedBitmap?.close();
|
|
26
|
+
cachedBitmap = await createImageBitmap(blob);
|
|
27
|
+
}
|
|
28
|
+
if (!cachedBitmap) throw new Error("Missing image bitmap in worker");
|
|
7
29
|
const { width, height } = computeTargetSize(
|
|
8
|
-
|
|
9
|
-
|
|
30
|
+
cachedBitmap.width,
|
|
31
|
+
cachedBitmap.height,
|
|
10
32
|
maxWidth,
|
|
11
33
|
maxHeight,
|
|
12
34
|
);
|
|
13
35
|
const canvas = new OffscreenCanvas(width, height);
|
|
14
|
-
const ctx = canvas.getContext(
|
|
36
|
+
const ctx = canvas.getContext(
|
|
37
|
+
"2d",
|
|
38
|
+
colorSpace === "display-p3" ? { colorSpace: "display-p3" } : undefined,
|
|
39
|
+
);
|
|
15
40
|
if (!ctx) throw new Error("OffscreenCanvas 2D context unavailable");
|
|
16
|
-
|
|
17
|
-
|
|
41
|
+
const actualColorSpace =
|
|
42
|
+
typeof ctx.getContextAttributes === "function"
|
|
43
|
+
? ctx.getContextAttributes()?.colorSpace
|
|
44
|
+
: undefined;
|
|
45
|
+
ctx.drawImage(cachedBitmap, 0, 0, width, height);
|
|
18
46
|
const outputBlob = await canvas.convertToBlob({ type: mimeType, quality });
|
|
19
|
-
self.postMessage({
|
|
47
|
+
self.postMessage({
|
|
48
|
+
id,
|
|
49
|
+
success: true,
|
|
50
|
+
blob: outputBlob,
|
|
51
|
+
width,
|
|
52
|
+
height,
|
|
53
|
+
colorSpace: actualColorSpace,
|
|
54
|
+
});
|
|
20
55
|
} catch (err) {
|
|
21
|
-
self.postMessage({
|
|
56
|
+
self.postMessage({
|
|
57
|
+
id,
|
|
58
|
+
success: false,
|
|
59
|
+
error: err?.message || String(err),
|
|
60
|
+
});
|
|
22
61
|
}
|
|
23
62
|
};
|
|
24
|
-
|
|
63
|
+
`,re=new WeakSet;function ae(e,t){if(typeof Worker>"u"||typeof OffscreenCanvas>"u"||re.has(Worker))return z(e,t);let r=null,n=null,a=null,o=!1,i=0;function d(){if(n)n.terminate(),n=null;if(a)URL.revokeObjectURL(a),a=null}function u(){if(d(),!r)r=z(e,t);return r}function c(){if(n)return n;try{let f=new Blob([it],{type:"application/javascript"});return a=URL.createObjectURL(f),n=new Worker(a),n}catch{return re.add(Worker),null}}return{async encode(f,s,l,g,p){if(P(p),r)return r.encode(f,s,l,g,p);let h=c();if(!h)return u().encode(f,s,l,g,p);let m=++i,B=!o;return o=!0,new Promise((M,A)=>{let S=!1,W=()=>{h.removeEventListener("message",v),h.removeEventListener("error",L),p?.removeEventListener("abort",T)},T=()=>{if(S)return;S=!0,W(),d(),A(j(p))},v=(w)=>{if(S||w.data?.id!==m)return;if(S=!0,W(),w.data.success)M({blob:w.data.blob,width:w.data.width,height:w.data.height,colorSpace:w.data.colorSpace});else A(Error(w.data.error||"Worker compression failed"))},L=(w)=>{if(S)return;if(S=!0,W(),!w.message){re.add(Worker),u().encode(f,s,l,g,p).then(M,A);return}d(),A(Error(w.message||"Worker execution failed"))};if(p){if(p.aborted){T();return}p.addEventListener("abort",T,{once:!0})}h.addEventListener("message",v),h.addEventListener("error",L),h.postMessage({type:"encode",id:m,blob:B?e:void 0,maxWidth:f,maxHeight:s,quality:l,mimeType:g,colorSpace:t})})},dispose(){if(r)r.dispose(),r=null;if(n)try{n.postMessage({type:"dispose"})}catch{}d()}}}async function Fe(e,t,r,n,a,o){let i=ae(e);try{let{blob:d,width:u,height:c}=await i.encode(t,r,n,a,o);return{blob:d,width:u,height:c}}finally{i.dispose()}}var Be={extract:{progress:10,stage:"Extracting metadata"},compress:{progress:30,stage:"Compressing pixels"},optimize:{progress:65,stage:"Optimizing file size"},preserve:{progress:85,stage:"Preserving metadata"},complete:{progress:100,stage:"Complete"}};function st(e,t,r){if(t==="image/webp")return r?.(100,"Completed (original image preserved to prevent WebP 100% inflation)"),e;if(t==="image/jpeg")return r?.(100,"Completed (original image preserved to prevent JPEG re-compression)"),e;return null}function ct(e,t){if(e==="other")return!1;if(e==="display-p3"&&t==="srgb")return!1;return!0}async function We(e,t={}){if(!(typeof OffscreenCanvas<"u"&&typeof createImageBitmap<"u")&&!(typeof document<"u"&&typeof Image<"u"))return e;let{maxSizeMB:a=Number.POSITIVE_INFINITY,maxWidth:o,maxHeight:i,quality:d=0.85,mimeType:u,preserveMetadata:c=!0,useWebWorker:f=!0,strict:s,signal:l,onProgress:g}=t,p=(x)=>g?.(Be[x].progress,Be[x].stage);P(l);let h=N(e),m,M=await(async()=>{if(h)return h;return m??=await de(e),m?`image/${m}`:void 0})(),A=u??ue(M),S=Number.isFinite(d)?Math.min(1,Math.max(0,d)):0.85,W=o!==void 0&&Number.isFinite(o)&&o>0?o:void 0,T=i!==void 0&&Number.isFinite(i)&&i>0?i:void 0,v=Boolean(W||T),L=a*1024*1024,w=t.maxSizeMB!==void 0&&Number.isFinite(L)&&L>0;if(S>=1&&!w&&!v&&M===A){let x=st(e,A,g);if(x)return x}if(w&&e.size<=L&&!v&&M===A&&S>=1)return p("complete"),e;p("extract");let E;if(c&&m!==null)try{E=await Ce(e)}catch{}P(l);let oe=E?.iccProfile?Ue(E.iccProfile):void 0,ie=oe==="display-p3"?"display-p3":void 0;p("compress");let J=f?ae(e,ie):z(e,ie),b;try{if(b=await J.encode(W,T,S,A,l),w&&b.blob.size>L){let x=b,I=S;for(let R=0;R<3&&b.blob.size>L;R++){let _=Math.min(0.85,Math.sqrt(L/b.blob.size)*0.95),se=Math.max(1,Math.round(b.width*_)),ce=Math.max(1,Math.round(b.height*_));if(b.blob.type==="image/png"&&se===b.width&&ce===b.height)break;if(p("optimize"),I=Math.min(I,Math.max(0.4,I*0.8)),b=await J.encode(se,ce,I,A,l),b.blob.size<x.blob.size)x=b}b=x}}finally{J.dispose()}P(l),p("preserve");let F=b.blob,K=F.type||A,Te=Boolean(E&&(E.exifTiff||E.iccProfile||E.extraJpegSegments.length>0));if(E&&Te){let x=ct(oe,b.colorSpace);try{if(K==="image/jpeg"){let I=we(E,x);if(I.length>0)F=await H(F,I)}else if(K==="image/webp"){let I=x?E.iccProfile:void 0;if(E.exifTiff||I){let R=new Uint8Array(await F.arrayBuffer()),_=O(R,E.exifTiff,I,b.width,b.height);F=new Blob([_.buffer],{type:"image/webp"})}}}catch(I){console.warn("[imagepressor] Warning: Could not preserve metadata on the compressed image; returning pixels without metadata.",I)}}let ve=s!==void 0?s:M===K||M===A;if(F.size>=e.size&&ve&&!v&&!w)return p("complete"),e;if(p("complete"),e instanceof File){let x=F.type||A,I=x==="image/webp"?"webp":x==="image/png"?"png":"jpg",R=e.name.replace(/\.[^/.]+$/,"")||"photo";return new File([F],`${R}.${I}`,{type:x,lastModified:e.lastModified})}return F}var ut=We;
|
|
25
64
|
|
|
26
|
-
//# debugId=
|
|
65
|
+
//# debugId=C7712DBA156ACFF964756E2164756E21
|
|
27
66
|
//# sourceMappingURL=index.cjs.map
|