/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import t from"../../../../core/Error.js";const e=16;function n(t,n){return n=Math.floor(n/e)*e,Math.min(Math.round(t/e)*e,n)}function r(t,n){return n=Math.floor(n/e)*e,Math.min(Math.ceil(t/e)*e,n)}function o(t,e){const[n,r]=a(t,e);return t.width===n&&t.height===r?t:i(t,n,r)}function a({width:t,height:e},{maxPreferredTexturePixels:r,maxTextureSize:o}){const a=Math.max(t,e),i=t*e;if(a<=o&&i<=r)return[t,e];const h=Math.min(Math.sqrt(r/i),o/a);return[n(Math.round(t*h),o),n(Math.round(e*h),o)]}function i(t,e,n){if(t instanceof ImageData)return i(h(t),e,n);const r=document.createElement("canvas");r.width=e,r.height=n;return r.getContext("2d").drawImage(t,0,0,r.width,r.height),r}function h(e){const n=document.createElement("canvas");n.width=e.width,n.height=e.height;const r=n.getContext("2d");if(null==r)throw new t("texture:context-failed","Failed to create 2d context from HTMLCanvasElement");return r.putImageData(e,0,0),n}export{n as applyTextureResizeModulo,r as applyTextureResizeModuloCeil,o as ensureImageMaxSize,a as ensureTextureSize};
