/**
 * Minified by jsDelivr using Terser v5.39.0.
 * Original file: /npm/p5.glitch@0.1.5/p5.glitch.js
 *
 * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files
 */
class Glitch{constructor(t){this.p5=void 0!==t?t:p5.instance,this.version="0.1.5",this.mode="image",this.width=1,this.height=1,this.image=this.p5.createImage(1,1),this.bytes=[],this.bytesGlitched=[],this.hex=[],this.hexGlitched=[],this.base64="",this.base64Glitched="",this.base64Type="",this.limitStart=.2,this.limitStop=1,this.fileType="image/jpeg",this.fileQuality=1,this.fileFormat="jpeg",this.types=[],this.initTypes(),this.initBase64(),this.errorOut=!0,this.debugOut=!1}loadBytes(t,e){this.debugMsg("p5.glitch - loadBytes()..."),"string"==typeof t?this.p5.loadBytes(t,(t=>{this.parseBytes(t.bytes,e)}),function(){this.errorMsg("p5.glitch - error loading bytes")}.bind(this)):"object"==typeof t&&t.hasOwnProperty("bytes")&&t.bytes.length>0?this.parseBytes(t.bytes,e):"object"==typeof t&&"Uint8Array"==t.constructor.name&&t.length>0||t instanceof Array&&t.length>0?this.parseBytes(t,e):this.errorMsg("p5.glitch - error loading bytes")}parseBytes(t,e){this.debugMsg("p5.glitch - parseBytes()..."),this.mode="binary",this.bytes=t,this.bytesGlitched=this.bytes,this.debugMsg("p5.glitch - binary loaded - bytes.length: "+this.bytes.length),"function"==typeof e&&void 0!==e&&e()}saveBytes(t="p5.glitch"){if("binary"===this.mode){this.debugMsg("p5.glitch - saveBytes()...");var e=document.createElement("a");document.body.appendChild(e),e.style="display: none","p5.glitch"===t&&(t=t+"_"+this.timeStamp(),this.debugMsg("p5.glitch - saving unknown datatype, use saveBytes(filename.xxx) with file extension"));let i=new Blob([this.bytesGlitched],{type:"octet/stream"}),s=window.URL.createObjectURL(i);e.href=s,e.download=t,e.click(),window.URL.revokeObjectURL(s),this.debugMsg("p5.glitch - binary downloaded")}}initTypes(){let t=[],e=document.createElement("canvas");return["image/png","image/jpeg","image/jpg","image/webp","image/ico","image/bmp","image/gif","image/tif"].forEach((i=>{e.toDataURL(i).startsWith(`data:${i}`)&&t.push(i)})),this.types=t,e.remove(),t}loadType(t){-1===t.indexOf("/")&&(t="image/"+t),"image/jpg"===t&&(t="image/jpeg"),-1===this.types.indexOf(t)&&this.errorMsg("p5.glitch - "+t+" is not supported in this browser"),this.fileType=t,this.debugMsg("p5.glitch - loadType: "+this.fileType)}loadQuality(t){this.fileQuality=this.p5.constrain(t,0,1),this.debugMsg("p5.glitch - loadQuality: "+this.fileQuality)}loadImage(t,e){this.debugMsg("p5.glitch - loadImage()..."),"string"==typeof t?this.p5.loadImage(t,(t=>{this.parseImage(t,e)}),function(){this.errorMsg("p5.glitch - error loading image")}.bind(this)):t.hasOwnProperty("width")&&t.width>0?this.parseImage(t,e):this.errorMsg("p5.glitch - error loading image")}parseImage(t,e){this.debugMsg("p5.glitch - parseImage()..."),this.mode="image";let i=t.get().canvas.toDataURL(this.fileType,this.fileQuality).split(",");this.fileFormat=this.fileType.split("/")[1],this.base64Type=i[0],this.base64=i[1],this.base64Glitched=this.base64,this.bytes=this.base64ToBytes(i[1]),this.bytesGlitched=this.bytes,this.buildImage(this.bytes),this.debugMsg("p5.glitch - image loaded - bytes.length: "+this.bytes.length),"function"==typeof e&&void 0!==e&&e()}scaleImage(t){let e=1;e=this.p5.height>=this.p5.width||this.p5.abs(this.p5.height-this.p5.width)<5?this.p5.height/t.height:this.p5.width/t.width,this.width=t.width*e,this.height=t.height*e}buildImage(t){"image"===this.mode&&""!==this.base64Type&&""!==this.base64Glitched&&this.p5.loadImage(this.base64Type+", "+this.base64Glitched,(e=>{this.scaleImage(e),this.image=e.get(),"function"==typeof t&&void 0!==t&&t(e)}),function(){this.errorMsg("p5.glitch - error building image")}.bind(this))}saveImage(t="p5.glitch"){"image"===this.mode&&(this.debugMsg("p5.glitch - saveImage()..."),"p5.glitch"===t&&(t=t+"_"+this.timeStamp()),this.image.save(t+"."+this.fileFormat),this.debugMsg("p5.glitch - image downloaded"))}saveSafe(t="p5.glitch",e="png"){if("image"===this.mode){this.debugMsg("p5.glitch - saveSafe()..."),"p5.glitch"===t&&(t=t+"_"+this.timeStamp()),void 0!==e&&"jpg"===e&&(e="jpg"),this.image.get().save(t+"_safe."+e),this.debugMsg("p5.glitch - safe image downloaded")}}saveCanvas(t="p5.glitch",e="png"){"image"===this.mode&&(this.debugMsg("p5.glitch - saveCanvas()..."),"p5.glitch"===t&&(t=t+"_"+this.timeStamp()),void 0!==e&&"jpg"===e&&(e="jpg"),this.p5.save(t+"_canvas."+e),this.debugMsg("p5.glitch - canvas downloaded"))}resetBytes(){this.bytesGlitched=this.bytes.slice(),this.base64Glitched=this.base64,this.debugMsg("p5.glitch - resetBytes()")}limitBytes(t=.2,e=1){this.limitStart=this.p5.constrain(t,0,1),this.limitStop=this.p5.constrain(e,this.limitStart,1),this.debugMsg("p5.glitch - limitStart: "+t+"/ limitStop: "+e)}parseLimit(t){void 0!==t&&t instanceof Array&&(1===t.length?this.limitBytes(t[0]):2===t.length&&this.limitBytes(t[0],t[1]))}randomByte(t){t=this.parsePosition(t);let e=this.bytesGlitched.slice();e[t]=this.parseByte(this.p5.random(255)),this.updateBytes(e)}randomBytes(t,e){let i=this.bytesGlitched.slice();void 0===t&&(t=1);for(let s=0;s<t;s++){e=void 0!==e?this.parseByte(e):this.parseByte(this.p5.random(255)),i[this.p5.floor(this.p5.random(i.length*this.limitStart,i.length*this.limitStop))]=e}this.updateBytes(i)}replaceByte(t,e){let i=this.bytesGlitched.slice();i[t=this.parsePosition(t)]=this.parseByte(e),this.updateBytes(i)}replaceBytes(t,e){let i=this.bytesGlitched.slice();t=this.parseByte(t),e=this.parseByte(e);let s=this.p5.floor(i.length*this.limitStart),h=this.p5.floor(i.length*this.limitStop);for(let a=s;a<h;a++)i[a]===t&&(i[a]=e);this.updateBytes(i)}replaceHex(t,e){let i=this.bytesGlitched.slice(),s=this.bytesToHex(i);"string"==typeof t&&"string"==typeof e&&(s=s.split(t).join(e)),this.updateBytes(this.hexToBytes(s))}swapBytes(t,e){let i=this.bytesGlitched.slice();t=this.parseByte(t),e=this.parseByte(e);let s=this.p5.floor(i.length*this.limitStart),h=this.p5.floor(i.length*this.limitStop);for(let a=s;a<h;a++)i[a]===t?i[a]=e:i[a]===e&&(i[a]=t);this.updateBytes(i)}getOffset(t,e=null){let i=this.bytesToHex(this.bytesGlitched).indexOf(t.toLowerCase())/2;return null!=e&&(i+=t.length/2+e),i}getByte(t){return this.bytesGlitched[t%this.bytesGlitched.length]}getHex(t){let e=this.bytesGlitched[t%this.bytesGlitched.length];return this.bytesToHex([e])}updateBytes(t=this.bytes){this.base64Glitched=this.bytesToBase64(t.slice()),this.bytesGlitched=t.slice()}parsePosition(t,e){"string"==typeof t&&(t=parseInt(t,16));let i=this.bytesGlitched.length;return void 0!==e&&e instanceof Array&&e.length>0&&(i=e.length),t=this.p5.floor(this.p5.constrain(t,0,i))}parseByte(t){return"string"==typeof t?t=parseInt(t,16):isNaN(t)||(t=this.p5.floor(this.p5.constrain(t,0,255))),t}pixelate(t=1){t=this.p5.constrain(t,0,this.p5.displayDensity()),this.p5.pixelDensity(t),document.body.style.imageRendering="pixelated",this.p5.noSmooth(),this.debugMsg("p5.glitch - pixelate density: "+t)}errors(t){if("boolean"!=typeof t)return this.errorMsg("p5.glitch - errors() use true/false"),!1;this.errorOut=t,this.errorOut?window.removeEventListener("error",(function(){return!0})):window.addEventListener("error",(function(){return!0})),this.debugMsg("p5.glitch - errors: "+this.errorOut)}errorMsg(t){(this.errorOut||this.debugOut)&&console.log(t)}debug(t=!0){this.debugOut=t,this.debugOut&&(this.debugMsg("p5.glitch - debug: "+this.debugOut),this.debugMsg("p5.glitch - available image types:\n"+this.types.join(", ")))}debugMsg(t){this.debugOut&&console.log(t)}timeStamp(){let t=new Date;return t.setTime(t.getTime()-60*(new Date).getTimezoneOffset()*1e3),t.toISOString().replace(/[^0-9]/g,"").slice(0,-3)}initBase64(){this.base64abc=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"],this.base64codes=[255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,62,255,255,255,63,52,53,54,55,56,57,58,59,60,61,255,255,255,0,255,255,255,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,255,255,255,255,255,255,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]}getBase64Code(t){if(t>this.base64codes.length)throw new Error("Unable to parse base64 string.");const e=this.base64codes[t];if(255===e)throw new Error("Unable to parse base64 string.");return e}bytesToBase64(t){let e,i="",s=t.length;for(e=2;e<s;e+=3)i+=this.base64abc[t[e-2]>>2],i+=this.base64abc[(3&t[e-2])<<4|t[e-1]>>4],i+=this.base64abc[(15&t[e-1])<<2|t[e]>>6],i+=this.base64abc[63&t[e]];return e===s+1&&(i+=this.base64abc[t[e-2]>>2],i+=this.base64abc[(3&t[e-2])<<4],i+="=="),e===s&&(i+=this.base64abc[t[e-2]>>2],i+=this.base64abc[(3&t[e-2])<<4|t[e-1]>>4],i+=this.base64abc[(15&t[e-1])<<2],i+="="),i}base64ToBytes(t){if(t.length%4!=0)throw new Error("Unable to parse base64 string.");const e=t.indexOf("=");if(-1!==e&&e<t.length-2)throw new Error("Unable to parse base64 string.");let i,s=t.endsWith("==")?2:t.endsWith("=")?1:0,h=t.length,a=new Uint8Array(h/4*3);for(let e=0,s=0;e<h;e+=4,s+=3)i=this.getBase64Code(t.charCodeAt(e))<<18|this.getBase64Code(t.charCodeAt(e+1))<<12|this.getBase64Code(t.charCodeAt(e+2))<<6|this.getBase64Code(t.charCodeAt(e+3)),a[s]=i>>16,a[s+1]=i>>8&255,a[s+2]=255&i;return a.subarray(0,a.length-s)}hexToBytes(t){for(var e=[],i=0;i<t.length;i+=2)e.push(parseInt(t.substr(i,2),16));return e}bytesToHex(t){for(var e=[],i=0;i<t.length;i++){var s=t[i]<0?t[i]+256:t[i];e.push((s>>>4).toString(16)),e.push((15&s).toString(16))}return e.join("")}}
//# sourceMappingURL=/sm/121d88ec2b639337c91d47c8f3ecc60c4ce5dba07479c7fffa708ee8a0be5c9f.map