/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import e from"../../../../../core/Error.js";import{throwIfAborted as t}from"../../../../../core/promiseUtils.js";import{isDataProtocol as n,dataComponents as r}from"../../../../../core/urlUtils.js";function a(e){const t=m(e);return null!=t?t.toDataURL():""}async function i(n,r){const a=m(n);if(null==a)throw new e("imageToArrayBuffer","Unsupported image type");const i=o(n),c=await new Promise(e=>a.toBlob(e,i));if(t(r),!c)throw new e("imageToArrayBuffer","Failed to encode image");const s=await c.arrayBuffer();return t(r),{data:s,type:i}}function o(e){if(!(e instanceof HTMLImageElement))return"image/png";const t=e.src;if(n(t)){const e=r(t);return"image/jpeg"===e?.mediaType?e.mediaType:"image/png"}return/\.png$/i.test(t)?"image/png":/\.(jpg|jpeg)$/i.test(t)?"image/jpeg":"image/png"}function m(e){if(e instanceof HTMLCanvasElement)return e;if(e instanceof HTMLVideoElement)return null;const t=document.createElement("canvas");t.width=e.width,t.height=e.height;const n=t.getContext("2d");return e instanceof HTMLImageElement?n.drawImage(e,0,0,e.width,e.height):e instanceof ImageData&&n.putImageData(e,0,0),t}function c(e){const t=[],n=new Uint8Array(e);for(let r=0;r<n.length;r++)t.push(String.fromCharCode(n[r]));return"data:application/octet-stream;base64,"+btoa(t.join(""))}function s(e){if(e.byteLength<8)return!1;const t=new Uint8Array(e);return 137===t[0]&&80===t[1]&&78===t[2]&&71===t[3]&&13===t[4]&&10===t[5]&&26===t[6]&&10===t[7]}export{c as encodeBase64DataUri,i as imageToArrayBuffer,a as imageToDataURI,m as imageTypeToCanvas,s as isArrayBufferPNG};
