function clamp(e,a,t){return et?t:e}function clone(e){let a=!1;if(void 0!==e)try{a=JSON.parse(JSON.stringify(e))}catch(e){}return a}var defaultParams={amount:35,iterations:20,quality:30,seed:25};function sanitizeInput(e){return"object"!=typeof(e=clone(e))&&(e={}),Object.keys(defaultParams).filter(e=>"iterations"!==e).forEach(a=>{"number"!=typeof e[a]||isNaN(e[a])?e[a]=defaultParams[a]:e[a]=clamp(e[a],0,100),e[a]=Math.round(e[a])}),("number"!=typeof e.iterations||isNaN(e.iterations)||e.iterations<=0)&&(e.iterations=defaultParams.iterations),e.iterations=Math.round(e.iterations),e}class Canvas{constructor(e=300,a=150){"undefined"==typeof window?(this.canvasEl={width:e,height:a},this.ctx=null):(this.canvasEl=document.createElement("canvas"),this.canvasEl.width=e,this.canvasEl.height=a,this.ctx=this.canvasEl.getContext("2d"))}getContext(){return this.ctx}toDataURL(e,a,t){if("function"!=typeof t)return this.canvasEl.toDataURL(e,a);t(this.canvasEl.toDataURL(e,a))}get width(){return this.canvasEl.width}set width(e){this.canvasEl.width=e}get height(){return this.canvasEl.height}set height(e){this.canvasEl.height=e}}function imageToImageData(e){if(e instanceof HTMLImageElement){if(!e.naturalWidth||!e.naturalHeight||!1===e.complete)throw new Error("This this image hasn't finished loading: "+e.src);const a=new Canvas(e.naturalWidth,e.naturalHeight),t=a.getContext("2d");t.drawImage(e,0,0,a.width,a.height);const n=t.getImageData(0,0,a.width,a.height);return n.data&&n.data.length&&(void 0===n.width&&(n.width=e.naturalWidth),void 0===n.height&&(n.height=e.naturalHeight)),n}throw new Error("This object does not seem to be an image.")}"undefined"!=typeof window&&(Canvas.Image=Image);const Image$1=Canvas.Image;function loadBase64Image(e){return new Promise((a,t)=>{const n=new Image$1;n.onload=(()=>{a(n)}),n.onerror=t;try{n.src=e}catch(e){t(e)}})}function base64URLToImage(e,a,t,n){loadBase64Image(e).then(t,n)}function getImageSize(e){return{width:e.width||e.naturalWidth,height:e.height||e.naturalHeight}}function canvasFromImage(e){const a=getImageSize(e),t=new Canvas(a.width,a.height),n=t.getContext("2d");return n.drawImage(e,0,0,a.width,a.height),{canvas:t,ctx:n}}function base64URLToImageData(e,a,t,n){loadBase64Image(e).then(e=>{const a=getImageSize(e),n=canvasFromImage(e).ctx.getImageData(0,0,a.width,a.height);n.width||(n.width=a.width),n.height||(n.height=a.height),t(n)},n)}function isImageData(e){return e&&"number"==typeof e.width&&"number"==typeof e.height&&e.data&&"number"==typeof e.data.length&&"object"==typeof e.data}function imageDataToBase64(e,a){return new Promise((t,n)=>{if(isImageData(e)){const n=new Canvas(e.width,e.height);n.getContext("2d").putImageData(e,0,0),t(n.toDataURL("image/jpeg",a/100))}else n(new Error("object is not valid imageData"))})}const objectAssign=Object.assign;function glitch(e){let a,t;e=sanitizeInput(e);const n=new Worker(URL.createObjectURL(new Blob(['function isImageData(a){return a&&"number"==typeof a.width&&"number"==typeof a.height&&a.data&&"number"==typeof a.data.length&&"object"==typeof a.data}const base64Chars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",base64Map$1=base64Chars.split(""),reversedBase64Map$1={};base64Map$1.forEach((a,e)=>{reversedBase64Map$1[a]=e});var maps={base64Map:base64Map$1,reversedBase64Map:reversedBase64Map$1};const reversedBase64Map=maps.reversedBase64Map;function base64ToByteArray(a){const e=[];let s;const t=a.replace("data:image/jpeg;base64,","");for(var r=0,i=t.length;r>4);break;case 2:e.push((15&s)<<4|a>>2);break;case 3:e.push((3&s)<<6|a)}s=a}return e}function jpgHeaderLength(a){let e=417;for(let s=0,t=a.length;si&&(s=i),a[~~(r+s)]=~~(256*n)}return a}const base64Map=maps.base64Map;function byteArrayToBase64(a){const e=["data:image/jpeg;base64,"];let s,t;for(let r=0,i=a.length;r>2]);break;case 1:e.push(base64Map[(3&t)<<4|i>>4]);break;case 2:e.push(base64Map[(15&t)<<2|i>>6]),e.push(base64Map[63&i])}t=i}return 0===s?(e.push(base64Map[(3&t)<<4]),e.push("==")):1===s&&(e.push(base64Map[(15&t)<<2]),e.push("=")),e.join("")}function glitchImageData(a,e,s){if(isImageData(a)){return byteArrayToBase64(glitchByteArray(base64ToByteArray(e),s.seed,s.amount,s.iterations))}throw new Error("glitchImageData: imageData seems to be corrupt.")}function fail(a){self.postMessage({err:a.message||a})}function success(a){self.postMessage({base64URL:a})}onmessage=(a=>{const e=a.data.imageData,s=a.data.params,t=a.data.base64URL;if(e&&t&&s)try{void 0===e.width&&"number"==typeof a.data.imageDataWidth&&(e.width=a.data.imageDataWidth),void 0===e.height&&"number"==typeof a.data.imageDataHeight&&(e.height=a.data.imageDataHeight),success(glitchImageData(e,t,s))}catch(a){fail(a)}else a.data.imageData?fail("Parameters are missing."):fail("ImageData is missing.");self.close()});'],{type:"text/javascript"}))),s={getParams:function(){return e},getInput:o,getOutput:h},i={fromImageData:function(e){return c(g,e)},fromImage:function(e){return c(imageToImageData,e)}},r={toImage:function(e){return u(base64URLToImage,e,!0)},toDataURL:function(e){return u(g)},toImageData:function(e){return u(base64URLToImageData,e,!0)}};function o(){const e=objectAssign({},s);return a||objectAssign(e,i),e}function h(){const e=objectAssign({},s);return t||objectAssign(e,r),e}function g(e){return e}function c(e,t,n){return a=(()=>new Promise((a,s)=>{if(n)e(t,a,s);else if(e===g)a(t);else try{a(e(t,a,s))}catch(e){s(e)}})),m()?d():h()}function u(e,a,n){return t=(t=>new Promise((s,i)=>{n?e(t,a,s,i):e===g?s(t):e(t,a).then(s,i)})),m()?d():o()}function m(){return a&&t}function d(){return new Promise((s,i)=>{a().then(a=>(function(e,a){return new Promise((t,s)=>{imageDataToBase64(e,a.quality).then(t=>(function(e,a,t){return new Promise((s,i)=>{n.addEventListener("message",e=>{e.data&&e.data.base64URL?s(e.data.base64URL):e.data&&e.data.err?i(e.data.err):i(e)}),n.postMessage({params:t,base64URL:a,imageData:e,imageDataWidth:e.width,imageDataHeight:e.height})})})(e,t,a),s).then(t,s)})})(a,e),i).then(e=>{t(e).then(s,i)},i)})}return o()}export default glitch;