/*! ImReso: Unlimited Target-Size Image Compressor — plugin code (c) ImageResizer.cc, GPLv2 or later. Bundled third-party code keeps its own licence: see licenses/ and readme.txt. */
import"./chunks/chunk-HWGPDIIB.js";function I(t){let e=t;if(e.length<12)return"unknown";if(e[0]===255&&e[1]===216&&e[2]===255)return"jpeg";if(e[0]===137&&e[1]===80&&e[2]===78&&e[3]===71)return"png";if(e[0]===71&&e[1]===73&&e[2]===70)return"gif";if(e[0]===66&&e[1]===77)return"bmp";if(e[0]===0&&e[1]===0&&e[2]===1&&e[3]===0)return"ico";if(e[0]===73&&e[1]===73&&e[2]===42||e[0]===77&&e[1]===77&&e[2]===0)return"tiff";if(e[4]===106&&e[5]===80||e[0]===255&&e[1]===79&&e[2]===255&&e[3]===81)return"jp2";let i=(a,s)=>{for(let r=0;r<s.length;r++)if(e[a+r]!==s.charCodeAt(r))return!1;return!0};if(i(0,"RIFF")&&i(8,"WEBP"))return"webp";if(i(4,"ftyp")){let a=String.fromCharCode(e[8],e[9],e[10],e[11]);if(a==="avif"||a==="avis")return"avif";if(a.startsWith("hei")||a.startsWith("mif")||a==="heic"||a==="heix")return"heic"}let n=String.fromCharCode(...e.slice(0,Math.min(64,e.length))).toLowerCase();return n.includes("<svg")||n.includes("<?xml")&&n.includes("svg")?"svg":"unknown"}var g={avif:"image/avif",webp:"image/webp",jpeg:"image/jpeg",png:"image/png",gif:"image/gif"};var v=8,E=new URL(".",import.meta.url).href,b=t=>E+t,h=null,p=null,d=null;self.onmessage=async t=>{let{id:e,file:i,opts:n}=t.data;try{let a=await P(i,n),s={id:e,blob:a.blob,meta:a.meta};self.postMessage(s)}catch(a){let s={id:e,error:a instanceof Error?a.message:String(a)};self.postMessage(s)}};async function P(t,e){var c;let i=await z(t),{width:n,height:a}=B(i.width,i.height,e.maxDim),r=new OffscreenCanvas(n,a).getContext("2d");if(!r)throw new Error("OffscreenCanvas 2D context unavailable");r.imageSmoothingEnabled=!0,r.imageSmoothingQuality="high",r.drawImage(i,0,0,n,a);let o=r.getImageData(0,0,n,a),m=D(o),f=C(e,o,m);f==="jpeg"&&m&&(r.clearRect(0,0,n,a),r.fillStyle="#ffffff",r.fillRect(0,0,n,a),r.drawImage(i,0,0,n,a),o=r.getImageData(0,0,n,a)),(c=i.close)==null||c.call(i);let u;if(e.targetBytes&&e.targetBytes>0){let l=new Map,x=await q(async M=>{let y=await w(o,f,M);return l.set(M,y),y.size},e.targetBytes);u=l.get(x.quality)??await w(o,f,x.quality)}else u=await w(o,f,F(e.quality,1,100));return{blob:u,meta:{w:n,h:a,format:f}}}var S=5,k=100,A=7,j=.9;async function q(t,e){let i=S,n=k,a=0,s=null,r=null,o=null,m=null;for(;i<=n&&a<A;){let u;if(o&&m&&o.size>0&&m.size>o.size){let l=Math.log(e/o.size)/Math.log(m.size/o.size);u=Math.min(n,Math.max(i,Math.round(o.quality+l*(m.quality-o.quality))))}else u=Math.round((i+n)/2);let c=await t(u);if(a++,(r===null||Math.abs(c-e)<Math.abs(r.size-e))&&(r={quality:u,size:c}),c<=e){if(s={quality:u,size:c},c>=e*j)break;o={quality:u,size:c},i=u+1}else m={quality:u,size:c},n=u-1}let f=s??r;return{quality:f.quality,size:f.size,passes:a}}async function z(t){let e=await O(t);if(e==="heic"){let{heicTo:i}=await import("./chunks/heic-to-YID4BT7Y.js");return await i({blob:t,type:"bitmap"})}if(e==="tiff"){let i=(await import("./chunks/UTIF-GJCRY5V3.js")).default,n=await t.arrayBuffer(),a=i.decode(n);i.decodeImage(n,a[0]);let s=i.toRGBA8(a[0]),{width:r,height:o}=a[0];return createImageBitmap(new ImageData(new Uint8ClampedArray(s),r,o))}return createImageBitmap(t)}async function O(t){if(/^image\/(heic|heif)/i.test(t.type)||/\.(heic|heif)$/i.test(t.name))return"heic";if(/^image\/tiff/i.test(t.type)||/\.tiff?$/i.test(t.name))return"tiff";try{let e=new Uint8Array(await t.slice(0,64).arrayBuffer());return I(e)}catch{return"unknown"}}function B(t,e,i){if(!i||i<=0)return{width:t,height:e};let n=Math.max(t,e);if(n<=i)return{width:t,height:e};let a=i/n;return{width:Math.max(1,Math.round(t*a)),height:Math.max(1,Math.round(e*a))}}function C(t,e,i){let n;return t.format==="jpeg"||t.format==="webp"||t.format==="avif"?n=t.format:n=i?"webp":T(e)?"avif":"jpeg",n==="avif"&&t.noAvif&&(n="webp"),n}function D(t){let e=t.data,i=Math.max(4,Math.floor(e.length/4/4096)*4);for(let n=3;n<e.length;n+=i)if(e[n]<255)return!0;return!1}function T(t){let n=new OffscreenCanvas(t.width,t.height),a=n.getContext("2d");if(!a)return!0;a.putImageData(t,0,0);let r=new OffscreenCanvas(64,64).getContext("2d");if(!r)return!0;r.drawImage(n,0,0,64,64);let{data:o}=r.getImageData(0,0,64,64),m=new Set;for(let f=0;f<o.length;f+=4){let u=o[f]>>4<<8|o[f+1]>>4<<4|o[f+2]>>4;m.add(u)}return m.size>600}async function w(t,e,i){if(e==="jpeg"){let s=await import("./chunks/encode-5T5FRGKV.js");d||(d=s.init({locateFile:b})),await d;let r=await s.default(t,{quality:i});return new Blob([r],{type:g.jpeg})}if(e==="webp"){let s=await import("./chunks/encode-IBBV4YIP.js");p||(p=s.init({locateFile:b})),await p;let r=await s.default(t,{quality:i});return new Blob([r],{type:g.webp})}let n=await import("./chunks/encode-A2O2AJJ2.js");h||(h=n.init({locateFile:b})),await h;let a=await n.default(t,{quality:i,speed:v});return new Blob([a],{type:g.avif})}function F(t,e,i){return Math.max(e,Math.min(i,t|0))}
