image-js 1.6.2 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/dist/image-js.esm.js +8322 -7458
  2. package/dist/image-js.esm.js.map +1 -1
  3. package/dist/image-js.esm.min.js +2 -2
  4. package/dist/image-js.esm.min.js.map +1 -1
  5. package/dist/image-js.umd.js +8322 -7457
  6. package/dist/image-js.umd.js.map +1 -1
  7. package/dist/image-js.umd.min.js +2 -2
  8. package/dist/image-js.umd.min.js.map +1 -1
  9. package/dist-types/image-js.d.ts +40 -3
  10. package/lib/featureMatching/keypoints/getEigenvaluesForScore.d.ts +2 -2
  11. package/lib/featureMatching/keypoints/getEigenvaluesForScore.d.ts.map +1 -1
  12. package/lib/featureMatching/keypoints/getEigenvaluesForScore.js +27 -22
  13. package/lib/featureMatching/keypoints/getEigenvaluesForScore.js.map +1 -1
  14. package/lib/featureMatching/keypoints/getFastKeypoints.d.ts +4 -0
  15. package/lib/featureMatching/keypoints/getFastKeypoints.d.ts.map +1 -1
  16. package/lib/featureMatching/keypoints/getFastKeypoints.js +20 -5
  17. package/lib/featureMatching/keypoints/getFastKeypoints.js.map +1 -1
  18. package/lib/featureMatching/keypoints/getHarrisScore.js +1 -1
  19. package/lib/featureMatching/keypoints/getHarrisScore.js.map +1 -1
  20. package/lib/featureMatching/keypoints/getShiTomasiScore.d.ts +1 -2
  21. package/lib/featureMatching/keypoints/getShiTomasiScore.d.ts.map +1 -1
  22. package/lib/featureMatching/keypoints/getShiTomasiScore.js +1 -1
  23. package/lib/featureMatching/keypoints/getShiTomasiScore.js.map +1 -1
  24. package/lib/featureMatching/visualize/drawKeypoints.d.ts +2 -1
  25. package/lib/featureMatching/visualize/drawKeypoints.d.ts.map +1 -1
  26. package/lib/featureMatching/visualize/drawKeypoints.js +13 -0
  27. package/lib/featureMatching/visualize/drawKeypoints.js.map +1 -1
  28. package/lib/maskAnalysis/getContourMoore.d.ts.map +1 -1
  29. package/lib/maskAnalysis/getContourMoore.js +4 -2
  30. package/lib/maskAnalysis/getContourMoore.js.map +1 -1
  31. package/lib/maskAnalysis/getConvexityDefects.d.ts +17 -0
  32. package/lib/maskAnalysis/getConvexityDefects.d.ts.map +1 -0
  33. package/lib/maskAnalysis/getConvexityDefects.js +75 -0
  34. package/lib/maskAnalysis/getConvexityDefects.js.map +1 -0
  35. package/lib/maskAnalysis/getExternalContour.d.ts +1 -1
  36. package/lib/maskAnalysis/getExternalContour.d.ts.map +1 -1
  37. package/lib/maskAnalysis/index.d.ts +1 -0
  38. package/lib/maskAnalysis/index.d.ts.map +1 -1
  39. package/lib/maskAnalysis/index.js +1 -0
  40. package/lib/maskAnalysis/index.js.map +1 -1
  41. package/lib/roi/getExternalContour.d.ts +1 -1
  42. package/lib/roi/getExternalContour.d.ts.map +1 -1
  43. package/package.json +2 -2
  44. package/src/featureMatching/keypoints/getEigenvaluesForScore.ts +29 -27
  45. package/src/featureMatching/keypoints/getFastKeypoints.ts +23 -5
  46. package/src/featureMatching/keypoints/getHarrisScore.ts +1 -1
  47. package/src/featureMatching/keypoints/getShiTomasiScore.ts +2 -3
  48. package/src/featureMatching/visualize/drawKeypoints.ts +20 -3
  49. package/src/maskAnalysis/getContourMoore.ts +4 -3
  50. package/src/maskAnalysis/getConvexityDefects.ts +105 -0
  51. package/src/maskAnalysis/index.ts +1 -0
@@ -1,3 +1,3 @@
1
- /* image-js 1.6.2 | MIT | https://github.com/image-js/image-js#readme */
2
- !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).IJS={})}(this,function(exports){"use strict";var _documentCurrentScript="undefined"!=typeof document?document.currentScript:null;function getDefaultExportFromCjs(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function getAugmentedNamespace(t){if(Object.prototype.hasOwnProperty.call(t,"__esModule"))return t;var e=t.default;if("function"==typeof e){var r=function t(){var r=!1;try{r=this instanceof t}catch{}return r?Reflect.construct(e,arguments,this.constructor):e.apply(this,arguments)};r.prototype=e.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:!0}),Object.keys(t).forEach(function(e){var n=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(r,e,n.get?n:{enumerable:!0,get:function(){return t[e]}})}),r}var matrix={};const toString=Object.prototype.toString;function isAnyArray$1(t){const e=toString.call(t);return e.endsWith("Array]")&&!e.includes("Big")}var libEsm=Object.freeze({__proto__:null,isAnyArray:isAnyArray$1}),require$$0=getAugmentedNamespace(libEsm);function max$2(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,n=void 0===r?0:r,o=e.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var s=t[n],a=n+1;a<i;a++)t[a]>s&&(s=t[a]);return s}function min$1(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,n=void 0===r?0:r,o=e.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var s=t[n],a=n+1;a<i;a++)t[a]<s&&(s=t[a]);return s}function rescale$1(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!isAnyArray$1(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var n=min$1(t),o=max$2(t);if(n===o)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=r.min,s=void 0===i?r.autoMinMax?n:0:i,a=r.max,l=void 0===a?r.autoMinMax?o:1:a;if(s>=l)throw new RangeError("min option must be smaller than max option");for(var h=(l-s)/(o-n),c=0;c<t.length;c++)e[c]=(t[c]-n)*h+s;return e}var libEs6=Object.freeze({__proto__:null,default:rescale$1}),require$$1=getAugmentedNamespace(libEs6);Object.defineProperty(matrix,"__esModule",{value:!0});var isAnyArray=require$$0,rescale=require$$1;const indent=" ".repeat(2),indentData=" ".repeat(4);function inspectMatrix(){return inspectMatrixWithOptions(this)}function inspectMatrixWithOptions(t,e={}){const{maxRows:r=15,maxColumns:n=10,maxNumSize:o=8,padMinus:i="auto"}=e;return`${t.constructor.name} {\n${indent}[\n${indentData}${inspectData(t,r,n,o,i)}\n${indent}]\n${indent}rows: ${t.rows}\n${indent}columns: ${t.columns}\n}`}function inspectData(t,e,r,n,o){const{rows:i,columns:s}=t,a=Math.min(i,e),l=Math.min(s,r),h=[];if("auto"===o){o=!1;t:for(let e=0;e<a;e++)for(let r=0;r<l;r++)if(t.get(e,r)<0){o=!0;break t}}for(let e=0;e<a;e++){let r=[];for(let i=0;i<l;i++)r.push(formatNumber(t.get(e,i),n,o));h.push(`${r.join(" ")}`)}return l!==s&&(h[h.length-1]+=` ... ${s-r} more columns`),a!==i&&h.push(`... ${i-e} more rows`),h.join(`\n${indentData}`)}function formatNumber(t,e,r){return(t>=0&&r?` ${formatNumber2(t,e-1)}`:formatNumber2(t,e)).padEnd(e)}function formatNumber2(t,e){let r=t.toString();if(r.length<=e)return r;let n=t.toFixed(e);if(n.length>e&&(n=t.toFixed(Math.max(0,e-(n.length-e)))),n.length<=e&&!n.startsWith("0.000")&&!n.startsWith("-0.000"))return n;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function installMathOperations(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t);return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t.get(e,r));return this}}function checkRowIndex(t,e,r){let n=r?t.rows:t.rows-1;if(e<0||e>n)throw new RangeError("Row index out of range")}function checkColumnIndex(t,e,r){let n=r?t.columns:t.columns-1;if(e<0||e>n)throw new RangeError("Column index out of range")}function checkRowVector(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function checkColumnVector(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function checkRowIndices(t,e){if(!isAnyArray.isAnyArray(e))throw new TypeError("row indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.rows)throw new RangeError("row indices are out of range")}function checkColumnIndices(t,e){if(!isAnyArray.isAnyArray(e))throw new TypeError("column indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.columns)throw new RangeError("column indices are out of range")}function checkRange$1(t,e,r,n,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(checkNumber("startRow",e),checkNumber("endRow",r),checkNumber("startColumn",n),checkNumber("endColumn",o),e>r||n>o||e<0||e>=t.rows||r<0||r>=t.rows||n<0||n>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function newArray(t,e=0){let r=[];for(let n=0;n<t;n++)r.push(e);return r}function checkNumber(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function checkNonEmpty(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}function sumByRow(t){let e=newArray(t.rows);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]+=t.get(r,n);return e}function sumByColumn(t){let e=newArray(t.columns);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]+=t.get(r,n);return e}function sumAll(t){let e=0;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e+=t.get(r,n);return e}function productByRow(t){let e=newArray(t.rows,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]*=t.get(r,n);return e}function productByColumn(t){let e=newArray(t.columns,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]*=t.get(r,n);return e}function productAll(t){let e=1;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e*=t.get(r,n);return e}function varianceByRow(t,e,r){const n=t.rows,o=t.columns,i=[];for(let s=0;s<n;s++){let n=0,a=0,l=0;for(let e=0;e<o;e++)l=t.get(s,e)-r[s],n+=l,a+=l*l;e?i.push((a-n*n/o)/(o-1)):i.push((a-n*n/o)/o)}return i}function varianceByColumn(t,e,r){const n=t.rows,o=t.columns,i=[];for(let s=0;s<o;s++){let o=0,a=0,l=0;for(let e=0;e<n;e++)l=t.get(e,s)-r[s],o+=l,a+=l*l;e?i.push((a-o*o/n)/(n-1)):i.push((a-o*o/n)/n)}return i}function varianceAll(t,e,r){const n=t.rows,o=t.columns,i=n*o;let s=0,a=0,l=0;for(let e=0;e<n;e++)for(let n=0;n<o;n++)l=t.get(e,n)-r,s+=l,a+=l*l;return e?(a-s*s/i)/(i-1):(a-s*s/i)/i}function centerByRow(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[r])}function centerByColumn(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[n])}function centerAll(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e)}function getScaleByRow(t){const e=[];for(let r=0;r<t.rows;r++){let n=0;for(let e=0;e<t.columns;e++)n+=t.get(r,e)**2/(t.columns-1);e.push(Math.sqrt(n))}return e}function scaleByRow(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[r])}function getScaleByColumn(t){const e=[];for(let r=0;r<t.columns;r++){let n=0;for(let e=0;e<t.rows;e++)n+=t.get(e,r)**2/(t.rows-1);e.push(Math.sqrt(n))}return e}function scaleByColumn(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[n])}function getScaleAll(t){const e=t.size-1;let r=0;for(let n=0;n<t.columns;n++)for(let o=0;o<t.rows;o++)r+=t.get(o,n)**2/e;return Math.sqrt(r)}function scaleAll(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e)}class AbstractMatrix{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let n=new Matrix$2(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)n.set(o,t,r[o*e+t]);return n}static rowVector(t){let e=new Matrix$2(1,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r]);return e}static columnVector(t){let e=new Matrix$2(t.length,1);for(let r=0;r<t.length;r++)e.set(r,0,t[r]);return e}static zeros(t,e){return new Matrix$2(t,e)}static ones(t,e){return new Matrix$2(t,e).fill(1)}static rand(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{random:n=Math.random}=r;let o=new Matrix$2(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,n());return o}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:n=0,max:o=1e3,random:i=Math.random}=r;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(n>=o)throw new RangeError("min must be smaller than max");let s=o-n,a=new Matrix$2(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=n+Math.round(i()*s);a.set(r,t,e)}return a}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let n=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<n;t++)o.set(t,t,r);return o}static diag(t,e,r){let n=t.length;void 0===e&&(e=n),void 0===r&&(r=e);let o=Math.min(n,e,r),i=this.zeros(e,r);for(let e=0;e<o;e++)i.set(e,e,t[e]);return i}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new Matrix$2(r,n);for(let i=0;i<r;i++)for(let r=0;r<n;r++)o.set(i,r,Math.min(t.get(i,r),e.get(i,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new this(r,n);for(let i=0;i<r;i++)for(let r=0;r<n;r++)o.set(i,r,Math.max(t.get(i,r),e.get(i,r)));return o}static checkMatrix(t){return AbstractMatrix.isMatrix(t)?t:new Matrix$2(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.call(this,e,r);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.push(this.get(e,r));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let r=0;r<this.columns;r++)t[e].push(this.get(e,r))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);t++}return n}isReducedEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(n=!1);t++}return n}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let n=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(n,r)&&(n=o);if(0===t.get(n,r))r++;else{t.swapRows(e,n);let o=t.get(e,r);for(let n=r;n<t.columns;n++)t.set(e,n,t.get(e,n)/o);for(let n=e+1;n<t.rows;n++){let o=t.get(n,r)/t.get(e,r);t.set(n,r,0);for(let i=r+1;i<t.columns;i++)t.set(n,i,t.get(n,i)-t.get(e,i)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,n=r-1;for(;n>=0;)if(0===t.maxRow(n))n--;else{let o=0,i=!1;for(;o<r&&!1===i;)1===t.get(n,o)?i=!0:o++;for(let r=0;r<n;r++){let i=t.get(r,o);for(let s=o;s<e;s++){let e=t.get(r,s)-i*t.get(n,s);t.set(r,s,e)}}n--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let n=new Matrix$2(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)n.setSubMatrix(this,this.rows*t,this.columns*e);return n}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){checkRowIndex(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return Matrix$2.rowVector(this.getRow(t))}setRow(t,e){checkRowIndex(this,t),e=checkRowVector(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){checkRowIndex(this,t),checkRowIndex(this,e);for(let r=0;r<this.columns;r++){let n=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,n)}return this}getColumn(t){checkColumnIndex(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return Matrix$2.columnVector(this.getColumn(t))}setColumn(t,e){checkColumnIndex(this,t),e=checkColumnVector(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){checkColumnIndex(this,t),checkColumnIndex(this,e);for(let r=0;r<this.rows;r++){let n=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,n)}return this}addRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[r]);return this}subRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[r]);return this}mulRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[r]);return this}divRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[r]);return this}addColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){checkRowIndex(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){checkColumnIndex(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){checkNonEmpty(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)>t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){checkNonEmpty(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)<t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}maxRow(t){if(checkRowIndex(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r));return e}maxRowIndex(t){checkRowIndex(this,t),checkNonEmpty(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n),r[1]=n);return r}minRow(t){if(checkRowIndex(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r));return e}minRowIndex(t){checkRowIndex(this,t),checkNonEmpty(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n),r[1]=n);return r}maxColumn(t){if(checkColumnIndex(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t));return e}maxColumnIndex(t){checkColumnIndex(this,t),checkNonEmpty(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t),r[0]=n);return r}minColumn(t){if(checkColumnIndex(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t));return e}minColumnIndex(t){checkColumnIndex(this,t),checkNonEmpty(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t),r[0]=n);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){AbstractMatrix.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let n=0;n<e.length;n++)r+=e[n]*t[n];return r}mmul(t){t=Matrix$2.checkMatrix(t);let e=this.rows,r=this.columns,n=t.columns,o=new Matrix$2(e,n),i=new Float64Array(r);for(let s=0;s<n;s++){for(let e=0;e<r;e++)i[e]=t.get(e,s);for(let t=0;t<e;t++){let e=0;for(let n=0;n<r;n++)e+=this.get(t,n)*i[n];o.set(t,s,e)}}return o}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=Matrix$2.eye(this.rows),r=this;for(let n=t;n>=1;n/=2)1&n&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=Matrix$2.checkMatrix(t);let e=new Matrix$2(2,2);const r=this.get(0,0),n=t.get(0,0),o=this.get(0,1),i=t.get(0,1),s=this.get(1,0),a=t.get(1,0),l=this.get(1,1),h=t.get(1,1),c=(r+l)*(n+h),u=(s+l)*n,f=r*(i-h),d=l*(a-n),m=(r+o)*h,g=c+d-m+(o-l)*(a+h),p=f+m,w=u+d,y=c-u+f+(s-r)*(n+i);return e.set(0,0,g),e.set(0,1,p),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=Matrix$2.checkMatrix(t);let e=new Matrix$2(3,3);const r=this.get(0,0),n=this.get(0,1),o=this.get(0,2),i=this.get(1,0),s=this.get(1,1),a=this.get(1,2),l=this.get(2,0),h=this.get(2,1),c=this.get(2,2),u=t.get(0,0),f=t.get(0,1),d=t.get(0,2),m=t.get(1,0),g=t.get(1,1),p=t.get(1,2),w=t.get(2,0),y=t.get(2,1),b=t.get(2,2),x=(r-i)*(-f+g),M=(-r+i+s)*(u-f+g),v=(i+s)*(-u+f),I=r*u,A=(-r+l+h)*(u-d+p),k=(-r+l)*(d-p),R=(l+h)*(-u+d),E=(-o+h+c)*(g+w-y),$=(o-c)*(g-y),P=o*w,S=(h+c)*(-w+y),C=(-o+s+a)*(p+w-b),B=(o-a)*(p-b),T=(s+a)*(-w+b),D=I+P+n*m,_=(r+n+o-i-s-h-c)*g+M+v+I+E+P+S,V=I+A+R+(r+n+o-s-a-l-h)*p+P+C+T,N=x+s*(-u+f+m-g-p-w+b)+M+I+P+C+B,O=x+M+v+I+a*y,F=P+C+B+T+i*d,L=I+A+k+h*(-u+d+m-g-p-w+y)+E+$+P,U=E+$+P+S+l*f,z=I+A+k+R+c*b;return e.set(0,0,D),e.set(0,1,_),e.set(0,2,V),e.set(1,0,N),e.set(1,1,O),e.set(1,2,F),e.set(2,0,L),e.set(2,1,U),e.set(2,2,z),e}mmulStrassen(t){t=Matrix$2.checkMatrix(t);let e=this.clone(),r=e.rows,n=e.columns,o=t.rows,i=t.columns;function s(t,e,r){let n=t.rows,o=t.columns;if(n===e&&o===r)return t;{let n=AbstractMatrix.zeros(e,r);return n=n.setSubMatrix(t,0,0),n}}n!==o&&console.warn(`Multiplying ${r} x ${n} and ${o} x ${i} matrix: dimensions do not match.`);let a=Math.max(r,o),l=Math.max(n,i);return e=s(e,a,l),function t(e,r,n,o){if(n<=512||o<=512)return e.mmul(r);n%2==1&&o%2==1?(e=s(e,n+1,o+1),r=s(r,n+1,o+1)):n%2==1?(e=s(e,n+1,o),r=s(r,n+1,o)):o%2==1&&(e=s(e,n,o+1),r=s(r,n,o+1));let i=parseInt(e.rows/2,10),a=parseInt(e.columns/2,10),l=e.subMatrix(0,i-1,0,a-1),h=r.subMatrix(0,i-1,0,a-1),c=e.subMatrix(0,i-1,a,e.columns-1),u=r.subMatrix(0,i-1,a,r.columns-1),f=e.subMatrix(i,e.rows-1,0,a-1),d=r.subMatrix(i,r.rows-1,0,a-1),m=e.subMatrix(i,e.rows-1,a,e.columns-1),g=r.subMatrix(i,r.rows-1,a,r.columns-1),p=t(AbstractMatrix.add(l,m),AbstractMatrix.add(h,g),i,a),w=t(AbstractMatrix.add(f,m),h,i,a),y=t(l,AbstractMatrix.sub(u,g),i,a),b=t(m,AbstractMatrix.sub(d,h),i,a),x=t(AbstractMatrix.add(l,c),g,i,a),M=t(AbstractMatrix.sub(f,l),AbstractMatrix.add(h,u),i,a),v=t(AbstractMatrix.sub(c,m),AbstractMatrix.add(d,g),i,a),I=AbstractMatrix.add(p,b);I.sub(x),I.add(v);let A=AbstractMatrix.add(y,x),k=AbstractMatrix.add(w,b),R=AbstractMatrix.sub(p,w);R.add(y),R.add(M);let E=AbstractMatrix.zeros(2*I.rows,2*I.columns);return E=E.setSubMatrix(I,0,0),E=E.setSubMatrix(A,I.rows,0),E=E.setSubMatrix(k,0,I.columns),E=E.setSubMatrix(R,I.rows,I.columns),E.subMatrix(0,n-1,0,o-1)}(e,t=s(t,a,l),a,l)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new Matrix$2(this.rows,this.columns);for(let t=0;t<this.rows;t++){const o=this.getRow(t);o.length>0&&rescale(o,{min:e,max:r,output:o}),n.setRow(t,o)}return n}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new Matrix$2(this.rows,this.columns);for(let t=0;t<this.columns;t++){const o=this.getColumn(t);o.length&&rescale(o,{min:e,max:r,output:o}),n.setColumn(t,o)}return n}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),n=this.get(e,this.columns-1-r);this.set(e,r,n),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),n=this.get(this.rows-1-r,e);this.set(r,e,n),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=Matrix$2.checkMatrix(t);let e=this.rows,r=this.columns,n=t.rows,o=t.columns,i=new Matrix$2(e*n,r*o);for(let s=0;s<e;s++)for(let e=0;e<r;e++)for(let r=0;r<n;r++)for(let a=0;a<o;a++)i.set(n*s+r,o*e+a,this.get(s,e)*t.get(r,a));return i}kroneckerSum(t){if(t=Matrix$2.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,n=this.kroneckerProduct(Matrix$2.eye(r,r)),o=Matrix$2.eye(e,e).kroneckerProduct(t);return n.add(o)}transpose(){let t=new Matrix$2(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(t=compareNumbers){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=compareNumbers){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,n){checkRange$1(this,t,e,r,n);let o=new Matrix$2(e-t+1,n-r+1);for(let i=t;i<=e;i++)for(let e=r;e<=n;e++)o.set(i-t,e-r,this.get(i,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let n=new Matrix$2(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let i=e;i<=r;i++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);n.set(o,i-e,this.get(t[o],i))}return n}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let n=new Matrix$2(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let i=e;i<=r;i++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);n.set(i-e,o,this.get(i,t[o]))}return n}setSubMatrix(t,e,r){if((t=Matrix$2.checkMatrix(t)).isEmpty())return this;checkRange$1(this,e,e+t.rows-1,r,r+t.columns-1);for(let n=0;n<t.rows;n++)for(let o=0;o<t.columns;o++)this.set(e+n,r+o,t.get(n,o));return this}selection(t,e){checkRowIndices(this,t),checkColumnIndices(this,e);let r=new Matrix$2(t.length,e.length);for(let n=0;n<t.length;n++){let o=t[n];for(let t=0;t<e.length;t++){let i=e[t];r.set(n,t,this.get(o,i))}}return r}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){return this.constructor.copy(this,new Matrix$2(this.rows,this.columns))}static copy(t,e){for(const[r,n,o]of t.entries())e.set(r,n,o);return e}sum(t){switch(t){case"row":return sumByRow(this);case"column":return sumByColumn(this);case void 0:return sumAll(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return productByRow(this);case"column":return productByColumn(this);case void 0:return productAll(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":for(let t=0;t<this.rows;t++)e[t]/=this.columns;return e;case"column":for(let t=0;t<this.columns;t++)e[t]/=this.rows;return e;case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:n=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!isAnyArray.isAnyArray(n))throw new TypeError("mean must be an array");return varianceByRow(this,r,n);case"column":if(!isAnyArray.isAnyArray(n))throw new TypeError("mean must be an array");return varianceByColumn(this,r,n);case void 0:if("number"!=typeof n)throw new TypeError("mean must be a number");return varianceAll(this,r,n);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!isAnyArray.isAnyArray(r))throw new TypeError("center must be an array");return centerByRow(this,r),this;case"column":if(!isAnyArray.isAnyArray(r))throw new TypeError("center must be an array");return centerByColumn(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return centerAll(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=getScaleByRow(this);else if(!isAnyArray.isAnyArray(r))throw new TypeError("scale must be an array");return scaleByRow(this,r),this;case"column":if(void 0===r)r=getScaleByColumn(this);else if(!isAnyArray.isAnyArray(r))throw new TypeError("scale must be an array");return scaleByColumn(this,r),this;case void 0:if(void 0===r)r=getScaleAll(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return scaleAll(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return inspectMatrixWithOptions(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}function compareNumbers(t,e){return t-e}function isArrayOfNumbers(t){return t.every(t=>"number"==typeof t)}AbstractMatrix.prototype.klass="Matrix","undefined"!=typeof Symbol&&(AbstractMatrix.prototype[Symbol.for("nodejs.util.inspect.custom")]=inspectMatrix),AbstractMatrix.random=AbstractMatrix.rand,AbstractMatrix.randomInt=AbstractMatrix.randInt,AbstractMatrix.diagonal=AbstractMatrix.diag,AbstractMatrix.prototype.diagonal=AbstractMatrix.prototype.diag,AbstractMatrix.identity=AbstractMatrix.eye,AbstractMatrix.prototype.negate=AbstractMatrix.prototype.neg,AbstractMatrix.prototype.tensorProduct=AbstractMatrix.prototype.kroneckerProduct;let Matrix$2=class t extends AbstractMatrix{data;#t(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}constructor(e,r){if(super(),t.isMatrix(e))this.#t(e.rows,e.columns),t.copy(e,this);else if(Number.isInteger(e)&&e>=0)this.#t(e,r);else{if(!isAnyArray.isAnyArray(e))throw new TypeError("First argument must be a positive number or an array");{const t=e;if("number"!=typeof(r=(e=t.length)?t[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<e;n++){if(t[n].length!==r)throw new RangeError("Inconsistent array dimensions");if(!isArrayOfNumbers(t[n]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(t[n]))}this.rows=e,this.columns=r}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return checkRowIndex(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),checkRowIndex(this,t,!0),e=Float64Array.from(checkRowVector(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){checkColumnIndex(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let n=0;n<t;n++)r[n]=this.data[e][n];for(let n=t+1;n<this.columns;n++)r[n-1]=this.data[e][n];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),checkColumnIndex(this,t,!0),e=checkColumnVector(this,e);for(let r=0;r<this.rows;r++){const n=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)n[o]=this.data[r][o];for(n[o++]=e[r];o<this.columns+1;o++)n[o]=this.data[r][o-1];this.data[r]=n}return this.columns+=1,this}};installMathOperations(AbstractMatrix,Matrix$2);class SymmetricMatrix extends AbstractMatrix{#e;get size(){return this.#e.size}get rows(){return this.#e.rows}get columns(){return this.#e.columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return Matrix$2.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){if(super(),Matrix$2.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");this.#e=Matrix$2.copy(t,new Matrix$2(t.rows,t.rows))}else if(Number.isInteger(t)&&t>=0)this.#e=new Matrix$2(t,t);else if(this.#e=new Matrix$2(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new SymmetricMatrix(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())t.set(e,r,n);return t}toMatrix(){return new Matrix$2(this)}get(t,e){return this.#e.get(t,e)}set(t,e,r){return this.#e.set(t,e,r),this.#e.set(e,t,r),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),this.#e.addRow(t,r),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,n]of t.entries())n||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=++n);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const n=new SymmetricMatrix(r);for(let o=0,i=0,s=0;s<e;s++)n.set(o,i,t[s]),++o>=r&&(o=++i);return n}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield[t,e,r],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield r,++e>=this.diagonalSize&&(e=++t)}}}SymmetricMatrix.prototype.klassType="SymmetricMatrix";class DistanceMatrix extends SymmetricMatrix{static isDistanceMatrix(t){return SymmetricMatrix.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new SymmetricMatrix(this)}clone(){const t=new DistanceMatrix(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())e!==r&&t.set(e,r,n);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=1+ ++n);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const n=new this(r);for(let o=1,i=0,s=0;s<e;s++)n.set(o,i,t[s]),++o>=r&&(o=1+ ++i);return n}}DistanceMatrix.prototype.klassSubType="DistanceMatrix";class BaseView extends AbstractMatrix{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class MatrixColumnView extends BaseView{constructor(t,e){checkColumnIndex(t,e),super(t,t.rows,1),this.column=e}set(t,e,r){return this.matrix.set(t,this.column,r),this}get(t){return this.matrix.get(t,this.column)}}class MatrixColumnSelectionView extends BaseView{constructor(t,e){checkColumnIndices(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,r){return this.matrix.set(t,this.columnIndices[e],r),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}}class MatrixFlipColumnView extends BaseView{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(t,this.columns-e-1,r),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}}class MatrixFlipRowView extends BaseView{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(this.rows-t-1,e,r),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}}class MatrixRowView extends BaseView{constructor(t,e){checkRowIndex(t,e),super(t,1,t.columns),this.row=e}set(t,e,r){return this.matrix.set(this.row,e,r),this}get(t,e){return this.matrix.get(this.row,e)}}class MatrixRowSelectionView extends BaseView{constructor(t,e){checkRowIndices(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}}class MatrixSelectionView extends BaseView{constructor(t,e,r){checkRowIndices(t,e),checkColumnIndices(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class MatrixSubView extends BaseView{constructor(t,e,r,n,o){checkRange$1(t,e,r,n,o),super(t,r-e+1,o-n+1),this.startRow=e,this.startColumn=n}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}}class MatrixTransposeView extends BaseView{constructor(t){super(t,t.columns,t.rows)}set(t,e,r){return this.matrix.set(e,t,r),this}get(t,e){return this.matrix.get(e,t)}}let WrapperMatrix1D$1=class extends AbstractMatrix{constructor(t,e={}){const{rows:r=1}=e;if(t.length%r!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=r,this.columns=t.length/r,this.data=t}set(t,e,r){let n=this._calculateIndex(t,e);return this.data[n]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}};class WrapperMatrix2D extends AbstractMatrix{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}function wrap(t,e){if(isAnyArray.isAnyArray(t))return t[0]&&isAnyArray.isAnyArray(t[0])?new WrapperMatrix2D(t):new WrapperMatrix1D$1(t,e);throw new Error("the argument is not an array")}class LuDecomposition{constructor(t){let e,r,n,o,i,s,a,l,h,c=(t=WrapperMatrix2D.checkMatrix(t)).clone(),u=c.rows,f=c.columns,d=new Float64Array(u),m=1;for(e=0;e<u;e++)d[e]=e;for(l=new Float64Array(u),r=0;r<f;r++){for(e=0;e<u;e++)l[e]=c.get(e,r);for(e=0;e<u;e++){for(h=Math.min(e,r),i=0,n=0;n<h;n++)i+=c.get(e,n)*l[n];l[e]-=i,c.set(e,r,l[e])}for(o=r,e=r+1;e<u;e++)Math.abs(l[e])>Math.abs(l[o])&&(o=e);if(o!==r){for(n=0;n<f;n++)s=c.get(o,n),c.set(o,n,c.get(r,n)),c.set(r,n,s);a=d[o],d[o]=d[r],d[r]=a,m=-m}if(r<u&&0!==c.get(r,r))for(e=r+1;e<u;e++)c.set(e,r,c.get(e,r)/c.get(r,r))}this.LU=c,this.pivotVector=d,this.pivotSign=m}isSingular(){let t=this.LU,e=t.columns;for(let r=0;r<e;r++)if(0===t.get(r,r))return!0;return!1}solve(t){t=Matrix$2.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,n,o,i=t.columns,s=t.subMatrixRow(this.pivotVector,0,i-1),a=e.columns;for(o=0;o<a;o++)for(r=o+1;r<a;r++)for(n=0;n<i;n++)s.set(r,n,s.get(r,n)-s.get(o,n)*e.get(r,o));for(o=a-1;o>=0;o--){for(n=0;n<i;n++)s.set(o,n,s.get(o,n)/e.get(o,o));for(r=0;r<o;r++)for(n=0;n<i;n++)s.set(r,n,s.get(r,n)-s.get(o,n)*e.get(r,o))}return s}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,r=t.columns;for(let n=0;n<r;n++)e*=t.get(n,n);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new Matrix$2(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?n.set(o,e,t.get(o,e)):o===e?n.set(o,e,1):n.set(o,e,0);return n}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new Matrix$2(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?n.set(o,e,t.get(o,e)):n.set(o,e,0);return n}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function hypotenuse$1(t,e){let r=0;return Math.abs(t)>Math.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class QrDecomposition{constructor(t){let e,r,n,o,i=(t=WrapperMatrix2D.checkMatrix(t)).clone(),s=t.rows,a=t.columns,l=new Float64Array(a);for(n=0;n<a;n++){let t=0;for(e=n;e<s;e++)t=hypotenuse$1(t,i.get(e,n));if(0!==t){for(i.get(n,n)<0&&(t=-t),e=n;e<s;e++)i.set(e,n,i.get(e,n)/t);for(i.set(n,n,i.get(n,n)+1),r=n+1;r<a;r++){for(o=0,e=n;e<s;e++)o+=i.get(e,n)*i.get(e,r);for(o=-o/i.get(n,n),e=n;e<s;e++)i.set(e,r,i.get(e,r)+o*i.get(e,n))}}l[n]=-t}this.QR=i,this.Rdiag=l}solve(t){t=Matrix$2.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let n,o,i,s,a=t.columns,l=t.clone(),h=e.columns;for(i=0;i<h;i++)for(o=0;o<a;o++){for(s=0,n=i;n<r;n++)s+=e.get(n,i)*l.get(n,o);for(s=-s/e.get(i,i),n=i;n<r;n++)l.set(n,o,l.get(n,o)+s*e.get(n,i))}for(i=h-1;i>=0;i--){for(o=0;o<a;o++)l.set(i,o,l.get(i,o)/this.Rdiag[i]);for(n=0;n<i;n++)for(o=0;o<a;o++)l.set(n,o,l.get(n,o)-l.get(i,o)*e.get(n,i))}return l.subMatrix(0,h-1,0,a-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,n=r.columns,o=new Matrix$2(n,n);for(t=0;t<n;t++)for(e=0;e<n;e++)t<e?o.set(t,e,r.get(t,e)):t===e?o.set(t,e,this.Rdiag[t]):o.set(t,e,0);return o}get orthogonalMatrix(){let t,e,r,n,o=this.QR,i=o.rows,s=o.columns,a=new Matrix$2(i,s);for(r=s-1;r>=0;r--){for(t=0;t<i;t++)a.set(t,r,0);for(a.set(r,r,1),e=r;e<s;e++)if(0!==o.get(r,r)){for(n=0,t=r;t<i;t++)n+=o.get(t,r)*a.get(t,e);for(n=-n/o.get(r,r),t=r;t<i;t++)a.set(t,e,a.get(t,e)+n*o.get(t,r))}}return a}}let SingularValueDecomposition$1=class{constructor(t,e={}){if((t=WrapperMatrix2D.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,n=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:i=!0,autoTranspose:s=!1}=e;let a,l=Boolean(o),h=Boolean(i),c=!1;if(r<n)if(s){a=t.transpose(),r=a.rows,n=a.columns,c=!0;let e=l;l=h,h=e}else a=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else a=t.clone();let u=Math.min(r,n),f=Math.min(r+1,n),d=new Float64Array(f),m=new Matrix$2(r,u),g=new Matrix$2(n,n),p=new Float64Array(n),w=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(r-1,n),x=Math.max(0,Math.min(n-2,r)),M=Math.max(b,x);for(let t=0;t<M;t++){if(t<b){d[t]=0;for(let e=t;e<r;e++)d[t]=hypotenuse$1(d[t],a.get(e,t));if(0!==d[t]){a.get(t,t)<0&&(d[t]=-d[t]);for(let e=t;e<r;e++)a.set(e,t,a.get(e,t)/d[t]);a.set(t,t,a.get(t,t)+1)}d[t]=-d[t]}for(let e=t+1;e<n;e++){if(t<b&&0!==d[t]){let n=0;for(let o=t;o<r;o++)n+=a.get(o,t)*a.get(o,e);n=-n/a.get(t,t);for(let o=t;o<r;o++)a.set(o,e,a.get(o,e)+n*a.get(o,t))}p[e]=a.get(t,e)}if(l&&t<b)for(let e=t;e<r;e++)m.set(e,t,a.get(e,t));if(t<x){p[t]=0;for(let e=t+1;e<n;e++)p[t]=hypotenuse$1(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<n;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<r&&0!==p[t]){for(let e=t+1;e<r;e++)w[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<n;r++)w[e]+=p[r]*a.get(e,r);for(let e=t+1;e<n;e++){let n=-p[e]/p[t+1];for(let o=t+1;o<r;o++)a.set(o,e,a.get(o,e)+n*w[o])}}if(h)for(let e=t+1;e<n;e++)g.set(e,t,p[e])}}let v=Math.min(n,r+1);if(b<n&&(d[b]=a.get(b,b)),r<v&&(d[v-1]=0),x+1<v&&(p[x]=a.get(x,v-1)),p[v-1]=0,l){for(let t=b;t<u;t++){for(let e=0;e<r;e++)m.set(e,t,0);m.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==d[t]){for(let e=t+1;e<u;e++){let n=0;for(let o=t;o<r;o++)n+=m.get(o,t)*m.get(o,e);n=-n/m.get(t,t);for(let o=t;o<r;o++)m.set(o,e,m.get(o,e)+n*m.get(o,t))}for(let e=t;e<r;e++)m.set(e,t,-m.get(e,t));m.set(t,t,1+m.get(t,t));for(let e=0;e<t-1;e++)m.set(e,t,0)}else{for(let e=0;e<r;e++)m.set(e,t,0);m.set(t,t,1)}}if(h)for(let t=n-1;t>=0;t--){if(t<x&&0!==p[t])for(let e=t+1;e<n;e++){let r=0;for(let o=t+1;o<n;o++)r+=g.get(o,t)*g.get(o,e);r=-r/g.get(t+1,t);for(let o=t+1;o<n;o++)g.set(o,e,g.get(o,e)+r*g.get(o,t))}for(let e=0;e<n;e++)g.set(e,t,0);g.set(t,t,1)}let I=v-1,A=Number.EPSILON;for(;v>0;){let t,e;for(t=v-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+A*Math.abs(d[t]+Math.abs(d[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===v-2)e=4;else{let r;for(r=v-1;r>=t&&r!==t;r--){let e=(r!==v?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(d[r])<=A*e){d[r]=0;break}}r===t?e=3:r===v-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[v-2];p[v-2]=0;for(let r=v-2;r>=t;r--){let o=hypotenuse$1(d[r],e),i=d[r]/o,s=e/o;if(d[r]=o,r!==t&&(e=-s*p[r-1],p[r-1]=i*p[r-1]),h)for(let t=0;t<n;t++)o=i*g.get(t,r)+s*g.get(t,v-1),g.set(t,v-1,-s*g.get(t,r)+i*g.get(t,v-1)),g.set(t,r,o)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let n=t;n<v;n++){let o=hypotenuse$1(d[n],e),i=d[n]/o,s=e/o;if(d[n]=o,e=-s*p[n],p[n]=i*p[n],l)for(let e=0;e<r;e++)o=i*m.get(e,n)+s*m.get(e,t-1),m.set(e,t-1,-s*m.get(e,n)+i*m.get(e,t-1)),m.set(e,n,o)}break}case 3:{const e=Math.max(Math.abs(d[v-1]),Math.abs(d[v-2]),Math.abs(p[v-2]),Math.abs(d[t]),Math.abs(p[t])),o=d[v-1]/e,i=d[v-2]/e,s=p[v-2]/e,a=d[t]/e,c=p[t]/e,u=((i+o)*(i-o)+s*s)/2,f=o*s*(o*s);let w=0;0===u&&0===f||(w=u<0?0-Math.sqrt(u*u+f):Math.sqrt(u*u+f),w=f/(u+w));let y=(a+o)*(a-o)+w,b=a*c;for(let e=t;e<v-1;e++){let o=hypotenuse$1(y,b);0===o&&(o=Number.MIN_VALUE);let i=y/o,s=b/o;if(e!==t&&(p[e-1]=o),y=i*d[e]+s*p[e],p[e]=i*p[e]-s*d[e],b=s*d[e+1],d[e+1]=i*d[e+1],h)for(let t=0;t<n;t++)o=i*g.get(t,e)+s*g.get(t,e+1),g.set(t,e+1,-s*g.get(t,e)+i*g.get(t,e+1)),g.set(t,e,o);if(o=hypotenuse$1(y,b),0===o&&(o=Number.MIN_VALUE),i=y/o,s=b/o,d[e]=o,y=i*p[e]+s*d[e+1],d[e+1]=-s*p[e]+i*d[e+1],b=s*p[e+1],p[e+1]=i*p[e+1],l&&e<r-1)for(let t=0;t<r;t++)o=i*m.get(t,e)+s*m.get(t,e+1),m.set(t,e+1,-s*m.get(t,e)+i*m.get(t,e+1)),m.set(t,e,o)}p[v-2]=y;break}case 4:if(d[t]<=0&&(d[t]=d[t]<0?-d[t]:0,h))for(let e=0;e<=I;e++)g.set(e,t,-g.get(e,t));for(;t<I&&!(d[t]>=d[t+1]);){let e=d[t];if(d[t]=d[t+1],d[t+1]=e,h&&t<n-1)for(let r=0;r<n;r++)e=g.get(r,t+1),g.set(r,t+1,g.get(r,t)),g.set(r,t,e);if(l&&t<r-1)for(let n=0;n<r;n++)e=m.get(n,t+1),m.set(n,t+1,m.get(n,t)),m.set(n,t,e);t++}v--}}if(c){let t=g;g=m,m=t}this.m=r,this.n=n,this.s=d,this.U=m,this.V=g}solve(t){let e=t,r=this.threshold,n=this.s.length,o=Matrix$2.zeros(n,n);for(let t=0;t<n;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let i=this.U,s=this.rightSingularVectors,a=s.mmul(o),l=s.rows,h=i.rows,c=Matrix$2.zeros(l,h);for(let t=0;t<l;t++)for(let e=0;e<h;e++){let r=0;for(let o=0;o<n;o++)r+=a.get(t,o)*i.get(e,o);c.set(t,e,r)}return c.mmul(e)}solveForDiagonal(t){return this.solve(Matrix$2.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,n=t.columns,o=new Matrix$2(r,this.s.length);for(let i=0;i<r;i++)for(let r=0;r<n;r++)Math.abs(this.s[r])>e&&o.set(i,r,t.get(i,r)/this.s[r]);let i=this.U,s=i.rows,a=i.columns,l=new Matrix$2(r,s);for(let t=0;t<r;t++)for(let e=0;e<s;e++){let r=0;for(let n=0;n<a;n++)r+=o.get(t,n)*i.get(e,n);l.set(t,e,r)}return l}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let n=0,o=r.length;n<o;n++)r[n]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return Matrix$2.diag(this.s)}};function inverse$1(t,e=!1){return t=WrapperMatrix2D.checkMatrix(t),e?new SingularValueDecomposition$1(t).inverse():solve(t,Matrix$2.eye(t.rows))}function solve(t,e,r=!1){return t=WrapperMatrix2D.checkMatrix(t),e=WrapperMatrix2D.checkMatrix(e),r?new SingularValueDecomposition$1(t).solve(e):t.isSquare()?new LuDecomposition(t).solve(e):new QrDecomposition(t).solve(e)}function determinant$1(t){if((t=Matrix$2.checkMatrix(t)).isSquare()){if(0===t.columns)return 1;let e,r,n,o;if(2===t.columns)return e=t.get(0,0),r=t.get(0,1),n=t.get(1,0),o=t.get(1,1),e*o-r*n;if(3===t.columns){let o,i,s;return o=new MatrixSelectionView(t,[1,2],[1,2]),i=new MatrixSelectionView(t,[1,2],[0,2]),s=new MatrixSelectionView(t,[1,2],[0,1]),e=t.get(0,0),r=t.get(0,1),n=t.get(0,2),e*determinant$1(o)-r*determinant$1(i)+n*determinant$1(s)}return new LuDecomposition(t).determinant}throw Error("determinant can only be calculated for a square matrix")}function xrange(t,e){let r=[];for(let n=0;n<t;n++)n!==e&&r.push(n);return r}function dependenciesOneRow(t,e,r,n=1e-9,o=1e-9){if(t>o)return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<n&&t.set(e,0,0);return t.to1DArray()}}function linearDependencies(t,e={}){const{thresholdValue:r=1e-9,thresholdError:n=1e-9}=e;let o=(t=Matrix$2.checkMatrix(t)).rows,i=new Matrix$2(o,o);for(let e=0;e<o;e++){let s=Matrix$2.columnVector(t.getRow(e)),a=t.subMatrixRow(xrange(o,e)).transpose(),l=new SingularValueDecomposition$1(a).solve(s),h=Matrix$2.sub(s,a.mmul(l)).abs().max();i.setRow(e,dependenciesOneRow(h,l,e,r,n))}return i}function pseudoInverse$1(t,e=Number.EPSILON){if((t=Matrix$2.checkMatrix(t)).isEmpty())return t.transpose();let r=new SingularValueDecomposition$1(t,{autoTranspose:!0}),n=r.leftSingularVectors,o=r.rightSingularVectors,i=r.diagonal;for(let t=0;t<i.length;t++)Math.abs(i[t])>e?i[t]=1/i[t]:i[t]=0;return o.mmul(Matrix$2.diag(i).mmul(n.transpose()))}function covariance(t,e=t,r={}){t=new Matrix$2(t);let n=!1;if("object"!=typeof e||Matrix$2.isMatrix(e)||isAnyArray.isAnyArray(e)?e=new Matrix$2(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0}=r;o&&(t=t.center("column"),n||(e=e.center("column")));const i=t.transpose().mmul(e);for(let e=0;e<i.rows;e++)for(let r=0;r<i.columns;r++)i.set(e,r,i.get(e,r)*(1/(t.rows-1)));return i}function correlation(t,e=t,r={}){t=new Matrix$2(t);let n=!1;if("object"!=typeof e||Matrix$2.isMatrix(e)||isAnyArray.isAnyArray(e)?e=new Matrix$2(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0,scale:i=!0}=r;o&&(t.center("column"),n||e.center("column")),i&&(t.scale("column"),n||e.scale("column"));const s=t.standardDeviation("column",{unbiased:!0}),a=n?s:e.standardDeviation("column",{unbiased:!0}),l=t.transpose().mmul(e);for(let e=0;e<l.rows;e++)for(let r=0;r<l.columns;r++)l.set(e,r,l.get(e,r)*(1/(s[e]*a[r]))*(1/(t.rows-1)));return l}let EigenvalueDecomposition$1=class{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=WrapperMatrix2D.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let n,o,i=t.columns,s=new Matrix$2(i,i),a=new Float64Array(i),l=new Float64Array(i),h=t,c=!1;if(c=!!r||t.isSymmetric(),c){for(n=0;n<i;n++)for(o=0;o<i;o++)s.set(n,o,h.get(n,o));tred2(i,l,a,s),tql2(i,l,a,s)}else{let t=new Matrix$2(i,i),e=new Float64Array(i);for(o=0;o<i;o++)for(n=0;n<i;n++)t.set(n,o,h.get(n,o));orthes(i,t,e,s),hqr2(i,l,a,s,t)}this.n=i,this.e=l,this.d=a,this.V=s}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,n=this.e,o=this.d,i=new Matrix$2(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)i.set(t,e,0);i.set(t,t,o[t]),n[t]>0?i.set(t,t+1,n[t]):n[t]<0&&i.set(t,t-1,n[t])}return i}};function tred2(t,e,r,n){let o,i,s,a,l,h,c,u;for(l=0;l<t;l++)r[l]=n.get(t-1,l);for(a=t-1;a>0;a--){for(u=0,s=0,h=0;h<a;h++)u+=Math.abs(r[h]);if(0===u)for(e[a]=r[a-1],l=0;l<a;l++)r[l]=n.get(a-1,l),n.set(a,l,0),n.set(l,a,0);else{for(h=0;h<a;h++)r[h]/=u,s+=r[h]*r[h];for(o=r[a-1],i=Math.sqrt(s),o>0&&(i=-i),e[a]=u*i,s-=o*i,r[a-1]=o-i,l=0;l<a;l++)e[l]=0;for(l=0;l<a;l++){for(o=r[l],n.set(l,a,o),i=e[l]+n.get(l,l)*o,h=l+1;h<=a-1;h++)i+=n.get(h,l)*r[h],e[h]+=n.get(h,l)*o;e[l]=i}for(o=0,l=0;l<a;l++)e[l]/=s,o+=e[l]*r[l];for(c=o/(s+s),l=0;l<a;l++)e[l]-=c*r[l];for(l=0;l<a;l++){for(o=r[l],i=e[l],h=l;h<=a-1;h++)n.set(h,l,n.get(h,l)-(o*e[h]+i*r[h]));r[l]=n.get(a-1,l),n.set(a,l,0)}}r[a]=s}for(a=0;a<t-1;a++){if(n.set(t-1,a,n.get(a,a)),n.set(a,a,1),s=r[a+1],0!==s){for(h=0;h<=a;h++)r[h]=n.get(h,a+1)/s;for(l=0;l<=a;l++){for(i=0,h=0;h<=a;h++)i+=n.get(h,a+1)*n.get(h,l);for(h=0;h<=a;h++)n.set(h,l,n.get(h,l)-i*r[h])}}for(h=0;h<=a;h++)n.set(h,a+1,0)}for(l=0;l<t;l++)r[l]=n.get(t-1,l),n.set(t-1,l,0);n.set(t-1,t-1,1),e[0]=0}function tql2(t,e,r,n){let o,i,s,a,l,h,c,u,f,d,m,g,p,w,y,b;for(s=1;s<t;s++)e[s-1]=e[s];e[t-1]=0;let x=0,M=0,v=Number.EPSILON;for(h=0;h<t;h++){for(M=Math.max(M,Math.abs(r[h])+Math.abs(e[h])),c=h;c<t&&!(Math.abs(e[c])<=v*M);)c++;if(c>h)do{for(o=r[h],u=(r[h+1]-o)/(2*e[h]),f=hypotenuse$1(u,1),u<0&&(f=-f),r[h]=e[h]/(u+f),r[h+1]=e[h]*(u+f),d=r[h+1],i=o-r[h],s=h+2;s<t;s++)r[s]-=i;for(x+=i,u=r[c],m=1,g=m,p=m,w=e[h+1],y=0,b=0,s=c-1;s>=h;s--)for(p=g,g=m,b=y,o=m*e[s],i=m*u,f=hypotenuse$1(u,e[s]),e[s+1]=y*f,y=e[s]/f,m=u/f,u=m*r[s]-y*o,r[s+1]=i+y*(m*o+y*r[s]),l=0;l<t;l++)i=n.get(l,s+1),n.set(l,s+1,y*n.get(l,s)+m*i),n.set(l,s,m*n.get(l,s)-y*i);u=-y*b*p*w*e[h]/d,e[h]=y*u,r[h]=m*u}while(Math.abs(e[h])>v*M);r[h]=r[h]+x,e[h]=0}for(s=0;s<t-1;s++){for(l=s,u=r[s],a=s+1;a<t;a++)r[a]<u&&(l=a,u=r[a]);if(l!==s)for(r[l]=r[s],r[s]=u,a=0;a<t;a++)u=n.get(a,s),n.set(a,s,n.get(a,l)),n.set(a,l,u)}}function orthes(t,e,r,n){let o,i,s,a,l,h,c,u=t-1;for(h=1;h<=u-1;h++){for(c=0,a=h;a<=u;a++)c+=Math.abs(e.get(a,h-1));if(0!==c){for(s=0,a=u;a>=h;a--)r[a]=e.get(a,h-1)/c,s+=r[a]*r[a];for(i=Math.sqrt(s),r[h]>0&&(i=-i),s-=r[h]*i,r[h]=r[h]-i,l=h;l<t;l++){for(o=0,a=u;a>=h;a--)o+=r[a]*e.get(a,l);for(o/=s,a=h;a<=u;a++)e.set(a,l,e.get(a,l)-o*r[a])}for(a=0;a<=u;a++){for(o=0,l=u;l>=h;l--)o+=r[l]*e.get(a,l);for(o/=s,l=h;l<=u;l++)e.set(a,l,e.get(a,l)-o*r[l])}r[h]=c*r[h],e.set(h,h-1,c*i)}}for(a=0;a<t;a++)for(l=0;l<t;l++)n.set(a,l,a===l?1:0);for(h=u-1;h>=1;h--)if(0!==e.get(h,h-1)){for(a=h+1;a<=u;a++)r[a]=e.get(a,h-1);for(l=h;l<=u;l++){for(i=0,a=h;a<=u;a++)i+=r[a]*n.get(a,l);for(i=i/r[h]/e.get(h,h-1),a=h;a<=u;a++)n.set(a,l,n.get(a,l)+i*r[a])}}}function hqr2(t,e,r,n,o){let i,s,a,l,h,c,u,f,d,m,g,p,w,y,b,x=t-1,M=t-1,v=Number.EPSILON,I=0,A=0,k=0,R=0,E=0,$=0,P=0,S=0;for(i=0;i<t;i++)for((i<0||i>M)&&(r[i]=o.get(i,i),e[i]=0),s=Math.max(i-1,0);s<t;s++)A+=Math.abs(o.get(i,s));for(;x>=0;){for(l=x;l>0&&($=Math.abs(o.get(l-1,l-1))+Math.abs(o.get(l,l)),0===$&&($=A),!(Math.abs(o.get(l,l-1))<v*$));)l--;if(l===x)o.set(x,x,o.get(x,x)+I),r[x]=o.get(x,x),e[x]=0,x--,S=0;else if(l===x-1){if(u=o.get(x,x-1)*o.get(x-1,x),k=(o.get(x-1,x-1)-o.get(x,x))/2,R=k*k+u,P=Math.sqrt(Math.abs(R)),o.set(x,x,o.get(x,x)+I),o.set(x-1,x-1,o.get(x-1,x-1)+I),f=o.get(x,x),R>=0){for(P=k>=0?k+P:k-P,r[x-1]=f+P,r[x]=r[x-1],0!==P&&(r[x]=f-u/P),e[x-1]=0,e[x]=0,f=o.get(x,x-1),$=Math.abs(f)+Math.abs(P),k=f/$,R=P/$,E=Math.sqrt(k*k+R*R),k/=E,R/=E,s=x-1;s<t;s++)P=o.get(x-1,s),o.set(x-1,s,R*P+k*o.get(x,s)),o.set(x,s,R*o.get(x,s)-k*P);for(i=0;i<=x;i++)P=o.get(i,x-1),o.set(i,x-1,R*P+k*o.get(i,x)),o.set(i,x,R*o.get(i,x)-k*P);for(i=0;i<=M;i++)P=n.get(i,x-1),n.set(i,x-1,R*P+k*n.get(i,x)),n.set(i,x,R*n.get(i,x)-k*P)}else r[x-1]=f+k,r[x]=f+k,e[x-1]=P,e[x]=-P;x-=2,S=0}else{if(f=o.get(x,x),d=0,u=0,l<x&&(d=o.get(x-1,x-1),u=o.get(x,x-1)*o.get(x-1,x)),10===S){for(I+=f,i=0;i<=x;i++)o.set(i,i,o.get(i,i)-f);$=Math.abs(o.get(x,x-1))+Math.abs(o.get(x-1,x-2)),f=d=.75*$,u=-.4375*$*$}if(30===S&&($=(d-f)/2,$=$*$+u,$>0)){for($=Math.sqrt($),d<f&&($=-$),$=f-u/((d-f)/2+$),i=0;i<=x;i++)o.set(i,i,o.get(i,i)-$);I+=$,f=d=u=.964}for(S+=1,h=x-2;h>=l&&(P=o.get(h,h),E=f-P,$=d-P,k=(E*$-u)/o.get(h+1,h)+o.get(h,h+1),R=o.get(h+1,h+1)-P-E-$,E=o.get(h+2,h+1),$=Math.abs(k)+Math.abs(R)+Math.abs(E),k/=$,R/=$,E/=$,h!==l)&&!(Math.abs(o.get(h,h-1))*(Math.abs(R)+Math.abs(E))<v*(Math.abs(k)*(Math.abs(o.get(h-1,h-1))+Math.abs(P)+Math.abs(o.get(h+1,h+1)))));)h--;for(i=h+2;i<=x;i++)o.set(i,i-2,0),i>h+2&&o.set(i,i-3,0);for(a=h;a<=x-1&&(y=a!==x-1,a!==h&&(k=o.get(a,a-1),R=o.get(a+1,a-1),E=y?o.get(a+2,a-1):0,f=Math.abs(k)+Math.abs(R)+Math.abs(E),0!==f&&(k/=f,R/=f,E/=f)),0!==f);a++)if($=Math.sqrt(k*k+R*R+E*E),k<0&&($=-$),0!==$){for(a!==h?o.set(a,a-1,-$*f):l!==h&&o.set(a,a-1,-o.get(a,a-1)),k+=$,f=k/$,d=R/$,P=E/$,R/=k,E/=k,s=a;s<t;s++)k=o.get(a,s)+R*o.get(a+1,s),y&&(k+=E*o.get(a+2,s),o.set(a+2,s,o.get(a+2,s)-k*P)),o.set(a,s,o.get(a,s)-k*f),o.set(a+1,s,o.get(a+1,s)-k*d);for(i=0;i<=Math.min(x,a+3);i++)k=f*o.get(i,a)+d*o.get(i,a+1),y&&(k+=P*o.get(i,a+2),o.set(i,a+2,o.get(i,a+2)-k*E)),o.set(i,a,o.get(i,a)-k),o.set(i,a+1,o.get(i,a+1)-k*R);for(i=0;i<=M;i++)k=f*n.get(i,a)+d*n.get(i,a+1),y&&(k+=P*n.get(i,a+2),n.set(i,a+2,n.get(i,a+2)-k*E)),n.set(i,a,n.get(i,a)-k),n.set(i,a+1,n.get(i,a+1)-k*R)}}}if(0!==A){for(x=t-1;x>=0;x--)if(k=r[x],R=e[x],0===R)for(l=x,o.set(x,x,1),i=x-1;i>=0;i--){for(u=o.get(i,i)-k,E=0,s=l;s<=x;s++)E+=o.get(i,s)*o.get(s,x);if(e[i]<0)P=u,$=E;else if(l=i,0===e[i]?o.set(i,x,0!==u?-E/u:-E/(v*A)):(f=o.get(i,i+1),d=o.get(i+1,i),R=(r[i]-k)*(r[i]-k)+e[i]*e[i],c=(f*$-P*E)/R,o.set(i,x,c),o.set(i+1,x,Math.abs(f)>Math.abs(P)?(-E-u*c)/f:(-$-d*c)/P)),c=Math.abs(o.get(i,x)),v*c*c>1)for(s=i;s<=x;s++)o.set(s,x,o.get(s,x)/c)}else if(R<0)for(l=x-1,Math.abs(o.get(x,x-1))>Math.abs(o.get(x-1,x))?(o.set(x-1,x-1,R/o.get(x,x-1)),o.set(x-1,x,-(o.get(x,x)-k)/o.get(x,x-1))):(b=cdiv(0,-o.get(x-1,x),o.get(x-1,x-1)-k,R),o.set(x-1,x-1,b[0]),o.set(x-1,x,b[1])),o.set(x,x-1,0),o.set(x,x,1),i=x-2;i>=0;i--){for(m=0,g=0,s=l;s<=x;s++)m+=o.get(i,s)*o.get(s,x-1),g+=o.get(i,s)*o.get(s,x);if(u=o.get(i,i)-k,e[i]<0)P=u,E=m,$=g;else if(l=i,0===e[i]?(b=cdiv(-m,-g,u,R),o.set(i,x-1,b[0]),o.set(i,x,b[1])):(f=o.get(i,i+1),d=o.get(i+1,i),p=(r[i]-k)*(r[i]-k)+e[i]*e[i]-R*R,w=2*(r[i]-k)*R,0===p&&0===w&&(p=v*A*(Math.abs(u)+Math.abs(R)+Math.abs(f)+Math.abs(d)+Math.abs(P))),b=cdiv(f*E-P*m+R*g,f*$-P*g-R*m,p,w),o.set(i,x-1,b[0]),o.set(i,x,b[1]),Math.abs(f)>Math.abs(P)+Math.abs(R)?(o.set(i+1,x-1,(-m-u*o.get(i,x-1)+R*o.get(i,x))/f),o.set(i+1,x,(-g-u*o.get(i,x)-R*o.get(i,x-1))/f)):(b=cdiv(-E-d*o.get(i,x-1),-$-d*o.get(i,x),P,R),o.set(i+1,x-1,b[0]),o.set(i+1,x,b[1]))),c=Math.max(Math.abs(o.get(i,x-1)),Math.abs(o.get(i,x))),v*c*c>1)for(s=i;s<=x;s++)o.set(s,x-1,o.get(s,x-1)/c),o.set(s,x,o.get(s,x)/c)}for(i=0;i<t;i++)if(i<0||i>M)for(s=i;s<t;s++)n.set(i,s,o.get(i,s));for(s=t-1;s>=0;s--)for(i=0;i<=M;i++){for(P=0,a=0;a<=Math.min(s,M);a++)P+=n.get(i,a)*o.get(a,s);n.set(i,s,P)}}}function cdiv(t,e,r,n){let o,i;return Math.abs(r)>Math.abs(n)?(o=n/r,i=r+o*n,[(t+o*e)/i,(e-o*t)/i]):(o=r/n,i=n+o*r,[(o*t+e)/i,(o*e-t)/i])}class CholeskyDecomposition{constructor(t){if(!(t=WrapperMatrix2D.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,n,o=t,i=o.rows,s=new Matrix$2(i,i),a=!0;for(r=0;r<i;r++){let t=0;for(n=0;n<r;n++){let i=0;for(e=0;e<n;e++)i+=s.get(n,e)*s.get(r,e);i=(o.get(r,n)-i)/s.get(n,n),s.set(r,n,i),t+=i*i}for(t=o.get(r,r)-t,a&&=t>0,s.set(r,r,Math.sqrt(Math.max(t,0))),n=r+1;n<i;n++)s.set(r,n,0)}this.L=s,this.positiveDefinite=a}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=WrapperMatrix2D.checkMatrix(t);let e=this.L,r=e.rows;if(t.rows!==r)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let n,o,i,s=t.columns,a=t.clone();for(i=0;i<r;i++)for(o=0;o<s;o++){for(n=0;n<i;n++)a.set(i,o,a.get(i,o)-a.get(n,o)*e.get(i,n));a.set(i,o,a.get(i,o)/e.get(i,i))}for(i=r-1;i>=0;i--)for(o=0;o<s;o++){for(n=i+1;n<r;n++)a.set(i,o,a.get(i,o)-a.get(n,o)*e.get(n,i));a.set(i,o,a.get(i,o)/e.get(i,i))}return a}get lowerTriangularMatrix(){return this.L}}class nipals{constructor(t,e={}){t=WrapperMatrix2D.checkMatrix(t);let{Y:r}=e;const{scaleScores:n=!1,maxIterations:o=1e3,terminationCriteria:i=1e-10}=e;let s;if(r){if(r=isAnyArray.isAnyArray(r)&&"number"==typeof r[0]?Matrix$2.columnVector(r):WrapperMatrix2D.checkMatrix(r),r.rows!==t.rows)throw new Error("Y should have the same number of rows as X");s=r.getColumnVector(0)}else s=t.getColumnVector(0);let a,l,h,c,u=1;for(let e=0;e<o&&u>i;e++)h=t.transpose().mmul(s).div(s.transpose().mmul(s).get(0,0)),h=h.div(h.norm()),a=t.mmul(h).div(h.transpose().mmul(h).get(0,0)),e>0&&(u=a.clone().sub(c).pow(2).sum()),c=a.clone(),r?(l=r.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),l=l.div(l.norm()),s=r.mmul(l).div(l.transpose().mmul(l).get(0,0))):s=a;if(r){let e=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0));e=e.div(e.norm());let n=t.clone().sub(a.clone().mmul(e.transpose())),o=s.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),i=r.clone().sub(a.clone().mulS(o.get(0,0)).mmul(l.transpose()));this.t=a,this.p=e.transpose(),this.w=h.transpose(),this.q=l,this.u=s,this.s=a.transpose().mmul(a),this.xResidual=n,this.yResidual=i,this.betas=o}else this.w=h.transpose(),this.s=a.transpose().mmul(a).sqrt(),this.t=n?a.clone().div(this.s.get(0,0)):a,this.xResidual=t.sub(a.mmul(h.transpose()))}}matrix.AbstractMatrix=AbstractMatrix,matrix.CHO=CholeskyDecomposition,matrix.CholeskyDecomposition=CholeskyDecomposition,matrix.DistanceMatrix=DistanceMatrix,matrix.EVD=EigenvalueDecomposition$1;var EigenvalueDecomposition_1=matrix.EigenvalueDecomposition=EigenvalueDecomposition$1;matrix.LU=LuDecomposition,matrix.LuDecomposition=LuDecomposition;var Matrix_1=matrix.Matrix=Matrix$2;matrix.MatrixColumnSelectionView=MatrixColumnSelectionView,matrix.MatrixColumnView=MatrixColumnView,matrix.MatrixFlipColumnView=MatrixFlipColumnView,matrix.MatrixFlipRowView=MatrixFlipRowView,matrix.MatrixRowSelectionView=MatrixRowSelectionView,matrix.MatrixRowView=MatrixRowView,matrix.MatrixSelectionView=MatrixSelectionView,matrix.MatrixSubView=MatrixSubView,matrix.MatrixTransposeView=MatrixTransposeView,matrix.NIPALS=nipals,matrix.Nipals=nipals,matrix.QR=QrDecomposition,matrix.QrDecomposition=QrDecomposition;var SVD$1=matrix.SVD=SingularValueDecomposition$1,SingularValueDecomposition_1=matrix.SingularValueDecomposition=SingularValueDecomposition$1;matrix.SymmetricMatrix=SymmetricMatrix;var WrapperMatrix1D_1=matrix.WrapperMatrix1D=WrapperMatrix1D$1;matrix.WrapperMatrix2D=WrapperMatrix2D,matrix.correlation=correlation,matrix.covariance=covariance;var _default=matrix.default=Matrix$2,determinant_1=matrix.determinant=determinant$1,inverse_1=matrix.inverse=inverse$1;matrix.linearDependencies=linearDependencies;var pseudoInverse_1=matrix.pseudoInverse=pseudoInverse$1;matrix.solve=solve,matrix.wrap=wrap;const EigenvalueDecomposition=EigenvalueDecomposition_1,Matrix=Matrix_1,SVD=SVD$1,SingularValueDecomposition=SingularValueDecomposition_1,WrapperMatrix1D=WrapperMatrix1D_1;var Matrix$1=_default.Matrix?_default.Matrix:Matrix_1;const determinant=determinant_1,inverse=inverse_1,pseudoInverse=pseudoInverse_1;function getCentroid(t){let e=new Matrix$1(3,1);const r=t.columns;let n=0,o=0;for(let e=0;e<r;e++)n+=t.get(0,e),o+=t.get(1,e);return e.set(0,0,n/r),e.set(1,0,o/r),e}function getAffineTransform$1(t,e){if(t.columns!==e.columns)throw new Error("Source and destination matrices should have same dimensions (same number of points).");if(t.columns<2)throw new Error("Matrices should contain at least two points for the algorithm to run properly.");const r=getCentroid(t),n=getCentroid(e),o=t.clone().subColumnVector(r),i=e.clone().subColumnVector(n);let s=0;const a=t.columns;for(let t=0;t<a;t++){const e=o.getColumn(t);s+=getDistanceToOrigin(i.getColumn(t))/getDistanceToOrigin(e)}const l=s/a,h=Matrix$1.mul(o,l).mmul(i.transpose()),c=new SingularValueDecomposition(h),u=c.leftSingularVectors,f=c.rightSingularVectors;let d=f.mmul(u.transpose());if(determinant(d)<0){d=f.mulColumn(2,-1).mmul(u.transpose())}let m=180*Math.atan2(d.get(1,0),d.get(0,0))/Math.PI;-180===m&&(m=180);const g=Matrix$1.sub(n,Matrix$1.mul(d.mmul(r),l));return{translation:{x:g.get(0,0),y:g.get(1,0)},rotation:m,scale:l}}function getDistanceToOrigin(t){return Math.hypot(t[0],t[1])}var medianQuickselect_min={exports:{}};!function(t){!function(){function e(t){for(var e=0,o=t.length-1,i=void 0,s=void 0,a=void 0,l=n(e,o);;){if(o<=e)return t[l];if(o==e+1)return t[e]>t[o]&&r(t,e,o),t[l];for(t[i=n(e,o)]>t[o]&&r(t,i,o),t[e]>t[o]&&r(t,e,o),t[i]>t[e]&&r(t,i,e),r(t,i,e+1),s=e+1,a=o;;){do{s++}while(t[e]>t[s]);do{a--}while(t[a]>t[e]);if(a<s)break;r(t,s,a)}r(t,e,a),a<=l&&(e=s),a>=l&&(o=a-1)}}var r=function(t,e,r){var n;return n=[t[r],t[e]],t[e]=n[0],t[r]=n[1],n},n=function(t,e){return~~((t+e)/2)};t.exports?t.exports=e:window.median=e}()}(medianQuickselect_min);var medianQuickselect_minExports=medianQuickselect_min.exports,quickMedian=getDefaultExportFromCjs(medianQuickselect_minExports);function median$1(t){if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");return quickMedian(t.slice())}const LOOP$1=8,FLOAT_MUL$1=1/16777216,sh1$1=15,sh2$1=18,sh3$1=11;function multiply_uint32(t,e){const r=65535&(t>>>=0);return((t-r)*(e>>>=0)>>>0)+r*e>>>0}let XSadd$1=class{constructor(t=Date.now()){this.state=new Uint32Array(4),this.init(t),this.random=this.getFloat.bind(this)}getUint32(){return this.nextState(),this.state[3]+this.state[2]>>>0}getFloat(){return(this.getUint32()>>>8)*FLOAT_MUL$1}init(t){if(!Number.isInteger(t))throw new TypeError("seed must be an integer");this.state[0]=t,this.state[1]=0,this.state[2]=0,this.state[3]=0;for(let t=1;t<LOOP$1;t++)this.state[3&t]^=t+multiply_uint32(1812433253,this.state[t-1&3]^this.state[t-1&3]>>>30>>>0)>>>0;this.periodCertification();for(let t=0;t<LOOP$1;t++)this.nextState()}periodCertification(){0===this.state[0]&&0===this.state[1]&&0===this.state[2]&&0===this.state[3]&&(this.state[0]=88,this.state[1]=83,this.state[2]=65,this.state[3]=68)}nextState(){let t=this.state[0];t^=t<<sh1$1,t^=t>>>sh2$1,t^=this.state[3]<<sh3$1,this.state[0]=this.state[1],this.state[1]=this.state[2],this.state[2]=this.state[3],this.state[3]=t}};const PROB_TOLERANCE=1e-8;function randomChoice(t,e={},r=Math.random){const{size:n=1,replace:o=!1,probabilities:i}=e;let s,a;if(s="number"==typeof t?getArray(t):t.slice(),i){if(!o)throw new Error("choice with probabilities and no replacement is not implemented");if(i.length!==s.length)throw new Error("the length of probabilities option should be equal to the number of choices");a=[i[0]];for(let t=1;t<i.length;t++)a[t]=a[t-1]+i[t];if(Math.abs(1-a[a.length-1])>PROB_TOLERANCE)throw new Error(`probabilities should sum to 1, but instead sums to ${a[a.length-1]}`)}if(!1===o&&n>s.length)throw new Error("size option is too large");const l=[];for(let t=0;t<n;t++){const t=randomIndex(s.length,r,a);l.push(s[t]),o||s.splice(t,1)}return l}function getArray(t){const e=[];for(let r=0;r<t;r++)e.push(r);return e}function randomIndex(t,e,r){const n=e();if(r){let t=0;for(;n>r[t];)t++;return t}return Math.floor(n*t)}class Random{constructor(t=Math.random){if("number"==typeof t){const e=new XSadd$1(t);this.randomGenerator=e.random}else this.randomGenerator=t}choice(t,e){return randomChoice(t,e,this.randomGenerator)}random(){return this.randomGenerator()}randInt(t,e){return void 0===e&&(e=t,t=0),t+Math.floor(this.randomGenerator()*(e-t))}randomSample(t){const e=[];for(let r=0;r<t;r++)e.push(this.random());return e}}function getNbIterations(t,e,r){const n=Math.log10(1-t)/Math.log10(1-(1-e)**r);return Math.ceil(n)}function ransac(t,e,r){let n=0;n=r.minNbInliers?getNbValues(r.minNbInliers,t.length):t.length;const{sampleSize:o=2,threshold:i=1,fitFunction:s,distanceFunction:a,modelFunction:l,seed:h}=r;if(t.length!==e.length)throw new Error("source and destination data should have the same length");let c;if("outliersFraction"in r){const{stopProbabilty:t=.99}=r;c=getNbIterations(t,r.outliersFraction,o)}else c=r.maxNbIterations?r.maxNbIterations:100;let u=0,f=0,d=[],m=[],g=0,p=[];for(void 0!==h&&(p=new Random(h).choice(c,{size:c}));u<c;){let r;u++,r=void 0!==h?new Random(p[u]).choice(t.length,{size:o}):(new Random).choice(t.length,{size:o});const c=[],w=[];for(let n of r)c.push(t[n]),w.push(e[n]);const y=s(c,w),b=l(y);let x=[];for(let e of t)x.push(b(e));let M=0,v=[],I=[],A=0;for(let t=0;t<e.length;t++){if(r.includes(t)){M++,v.push(t);continue}const n=a(e[t],x[t]);I.push(n),n<i&&(M++,v.push(t))}if(A=median$1(I),M>f&&(f=M,d=v,m=y,g=A,M>=n))return{modelParameters:y,inliers:v,nbIterations:u,error:A}}return{modelParameters:m,inliers:d,nbIterations:c,error:g}}function getNbValues(t,e){return Number.isInteger(t)?t:Math.ceil(t*e)}function getDistanceMatrix(t){const e=t.length,r=new Array(e);for(let t=0;t<e;t++)r[t]=new Float64Array(e).fill(0);for(let n=0;n<e;n++){const o=t[n].origin;for(let i=n;i<e;i++){n===i&&(r[n][i]=0);const e=t[i].origin,s=(o.row-e.row)**2+(o.column-e.column)**2;r[n][i]=s,r[i][n]=s}}return r}function getKeypointsInRadius(t,e){const r=t.length,n=getDistanceMatrix(t),o=e**2,i=[];for(let t=0;t<r;t++){const e=[];for(let i=0;i<r;i++)t!==i&&n[t][i]<=o&&e.push(i);i.push(e)}return i}function getBestKeypointsInRadius(t,e=5){const r=t.length,n=getKeypointsInRadius(t,e),o=new Uint8Array(r).fill(0);for(let e=0;e<r;e++){const r=t[e];for(const i of n[e]){const n=t[i];r.score<n.score&&(o[e]=1)}}const i=[];for(let e=0;e<r;e++)o[e]||i.push(t[e]);return i}function difference(t,e){return{column:t.column-e.column,row:t.row-e.row}}function sum$1(t,e){return{column:t.column+e.column,row:t.row+e.row}}function normalize(t){const e=Math.hypot(t.column,t.row);return{column:t.column/e,row:t.row/e}}function rotate$1(t,e){const r=[],n=Math.cos(t),o=Math.sin(t);for(const t of e)r.push({column:n*t.column-o*t.row,row:o*t.column+n*t.row});return r}function sortByColumnRow(t){const e=t.slice();return e.sort((t,e)=>t.column<e.column?-1:t.column>e.column?1:t.row-e.row),e}function getAngle(t,e){const r=normalize(difference(e,t)),n=Math.acos(r.column);return r.row<0?-n:n}function getClockwiseAngle(t,e){const r=normalize(difference(e,t)),n=-Math.atan(r.row/r.column);return r.column<0?r.row<0?n-Math.PI:n+Math.PI:n}function toDegrees(t){return 180*t/Math.PI}function getRadius(t){if(t%2!=1||t<0)throw new RangeError("size must be positive and odd");return(t-1)/2}function checkBorderDistance(t,e,r){return e.column>=r&&e.row>=r&&t.width-e.column>r&&t.height-e.row>r}const t$2=Symbol.for("@ts-pattern/matcher"),e$2=Symbol.for("@ts-pattern/isVariadic"),n$2="@ts-pattern/anonymous-select-key",r$2=t=>Boolean(t&&"object"==typeof t),i$3=t=>t&&!!t[t$2],o$2=(t,e,r)=>{if(i$3(t)){const n=t[t$2](),{matched:o,selections:i}=n.match(e);return o&&i&&Object.keys(i).forEach(t=>r(t,i[t])),o}if(r$2(t)){if(!r$2(e))return!1;if(Array.isArray(t)){if(!Array.isArray(e))return!1;let n=[],o=[],i=[];for(const e of t.keys()){const r=t[e];i$3(r)&&r[e$2]?i.push(r):i.length?o.push(r):n.push(r)}if(i.length){if(i.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(e.length<n.length+o.length)return!1;const t=e.slice(0,n.length),s=0===o.length?[]:e.slice(-o.length),a=e.slice(n.length,0===o.length?1/0:-o.length);return n.every((e,n)=>o$2(e,t[n],r))&&o.every((t,e)=>o$2(t,s[e],r))&&(0===i.length||o$2(i[0],a,r))}return t.length===e.length&&t.every((t,n)=>o$2(t,e[n],r))}return Reflect.ownKeys(t).every(n=>{const o=t[n];return(n in e||i$3(i=o)&&"optional"===i[t$2]().matcherType)&&o$2(o,e[n],r);var i})}return Object.is(e,t)},s$1=t=>{var e,r,n;return r$2(t)?i$3(t)?null!=(e=null==(r=(n=t[t$2]()).getSelectionKeys)?void 0:r.call(n))?e:[]:Array.isArray(t)?c$1(t,s$1):c$1(Object.values(t),s$1):[]},c$1=(t,e)=>t.reduce((t,r)=>t.concat(e(r)),[]);function u$2(...t){if(1===t.length){const[e]=t;return t=>o$2(e,t,()=>{})}if(2===t.length){const[e,r]=t;return o$2(e,r,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}function a$2(t){return Object.assign(t,{optional:()=>h$2(t),and:e=>d$1(t,e),or:e=>y$1(t,e),select:e=>void 0===e?v$1(t):v$1(e,t)})}function l$2(t){return Object.assign((t=>Object.assign(t,{[Symbol.iterator](){let e=0;const r=[{value:Object.assign(t,{[e$2]:!0}),done:!1},{done:!0,value:void 0}];return{next:()=>{var t;return null!=(t=r[e++])?t:r.at(-1)}}}}))(t),{optional:()=>l$2(h$2(t)),select:e=>l$2(void 0===e?v$1(t):v$1(e,t))})}function h$2(t){return a$2({[t$2]:()=>({match:e=>{let r={};const n=(t,e)=>{r[t]=e};return void 0===e?(s$1(t).forEach(t=>n(t,void 0)),{matched:!0,selections:r}):{matched:o$2(t,e,n),selections:r}},getSelectionKeys:()=>s$1(t),matcherType:"optional"})})}const f$1=(t,e)=>{for(const r of t)if(!e(r))return!1;return!0},g$1=(t,e)=>{for(const[r,n]of t.entries())if(!e(n,r))return!1;return!0},m$1=(t,e)=>{const r=Reflect.ownKeys(t);for(const n of r)if(!e(n,t[n]))return!1;return!0};function d$1(...t){return a$2({[t$2]:()=>({match:e=>{let r={};const n=(t,e)=>{r[t]=e};return{matched:t.every(t=>o$2(t,e,n)),selections:r}},getSelectionKeys:()=>c$1(t,s$1),matcherType:"and"})})}function y$1(...t){return a$2({[t$2]:()=>({match:e=>{let r={};const n=(t,e)=>{r[t]=e};return c$1(t,s$1).forEach(t=>n(t,void 0)),{matched:t.some(t=>o$2(t,e,n)),selections:r}},getSelectionKeys:()=>c$1(t,s$1),matcherType:"or"})})}function p$2(t){return{[t$2]:()=>({match:e=>({matched:Boolean(t(e))})})}}function v$1(...t){const e="string"==typeof t[0]?t[0]:void 0,r=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return a$2({[t$2]:()=>({match:t=>{let n={[null!=e?e:n$2]:t};return{matched:void 0===r||o$2(r,t,(t,e)=>{n[t]=e}),selections:n}},getSelectionKeys:()=>[null!=e?e:n$2].concat(void 0===r?[]:s$1(r))})})}function b$2(t){return!0}function w$2(t){return"number"==typeof t}function S$1(t){return"string"==typeof t}function j$1(t){return"bigint"==typeof t}const K=a$2(p$2(b$2)),O=a$2(p$2(b$2)),E=K,x$2=t=>Object.assign(a$2(t),{startsWith:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&t.startsWith(r)))));var r},endsWith:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&t.endsWith(r)))));var r},minLength:e=>x$2(d$1(t,(t=>p$2(e=>S$1(e)&&e.length>=t))(e))),length:e=>x$2(d$1(t,(t=>p$2(e=>S$1(e)&&e.length===t))(e))),maxLength:e=>x$2(d$1(t,(t=>p$2(e=>S$1(e)&&e.length<=t))(e))),includes:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&t.includes(r)))));var r},regex:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&Boolean(t.match(r))))));var r}}),A=x$2(p$2(S$1)),N$1=t=>Object.assign(a$2(t),{between:(e,r)=>N$1(d$1(t,((t,e)=>p$2(r=>w$2(r)&&t<=r&&e>=r))(e,r))),lt:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e<t))(e))),gt:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e>t))(e))),lte:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e<=t))(e))),gte:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e>=t))(e))),int:()=>N$1(d$1(t,p$2(t=>w$2(t)&&Number.isInteger(t)))),finite:()=>N$1(d$1(t,p$2(t=>w$2(t)&&Number.isFinite(t)))),positive:()=>N$1(d$1(t,p$2(t=>w$2(t)&&t>0))),negative:()=>N$1(d$1(t,p$2(t=>w$2(t)&&t<0)))}),P=N$1(p$2(w$2)),k$1=t=>Object.assign(a$2(t),{between:(e,r)=>k$1(d$1(t,((t,e)=>p$2(r=>j$1(r)&&t<=r&&e>=r))(e,r))),lt:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e<t))(e))),gt:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e>t))(e))),lte:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e<=t))(e))),gte:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e>=t))(e))),positive:()=>k$1(d$1(t,p$2(t=>j$1(t)&&t>0))),negative:()=>k$1(d$1(t,p$2(t=>j$1(t)&&t<0)))}),T=k$1(p$2(j$1)),B=a$2(p$2(function(t){return"boolean"==typeof t})),_=a$2(p$2(function(t){return"symbol"==typeof t})),W=a$2(p$2(function(t){return null==t})),$$1=a$2(p$2(function(t){return null!=t}));var z={__proto__:null,matcher:t$2,optional:h$2,array:function(...t){return l$2({[t$2]:()=>({match:e=>{if(!Array.isArray(e))return{matched:!1};if(0===t.length)return{matched:!0};const r=t[0];let n={};if(0===e.length)return s$1(r).forEach(t=>{n[t]=[]}),{matched:!0,selections:n};const o=(t,e)=>{n[t]=(n[t]||[]).concat([e])};return{matched:e.every(t=>o$2(r,t,o)),selections:n}},getSelectionKeys:()=>0===t.length?[]:s$1(t[0])})})},set:function(...t){return a$2({[t$2]:()=>({match:e=>{if(!(e instanceof Set))return{matched:!1};let r={};if(0===e.size)return{matched:!0,selections:r};if(0===t.length)return{matched:!0};const n=(t,e)=>{r[t]=(r[t]||[]).concat([e])},o=t[0];return{matched:f$1(e,t=>o$2(o,t,n)),selections:r}},getSelectionKeys:()=>0===t.length?[]:s$1(t[0])})})},map:function(...t){return a$2({[t$2]:()=>({match:e=>{if(!(e instanceof Map))return{matched:!1};let r={};if(0===e.size)return{matched:!0,selections:r};const n=(t,e)=>{r[t]=(r[t]||[]).concat([e])};if(0===t.length)return{matched:!0};var o;if(1===t.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(o=t[0])?void 0:o.toString()}`);const[i,s]=t;return{matched:g$1(e,(t,e)=>{const r=o$2(i,e,n),o=o$2(s,t,n);return r&&o}),selections:r}},getSelectionKeys:()=>0===t.length?[]:[...s$1(t[0]),...s$1(t[1])]})})},record:function(...t){return a$2({[t$2]:()=>({match:e=>{if(null===e||"object"!=typeof e||Array.isArray(e))return{matched:!1};var r;if(0===t.length)throw new Error(`\`P.record\` wasn't given enough arguments. Expected (value) or (key, value), received ${null==(r=t[0])?void 0:r.toString()}`);let n={};const o=(t,e)=>{n[t]=(n[t]||[]).concat([e])},[i,s]=1===t.length?[A,t[0]]:t;return{matched:m$1(e,(t,e)=>{const r="string"!=typeof t||Number.isNaN(Number(t))?null:Number(t),n=null!==r&&o$2(i,r,o),a=o$2(i,t,o),l=o$2(s,e,o);return(a||n)&&l}),selections:n}},getSelectionKeys:()=>0===t.length?[]:[...s$1(t[0]),...s$1(t[1])]})})},intersection:d$1,union:y$1,not:function(t){return a$2({[t$2]:()=>({match:e=>({matched:!o$2(t,e,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:p$2,select:v$1,any:K,unknown:O,_:E,string:A,number:P,bigint:T,boolean:B,symbol:_,nullish:W,nonNullable:$$1,instanceOf:function(t){return a$2(p$2(function(t){return e=>e instanceof t}(t)))},shape:function(t){return a$2(p$2(u$2(t)))}};class I extends Error{constructor(t){let e;try{e=JSON.stringify(t)}catch(r){e=t}super(`Pattern matching error: no pattern matches value ${e}`),this.input=void 0,this.input=t}}const L={matched:!1,value:void 0};function M$2(t){return new R(t,L)}class R{constructor(t,e){this.input=void 0,this.state=void 0,this.input=t,this.state=e}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]];let n;3===t.length&&"function"==typeof t[1]?n=t[1]:t.length>2&&r.push(...t.slice(1,t.length-1));let o=!1,i={};const s=(t,e)=>{o=!0,i[t]=e},a=!r.some(t=>o$2(t,this.input,s))||n&&!Boolean(n(this.input))?L:{matched:!0,value:e(o?n$2 in i?i[n$2]:i:this.input,this.input)};return new R(this.input,a)}when(t,e){if(this.state.matched)return this;const r=Boolean(t(this.input));return new R(this.input,r?{matched:!0,value:e(this.input,this.input)}:L)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(t=F){return this.state.matched?this.state.value:t(this.input)}run(){return this.exhaustive()}returnType(){return this}narrow(){return this}}function F(t){throw new I(t)}var index_umd$1={exports:{}},index_umd=index_umd$1.exports,exports$1;exports$1=index_umd$1.exports,function(t){function e(t,e,r,n,o){const i=Math.abs(r-t),s=t<r?1:-1,a=-Math.abs(n-e),l=e<n?1:-1;let h,c=i+a;for(;;){if(o(t,e),h=2*c,h>=a){if(t===r)break;c+=a,t+=s}if(h<=i){if(e===n)break;c+=i,e+=l}}}function r(t,e,r,n,o){const i=t<r?1:-1,s=e<n?1:-1;let a,l=Math.abs(r-t),h=Math.abs(n-e),c=l*l+h*h,u=0===c?1:1/Math.sqrt(c);for(l*=u,h*=u,c=l-h;;){if(o(t,e,Math.abs(c-(l+h))>>16),u=c,a=t,2*u>=-l){if(t===r)break;u+h<16711680&&o(t,e+s,u+h>>16),c-=h,t+=i}if(2*u<=h){if(e===n)break;l-u<16711680&&o(a+i,e,l-u>>16),c+=l,e+=s}}}function n(t,e,r,n,o,i){let s,a,l,h=Math.abs(r-t),c=t<r?1:-1,u=Math.abs(n-e),f=e<n?1:-1,d=h-u,m=h+u==0?1:Math.sqrt(h*h+u*u);for(o=(o+1)/2;;){if(i(t,e,Math.max(0,255*(Math.abs(d-h+u)/m-o+1))),s=d,a=t,2*s>=-h){for(s+=u,l=e;s<m*o&&(n!=l||h>u);s+=h)i(t,l+=f,Math.max(0,255*(Math.abs(s)/m-o+1)));if(t==r)break;s=d,d-=u,t+=c}if(2*s<=u){for(s=h-s;s<m*o&&(r!=a||h<u);s+=u)i(a+=c,e,Math.max(0,255*(Math.abs(s)/m-o+1)));if(e==n)break;d+=h,e+=f}}}function o(t,e="assert error"){if(!t)throw new Error(e)}function i(t,e,r,n,i,a,l,h){var c,u,f,d=t-2*r+i,m=e-2*n+a,g=t-r,p=e-n;o(l>=0,"width is negative"),g*(i-r)>0&&(p*(a-n)>0&&Math.abs(g*m)>Math.abs(p*d)&&(t=i,i=g+r,e=a,a=p+n),t==i||1==l?u=(t-r)/d:(f=Math.sqrt(4*l*l*(t-r)*(i-r)+(i-t)*(i-t)),r<t&&(f=-f),u=(2*l*(t-r)-t+i+f)/(2*(1-l)*(i-t))),g=(u*u*(t-2*l*r+i)+2*u*(l*r-t)+t)*(f=1/(2*u*(1-u)*(l-1)+1)),p=(u*u*(e-2*l*n+a)+2*u*(l*n-e)+e)*f,c=u*(l-1)+1,c*=c*f,l=((1-u)*(l-1)+1)*Math.sqrt(f),d=Math.floor(g+.5),m=Math.floor(p+.5),p=(g-t)*(n-e)/(r-t)+e,s(t,e,d,Math.floor(p+.5),d,m,c,h),p=(g-i)*(n-a)/(r-i)+a,n=Math.floor(p+.5),t=r=d,e=m),(e-n)*(a-n)>0&&(e==a||1==l?u=(e-n)/(e-2*n+a):(f=Math.sqrt(4*l*l*(e-n)*(a-n)+(a-e)*(a-e)),n<e&&(f=-f),u=(2*l*(e-n)-e+a+f)/(2*(1-l)*(a-e))),g=(u*u*(t-2*l*r+i)+2*u*(l*r-t)+t)*(f=1/(2*u*(1-u)*(l-1)+1)),p=(u*u*(e-2*l*n+a)+2*u*(l*n-e)+e)*f,c=u*(l-1)+1,c*=c*f,l=((1-u)*(l-1)+1)*Math.sqrt(f),d=Math.floor(g+.5),m=Math.floor(p+.5),g=(r-t)*(p-e)/(n-e)+t,s(t,e,Math.floor(g+.5),m,d,m,c,h),g=(r-i)*(p-a)/(n-a)+i,r=Math.floor(g+.5),t=d,e=n=m),s(t,e,r,n,i,a,l*l,h)}function s(t,r,n,i,a,l,h,c){var u,f=a-n,d=l-i,m=t-a,g=r-l,p=t-n,w=r-i,y=p*d+w*f,b=p*d-w*f;if(o(p*f<=0&&w*d<=0,"sign of gradient must not change"),0!=b&&h>0){if(f*f+d*d>p*p+w*w&&(a=t,t-=m,l=r,r-=g,b=-b),p=2*(4*h*f*p+m*m),w=2*(4*h*d*w+g*g),y=-2*(f=t<a?1:-1)*(d=r<l?1:-1)*(2*h*y+m*g),b*f*d<0&&(p=-p,w=-w,y=-y,b=-b),m=4*h*(n-t)*d*b+p/2+y,g=4*h*(r-i)*f*b+w/2+y,h<.5&&(g>y||m<y))return b=(h+1)/2,y=1/((h=Math.sqrt(h))+1),f=Math.floor((t+2*h*n+a)*y/2+.5),d=Math.floor((r+2*h*i+l)*y/2+.5),s(t,r,m=Math.floor((h*n+t)*y+.5),g=Math.floor((i*h+r)*y+.5),f,d,b,c),void s(f,d,m=Math.floor((h*n+a)*y+.5),g=Math.floor((i*h+l)*y+.5),a,l,b,c);u=m+g-y;do{if(c(t,r),t==a&&r==l)return;n=2*u>g?1:0,i=2*(u+w)<-g?1:0,(2*u<m||i)&&(r+=d,g+=y,u+=m+=p),(2*u>m||n)&&(t+=f,m+=y,u+=g+=w)}while(g<=y&&m>=y)}e(t,r,a,l,c)}function a(t,e,n,i,s,l,h,c){var u,f,d,m=s-n,g=l-i,p=t-s,w=e-l,y=t-n,b=e-i,x=y*g+b*m,M=y*g-b*m;if(o(y*m<=0&&b*g<=0),0!=M&&h>0){if(m*m+g*g>y*y+b*b&&(s=t,t-=p,l=e,e-=w,M=-M),y=2*(4*h*m*y+p*p),b=2*(4*h*g*b+w*w),x=-2*(m=t<s?1:-1)*(g=e<l?1:-1)*(2*h*x+p*w),M*m*g<0&&(y=-y,b=-b,M=-M,x=-x),p=4*h*(n-t)*g*M+y/2+x,w=4*h*(e-i)*m*M+b/2+x,h<.5&&w>p)return M=(h+1)/2,x=1/((h=Math.sqrt(h))+1),m=Math.floor((t+2*h*n+s)*x/2+.5),g=Math.floor((e+2*h*i+l)*x/2+.5),a(t,e,p=Math.floor((h*n+t)*x+.5),w=Math.floor((i*h+e)*x+.5),m,g,M,c),a(m,g,p=Math.floor((h*n+s)*x+.5),w=Math.floor((i*h+l)*x+.5),s,l,M,c);u=p+w-x;do{if(M=Math.min(p-x,x-w),f=Math.max(p-x,x-w),f+=2*f*M*M/(4*f*f+M*M),(n=255*Math.abs(u-p-w+x)/f)<256&&c(t,e,n),d=2*u+w<0){if(e==l)return;p-u<f&&c(t+m,e,255*Math.abs(p-u)/f)}if(2*u+p>0){if(t==s)return;u-w<f&&c(t,e+g,255*Math.abs(u-w)/f),t+=m,p+=x,u+=w+=b}d&&(e+=g,w+=x,u+=p+=y)}while(w<p)}r(t,e,s,l,c)}function l(t,e,r,n,o){let i=-r,s=0,a=n*n,l=i*(2*a+i)+a;do{o(t-i,e+s),o(t+i,e+s),o(t+i,e-s),o(t-i,e-s),a=2*l,a>=(2*i+1)*n*n&&(l+=(2*++i+1)*n*n),a<=(2*s+1)*r*r&&(l+=(2*++s+1)*r*r)}while(i<=0);for(;s++<n;)o(t,e+s),o(t,e-s)}function h(t,e,r,n,o,i){let s=r*r,a=n*n;const l=Math.sin(o);let h=(s-a)*l;s=Math.sqrt(s-h*l),a=Math.sqrt(a+h*l),h=h*(r=s+.5)*(n=a+.5)/(s*a),c(t-r,e-n,t+r,e+n,4*h*Math.cos(o),i)}function c(t,e,r,n,i,a){let l=r-t,h=n-e,c=l*h;if(0===i)return u(t,e,r,n,a);0!==c&&(c=(c-i)/(c+c)),o(c<=1&&c>=0,"limit angle to |zd|<=xd*yd"),l=Math.floor(l*c+.5),h=Math.floor(h*c+.5),s(t,e+h,t,e,t+l,e,1-c,a),s(t,e+h,t,n,r-l,n,c,a),s(r,n-h,r,n,r-l,n,1-c,a),s(r,n-h,r,e,t+l,e,c,a)}function u(t,e,r,n,o){let i,s=Math.abs(r-t),a=Math.abs(n-e),l=1&a,h=4*(1-s)*a*a,c=4*(l+1)*s*s,u=h+c+l*s*s;t>r&&(t=r,r+=s),e>n&&(e=n),n=(e+=(a+1)/2)-l,s*=8*s,l=8*a*a;do{o(r,e),o(t,e),o(t,n),o(r,n),i=2*u,i<=c&&(e++,n--,u+=c+=s),(i>=h||2*u>c)&&(t++,r--,u+=h+=l)}while(t<=r);for(;e-n<=a;)o(t-1,e),o(r+1,e++),o(t-1,n),o(r+1,n--)}function f(t,e,r,n){var o=-r,i=0,s=2-2*r;do{n(t-o,e+i),n(t-i,e-o),n(t+o,e-i),n(t+i,e+o),(r=s)<=i&&(s+=2*++i+1),(r>o||s>i)&&(s+=2*++o+1)}while(o<0)}function d(t,e,r,n){var o,i,s,a=-r,l=0,h=2-2*r;r=1-h;do{n(t-a,e+l,o=255*Math.abs(h-2*(a+l)-2)/r),n(t-l,e-a,o),n(t+a,e-l,o),n(t+l,e+a,o),s=h,i=a,h+l>0&&((o=255*(h-2*a-1)/r)<256&&(n(t-a,e+l+1,o),n(t-l-1,e-a,o),n(t+a,e-l-1,o),n(t+l+1,e+a,o)),h+=2*++a+1),s+i<=0&&((o=255*(2*l+3-s)/r)<256&&(n(t-i-1,e+l,o),n(t-l,e-i-1,o),n(t+i+1,e-l,o),n(t+l,e+i+1,o)),h+=2*++l+1)}while(a<0)}function m(t,e,r,n,o,i,s){var a,l=t-r,h=e-n,c=t-2*r+o;l*(o-r)>0&&(h*(i-n)>0&&Math.abs((e-2*n+i)/c*l)>Math.abs(h)&&(t=o,o=l+r,e=i,i=h+n),a=(1-(c=(t-r)/c))*((1-c)*e+2*c*n)+c*c*i,c=(t*o-r*r)*c/(t-r),l=Math.floor(c+.5),h=Math.floor(a+.5),a=(n-e)*(c-t)/(r-t)+e,g(t,e,l,Math.floor(a+.5),l,h,s),a=(n-i)*(c-o)/(r-o)+i,t=r=l,e=h,n=Math.floor(a+.5)),(e-n)*(i-n)>0&&(a=(1-(c=(e-n)/(c=e-2*n+i)))*((1-c)*t+2*c*r)+c*c*o,c=(e*i-n*n)*c/(e-n),l=Math.floor(a+.5),h=Math.floor(c+.5),a=(r-t)*(c-e)/(n-e)+t,g(t,e,Math.floor(a+.5),h,l,h,s),a=(r-o)*(c-i)/(n-i)+o,t=l,r=Math.floor(a+.5),e=n=h),g(t,e,r,n,o,i,s)}function g(t,r,n,i,s,a,l){var h,c,u,f,d=s-n,m=a-i,g=t-n,p=r-i,w=g*m-p*d;if(o(g*d<=0&&p*m<=0,"sign of gradient must not change"),d*d+m*m>g*g+p*p&&(s=t,t=d+n,a=r,r=m+i,w=-w),0!=w){g+=d,p+=m,h=2*(g*=d=t<s?1:-1)*(p*=m=r<a?1:-1),g*=g,p*=p,w*d*m<0&&(g=-g,p=-p,h=-h,w=-w),c=4*m*w*(n-t)+g-h,u=4*d*w*(r-i)+p-h,g+=g,p+=p,f=c+u+h;do{if(l(t,r),t==s&&r==a)return;i=+(2*f<c),2*f>u&&(t+=d,c-=h,f+=u+=p),i&&(r+=m,u-=h,f+=c+=g)}while(u<0&&c>0)}e(t,r,s,a,l)}function p(t,e,r,n,o,i,s){var a,l=t-r,h=e-n,c=t-2*r+o;l*(o-r)>0&&(h*(i-n)>0&&Math.abs((e-2*n+i)/c*l)>Math.abs(h)&&(t=o,o=l+r,e=i,i=h+n),a=(1-(c=(t-r)/c))*((1-c)*e+2*c*n)+c*c*i,c=(t*o-r*r)*c/(t-r),l=Math.floor(c+.5),h=Math.floor(a+.5),a=(n-e)*(c-t)/(r-t)+e,w(t,e,l,Math.floor(a+.5),l,h,s),a=(n-i)*(c-o)/(r-o)+i,t=r=l,e=h,n=Math.floor(a+.5)),(e-n)*(i-n)>0&&(a=(1-(c=(e-n)/(c=e-2*n+i)))*((1-c)*t+2*c*r)+c*c*o,c=(e*i-n*n)*c/(e-n),l=Math.floor(a+.5),h=Math.floor(c+.5),a=(r-t)*(c-e)/(n-e)+t,w(t,e,Math.floor(a+.5),h,l,h,s),a=(r-o)*(c-i)/(n-i)+o,t=l,r=Math.floor(a+.5),e=n=h),w(t,e,r,n,o,i,s)}function w(t,e,n,o,i,s,a){var l,h,c,u,f,d=i-n,m=s-o,g=t-n,p=e-o,w=g*m-p*d;if(d*d+m*m>g*g+p*p&&(i=t,t=d+n,s=e,e=m+o,w=-w),0!=w){g+=d,p+=m,l=2*(g*=d=t<i?1:-1)*(p*=m=e<s?1:-1),g*=g,p*=p,w*d*m<0&&(g=-g,p=-p,l=-l,w=-w),h=4*m*(n-t)*w+g-l,c=4*d*(e-o)*w+p-l,g+=g,p+=p,u=h+c+l;do{if(w=Math.min(h+l,-l-c),f=Math.max(h+l,-l-c),f+=2*f*w*w/(4*f*f+w*w),a(t,e,255*Math.abs(u-h-c-l)/f),t==i||e==s)break;n=t,w=h-u,o=+(2*u+c<0),2*u+h>0&&(u-c<f&&a(t,e+m,255*Math.abs(u-c)/f),t+=d,h-=l,u+=c+=p),o&&(w<f&&a(n+d,e,255*Math.abs(w)/f),e+=m,c-=l,u+=h+=g)}while(c<h)}r(t,e,i,s,a)}function y(t,r,n,i,s,a,l,h,c){var u,f,d,m=1;let p,w,y,b,x,M,v,I,A,k,R,E=t<l?1:-1,$=r<h?1:-1,P=-Math.abs(t+n-s-l),S=P-4*E*(n-s),C=E*(t-n-s+l),B=-Math.abs(r+i-a-h),T=B-4*$*(i-a),D=$*(r-i-a+h),_=.01;if(o((n-t)*(s-l)<_&&((l-t)*(n-s)<_||C*C<S*P+_),"slope change"),o((i-r)*(a-h)<_&&((h-r)*(i-a)<_||D*D<T*B+_),"slope change"),0==S&&0==T)return E=Math.floor((3*n-t+1)/2),$=Math.floor((3*i-r+1)/2),g(t,r,E,$,l,h,c);n=(n-t)*(n-t)+(i-r)*(i-r)+1,s=(s-l)*(s-l)+(a-h)*(a-h)+1;do{p=S*D-C*T,w=S*B-P*T,y=C*B-P*D,k=p*(p+w-3*y)+w*w,p*=u=k>0?1:Math.sqrt(1+1024/n),w*=u,y*=u,k*=u*u,M=9*(p+w+y)/8,b=8*(S-T),I=27*(8*p*(D*D-T*B)+k*(T+2*D+B))/64-T*T*(M-T),A=27*(8*p*(C*C-S*P)-k*(S+2*C+P))/64-S*S*(M+S),x=3*(3*p*(3*D*D-T*T-2*T*B)-T*(3*w*(T+D)+T*b))/4,v=3*(3*p*(3*C*C-S*S-2*S*P)-S*(3*w*(S+C)+S*b))/4,M=S*T*(6*p+6*w-3*y+b),w=T*T,b=S*S,M=3*(M+9*u*(b*D*B-C*P*w)-18*C*D*p)/8,k<0&&(I=-I,A=-A,x=-x,v=-v,M=-M,w=-w,b=-b),p=6*T*w,w*=-6*S,y=6*T*b,b*=-6*S,I+=M,k=I+A,A+=M;t:for(R=M,f=d=u;t!=l&&r!=h;){c(t,r);do{if(I>R||A<R)break t;i=2*k-A,2*k>=I&&(f--,k+=I+=x,A+=M+=w,v+=y,x+=p),i<=0&&(d--,k+=A+=v,I+=M+=y,x+=w,v+=b)}while(f>0&&d>0);2*f<=u&&(t+=E,f+=u),2*d<=u&&(r+=$,d+=u),R==M&&I<0&&A>0&&(R=_)}x=t,t=l,l=x,E=-E,C=-C,v=r,r=h,h=v,$=-$,D=-D,n=s}while(m--);e(t,r,l,h,c)}function b(t,e,n,i,s,a,l,h,c){let u,f,d,m,g,p,y,b,x,M,v,I,A,k,R,E,$,P=1,S=t<l?1:-1,C=e<h?1:-1,B=-Math.abs(t+n-s-l),T=B-4*S*(n-s),D=S*(t-n-s+l),_=-Math.abs(e+i-a-h),V=_-4*C*(i-a),N=C*(e-i-a+h);const O=.01;if(o((n-t)*(s-l)<O&&((l-t)*(n-s)<O||D*D<T*B+O)),o((i-e)*(a-h)<O&&((h-e)*(i-a)<O||N*N<V*_+O)),0===T&&0===V)return S=Math.floor((3*n-t+1)/2),C=Math.floor((3*i-e+1)/2),w(t,e,S,C,l,h,c);n=(n-t)*(n-t)+(i-e)*(i-e)+1,s=(s-l)*(s-l)+(a-h)*(a-h)+1;do{m=T*N-D*V,g=T*_-B*V,p=D*_-B*N,$=4*m*p-g*g,A=m*(m+g-3*p)+g*g,u=A>0?1:Math.sqrt(1+1024/n),m*=u,g*=u,p*=u,A*=u*u,x=9*(m+g+p)/8,y=8*(T-V),v=27*(8*m*(N*N-V*_)+A*(V+2*N+_))/64-V*V*(x-V),I=27*(8*m*(D*D-T*B)-A*(T+2*D+B))/64-T*T*(x+T),b=3*(3*m*(3*N*N-V*V-2*V*_)-V*(3*g*(V+N)+V*y))/4,M=3*(3*m*(3*D*D-T*T-2*T*B)-T*(3*g*(T+D)+T*y))/4,x=T*V*(6*m+6*g-3*p+y),g=V*V,y=T*T,x=3*(x+9*u*(y*N*_-D*B*g)-18*D*N*m)/8,A<0&&(v=-v,I=-I,b=-b,M=-M,x=-x,g=-g,y=-y),m=6*V*g,g*=-6*T,p=6*V*y,y*=-6*T,v+=x,A=v+I,I+=x;let r=!1;t:for(f=d=u;t!==l&&e!==h;){i=Math.min(Math.abs(x-v),Math.abs(I-x)),E=Math.max(Math.abs(x-v),Math.abs(I-x)),E=u*(E+2*E*i*i/(4*E*E+i*i)),(i=255*Math.abs(A-(u-f+1)*v-(u-d+1)*I+u*x)/E)<256&&c(t,e,i),k=Math.abs(A-(u-f+1)*v+(d-1)*I),R=Math.abs(A+(f-1)*v-(u-d+1)*I),a=e;do{if($>=-O&&(v+b>x||I+M<x)){r=!0;break t}if(i=2*A+v,2*A+I>0)f--,A+=v+=b,I+=x+=g,M+=p,b+=m;else if(i>0){r=!0;break t}i<=0&&(d--,A+=I+=M,v+=x+=p,b+=g,M+=y)}while(f>0&&d>0);2*d<=u&&(R<E&&c(t+S,e,255*R/E),e+=C,d+=u),2*f<=u&&(k<E&&c(t,a+C,255*k/E),t+=S,f+=u)}r&&(2*A<I&&2*d<=u+2&&(R<E&&c(t+S,e,255*R/E),e+=C),2*A>v&&2*f<=u+2&&(k<E&&c(t,a+C,255*k/E),t+=S),b=t,t=l,l=b,S=-S,D=-D,M=e,e=h,h=M,C=-C,N=-N,n=s);break}while(P--);r(Math.floor(t),Math.floor(e),Math.floor(l),Math.floor(h),c)}function x(t,e,r,n,o,i,s,a,l,h){let c=0,u=0,f=t+r-o-s,d=f-4*(r-o),m=t-r-o+s,g=m+4*(r+o),p=e+n-i-a,w=p-4*(n-i),y=e-n-i+a,b=y+4*(n+i);var x,M,v,I,A,k,R=t,E=e;let $,P=m*m-d*f;const S=[0,0,0,0,0];for(0==d?Math.abs(f)<2*Math.abs(m)&&(S[c++]=f/(2*m)):P>0&&($=Math.sqrt(P),P=(m-$)/d,Math.abs(P)<1&&(S[c++]=P),P=(m+$)/d,Math.abs(P)<1&&(S[c++]=P)),P=y*y-w*p,0==w?Math.abs(p)<2*Math.abs(y)&&(S[c++]=p/(2*y)):P>0&&($=Math.sqrt(P),P=(y-$)/w,Math.abs(P)<1&&(S[c++]=P),P=(y+$)/w,Math.abs(P)<1&&(S[c++]=P)),u=1;u<c;u++)(P=S[u-1])>S[u]&&(S[u-1]=S[u],S[u]=P,u=0);for(P=-1,S[c]=1,u=0;u<=c;u++)$=S[u],x=(P*(P*m-2*f)-$*(P*(P*d-2*m)+f)+g)/8-R,I=(P*(P*y-2*p)-$*(P*(P*w-2*y)+p)+b)/8-E,M=($*($*m-2*f)-P*($*($*d-2*m)+f)+g)/8-R,A=($*($*y-2*p)-P*($*($*w-2*y)+p)+b)/8-E,R-=v=($*($*(3*m-$*d)-3*f)+g)/8,E-=k=($*($*(3*y-$*w)-3*p)+b)/8,s=Math.floor(v+.5),a=Math.floor(k+.5),0!=R&&(x*=R=(t-s)/R,M*=R),0!=E&&(I*=E=(e-a)/E,A*=E),(t!=s||e!=a)&&l(t,e,t+x,e+I,t+M,e+A,s,a,h),t=s,e=a,R=v,E=k,P=$}function M(t,e,r,n,o,i,s,a,l){x(t,e,r,n,o,i,s,a,y,l)}function v(t,e,r,n,o,i,s,a,l){x(t,e,r,n,o,i,s,a,b,l)}t.circle=f,t.circleAA=d,t.cubicBezier=M,t.cubicBezierAA=v,t.cubicBezierSegment=y,t.cubicBezierSegmentAA=b,t.ellipse=l,t.ellipseRect=u,t.line=e,t.lineAA=r,t.lineWidth=n,t.quadBezier=m,t.quadBezierAA=p,t.quadBezierSegment=g,t.quadBezierSegmentAA=w,t.quadRationalBezier=i,t.quadRationalBezierSegment=s,t.quadRationalBezierSegmentAA=a,t.rotatedEllipse=h,t.rotatedEllipseRect=c,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}(exports$1);var index_umdExports=index_umd$1.exports;function deleteDuplicates(t){const e=[];for(let r=0;r<t.length;r++)t[r].column===t[(r+1)%t.length].column&&t[r].row===t[(r+1)%t.length].row||e.push(t[r]);return e}function getCirclePoints(t){const e=[];index_umdExports.circle(t,t,t,(r,n)=>{e.push({row:n-t,column:r-t})});const r=[],n=[],o=[],i=[];for(let t=0;t<e.length;t+=4)r.push(e[t%e.length]),n.push(e[(t+1)%e.length]),o.push(e[(t+2)%e.length]),i.push(e[(t+3)%e.length]);return r.concat(n,o,i)}const zeroPoint={row:0,column:0};function getFilledCirclePoints(t,e=zeroPoint){const r=[];if(0===t)return[e];1===t&&r.push(e),index_umdExports.circle(e.column,e.row,t,(t,n)=>{r.push({row:n,column:t}),t-1>e.column?r.push(...getLinePoints({row:n,column:t-1},{row:n,column:e.column})):t+1<e.column&&r.push(...getLinePoints({row:n,column:t+1},{row:n,column:e.column}))});return deleteDuplicates(sortByColumnRow(r))}function getLinePoints(t,e){const r=[];return index_umdExports.line(t.column,t.row,e.column,e.row,(t,e)=>{r.push({row:e,column:t})}),r}function getCompassPoints(t){return index_umdExports.circle(t,t,t,(t,e)=>{}),[{row:0,column:t},{row:t,column:0},{row:0,column:-t},{row:-t,column:0}]}function getIndex(t,e,r,n=0){return(e*r.width+t)*r.channels+n}const surroundingPixels=[{row:0,column:1},{row:1,column:1},{row:1,column:0},{row:1,column:-1},{row:0,column:-1},{row:-1,column:-1},{row:-1,column:0},{row:-1,column:1}],formatter=new Intl.ListFormat("en",{type:"disjunction"});function checkProcessable$1(t,e={}){let{bitDepth:r,alpha:n,colorModel:o,components:i,channels:s}=e;if(r&&(Array.isArray(r)||(r=[r]),!r.includes(t.bitDepth)))throw new RangeError(`image bitDepth must be ${format(r)} to apply this algorithm`);if(n&&(Array.isArray(n)||(n=[n]),!n.includes(t.alpha)))throw new RangeError(`image alpha must be ${format(n)} to apply this algorithm`);if(o&&(Array.isArray(o)||(o=[o]),!o.includes(t.colorModel)))throw new RangeError(`image colorModel must be ${format(o)} to apply this algorithm`);if(i&&(Array.isArray(i)||(i=[i]),!i.includes(t.components))){const t=`image components must be ${format(i)} to apply this algorithm`;throw 1===i.length&&1===i[0]?new RangeError(`${t}. The image can be converted using "image.grey()"`):new RangeError(t)}if(s&&(Array.isArray(s)||(s=[s]),!s.includes(t.channels)))throw new RangeError(`image channels must be ${format(s)} to apply this algorithm`)}function format(t){return formatter.format(t.map(String))}function getFastScore(t,e,r,n){const o=t.getValueByPoint(e,0);let i=0,s=0;for(const a of n){const n=t.getValue(e.column+a.column,e.row+a.row,0);n>=o+r?i+=Math.abs(n-o)-r:n<=o-r&&(s+=Math.abs(o-n)-r)}return Math.max(i,s)}const SOBEL_X=[[-1,0,1],[-2,0,2],[-1,0,1]],SOBEL_Y=[[-1,-2,-1],[0,0,0],[1,2,1]],SCHARR_X=[[3,0,-3],[10,0,-10],[3,0,-3]],SCHARR_Y=[[3,10,3],[0,0,0],[-3,-10,-3]],PREWITT_X=[[1,0,-1],[1,0,-1],[1,0,-1]],PREWITT_Y=[[1,1,1],[0,0,0],[-1,-1,-1]];function getEigenvaluesForScore(t,e,r=7){if(!(r%2))throw new TypeError("windowSize must be an odd integer");const n={row:e.row-(r-1)/2,column:e.column-(r-1)/2},o=t.crop({origin:n,width:r,height:r}),i=o.gradientFilter({kernelX:SOBEL_X}),s=o.gradientFilter({kernelY:SOBEL_Y}),a=new WrapperMatrix1D(i.getRawImage().data,{rows:i.height}),l=new WrapperMatrix1D(s.getRawImage().data,{rows:s.height}),h=a.mmul(a),c=l.mmul(a),u=l.mmul(l),f=h.sum(),d=c.sum(),m=u.sum(),g=new Matrix([[f,d],[d,m]]);return new EigenvalueDecomposition(g).realEigenvalues}function getHarrisScore(t,e,r={}){const{harrisConstant:n=.04,windowSize:o}=r,i=getEigenvaluesForScore(t,e,o);return i[0]*i[1]-n*(i[0]+i[1])**2}function getShiTomasiScore(t,e,r={}){const{windowSize:n=7}=r,o=getEigenvaluesForScore(t,e,n);return Math.min(o[0],o[1])}function isFastKeypoint(t,e,r,n,o={}){const{nbContiguousPixels:i=12,threshold:s=20}=o,a=e.getValueByPoint(t,0);let l=0,h=0;if(i>=12){for(const r of n){const n=e.getValue(t.column+r.column,t.row+r.row,0);a-n>s?h++:n-a>s&&l++}if(h<3&&l<3)return!1}const c=[];for(const n of r){const r=e.getValue(t.column+n.column,t.row+n.row,0);a+s<=r?c.push(-1):r<=a-s?c.push(1):c.push(0)}let u=1;const f=[];for(let t=0;t<c.length;t++){c[t]===c[(t+1)%c.length]?t===c.length-1?0===f.length?f.push(u):f[0]+=u:u++:(f.push(u),u=1)}return Math.max(...f)>=i}function getFastKeypoints(t,e={}){const{fastRadius:r=3,scoreAlgorithm:n="FAST",scoreOptions:o}=e,i=getCirclePoints(r),s=getCompassPoints(r),{maxNbFeatures:a=500,nbContiguousPixels:l=3/4*i.length,threshold:h=20,nonMaxSuppression:c=!0}=e;function u(t,e){return getHarrisScore(t,e,o)}function f(t,e){return getFastScore(t,e,h,i)}function d(t,e){return getShiTomasiScore(t,e,o)}checkProcessable$1(t,{channels:[1],alpha:!1});const m=M$2(n).with("HARRIS",()=>u).with("FAST",()=>f).with("TOMASI",()=>d).exhaustive(),g=[],p=new Float64Array(t.size).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++){const n={row:e,column:r};if(isFastKeypoint(n,t,i,s,{nbContiguousPixels:l,threshold:h})){const e=m(t,n);p[getIndex(n.column,n.row,t,0)]=e,g.push({origin:n,score:e})}}let w=[];if(c)for(const e of g){const r=p[getIndex(e.origin.column,e.origin.row,t,0)];for(let n=0;n<surroundingPixels.length;n++){const o=surroundingPixels[n];if(p[getIndex(e.origin.column+o.column,e.origin.row+o.row,t,0)]>r)break;n===surroundingPixels.length-1&&w.push(e)}}else w=g;return w.sort((t,e)=>e.score-t.score),w.slice(0,a)}function getPatchIntensityMoment(t,e,r,n={}){const{center:o=t.getCoordinates("center"),radius:i=3}=n;if(!checkBorderDistance(t,o,i))throw new RangeError("desired patch is too close to image border");const s=new Array(t.channels).fill(0),a=getFilledCirclePoints(i);for(const n of a)for(let i=0;i<t.channels;i++){const a=t.getValue(n.column+o.column,n.row+o.row,i);s[i]+=n.column**e*n.row**r*a}return s}function getPatchIntensityCentroid(t,e={}){const r=getPatchIntensityMoment(t,1,0,e),n=getPatchIntensityMoment(t,0,1,e),o=getPatchIntensityMoment(t,0,0,e),i=[];for(let e=0;e<t.channels;e++)0===o[e]?i.push({column:0,row:0}):i.push({column:r[e]/o[e],row:n[e]/o[e]});return i}function getOrientedFastKeypoints(t,e={}){const{centroidPatchDiameter:r=7}=e,n=getFastKeypoints(t,e),o=getRadius(r),i=[];for(const e of n)checkBorderDistance(t,e.origin,o)&&i.push(e);const s=[];for(const e of i){const r=toDegrees(getClockwiseAngle({column:0,row:0},getPatchIntensityCentroid(t,{center:e.origin,radius:o})[0]));s.push({...e,angle:r})}return s}function compareIntensity(t,e,r,n={}){const{origin:o=t.getCoordinates("center")}=n,i=sum$1(e,o),s=sum$1(r,o);return t.getValueByPoint(i,0)<t.getValueByPoint(s,0)}function xMedian(t,e={}){if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");const{exact:r=!1}=e||{},n=t.slice(),o=calcMiddle(0,n.length-1),i=quickSelect(n,o);if(n.length%2==1||!r)return i;return(i+quickSelect(n,o+1))/2}function quickSelect(t,e){let r=0,n=t.length-1,o=0,i=0,s=0;for(;;){if(n<=r)return t[e];if(n===r+1)return t[r]>t[n]&&swap(t,r,n),t[e];for(o=calcMiddle(r,n),t[o]>t[n]&&swap(t,o,n),t[r]>t[n]&&swap(t,r,n),t[o]>t[r]&&swap(t,o,r),swap(t,o,r+1),i=r+1,s=n;;){do{i++}while(t[r]>t[i]);do{s--}while(t[s]>t[r]);if(s<i)break;swap(t,i,s)}swap(t,r,s),s<=e&&(r=i),s>=e&&(n=s-1)}}function swap(t,e,r){const n=t[r];t[r]=t[e],t[e]=n}function calcMiddle(t,e){return Math.floor((t+e)/2)}function xCheck(t,e={}){const{minLength:r}=e;if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if("number"!=typeof t[0])throw new TypeError("input must contain numbers");if(r&&t.length<r)throw new Error(`input must have a length of at least ${r}`)}function xFindClosestIndex(t,e,r={}){const{sorted:n=!0}=r;if(n){let r=0,n=t.length-1,o=0;for(;n-r>1;)if(o=r+(n-r>>1),t[o]<e)r=o;else{if(!(t[o]>e))return o;n=o}return r<t.length-1?Math.abs(e-t[r])<Math.abs(t[r+1]-e)?r:r+1:r}{let r=0,n=Number.POSITIVE_INFINITY;for(let o=0;o<t.length;o++){const i=Math.abs(t[o]-e);i<n&&(n=i,r=o)}return r}}function xGetFromToIndex(t,e={}){let{fromIndex:r,toIndex:n}=e;const{from:o,to:i}=e;return void 0===r&&(r=void 0!==o?xFindClosestIndex(t,o):0),void 0===n&&(n=void 0!==i?xFindClosestIndex(t,i):t.length-1),r<0&&(r=0),n<0&&(n=0),r>=t.length&&(r=t.length-1),n>=t.length&&(n=t.length-1),r>n&&([r,n]=[n,r]),{fromIndex:r,toIndex:n}}function xMean(t,e={}){xCheck(t);const{fromIndex:r,toIndex:n}=xGetFromToIndex(t,e);let o=t[r];for(let e=r+1;e<=n;e++)o+=t[e];return o/(n-r+1)}function xVariance(t,e={}){if(!isAnyArray$1(t))throw new TypeError("input must be an array");const{unbiased:r=!0,mean:n=xMean(t)}=e;let o=0;for(const e of t){const t=e-n;o+=t*t}return r?o/(t.length-1):o/t.length}function FFT(t){if(this.size=0|t,this.size<=1||this.size&this.size-1)throw new Error("FFT size must be a power of two and bigger than 1");this._csize=t<<1;for(var e=new Array(2*this.size),r=0;r<e.length;r+=2){const t=Math.PI*r/this.size;e[r]=Math.cos(t),e[r+1]=-Math.sin(t)}this.table=e;for(var n=0,o=1;this.size>o;o<<=1)n++;this._width=n%2==0?n-1:n,this._bitrev=new Array(1<<this._width);for(var i=0;i<this._bitrev.length;i++){this._bitrev[i]=0;for(var s=0;s<this._width;s+=2){var a=this._width-s-2;this._bitrev[i]|=(i>>>s&3)<<a}}this._out=null,this._data=null,this._inv=0}FFT.prototype.fromComplexArray=function(t,e){for(var r=e||new Array(t.length>>>1),n=0;n<t.length;n+=2)r[n>>>1]=t[n];return r},FFT.prototype.createComplexArray=function(){const t=new Array(this._csize);for(var e=0;e<t.length;e++)t[e]=0;return t},FFT.prototype.toComplexArray=function(t,e){for(var r=e||this.createComplexArray(),n=0;n<r.length;n+=2)r[n]=t[n>>>1],r[n+1]=0;return r},FFT.prototype.completeSpectrum=function(t){for(var e=this._csize,r=e>>>1,n=2;n<r;n+=2)t[e-n]=t[n],t[e-n+1]=-t[n+1]},FFT.prototype.transform=function(t,e){if(t===e)throw new Error("Input and output buffers must be different");this._out=t,this._data=e,this._inv=0,this._transform4(),this._out=null,this._data=null},FFT.prototype.realTransform=function(t,e){if(t===e)throw new Error("Input and output buffers must be different");this._out=t,this._data=e,this._inv=0,this._realTransform4(),this._out=null,this._data=null},FFT.prototype.inverseTransform=function(t,e){if(t===e)throw new Error("Input and output buffers must be different");this._out=t,this._data=e,this._inv=1,this._transform4();for(var r=0;r<t.length;r++)t[r]/=this.size;this._out=null,this._data=null},FFT.prototype._transform4=function(){var t,e,r=this._out,n=this._csize,o=1<<this._width,i=n/o<<1,s=this._bitrev;if(4===i)for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleTransform2(t,r,o)}else for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleTransform4(t,r,o)}var a=this._inv?-1:1,l=this.table;for(o>>=2;o>=2;o>>=2){var h=(i=n/o<<1)>>>2;for(t=0;t<n;t+=i)for(var c=t+h,u=t,f=0;u<c;u+=2,f+=o){const t=u,e=t+h,n=e+h,o=n+h,i=r[t],s=r[t+1],c=r[e],d=r[e+1],m=r[n],g=r[n+1],p=r[o],w=r[o+1],y=i,b=s,x=l[f],M=a*l[f+1],v=c*x-d*M,I=c*M+d*x,A=l[2*f],k=a*l[2*f+1],R=m*A-g*k,E=m*k+g*A,$=l[3*f],P=a*l[3*f+1],S=p*$-w*P,C=p*P+w*$,B=y+R,T=b+E,D=y-R,_=b-E,V=v+S,N=I+C,O=a*(v-S),F=a*(I-C),L=B+V,U=T+N,z=B-V,G=T-N,q=D+F,Y=_-O,H=D-F,j=_+O;r[t]=L,r[t+1]=U,r[e]=q,r[e+1]=Y,r[n]=z,r[n+1]=G,r[o]=H,r[o+1]=j}}},FFT.prototype._singleTransform2=function(t,e,r){const n=this._out,o=this._data,i=o[e],s=o[e+1],a=o[e+r],l=o[e+r+1],h=i+a,c=s+l,u=i-a,f=s-l;n[t]=h,n[t+1]=c,n[t+2]=u,n[t+3]=f},FFT.prototype._singleTransform4=function(t,e,r){const n=this._out,o=this._data,i=this._inv?-1:1,s=2*r,a=3*r,l=o[e],h=o[e+1],c=o[e+r],u=o[e+r+1],f=o[e+s],d=o[e+s+1],m=o[e+a],g=o[e+a+1],p=l+f,w=h+d,y=l-f,b=h-d,x=c+m,M=u+g,v=i*(c-m),I=i*(u-g),A=p+x,k=w+M,R=y+I,E=b-v,$=p-x,P=w-M,S=y-I,C=b+v;n[t]=A,n[t+1]=k,n[t+2]=R,n[t+3]=E,n[t+4]=$,n[t+5]=P,n[t+6]=S,n[t+7]=C},FFT.prototype._realTransform4=function(){var t,e,r=this._out,n=this._csize,o=1<<this._width,i=n/o<<1,s=this._bitrev;if(4===i)for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleRealTransform2(t,r>>>1,o>>>1)}else for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleRealTransform4(t,r>>>1,o>>>1)}var a=this._inv?-1:1,l=this.table;for(o>>=2;o>=2;o>>=2){var h=(i=n/o<<1)>>>1,c=h>>>1,u=c>>>1;for(t=0;t<n;t+=i)for(var f=0,d=0;f<=u;f+=2,d+=o){var m=t+f,g=m+c,p=g+c,w=p+c,y=r[m],b=r[m+1],x=r[g],M=r[g+1],v=r[p],I=r[p+1],A=r[w],k=r[w+1],R=y,E=b,$=l[d],P=a*l[d+1],S=x*$-M*P,C=x*P+M*$,B=l[2*d],T=a*l[2*d+1],D=v*B-I*T,_=v*T+I*B,V=l[3*d],N=a*l[3*d+1],O=A*V-k*N,F=A*N+k*V,L=R+D,U=E+_,z=R-D,G=E-_,q=S+O,Y=C+F,H=a*(S-O),j=a*(C-F),W=L+q,K=U+Y,X=z+j,J=G-H;if(r[m]=W,r[m+1]=K,r[g]=X,r[g+1]=J,0!==f){if(f!==u){var Q=z+-a*j,Z=-G+-a*H,tt=L+-a*q,et=-U- -a*Y,rt=t+c-f,nt=t+h-f;r[rt]=Q,r[rt+1]=Z,r[nt]=tt,r[nt+1]=et}}else{var ot=L-q,it=U-Y;r[p]=ot,r[p+1]=it}}}},FFT.prototype._singleRealTransform2=function(t,e,r){const n=this._out,o=this._data,i=o[e],s=o[e+r],a=i+s,l=i-s;n[t]=a,n[t+1]=0,n[t+2]=l,n[t+3]=0},FFT.prototype._singleRealTransform4=function(t,e,r){const n=this._out,o=this._data,i=this._inv?-1:1,s=2*r,a=3*r,l=o[e],h=o[e+r],c=o[e+s],u=o[e+a],f=l+c,d=l-c,m=h+u,g=i*(h-u),p=f+m,w=d,y=-g,b=f-m,x=d,M=g;n[t]=p,n[t+1]=0,n[t+2]=w,n[t+3]=y,n[t+4]=b,n[t+5]=0,n[t+6]=x,n[t+7]=M};const LOOP=8,FLOAT_MUL=1/16777216,sh1=15,sh2=18,sh3=11;function multiplyUint32(t,e){const r=65535&(t>>>=0);return((t-r)*(e>>>=0)>>>0)+r*e>>>0}class XSadd{random;state;constructor(t=Date.now()){this.state=new Uint32Array(4),this.init(t),this.random=this.getFloat.bind(this)}getUint32(){return this.nextState(),this.state[3]+this.state[2]>>>0}getFloat(){return(this.getUint32()>>>8)*FLOAT_MUL}init(t){if(!Number.isInteger(t))throw new TypeError("seed must be an integer");this.state[0]=t,this.state[1]=0,this.state[2]=0,this.state[3]=0;for(let t=1;t<LOOP;t++)this.state[3&t]^=t+multiplyUint32(1812433253,this.state[t-1&3]^this.state[t-1&3]>>>30>>>0)>>>0;this.periodCertification();for(let t=0;t<LOOP;t++)this.nextState()}periodCertification(){0===this.state[0]&&0===this.state[1]&&0===this.state[2]&&0===this.state[3]&&(this.state[0]=88,this.state[1]=83,this.state[2]=65,this.state[3]=68)}nextState(){let t=this.state[0];t^=t<<sh1,t^=t>>>sh2,t^=this.state[3]<<sh3,this.state[0]=this.state[1],this.state[1]=this.state[2],this.state[2]=this.state[3],this.state[3]=t}}function createRandomArray(t={}){const{mean:e=0,standardDeviation:r=1,length:n=1e3,range:o=1,seed:i,distribution:s="normal"}=t,a=new XSadd(i),l=new Float64Array(n);switch(s){case"normal":{const t=new GaussianGenerator(e,r,a);for(let e=0;e<n;e++)l[e]=t.generateGaussian();break}case"uniform":for(let t=0;t<n;t++)l[t]=(a.random()-.5)*o+e;break;default:throw new Error(`unknown distribution: ${String(s)}`)}return l}class GaussianGenerator{#r=0;#n=!1;#o;#i;#s;constructor(t,e,r){this.#o=t,this.#i=e,this.#s=r}generateGaussian(){let t,e,r,n;if(this.#n)this.#n=!1,t=this.#r*this.#i+this.#o;else{do{e=2*this.#s.random()-1,r=2*this.#s.random()-1,n=e*e+r*r}while(n>=1||0===n);n=Math.sqrt(-2*Math.log(n)/n),this.#r=r*n,this.#n=!0,t=this.#o+this.#i*e*n}return t}}function xyCovariance(t,e={}){const{x:r,y:n}=t,{unbiased:o=!0}=e,i=xMean(r),s=xMean(n);let a=0;for(let t=0;t<r.length;t++)a+=(r[t]-i)*(n[t]-s);return o?a/(r.length-1):a/r.length}function assert(t,e){if(!t)throw new Error(e||"unreachable")}function assertUnreachable(t){throw new Error(`unreachable: ${String(t)}`)}function getClamp(t){return 255===t.maxValue?clamp255:(assert(65535===t.maxValue),clamp65535)}function clamp255(t){return Math.min(Math.max(t,0),255)}function clamp65535(t){return Math.min(Math.max(t,0),65535)}function getClampFromTo(t,e){return function(r){return Math.max(Math.min(r,e),t)}}function getGaussianPoints(t,e,r={}){const{nbPoints:n=1024,xSeed:o=0,ySeed:i=1,sigma:s}=r,a=getGaussianValues(t,o,n,s),l=getGaussianValues(e,i,n,s),h=[];for(let t=0;t<n;t++)h.push({column:a[t],row:l[t]});return h}function getGaussianValues(t,e,r,n=t/5){const o=(t-1)/2,i=createRandomArray({distribution:"normal",seed:e,length:r,standardDeviation:n,mean:0}),s=getClampFromTo(-o,o);return i.map(t=>{const e=Math.round(t);return s(e)})}function extractSquareImage(t,e,r){const n=(r-1)/2,o={column:e.column-n,row:e.row-n};return t.crop({origin:o,width:r,height:r})}function getKeypointPatch(t,e,r={}){const{patchSize:n=31}=r,o=e.angle*Math.PI/180,i=Math.floor(n*(Math.abs(Math.cos(o))+Math.abs(Math.sin(o)))),s=i%2?i:i+1,a=getRadius(s);if(!checkBorderDistance(t,e.origin,a))throw new RangeError("keypoint is too close to border for given patch size");const l=extractSquareImage(t,e.origin,s).transformRotate(-e.angle,{center:"center",interpolationType:"nearest"}),h=l.getCoordinates("center");return extractSquareImage(l,h,n)}function getBriefDescriptors(t,e,r={}){const{patchSize:n=31,descriptorLength:o=256,smoothingOptions:i={sigma:Math.sqrt(2),size:Math.min(t.height,t.width,9)},pointsDistributionOptions:s}=r;if(checkProcessable$1(t,{alpha:!1,colorModel:"GREY"}),!(n%2))throw new TypeError("patchSize must be an odd integer");if(Math.min(t.width,t.height)<n)throw new RangeError(`image is too small for patchSize = ${n}`);const a=getGaussianPoints(n,n,{...s,nbPoints:2*o}),l=t.gaussianBlur(i),h=[],c=[];for(const t of e){let e;try{e=getKeypointPatch(l,t,{patchSize:n})}catch{continue}if(null===e)continue;const r=new Uint8Array(o);for(let t=0;t<o;t++){const n=a[t],i=a[t+o];r[t]=Number(compareIntensity(e,n,i))}h.push(r),c.push(t)}return{keypoints:c,descriptors:h}}function getBrief(t,e={}){const{centroidPatchDiameter:r=15,bestKptRadius:n=10,minScore:o}=e,i=getBriefDescriptors(t,getBestKeypointsInRadius(getOrientedFastKeypoints(t,{centroidPatchDiameter:r}),n));if(o)for(let t=0;t<i.keypoints.length;t++)if(i.keypoints[t].score<o)return{keypoints:i.keypoints.slice(0,t-1),descriptors:i.descriptors.slice(0,t-1)};return i}function getIntensityMoment(t,e,r,n={}){const{origin:o=t.getCoordinates("center")}=n,i=new Array(t.channels).fill(0);for(let n=0;n<t.height;n++)for(let s=0;s<t.width;s++){const a=s-o.column,l=n-o.row;for(let o=0;o<t.channels;o++){const h=t.getValue(s,n,o);i[o]+=a**e*l**r*h}}return i}function getIntensityCentroid(t){const e=getIntensityMoment(t,1,0),r=getIntensityMoment(t,0,1),n=getIntensityMoment(t,0,0),o=[];for(let i=0;i<t.channels;i++)0===n[i]?o.push({column:0,row:0}):o.push({column:e[i]/n[i],row:r[i]/n[i]});return o}function getHammingDistance(t,e){let r=0;for(let n=0;n<t.length;n++)r+=t[n]^e[n];return r}function bruteForceOneMatch(t,e,r={}){const{sort:n=!!r.nbBestMatches,nbBestMatches:o=t.length}=r,i=[];for(let r=0;r<t.length;r++){let n=Number.POSITIVE_INFINITY,o=0;for(let i=0;i<e.length;i++){const s=getHammingDistance(t[r],e[i]);s<n&&(n=s,o=i)}i.push({sourceIndex:r,destinationIndex:o,distance:n})}return n&&i.sort((t,e)=>t.distance-e.distance),i.slice(0,o)}function sortBySourceDest(t){const e=t.slice();return e.sort((t,e)=>t.sourceIndex<e.sourceIndex?-1:t.sourceIndex>e.sourceIndex?1:t.destinationIndex-e.destinationIndex),e}function sortByDestSource(t){const e=t.slice();return e.sort((t,e)=>t.destinationIndex<e.destinationIndex?-1:t.destinationIndex>e.destinationIndex?1:t.sourceIndex-e.sourceIndex),e}function getCrosscheckMatches(t,e){return crosscheck(bruteForceOneMatch(t,e),bruteForceOneMatch(e,t))}function crosscheck(t,e){const r=[],n=sortBySourceDest(t),o=sortByDestSource(e);let i=0,s=0;for(;i<n.length&&s<o.length;){const t=n[i],e=o[s];t.sourceIndex>e.destinationIndex?s++:t.sourceIndex<e.destinationIndex?i++:t.destinationIndex>e.sourceIndex?s++:t.destinationIndex<e.sourceIndex?i++:(r.push({distance:Math.max(t.distance,e.distance),sourceIndex:t.sourceIndex,destinationIndex:t.destinationIndex}),i++,s++)}return r}function validateChannels(t,e){for(const r of t)validateChannel(r,e)}function validateChannel(t,e){if(!Number.isInteger(t)||t>=e.channels||t<0)throw new RangeError(`invalid channel: ${t}. It must be a positive integer smaller than ${e.channels}`)}function validateValues(t,e){for(const r of t)validateValue(r,e)}function validateValue(t,e){if(t>e.maxValue||t<0)throw new RangeError(`invalid value: ${t}. It must be a positive value smaller than ${e.maxValue+1}`)}function validateForComparison(t,e){if(t.width!==e.width||t.height!==e.height)throw new RangeError("both images must have the same size");if(t.alpha!==e.alpha||t.bitDepth!==e.bitDepth)throw new RangeError("both images must have the same alpha and bitDepth");if(t.channels!==e.channels)throw new RangeError("both images must have the same number of channels")}function validateColor(t,e){validateChannel(t.length-1,e),validateValues(t,e)}function divide(t,e,r={}){const{channels:n=new Array(t.channels).fill(0).map((t,e)=>e)}=r;if(validateChannels(n,t),0===e)throw new TypeError("Cannot divide by 0");const o=getOutputImage(t,r,{clone:!0});if(0===n.length)return o;for(const t of n)for(let r=0;r<o.height;r++)for(let n=0;n<o.width;n++){const i=o.getValue(n,r,t)/e;o.setClampedValue(n,r,t,i)}return o}function computeRmse(t,e){const r=computeMse(t,e);return Math.sqrt(r)}function computeMse(t,e){const r=t.subtract(e,{absolute:!0});let n=0;for(let e=0;e<t.size;e++)for(let o=0;o<t.channels;o++){n+=r.getValueByIndex(e,o)**2}return n/(t.size*t.channels)}function computePsnr(t,e){const r=computeRmse(t,e);return 20*Math.log10(t.maxValue/(r+Number.MIN_VALUE))}function getDefaultColor(t){return M$2(t.colorModel).with("GREY",()=>[0]).with("GREYA",()=>[0,t.maxValue]).with("RGB",()=>[0,0,0]).with("RGBA",()=>[0,0,0,t.maxValue]).with("BINARY",()=>[1]).exhaustive()}function setBlendedPixel(t,e,r,n){if(n=n??getDefaultColor(t),t.alpha){assert(t instanceof Image);const o=n.at(-1);if(o===t.maxValue)return void t.setPixel(e,r,n);const i=t.getValue(e,r,t.channels-1),s=o+i*(1-o/t.maxValue);t.setValue(e,r,t.channels-1,s);for(let a=0;a<t.components;a++){const l=(n[a]*o+t.getValue(e,r,a)*i*(1-o/t.maxValue))/s;t.setValue(e,r,a,l)}}else t.setPixel(e,r,n)}function setBlendedVisiblePixel(t,e,r,n){e>=0&&e<t.width&&r>=0&&r<t.height&&setBlendedPixel(t,e,r,n)}function roundPoint(t){return{row:Math.round(t.row),column:Math.round(t.column)}}function drawCircleOnImage(t,e,r,n={}){const o=getOutputImage(t,n,{clone:!0}),{strokeColor:i=getDefaultColor(o),fillColor:s}=n;if(validateColor(i,o),checkProcessable$1(o,{bitDepth:[8,16]}),r<0)throw new RangeError("circle radius must be positive");if(e=roundPoint(e),0===(r=Math.round(r)))return setBlendedVisiblePixel(o,e.column,e.row,i),o;if(s){1===r&&setBlendedVisiblePixel(o,e.column,e.row,s);let t=e.row+r,n=0;index_umdExports.circle(e.column,e.row,r,(r,a)=>{setBlendedVisiblePixel(o,r,a,i),0===n&&o.drawLine({row:a,column:r-1},{row:a,column:e.column-(r-e.column-1)},{strokeColor:s,out:o}),n%4==1&&t!==a&&(o.drawLine({row:a,column:r+1},{row:a,column:e.column-(r-e.column+1)},{strokeColor:s,out:o}),t=a,o.drawLine({row:e.row-(a-e.row),column:r+1},{row:e.row-(a-e.row),column:e.column-(r-e.column+1)},{strokeColor:s,out:o})),n++})}else index_umdExports.circle(e.column,e.row,r,(t,e)=>{setBlendedVisiblePixel(o,t,e,i)});return o}function drawLineOnImage(t,e,r,n={}){const o=getOutputImage(t,n,{clone:!0}),{strokeColor:i=getDefaultColor(o),origin:s={column:0,row:0}}=n;return validateColor(i,o),checkProcessable$1(o,{bitDepth:[8,16]}),index_umdExports.line(Math.round(s.column+e.column),Math.round(s.row+e.row),Math.round(s.column+r.column),Math.round(s.row+r.row),(t,e)=>{setBlendedVisiblePixel(o,t,e,i)}),o}function drawLineOnMask(t,e,r,n={}){const{origin:o={column:0,row:0}}=n,i=maskToOutputMask(t,n,{clone:!0});return index_umdExports.line(Math.round(o.column+e.column),Math.round(o.row+e.row),Math.round(o.column+r.column),Math.round(o.row+r.row),(t,e)=>{i.setVisiblePixel(t,e,[1])}),i}function drawPolylineOnImage(t,e,r={}){const{strokeColor:n=getDefaultColor(t),origin:o={column:0,row:0}}=r;checkProcessable$1(t,{bitDepth:[8,16]});const i=getOutputImage(t,r,{clone:!0});validateColor(n,i);for(let t=0;t<e.length-1;t++){const r=e[t],s=e[t+1];i.drawLine(r,s,{out:i,strokeColor:n,origin:o})}return i}function drawPolylineOnMask(t,e,r={}){const n=maskToOutputMask(t,r,{clone:!0});for(let t=0;t<e.length-1;t++){const o=e[t],i=e[t+1];n.drawLine(o,i,{out:n,origin:r.origin})}return n}var orientation={exports:{}},twoProduct_1=twoProduct$1,SPLITTER=+(Math.pow(2,27)+1);function twoProduct$1(t,e,r){var n=t*e,o=SPLITTER*t,i=o-(o-t),s=t-i,a=SPLITTER*e,l=a-(a-e),h=e-l,c=s*h-(n-i*l-s*l-i*h);return r?(r[0]=c,r[1]=n,r):[c,n]}var robustSum=linearExpansionSum;function scalarScalar$1(t,e){var r=t+e,n=r-t,o=t-(r-n)+(e-n);return o?[o,r]:[r]}function linearExpansionSum(t,e){var r=0|t.length,n=0|e.length;if(1===r&&1===n)return scalarScalar$1(t[0],e[0]);var o,i,s=new Array(r+n),a=0,l=0,h=0,c=Math.abs,u=t[l],f=c(u),d=e[h],m=c(d);f<m?(i=u,(l+=1)<r&&(f=c(u=t[l]))):(i=d,(h+=1)<n&&(m=c(d=e[h]))),l<r&&f<m||h>=n?(o=u,(l+=1)<r&&(f=c(u=t[l]))):(o=d,(h+=1)<n&&(m=c(d=e[h])));for(var g,p,w=o+i,y=w-o,b=i-y,x=b,M=w;l<r&&h<n;)f<m?(o=u,(l+=1)<r&&(f=c(u=t[l]))):(o=d,(h+=1)<n&&(m=c(d=e[h]))),(b=(i=x)-(y=(w=o+i)-o))&&(s[a++]=b),x=M-((g=M+w)-(p=g-M))+(w-p),M=g;for(;l<r;)(b=(i=x)-(y=(w=(o=u)+i)-o))&&(s[a++]=b),x=M-((g=M+w)-(p=g-M))+(w-p),M=g,(l+=1)<r&&(u=t[l]);for(;h<n;)(b=(i=x)-(y=(w=(o=d)+i)-o))&&(s[a++]=b),x=M-((g=M+w)-(p=g-M))+(w-p),M=g,(h+=1)<n&&(d=e[h]);return x&&(s[a++]=x),M&&(s[a++]=M),a||(s[a++]=0),s.length=a,s}var twoSum$1=fastTwoSum;function fastTwoSum(t,e,r){var n=t+e,o=n-t,i=e-o,s=t-(n-o);return r?(r[0]=s+i,r[1]=n,r):[s+i,n]}var twoProduct=twoProduct_1,twoSum=twoSum$1,robustScale=scaleLinearExpansion;function scaleLinearExpansion(t,e){var r=t.length;if(1===r){var n=twoProduct(t[0],e);return n[0]?n:[n[1]]}var o=new Array(2*r),i=[.1,.1],s=[.1,.1],a=0;twoProduct(t[0],e,i),i[0]&&(o[a++]=i[0]);for(var l=1;l<r;++l){twoProduct(t[l],e,s);var h=i[1];twoSum(h,s[0],i),i[0]&&(o[a++]=i[0]);var c=s[1],u=i[1],f=c+u,d=u-(f-c);i[1]=f,d&&(o[a++]=d)}return i[1]&&(o[a++]=i[1]),0===a&&(o[a++]=0),o.length=a,o}var robustDiff=robustSubtract;function scalarScalar(t,e){var r=t+e,n=r-t,o=t-(r-n)+(e-n);return o?[o,r]:[r]}function robustSubtract(t,e){var r=0|t.length,n=0|e.length;if(1===r&&1===n)return scalarScalar(t[0],-e[0]);var o,i,s=new Array(r+n),a=0,l=0,h=0,c=Math.abs,u=t[l],f=c(u),d=-e[h],m=c(d);f<m?(i=u,(l+=1)<r&&(f=c(u=t[l]))):(i=d,(h+=1)<n&&(m=c(d=-e[h]))),l<r&&f<m||h>=n?(o=u,(l+=1)<r&&(f=c(u=t[l]))):(o=d,(h+=1)<n&&(m=c(d=-e[h])));for(var g,p,w=o+i,y=w-o,b=i-y,x=b,M=w;l<r&&h<n;)f<m?(o=u,(l+=1)<r&&(f=c(u=t[l]))):(o=d,(h+=1)<n&&(m=c(d=-e[h]))),(b=(i=x)-(y=(w=o+i)-o))&&(s[a++]=b),x=M-((g=M+w)-(p=g-M))+(w-p),M=g;for(;l<r;)(b=(i=x)-(y=(w=(o=u)+i)-o))&&(s[a++]=b),x=M-((g=M+w)-(p=g-M))+(w-p),M=g,(l+=1)<r&&(u=t[l]);for(;h<n;)(b=(i=x)-(y=(w=(o=d)+i)-o))&&(s[a++]=b),x=M-((g=M+w)-(p=g-M))+(w-p),M=g,(h+=1)<n&&(d=-e[h]);return x&&(s[a++]=x),M&&(s[a++]=M),a||(s[a++]=0),s.length=a,s}!function(t){var e=twoProduct_1,r=robustSum,n=robustScale,o=robustDiff;function i(t,e,r,n){return function(r,o,i){var s=t(t(e(o[1],i[0]),e(-i[1],o[0])),t(e(r[1],o[0]),e(-o[1],r[0]))),a=t(e(r[1],i[0]),e(-i[1],r[0])),l=n(s,a);return l[l.length-1]}}function s(t,e,r,n){return function(o,i,s,a){var l=t(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),i[2]),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),a[2]))),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),a[2])))),h=t(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),a[2]))),t(r(t(e(i[1],s[0]),e(-s[1],i[0])),o[2]),t(r(t(e(o[1],s[0]),e(-s[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),s[2])))),c=n(l,h);return c[c.length-1]}}function a(t,e,r,n){return function(o,i,s,a,l){var h=t(t(t(r(t(r(t(e(a[1],l[0]),e(-l[1],a[0])),s[2]),t(r(t(e(s[1],l[0]),e(-l[1],s[0])),-a[2]),r(t(e(s[1],a[0]),e(-a[1],s[0])),l[2]))),i[3]),t(r(t(r(t(e(a[1],l[0]),e(-l[1],a[0])),i[2]),t(r(t(e(i[1],l[0]),e(-l[1],i[0])),-a[2]),r(t(e(i[1],a[0]),e(-a[1],i[0])),l[2]))),-s[3]),r(t(r(t(e(s[1],l[0]),e(-l[1],s[0])),i[2]),t(r(t(e(i[1],l[0]),e(-l[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),l[2]))),a[3]))),t(r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),i[2]),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),a[2]))),-l[3]),t(r(t(r(t(e(a[1],l[0]),e(-l[1],a[0])),i[2]),t(r(t(e(i[1],l[0]),e(-l[1],i[0])),-a[2]),r(t(e(i[1],a[0]),e(-a[1],i[0])),l[2]))),o[3]),r(t(r(t(e(a[1],l[0]),e(-l[1],a[0])),o[2]),t(r(t(e(o[1],l[0]),e(-l[1],o[0])),-a[2]),r(t(e(o[1],a[0]),e(-a[1],o[0])),l[2]))),-i[3])))),t(t(r(t(r(t(e(i[1],l[0]),e(-l[1],i[0])),o[2]),t(r(t(e(o[1],l[0]),e(-l[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),l[2]))),a[3]),t(r(t(r(t(e(i[1],a[0]),e(-a[1],i[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),a[2]))),-l[3]),r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),i[2]),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),a[2]))),o[3]))),t(r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),a[2]))),-i[3]),t(r(t(r(t(e(i[1],a[0]),e(-a[1],i[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),a[2]))),s[3]),r(t(r(t(e(i[1],s[0]),e(-s[1],i[0])),o[2]),t(r(t(e(o[1],s[0]),e(-s[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),s[2]))),-a[3]))))),c=t(t(t(r(t(r(t(e(a[1],l[0]),e(-l[1],a[0])),s[2]),t(r(t(e(s[1],l[0]),e(-l[1],s[0])),-a[2]),r(t(e(s[1],a[0]),e(-a[1],s[0])),l[2]))),o[3]),r(t(r(t(e(a[1],l[0]),e(-l[1],a[0])),o[2]),t(r(t(e(o[1],l[0]),e(-l[1],o[0])),-a[2]),r(t(e(o[1],a[0]),e(-a[1],o[0])),l[2]))),-s[3])),t(r(t(r(t(e(s[1],l[0]),e(-l[1],s[0])),o[2]),t(r(t(e(o[1],l[0]),e(-l[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),l[2]))),a[3]),r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),a[2]))),-l[3]))),t(t(r(t(r(t(e(s[1],l[0]),e(-l[1],s[0])),i[2]),t(r(t(e(i[1],l[0]),e(-l[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),l[2]))),o[3]),r(t(r(t(e(s[1],l[0]),e(-l[1],s[0])),o[2]),t(r(t(e(o[1],l[0]),e(-l[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),l[2]))),-i[3])),t(r(t(r(t(e(i[1],l[0]),e(-l[1],i[0])),o[2]),t(r(t(e(o[1],l[0]),e(-l[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),l[2]))),s[3]),r(t(r(t(e(i[1],s[0]),e(-s[1],i[0])),o[2]),t(r(t(e(o[1],s[0]),e(-s[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),s[2]))),-l[3])))),u=n(h,c);return u[u.length-1]}}function l(t){return(3===t?i:4===t?s:a)(r,e,n,o)}var h=l(3),c=l(4),u=[function(){return 0},function(){return 0},function(t,e){return e[0]-t[0]},function(t,e,r){var n,o=(t[1]-r[1])*(e[0]-r[0]),i=(t[0]-r[0])*(e[1]-r[1]),s=o-i;if(o>0){if(i<=0)return s;n=o+i}else{if(!(o<0))return s;if(i>=0)return s;n=-(o+i)}var a=33306690738754716e-32*n;return s>=a||s<=-a?s:h(t,e,r)},function(t,e,r,n){var o=t[0]-n[0],i=e[0]-n[0],s=r[0]-n[0],a=t[1]-n[1],l=e[1]-n[1],h=r[1]-n[1],u=t[2]-n[2],f=e[2]-n[2],d=r[2]-n[2],m=i*h,g=s*l,p=s*a,w=o*h,y=o*l,b=i*a,x=u*(m-g)+f*(p-w)+d*(y-b),M=7771561172376103e-31*((Math.abs(m)+Math.abs(g))*Math.abs(u)+(Math.abs(p)+Math.abs(w))*Math.abs(f)+(Math.abs(y)+Math.abs(b))*Math.abs(d));return x>M||-x>M?x:c(t,e,r,n)}];function f(t){var e=u[t.length];return e||(e=u[t.length]=l(t.length)),e.apply(void 0,t)}function d(t,e,r,n,o,i,s){return function(e,r,a,l,h){switch(arguments.length){case 0:case 1:return 0;case 2:return n(e,r);case 3:return o(e,r,a);case 4:return i(e,r,a,l);case 5:return s(e,r,a,l,h)}for(var c=new Array(arguments.length),u=0;u<arguments.length;++u)c[u]=arguments[u];return t(c)}}!function(){for(;u.length<=5;)u.push(l(u.length));t.exports=d.apply(void 0,[f].concat(u));for(var e=0;e<=5;++e)t.exports[e]=u[e]}()}(orientation);var orientationExports=orientation.exports,robustPnp=robustPointInPolygon,orient=orientationExports;function robustPointInPolygon(t,e){for(var r=e[0],n=e[1],o=t.length,i=1,s=o,a=0,l=o-1;a<s;l=a++){var h=t[a],c=t[l],u=h[1],f=c[1];if(f<u){if(f<n&&n<u){if(0===(d=orient(h,c,e)))return 0;i^=0<d|0}else if(n===u){if(u<t[(a+1)%o][1]){if(0===(d=orient(h,c,e)))return 0;i^=0<d|0}}}else if(u<f){if(u<n&&n<f){if(0===(d=orient(h,c,e)))return 0;i^=d<0|0}else if(n===u){if(t[(a+1)%o][1]<u){var d;if(0===(d=orient(h,c,e)))return 0;i^=d<0|0}}}else if(n===u){var m=Math.min(h[0],c[0]),g=Math.max(h[0],c[0]);if(0===a){for(;l>0;){var p=(l+o-1)%o;if((b=t[p])[1]!==n)break;var w=b[0];m=Math.min(m,w),g=Math.max(g,w),l=p}if(0===l)return m<=r&&r<=g?0:1;s=l+1}for(var y=t[(l+o-1)%o][1];a+1<s;){var b;if((b=t[a+1])[1]!==n)break;w=b[0];m=Math.min(m,w),g=Math.max(g,w),a+=1}if(m<=r&&r<=g)return 0;var x=t[(a+1)%o][1];r<m&&y<n!=x<n&&(i^=1)}}return 2*i-1}var robustPointInPolygon$1=getDefaultExportFromCjs(robustPnp);function arrayPointsToObjects(t){const e=[];for(const r of t)e.push([r.column,r.row]);return e}function drawPolygonOnImage(t,e,r={}){const{fillColor:n,origin:o={column:0,row:0},...i}=r;checkProcessable$1(t,{bitDepth:[8,16]});const s=getOutputImage(t,r,{clone:!0});if(void 0===n)return s.drawPolyline([...e,e[0]],{origin:o,...i});{validateColor(n,s);const t=arrayPointsToObjects(deleteDuplicates(e));for(let e=0;e<s.height;e++)for(let r=0;r<s.width;r++)-1===robustPointInPolygon$1(t,[r,e])&&setBlendedVisiblePixel(s,Math.round(o.column)+r,Math.round(o.row)+e,n)}return s.drawPolyline([...e,e[0]],{origin:o,...i})}function drawPolygonOnMask(t,e,r={}){const{filled:n=!1,origin:o={column:0,row:0},...i}=r,s=maskToOutputMask(t,r,{clone:!0});if(!n)return s.drawPolyline([...e,e[0]],{origin:o,...i});const a=arrayPointsToObjects(deleteDuplicates(e));for(let t=0;t<s.height;t++)for(let e=0;e<s.width;e++)-1===robustPointInPolygon$1(a,[e,t])&&s.setBit(Math.round(o.column)+e,Math.round(o.row)+t,1);return s.drawPolyline([...e,e[0]],{origin:o,...i})}function drawRectangle(t,e={}){const{width:r=t.width,height:n=t.height,origin:o={column:0,row:0},strokeColor:i=getDefaultColor(t),fillColor:s}=e,a=Math.round(r),l=Math.round(n),h=Math.round(o.column),c=Math.round(o.row);let u;t instanceof Image?(checkProcessable$1(t,{bitDepth:[8,16]}),validateColor(i,t),s&&validateColor(s,t),u=getOutputImage(t,e,{clone:!0})):u=maskToOutputMask(t,e,{clone:!0});for(let t=h;t<h+a;t++)setBlendedVisiblePixel(u,t,c,i),setBlendedVisiblePixel(u,t,c+l-1,i);for(let t=c+1;t<c+l-1;t++)setBlendedVisiblePixel(u,h,t,i),setBlendedVisiblePixel(u,h+a-1,t,i);if(s)for(let t=c+1;t<c+l-1;t++)for(let e=h+1;e<h+a-1;e++)setBlendedVisiblePixel(u,e,t,s);return u}function drawPoints(t,e,r={}){const{color:n=getDefaultColor(t),origin:o={row:0,column:0}}=r;let i;t instanceof Image?(i=getOutputImage(t,r,{clone:!0}),validateColor(n,i)):i=maskToOutputMask(t,r,{clone:!0}),checkProcessable$1(i,{bitDepth:[1,8,16]});for(const t of e)setBlendedVisiblePixel(i,Math.round(o.column+t.column),Math.round(o.row+t.row),n);return i}function drawMarker(t,e,r){const n=getOutputImage(t,r,{clone:!0}),{strokeColor:o=getDefaultColor(n),fillColor:i,shape:s="cross",size:a=1}=r,l=Math.round(a);if(validateColor(o,n),i&&validateColor(i,n),checkProcessable$1(n,{bitDepth:[8,16]}),"circle"===s&&n.drawCircle(e,l,{strokeColor:o,fillColor:i,out:n}),"triangle"===s){const t=[{row:e.row-l,column:e.column},{row:e.row,column:e.column+l},{row:e.row,column:e.column-l}];n.drawPolygon(t,{strokeColor:o,fillColor:i,out:n})}if("cross"===s&&(n.drawLine({row:e.row-l,column:e.column},{row:e.row+l,column:e.column},{strokeColor:o,out:n}),n.drawLine({row:e.row,column:e.column-l},{row:e.row,column:e.column+l},{strokeColor:o,out:n})),"square"===s){const t={row:e.row-(l-1)/2,column:e.column-(l-1)/2};n.drawRectangle({origin:t,width:l,height:l,strokeColor:o,fillColor:i,out:n})}return n}function drawMarkers(t,e,r={}){const n=getOutputImage(t,r,{clone:!0});for(const t of e)drawMarker(n,t,{...r,out:n});return n}function and(t,e,r){const n=maskToOutputMask(t,r);if(t.width!==e.width||t.height!==e.height)throw new RangeError("both masks must have the same size");for(let r=0;r<n.size;r++)t.getBitByIndex(r)&&e.getBitByIndex(r)?n.setBitByIndex(r,1):n.setBitByIndex(r,0);return n}function checkSize(t){if(!Number.isInteger(t)||t<1)throw new TypeError(`size must be a positive integer. Got ${t}`)}function checkKernel$1(t){if(0===t.length||t.length%2!=1)throw new RangeError(`kernel must have an odd positive length. Got ${t.length}`)}function checkBorderType(t){if("CONSTANT"!==t&&"CUT"!==t)throw new RangeError(`unexpected border type: ${t}`)}function checkInputLength(t,e){if(t!==e)throw new RangeError(`input length (${t}) does not match setup size (${e})`)}function createArray(t){const e=[];for(var r=0;r<t;r++)e.push(0);return e}class DirectConvolution{constructor(t,e,r="CONSTANT"){checkSize(t),checkKernel$1(e),checkBorderType(r),this.size=t,this.kernelOffset=(e.length-1)/2,this.outputSize="CONSTANT"===r?t:t-2*this.kernelOffset,this.output=createArray(this.outputSize),this.kernel=e,this.kernelSize=e.length,this.borderType=r}convolve(t){return checkInputLength(t.length,this.size),this.output.fill(0),"CONSTANT"===this.borderType?this._convolutionBorder0(t):this._convolutionBorderCut(t),this.output}_convolutionBorder0(t){for(let e=0;e<this.size;e++)for(let r=0;r<this.kernelSize;r++)this.output[e]+=interpolateInput(t,e-this.kernelOffset+r)*this.kernel[r]}_convolutionBorderCut(t){for(let e=this.kernelOffset;e<this.size-this.kernelOffset;e++){const r=e-this.kernelOffset;for(let e=0;e<this.kernelSize;e++)this.output[r]+=t[r+e]*this.kernel[e]}}}function interpolateInput(t,e){return e<0||e>=t.length?0:t[e]}const BorderType$1={CUT:"CUT"},BorderType={CONSTANT:"constant",REPLICATE:"replicate",REFLECT:"reflect",WRAP:"wrap",REFLECT_101:"reflect101"};function getBorderInterpolation(t,e){return"number"==typeof e&&(e=new Array(4).fill(e)),M$2(t).with("constant",()=>getInterpolateConstant(e)).with("replicate",()=>interpolateReplicate).with("reflect",()=>interpolateReflect).with("reflect101",()=>interpolateReflect101).with("wrap",()=>interpolateWrap).exhaustive()}function checkRange(t,e){if(t<=0-e||t>=e+e-1)throw new RangeError("border must be smaller than the original image")}function getInterpolateConstant(t){return function(e,r,n,o){const i=interpolateConstantPoint(e,o.width),s=interpolateConstantPoint(r,o.height);return-1===i||-1===s?t[n]:o.getValue(i,s,n)}}function interpolateConstantPoint(t,e){return t>=0&&t<e?t:-1}function interpolateReplicate(t,e,r,n){return n.getValue(interpolateReplicatePoint(t,n.width),interpolateReplicatePoint(e,n.height),r)}function interpolateReplicatePoint(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?0:e-1)}function interpolateReflect(t,e,r,n){return n.getValue(interpolateReflectPoint(t,n.width),interpolateReflectPoint(e,n.height),r)}function interpolateReflectPoint(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?-1-t:e+e-1-t)}function interpolateWrap(t,e,r,n){return n.getValue(interpolateWrapPoint(t,n.width),interpolateWrapPoint(e,n.height),r)}function interpolateWrapPoint(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?e+t:t-e)}function interpolateReflect101(t,e,r,n){return n.getValue(interpolateReflect101Point(t,n.width),interpolateReflect101Point(e,n.height),r)}function interpolateReflect101Point(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?0-t:e+e-2-t)}function extendBorders(t,e){const{horizontal:r,vertical:n,borderType:o="reflect101",borderValue:i=getDefaultColor(t)}=e;Array.isArray(i)&&validateColor(i,t);const s=getBorderInterpolation(o,i),a=Image.createFrom(t,{width:t.width+2*r,height:t.height+2*n});t.copyTo(a,{origin:{column:r,row:n},out:a});for(let e=0;e<n;e++)for(let o=0;o<a.width;o++)for(let i=0;i<t.channels;i++){const l=s(o-r,e-n,i,t);a.setValue(o,e,i,l)}for(let e=a.height-n;e<a.height;e++)for(let o=0;o<a.width;o++)for(let i=0;i<t.channels;i++){const l=s(o-r,e-n,i,t);a.setValue(o,e,i,l)}for(let e=n;e<a.height-n;e++)for(let o=0;o<r;o++)for(let i=0;i<t.channels;i++){const l=s(o-r,e-n,i,t);a.setValue(o,e,i,l)}for(let e=n;e<a.height-n;e++)for(let o=a.width-r;o<a.width;o++)for(let i=0;i<t.channels;i++){const l=s(o-r,e-n,i,t);a.setValue(o,e,i,l)}return a}function round(t){const e=Math.trunc(t),r=t-e;return r<.5?e:r>.5?e+1:e%2==0?e:e+1}function directConvolution(t,e,r={}){const{borderType:n="reflect101",borderValue:o=0}=r,i=rawDirectConvolution(t,e,{borderType:n,borderValue:o}),s=getOutputImage(t,r),a=getClamp(s);for(let e=0;e<t.size;e++)for(let r=0;r<t.channels;r++){const n=round(a(i[e*t.channels+r]));s.setValueByIndex(e,r,n)}return s}function rawDirectConvolution(t,e,r={}){const{borderType:n="reflect101",borderValue:o=getDefaultColor(t)}=r;Array.isArray(o)&&validateColor(o,t);const i=getBorderInterpolation(n,o),s=new Float64Array(t.size*t.channels);for(let r=0;r<t.channels;r++)for(let n=0;n<t.height;n++)for(let o=0;o<t.width;o++){s[getIndex(o,n,t,r)]=computeConvolutionValue(o,n,r,t,e,i,{returnRawValue:!0})}return s}function separableConvolution(t,e,r,n={}){const{normalize:o,borderType:i="reflect101",borderValue:s=0}=n;o&&([e,r]=normalizeSeparatedKernel(e,r));const a=extendBorders(t,{horizontal:(e.length-1)/2,vertical:(r.length-1)/2,borderType:i,borderValue:s}),l=Image.createFrom(t),h=getClamp(l),c=new DirectConvolution(a.width,e,BorderType$1.CUT),u=new DirectConvolution(a.height,r,BorderType$1.CUT),f=new Float64Array(a.width),d=new Float64Array(a.height),m=new Float64Array(t.width*a.height);for(let e=0;e<a.channels;e++){for(let r=0;r<a.height;r++){for(let t=0;t<a.width;t++)f[t]=a.getValue(t,r,e);const n=c.convolve(f);for(let e=0;e<t.width;e++)m[r*t.width+e]=n[e]}for(let r=0;r<t.width;r++){for(let e=0;e<a.height;e++)d[e]=m[e*t.width+r];const n=u.convolve(d);for(let o=0;o<t.height;o++)l.setValue(r,o,e,round(h(n[o])))}}return l}function computeConvolutionValue(t,e,r,n,o,i,s={}){let{clamp:a}=s;const{returnRawValue:l=!1}=s;l&&(a=void 0);let h=0;const c=o[0].length,u=o.length,f=(c-1)/2,d=(u-1)/2;for(let s=0;s<u;s++)for(let a=0;a<c;a++){h+=o[s][a]*i(t+a-f,e+s-d,r,n)}return a?round(a(h)):h}function normalizeSeparatedKernel(t,e){const r=t.reduce((t,e)=>t+e,0)*e.reduce((t,e)=>t+e,0);if(r<0)throw new RangeError("this separated kernel cannot be normalized");const n=1/Math.sqrt(Math.abs(r));return[t.map(t=>t*n),e.map(t=>t*n)]}function blur(t,e){const{width:r,height:n}=e;if(r<1||r%2==0)throw new RangeError(`Invalid property "width". Must be an odd number greater than 0. Received ${r}.`);if(n<1||n%2==0)throw new RangeError(`Invalid property "height". Must be an odd number greater than 0. Received ${n}.`);return separableConvolution(t,new Array(r).fill(1),new Array(n).fill(1),{normalize:!0,...e})}const DerivativeFilter={SOBEL:"sobel",SCHARR:"scharr",PREWITT:"prewitt"};function derivativeFilter(t,e={}){const{filter:r="sobel"}=e,n=M$2(r).with("sobel",()=>({kernelX:SOBEL_X,kernelY:SOBEL_Y})).with("scharr",()=>({kernelX:SCHARR_X,kernelY:SCHARR_Y})).with("prewitt",()=>({kernelX:PREWITT_X,kernelY:PREWITT_Y})).exhaustive();return t.gradientFilter({...n,...e})}const ImageColorModel={GREY:"GREY",GREYA:"GREYA",RGB:"RGB",RGBA:"RGBA",BINARY:"BINARY"},colorModels={GREY:{components:1,alpha:!1,channels:1},GREYA:{components:1,alpha:!0,channels:2},RGB:{components:3,alpha:!1,channels:3},RGBA:{components:3,alpha:!0,channels:4},BINARY:{components:1,alpha:!1,channels:1}};function increaseContrast(t,e={}){const{uniform:r=!1}=e;checkProcessable$1(t,{bitDepth:[8,16]});const n=t.minMax();let o=n.min,i=n.max;if(r){let t=-1,e=-1;for(let r=0;r<n.max.length;r++){const o=n.max[r]-n.min[r];o>e&&(t=r,e=o)}o=n.min[t],i=n.max[t]}let s=new Array(t.components).fill(0).map((t,e)=>e);return t.colorModel===ImageColorModel.GREYA?s=[0]:t.colorModel===ImageColorModel.RGBA&&(s=[0,1,2]),t.level({inputMin:o,inputMax:i,outputMin:0,outputMax:t.maxValue,channels:s,...e})}function gaussianBlur(t,e){if("sigma"in e){const{sigma:r,size:n=getSize$1(r),borderType:o}=e,i=getKernel(getRadius(n),r);return separableConvolution(t,i,i,{borderType:o})}if("sigmaX"in e&&"sigmaY"in e){const{sigmaX:r,sigmaY:n,sizeX:o=getSize$1(r),sizeY:i=getSize$1(n),borderType:s}=e,a=getRadius(o),l=getRadius(i);return separableConvolution(t,getKernel(a,r),getKernel(l,n),{borderType:s})}throw new TypeError("you must either define sigma or sigmaX and sigmaY in the options argument")}function getKernel(t,e){const r=2*t+1,n=new Array(r),o=e||.3*(.5*(r-1)-1)+.8,i=-.5/(o*o);let s=0;for(let e=0;e<r;e++){const r=e-t,o=Math.exp(i*r*r);n[e]=o,s+=o}for(let t=0;t<r;t++)n[t]/=s;return n}function getSize$1(t){return 2*Math.ceil(2*t)+1}function gradientFilter(t,e){const{borderType:r="replicate",borderValue:n=0}=e;if(checkProcessable$1(t,{bitDepth:[8,16],colorModel:"GREY"}),"kernelX"in e&&"kernelY"in e){const{kernelX:o,kernelY:i}=e,s=t.rawDirectConvolution(o,{borderType:r,borderValue:n}),a=t.rawDirectConvolution(i,{borderType:r,borderValue:n}),l=new Image(t.width,t.height,{colorModel:"GREY"});for(let e=0;e<t.size;e++)l.setValueByIndex(e,0,Math.hypot(s[e],a[e]));return l}if("kernelX"in e)return t.directConvolution(e.kernelX,{borderType:r,borderValue:n});if("kernelY"in e)return t.directConvolution(e.kernelY,{borderType:r,borderValue:n});throw new TypeError("kernelX and KernelY are not defined")}function hypotenuse(t,e,r={}){const{channels:n=new Array(t.components).fill(0).map((t,e)=>e)}=r;if(checkProcessable$1(t,{bitDepth:[8,16]}),t.width!==e.width||t.height!==e.height)throw new RangeError("both images must have the same size");if(t.alpha!==e.alpha||t.bitDepth!==e.bitDepth)throw new RangeError("both images must have the same alpha and bitDepth");if(t.channels!==e.channels)throw new RangeError("both images must have the same number of channels");validateChannels(n,t);const o=getOutputImage(t,{},{clone:!0});for(const r of n)for(let n=0;n<t.size;n++){const i=Math.hypot(t.getValueByIndex(n,r),e.getValueByIndex(n,r));o.setValueByIndex(n,r,Math.min(i,o.maxValue))}return o}function convertColor(t,e,r={}){const n=new Map([["GREY",["GREYA","RGB","RGBA"]],["GREYA",["GREY","RGB","RGBA"]],["RGB",["GREY","GREYA","RGBA"]],["RGBA",["GREY","GREYA","RGB"]],["BINARY",["GREY","RGB","RGBA"]]]);if(t.colorModel===e&&"BINARY"!==e)return getOutputImage(t,{out:r.out},{clone:!0});const o=n.get(t.colorModel);if(!o?.includes(e))throw new RangeError(`conversion from ${t.colorModel} to ${e} not implemented`);if(t instanceof Image){const n=getOutputImage(t,r,{newParameters:{colorModel:e}});return"GREY"!==t.colorModel&&"GREYA"!==t.colorModel||convertGreyToAny(t,n),"RGB"!==t.colorModel&&"RGBA"!==t.colorModel||("RGB"===e||"RGBA"===e?convertRgbToRgb(t,n):convertRgbToGrey(t,n)),!t.alpha&&n.alpha&&n.fillAlpha(n.maxValue),t.alpha&&n.alpha&&copyAlpha(t,n),n}if("GREY"===e){const e=maskToOutputImage(t,r);return convertBinaryToGrey(t,e),e}{const r=new Image(t.width,t.height,{colorModel:e});return convertBinaryToRgb(t,r),r}}function copyAlpha(t,e){if(t.size!==e.size)throw new RangeError("source and destination have different sizes");if(!t.alpha)throw new RangeError("source image does not have alpha");if(!e.alpha)throw new RangeError("destination does not have alpha");for(let r=0;r<e.size;r++)e.setValueByIndex(r,e.channels-1,t.getValueByIndex(r,t.channels-1))}function convertGreyToAny(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<e.components;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,0))}function convertRgbToRgb(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<3;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,n))}function convertRgbToGrey(t,e){for(let r=0;r<t.size;r++){const n=t.getValueByIndex(r,0),o=t.getValueByIndex(r,1),i=t.getValueByIndex(r,2);e.setValueByIndex(r,0,Math.round(.299*n+.587*o+.114*i))}}function convertBinaryToGrey(t,e){for(let r=0;r<t.size;r++)e.setValueByIndex(r,0,t.getBitByIndex(r)?e.maxValue:0)}function convertBinaryToRgb(t,e){const r=new Array(e.components).fill(0),n=new Array(e.components).fill(e.maxValue);e.alpha&&(r.push(e.maxValue),n.push(e.maxValue));for(let o=0;o<t.size;o++)e.setPixelByIndex(o,t.getBitByIndex(o)?n:r)}function convertBitDepth(t,e,r={}){if(t.bitDepth===e)return getOutputImage(t,r,{clone:!0});if(8!==e&&16!==e)throw new RangeError(`This image bit depth is not supported: ${e}`);const n=getOutputImage(t,r,{clone:!1,newParameters:{bitDepth:e,colorModel:t.colorModel}});return 8===e?convertToUint8(t,n):convertToUint16(t,n)}function convertToUint16(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<e.channels;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,n)<<8);return e}function convertToUint8(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<e.channels;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,n)>>8);return e}function checkPointIsInteger(t,e="Point"){if(!Number.isInteger(t.row)||!Number.isInteger(t.column))throw new TypeError(`${e} row and column must be integers`)}function extract(t,e,r={}){const{origin:n={row:0,column:0}}=r,{row:o,column:i}=n;assert(o<t.height&&i<t.width&&o+e.width>0&&i+e.height>0,"extract: image and mask have no overlap"),checkPointIsInteger(n,"Origin");const s=Math.min(t.width,e.width+i)-Math.max(0,i),a=Math.min(t.height,e.height+o)-Math.max(0,o);let l=new Image(s,a,{colorModel:t.colorModel,origin:n});l.alpha&&(l=l.fillAlpha(0));const h=Math.max(0,o),c=Math.max(0,i),u=o<0?-o:0,f=i<0?-i:0;for(let r=0;r<a;r++)for(let n=0;n<s;n++)e.getBit(f+n,u+r)&&l.setPixel(n,r,t.getPixel(c+n,h+r));return l}function merge(t){const e=t.length;let r;switch(e){case 2:r="GREYA";break;case 3:r="RGB";break;case 4:r="RGBA";break;default:throw new RangeError(`merge expects an array of two to four images. Received ${e}`)}const n=t[0];if(1!==n.channels)throw new RangeError(`each image must have one channel. Received ${n.channels}`);for(let r=1;r<e;r++){const e=t[r];if(1!==e.channels)throw new RangeError(`each image must have one channel. Received ${e.channels}`);if(e.width!==n.width||e.height!==n.height||e.bitDepth!==n.bitDepth)throw new RangeError("all images must have the same width, height and bitDepth")}const o=Image.createFrom(n,{colorModel:r});for(let r=0;r<e;r++){const e=t[r];for(let t=0;t<o.size;t++)o.setValueByIndex(t,r,e.getValueByIndex(t,0))}return o}function split(t){const e=[];for(let r=0;r<t.channels;r++){const n=Image.createFrom(t,{colorModel:"GREY"});for(let e=0;e<n.size;e++)n.setValueByIndex(e,0,t.getValueByIndex(e,r));e.push(n)}return e}function huang(t){let e=0;for(let r=0;r<t.length;r++)if(0!==t[r]){e=r;break}let r=t.length-1;for(let n=t.length-1;n>=e;n--)if(0!==t[n]){r=n;break}const n=1/(r-e),o=new Array(t.length);let i=0,s=0;for(let r=e;r<t.length;r++)i+=r*t[r],s+=t[r],o[r]=i/s;const a=new Array(t.length);i=0,s=0;for(let e=r;e>0;e--)i+=e*t[e],s+=t[e],a[e-1]=i/s;let l=-1,h=Number.MAX_VALUE;for(let e=0;e<t.length;e++){let r,i=0;for(let s=0;s<=e;s++)r=1/(1+n*Math.abs(s-o[e])),r<1e-6||r>.999999||(i+=t[s]*(-r*Math.log(r)-(1-r)*Math.log(1-r)));for(let o=e+1;o<t.length;o++)r=1/(1+n*Math.abs(o-a[e])),r<1e-6||r>.999999||(i+=t[o]*(-r*Math.log(r)-(1-r)*Math.log(1-r)));i<h&&(h=i,l=e)}return l}function intermodes(t){const e=t.slice();let r=0;for(;!bimodalTest$1(e);){let n=0,o=0,i=e[0];for(let r=0;r<t.length-1;r++)n=o,o=i,i=e[r+1],e[r]=(n+o+i)/3;e[t.length-1]=(o+i)/3,r++,assert(r<1e3,"Intermodes threshold not found after 1000 iterations")}let n=0;for(let r=1;r<t.length-1;r++)e[r-1]<e[r]&&e[r+1]<e[r]&&(n+=r);return Math.floor(n/2)}function bimodalTest$1(t){let e=!1,r=0;for(let e=1;e<t.length-1;e++)if(t[e-1]<t[e]&&t[e+1]<t[e]&&(r++,r>2))return!1;return 2===r&&(e=!0),e}function isodata(t){let e,r,n,o,i=0;for(let e=1;e<t.length;e++)if(t[e]>0){i=e+1;break}for(;;){e=0,n=0;for(let r=0;r<i;r++)n+=t[r],e+=t[r]*r;o=0,r=0;for(let e=i+1;e<t.length;e++)r+=t[e],o+=t[e]*e;if(n>0&&r>0&&(e/=n,o/=r,i===Math.round((e+o)/2)))break;i++,assert(i<=t.length-2,"Threshold not found")}return i}function li(t,e){let r,n,o,i,s,a,l,h,c,u;let f;u=0;for(let e=0;e<t.length;e++)u+=e*t[e];u/=e,l=u;do{a=l,r=a+.5|0,n=0,i=0;for(let e=0;e<=r;e++)n+=e*t[e],i+=t[e];h=0===i?0:n/i,o=0,s=0;for(let e=r+1;e<t.length;e++)o+=e*t[e],s+=t[e];c=0===s?0:o/s,f=(h-c)/(Math.log(h)-Math.log(c)),l=f<-Number.EPSILON?f-.5|0:f+.5|0}while(Math.abs(l-a)>.5);return r}function maxEntropy(t,e){const r=new Array(t.length);for(let n=0;n<t.length;n++)r[n]=t[n]/e;const n=new Array(t.length),o=new Array(t.length);n[0]=r[0],o[0]=1-n[0];for(let e=1;e<t.length;e++)n[e]=n[e-1]+r[e],o[e]=1-n[e];let i=0;for(let e=0;e<t.length;e++)if(Math.abs(n[e])>=Number.EPSILON){i=e;break}let s=t.length-1;for(let e=t.length-1;e>=i;e--)if(Math.abs(o[e])>=Number.EPSILON){s=e;break}let a,l,h,c=-1,u=Number.MIN_VALUE;for(let e=i;e<=s;e++){l=0;for(let o=0;o<=e;o++)0!==t[o]&&(l-=r[o]/n[e]*Math.log(r[o]/n[e]));h=0;for(let n=e+1;n<t.length;n++)0!==t[n]&&(h-=r[n]/o[e]*Math.log(r[n]/o[e]));a=l+h,u<a&&(u=a,c=e)}return c}function mean$1(t,e){let r=0;for(let e=0;e<t.length;e++)r+=e*t[e];return Math.floor(r/e)}function minError(t,e){let r,n,o,i,s,a,l,h,c,u,f,d,m=-2,g=0;for(let e=0;e<t.length;e++)g+=e*t[e];for(g/=e,r=g;r!==m;){const e=sumA(t,r),g=sumA(t,t.length-1),p=sumB(t,r),w=sumB(t,t.length-1),y=sumC(t,r);if(n=p/e,o=(w-p)/(g-e),i=e/g,s=(g-e)/g,a=y/e-n*n,l=(sumC(t,t.length-1)-y)/(g-e)-o*o,h=1/a-1/l,c=n/a-o/l,u=n*n/a-o*o/l+Math.log10(a*(s*s)/(l*(i*i))),f=c*c-h*u,f<0)return r;m=r,d=(c+Math.sqrt(f))/h,r=Number.isNaN(d)?m:Math.floor(d)}return r}function sumA(t,e){let r=0;for(let n=0;n<=e;n++)r+=t[n];return r}function sumB(t,e){let r=0;for(let n=0;n<=e;n++)r+=n*t[n];return r}function sumC(t,e){let r=0;for(let n=0;n<=e;n++)r+=n*n*t[n];return r}function minimum(t){if(t.length<2)return 0;let e=0,r=-1,n=-1,o=new Array(t.length);for(let e=0;e<t.length;e++)o[e]=t[e],t[e]>0&&(n=e);for(;!bimodalTest(o);)if(o=smoothed(o),e++,e>1e4)return r;for(let t=1;t<n;t++)if(o[t-1]>o[t]&&o[t+1]>=o[t]){r=t;break}return r}function smoothed(t){const e=new Array(t.length);for(let r=1;r<t.length-1;r++)e[r]=(t[r-1]+t[r]+t[r+1])/3;return e[0]=(t[0]+t[1])/3,e[t.length-1]=(t.at(-2)+t.at(-1))/3,e}function bimodalTest(t){const e=t.length;let r=!1,n=0;for(let r=1;r<e-1;r++)if(t[r-1]<t[r]&&t[r+1]<t[r]&&(n++,n>2))return!1;return 2===n&&(r=!0),r}function moments(t,e){let r=0,n=0,o=0,i=0,s=-1;const a=t.length,l=[];for(let r=0;r<a;r++)l.push(t[r]/e);for(let t=0;t<a;t++)r+=t*l[t],n+=t*t*l[t],o+=t*t*t*l[t];const h=1*n-r*r,c=(-n*n+r*o)/h,u=(1*-o+n*r)/h,f=.5*(-u-Math.sqrt(u*u-4*c)),d=.5*(-u+Math.sqrt(u*u-4*c)),m=(d-r)/(d-f);for(let t=0;t<a;t++)if(i+=l[t],i>m){s=t;break}return s}function otsu(t,e){let r=0,n=0,o=0,i=0,s=0;for(let e=0;e<t.length;e++)s+=e*t[e];for(let a=0;a<t.length;a++){n+=t[a];const l=e-n;if(0===n||0===l)continue;r+=a*t[a];const h=(s-r)/l,c=n*l*(r/n-h)*(r/n-h);c>=o&&(i=a,o=c)}return i}function percentile(t){let e=-1;const r=new Array(t.length),n=partialSum(t,t.length-1);let o=1;for(let i=0;i<t.length;i++)r[i]=Math.abs(partialSum(t,i)/n-.5),r[i]<o&&(o=r[i],e=i);return e}function partialSum(t,e){let r=0;for(let n=0;n<=e;n++)r+=t[n];return r}function renyiEntropy(t,e){let r,n;const o=new Array(t.length),i=new Array(t.length),s=new Array(t.length);let a=0,l=0,h=0,c=0,u=0,f=0;for(let r=0;r<t.length;r++)o[r]=t[r]/e;i[0]=o[0],s[0]=1-i[0];for(let e=1;e<t.length;e++)i[e]=i[e-1]+o[e],s[e]=1-i[e];r=0;for(let e=0;e<t.length;e++)if(Math.abs(i[e])>=Number.EPSILON){r=e;break}n=t.length-1;for(let e=t.length-1;e>=r;e--)if(Math.abs(s[e])>=Number.EPSILON){n=e;break}for(let e=r;e<=n;e++){let r=0,n=0,d=0;for(let s=0;s<=e;s++)0!==t[s]&&(r-=o[s]/i[e]*Math.log(o[s]/i[e])),n+=Math.sqrt(o[s]/i[e]),d+=o[s]*o[s]/(i[e]*i[e]);let m=0,g=0,p=0;for(let r=e+1;r<t.length;r++)0!==t[r]&&(m-=o[r]/s[e]*Math.log(o[r]/s[e])),g+=Math.sqrt(o[r]/s[e]),p+=o[r]*o[r]/(s[e]*s[e]);const w=r+m,y=2*(n*g>0?Math.log(n*g):0),b=-1*(d*p>0?Math.log(d*p):0);w>c&&(c=w,a=e),y>u&&(u=y,l=e),b>f&&(f=b,h=e)}const d=new Uint32Array([a,l,h]);let m;d.sort(),m=Math.abs(d[0]-d[1])<=5?Math.abs(d[1]-d[2])<=5?[1,2,1]:[0,1,3]:Math.abs(d[1]-d[2])<=5?[3,1,0]:[1,2,1];const g=i[d[2]]-i[d[0]];return Math.round(d[0]*(i[d[0]]+.25*g*m[0])+.25*d[1]*g*m[1]+d[2]*(s[d[2]]+.25*g*m[2]))}function shanbhag(t,e){const r=new Array(t.length);for(let n=0;n<t.length;n++)r[n]=t[n]/e;const n=new Array(t.length),o=new Array(t.length);n[0]=r[0],o[0]=1-n[0];for(let e=1;e<t.length;e++)n[e]=n[e-1]+r[e],o[e]=1-n[e];let i=0;for(let e=0;e<t.length;e++)if(Math.abs(n[e])>=Number.EPSILON){i=e;break}let s=t.length-1;for(let e=t.length-1;e>=i;e--)if(Math.abs(o[e])>=Number.EPSILON){s=e;break}let a,l,h,c,u=-1,f=Number.MAX_VALUE;for(let e=i;e<=s;e++){h=0,a=.5/n[e];for(let t=1;t<=e;t++)h-=r[t]*Math.log(1-a*n[t-1]);h*=a,c=0,a=.5/o[e];for(let n=e+1;n<t.length;n++)c-=r[n]*Math.log(1-a*o[n]);c*=a,l=Math.abs(h-c),l<f&&(f=l,u=e)}return u}function triangle(t){let e=0,r=0,n=0,o=0;for(let r=0;r<t.length;r++)if(t[r]>0){e=r;break}e>0&&e--;for(let e=t.length-1;e>0;e--)if(t[e]>0){o=e;break}o<t.length-1&&o++;for(let e=0;e<t.length;e++)t[e]>r&&(n=e,r=t[e]);let i,s,a,l=!1;if(n-e<o-n){l=!0;let r=0,i=t.length-1;for(;r<i;){const e=t[r];t[r]=t[i],t[i]=e,r++,i--}e=t.length-1-o,n=t.length-1-n}if(e===n)return e;i=t[n],s=e-n,a=Math.hypot(i,s),i/=a,s/=a,a=i*e+s*t[e];let h=e,c=0;for(let r=e+1;r<=n;r++){const e=i*r+s*t[r]-a;e>c&&(h=r,c=e)}if(h--,l){let e=0,r=t.length-1;for(;e<r;){const n=t[e];t[e]=t[r],t[r]=n,e++,r--}return t.length-1-h}return h}function yen(t,e){const r=new Array(t.length);for(let n=0;n<t.length;n++)r[n]=t[n]/e;const n=new Array(t.length);n[0]=r[0];for(let e=1;e<t.length;e++)n[e]=n[e-1]+r[e];const o=new Array(t.length);o[0]=r[0]*r[0];for(let e=1;e<t.length;e++)o[e]=o[e-1]+r[e]*r[e];const i=new Array(t.length);i[t.length-1]=0;for(let e=t.length-2;e>=0;e--)i[e]=i[e+1]+r[e+1]*r[e+1];let s,a=-1,l=Number.MIN_VALUE;for(let e=0;e<t.length;e++)s=-1*(o[e]*i[e]>0?Math.log(o[e]*i[e]):0)+2*(n[e]*(1-n[e])>0?Math.log(n[e]*(1-n[e])):0),s>l&&(l=s,a=e);return a}const ThresholdAlgorithm={HUANG:"huang",INTERMODES:"intermodes",ISODATA:"isodata",LI:"li",MAX_ENTROPY:"maxEntropy",MEAN:"mean",MIN_ERROR:"minError",MINIMUM:"minimum",MOMENTS:"moments",OTSU:"otsu",PERCENTILE:"percentile",RENYI_ENTROPY:"renyiEntropy",SHANBHAG:"shanbhag",TRIANGLE:"triangle",YEN:"yen"};function computeThreshold(t,e={}){const{algorithm:r="otsu",slots:n}=e;if(1!==t.channels)throw new TypeError("threshold can only be computed on images with one channel");const o=t.histogram({slots:n}),i=n?2**t.bitDepth/n:1;return M$2(r).with("huang",()=>huang(o)*i).with("intermodes",()=>intermodes(o)*i).with("isodata",()=>isodata(o)*i).with("li",()=>li(o,t.size)*i).with("maxEntropy",()=>maxEntropy(o,t.size)*i).with("mean",()=>mean$1(o,t.size)*i).with("minimum",()=>minimum(o)*i).with("minError",()=>minError(o,t.size)*i).with("moments",()=>moments(o,t.size)*i).with("otsu",()=>otsu(o,t.size)*i).with("percentile",()=>percentile(o)*i).with("renyiEntropy",()=>renyiEntropy(o,t.size)*i).with("shanbhag",()=>shanbhag(o,t.size)*i).with("triangle",()=>triangle(o)*i).with("yen",()=>yen(o,t.size)*i).exhaustive()}function threshold(t,e={}){let r;if("threshold"in e){const n=e.threshold;if(n<0||n>1)throw new RangeError("threshold must be a value between 0 and 1");r=n*t.maxValue}else r=computeThreshold(t,e);const n=imageToOutputMask(t,e);for(let e=0;e<t.size;e++)n.setBitByIndex(e,t.getValueByIndex(e,0)>r?1:0);return n}function luma709(t,e,r){return 6966*t+23436*e+2366*r>>15}function luma601(t,e,r){return 9798*t+19235*e+3735*r>>15}function max$1(t,e,r){return Math.max(t,e,r)}function min(t,e,r){return Math.min(t,e,r)}function average(t,e,r){return(t+e+r)/3|0}function minmax(t,e,r){return(Math.max(t,e,r)+Math.min(t,e,r))/2}function red(t){return t}function green(t,e){return e}function blue(t,e,r){return r}function black(t,e,r,n){return Math.min(n.maxValue-t,n.maxValue-e,n.maxValue-r)}function cyan(t,e,r,n){const o=black(t,e,r,n);return(n.maxValue-t-o)/(1-o/n.maxValue)|0}function magenta(t,e,r,n){const o=black(t,e,r,n);return(n.maxValue-e-o)/(1-o/n.maxValue)|0}function yellow(t,e,r,n){const o=black(t,e,r,n);return(n.maxValue-r-o)/(1-o/n.maxValue)|0}function hue(t,e,r,n){const o=min(t,e,r),i=max$1(t,e,r);if(i===o)return 0;let s=0;const a=i-o;return i===t?s=(e-r)/a+(e<r?6:0):i===e?s=(r-t)/a+2:(assert(i===r),s=(t-e)/a+4),s/6*n.maxValue|0}function saturation(t,e,r,n){const o=min(t,e,r),i=max$1(t,e,r);return 0===i?0:(i-o)/i*n.maxValue}function lightness(t,e,r){const n=min(t,e,r);return(max$1(t,e,r)+n)/2}var greyAlgorithms=Object.freeze({__proto__:null,average:average,black:black,blue:blue,cyan:cyan,green:green,hue:hue,lightness:lightness,luma601:luma601,luma709:luma709,magenta:magenta,max:max$1,min:min,minmax:minmax,red:red,saturation:saturation,yellow:yellow});const GreyAlgorithm={LUMA_709:"luma709",LUMA_601:"luma601",MAX:"max",MIN:"min",AVERAGE:"average",MINMAX:"minmax",RED:"red",GREEN:"green",BLUE:"blue",BLACK:"black",CYAN:"cyan",MAGENTA:"magenta",YELLOW:"yellow",HUE:"hue",SATURATION:"saturation",LIGHTNESS:"lightness"};{const t=new Set(Object.values(GreyAlgorithm));for(const e of Object.keys(greyAlgorithms))assert(t.has(e),`Grey algorithm ${e} is missing in the GreyAlgorithm enum`)}function grey(t,e={}){let{keepAlpha:r=!1,mergeAlpha:n=!0}=e;const{algorithm:o="luma709"}=e;checkProcessable$1(t,{colorModel:["RGB","RGBA"]}),r=r&&t.alpha,n=n&&t.alpha,r&&(n=!1);const i=getOutputImage(t,e,{newParameters:{colorModel:r?"GREYA":"GREY"}});let s;s="function"==typeof o?o:greyAlgorithms[o];const a=getClamp(i);for(let e=0;e<t.size;e++){const o=t.getValueByIndex(e,0),l=t.getValueByIndex(e,1),h=t.getValueByIndex(e,2);let c;if(n){const r=t.getValueByIndex(e,3);c=a(s(o,l,h,t)*r/t.maxValue)}else if(c=a(s(o,l,h,t)),r){const r=t.getValueByIndex(e,3);i.setValueByIndex(e,1,r)}i.setValueByIndex(e,0,c)}return i}function copyTo(t,e,r={}){const{origin:n={column:0,row:0}}=r,{column:o,row:i}=n;if(t.colorModel!==e.colorModel)throw new RangeError("source and target must have the same color model");let s;checkPointIsInteger(n,"Origin"),s=e instanceof Image?getOutputImage(e,r,{clone:!0}):maskToOutputMask(e,r,{clone:!0});for(let r=Math.max(i,0);r<Math.min(t.height+i,e.height);r++)for(let n=Math.max(o,0);n<Math.min(t.width+o,e.width);n++){setBlendedPixel(s,n,r,t.getPixel(n-o,r-i))}return s}function crop(t,e={}){const{origin:r={row:0,column:0},width:n=t.width-r.column,height:o=t.height-r.row}=e,{column:i,row:s}=r;if(checkProcessable$1(t,{bitDepth:[8,16]}),checkPointIsInteger(r,"Origin"),!Number.isInteger(n)||!Number.isInteger(o))throw new RangeError(`Width and height (width:${n}; height:${o}) must be integers`);if(s>t.height-1||i>t.width-1)throw new RangeError(`Origin (row:${s}, column:${i}) out of range (${t.width-1}; ${t.height-1})`);if(n<=0||o<=0)throw new RangeError(`Width and height (width:${n}; height:${o}) must be positive numbers`);if(s<0||i<0)throw new RangeError(`Row and column (row:${s}, column:${i}) must be positive numbers`);if(n>t.width-i||o>t.height-s)throw new RangeError(`Size is out of range (row:${s}, column:${i}, width:${n}, height:${o})`);const a=Image.createFrom(t,{width:n,height:o});for(let e=0;e<o;e++)for(let r=0;r<n;r++)a.setPixel(r,e,t.getPixel(i+r,s+e));return a}function cropAlpha(t,e={}){checkProcessable$1(t,{alpha:!0});const{threshold:r=t.maxValue}=e,n=findLeft(t,r,t.components);if(-1===n)throw new RangeError(`could not find new dimensions. Threshold may be too high: ${r}`);const o=findTop(t,r,t.components,n),i=findBottom(t,r,t.components,n),s=findRight(t,r,t.components,n,o,i);return t.crop({origin:{column:n,row:o},width:s-n+1,height:i-o+1})}function findLeft(t,e,r){for(let n=0;n<t.width;n++)for(let o=0;o<t.height;o++)if(t.getValue(n,o,r)>=e)return n;return-1}function findTop(t,e,r,n){for(let o=0;o<t.height;o++)for(let i=n;i<t.width;i++)if(t.getValue(i,o,r)>=e)return o;return-1}function findBottom(t,e,r,n){for(let o=t.height-1;o>=0;o--)for(let i=n;i<t.width;i++)if(t.getValue(i,o,r)>=e)return o;return-1}function findRight(t,e,r,n,o,i){for(let s=t.width-1;s>=n;s--)for(let n=o;n<=i;n++)if(t.getValue(s,n,r)>=e)return s;return-1}const InterpolationType={NEAREST:"nearest",BILINEAR:"bilinear",BICUBIC:"bicubic"};function getInterpolationFunction(t){return M$2(t).with("nearest",()=>interpolateNearest).with("bilinear",()=>interpolateBilinear).with("bicubic",()=>interpolateBicubic).exhaustive()}function interpolateNearest(t,e,r,n,o){return o(e=Math.round(e),r=Math.round(r),n,t)}function interpolateBilinear(t,e,r,n,o){const i=Math.floor(e),s=Math.floor(r),a=i+1,l=s+1;if(a<t.width&&l<t.height&&i>=0&&s>=0){return round((l-r)*((a-e)*t.getValue(i,s,n)+(e-i)*t.getValue(a,s,n))+(r-s)*((a-e)*t.getValue(i,l,n)+(e-i)*t.getValue(a,l,n)))}return round((l-r)*((a-e)*o(i,s,n,t)+(e-i)*o(a,s,n,t))+(r-s)*((a-e)*o(i,l,n,t)+(e-i)*o(a,l,n,t)))}function interpolateBicubic(t,e,r,n,o,i){const s=Math.floor(e),a=Math.floor(r);if(s===e&&a===r)return o(s,a,n,t);const l=e-s,h=r-a,c=cubic(o(s-1,a-1,n,t),o(s,a-1,n,t),o(s+1,a-1,n,t),o(s+2,a-1,n,t),l),u=cubic(o(s-1,a,n,t),o(s,a,n,t),o(s+1,a,n,t),o(s+2,a,n,t),l),f=cubic(o(s-1,a+1,n,t),o(s,a+1,n,t),o(s+1,a+1,n,t),o(s+2,a+1,n,t),l),d=cubic(o(s-1,a+2,n,t),o(s,a+2,n,t),o(s+1,a+2,n,t),o(s+2,a+2,n,t),l);return round(i(cubic(c,u,f,d,h)))}function cubic(t,e,r,n,o){return e+.5*o*(r-t+o*(2*t-5*e+4*r-n+o*(3*(e-r)+n-t)))}function transform(t,e,r={}){const{borderValue:n=getDefaultColor(t),borderType:o="constant",interpolationType:i="bilinear",fullImage:s}=r;let{width:a=t.width,height:l=t.height}=r;if(Array.isArray(n)&&validateColor(n,t),!isValidMatrix(e))throw new TypeError(`transformation matrix must be 2x3 or 3x3. Received ${e.length}x${e[1].length}`);if(2===e.length&&e.push([0,0,1]),s){(e=e.map(t=>t.slice()))[0][2]=0,e[1][2]=0;const r=[t.getCoordinates("top-left"),t.getCoordinates("top-right"),t.getCoordinates("bottom-right"),t.getCoordinates("bottom-left")];r[1].column+=1,r[2].column+=1,r[2].row+=1,r[3].row+=1;const n=r.map(t=>[transformPoint(e[0],e[2],t.column,t.row),transformPoint(e[1],e[2],t.column,t.row)]),o=n.map(t=>t[0]),i=n.map(t=>t[1]),s=Math.max(...o),h=Math.max(...i),c=Math.min(...o),u=Math.min(...i),f=[(t.width-1)/2,(t.height-1)/2];a=s-c,l=h-u;const d=(a-1)/2-transformPoint(e[0],e[2],f[0],f[1]),m=(l-1)/2-transformPoint(e[1],e[2],f[0],f[1]);e[0][2]=d,e[1][2]=m,a=Math.round(a),l=Math.round(l)}r.inverse||(e=inverse(new Matrix(e)).to2DArray());const h=Image.createFrom(t,{width:a,height:l}),c=getBorderInterpolation(o,n),u=getClamp(h),f=getInterpolationFunction(i);for(let r=0;r<h.height;r++)for(let n=0;n<h.width;n++){const o=transformPoint(e[0],e[2],n,r),i=transformPoint(e[1],e[2],n,r);for(let e=0;e<h.channels;e++){const s=f(t,o,i,e,c,u);h.setValue(n,r,e,s)}}return h}function transformPoint(t,e,r,n){return(t[0]*r+t[1]*n+t[2])/(e[0]*r+e[1]*n+e[2])}function isValidMatrix(t){return 3===t.length&&3===t[0].length&&3===t[1].length&&3===t[2].length||2===t.length&&3===t[0].length&&3===t[1].length}function resize(t,e){const{interpolationType:r="bilinear",borderType:n="replicate",borderValue:o=0}=e,{width:i,height:s,xFactor:a,yFactor:l}=checkOptions(t,e);return transform(t,[[a,0,a/2],[0,l,l/2]],{interpolationType:r,borderType:n,borderValue:o,height:s,width:i})}function checkOptions(t,e){const{width:r,height:n,xFactor:o,yFactor:i,preserveAspectRatio:s=!0}=e;if(void 0===r&&void 0===n&&void 0===o&&void 0===i)throw new TypeError("at least one of the width, height, xFactor or yFactor options must be passed");let a,l;const h=getSize(r,o,t.width,s),c=getSize(n,i,t.height,s);return void 0===h?(assert(void 0!==c),a=Math.round(c*(t.width/t.height))):a=h,void 0===c?(assert(void 0!==h),l=Math.round(h*(t.height/t.width))):l=c,{width:a,height:l,xFactor:o??a/t.width,yFactor:i??l/t.height}}function getSize(t,e,r,n){if(void 0!==t){if(void 0!==e)throw new TypeError("factor and size cannot be passed together");return t}return void 0!==e?Math.round(r*e):n?void 0:r}function rotate(t,e){const r=e%180==0?t.width:t.height,n=e%180==0?t.height:t.width,o=Image.createFrom(t,{width:r,height:n});if(90===e||-270===e)for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.channels;n++)o.setValue(o.width-r-1,e,n,t.getValue(e,r,n));else if(180===e||-180===e)for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.channels;n++)o.setValue(o.width-e-1,o.height-r-1,n,t.getValue(e,r,n));else{if(270!==e&&-90!==e)throw new RangeError(`invalid angle: ${e}`);for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.channels;n++)o.setValue(r,o.height-e-1,n,t.getValue(e,r,n))}return o}function transformRotate(t,e,r={}){const{center:n="center",scale:o=1,...i}=r;let s;s="string"==typeof n?t.getCoordinates(n):n;return transform(t,getRotationMatrix(e,s,o),i)}function getRotationMatrix(t,e,r){const n=t*Math.PI/180,o=r*Math.cos(n),i=r*Math.sin(n);return[[o,i,(1-o)*e.column-i*e.row],[-i,o,i*e.column+(1-o)*e.row],[0,0,1]]}function getPerspectiveWarp(t,e={}){if(4!==t.length)throw new Error(`The array pts must have four elements, which are the four corners. Currently, pts have ${t.length} elements`);const{width:r,height:n}=e,[o,i,s,a]=order4Points(t);let l,h;if(n&&r)l=r,h=n;else{if(n||r)throw new Error(`Invalid dimensions: ${r?"`height`":"`width`"} is missing. Either provide both width and height, or omit both to auto-calculate dimensions.`);l=Math.ceil(Math.max(distance2Points(o,i),distance2Points(a,s))),h=Math.ceil(Math.max(distance2Points(o,a),distance2Points(i,s)))}const[c,u]=[0,0],[f,d]=[l-1,0],[m,g]=[l-1,h-1],[p,w]=[0,h-1],y=new Matrix([[c,u,1,0,0,0,-c*o.column,-u*o.column],[f,d,1,0,0,0,-f*i.column,-d*i.column],[m,g,1,0,0,0,-m*s.column,-g*s.column],[p,w,1,0,0,0,-p*a.column,-w*a.column],[0,0,0,c,u,1,-c*o.row,-u*o.row],[0,0,0,f,d,1,-f*i.row,-d*i.row],[0,0,0,m,g,1,-m*s.row,-g*s.row],[0,0,0,p,w,1,-p*a.row,-w*a.row]]),b=Matrix.columnVector([o.column,i.column,s.column,a.column,o.row,i.row,s.row,a.row]),x=new SingularValueDecomposition(y).solve(b).to1DArray();x.push(1);const M=[];for(let t=0;t<3;t++){const e=[];for(let r=0;r<3;r++)e.push(x[3*t+r]);M.push(e)}return{matrix:M,width:l,height:h}}function order4Points(t){let e,r,n,o,i=t[0].column,s=0;for(let e=1;e<t.length;e++)t[e].column<i&&(i=t[e].column,s=e);let a=t[(s+1)%t.length].column,l=(s+1)%t.length;for(let e=0;e<t.length;e++)t[e].column<a&&e!==s&&(a=t[e].column,l=e);return t[l].row<t[s].row?(e=t[l],o=t[s],s!==(l+1)%4?(r=t[(l+1)%4],n=t[(l+2)%4]):(r=t[(l+2)%4],n=t[(l+3)%4])):(o=t[l],e=t[s],l!==(s+1)%4?(r=t[(s+1)%4],n=t[(s+2)%4]):(r=t[(s+2)%4],n=t[(s+3)%4])),[e,r,n,o]}function distance2Points(t,e){return Math.hypot(t.column-e.column,t.row-e.row)}function rotatePoint(t,e,r){const n=Math.cos(r),o=Math.sin(r);return{column:t.column*n-t.row*o+(1-n)*e.column+e.row*o,row:t.column*o+t.row*n+(1-n)*e.row-e.column*o}}function cropRectangle(t,e,r){if(4!==e.length)throw new Error("The points array must contain 4 points");const n=getSmallestAngle(e),o={row:(e[0].row+e[2].row)/2,column:(e[0].column+e[2].column)/2},i=e.map(t=>rotatePoint(t,o,n)),[s,a,l]=i,h=Math.max(Math.abs(s.column-a.column),Math.abs(a.column-l.column)),c=Math.max(Math.abs(s.row-a.row),Math.abs(a.row-l.row)),u=Math.min(Math.ceil(h),Math.ceil(h-1e-10)),f=Math.min(Math.ceil(c),Math.ceil(c-1e-10)),d={row:Math.min(...i.map(t=>t.row))-(f-c)/2,column:Math.min(...i.map(t=>t.column))-(u-h)/2},m=rotatePoint(d,o,-n),g=Math.cos(-n),p=Math.sin(-n);return transform(t,[[g,-p,m.column],[p,g,m.row]],{inverse:!0,width:u,height:f,...r})}function getSmallestAngle(t){let e=-getAngle(t[1],t[0]);e>Math.PI/2?e-=Math.PI:e<-Math.PI/2&&(e+=Math.PI);let r=e;return Math.abs(e)>Math.PI/4&&(r=e>0?-Math.PI/2+e:Math.PI/2+e),r}function paintMaskOnImage(t,e,r={}){const{origin:n={row:0,column:0},color:o=getDefaultColor(t),blend:i=!0}=r,{column:s,row:a}=n;if(o.length!==t.channels)throw new RangeError("the given color is not compatible with the image");checkPointIsInteger(n,"Origin");const l=getOutputImage(t,r,{clone:!0});if(i){checkColorIsNumberArray(o);for(let r=Math.max(a,0);r<Math.min(e.height+a,t.height);r++)for(let n=Math.max(s,0);n<Math.min(e.width+s,t.width);n++)e.getBit(n-s,r-a)&&setBlendedPixel(l,n,r,o)}else for(let r=Math.max(a,0);r<Math.min(e.height+a,t.height);r++)for(let n=Math.max(s,0);n<Math.min(e.width+s,t.width);n++)if(e.getBit(n-s,r-a))for(let e=0;e<t.channels;e++){const t=o[e];"number"==typeof t&&l.setValue(n,r,e,t)}return l}function checkColorIsNumberArray(t){for(const e of t)if("number"!=typeof e)throw new TypeError("cannot have null channels in color if blend is true")}function paintMaskOnMask(t,e,r={}){const{origin:n={row:0,column:0},value:o=1}=r,{column:i,row:s}=n;checkPointIsInteger(n,"Origin");const a=maskToOutputMask(t,r,{clone:!0});for(let r=Math.max(s,0);r<Math.min(e.height+s,t.height);r++)for(let n=Math.max(i,0);n<Math.min(e.width+i,t.width);n++)e.getBit(n-i,r-s)&&a.setBit(n,r,o);return a}function maybeToPrecision(t,e){return t<0?(t=0-t,"number"==typeof e?`- ${t.toPrecision(e)}`:`- ${t.toString()}`):"number"==typeof e?t.toPrecision(e):t.toString()}function checkArrayLength(t,e){if(!isAnyArray$1(t.x)||!isAnyArray$1(t.y)||!isAnyArray$1(e))throw new TypeError("x, y and outputs must be arrays");if(t.x.length<2)throw new RangeError("explanatory variable should be two element per point");if(t.x.length!==t.y.length)throw new RangeError("x and y data must have the same length");if(t.x.length!==e.length)throw new RangeError("input and outputs must have the same length")}class BaseRegression2D{constructor(){if(new.target===BaseRegression2D)throw new Error("BaseRegression must be subclassed")}predict(t){if(isOnePoint(t))return this._predict(t);if(isAnyArray$1(t.x)){const{x:e,y:r}=t,n=new Float64Array(e.length);for(let t=0;t<e.length;t++)n[t]=this._predict({x:e[t],y:r[t]});return n}throw new TypeError("x must be a number or array")}_predict(t){throw new Error("_predict must be implemented")}train(){}toString(t){return""}toLaTeX(t){return""}getScore(t,e){checkArrayLength(t,e);const r=this.predict(t);let n=0,o=0,i=0,s=0,a=0,l=0,h=0;const c=e.length;for(let t=0;t<c;t++)n+=r[t],o+=e[t],a+=r[t]*r[t],l+=e[t]*e[t],h+=r[t]*e[t],0!==e[t]&&(i+=(e[t]-r[t])*(e[t]-r[t])/e[t]),s+=(e[t]-r[t])*(e[t]-r[t]);const u=(c*h-n*o)/Math.sqrt((c*a-n*n)*(c*l-o*o));return{r:u,r2:u*u,chi2:i,rmsd:Math.sqrt(s/c)}}}function isOnePoint(t){return!isAnyArray$1(t.x)}class PolynomialRegression2D extends BaseRegression2D{order;coefficients;score;constructor(t,e,r={}){if(super(),!0===t)this.coefficients=Matrix.columnVector(e.coefficients),this.order=e.order,this.score=e.score;else{checkArrayLength(t,e);const{order:n=2}=r;this.order=n,this.coefficients=train(t,e,n),this.score=this.getScore(t,e)}}_predict(t){const{x:e,y:r}=t;let n=0,o=0;for(let t=0;t<=this.order;t++)for(let i=0;i<=this.order-t;i++)n+=e**t*r**i*this.coefficients.get(o,0),o++;return n}toString(t){return this._toFormula(t,!1)}toLaTeX(t){return this._toFormula(t,!0)}_toFormula(t,e){let r="^",n="",o=" * ";e&&(r="^{",n="}",o="");let i="",s="",a=0;for(let e=0;e<=this.order;e++)for(let l=0;l<=this.order-e;l++){s="";const h=this.coefficients.get(a,0);0!==h&&(s+=maybeToPrecision(h,t),1===e?s+=`${o}x`:e>1&&(s+=`${o}x${r}${e}${n}`),1===l?s+=`${o}y`:l>1&&(s+=`${o}y${r}${l}${n}`),s=h>0?` + ${s}`:` ${s}`),a++,i=s+i}return`f(x, y) = ${i.startsWith("+")?i.slice(1):i}`}toJSON(){return{name:"polyfit2D",order:this.order,score:this.score,coefficients:this.coefficients}}static load(t){if("polyfit2D"!==t.name)throw new TypeError("not a polyfit2D model");return new PolynomialRegression2D(!0,t)}}function powColVector(t,e){const r=t.clone();for(let n=0;n<t.rows;++n)r.set(n,0,r.get(n,0)**e);return r}function train(t,e,r){Matrix.isMatrix(e)||(e=Matrix.columnVector(e));const n=new Matrix(e.rows,2);if(n.setColumn(0,t.x),n.setColumn(1,t.y),e.rows!==n.rows&&(e=e.transpose()),2!==n.columns)throw new RangeError(`You give x with ${n.columns} columns and it must be 2`);if(n.rows!==e.rows)throw new RangeError("x and y must have the same rows");const o=n.rows,i=(r+2)*(r+1)/2;if(o<i)throw new TypeError("Insufficient number of points to create regression model.");const s=n.getColumnVector(0),a=n.getColumnVector(1),l=1/s.clone().abs().max(),h=1/a.clone().abs().max(),c=1/e.clone().abs().max();s.mulColumn(0,l),a.mulColumn(0,h),e.mulColumn(0,c);const u=new Matrix(o,i);let f=0;for(let t=0;t<=r;++t){const e=r-t;for(let r=0;r<=e;++r){const e=powColVector(s,t).mulColumnVector(powColVector(a,r));u.setColumn(f,e),f++}}const d=new SVD(u.transpose(),{computeLeftSingularVectors:!0,computeRightSingularVectors:!0,autoTranspose:!1});let m=Matrix.rowVector(d.diagonal);m=m.apply((t,e)=>{m.get(t,e)>=1e-15?m.set(t,e,1/m.get(t,e)):m.set(t,e,0)});const g=Matrix.zeros(o,i);for(let t=0;t<i;++t)g.set(t,t,m.get(0,t));m=g;const p=d.rightSingularVectors,w=d.leftSingularVectors.mmul(m.transpose()).mmul(p.transpose()).mmul(e);for(let t=0,e=0;t<=i;++t){const n=r-t;for(let r=0;r<=n;++r)w.set(e,0,w.get(e,0)*l**t*h**r/c),e++}return w}function correctBackground(t,e){const{background:r,order:n=2,backgroundKind:o="light"}=e;checkProcessable$1(t,{colorModel:["GREY"]});const i=new Array,s=new Array,a=new Array;for(const e of r)i.push(e.column),s.push(e.row),a.push(t.getValueByPoint(e,0));const l=new PolynomialRegression2D({x:i,y:s},a,{order:n}),h={x:[],y:[]};for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++)h.x.push(r),h.y.push(e);const c=l.predict(h);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++){const n=Math.abs(t.getValue(r,e,0)-c[e*t.width+r]);t.setValue(r,e,0,n)}return"light"===o?t.invert():t}function invert(t,e){if(t instanceof Image){const r=getOutputImage(t,e);t.alpha&&copyAlpha(t,r);const{maxValue:n,size:o}=r;for(let e=0;e<o;e++)for(let o=0;o<t.components;o++)r.setValueByIndex(e,o,n-t.getValueByIndex(e,o));return r}{const r=maskToOutputMask(t,e);for(let e=0;e<r.size;e++)r.setBitByIndex(e,!t.getBitByIndex(e));return r}}function level(t,e={}){const r=t.minMax();let{inputMin:n=r.min,inputMax:o=r.max,outputMin:i=0,outputMax:s=t.maxValue,gamma:a=1}=e;const{channels:l=new Array(t.components).fill(0).map((t,e)=>e)}=e;validateChannels(l,t),checkProcessable$1(t,{bitDepth:[8,16]});const h=getOutputImage(t,e,{clone:!0}),c=getClamp(t);n=getValueArray(n,t.channels),o=getValueArray(o,t.channels),i=getValueArray(i,t.channels),s=getValueArray(s,t.channels),a=getValueArray(a,t.channels);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++)for(const u of l){const l=t.getValue(r,e,u);let f=c((Math.max(Math.min(l,o[u]),n[u])-n[u])/(o[u]-n[u]));Number.isNaN(f)&&(f=0);const d=c(f**(1/a[u])*(s[u]-i[u])+i[u]);h.setValue(r,e,u,d)}return h}function getValueArray(t,e){if(Array.isArray(t)){if(t.length===e)return t;throw new RangeError("array length is not compatible with channel option")}return new Array(e).fill(t)}function or(t,e,r){const n=maskToOutputMask(t,r);if(t.width!==e.width||t.height!==e.height)throw new RangeError("both masks must have the same size");for(let r=0;r<n.size;r++)t.getBitByIndex(r)||e.getBitByIndex(r)?n.setBitByIndex(r,1):n.setBitByIndex(r,0);return n}function flipX(t){checkProcessable$1(t,{bitDepth:[8,16]});for(let e=0;e<t.height;e++)for(let r=0;r<Math.floor(t.width/2);r++){const n=r,o=t.width-r-1;for(let r=0;r<t.channels;r++){const i=t.getValue(n,e,r);t.setValue(n,e,r,t.getValue(o,e,r)),t.setValue(o,e,r,i)}}return t}function flipY(t){checkProcessable$1(t,{bitDepth:[8,16]});for(let e=0;e<Math.floor(t.height/2);e++)for(let r=0;r<t.width;r++){const n=e,o=t.height-e-1;for(let e=0;e<t.channels;e++){const i=t.getValue(r,n,e);t.setValue(r,n,e,t.getValue(r,o,e)),t.setValue(r,o,e,i)}}return t}function flip(t,e={}){const{axis:r="horizontal"}=e,n=getOutputImage(t,e,{clone:!0});return"horizontal"===r?flipX(n):flipY("vertical"===r?n:flipX(n))}function medianFilter(t,e={}){const{cellSize:r=3,borderType:n="reflect101",borderValue:o=getDefaultColor(t)}=e;if(checkProcessable$1(t,{bitDepth:[8,16]}),r<1)throw new RangeError(`Invalid property "cellSize". Must be greater than 0. Received ${r}.`);if(r%2==0)throw new RangeError(`Invalid property "cellSize". Must be an odd number. Received ${r}.`);const i=getBorderInterpolation(n,o),s=Image.createFrom(t),a=new Uint16Array(r**2),l=(r-1)/2;for(let e=0;e<t.channels;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.width;n++){let o=0;for(let s=-l;s<=l;s++)for(let h=-l;h<=l;h++)a[o++]=i(n+h,r+s,e,t);s.setValue(n,r,e,xMedian(a))}return s}function pixelate(t,e){const{cellSize:r,algorithm:n="center"}=e;if(!Number.isInteger(r))throw new TypeError("cellSize must be an integer");if(r<2)throw new RangeError("cellSize must be greater than 1");const o=getOutputImage(t,e),i=getCellValueFunction(n);for(let e=0;e<t.channels;e++)for(let n=0;n<t.width;n+=r)for(let s=0;s<t.height;s+=r){const a=Math.min(r,t.width-n),l=Math.min(r,t.height-s),h=i(t,e,{width:a,height:l,origin:{column:n,row:s}});for(let t=n;t<n+a;t++)for(let r=s;r<s+l;r++)o.setValue(t,r,e,h)}return o}function getCellCenter(t,e,r){const n={column:Math.floor((r.origin.column+r.origin.column+r.width-1)/2),row:Math.floor((r.origin.row+r.origin.row+r.height-1)/2)};return t.getValue(n.column,n.row,e)}function getCellMean(t,e,r){let n=0;for(let o=r.origin.column;o<r.origin.column+r.width;o++)for(let i=r.origin.row;i<r.origin.row+r.height;i++)n+=t.getValue(o,i,e);return Math.round(n/(r.width*r.height))}function getCellMedian(t,e,r){const n=[];for(let o=r.origin.column;o<r.origin.column+r.width;o++)for(let i=r.origin.row;i<r.origin.row+r.height;i++)n.push(t.getValue(o,i,e));return xMedian(n)}function getCellValueFunction(t){switch(t){case"mean":return getCellMean;case"median":return getCellMedian;case"center":return getCellCenter;default:assertUnreachable(t)}}function getBorderPoints$1(t,e={}){const{innerBorders:r=!1,allowCorners:n=!1}=e;r||(t=t.solidFill());const o=[];for(let e=0;e<t.width;e++)t.getBit(e,0)&&o.push({column:e,row:0}),t.getBit(e,t.height-1)&&o.push({column:e,row:t.height-1});for(let e=0;e<t.height;e++)t.getBit(0,e)&&o.push({column:0,row:e}),t.getBit(t.width-1,e)&&o.push({column:t.width-1,row:e});for(let e=1;e<t.height-1;e++)for(let r=1;r<t.width-1;r++)t.getBit(r,e)&&(0!==t.getBit(r-1,e)&&0!==t.getBit(r,e-1)&&0!==t.getBit(r+1,e)&&0!==t.getBit(r,e+1)||o.push({column:r,row:e}),!n||0!==t.getBit(r-1,e-1)&&0!==t.getBit(r-1,e+1)&&0!==t.getBit(r+1,e-1)&&0!==t.getBit(r+1,e+1)||o.push({column:r,row:e}));return o}function getLineLength(t,e){return Math.hypot(t.column-e.column,t.row-e.row)}function getPolygonPerimeter(t){let e=0;for(let r=0;r<t.length;r++)e+=getLineLength(t[(r+1)%t.length],t[r]);return e}function getPolygonArea(t){let e=0;for(let r=0;r<t.length;r++){const n=(r+1)%t.length;e+=t[r].column*t[n].row,e-=t[r].row*t[n].column}return Math.abs(e/2)}function getCoordsFromIndex(t,e){return[Math.floor(t/e),t%e]}function getBitSafe(t,e,r){return isInBounds(t,e,r)?t.getBit(e,r):0}function isInBounds(t,e,r){return e>=0&&e<t.width&&r>=0&&r<t.height}const directions=[{dc:-1,dr:0},{dc:-1,dr:-1},{dc:0,dr:-1},{dc:1,dr:-1},{dc:1,dr:0},{dc:1,dr:1},{dc:0,dr:1},{dc:-1,dr:1}],directionIndexLookup=new Int8Array(9).fill(-1);for(let t=0;t<directions.length;t++){const{dc:e,dr:r}=directions[t];directionIndexLookup[3*(r+1)+(e+1)]=t}function getContourMoore(t){let e=0;const r=[],n=new Uint8Array(t.width*t.height);for(;1!==t.getBitByIndex(e)&&e!==t.size;)e++;if(e===t.size)return r;const[o,i]=getCoordsFromIndex(e,t.width),s={column:i,row:o};let a=s;n[e]=1,r.push(a);const l=findNextPoint(t,a,{column:i-1,row:o});if(!l)return r;let h=l.prevPoint,c=l.prevPoint;for(a=l.currPoint;;){e=a.row*t.width+a.column,n[e]||(n[e]=1,r.push(a));const o=findNextPoint(t,a,c);if(assert(o,"Next border point is undefined."),c=o.prevPoint,a=o.currPoint,a.column===s.column&&a.row===s.row){if(c.column===h.column&&c.row===h.row)break;h=c}}return r}function findNextPoint(t,e,r){let n=r;const o=r.column-e.column,i=r.row-e.row,s=directionIndexLookup[3*(i+1)+(o+1)];for(let r=0;r<directions.length;r++){const{dc:o,dr:i}=directions.at((s+r)%directions.length),a=e.column+o,l=e.row+i;if(1===getBitSafe(t,a,l))return{prevPoint:n,currPoint:{column:a,row:l}};n={column:a,row:l}}}const DR=[-1,0,1,0],DC=[0,1,0,-1];function turnLeft(t){return(t+3)%4}function turnRight(t){return(t+1)%4}function getContourPavlidis(t){let e=0;for(;e<t.size&&1!==t.getBitByIndex(e);)e++;if(e===t.size)return[];const[r,n]=getCoordsFromIndex(e,t.width);return traceFrom(t,n,r)}function traceFrom(t,e,r){let n=r,o=e,i=2,s=0;const a=new Uint8Array(t.width*t.height),l=[];let h=0;for(;;){const c=n*t.width+o;if(a[c]){if(n===r&&o===e){if(2===h)break;h++}}else a[c]=1,l.push({row:n,column:o});const u=turnLeft(i),f=turnRight(i),d=n+DR[u],m=o+DC[u],g=n+DR[i],p=o+DC[i],w=n+DR[f],y=o+DC[f];if(1===getBitSafe(t,m,d))i=u,n=d,o=m,s=0;else if(1===getBitSafe(t,p,g))n=g,o=p,s=0;else if(1===getBitSafe(t,y,w))i=f,n=w,o=y,s=0;else if(i=f,s++,s>=4)break}return l}function getExternalContour$1(t,e={}){const{allowCorners:r=!1}=e;return r?getContourPavlidis(t):getContourMoore(t)}function getExtendedBorderPoints(t){const e=getExternalContour$1(t),r=[];for(const t of e)r.push(t,{column:t.column+1,row:t.row},{column:t.column+1,row:t.row+1},{column:t.column,row:t.row+1});return r}function monotoneChainConvexHull(t,e={}){const{sorted:r=!1}=e;r||(t=t.slice()).sort(byXThenY);const n=t.length,o=new Array(2*n);let i=0;for(let e=0;e<n;e++){const r=t[e];for(;i>=2&&cw(o[i-2],o[i-1],r)<=0;)i--;o[i++]=r}const s=i+1;for(let e=n-2;e>=0;e--){const r=t[e];for(;i>=s&&cw(o[i-2],o[i-1],r)<=0;)i--;o[i++]=r}return o.slice(0,i-1)}function cw(t,e,r){return(e.row-t.row)*(r.column-t.column)-(e.column-t.column)*(r.row-t.row)}function byXThenY(t,e){return t.column===e.column?t.row-e.row:t.column-e.column}function getConvexHull(t){const e=getExtendedBorderPoints(t);if(0===e.length)return{points:[],surface:0,perimeter:0};const r=monotoneChainConvexHull(e);return{points:r,perimeter:getPolygonPerimeter(r),surface:getPolygonArea(r)}}function getFeret(t){const e=t.getConvexHull(),r=e.points;if(0===e.surface)return{minDiameter:{length:0,points:[{column:0,row:0},{column:0,row:0}],angle:0,calliperLines:[[{column:0,row:0},{column:0,row:0}],[{column:0,row:0},{column:0,row:0}]]},maxDiameter:{length:0,points:[{column:0,row:0},{column:0,row:0}],angle:0,calliperLines:[[{column:0,row:0},{column:0,row:0}],[{column:0,row:0},{column:0,row:0}]]},aspectRatio:1};let n,o=Number.POSITIVE_INFINITY,i=0,s=[];for(let t=0;t<r.length;t++){const e=getAngle(r[t],r[(t+1)%r.length]),a=rotate$1(-e,r);let l=0,h=[];for(let e=0;e<r.length;e++){const r=Math.abs(a[t].row-a[e].row);r>l&&(l=r,h=[a[t],a[e]])}if(l<o){o=l,i=e,s=h;const{minIndex:t,maxIndex:r}=findPointIndexesOfExtremeColumns(a);n=getMinLines(i,t,r,a,s)}}const a={points:rotate$1(i,s),length:o,angle:toDegrees(i),calliperLines:n};let l=[],h=0,c=[];for(let t=0;t<r.length-1;t++)for(let e=t+1;e<r.length;e++){const n=(r[t].column-r[e].column)**2+(r[t].row-r[e].row)**2;n>h&&(h=n,l=[r[t],r[e]],c=[t,e])}const u=getAngle(l[0],l[1]),f=rotate$1(-u,r),{minIndex:d,maxIndex:m}=findPointsIndexesOfExtremeRows(f),g=getMaxLines(u,d,m,f,c),p={length:Math.sqrt(h),angle:toDegrees(getAngle(l[0],l[1])),points:l,calliperLines:g};return{minDiameter:a,maxDiameter:p,aspectRatio:a.length/p.length}}function findPointIndexesOfExtremeColumns(t){let e=0,r=0;for(let n=0;n<t.length;n++)t[n].column>t[e].column&&(e=n),t[n].column<t[r].column&&(r=n);return{minIndex:r,maxIndex:e}}function findPointsIndexesOfExtremeRows(t){let e=0,r=0;for(let n=0;n<t.length;n++)t[n].row>t[e].row&&(e=n),t[n].row<t[r].row&&(r=n);return{minIndex:r,maxIndex:e}}function getMinLines(t,e,r,n,o){const i=[{column:n[e].column,row:o[0].row},{column:n[r].column,row:o[0].row}],s=[{column:n[e].column,row:o[1].row},{column:n[r].column,row:o[1].row}];return[rotate$1(t,i),rotate$1(t,s)]}function getMaxLines(t,e,r,n,o){const i=[{column:n[o[0]].column,row:n[e].row},{column:n[o[0]].column,row:n[r].row}],s=[{column:n[o[1]].column,row:n[e].row},{column:n[o[1]].column,row:n[r].row}];return[rotate$1(t,i),rotate$1(t,s)]}const leftFirst=(t,e)=>t.column<=e.column?-1:1,topFirst=(t,e)=>t.row>=e.row?-1:1;function getMbrAngle(t){const e=t.slice();e.sort(leftFirst);const r=e.slice(0,2),n=e.slice(2,4);r.sort(topFirst),n.sort(topFirst);return-toDegrees(getAngle(r[0],n[0]))}function getMbrFromPoints(t){if(0===t.length)return{points:[],angle:0,width:0,height:0,surface:0,perimeter:0,aspectRatio:0};if(1===t.length)return{points:[t[0],t[0],t[0],t[0]],perimeter:0,surface:0,angle:0,width:0,height:0,aspectRatio:1};let e=[],r=Number.POSITIVE_INFINITY,n=0,o=[];for(let i=0;i<t.length;i++){const s=getAngle(t[i],t[(i+1)%t.length]);e=rotate$1(-s,t);const a=e[i].column,l=e[i].row,h=e[(i+1)%e.length].column,c=e[(i+1)%e.length].row;let u=!0,f=0,d=0,m=0;for(const t of e){const e=(t.column-a)/(h-a);u?(u=!1,f=e,d=e):(e<f&&(f=e),e>d&&(d=e));const r=(-(h-a)*t.row+h*l-c*a)/(h-a);Math.abs(r)>Math.abs(m)&&(m=r)}const g={column:a+f*(h-a),row:l},p={column:a+d*(h-a),row:l},w=Math.abs(m*(f-d)*(h-a));w<r&&(n=s,r=w,o=[p,g,{column:g.column,row:g.row-m},{column:p.column,row:p.row-m}])}const i=rotate$1(n,o),s=[Math.hypot(o[0].column-o[1].column,o[0].row-o[1].row),Math.hypot(o[0].column-o[3].column,o[0].row-o[3].row)],a=Math.max(...s),l=Math.min(...s);return{points:i,surface:r,angle:getMbrAngle(i),width:a,height:l,perimeter:2*a+2*l,aspectRatio:l/a}}function getMbr(t){return getMbrFromPoints(monotoneChainConvexHull(getExtendedBorderPoints(t)))}function checkKernel(t){if(t.length%2==0||t[0].length%2==0)throw new TypeError("the number of rows and columns of the kernel must be odd")}function erode(t,e={}){let r=!1;void 0===e.kernel&&(r=!0);const{kernel:n=[[1,1,1],[1,1,1],[1,1,1]],iterations:o=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(n);let i=!0;if(!r)t:for(const t of n)for(const e of t)if(1!==e){i=!1;break t}let s=t;for(let e=0;e<o;e++)if(s instanceof Mask)if(i){const t=s.clone();s=erodeMaskOnlyOnes(s,t,n[0].length,n.length)}else{s=erodeMask(s,Mask.createFrom(t),n)}else if(i){s=erodeGreyOnlyOnes(s,Image.createFrom(t),n[0].length,n.length)}else{s=erodeGrey(s,Image.createFrom(t),n)}return s}function erodeGrey(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let l=0;l<t.width;l++){let h=t.maxValue;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+l,c=e-s+a;if(n<0||c<0||n>=t.width||c>=t.height)continue;const u=t.getValue(n,c,0);u<h&&(h=u)}e.setValue(l,a,0,h)}return e}function erodeGreyOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(0);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){let n=t.maxValue;for(let o=Math.max(0,r-i);o<Math.min(t.height,r+i+1);o++){const r=t.getValue(e,o,0);r<n&&(n=r)}s[e]=n}for(let n=0;n<t.width;n++){let i=t.maxValue;for(let e=Math.max(0,n-o);e<Math.min(t.width,n+o+1);e++)s[e]<i&&(i=s[e]);e.setValue(n,r,0,i)}}return e}function erodeMask(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let l=0;l<t.width;l++){let h=1;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+l,c=e-s+a;if(c<0||n<0||n>=t.width||c>=t.height)continue;if(0===t.getBit(n,c)){h=0;break}}1===h&&e.setBit(l,a,1)}return e}function erodeMaskOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(0);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){s[e]=1;for(let n=Math.max(0,r-i);n<Math.min(t.height,r+i+1);n++)if(0===t.getBit(e,n)){s[e]=0;break}}for(let n=0;n<t.width;n++)if(0!==e.getBit(n,r))for(let i=Math.max(0,n-o);i<Math.min(t.width,n+o+1);i++)if(0===s[i]){e.setBit(n,r,0);break}}return e}function dilate(t,e={}){let r=!1;void 0===e.kernel&&(r=!0);const{kernel:n=[[1,1,1],[1,1,1],[1,1,1]],iterations:o=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(n);let i=!0;if(!r)t:for(const t of n)for(const e of t)if(1!==e){i=!1;break t}let s=t;for(let t=0;t<o;t++)if(s instanceof Mask)if(i){const t=s.clone();s=dilatMaskOnlyOnes(s,t,n[0].length,n.length)}else{s=dilateMask(s,Mask.createFrom(s),n)}else if(i){s=dilateGreyOnlyOnes(s,Image.createFrom(s),n[0].length,n.length)}else{s=dilateGrey(s,Image.createFrom(s),n)}return s}function dilateGrey(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let l=0;l<t.width;l++){let h=0;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+l,c=e-s+a;if(n<0||c<0||n>=t.width||c>=t.height)continue;const u=t.getValue(n,c,0);u>h&&(h=u)}e.setValue(l,a,0,h)}return e}function dilateGreyOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(0);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){let n=0;for(let o=Math.max(0,r-i);o<Math.min(t.height,r+i+1);o++){const r=t.getValue(e,o,0);r>n&&(n=r)}s[e]=n}for(let n=0;n<t.width;n++){let i=0;for(let e=Math.max(0,n-o);e<Math.min(t.width,n+o+1);e++)s[e]>i&&(i=s[e]);e.setValue(n,r,0,i)}}return e}function dilateMask(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let l=0;l<t.width;l++){let h=0;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+l,c=e-s+a;if(c<0||n<0||n>=t.width||c>=t.height)continue;if(1===t.getBit(n,c)){h=1;break}}1===h&&e.setBit(l,a,1)}return e}function dilatMaskOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(1);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){s[e]=0;for(let n=Math.max(0,r-i);n<Math.min(t.height,r+i+1);n++)if(1===t.getBit(e,n)){s[e]=1;break}}for(let n=0;n<t.width;n++)if(1!==e.getBit(n,r))for(let i=Math.max(0,n-o);i<Math.min(t.width,n+o+1);i++)if(1===s[i]){e.setBit(n,r,1);break}}return e}function open(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++)o=o.erode({kernel:r}),o=o.dilate({kernel:r});return o}function close(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++)o=o.dilate({kernel:r}).erode({kernel:r});return o}function topHat(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++){const t=o.open({kernel:r});o=subtract(t,o,{absolute:!0})}return o}function bottomHat(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++){const t=o.close({kernel:r});o=subtract(t,o,{absolute:!0})}return o}function morphologicalGradient(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++){const t=o.dilate({kernel:r}),e=o.erode({kernel:r});o=subtract(t,e,{absolute:!0})}return o}function*borderIterator(t){for(let e=0;e<t.width;e++)yield e;for(let e=2;e<t.height;e++)yield e*t.width-1;for(let e=0;e<t.width;e++)yield t.width*t.height-e-1;for(let e=t.height-2;e>=1;e--)yield e*t.width}const MAX_ARRAY=65536,toProcess=new Uint32Array(MAX_ARRAY);function multipleFloodFill(t,e={}){const{startPixels:r=[0],startPixelValue:n=0,newPixelValue:o=1,allowCorners:i=!1}=e,s=maskToOutputMask(t,e,{clone:!0}),a=Mask.createFrom(t);let l=0,h=0;for(const t of r)s.getBitByIndex(t)===n&&(toProcess[h++%MAX_ARRAY]=t,a.setBitByIndex(t,1),s.setBitByIndex(t,o));for(;l<h;){assert(h-l<=MAX_ARRAY);const e=toProcess[l++%MAX_ARRAY];s.setBitByIndex(e,o);const r=e<t.width,n=e%t.width===0,a=e%t.width===t.width-1,u=e>t.size-t.width;if(!u){c(e+t.width)}if(!n){c(e-1)}if(!r){c(e-t.width)}if(!a){c(e+1)}if(i){if(!r){if(!n){c(e-t.width-1)}if(!a){c(e-t.width+1)}}if(!u){if(!n){c(e+t.width-1)}if(!a){c(e+t.width+1)}}}}function c(t){a.getBitByIndex(t)||s.getBitByIndex(t)===n&&(toProcess[h++%MAX_ARRAY]=t,a.setBitByIndex(t,1))}return s}function clearBorder(t,e={}){const{allowCorners:r=!1,out:n,color:o="white"}=e;return multipleFloodFill(t,{startPixels:borderIterator(t),startPixelValue:"white"===o?1:0,newPixelValue:"white"===o?0:1,allowCorners:r,out:n})}const kernelX=[[-1,0,1],[-2,0,2],[-1,0,1]],kernelY=[[-1,-2,-1],[0,0,0],[1,2,1]];function cannyEdgeDetector(t,e={}){const{hysteresis:r=!0,lowThreshold:n=.04,highThreshold:o=.1,gaussianBlurOptions:i={sigma:1}}=e,s=n*t.maxValue,a=o*t.maxValue;checkProcessable$1(t,{colorModel:"GREY"});const l=t.width,h=t.height,c=t.gaussianBlur(i),u=c.rawDirectConvolution(kernelY),f=c.rawDirectConvolution(kernelX),d=new Float64Array(t.size);for(let e=0;e<t.size;e++)d[e]=Math.hypot(u[e],f[e]);const m=new Float64Array(t.size),g=new Float64Array(t.size),p=imageToOutputMask(t,e);for(let e=1;e<l-1;e++)for(let r=1;r<h-1;r++){const n=getDirection(u[getIndex(e,r,t,0)],f[getIndex(e,r,t,0)]),o=d[getIndex(e,r,t,0)];(0===n&&o>=d[getIndex(e,r-1,t,0)]&&o>=d[getIndex(e,r+1,t,0)]||1===n&&o>=d[getIndex(e-1,r-1,t,0)]&&o>=d[getIndex(e+1,r+1,t,0)]||2===n&&o>=d[getIndex(e-1,r,t,0)]&&o>=d[getIndex(e+1,r,t,0)]||3===n&&o>=d[getIndex(e-1,r+1,t,0)]&&o>=d[getIndex(e+1,r-1,t,0)])&&(m[getIndex(e,r,t,0)]=o)}for(let t=0;t<l*h;++t){const e=m[t];let r=0;e>a&&(r++,p.setBitByIndex(t,1)),e>s&&r++,g[t]=r}if(r){let e=[];for(let r=1;r<l-1;++r)for(let n=1;n<h-1;++n)if(1===g[getIndex(r,n,t,0)])t:for(let o=r-1;o<r+2;++o)for(let i=n-1;i<n+2;++i)if(2===g[getIndex(o,i,t,0)]){e.push([r,n]),p.setValue(r,n,0,1);break t}for(;e.length>0;){const r=[];for(const n of e)for(let e=-1;e<2;++e)for(let o=-1;o<2;++o){if(0===e&&0===o)continue;const i=n[0]+e,s=n[1]+o;1===g[getIndex(s,i,t,0)]&&0===p.getValue(s,i,0)&&(r.push([i,s]),p.setValue(s,i,0,1))}e=r}}return p}function getDirection(t,e){return(Math.round(Math.atan2(e,t)*(4/Math.PI))+4)%4}function floodFill(t,e={}){const{origin:r={row:0,column:0},allowCorners:n=!1,out:o}=e;return multipleFloodFill(t,{startPixels:[getIndex(r.column,r.row,t)],allowCorners:n,out:o})}function solidFill(t,e={}){const{allowCorners:r=!1}=e,n=maskToOutputMask(t,e,{clone:!0}),o=t.invert().clearBorder({allowCorners:r});return n.or(o,{out:n})}function boolToNumber(t){let e;return e="boolean"==typeof t?t?1:0:t,e}class Mask{width;height;size;bitDepth;colorModel;components;channels;alpha;maxValue;origin;data;constructor(t,e,r={}){const{data:n,origin:o={row:0,column:0}}=r;if(t<1||!Number.isInteger(t))throw new RangeError(`width must be an integer and at least 1. Received ${t}`);if(e<1||!Number.isInteger(e))throw new RangeError(`height must be an integer and at least 1. Received ${e}`);this.width=t,this.height=e,this.size=t*e,this.bitDepth=1,this.colorModel="BINARY",this.origin=o;const i=colorModels[this.colorModel];if(this.components=i.components,this.alpha=i.alpha,this.channels=i.channels,this.maxValue=1,void 0===n)this.data=new Uint8Array(this.size);else{const t=this.size*this.channels;if(n.length!==t)throw new RangeError(`incorrect data size: ${n.length}. Expected ${t}`);this.data=n}}static createFrom(t,e={}){const{width:r=t.width,height:n=t.height,origin:o=t.origin}=e;return new Mask(r,n,{origin:o,...e})}getPixel(t,e){const r=[],n=e*this.width+t;return r.push(this.data[n]),r}setPixel(t,e,r){const n=e*this.width+t;this.data[n]=r[0]}setVisiblePixel(t,e,r){t>=0&&t<this.width&&e>=0&&e<this.height&&this.setPixel(t,e,r)}getPixelByIndex(t){return[this.data[t]]}setPixelByIndex(t,e){this.data[t]=e[0]}static fromPoints(t,e,r){const n=new Mask(t,e);for(const t of r)n.setBit(t.column,t.row,1);return n}clone(){return Mask.createFrom(this,{data:this.data.slice()})}getBit(t,e){const r=e*this.width+t;return this.data[r]}setBit(t,e,r){const n=e*this.width+t;this.data[n]=r}getBitByIndex(t){return this.data[t*this.channels]}setBitByIndex(t,e){this.data[t*this.channels]=boolToNumber(e)}getNbNonZeroPixels(){let t=0;for(const e of this.data)e&&t++;return t}getValue(t,e,r){return checkChannel(r),this.getBit(t,e)}setValue(t,e,r,n){checkChannel(r),this.setBit(t,e,n)}getValueByIndex(t,e){return checkChannel(e),this.getBitByIndex(t)}setValueByIndex(t,e,r){checkChannel(e),this.setBitByIndex(t,r)}getValueByPoint(t){return this.getValue(t.column,t.row,0)}setValueByPoint(t,e){this.setValue(t.column,t.row,0,e)}getRawImage(){return{width:this.width,height:this.height,data:this.data}}[Symbol.for("nodejs.util.inspect.custom")](){let t;return t=this.height>20||this.width>20?"[...]":printData$1(this),`Mask {\n width: ${this.width}\n height: ${this.height}\n data: ${t}\n}`}fill(t){const e=boolToNumber(t);return this.data.fill(e),this}convertColor(t){return convertColor(this,t)}invert(t){return invert(this,t)}subtract(t,e){return subtract(this,t,e)}and(t,e){return and(this,t,e)}or(t,e){return or(this,t,e)}getBorderPoints(t){return getBorderPoints$1(this,t)}getExternalContour(t){return getExternalContour$1(this,t)}getConvexHull(){return getConvexHull(this)}getMbr(){return getMbr(this)}getFeret(){return getFeret(this)}erode(t){return erode(this,t)}dilate(t){return dilate(this,t)}open(t){return open(this,t)}close(t){return close(this,t)}topHat(t){return topHat(this,t)}bottomHat(t){return bottomHat(this,t)}morphologicalGradient(t){return morphologicalGradient(this,t)}clearBorder(t){return clearBorder(this,t)}floodFill(t){return floodFill(this,t)}solidFill(t){return solidFill(this,t)}drawPoints(t,e={}){return drawPoints(this,t,e)}drawLine(t,e,r={}){return drawLineOnMask(this,t,e,r)}drawPolyline(t,e={}){return drawPolylineOnMask(this,t,e)}drawPolygon(t,e={}){return drawPolygonOnMask(this,t,e)}drawRectangle(t={}){return drawRectangle(this,t)}copyTo(t,e={}){return copyTo(this,t,e)}paintMask(t,e){return paintMaskOnMask(this,t,e)}}function printData$1(t){const e=[];for(let r=0;r<t.height;r++){const n=[];for(let e=0;e<t.width;e++)n.push(String(t.getBit(e,r)));e.push(`[${n.join(" ")}]`)}return e.join("\n ")}function checkChannel(t){if(0!==t)throw new RangeError(`channel value must be 0 on type Mask. Received ${t}`)}function subtract(t,e,r={}){const{absolute:n=!1}=r;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:[1,3],alpha:!1}),validateForComparison(t,e);const o=t.clone();if(o instanceof Image)for(let r=0;r<t.size;r++)for(let i=0;i<t.channels;i++){const s=t.getValueByIndex(r,i)-e.getValueByIndex(r,i);n?s<0?o.setValueByIndex(r,i,-s):o.setValueByIndex(r,i,s):s<0?o.setValueByIndex(r,i,0):o.setValueByIndex(r,i,s)}else if(t instanceof Mask&&e instanceof Mask)for(let r=0;r<t.size;r++){const i=t.getBitByIndex(r)-e.getBitByIndex(r);n?o.setBitByIndex(r,i?1:0):o.setBitByIndex(r,i>0?1:0)}return o}function add(t,e){t instanceof Image&&checkProcessable$1(t,{bitDepth:[8,16],components:[1,3],alpha:!1}),validateForComparison(t,e);const r=t.clone(),n=getClamp(t);for(let o=0;o<t.size;o++)for(let i=0;i<t.channels;i++){const s=t.getValueByIndex(o,i)+e.getValueByIndex(o,i);r.setValueByIndex(o,i,n(s))}return r}function multiply(t,e,r={}){const{channels:n=new Array(t.channels).fill(0).map((t,e)=>e)}=r;validateChannels(n,t);const o=getOutputImage(t,r,{clone:!0});if(0===n.length)return o;for(const t of n)for(let r=0;r<o.height;r++)for(let n=0;n<o.width;n++){const i=o.getValue(n,r,t)*e;o.setClampedValue(n,r,t,i)}return o}function mean(t,e){const r=new Array(t.channels).fill(0),n=e?e.points.length:t.size;if(0===n)throw new RangeError("Array of coordinates is empty.");if(e)for(const n of e.points)for(let e=0;e<t.channels;e++){if(n.column<0||n.column>=t.width||n.row<0||n.row>=t.height)throw new RangeError(`Invalid coordinate: {column: ${n.column}, row: ${n.row}}.`);r[e]+=t.getValueByPoint(n,e)}else for(let e=0;e<t.height;e++)for(let n=0;n<t.width;n++)for(let o=0;o<t.channels;o++)r[o]+=t.getValue(n,e,o);return r.map(t=>t/n)}function histogram$1(t,e={}){let{channel:r}=e;const{slots:n=2**t.bitDepth}=e;if(0===n||n&n-1)throw new RangeError("slots must be a power of 2, for example: 64, 256, 1024");if("number"!=typeof r){if(1!==t.channels)throw new TypeError("channel option is mandatory for multi-channel images");r=0}validateChannel(r,t);const o=new Uint32Array(n);let i=0;const s=Math.log2(n);i=t.bitDepth-s;for(let e=0;e<t.size;e++)o[t.getValueByIndex(e,r)>>i]++;return o}function median(t,e){const r=new Array(t.channels).fill(0);if(e){if(0===e.points.length)throw new RangeError("Array of coordinates is empty.");for(let n=0;n<t.channels;n++){const o=[];for(const r of e.points){if(r.column<0||r.column>=t.width||r.row<0||r.row>=t.height)throw new RangeError(`Invalid coordinate: {column: ${r.column}, row: ${r.row}}.`);o.push(t.getValueByPoint(r,n))}r[n]=quickMedian(o)}}else for(let e=0;e<t.channels;e++){const n=t.getChannel(e);r[e]=quickMedian(n)}return r}function getExtrema(t,e){const{kind:r="maximum",mask:n,algorithm:o="star",maxEquals:i=2}=e;checkProcessable$1(t,{bitDepth:[8,16]});const s="minimum"===r,a=s?0:1,l=[1,0,-1,0,1,1,-1,-1,2,0,-2,0,2,2,-2,-2],h=[0,1,0,-1,1,-1,1,-1,0,2,0,-2,2,-2,2,-2];switch(o){case"cross":l.length=4,h.length=4;break;case"square":l.length=8,h.length=8;break;case"star":break;default:assertUnreachable(o)}const c=l.length<=8?1:2,u=[];for(let e=0;e<t.channels;e++)for(let r=c;r<t.height-c;r++)for(let o=c;o<t.width-c;o++){if(n&&n.getBit(o,r)!==a)continue;let c=0,f=0;const d=t.getValue(o,r,e);for(let n=0;n<l.length;n++){const i=t.getValue(o+l[n],r+h[n],e);s?i>d&&c++:i<d&&c++,i===d&&f++}c+f===l.length&&f<=i&&u.push({column:o,row:r})}return u}function variance(t,e){const r=t.mean(e),n=new Array(t.channels).fill(0);if(e)for(const o of e.points)for(let e=0;e<t.channels;e++)n[e]+=(t.getValue(o.column,o.row,e)-r[e])**2;else for(let e=0;e<t.size;e++)for(let o=0;o<t.channels;o++)n[o]+=(t.getValueByIndex(e,o)-r[o])**2;const o=e?e.points.length*t.channels:t.size*t.channels;return n.map(t=>t/o)}class MultivariateLinearRegression{constructor(t,e,r={}){const{intercept:n=!0,statistics:o=!0}=r;if(this.statistics=o,!0===t)this.weights=e.weights,this.inputs=e.inputs,this.outputs=e.outputs,this.intercept=e.intercept;else{t=new Matrix(t),e=new Matrix(e),n&&t.addColumn(new Array(t.rows).fill(1));let r=t.transpose();const i=r.mmul(t),s=r.mmul(e),a=new SVD(i).inverse(),l=s.transpose().mmul(a).transpose();if(this.weights=l.to2DArray(),this.inputs=t.columns,this.outputs=e.columns,n&&this.inputs--,this.intercept=n,o){const r=t.mmul(l),n=e.clone().addM(r.neg()).to2DArray().map(t=>Math.pow(t[0],2)).reduce((t,e)=>t+e)/(e.rows-t.columns);this.stdError=Math.sqrt(n),this.stdErrorMatrix=pseudoInverse(i).mul(n),this.stdErrors=this.stdErrorMatrix.diagonal().map(t=>Math.sqrt(t)),this.tStats=this.weights.map((t,e)=>0===this.stdErrors[e]?0:t[0]/this.stdErrors[e])}}}predict(t){if(Array.isArray(t)){if("number"==typeof t[0])return this._predict(t);if(Array.isArray(t[0])){const e=new Array(t.length);for(let r=0;r<t.length;r++)e[r]=this._predict(t[r]);return e}}else if(Matrix.isMatrix(t)){const e=new Matrix(t.rows,this.outputs);for(let r=0;r<t.rows;r++)e.setRow(r,this._predict(t.getRow(r)));return e}throw new TypeError("x must be a matrix or array of numbers")}_predict(t){const e=new Array(this.outputs);if(this.intercept)for(let t=0;t<this.outputs;t++)e[t]=this.weights[this.inputs][t];else e.fill(0);for(let r=0;r<this.inputs;r++)for(let n=0;n<this.outputs;n++)e[n]+=this.weights[r][n]*t[r];return e}score(){throw new Error("score method is not implemented yet")}toJSON(){return{name:"multivariateLinearRegression",weights:this.weights,inputs:this.inputs,outputs:this.outputs,intercept:this.intercept,summary:this.statistics?{regressionStatistics:{standardError:this.stdError,observations:this.outputs},variables:this.weights.map((t,e)=>({label:e===this.weights.length-1?"Intercept":`X Variable ${e+1}`,coefficients:t,standardError:this.stdErrors[e],tStat:this.tStats[e]}))}:void 0}}static load(t){if("multivariateLinearRegression"!==t.name)throw new Error("not a MLR model");return new MultivariateLinearRegression(!0,t)}}var r$1={grad:.9,turn:360,rad:360/(2*Math.PI)},t$1=function(t){return"string"==typeof t?t.length>0:"number"==typeof t},n$1=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=Math.pow(10,e)),Math.round(r*t)/r+0},e$1=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=1),t>r?r:t>e?t:e},u$1=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},a$1=function(t){return{r:e$1(t.r,0,255),g:e$1(t.g,0,255),b:e$1(t.b,0,255),a:e$1(t.a)}},o$1=function(t){return{r:n$1(t.r),g:n$1(t.g),b:n$1(t.b),a:n$1(t.a,3)}},i$2=/^#([0-9a-f]{3,8})$/i,s=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},h$1=function(t){var e=t.r,r=t.g,n=t.b,o=t.a,i=Math.max(e,r,n),s=i-Math.min(e,r,n),a=s?i===e?(r-n)/s:i===r?2+(n-e)/s:4+(e-r)/s:0;return{h:60*(a<0?a+6:a),s:i?s/i*100:0,v:i/255*100,a:o}},b$1=function(t){var e=t.h,r=t.s,n=t.v,o=t.a;e=e/360*6,r/=100,n/=100;var i=Math.floor(e),s=n*(1-r),a=n*(1-(e-i)*r),l=n*(1-(1-e+i)*r),h=i%6;return{r:255*[n,a,s,s,l,n][h],g:255*[l,n,n,a,s,s][h],b:255*[s,s,l,n,n,a][h],a:o}},g=function(t){return{h:u$1(t.h),s:e$1(t.s,0,100),l:e$1(t.l,0,100),a:e$1(t.a)}},d=function(t){return{h:n$1(t.h),s:n$1(t.s),l:n$1(t.l),a:n$1(t.a,3)}},f=function(t){return b$1((r=(e=t).s,{h:e.h,s:(r*=((n=e.l)<50?n:100-n)/100)>0?2*r/(n+r)*100:0,v:n+r,a:e.a}));var e,r,n},c=function(t){return{h:(e=h$1(t)).h,s:(o=(200-(r=e.s))*(n=e.v)/100)>0&&o<200?r*n/100/(o<=100?o:200-o)*100:0,l:o/2,a:e.a};var e,r,n,o},l$1=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,p$1=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,v=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,m=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,y={string:[[function(t){var e=i$2.exec(t);return e?(t=e[1]).length<=4?{r:parseInt(t[0]+t[0],16),g:parseInt(t[1]+t[1],16),b:parseInt(t[2]+t[2],16),a:4===t.length?n$1(parseInt(t[3]+t[3],16)/255,2):1}:6===t.length||8===t.length?{r:parseInt(t.substr(0,2),16),g:parseInt(t.substr(2,2),16),b:parseInt(t.substr(4,2),16),a:8===t.length?n$1(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=v.exec(t)||m.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:a$1({r:Number(e[1])/(e[2]?100/255:1),g:Number(e[3])/(e[4]?100/255:1),b:Number(e[5])/(e[6]?100/255:1),a:void 0===e[7]?1:Number(e[7])/(e[8]?100:1)}):null},"rgb"],[function(t){var e=l$1.exec(t)||p$1.exec(t);if(!e)return null;var r,n,o=g({h:(r=e[1],n=e[2],void 0===n&&(n="deg"),Number(r)*(r$1[n]||1)),s:Number(e[3]),l:Number(e[4]),a:void 0===e[5]?1:Number(e[5])/(e[6]?100:1)});return f(o)},"hsl"]],object:[[function(t){var e=t.r,r=t.g,n=t.b,o=t.a,i=void 0===o?1:o;return t$1(e)&&t$1(r)&&t$1(n)?a$1({r:Number(e),g:Number(r),b:Number(n),a:Number(i)}):null},"rgb"],[function(t){var e=t.h,r=t.s,n=t.l,o=t.a,i=void 0===o?1:o;if(!t$1(e)||!t$1(r)||!t$1(n))return null;var s=g({h:Number(e),s:Number(r),l:Number(n),a:Number(i)});return f(s)},"hsl"],[function(t){var e=t.h,r=t.s,n=t.v,o=t.a,i=void 0===o?1:o;if(!t$1(e)||!t$1(r)||!t$1(n))return null;var s=function(t){return{h:u$1(t.h),s:e$1(t.s,0,100),v:e$1(t.v,0,100),a:e$1(t.a)}}({h:Number(e),s:Number(r),v:Number(n),a:Number(i)});return b$1(s)},"hsv"]]},N=function(t,e){for(var r=0;r<e.length;r++){var n=e[r][0](t);if(n)return[n,e[r][1]]}return[null,void 0]},x$1=function(t){return"string"==typeof t?N(t.trim(),y.string):"object"==typeof t&&null!==t?N(t,y.object):[null,void 0]},M$1=function(t,e){var r=c(t);return{h:r.h,s:e$1(r.s+100*e,0,100),l:r.l,a:r.a}},H=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},$=function(t,e){var r=c(t);return{h:r.h,s:r.s,l:e$1(r.l+100*e,0,100),a:r.a}},j=function(){function t(t){this.parsed=x$1(t)[0],this.rgba=this.parsed||{r:0,g:0,b:0,a:1}}return t.prototype.isValid=function(){return null!==this.parsed},t.prototype.brightness=function(){return n$1(H(this.rgba),2)},t.prototype.isDark=function(){return H(this.rgba)<.5},t.prototype.isLight=function(){return H(this.rgba)>=.5},t.prototype.toHex=function(){return e=(t=o$1(this.rgba)).r,r=t.g,n=t.b,i=(o=t.a)<1?s(n$1(255*o)):"","#"+s(e)+s(r)+s(n)+i;var t,e,r,n,o,i},t.prototype.toRgb=function(){return o$1(this.rgba)},t.prototype.toRgbString=function(){return e=(t=o$1(this.rgba)).r,r=t.g,n=t.b,(o=t.a)<1?"rgba("+e+", "+r+", "+n+", "+o+")":"rgb("+e+", "+r+", "+n+")";var t,e,r,n,o},t.prototype.toHsl=function(){return d(c(this.rgba))},t.prototype.toHslString=function(){return e=(t=d(c(this.rgba))).h,r=t.s,n=t.l,(o=t.a)<1?"hsla("+e+", "+r+"%, "+n+"%, "+o+")":"hsl("+e+", "+r+"%, "+n+"%)";var t,e,r,n,o},t.prototype.toHsv=function(){return t=h$1(this.rgba),{h:n$1(t.h),s:n$1(t.s),v:n$1(t.v),a:n$1(t.a,3)};var t},t.prototype.invert=function(){return w$1({r:255-(t=this.rgba).r,g:255-t.g,b:255-t.b,a:t.a});var t},t.prototype.saturate=function(t){return void 0===t&&(t=.1),w$1(M$1(this.rgba,t))},t.prototype.desaturate=function(t){return void 0===t&&(t=.1),w$1(M$1(this.rgba,-t))},t.prototype.grayscale=function(){return w$1(M$1(this.rgba,-1))},t.prototype.lighten=function(t){return void 0===t&&(t=.1),w$1($(this.rgba,t))},t.prototype.darken=function(t){return void 0===t&&(t=.1),w$1($(this.rgba,-t))},t.prototype.rotate=function(t){return void 0===t&&(t=15),this.hue(this.hue()+t)},t.prototype.alpha=function(t){return"number"==typeof t?w$1({r:(e=this.rgba).r,g:e.g,b:e.b,a:t}):n$1(this.rgba.a,3);var e},t.prototype.hue=function(t){var e=c(this.rgba);return"number"==typeof t?w$1({h:t,s:e.s,l:e.l,a:e.a}):n$1(e.h)},t.prototype.isEqual=function(t){return this.toHex()===w$1(t).toHex()},t}(),w$1=function(t){return t instanceof j?t:new j(t)},S=[],k=function(t){t.forEach(function(t){S.indexOf(t)<0&&(t(j,y),S.push(t))})},a=function(t){return"string"==typeof t?t.length>0:"number"==typeof t},t=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=Math.pow(10,e)),Math.round(r*t)/r+0},o=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=1),t>r?r:t>e?t:e},r=function(t){var e=t/255;return e<.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},h=function(t){return 255*(t>.0031308?1.055*Math.pow(t,1/2.4)-.055:12.92*t)},n=96.422,p=100,M=82.521,u=function(t){var e,r,n=.9555766*(e=t).x+-.0230393*e.y+.0631636*e.z,i=-.0282895*e.x+1.0099416*e.y+.0210077*e.z,s=.0122982*e.x+-.020483*e.y+1.3299098*e.z;return r={r:h(.032404542*n-.015371385*i-.004985314*s),g:h(-.00969266*n+.018760108*i+41556e-8*s),b:h(556434e-9*n-.002040259*i+.010572252*s),a:t.a},{r:o(r.r,0,255),g:o(r.g,0,255),b:o(r.b,0,255),a:o(r.a)}},e=function(t){var e=r(t.r),i=r(t.g),s=r(t.b);return function(t){return{x:o(t.x,0,n),y:o(t.y,0,p),z:o(t.z,0,M),a:o(t.a)}}(function(t){return{x:1.0478112*t.x+.0228866*t.y+-.050127*t.z,y:.0295424*t.x+.9904844*t.y+-.0170491*t.z,z:-.0092345*t.x+.0150436*t.y+.7521316*t.z,a:t.a}}({x:100*(.4124564*e+.3575761*i+.1804375*s),y:100*(.2126729*e+.7151522*i+.072175*s),z:100*(.0193339*e+.119192*i+.9503041*s),a:t.a}))},w=216/24389,b=24389/27,i$1=function(t){var e=t.l,r=t.a,n=t.b,i=t.alpha,s=void 0===i?1:i;if(!a(e)||!a(r)||!a(n))return null;var h=function(t){return{l:o(t.l,0,400),a:t.a,b:t.b,alpha:o(t.alpha)}}({l:Number(e),a:Number(r),b:Number(n),alpha:Number(s)});return l(h)},l=function(t){var e=(t.l+16)/116,r=t.a/500+e,o=e-t.b/200;return u({x:(Math.pow(r,3)>w?Math.pow(r,3):(116*r-16)/b)*n,y:(t.l>8?Math.pow((t.l+16)/116,3):t.l/b)*p,z:(Math.pow(o,3)>w?Math.pow(o,3):(116*o-16)/b)*M,a:t.alpha})};function labPlugin(r,i){r.prototype.toLab=function(){return s=(o=e(this.rgba)).y/p,a=o.z/M,i=(i=o.x/n)>w?Math.cbrt(i):(b*i+16)/116,r={l:116*(s=s>w?Math.cbrt(s):(b*s+16)/116)-16,a:500*(i-s),b:200*(s-(a=a>w?Math.cbrt(a):(b*a+16)/116)),alpha:o.a},{l:t(r.l,2),a:t(r.a,2),b:t(r.b,2),alpha:t(r.alpha,3)};var r,o,i,s,a},r.prototype.delta=function(e){void 0===e&&(e="#FFF");var n=e instanceof r?e:new r(e),i=function(t,e){var r=t.l,n=t.a,o=t.b,i=e.l,s=e.a,a=e.b,l=180/Math.PI,h=Math.PI/180,c=Math.pow(Math.pow(n,2)+Math.pow(o,2),.5),u=Math.pow(Math.pow(s,2)+Math.pow(a,2),.5),f=(r+i)/2,d=Math.pow((c+u)/2,7),m=.5*(1-Math.pow(d/(d+Math.pow(25,7)),.5)),g=n*(1+m),p=s*(1+m),w=Math.pow(Math.pow(g,2)+Math.pow(o,2),.5),y=Math.pow(Math.pow(p,2)+Math.pow(a,2),.5),b=(w+y)/2,x=0===g&&0===o?0:Math.atan2(o,g)*l,M=0===p&&0===a?0:Math.atan2(a,p)*l;x<0&&(x+=360),M<0&&(M+=360);var v=M-x,I=Math.abs(M-x);I>180&&M<=x?v+=360:I>180&&M>x&&(v-=360);var A=x+M;I<=180?A/=2:A=(x+M<360?A+360:A-360)/2;var k=1-.17*Math.cos(h*(A-30))+.24*Math.cos(2*h*A)+.32*Math.cos(h*(3*A+6))-.2*Math.cos(h*(4*A-63)),R=i-r,E=y-w,$=2*Math.sin(h*v/2)*Math.pow(w*y,.5),P=1+.015*Math.pow(f-50,2)/Math.pow(20+Math.pow(f-50,2),.5),S=1+.045*b,C=1+.015*b*k,B=30*Math.exp(-1*Math.pow((A-275)/25,2)),T=-2*Math.pow(d/(d+Math.pow(25,7)),.5)*Math.sin(2*h*B);return Math.pow(Math.pow(R/1/P,2)+Math.pow(E/1/S,2)+Math.pow($/1/C,2)+T*E*$/(1*S*1*C),.5)}(this.toLab(),n.toLab())/100;return o(t(i,3))},i.object.push([i$1,"lab"])}function formatReferenceForMlr(t,e){const r={r:[],g:[],b:[]};for(const n of t)r.r.push([n.r/e]),r.g.push([n.g/e]),r.b.push([n.b/e]);return r}function formatInputForMlr(t,e){const r=[];for(const n of t)r.push(getRegressionVariables(n.r,n.g,n.b,e));return r}function correctColor(t,e,r){if(checkProcessable$1(t,{colorModel:["RGB","RGBA"]}),e.length!==r.length)throw new RangeError("number of measured colors and reference colors must be the same");const n=formatInputForMlr(e,t.maxValue),o=formatReferenceForMlr(r,t.maxValue),i=new MultivariateLinearRegression(n,o.r),s=new MultivariateLinearRegression(n,o.g),a=new MultivariateLinearRegression(n,o.b),l=Image.createFrom(t);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++){const n=t.getPixel(r,e),o=getRegressionVariables(n[0],n[1],n[2],t.maxValue),h=getClamp(t),c=[0,0,0],u=i.predict(o)[0]*t.maxValue,f=s.predict(o)[0]*t.maxValue,d=a.predict(o)[0]*t.maxValue;c[0]=h(u),c[1]=h(f),c[2]=h(d),t.alpha&&(c[3]=t.getValue(r,e,3)),l.setPixel(r,e,c)}return l}function getRegressionVariables(t,e,r,n){return[t/=n,e/=n,r/=n,t**2,e**2,r**2,t**3,e**3,r**3,t*e,t*r,r*e]}function getMinMax(t){const e=new Array(t.channels).fill(t.maxValue),r=new Array(t.channels).fill(0);for(let n=0;n<t.height;n++)for(let o=0;o<t.width;o++)for(let i=0;i<t.channels;i++){const s=t.getValue(o,n,i);s<e[i]&&(e[i]=s),s>r[i]&&(r[i]=s)}return{min:e,max:r}}k([labPlugin]);const ImageCoordinates={CENTER:"center",TOP_LEFT:"top-left",TOP_RIGHT:"top-right",BOTTOM_LEFT:"bottom-left",BOTTOM_RIGHT:"bottom-right"};class Image{width;height;size;bitDepth;colorModel;components;channels;alpha;maxValue;origin;originalResolution;meta;data;constructor(t,e,r={}){const{bitDepth:n=8,data:o,colorModel:i="RGB",origin:s={row:0,column:0},meta:a,resolution:l}=r;if(t<1||!Number.isInteger(t))throw new RangeError(`width must be an integer and at least 1. Received ${t}`);if(e<1||!Number.isInteger(e))throw new RangeError(`height must be an integer and at least 1. Received ${e}`);this.width=t,this.height=e,this.size=t*e,this.bitDepth=n,this.colorModel=i,this.origin=s,this.meta=a,this.originalResolution=l;const h=colorModels[i];if(this.components=h.components,this.alpha=h.alpha,this.channels=h.channels,this.maxValue=2**n-1,void 0===o)this.data=createPixelArray(this.size,this.channels,this.alpha,this.bitDepth,this.maxValue);else{if(8===n&&o instanceof Uint16Array)throw new RangeError(`bitDepth is ${n} but data is Uint16Array`);if(16===n&&o instanceof Uint8Array)throw new RangeError(`bitDepth is ${n} but data is Uint8Array`);const t=this.size*this.channels;if(o.length!==t)throw new RangeError(`incorrect data size: ${o.length}. Expected ${t}`);this.data=o}}get normalizedResolution(){if(!this.originalResolution)return;switch(this.originalResolution.unit){case"inch":return{x:this.originalResolution.x/2.54,y:this.originalResolution.y/2.54};case"centimeter":return{x:this.originalResolution.x,y:this.originalResolution.y};case"meter":return{x:this.originalResolution.x/100,y:this.originalResolution.y/100};case"unknown":return null;default:throw new Error("Unknown resolution unit.")}}static createFrom(t,e={}){const{width:r=t.width,height:n=t.height}=e;let o;return o=t instanceof Image?t.bitDepth:8,new Image(r,n,{bitDepth:o,colorModel:t.colorModel,origin:t.origin,...e})}getPixel(t,e){const r=[],n=(e*this.width+t)*this.channels;for(let t=0;t<this.channels;t++)r.push(this.data[n+t]);return r}getColumn(t){const e=[];for(let r=0;r<this.channels;r++){const n=[];for(let e=0;e<this.height;e++)n.push(this.getValue(t,e,r));e.push(n)}return e}getRow(t){const e=[];for(let r=0;r<this.channels;r++){const n=[];for(let e=0;e<this.width;e++)n.push(this.getValue(e,t,r));e.push(n)}return e}setPixel(t,e,r){const n=(e*this.width+t)*this.channels;for(let t=0;t<this.channels;t++)this.data[n+t]=r[t]}setVisiblePixel(t,e,r){t>=0&&t<this.width&&e>=0&&e<this.height&&this.setPixel(t,e,r)}getPixelByIndex(t){const e=[],r=t*this.channels;for(let t=0;t<this.channels;t++)e.push(this.data[r+t]);return e}setPixelByIndex(t,e){const r=t*this.channels;for(let t=0;t<this.channels;t++)this.data[r+t]=e[t]}getValue(t,e,r){return this.data[(e*this.width+t)*this.channels+r]}setValue(t,e,r,n){this.data[(e*this.width+t)*this.channels+r]=n}setClampedValue(t,e,r,n){n<0?n=0:n>this.maxValue&&(n=this.maxValue),this.data[(e*this.width+t)*this.channels+r]=n}getValueByIndex(t,e){return this.data[t*this.channels+e]}setValueByIndex(t,e,r){this.data[t*this.channels+e]=r}setClampedValueByIndex(t,e,r){r<0?r=0:r>this.maxValue&&(r=this.maxValue),this.data[t*this.channels+e]=r}getValueByPoint(t,e){return this.getValue(t.column,t.row,e)}setValueByPoint(t,e,r){this.setValue(t.column,t.row,e,r)}minMax(){return getMinMax(this)}getRawImage(){return{width:this.width,height:this.height,data:this.data,channels:this.channels,bitDepth:this.bitDepth}}[Symbol.for("nodejs.util.inspect.custom")](){let t;return t=this.height>20||this.width>20?"[...]":printData(this),`Image {\n width: ${this.width}\n height: ${this.height}\n bitDepth: ${this.bitDepth}\n colorModel: ${this.colorModel}\n channels: ${this.channels}\n data: ${t}\n}`}fill(t){if("number"==typeof t)return validateValue(t,this),this.data.fill(t),this;if(t.length!==this.channels)throw new RangeError(`the size of value must match the number of channels (${this.channels}). Received ${t.length}`);for(const e of t)validateValue(e,this);for(let e=0;e<this.data.length;e+=this.channels)for(let r=0;r<=this.channels;r++)this.data[e+r]=t[r];return this}fillChannel(t,e){validateChannel(t,this),validateValue(e,this);for(let r=t;r<this.data.length;r+=this.channels)this.data[r]=e;return this}getChannel(t){validateChannel(t,this);const e=new Array(this.size);for(let r=0;r<this.size;r++)e[r]=this.data[t+r*this.channels];return e}fillAlpha(t){if(validateValue(t,this),!this.alpha)throw new TypeError("fillAlpha can only be called if the image has an alpha channel");const e=this.channels-1;return this.fillChannel(e,t)}clone(){return Image.createFrom(this,{data:this.data.slice()})}changeEach(t){for(let e=0;e<this.data.length;e++)this.data[e]=t(this.data[e])}getCoordinates(t,e=!1){return M$2(t).with("center",()=>{const t=(this.width-1)/2,r=(this.height-1)/2;return e?{column:Math.round(t),row:Math.round(r)}:{column:t,row:r}}).with("top-left",()=>({column:0,row:0})).with("top-right",()=>({column:this.width-1,row:0})).with("bottom-left",()=>({column:0,row:this.height-1})).with("bottom-right",()=>({column:this.width-1,row:this.height-1})).exhaustive()}subtract(t,e={}){return subtract(this,t,e)}add(t){return add(this,t)}multiply(t,e={}){return multiply(this,t,e)}divide(t,e={}){return divide(this,t,e)}histogram(t){return histogram$1(this,t)}mean(t){return mean(this,t)}median(t){return median(this,t)}variance(t){return variance(this,t)}drawPoints(t,e={}){return drawPoints(this,t,e)}drawLine(t,e,r={}){return drawLineOnImage(this,t,e,r)}drawRectangle(t={}){return drawRectangle(this,t)}drawPolyline(t,e={}){return drawPolylineOnImage(this,t,e)}drawPolygon(t,e={}){return drawPolygonOnImage(this,t,e)}drawCircle(t,e,r={}){return drawCircleOnImage(this,t,e,r)}drawMarker(t,e={}){return drawMarker(this,t,e)}drawMarkers(t,e={}){return drawMarkers(this,t,e)}split(){return split(this)}convertColor(t,e){return convertColor(this,t,e)}convertBitDepth(t,e){return convertBitDepth(this,t,e)}grey(t){return grey(this,t)}copyTo(t,e={}){return copyTo(this,t,e)}threshold(t={}){return threshold(this,t)}crop(t){return crop(this,t)}cropRectangle(t,e){return cropRectangle(this,t,e)}cropAlpha(t={}){return cropAlpha(this,t)}extract(t,e){return extract(this,t,e)}paintMask(t,e){return paintMaskOnImage(this,t,e)}blur(t){return blur(this,t)}pixelate(t){return pixelate(this,t)}directConvolution(t,e){return directConvolution(this,t,e)}rawDirectConvolution(t,e){return rawDirectConvolution(this,t,e)}separableConvolution(t,e,r){return separableConvolution(this,t,e,r)}gaussianBlur(t){return gaussianBlur(this,t)}flip(t){return flip(this,t)}invert(t){return invert(this,t)}hypotenuse(t,e){return hypotenuse(this,t,e)}gradientFilter(t){return gradientFilter(this,t)}derivativeFilter(t){return derivativeFilter(this,t)}level(t){return level(this,t)}increaseContrast(t={}){return increaseContrast(this,t)}correctColor(t,e){return correctColor(this,t,e)}medianFilter(t){return medianFilter(this,t)}resize(t){return resize(this,t)}rotate(t){return rotate(this,t)}transform(t,e){return transform(this,t,e)}transformRotate(t,e){return transformRotate(this,t,e)}erode(t){return erode(this,t)}dilate(t){return dilate(this,t)}open(t){return open(this,t)}close(t){return close(this,t)}topHat(t){return topHat(this,t)}bottomHat(t){return bottomHat(this,t)}morphologicalGradient(t){return morphologicalGradient(this,t)}cannyEdgeDetector(t){return cannyEdgeDetector(this,t)}}function createPixelArray(t,e,r,n,o){const i=e*t,s=M$2(n).with(8,()=>new Uint8Array(i)).with(16,()=>new Uint16Array(i)).otherwise(()=>{throw new RangeError(`invalid bitDepth: ${n}`)});if(r)for(let t=e-1;t<i;t+=e)s[t]=o;return s}function printData(t){const e=[],r=8===t.bitDepth?3:5;for(let n=0;n<t.height;n++){const o=[];for(let e=0;e<t.width;e++)for(let i=0;i<t.channels;i++)o.push(String(t.getValue(e,n,i)).padStart(r," "));e.push(`[${o.join(" ")}]`)}return`{\n [\n ${e.join("\n ")}\n ]\n }`}function copyData(t,e){if(e.width!==t.width||e.height!==t.height||e.colorModel!==t.colorModel)throw new RangeError("images width, height or color model is different");e.data=t.data.slice()}function getOutputImage(t,e={},r={}){const{out:n}=e,{newParameters:o,clone:i}=r;if(void 0===n)return i?t.clone():Image.createFrom(t,o);if(!(n instanceof Image))throw new TypeError("out must be an Image");return checkRequirements({width:t.width,height:t.height,bitDepth:t.bitDepth,colorModel:t.colorModel,...o},n),i&&t!==n&&copyData(t,n),n}function maskToOutputImage(t,e={}){const{out:r}=e;if(void 0===r)return Image.createFrom(t,{colorModel:"GREY"});if(!(r instanceof Image))throw new TypeError("out must be an Image");return checkRequirements({width:t.width,height:t.height,bitDepth:8,colorModel:"GREY"},r),r}function imageToOutputMask(t,e={}){const{out:r}=e;if(void 0===r)return Mask.createFrom(t);if(!(r instanceof Mask))throw new TypeError("out must be a Mask");return checkRequirements({width:t.width,height:t.height,bitDepth:1,colorModel:"BINARY"},r),r}function checkRequirements(t,e){for(const r in t){const n=r;if(e[n]!==t[n])throw new RangeError(`cannot use out image. Its ${r} property must be ${t[n]}. Received ${e[n]}`)}}function maskToOutputMask(t,e={},r={}){const{out:n}=e,{newParameters:o,clone:i}=r;if(void 0===n)return i?t.clone():Mask.createFrom(t,o);if(!(n instanceof Mask))throw new TypeError("out must be a Mask");return checkRequirements({width:t.width,height:t.height,bitDepth:1,colorModel:"BINARY"},n),i&&t!==n&&copyData(t,n),n}function getColors(t,e,r={}){const{nbShades:n=6,minValueFactor:o=.2}=r,i=Math.max(...e),s=i*o,a=Math.floor((i-s)/(n-1)),l=getClampFromTo(0,t.maxValue),h=[];for(let t=0;t<n;t++){const r=[];for(const n of e)r.push(l(n-t*a));h.push(r)}return h}function getKeypointColor(t,e,r){const n=t[0].score,o=t.at(-1).score;if(o===n)return r[0];const i=t[e].score;return r[r.length-1-Math.floor((r.length-1)*(i-o)/(n-o))]}function drawKeypoints(t,e,r={}){const{markerSize:n=10,fill:o=!1,showScore:i=!1,origin:s={row:0,column:0},showScoreOptions:a}=r;let{maxNbKeypoints:l=e.length}=r;const{strokeColor:h=[255,0,0]}=r;l>e.length&&(l=e.length);let c=getOutputImage(t,r,{clone:!0});"RGB"!==t.colorModel&&(c=c.convertColor("RGB"));const u=getColors(t,h,a),f=Math.ceil(n/2);for(let t=0;t<l;t++){const n=e[t];let a=h;i&&(a=getKeypointColor(e,t,u));const l=o?a:void 0,d=sum$1(n.origin,s);if(c.drawCircle(d,f,{fillColor:l,strokeColor:a,out:c}),isOrientedFastKeypoint(n)&&r.showOrientation){const t=d,e=n.angle*Math.PI/180,r={column:t.column+Math.round(f*Math.cos(e)),row:t.row-Math.round(f*Math.sin(e))};c.drawLine(t,r,{strokeColor:a,out:c})}}return c}function isOrientedFastKeypoint(t){return"angle"in t&&"number"==typeof t.angle}function getMatchColor(t,e,r){const n=t.at(-1).distance,o=t[0].distance;if(o===n)return r[0];const i=t[e].distance;return r[Math.floor((r.length-1)*(i-o)/(n-o))]}function sortByDistance(t){const e=t.slice();return e.sort((t,e)=>t.distance-e.distance),e}function scaleKeypoints(t,e){return 1===e?t.slice():t.map(t=>({origin:{row:t.origin.row*e,column:t.origin.column*e},score:t.score}))}function drawMatches(t,e,r,n,o={}){const{circleDiameter:i=10,strokeColor:s=[255,0,0],showDistance:a=!1,showDistanceOptions:l}=o;let{maxNbMatches:h=e.length}=o;h>e.length&&(h=e.length);const c=scaleKeypoints(r,t.scale),u=scaleKeypoints(n,t.scale),f=t.image,d=getColors(f,s,l),m=Math.ceil(i/2),g=sortByDistance(e);for(let r=0;r<h;r++){let n=s;a&&(n=getMatchColor(g,r,d));const o=c[e[r].sourceIndex].origin;f.drawCircle(o,m,{strokeColor:n,out:f});const i=u[e[r].destinationIndex].origin,l={column:i.column+t.destinationOrigin.column,row:i.row+t.destinationOrigin.row};f.drawCircle(l,m,{strokeColor:n,out:f}),f.drawLine(o,l,{out:f,strokeColor:n})}return f}const MontageDisposition={HORIZONTAL:"horizontal",VERTICAL:"vertical"};class Montage{sourceWidth;sourceHeight;destinationWidth;destinationHeight;destinationOrigin;width;height;scale;disposition;image;constructor(t,e,r={}){const{scale:n=1,disposition:o="horizontal"}=r;if(!Number.isInteger(n))throw new TypeError("scale must be an integer");if(this.scale=n,this.disposition=o,this.sourceWidth=n*t.width,this.destinationWidth=n*e.width,this.sourceHeight=n*t.height,this.destinationHeight=n*e.height,"horizontal"===o)this.destinationOrigin={row:0,column:this.sourceWidth},this.width=this.sourceWidth+this.destinationWidth,this.height=Math.max(this.sourceHeight,this.destinationHeight);else{if("vertical"!==o)throw new RangeError(`invalid disposition type: ${o}`);this.destinationOrigin={row:this.sourceHeight,column:0},this.width=Math.max(this.sourceWidth,this.destinationWidth),this.height=this.sourceHeight+this.destinationHeight}"RGB"!==t.colorModel&&(t=t.convertColor("RGB")),"RGB"!==e.colorModel&&(e=e.convertColor("RGB"));const i=new Image(this.width,this.height);t.resize({xFactor:n,yFactor:n}).copyTo(i,{out:i}),e.resize({xFactor:n,yFactor:n}).copyTo(i,{out:i,origin:this.destinationOrigin}),this.image=i}drawKeypoints(t,e={}){const r=scaleKeypoints(t,this.scale);this.image=drawKeypoints(this.image,r,e)}drawMatches(t,e,r,n={}){this.image=drawMatches(this,t,e,r,n)}}function overlapImages(t,e,r={}){const{origin:n={row:0,column:0},angle:o=0,scale:i=1}=r;if(0===i)throw new Error("Scale cannot be 0");t.colorModel!==ImageColorModel.GREY&&(t=t.grey()),e.colorModel!==ImageColorModel.GREY&&(e=e.grey());const s=t.invert(),a=e.invert(),l=s.transformRotate(o,{center:ImageCoordinates.TOP_LEFT}).resize({xFactor:i,yFactor:i}),h=Image.createFrom(a);return merge([l.copyTo(h,{origin:n}),a,h])}function filterEuclideanDistance(t,e,r,n={}){const{origin:o={column:0,row:0}}=n,i=t.slice();i.sort((t,e)=>t.destinationIndex-e.destinationIndex);const s=[];let a=[],l=i[0].destinationIndex;for(const t of i)t.destinationIndex===l?a.push(t):(a.sort((t,e)=>h(t)-h(e)),s.push(...a.slice(0,1)),l=t.destinationIndex,a=[t]);return a.sort((t,e)=>h(t)-h(e)),s.push(...a.slice(0,1)),s;function h(t){return(e[t.sourceIndex].origin.row-(r[t.destinationIndex].origin.row-o.row))**2+(e[t.sourceIndex].origin.column-r[t.destinationIndex].origin.column-o.column)**2}}function decode$4(t,e="utf8"){return new TextDecoder(e).decode(t)}const encoder$1=new TextEncoder;function encode$3(t){return encoder$1.encode(t)}const defaultByteLength=8192,hostBigEndian=(()=>{const t=new Uint8Array(4);return!((new Uint32Array(t.buffer)[0]=1)&t[0])})(),typedArrays={int8:globalThis.Int8Array,uint8:globalThis.Uint8Array,int16:globalThis.Int16Array,uint16:globalThis.Uint16Array,int32:globalThis.Int32Array,uint32:globalThis.Uint32Array,uint64:globalThis.BigUint64Array,int64:globalThis.BigInt64Array,float32:globalThis.Float32Array,float64:globalThis.Float64Array};class IOBuffer{buffer;byteLength;byteOffset;length;offset;lastWrittenByte;littleEndian;_data;_mark;_marks;constructor(t=defaultByteLength,e={}){let r=!1;"number"==typeof t?t=new ArrayBuffer(t):(r=!0,this.lastWrittenByte=t.byteLength);const n=e.offset?e.offset>>>0:0,o=t.byteLength-n;let i=n;(ArrayBuffer.isView(t)||t instanceof IOBuffer)&&(t.byteLength!==t.buffer.byteLength&&(i=t.byteOffset+n),t=t.buffer),this.lastWrittenByte=r?o:0,this.buffer=t,this.length=o,this.byteLength=o,this.byteOffset=i,this.offset=0,this.littleEndian=!0,this._data=new DataView(this.buffer,i,o),this._mark=0,this._marks=[]}available(t=1){return this.offset+t<=this.length}isLittleEndian(){return this.littleEndian}setLittleEndian(){return this.littleEndian=!0,this}isBigEndian(){return!this.littleEndian}setBigEndian(){return this.littleEndian=!1,this}skip(t=1){return this.offset+=t,this}back(t=1){return this.offset-=t,this}seek(t){return this.offset=t,this}mark(){return this._mark=this.offset,this}reset(){return this.offset=this._mark,this}pushMark(){return this._marks.push(this.offset),this}popMark(){const t=this._marks.pop();if(void 0===t)throw new Error("Mark stack empty");return this.seek(t),this}rewind(){return this.offset=0,this}ensureAvailable(t=1){if(!this.available(t)){const e=2*(this.offset+t),r=new Uint8Array(e);r.set(new Uint8Array(this.buffer)),this.buffer=r.buffer,this.length=e,this.byteLength=e,this._data=new DataView(this.buffer)}return this}readBoolean(){return 0!==this.readUint8()}readInt8(){return this._data.getInt8(this.offset++)}readUint8(){return this._data.getUint8(this.offset++)}readByte(){return this.readUint8()}readBytes(t=1){return this.readArray(t,"uint8")}readArray(t,e){const r=typedArrays[e].BYTES_PER_ELEMENT*t,n=this.byteOffset+this.offset,o=this.buffer.slice(n,n+r);if(this.littleEndian===hostBigEndian&&"uint8"!==e&&"int8"!==e){const t=new Uint8Array(this.buffer.slice(n,n+r));t.reverse();const o=new typedArrays[e](t.buffer);return this.offset+=r,o.reverse(),o}const i=new typedArrays[e](o);return this.offset+=r,i}readInt16(){const t=this._data.getInt16(this.offset,this.littleEndian);return this.offset+=2,t}readUint16(){const t=this._data.getUint16(this.offset,this.littleEndian);return this.offset+=2,t}readInt32(){const t=this._data.getInt32(this.offset,this.littleEndian);return this.offset+=4,t}readUint32(){const t=this._data.getUint32(this.offset,this.littleEndian);return this.offset+=4,t}readFloat32(){const t=this._data.getFloat32(this.offset,this.littleEndian);return this.offset+=4,t}readFloat64(){const t=this._data.getFloat64(this.offset,this.littleEndian);return this.offset+=8,t}readBigInt64(){const t=this._data.getBigInt64(this.offset,this.littleEndian);return this.offset+=8,t}readBigUint64(){const t=this._data.getBigUint64(this.offset,this.littleEndian);return this.offset+=8,t}readChar(){return String.fromCharCode(this.readInt8())}readChars(t=1){let e="";for(let r=0;r<t;r++)e+=this.readChar();return e}readUtf8(t=1){return decode$4(this.readBytes(t))}decodeText(t=1,e="utf8"){return decode$4(this.readBytes(t),e)}writeBoolean(t){return this.writeUint8(t?255:0),this}writeInt8(t){return this.ensureAvailable(1),this._data.setInt8(this.offset++,t),this._updateLastWrittenByte(),this}writeUint8(t){return this.ensureAvailable(1),this._data.setUint8(this.offset++,t),this._updateLastWrittenByte(),this}writeByte(t){return this.writeUint8(t)}writeBytes(t){this.ensureAvailable(t.length);for(let e=0;e<t.length;e++)this._data.setUint8(this.offset++,t[e]);return this._updateLastWrittenByte(),this}writeInt16(t){return this.ensureAvailable(2),this._data.setInt16(this.offset,t,this.littleEndian),this.offset+=2,this._updateLastWrittenByte(),this}writeUint16(t){return this.ensureAvailable(2),this._data.setUint16(this.offset,t,this.littleEndian),this.offset+=2,this._updateLastWrittenByte(),this}writeInt32(t){return this.ensureAvailable(4),this._data.setInt32(this.offset,t,this.littleEndian),this.offset+=4,this._updateLastWrittenByte(),this}writeUint32(t){return this.ensureAvailable(4),this._data.setUint32(this.offset,t,this.littleEndian),this.offset+=4,this._updateLastWrittenByte(),this}writeFloat32(t){return this.ensureAvailable(4),this._data.setFloat32(this.offset,t,this.littleEndian),this.offset+=4,this._updateLastWrittenByte(),this}writeFloat64(t){return this.ensureAvailable(8),this._data.setFloat64(this.offset,t,this.littleEndian),this.offset+=8,this._updateLastWrittenByte(),this}writeBigInt64(t){return this.ensureAvailable(8),this._data.setBigInt64(this.offset,t,this.littleEndian),this.offset+=8,this._updateLastWrittenByte(),this}writeBigUint64(t){return this.ensureAvailable(8),this._data.setBigUint64(this.offset,t,this.littleEndian),this.offset+=8,this._updateLastWrittenByte(),this}writeChar(t){return this.writeUint8(t.charCodeAt(0))}writeChars(t){for(let e=0;e<t.length;e++)this.writeUint8(t.charCodeAt(e));return this}writeUtf8(t){return this.writeBytes(encode$3(t))}toArray(){return new Uint8Array(this.buffer,this.byteOffset,this.lastWrittenByte)}getWrittenByteLength(){return this.lastWrittenByte-this.byteOffset}_updateLastWrittenByte(){this.offset>this.lastWrittenByte&&(this.lastWrittenByte=this.offset)}}class BMPDecoder{bufferData;pixelDataOffset;width;height;bitsPerPixel;xPixelsPerMeter;yPixelsPerMeter;compression;colorMasks;constructor(t){this.bufferData=new IOBuffer(t);const e=this.bufferData.readBytes(2);if(66!==e[0]&&77!==e[1])throw new Error("This is not a BMP image or the encoding is not correct.");if(this.pixelDataOffset=this.bufferData.skip(8).readUint32(),this.width=this.bufferData.skip(4).readUint32(),this.height=this.bufferData.readUint32(),this.bitsPerPixel=this.bufferData.seek(28).readUint16(),1!==this.bitsPerPixel&&8!==this.bitsPerPixel&&24!==this.bitsPerPixel&&32!==this.bitsPerPixel)throw new Error(`Invalid number of bits per pixel. Supported number of bits per pixel: 1, 8, 24, 32. Received: ${this.bitsPerPixel}`);if(this.compression=this.bufferData.readUint32(),0!==this.compression&&3!==this.compression)throw new Error("Only BI_RGB and BI_BITFIELDS compression methods are allowed. ");if(this.colorMasks=[this.bufferData.seek(54).readUint32(),this.bufferData.readUint32(),this.bufferData.readUint32()],32===this.bitsPerPixel&&(16711680!==this.colorMasks[0]||65280!==this.colorMasks[1]||255!==this.colorMasks[2]))throw new Error(`Unsupported color masks detected in 32-bit BMP image. Only standard RGBA (${16711680..toString(16)}, ${65280..toString(16)}, ${255..toString(16)}) masks are supported. Received: ${this.colorMasks[0].toString(16)},${this.colorMasks[1].toString(16)},${this.colorMasks[2].toString(16)}.`);this.bufferData.skip(1),this.xPixelsPerMeter=this.bufferData.seek(38).readInt32(),this.yPixelsPerMeter=this.bufferData.readInt32(),this.bufferData.skip(1)}decode(){this.bufferData.seek(this.pixelDataOffset),this.bufferData.setBigEndian();const t=Math.ceil(this.bitsPerPixel/8),e=t%2==0?t-1:t,r=this.decodePixelData(t,e);return{width:this.width,height:this.height,bitsPerPixel:this.bitsPerPixel,compression:this.compression,colorMasks:this.colorMasks,channels:t,components:e,data:r,yPixelsPerMeter:this.yPixelsPerMeter,xPixelsPerMeter:this.xPixelsPerMeter}}decodePixelData(t,e){const r=new Uint8Array(this.height*this.width*t);return 1===this.bitsPerPixel?this.decodeBitDepth1Pixels(r):t===e?this.decodeStandardPixels(r,t):this.decodePixelsWithAlpha(r,t,e),r}decodeBitDepth1Pixels(t){let e=0;for(let r=0;r<this.height;r++)for(let n=0;n<this.width;n++){const o=n%32;0===o&&(e=this.bufferData.readUint32()),e&1<<31-o&&(t[(this.height-r-1)*this.width+n]=1)}}decodeStandardPixels(t,e){const r=this.calculatePadding(e);for(let n=0;n<this.height;n++){const o=(this.height-n-1)*this.width;for(let r=0;r<this.width;r++)for(let n=e-1;n>=0;n--)t[(o+r)*e+n]=this.bufferData.readByte();this.bufferData.skip(r)}}decodePixelsWithAlpha(t,e,r){for(let n=0;n<this.height;n++){const o=(this.height-n-1)*this.width;for(let n=0;n<this.width;n++){const i=(o+n)*e;for(let e=r-1;e>=0;e--)t[i+e]=this.bufferData.readByte();t[i+r]=this.bufferData.readByte()}}}calculatePadding(t){return this.width*t%4==0?0:4-this.width*t%4}}const BITMAPV5HEADER={LogicalColorSpace:{LCS_sRGB:1934772034},GamutMappingIntent:{LCS_GM_GRAPHICS:2},DEFAULT_PIXELS_PER_METER:2835};class BMPEncoder{width;height;bitsPerPixel;channels;components;data;xPixelsPerMeter;yPixelsPerMeter;encoded=new IOBuffer;compression;colorMasks;constructor(t){if(!t.height||!t.width)throw new Error("ImageData width and height are required.");if(this.data=t.data,this.width=t.width,this.height=t.height,this.data.length!==t.width*t.height*t.channels)throw new Error("Invalid data length.");if(this.bitsPerPixel=t.bitsPerPixel,1!==this.bitsPerPixel&&8!==this.bitsPerPixel&&24!==this.bitsPerPixel&&32!==this.bitsPerPixel)throw new Error(`Invalid number of bits per pixel. Supported number of bits per pixel: 1, 8, 24, 32. Received: ${this.bitsPerPixel}`);this.channels=t.channels,this.components=this.channels%2==0?this.channels-1:this.channels,this.xPixelsPerMeter=t.xPixelsPerMeter??BITMAPV5HEADER.DEFAULT_PIXELS_PER_METER,this.yPixelsPerMeter=t.yPixelsPerMeter??BITMAPV5HEADER.DEFAULT_PIXELS_PER_METER,this.compression=t.compression??0,this.colorMasks=t.colorMasks??[16711680,65280,255]}encode(){this.encoded=new IOBuffer,this.encoded.skip(14),this.writeBitmapV5Header(),this.bitsPerPixel<=8&&this.writeColorTable();const t=this.encoded.offset;this.writePixelArray();const e=this.encoded.getWrittenByteLength();return this.encoded.rewind(),this.writeBitmapFileHeader(t,e),this.encoded.toArray()}writePixelArray(){this.encoded.setBigEndian(),1===this.bitsPerPixel?this.writeBitDepth1Pixels():this.channels===this.components?this.writeStandardPixels():this.writePixelsWithAlpha(),this.encoded.setLittleEndian()}writeBitDepth1Pixels(){let t=0;for(let e=this.height-1;e>=0;e--){for(let r=0;r<this.width;r++)r%32==0&&0!==r&&(this.encoded.writeUint32(t),t=0),t|=this.data[e*this.width+r]<<31-r%32;this.encoded.writeUint32(t),t=0}}writeStandardPixels(){for(let t=0;t<this.height;t++){const e=this.width*(this.height-t-1)*this.channels;for(let t=0;t<this.width;t++)for(let r=this.channels-1;r>=0;r--)this.encoded.writeByte(this.data[e+t*this.channels+r]);this.writePadding()}}writePixelsWithAlpha(){for(let t=0;t<this.height;t++){const e=this.width*(this.height-t-1);for(let t=0;t<this.width;t++){const r=(e+t)*this.channels;this.encoded.writeUint32(this.data[r+2]<<24|this.data[r+1]<<16|this.data[r]<<8|this.data[r+3])}}}writePadding(){const t=this.width*this.channels%4==0?0:4-this.width*this.channels%4;for(let e=0;e<t;e++)this.encoded.writeByte(0)}writeColorTable(){if(1===this.bitsPerPixel)this.encoded.writeUint32(0).writeUint32(16777215);else for(let t=0;t<256;t++)this.encoded.writeUint32(t<<16|t<<8|t)}writeBitmapFileHeader(t,e){this.encoded.writeChars("BM").writeInt32(e).writeUint16(0).writeUint16(0).writeUint32(t)}writeBitmapV5Header(){const t=4*Math.floor((this.bitsPerPixel*this.width+31)/32)*this.height;if(this.encoded.writeUint32(124).writeInt32(this.width).writeInt32(this.height).writeUint16(1).writeUint16(this.bitsPerPixel).writeUint32(this.compression).writeUint32(t).writeInt32(this.xPixelsPerMeter).writeInt32(this.yPixelsPerMeter).writeUint32(this.bitsPerPixel<=8?2**this.bitsPerPixel:0).writeUint32(this.bitsPerPixel<=8?2**this.bitsPerPixel:0),32===this.bitsPerPixel&&(16711680!==this.colorMasks[0]||65280!==this.colorMasks[1]||255!==this.colorMasks[2]))throw new Error(`Unsupported color masks detected in 32-bit BMP image. Only standard RGBA (${16711680..toString(16)}, ${65280..toString(16)}, ${255..toString(16)}) masks are supported. Received: ${this.colorMasks[0].toString(16)},${this.colorMasks[1].toString(16)},${this.colorMasks[2].toString(16)}.`);this.encoded.writeUint32(this.colorMasks[0]).writeUint32(this.colorMasks[1]).writeUint32(this.colorMasks[2]).writeUint32(this.channels===this.components?0:4278190080).writeUint32(BITMAPV5HEADER.LogicalColorSpace.LCS_sRGB).skip(36).skip(12).writeUint32(BITMAPV5HEADER.GamutMappingIntent.LCS_GM_GRAPHICS).skip(12)}}function encode$2(t){return new BMPEncoder(t).encode()}function decode$3(t){return new BMPDecoder(t).decode()}function encodeBmp(t){return encode$2(t instanceof Image?{width:t.width,height:t.height,components:t.components,bitsPerPixel:t.channels*t.bitDepth,channels:t.channels,data:t.getRawImage().data}:{width:t.width,height:t.height,components:1,bitsPerPixel:1,channels:1,data:t.getRawImage().data})}var encoder={exports:{}};!function(t){function e(t){var e,r,n,o,i,s=Math.floor,a=new Array(64),l=new Array(64),h=new Array(64),c=new Array(64),u=new Array(65535),f=new Array(65535),d=new Array(64),m=new Array(64),g=[],p=0,w=7,y=new Array(64),b=new Array(64),x=new Array(64),M=new Array(256),v=new Array(2048),I=[0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63],A=[0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0],k=[0,1,2,3,4,5,6,7,8,9,10,11],R=[0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125],E=[1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193,21,82,209,240,36,51,98,114,130,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,225,226,227,228,229,230,231,232,233,234,241,242,243,244,245,246,247,248,249,250],$=[0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0],P=[0,1,2,3,4,5,6,7,8,9,10,11],S=[0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119],C=[0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,129,8,20,66,145,161,177,193,9,35,51,82,240,21,98,114,209,10,22,36,52,225,37,241,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,130,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,226,227,228,229,230,231,232,233,234,242,243,244,245,246,247,248,249,250];function B(t,e){for(var r=0,n=0,o=new Array,i=1;i<=16;i++){for(var s=1;s<=t[i];s++)o[e[n]]=[],o[e[n]][0]=r,o[e[n]][1]=i,n++,r++;r*=2}return o}function T(t){for(var e=t[0],r=t[1]-1;r>=0;)e&1<<r&&(p|=1<<w),r--,--w<0&&(255==p?(D(255),D(0)):D(p),w=7,p=0)}function D(t){g.push(t)}function _(t){D(t>>8&255),D(255&t)}function V(t,e,r,n,o){for(var i,s=o[0],a=o[240],l=function(t,e){var r,n,o,i,s,a,l,h,c,u,f=0;for(c=0;c<8;++c){r=t[f],n=t[f+1],o=t[f+2],i=t[f+3],s=t[f+4],a=t[f+5],l=t[f+6];var m=r+(h=t[f+7]),g=r-h,p=n+l,w=n-l,y=o+a,b=o-a,x=i+s,M=i-s,v=m+x,I=m-x,A=p+y,k=p-y;t[f]=v+A,t[f+4]=v-A;var R=.707106781*(k+I);t[f+2]=I+R,t[f+6]=I-R;var E=.382683433*((v=M+b)-(k=w+g)),$=.5411961*v+E,P=1.306562965*k+E,S=.707106781*(A=b+w),C=g+S,B=g-S;t[f+5]=B+$,t[f+3]=B-$,t[f+1]=C+P,t[f+7]=C-P,f+=8}for(f=0,c=0;c<8;++c){r=t[f],n=t[f+8],o=t[f+16],i=t[f+24],s=t[f+32],a=t[f+40],l=t[f+48];var T=r+(h=t[f+56]),D=r-h,_=n+l,V=n-l,N=o+a,O=o-a,F=i+s,L=i-s,U=T+F,z=T-F,G=_+N,q=_-N;t[f]=U+G,t[f+32]=U-G;var Y=.707106781*(q+z);t[f+16]=z+Y,t[f+48]=z-Y;var H=.382683433*((U=L+O)-(q=V+D)),j=.5411961*U+H,W=1.306562965*q+H,K=.707106781*(G=O+V),X=D+K,J=D-K;t[f+40]=J+j,t[f+24]=J-j,t[f+8]=X+W,t[f+56]=X-W,f++}for(c=0;c<64;++c)u=t[c]*e[c],d[c]=u>0?u+.5|0:u-.5|0;return d}(t,e),h=0;h<64;++h)m[I[h]]=l[h];var c=m[0]-r;r=m[0],0==c?T(n[0]):(T(n[f[i=32767+c]]),T(u[i]));for(var g=63;g>0&&0==m[g];g--);if(0==g)return T(s),r;for(var p,w=1;w<=g;){for(var y=w;0==m[w]&&w<=g;++w);var b=w-y;if(b>=16){p=b>>4;for(var x=1;x<=p;++x)T(a);b&=15}i=32767+m[w],T(o[(b<<4)+f[i]]),T(u[i]),w++}return 63!=g&&T(s),r}function N(t){if(t<=0&&(t=1),t>100&&(t=100),i!=t){(function(t){for(var e=[16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22,37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99],r=0;r<64;r++){var n=s((e[r]*t+50)/100);n<1?n=1:n>255&&(n=255),a[I[r]]=n}for(var o=[17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99],i=0;i<64;i++){var u=s((o[i]*t+50)/100);u<1?u=1:u>255&&(u=255),l[I[i]]=u}for(var f=[1,1.387039845,1.306562965,1.175875602,1,.785694958,.5411961,.275899379],d=0,m=0;m<8;m++)for(var g=0;g<8;g++)h[d]=1/(a[I[d]]*f[m]*f[g]*8),c[d]=1/(l[I[d]]*f[m]*f[g]*8),d++})(t<50?Math.floor(5e3/t):Math.floor(200-2*t)),i=t}}this.encode=function(t,i){var s;(new Date).getTime(),i&&N(i),g=new Array,p=0,w=7,_(65496),_(65504),_(16),D(74),D(70),D(73),D(70),D(0),D(1),D(1),D(0),_(1),_(1),D(0),D(0),void 0!==(s=t.comments)&&s.constructor===Array&&s.forEach(t=>{if("string"==typeof t){_(65534);var e,r=t.length;for(_(r+2),e=0;e<r;e++)D(t.charCodeAt(e))}}),function(t){if(t){_(65505),69===t[0]&&120===t[1]&&105===t[2]&&102===t[3]?_(t.length+2):(_(t.length+5+2),D(69),D(120),D(105),D(102),D(0));for(var e=0;e<t.length;e++)D(t[e])}}(t.exifBuffer),function(){_(65499),_(132),D(0);for(var t=0;t<64;t++)D(a[t]);D(1);for(var e=0;e<64;e++)D(l[e])}(),function(t,e){_(65472),_(17),D(8),_(e),_(t),D(3),D(1),D(17),D(0),D(2),D(17),D(1),D(3),D(17),D(1)}(t.width,t.height),function(){_(65476),_(418),D(0);for(var t=0;t<16;t++)D(A[t+1]);for(var e=0;e<=11;e++)D(k[e]);D(16);for(var r=0;r<16;r++)D(R[r+1]);for(var n=0;n<=161;n++)D(E[n]);D(1);for(var o=0;o<16;o++)D($[o+1]);for(var i=0;i<=11;i++)D(P[i]);D(17);for(var s=0;s<16;s++)D(S[s+1]);for(var a=0;a<=161;a++)D(C[a])}(),_(65498),_(12),D(3),D(1),D(0),D(2),D(17),D(3),D(17),D(0),D(63),D(0);var u=0,f=0,d=0;p=0,w=7,this.encode.displayName="_encode_";for(var m,M,I,B,O,F,L,U,z,G=t.data,q=t.width,Y=t.height,H=4*q,j=0;j<Y;){for(m=0;m<H;){for(F=O=H*j+m,L=-1,U=0,z=0;z<64;z++)F=O+(U=z>>3)*H+(L=4*(7&z)),j+U>=Y&&(F-=H*(j+1+U-Y)),m+L>=H&&(F-=m+L-H+4),M=G[F++],I=G[F++],B=G[F++],y[z]=(v[M]+v[I+256|0]+v[B+512|0]>>16)-128,b[z]=(v[M+768|0]+v[I+1024|0]+v[B+1280|0]>>16)-128,x[z]=(v[M+1280|0]+v[I+1536|0]+v[B+1792|0]>>16)-128;u=V(y,h,u,e,n),f=V(b,c,f,r,o),d=V(x,c,d,r,o),m+=32}j+=8}if(w>=0){var W=[];W[1]=w+1,W[0]=(1<<w+1)-1,T(W)}return _(65497),Buffer.from(g)},(new Date).getTime(),t||(t=50),function(){for(var t=String.fromCharCode,e=0;e<256;e++)M[e]=t(e)}(),e=B(A,k),r=B($,P),n=B(R,E),o=B(S,C),function(){for(var t=1,e=2,r=1;r<=15;r++){for(var n=t;n<e;n++)f[32767+n]=r,u[32767+n]=[],u[32767+n][1]=r,u[32767+n][0]=n;for(var o=-(e-1);o<=-t;o++)f[32767+o]=r,u[32767+o]=[],u[32767+o][1]=r,u[32767+o][0]=e-1+o;t<<=1,e<<=1}}(),function(){for(var t=0;t<256;t++)v[t]=19595*t,v[t+256|0]=38470*t,v[t+512|0]=7471*t+32768,v[t+768|0]=-11059*t,v[t+1024|0]=-21709*t,v[t+1280|0]=32768*t+8421375,v[t+1536|0]=-27439*t,v[t+1792|0]=-5329*t}(),N(t),(new Date).getTime()}t.exports=function(t,r){void 0===r&&(r=50);var n=new e(r),o=n.encode(t,r);return{data:o,width:t.width,height:t.height}}}(encoder);var encoderExports=encoder.exports,decoder={exports:{}};!function(t){var e=function(){var t=new Int32Array([0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63]),e=4017,r=799,n=3406,o=2276,i=1567,s=3784,a=5793,l=2896;function h(){}function c(t,e){for(var r,n,o=0,i=[],s=16;s>0&&!t[s-1];)s--;i.push({children:[],index:0});var a,l=i[0];for(r=0;r<s;r++){for(n=0;n<t[r];n++){for((l=i.pop()).children[l.index]=e[o];l.index>0;){if(0===i.length)throw new Error("Could not recreate Huffman Table");l=i.pop()}for(l.index++,i.push(l);i.length<=r;)i.push(a={children:[],index:0}),l.children[l.index]=a.children,l=a;o++}r+1<s&&(i.push(a={children:[],index:0}),l.children[l.index]=a.children,l=a)}return i[0].children}function u(e,r,n,o,i,s,a,l,h,c){n.precision,n.samplesPerLine,n.scanLines;var u=n.mcusPerLine,f=n.progressive;n.maxH,n.maxV;var d=r,m=0,g=0;function p(){if(g>0)return g--,m>>g&1;if(255==(m=e[r++])){var t=e[r++];if(t)throw new Error("unexpected marker: "+(m<<8|t).toString(16))}return g=7,m>>>7}function w(t){for(var e,r=t;null!==(e=p());){if("number"==typeof(r=r[e]))return r;if("object"!=typeof r)throw new Error("invalid huffman sequence")}return null}function y(t){for(var e=0;t>0;){var r=p();if(null===r)return;e=e<<1|r,t--}return e}function b(t){var e=y(t);return e>=1<<t-1?e:e+(-1<<t)+1}var x=0;var M,v=0;function I(t,e,r,n,o){var i=r%u,s=(r/u|0)*t.v+n,a=i*t.h+o;void 0===t.blocks[s]&&c.tolerantDecoding||e(t,t.blocks[s][a])}function A(t,e,r){var n=r/t.blocksPerLine|0,o=r%t.blocksPerLine;void 0===t.blocks[n]&&c.tolerantDecoding||e(t,t.blocks[n][o])}var k,R,E,$,P,S,C=o.length;S=f?0===s?0===l?function(t,e){var r=w(t.huffmanTableDC),n=0===r?0:b(r)<<h;e[0]=t.pred+=n}:function(t,e){e[0]|=p()<<h}:0===l?function(e,r){if(x>0)x--;else for(var n=s,o=a;n<=o;){var i=w(e.huffmanTableAC),l=15&i,c=i>>4;if(0!==l)r[t[n+=c]]=b(l)*(1<<h),n++;else{if(c<15){x=y(c)+(1<<c)-1;break}n+=16}}}:function(e,r){for(var n=s,o=a,i=0;n<=o;){var l=t[n],c=r[l]<0?-1:1;switch(v){case 0:var u=w(e.huffmanTableAC),f=15&u;if(i=u>>4,0===f)i<15?(x=y(i)+(1<<i),v=4):(i=16,v=1);else{if(1!==f)throw new Error("invalid ACn encoding");M=b(f),v=i?2:3}continue;case 1:case 2:r[l]?r[l]+=(p()<<h)*c:0===--i&&(v=2==v?3:0);break;case 3:r[l]?r[l]+=(p()<<h)*c:(r[l]=M<<h,v=0);break;case 4:r[l]&&(r[l]+=(p()<<h)*c)}n++}4===v&&0===--x&&(v=0)}:function(e,r){var n=w(e.huffmanTableDC),o=0===n?0:b(n);r[0]=e.pred+=o;for(var i=1;i<64;){var s=w(e.huffmanTableAC),a=15&s,l=s>>4;if(0!==a)r[t[i+=l]]=b(a),i++;else{if(l<15)break;i+=16}}};var B,T,D,_,V=0;for(T=1==C?o[0].blocksPerLine*o[0].blocksPerColumn:u*n.mcusPerColumn,i||(i=T);V<T;){for(R=0;R<C;R++)o[R].pred=0;if(x=0,1==C)for(k=o[0],P=0;P<i;P++)A(k,S,V),V++;else for(P=0;P<i;P++){for(R=0;R<C;R++)for(D=(k=o[R]).h,_=k.v,E=0;E<_;E++)for($=0;$<D;$++)I(k,S,V,E,$);if(++V===T)break}if(V===T)do{if(255===e[r]&&0!==e[r+1])break;r+=1}while(r<e.length-2);if(g=0,(B=e[r]<<8|e[r+1])<65280)throw new Error("marker was not found");if(!(B>=65488&&B<=65495))break;r+=2}return r-d}function f(t,h){var c,u,f=[],d=h.blocksPerLine,m=h.blocksPerColumn,g=d<<3,w=new Int32Array(64),y=new Uint8Array(64);function b(t,c,u){var f,d,m,g,p,w,y,b,x,M,v=h.quantizationTable,I=u;for(M=0;M<64;M++)I[M]=t[M]*v[M];for(M=0;M<8;++M){var A=8*M;0!=I[1+A]||0!=I[2+A]||0!=I[3+A]||0!=I[4+A]||0!=I[5+A]||0!=I[6+A]||0!=I[7+A]?(f=a*I[0+A]+128>>8,d=a*I[4+A]+128>>8,m=I[2+A],g=I[6+A],p=l*(I[1+A]-I[7+A])+128>>8,b=l*(I[1+A]+I[7+A])+128>>8,w=I[3+A]<<4,y=I[5+A]<<4,x=f-d+1>>1,f=f+d+1>>1,d=x,x=m*s+g*i+128>>8,m=m*i-g*s+128>>8,g=x,x=p-y+1>>1,p=p+y+1>>1,y=x,x=b+w+1>>1,w=b-w+1>>1,b=x,x=f-g+1>>1,f=f+g+1>>1,g=x,x=d-m+1>>1,d=d+m+1>>1,m=x,x=p*o+b*n+2048>>12,p=p*n-b*o+2048>>12,b=x,x=w*r+y*e+2048>>12,w=w*e-y*r+2048>>12,y=x,I[0+A]=f+b,I[7+A]=f-b,I[1+A]=d+y,I[6+A]=d-y,I[2+A]=m+w,I[5+A]=m-w,I[3+A]=g+p,I[4+A]=g-p):(x=a*I[0+A]+512>>10,I[0+A]=x,I[1+A]=x,I[2+A]=x,I[3+A]=x,I[4+A]=x,I[5+A]=x,I[6+A]=x,I[7+A]=x)}for(M=0;M<8;++M){var k=M;0!=I[8+k]||0!=I[16+k]||0!=I[24+k]||0!=I[32+k]||0!=I[40+k]||0!=I[48+k]||0!=I[56+k]?(f=a*I[0+k]+2048>>12,d=a*I[32+k]+2048>>12,m=I[16+k],g=I[48+k],p=l*(I[8+k]-I[56+k])+2048>>12,b=l*(I[8+k]+I[56+k])+2048>>12,w=I[24+k],y=I[40+k],x=f-d+1>>1,f=f+d+1>>1,d=x,x=m*s+g*i+2048>>12,m=m*i-g*s+2048>>12,g=x,x=p-y+1>>1,p=p+y+1>>1,y=x,x=b+w+1>>1,w=b-w+1>>1,b=x,x=f-g+1>>1,f=f+g+1>>1,g=x,x=d-m+1>>1,d=d+m+1>>1,m=x,x=p*o+b*n+2048>>12,p=p*n-b*o+2048>>12,b=x,x=w*r+y*e+2048>>12,w=w*e-y*r+2048>>12,y=x,I[0+k]=f+b,I[56+k]=f-b,I[8+k]=d+y,I[48+k]=d-y,I[16+k]=m+w,I[40+k]=m-w,I[24+k]=g+p,I[32+k]=g-p):(x=a*u[M+0]+8192>>14,I[0+k]=x,I[8+k]=x,I[16+k]=x,I[24+k]=x,I[32+k]=x,I[40+k]=x,I[48+k]=x,I[56+k]=x)}for(M=0;M<64;++M){var R=128+(I[M]+8>>4);c[M]=R<0?0:R>255?255:R}}p(g*m*8);for(var x=0;x<m;x++){var M=x<<3;for(c=0;c<8;c++)f.push(new Uint8Array(g));for(var v=0;v<d;v++){b(h.blocks[x][v],y,w);var I=0,A=v<<3;for(u=0;u<8;u++){var k=f[M+u];for(c=0;c<8;c++)k[A+c]=y[I++]}}}return f}function d(t){return t<0?0:t>255?255:t}h.prototype={load:function(t){var e=new XMLHttpRequest;e.open("GET",t,!0),e.responseType="arraybuffer",e.onload=function(){var t=new Uint8Array(e.response||e.mozResponseArrayBuffer);this.parse(t),this.onload&&this.onload()}.bind(this),e.send(null)},parse:function(e){var r=1e3*this.opts.maxResolutionInMP*1e3,n=0;function o(){var t=e[n]<<8|e[n+1];return n+=2,t}function i(){var t=o(),r=e.subarray(n,n+t-2);return n+=r.length,r}function s(t){var e,r,n=1,o=1;for(r in t.components)t.components.hasOwnProperty(r)&&(n<(e=t.components[r]).h&&(n=e.h),o<e.v&&(o=e.v));var i=Math.ceil(t.samplesPerLine/8/n),s=Math.ceil(t.scanLines/8/o);for(r in t.components)if(t.components.hasOwnProperty(r)){e=t.components[r];var a=Math.ceil(Math.ceil(t.samplesPerLine/8)*e.h/n),l=Math.ceil(Math.ceil(t.scanLines/8)*e.v/o),h=i*e.h,c=s*e.v,u=[];p(256*(c*h));for(var f=0;f<c;f++){for(var d=[],m=0;m<h;m++)d.push(new Int32Array(64));u.push(d)}e.blocksPerLine=a,e.blocksPerColumn=l,e.blocks=u}t.maxH=n,t.maxV=o,t.mcusPerLine=i,t.mcusPerColumn=s}e.length;var a,l,h=null,d=null,m=[],g=[],w=[],y=[],b=o(),x=-1;if(this.comments=[],65496!=b)throw new Error("SOI not found");for(b=o();65497!=b;){switch(b){case 65280:break;case 65504:case 65505:case 65506:case 65507:case 65508:case 65509:case 65510:case 65511:case 65512:case 65513:case 65514:case 65515:case 65516:case 65517:case 65518:case 65519:case 65534:var M=i();if(65534===b){var v=String.fromCharCode.apply(null,M);this.comments.push(v)}65504===b&&74===M[0]&&70===M[1]&&73===M[2]&&70===M[3]&&0===M[4]&&(h={version:{major:M[5],minor:M[6]},densityUnits:M[7],xDensity:M[8]<<8|M[9],yDensity:M[10]<<8|M[11],thumbWidth:M[12],thumbHeight:M[13],thumbData:M.subarray(14,14+3*M[12]*M[13])}),65505===b&&69===M[0]&&120===M[1]&&105===M[2]&&102===M[3]&&0===M[4]&&(this.exifBuffer=M.subarray(5,M.length)),65518===b&&65===M[0]&&100===M[1]&&111===M[2]&&98===M[3]&&101===M[4]&&0===M[5]&&(d={version:M[6],flags0:M[7]<<8|M[8],flags1:M[9]<<8|M[10],transformCode:M[11]});break;case 65499:for(var I=o()+n-2;n<I;){var A=e[n++];p(256);var k=new Int32Array(64);if(A>>4){if(A>>4!=1)throw new Error("DQT: invalid table spec");for(j=0;j<64;j++){k[t[j]]=o()}}else for(j=0;j<64;j++){k[t[j]]=e[n++]}m[15&A]=k}break;case 65472:case 65473:case 65474:o(),(a={}).extended=65473===b,a.progressive=65474===b,a.precision=e[n++],a.scanLines=o(),a.samplesPerLine=o(),a.components={},a.componentsOrder=[];var R=a.scanLines*a.samplesPerLine;if(R>r){var E=Math.ceil((R-r)/1e6);throw new Error(`maxResolutionInMP limit exceeded by ${E}MP`)}var $,P=e[n++];for(Y=0;Y<P;Y++){$=e[n];var S=e[n+1]>>4,C=15&e[n+1],B=e[n+2];if(S<=0||C<=0)throw new Error("Invalid sampling factor, expected values above 0");a.componentsOrder.push($),a.components[$]={h:S,v:C,quantizationIdx:B},n+=3}s(a),g.push(a);break;case 65476:var T=o();for(Y=2;Y<T;){var D=e[n++],_=new Uint8Array(16),V=0;for(j=0;j<16;j++,n++)V+=_[j]=e[n];p(16+V);var N=new Uint8Array(V);for(j=0;j<V;j++,n++)N[j]=e[n];Y+=17+V,(D>>4?w:y)[15&D]=c(_,N)}break;case 65501:o(),l=o();break;case 65500:o(),o();break;case 65498:o();var O=e[n++],F=[];for(Y=0;Y<O;Y++){W=a.components[e[n++]];var L=e[n++];W.huffmanTableDC=y[L>>4],W.huffmanTableAC=w[15&L],F.push(W)}var U=e[n++],z=e[n++],G=e[n++],q=u(e,n,a,F,l,U,z,G>>4,15&G,this.opts);n+=q;break;case 65535:255!==e[n]&&n--;break;default:if(255==e[n-3]&&e[n-2]>=192&&e[n-2]<=254){n-=3;break}if(224===b||225==b){if(-1!==x)throw new Error(`first unknown JPEG marker at offset ${x.toString(16)}, second unknown JPEG marker ${b.toString(16)} at offset ${(n-1).toString(16)}`);x=n-1;const t=o();if(255===e[n+t-2]){n+=t-2;break}}throw new Error("unknown JPEG marker "+b.toString(16))}b=o()}if(1!=g.length)throw new Error("only single frame JPEGs supported");for(var Y=0;Y<g.length;Y++){var H=g[Y].components;for(var j in H)H[j].quantizationTable=m[H[j].quantizationIdx],delete H[j].quantizationIdx}this.width=a.samplesPerLine,this.height=a.scanLines,this.jfif=h,this.adobe=d,this.components=[];for(Y=0;Y<a.componentsOrder.length;Y++){var W=a.components[a.componentsOrder[Y]];this.components.push({lines:f(0,W),scaleX:W.h/a.maxH,scaleY:W.v/a.maxV})}},getData:function(t,e){var r,n,o,i,s,a,l,h,c,u,f,m,g,w,y,b,x,M,v,I,A,k=this.width/t,R=this.height/e,E=0,$=t*e*this.components.length;p($);var P=new Uint8Array($);switch(this.components.length){case 1:for(r=this.components[0],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],c=0;c<t;c++)f=s[0|c*r.scaleX*k],P[E++]=f;break;case 2:for(r=this.components[0],n=this.components[1],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],a=n.lines[0|u*n.scaleY*R],c=0;c<t;c++)f=s[0|c*r.scaleX*k],P[E++]=f,f=a[0|c*n.scaleX*k],P[E++]=f;break;case 3:for(A=!0,this.adobe&&this.adobe.transformCode?A=!0:void 0!==this.opts.colorTransform&&(A=!!this.opts.colorTransform),r=this.components[0],n=this.components[1],o=this.components[2],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],a=n.lines[0|u*n.scaleY*R],l=o.lines[0|u*o.scaleY*R],c=0;c<t;c++)A?(f=s[0|c*r.scaleX*k],m=a[0|c*n.scaleX*k],M=d(f+1.402*((g=l[0|c*o.scaleX*k])-128)),v=d(f-.3441363*(m-128)-.71413636*(g-128)),I=d(f+1.772*(m-128))):(M=s[0|c*r.scaleX*k],v=a[0|c*n.scaleX*k],I=l[0|c*o.scaleX*k]),P[E++]=M,P[E++]=v,P[E++]=I;break;case 4:if(!this.adobe)throw new Error("Unsupported color mode (4 components)");for(A=!1,this.adobe&&this.adobe.transformCode?A=!0:void 0!==this.opts.colorTransform&&(A=!!this.opts.colorTransform),r=this.components[0],n=this.components[1],o=this.components[2],i=this.components[3],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],a=n.lines[0|u*n.scaleY*R],l=o.lines[0|u*o.scaleY*R],h=i.lines[0|u*i.scaleY*R],c=0;c<t;c++)A?(f=s[0|c*r.scaleX*k],m=a[0|c*n.scaleX*k],g=l[0|c*o.scaleX*k],w=h[0|c*i.scaleX*k],y=255-d(f+1.402*(g-128)),b=255-d(f-.3441363*(m-128)-.71413636*(g-128)),x=255-d(f+1.772*(m-128))):(y=s[0|c*r.scaleX*k],b=a[0|c*n.scaleX*k],x=l[0|c*o.scaleX*k],w=h[0|c*i.scaleX*k]),P[E++]=255-y,P[E++]=255-b,P[E++]=255-x,P[E++]=255-w;break;default:throw new Error("Unsupported color mode")}return P},copyToImageData:function(t,e){var r,n,o,i,s,a,l,h,c,u=t.width,f=t.height,m=t.data,g=this.getData(u,f),p=0,w=0;switch(this.components.length){case 1:for(n=0;n<f;n++)for(r=0;r<u;r++)o=g[p++],m[w++]=o,m[w++]=o,m[w++]=o,e&&(m[w++]=255);break;case 3:for(n=0;n<f;n++)for(r=0;r<u;r++)l=g[p++],h=g[p++],c=g[p++],m[w++]=l,m[w++]=h,m[w++]=c,e&&(m[w++]=255);break;case 4:for(n=0;n<f;n++)for(r=0;r<u;r++)s=g[p++],a=g[p++],o=g[p++],l=255-d(s*(1-(i=g[p++])/255)+i),h=255-d(a*(1-i/255)+i),c=255-d(o*(1-i/255)+i),m[w++]=l,m[w++]=h,m[w++]=c,e&&(m[w++]=255);break;default:throw new Error("Unsupported color mode")}}};var m=0,g=0;function p(t=0){var e=m+t;if(e>g){var r=Math.ceil((e-g)/1024/1024);throw new Error(`maxMemoryUsageInMB limit exceeded by at least ${r}MB`)}m=e}return h.resetMaxMemoryUsage=function(t){m=0,g=t},h.getBytesAllocated=function(){return m},h.requestMemoryAllocation=p,h}();t.exports=function(t,r={}){var n={colorTransform:void 0,useTArray:!1,formatAsRGBA:!0,tolerantDecoding:!0,maxResolutionInMP:100,maxMemoryUsageInMB:512,...r},o=new Uint8Array(t),i=new e;i.opts=n,e.resetMaxMemoryUsage(1024*n.maxMemoryUsageInMB*1024),i.parse(o);var s=n.formatAsRGBA?4:3,a=i.width*i.height*s;try{e.requestMemoryAllocation(a);var l={width:i.width,height:i.height,exifBuffer:i.exifBuffer,data:n.useTArray?new Uint8Array(a):Buffer.alloc(a)};i.comments.length>0&&(l.comments=i.comments)}catch(t){if(t instanceof RangeError)throw new Error("Could not allocate enough memory for the image. Required: "+a);if(t instanceof ReferenceError&&"Buffer is not defined"===t.message)throw new Error("Buffer is not globally defined in this environment. Consider setting useTArray to true");throw t}return i.copyToImageData(l,n.formatAsRGBA),l}}(decoder);var decoderExports=decoder.exports,encode$1=encoderExports,decode$2=decoderExports,jpegJs={encode:encode$1,decode:decode$2};function encodeJpeg(t,e={}){const{quality:r=50}=e;("RGBA"!==t.colorModel||t instanceof Mask)&&(t=t.convertColor("RGBA")),8!==t.bitDepth&&(t=t.convertBitDepth(8));const n=jpegJs.encode(t.getRawImage(),r).data;return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}var u8=Uint8Array,u16=Uint16Array,i32=Int32Array,fleb=new u8([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),fdeb=new u8([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),clim=new u8([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),freb=function(t,e){for(var r=new u16(31),n=0;n<31;++n)r[n]=e+=1<<t[n-1];var o=new i32(r[30]);for(n=1;n<30;++n)for(var i=r[n];i<r[n+1];++i)o[i]=i-r[n]<<5|n;return{b:r,r:o}},_a=freb(fleb,2),fl=_a.b,revfl=_a.r;fl[28]=258,revfl[258]=28;for(var _b=freb(fdeb,0),fd=_b.b,revfd=_b.r,rev=new u16(32768),i=0;i<32768;++i){var x=(43690&i)>>1|(21845&i)<<1;x=(52428&x)>>2|(13107&x)<<2,x=(61680&x)>>4|(3855&x)<<4,rev[i]=((65280&x)>>8|(255&x)<<8)>>1}for(var hMap=function(t,e,r){for(var n=t.length,o=0,i=new u16(e);o<n;++o)t[o]&&++i[t[o]-1];var s,a=new u16(e);for(o=1;o<e;++o)a[o]=a[o-1]+i[o-1]<<1;if(r){s=new u16(1<<e);var l=15-e;for(o=0;o<n;++o)if(t[o])for(var h=o<<4|t[o],c=e-t[o],u=a[t[o]-1]++<<c,f=u|(1<<c)-1;u<=f;++u)s[rev[u]>>l]=h}else for(s=new u16(n),o=0;o<n;++o)t[o]&&(s[o]=rev[a[t[o]-1]++]>>15-t[o]);return s},flt=new u8(288),i=0;i<144;++i)flt[i]=8;for(var i=144;i<256;++i)flt[i]=9;for(var i=256;i<280;++i)flt[i]=7;for(var i=280;i<288;++i)flt[i]=8;for(var fdt=new u8(32),i=0;i<32;++i)fdt[i]=5;var flm=hMap(flt,9,0),flrm=hMap(flt,9,1),fdm=hMap(fdt,5,0),fdrm=hMap(fdt,5,1),max=function(t){for(var e=t[0],r=1;r<t.length;++r)t[r]>e&&(e=t[r]);return e},bits=function(t,e,r){var n=e/8|0;return(t[n]|t[n+1]<<8)>>(7&e)&r},bits16=function(t,e){var r=e/8|0;return(t[r]|t[r+1]<<8|t[r+2]<<16)>>(7&e)},shft=function(t){return(t+7)/8|0},slc=function(t,e,r){return(null==e||e<0)&&(e=0),(null==r||r>t.length)&&(r=t.length),new u8(t.subarray(e,r))},ec=["unexpected EOF","invalid block type","invalid length/literal","invalid distance","stream finished","no stream handler",,"no callback","invalid UTF-8 data","extra field too long","date not in range 1980-2099","filename too long","stream finishing","invalid zip data"],err=function(t,e,r){var n=new Error(e||ec[t]);if(n.code=t,Error.captureStackTrace&&Error.captureStackTrace(n,err),!r)throw n;return n},inflt=function(t,e,r,n){var o=t.length;if(!o||e.f&&!e.l)return r||new u8(0);var i=!r,s=i||2!=e.i,a=e.i;i&&(r=new u8(3*o));var l=function(t){var e=r.length;if(t>e){var n=new u8(Math.max(2*e,t));n.set(r),r=n}},h=e.f||0,c=e.p||0,u=e.b||0,f=e.l,d=e.d,m=e.m,g=e.n,p=8*o;do{if(!f){h=bits(t,c,1);var w=bits(t,c+1,3);if(c+=3,!w){var y=t[(P=shft(c)+4)-4]|t[P-3]<<8,b=P+y;if(b>o){a&&err(0);break}s&&l(u+y),r.set(t.subarray(P,b),u),e.b=u+=y,e.p=c=8*b,e.f=h;continue}if(1==w)f=flrm,d=fdrm,m=9,g=5;else if(2==w){var x=bits(t,c,31)+257,M=bits(t,c+10,15)+4,v=x+bits(t,c+5,31)+1;c+=14;for(var I=new u8(v),A=new u8(19),k=0;k<M;++k)A[clim[k]]=bits(t,c+3*k,7);c+=3*M;var R=max(A),E=(1<<R)-1,$=hMap(A,R,1);for(k=0;k<v;){var P,S=$[bits(t,c,E)];if(c+=15&S,(P=S>>4)<16)I[k++]=P;else{var C=0,B=0;for(16==P?(B=3+bits(t,c,3),c+=2,C=I[k-1]):17==P?(B=3+bits(t,c,7),c+=3):18==P&&(B=11+bits(t,c,127),c+=7);B--;)I[k++]=C}}var T=I.subarray(0,x),D=I.subarray(x);m=max(T),g=max(D),f=hMap(T,m,1),d=hMap(D,g,1)}else err(1);if(c>p){a&&err(0);break}}s&&l(u+131072);for(var _=(1<<m)-1,V=(1<<g)-1,N=c;;N=c){var O=(C=f[bits16(t,c)&_])>>4;if((c+=15&C)>p){a&&err(0);break}if(C||err(2),O<256)r[u++]=O;else{if(256==O){N=c,f=null;break}var F=O-254;if(O>264){var L=fleb[k=O-257];F=bits(t,c,(1<<L)-1)+fl[k],c+=L}var U=d[bits16(t,c)&V],z=U>>4;U||err(3),c+=15&U;D=fd[z];if(z>3){L=fdeb[z];D+=bits16(t,c)&(1<<L)-1,c+=L}if(c>p){a&&err(0);break}s&&l(u+131072);var G=u+F;if(u<D){var q=0-D,Y=Math.min(D,G);for(q+u<0&&err(3);u<Y;++u)r[u]=n[q+u]}for(;u<G;++u)r[u]=r[u-D]}}e.l=f,e.p=N,e.b=u,e.f=h,f&&(h=1,e.m=m,e.d=d,e.n=g)}while(!h);return u!=r.length&&i?slc(r,0,u):r.subarray(0,u)},wbits=function(t,e,r){r<<=7&e;var n=e/8|0;t[n]|=r,t[n+1]|=r>>8},wbits16=function(t,e,r){r<<=7&e;var n=e/8|0;t[n]|=r,t[n+1]|=r>>8,t[n+2]|=r>>16},hTree=function(t,e){for(var r=[],n=0;n<t.length;++n)t[n]&&r.push({s:n,f:t[n]});var o=r.length,i=r.slice();if(!o)return{t:et,l:0};if(1==o){var s=new u8(r[0].s+1);return s[r[0].s]=1,{t:s,l:1}}r.sort(function(t,e){return t.f-e.f}),r.push({s:-1,f:25001});var a=r[0],l=r[1],h=0,c=1,u=2;for(r[0]={s:-1,f:a.f+l.f,l:a,r:l};c!=o-1;)a=r[r[h].f<r[u].f?h++:u++],l=r[h!=c&&r[h].f<r[u].f?h++:u++],r[c++]={s:-1,f:a.f+l.f,l:a,r:l};var f=i[0].s;for(n=1;n<o;++n)i[n].s>f&&(f=i[n].s);var d=new u16(f+1),m=ln(r[c-1],d,0);if(m>e){n=0;var g=0,p=m-e,w=1<<p;for(i.sort(function(t,e){return d[e.s]-d[t.s]||t.f-e.f});n<o;++n){var y=i[n].s;if(!(d[y]>e))break;g+=w-(1<<m-d[y]),d[y]=e}for(g>>=p;g>0;){var b=i[n].s;d[b]<e?g-=1<<e-d[b]++-1:++n}for(;n>=0&&g;--n){var x=i[n].s;d[x]==e&&(--d[x],++g)}m=e}return{t:new u8(d),l:m}},ln=function(t,e,r){return-1==t.s?Math.max(ln(t.l,e,r+1),ln(t.r,e,r+1)):e[t.s]=r},lc=function(t){for(var e=t.length;e&&!t[--e];);for(var r=new u16(++e),n=0,o=t[0],i=1,s=function(t){r[n++]=t},a=1;a<=e;++a)if(t[a]==o&&a!=e)++i;else{if(!o&&i>2){for(;i>138;i-=138)s(32754);i>2&&(s(i>10?i-11<<5|28690:i-3<<5|12305),i=0)}else if(i>3){for(s(o),--i;i>6;i-=6)s(8304);i>2&&(s(i-3<<5|8208),i=0)}for(;i--;)s(o);i=1,o=t[a]}return{c:r.subarray(0,n),n:e}},clen=function(t,e){for(var r=0,n=0;n<e.length;++n)r+=t[n]*e[n];return r},wfblk=function(t,e,r){var n=r.length,o=shft(e+2);t[o]=255&n,t[o+1]=n>>8,t[o+2]=255^t[o],t[o+3]=255^t[o+1];for(var i=0;i<n;++i)t[o+i+4]=r[i];return 8*(o+4+n)},wblk=function(t,e,r,n,o,i,s,a,l,h,c){wbits(e,c++,r),++o[256];for(var u=hTree(o,15),f=u.t,d=u.l,m=hTree(i,15),g=m.t,p=m.l,w=lc(f),y=w.c,b=w.n,x=lc(g),M=x.c,v=x.n,I=new u16(19),A=0;A<y.length;++A)++I[31&y[A]];for(A=0;A<M.length;++A)++I[31&M[A]];for(var k=hTree(I,7),R=k.t,E=k.l,$=19;$>4&&!R[clim[$-1]];--$);var P,S,C,B,T=h+5<<3,D=clen(o,flt)+clen(i,fdt)+s,_=clen(o,f)+clen(i,g)+s+14+3*$+clen(I,R)+2*I[16]+3*I[17]+7*I[18];if(l>=0&&T<=D&&T<=_)return wfblk(e,c,t.subarray(l,l+h));if(wbits(e,c,1+(_<D)),c+=2,_<D){P=hMap(f,d,0),S=f,C=hMap(g,p,0),B=g;var V=hMap(R,E,0);wbits(e,c,b-257),wbits(e,c+5,v-1),wbits(e,c+10,$-4),c+=14;for(A=0;A<$;++A)wbits(e,c+3*A,R[clim[A]]);c+=3*$;for(var N=[y,M],O=0;O<2;++O){var F=N[O];for(A=0;A<F.length;++A){var L=31&F[A];wbits(e,c,V[L]),c+=R[L],L>15&&(wbits(e,c,F[A]>>5&127),c+=F[A]>>12)}}}else P=flm,S=flt,C=fdm,B=fdt;for(A=0;A<a;++A){var U=n[A];if(U>255){wbits16(e,c,P[(L=U>>18&31)+257]),c+=S[L+257],L>7&&(wbits(e,c,U>>23&31),c+=fleb[L]);var z=31&U;wbits16(e,c,C[z]),c+=B[z],z>3&&(wbits16(e,c,U>>5&8191),c+=fdeb[z])}else wbits16(e,c,P[U]),c+=S[U]}return wbits16(e,c,P[256]),c+S[256]},deo=new i32([65540,131080,131088,131104,262176,1048704,1048832,2114560,2117632]),et=new u8(0),dflt=function(t,e,r,n,o,i){var s=i.z||t.length,a=new u8(n+s+5*(1+Math.ceil(s/7e3))+o),l=a.subarray(n,a.length-o),h=i.l,c=7&(i.r||0);if(e){c&&(l[0]=i.r>>3);for(var u=deo[e-1],f=u>>13,d=8191&u,m=(1<<r)-1,g=i.p||new u16(32768),p=i.h||new u16(m+1),w=Math.ceil(r/3),y=2*w,b=function(e){return(t[e]^t[e+1]<<w^t[e+2]<<y)&m},x=new i32(25e3),M=new u16(288),v=new u16(32),I=0,A=0,k=i.i||0,R=0,E=i.w||0,$=0;k+2<s;++k){var P=b(k),S=32767&k,C=p[P];if(g[S]=C,p[P]=S,E<=k){var B=s-k;if((I>7e3||R>24576)&&(B>423||!h)){c=wblk(t,l,0,x,M,v,A,R,$,k-$,c),R=I=A=0,$=k;for(var T=0;T<286;++T)M[T]=0;for(T=0;T<30;++T)v[T]=0}var D=2,_=0,V=d,N=S-C&32767;if(B>2&&P==b(k-N))for(var O=Math.min(f,B)-1,F=Math.min(32767,k),L=Math.min(258,B);N<=F&&--V&&S!=C;){if(t[k+D]==t[k+D-N]){for(var U=0;U<L&&t[k+U]==t[k+U-N];++U);if(U>D){if(D=U,_=N,U>O)break;var z=Math.min(N,U-2),G=0;for(T=0;T<z;++T){var q=k-N+T&32767,Y=q-g[q]&32767;Y>G&&(G=Y,C=q)}}}N+=(S=C)-(C=g[S])&32767}if(_){x[R++]=268435456|revfl[D]<<18|revfd[_];var H=31&revfl[D],j=31&revfd[_];A+=fleb[H]+fdeb[j],++M[257+H],++v[j],E=k+D,++I}else x[R++]=t[k],++M[t[k]]}}for(k=Math.max(k,E);k<s;++k)x[R++]=t[k],++M[t[k]];c=wblk(t,l,h,x,M,v,A,R,$,k-$,c),h||(i.r=7&c|l[c/8|0]<<3,c-=7,i.h=p,i.p=g,i.i=k,i.w=E)}else{for(k=i.w||0;k<s+h;k+=65535){var W=k+65535;W>=s&&(l[c/8|0]=h,W=s),c=wfblk(l,c+1,t.subarray(k,W))}i.i=s}return slc(a,0,n+shft(c)+o)},adler=function(){var t=1,e=0;return{p:function(r){for(var n=t,o=e,i=0|r.length,s=0;s!=i;){for(var a=Math.min(s+2655,i);s<a;++s)o+=n+=r[s];n=(65535&n)+15*(n>>16),o=(65535&o)+15*(o>>16)}t=n,e=o},d:function(){return(255&(t%=65521))<<24|(65280&t)<<8|(255&(e%=65521))<<8|e>>8}}},dopt=function(t,e,r,n,o){if(!o&&(o={l:1},e.dictionary)){var i=e.dictionary.subarray(-32768),s=new u8(i.length+t.length);s.set(i),s.set(t,i.length),t=s,o.w=i.length}return dflt(t,null==e.level?6:e.level,null==e.mem?o.l?Math.ceil(1.5*Math.max(8,Math.min(13,Math.log(t.length)))):20:12+e.mem,r,n,o)},wbytes=function(t,e,r){for(;r;++e)t[e]=r,r>>>=8},gzs=function(t){31==t[0]&&139==t[1]&&8==t[2]||err(6,"invalid gzip data");var e=t[3],r=10;4&e&&(r+=2+(t[10]|t[11]<<8));for(var n=(e>>3&1)+(e>>4&1);n>0;n-=!t[r++]);return r+(2&e)},gzl=function(t){var e=t.length;return(t[e-4]|t[e-3]<<8|t[e-2]<<16|t[e-1]<<24)>>>0},zlh=function(t,e){var r=e.level,n=0==r?0:r<6?1:9==r?3:2;if(t[0]=120,t[1]=n<<6|(e.dictionary&&32),t[1]|=31-(t[0]<<8|t[1])%31,e.dictionary){var o=adler();o.p(e.dictionary),wbytes(t,2,o.d())}},zls=function(t,e){return(8!=(15&t[0])||t[0]>>4>7||(t[0]<<8|t[1])%31)&&err(6,"invalid zlib data"),(t[1]>>5&1)==+!e&&err(6,"invalid zlib data: "+(32&t[1]?"need":"unexpected")+" dictionary"),2+(t[1]>>3&4)},Inflate=function(){function t(t,e){"function"==typeof t&&(e=t,t={}),this.ondata=e;var r=t&&t.dictionary&&t.dictionary.subarray(-32768);this.s={i:0,b:r?r.length:0},this.o=new u8(32768),this.p=new u8(0),r&&this.o.set(r)}return t.prototype.e=function(t){if(this.ondata||err(5),this.d&&err(4),this.p.length){if(t.length){var e=new u8(this.p.length+t.length);e.set(this.p),e.set(t,this.p.length),this.p=e}}else this.p=t},t.prototype.c=function(t){this.s.i=+(this.d=t||!1);var e=this.s.b,r=inflt(this.p,this.s,this.o);this.ondata(slc(r,e,this.s.b),this.d),this.o=slc(r,this.s.b-32768),this.s.b=this.o.length,this.p=slc(this.p,this.s.p/8|0),this.s.p&=7},t.prototype.push=function(t,e){this.e(t),this.c(e)},t}();function inflateSync(t,e){return inflt(t,{i:2},e,e)}function gunzipSync(t,e){var r=gzs(t);return r+8>t.length&&err(6,"invalid gzip data"),inflt(t.subarray(r,-8),{i:2},new u8(gzl(t)),e)}function zlibSync(t,e){e||(e={});var r=adler();r.p(t);var n=dopt(t,e,e.dictionary?6:2,4);return zlh(n,e),wbytes(n,n.length-4,r.d()),n}var Unzlib=function(){function t(t,e){Inflate.call(this,t,e),this.v=t&&t.dictionary?2:1}return t.prototype.push=function(t,e){if(Inflate.prototype.e.call(this,t),this.v){if(this.p.length<6&&!e)return;this.p=this.p.subarray(zls(this.p,this.v-1)),this.v=0}e&&(this.p.length<4&&err(6,"invalid zlib data"),this.p=this.p.subarray(0,-4)),Inflate.prototype.c.call(this,e)},t}();function unzlibSync(t,e){return inflt(t.subarray(zls(t,e),-4),{i:2},e,e)}function decompressSync(t,e){return 31==t[0]&&139==t[1]&&8==t[2]?gunzipSync(t,e):8!=(15&t[0])||t[0]>>4>7||(t[0]<<8|t[1])%31?inflateSync(t,e):unzlibSync(t,e)}var td="undefined"!=typeof TextDecoder&&new TextDecoder,tds=0;try{td.decode(et,{stream:!0}),tds=1}catch(e){}const crcTable=[];for(let t=0;t<256;t++){let e=t;for(let t=0;t<8;t++)1&e?e=3988292384^e>>>1:e>>>=1;crcTable[t]=e}const initialCrc=4294967295;function updateCrc(t,e,r){let n=t;for(let t=0;t<r;t++)n=crcTable[255&(n^e[t])]^n>>>8;return n}function crc(t,e){return(updateCrc(initialCrc,t,e)^initialCrc)>>>0}function checkCrc(t,e,r){const n=t.readUint32(),o=crc(new Uint8Array(t.buffer,t.byteOffset+t.offset-e-4,e),e);if(o!==n)throw new Error(`CRC mismatch for chunk ${r}. Expected ${n}, found ${o}`)}function writeCrc(t,e){t.writeUint32(crc(new Uint8Array(t.buffer,t.byteOffset+t.offset-e,e),e))}function unfilterNone(t,e,r){for(let n=0;n<r;n++)e[n]=t[n]}function unfilterSub(t,e,r,n){let o=0;for(;o<n;o++)e[o]=t[o];for(;o<r;o++)e[o]=t[o]+e[o-n]&255}function unfilterUp(t,e,r,n){let o=0;if(0===r.length)for(;o<n;o++)e[o]=t[o];else for(;o<n;o++)e[o]=t[o]+r[o]&255}function unfilterAverage(t,e,r,n,o){let i=0;if(0===r.length){for(;i<o;i++)e[i]=t[i];for(;i<n;i++)e[i]=t[i]+(e[i-o]>>1)&255}else{for(;i<o;i++)e[i]=t[i]+(r[i]>>1)&255;for(;i<n;i++)e[i]=t[i]+(e[i-o]+r[i]>>1)&255}}function unfilterPaeth(t,e,r,n,o){let i=0;if(0===r.length){for(;i<o;i++)e[i]=t[i];for(;i<n;i++)e[i]=t[i]+e[i-o]&255}else{for(;i<o;i++)e[i]=t[i]+r[i]&255;for(;i<n;i++)e[i]=t[i]+paethPredictor(e[i-o],r[i],r[i-o])&255}}function paethPredictor(t,e,r){const n=t+e-r,o=Math.abs(n-t),i=Math.abs(n-e),s=Math.abs(n-r);return o<=i&&o<=s?t:i<=s?e:r}function applyUnfilter(t,e,r,n,o,i){switch(t){case 0:unfilterNone(e,r,o);break;case 1:unfilterSub(e,r,o,i);break;case 2:unfilterUp(e,r,n,o);break;case 3:unfilterAverage(e,r,n,o,i);break;case 4:unfilterPaeth(e,r,n,o,i);break;default:throw new Error(`Unsupported filter: ${t}`)}}const uint16$1=new Uint16Array([255]),uint8$1=new Uint8Array(uint16$1.buffer),osIsLittleEndian$1=255===uint8$1[0];function decodeInterlaceAdam7(t){const{data:e,width:r,height:n,channels:o,depth:i}=t,s=[{x:0,y:0,xStep:8,yStep:8},{x:4,y:0,xStep:8,yStep:8},{x:0,y:4,xStep:4,yStep:8},{x:2,y:0,xStep:4,yStep:4},{x:0,y:2,xStep:2,yStep:4},{x:1,y:0,xStep:2,yStep:2},{x:0,y:1,xStep:1,yStep:2}],a=Math.ceil(i/8)*o,l=new Uint8Array(n*r*a);let h=0;for(let t=0;t<7;t++){const o=s[t],i=Math.ceil((r-o.x)/o.xStep),c=Math.ceil((n-o.y)/o.yStep);if(i<=0||c<=0)continue;const u=i*a,f=new Uint8Array(u);for(let t=0;t<c;t++){const s=e[h++],c=e.subarray(h,h+u);h+=u;const d=new Uint8Array(u);applyUnfilter(s,c,d,f,u,a),f.set(d);for(let e=0;e<i;e++){const i=o.x+e*o.xStep,s=o.y+t*o.yStep;if(!(i>=r||s>=n))for(let t=0;t<a;t++)l[(s*r+i)*a+t]=d[e*a+t]}}}if(16===i){const t=new Uint16Array(l.buffer);if(osIsLittleEndian$1)for(let e=0;e<t.length;e++)t[e]=swap16$1(t[e]);return t}return l}function swap16$1(t){return(255&t)<<8|t>>8&255}const uint16=new Uint16Array([255]),uint8=new Uint8Array(uint16.buffer),osIsLittleEndian=255===uint8[0],empty=new Uint8Array(0);function decodeInterlaceNull(t){const{data:e,width:r,height:n,channels:o,depth:i}=t,s=Math.ceil(i/8)*o,a=Math.ceil(i/8*o*r),l=new Uint8Array(n*a);let h,c,u=empty,f=0;for(let t=0;t<n;t++){switch(h=e.subarray(f+1,f+1+a),c=l.subarray(t*a,(t+1)*a),e[f]){case 0:unfilterNone(h,c,a);break;case 1:unfilterSub(h,c,a,s);break;case 2:unfilterUp(h,c,u,a);break;case 3:unfilterAverage(h,c,u,a,s);break;case 4:unfilterPaeth(h,c,u,a,s);break;default:throw new Error(`Unsupported filter: ${e[f]}`)}u=c,f+=a+1}if(16===i){const t=new Uint16Array(l.buffer);if(osIsLittleEndian)for(let e=0;e<t.length;e++)t[e]=swap16(t[e]);return t}return l}function swap16(t){return(255&t)<<8|t>>8&255}const pngSignature=Uint8Array.of(137,80,78,71,13,10,26,10);function writeSignature(t){t.writeBytes(pngSignature)}function checkSignature(t){if(!hasPngSignature(t.readBytes(pngSignature.length)))throw new Error("wrong PNG signature")}function hasPngSignature(t){if(t.length<pngSignature.length)return!1;for(let e=0;e<pngSignature.length;e++)if(t[e]!==pngSignature[e])return!1;return!0}const textChunkName="tEXt",NULL=0,latin1Decoder=new TextDecoder("latin1");function validateKeyword(t){if(validateLatin1(t),0===t.length||t.length>79)throw new Error("keyword length must be between 1 and 79")}const latin1Regex=/^[\u0000-\u00FF]*$/;function validateLatin1(t){if(!latin1Regex.test(t))throw new Error("invalid latin1 text")}function decodetEXt(t,e,r){const n=readKeyword(e);t[n]=readLatin1(e,r-n.length-1)}function encodetEXt(t,e,r){validateKeyword(e),validateLatin1(r);const n=e.length+1+r.length;t.writeUint32(n),t.writeChars(textChunkName),t.writeChars(e),t.writeByte(NULL),t.writeChars(r),writeCrc(t,n+4)}function readKeyword(t){for(t.mark();t.readByte()!==NULL;);const e=t.offset;t.reset();const r=latin1Decoder.decode(t.readBytes(e-t.offset-1));return t.skip(1),validateKeyword(r),r}function readLatin1(t,e){return latin1Decoder.decode(t.readBytes(e))}const ColorType={UNKNOWN:-1,GREYSCALE:0,TRUECOLOUR:2,INDEXED_COLOUR:3,GREYSCALE_ALPHA:4,TRUECOLOUR_ALPHA:6},CompressionMethod={UNKNOWN:-1,DEFLATE:0},FilterMethod={UNKNOWN:-1,ADAPTIVE:0},InterlaceMethod={UNKNOWN:-1,NO_INTERLACE:0,ADAM7:1},DisposeOpType={NONE:0,BACKGROUND:1,PREVIOUS:2},BlendOpType={SOURCE:0,OVER:1};class PngDecoder extends IOBuffer{_checkCrc;_inflator;_png;_apng;_end;_hasPalette;_palette;_hasTransparency;_transparency;_compressionMethod;_filterMethod;_interlaceMethod;_colorType;_isAnimated;_numberOfFrames;_numberOfPlays;_frames;_writingDataChunks;_chunks;_inflatorResult;constructor(t,e={}){super(t);const{checkCrc:r=!1}=e;this._checkCrc=r,this._inflator=new Unzlib((t,e)=>{if(this._chunks.push(t),e){const t=this._chunks.reduce((t,e)=>t+e.length,0);this._inflatorResult=new Uint8Array(t);let e=0;for(const t of this._chunks)this._inflatorResult.set(t,e),e+=t.length;this._chunks=[]}}),this._chunks=[],this._png={width:-1,height:-1,channels:-1,data:new Uint8Array(0),depth:1,text:{}},this._apng={width:-1,height:-1,channels:-1,depth:1,numberOfFrames:1,numberOfPlays:0,text:{},frames:[]},this._end=!1,this._hasPalette=!1,this._palette=[],this._hasTransparency=!1,this._transparency=new Uint16Array(0),this._compressionMethod=CompressionMethod.UNKNOWN,this._filterMethod=FilterMethod.UNKNOWN,this._interlaceMethod=InterlaceMethod.UNKNOWN,this._colorType=ColorType.UNKNOWN,this._isAnimated=!1,this._numberOfFrames=1,this._numberOfPlays=0,this._frames=[],this._writingDataChunks=!1,this._inflatorResult=new Uint8Array(0),this.setBigEndian()}decode(){for(checkSignature(this);!this._end;){const t=this.readUint32(),e=this.readChars(4);this.decodeChunk(t,e)}return this._inflator.push(new Uint8Array(0),!0),this.decodeImage(),this._png}decodeApng(){for(checkSignature(this);!this._end;){const t=this.readUint32(),e=this.readChars(4);this.decodeApngChunk(t,e)}return this.decodeApngImage(),this._apng}decodeChunk(t,e){const r=this.offset;switch(e){case"IHDR":this.decodeIHDR();break;case"PLTE":this.decodePLTE(t);break;case"IDAT":this.decodeIDAT(t);break;case"IEND":this._end=!0;break;case"tRNS":this.decodetRNS(t);break;case"iCCP":this.decodeiCCP(t);break;case textChunkName:decodetEXt(this._png.text,this,t);break;case"pHYs":this.decodepHYs();break;default:this.skip(t)}if(this.offset-r!==t)throw new Error(`Length mismatch while decoding chunk ${e}`);this._checkCrc?checkCrc(this,t+4,e):this.skip(4)}decodeApngChunk(t,e){const r=this.offset;switch("fdAT"!==e&&"IDAT"!==e&&this._writingDataChunks&&this.pushDataToFrame(),e){case"acTL":this.decodeACTL();break;case"fcTL":this.decodeFCTL();break;case"fdAT":this.decodeFDAT(t);break;default:this.decodeChunk(t,e),this.offset=r+t}if(this.offset-r!==t)throw new Error(`Length mismatch while decoding chunk ${e}`);this._checkCrc?checkCrc(this,t+4,e):this.skip(4)}decodeIHDR(){const t=this._png;t.width=this.readUint32(),t.height=this.readUint32(),t.depth=checkBitDepth(this.readUint8());const e=this.readUint8();let r;switch(this._colorType=e,e){case ColorType.GREYSCALE:r=1;break;case ColorType.TRUECOLOUR:r=3;break;case ColorType.INDEXED_COLOUR:r=1;break;case ColorType.GREYSCALE_ALPHA:r=2;break;case ColorType.TRUECOLOUR_ALPHA:r=4;break;case ColorType.UNKNOWN:default:throw new Error(`Unknown color type: ${e}`)}if(this._png.channels=r,this._compressionMethod=this.readUint8(),this._compressionMethod!==CompressionMethod.DEFLATE)throw new Error(`Unsupported compression method: ${this._compressionMethod}`);this._filterMethod=this.readUint8(),this._interlaceMethod=this.readUint8()}decodeACTL(){this._numberOfFrames=this.readUint32(),this._numberOfPlays=this.readUint32(),this._isAnimated=!0}decodeFCTL(){const t={sequenceNumber:this.readUint32(),width:this.readUint32(),height:this.readUint32(),xOffset:this.readUint32(),yOffset:this.readUint32(),delayNumber:this.readUint16(),delayDenominator:this.readUint16(),disposeOp:this.readUint8(),blendOp:this.readUint8(),data:new Uint8Array(0)};this._frames.push(t)}decodePLTE(t){if(t%3!=0)throw new RangeError(`PLTE field length must be a multiple of 3. Got ${t}`);const e=t/3;this._hasPalette=!0;const r=[];this._palette=r;for(let t=0;t<e;t++)r.push([this.readUint8(),this.readUint8(),this.readUint8()])}decodeIDAT(t){this._writingDataChunks=!0;const e=t,r=this.offset+this.byteOffset;try{this._inflator.push(new Uint8Array(this.buffer,r,e),!1)}catch(t){throw new Error("Error while decompressing the data:",{cause:t})}this.skip(t)}decodeFDAT(t){this._writingDataChunks=!0;let e=t,r=this.offset+this.byteOffset;r+=4,e-=4;try{this._inflator.push(new Uint8Array(this.buffer,r,e),!1)}catch(t){throw new Error("Error while decompressing the data:",{cause:t})}this.skip(t)}decodetRNS(t){switch(this._colorType){case ColorType.GREYSCALE:case ColorType.TRUECOLOUR:if(t%2!=0)throw new RangeError(`tRNS chunk length must be a multiple of 2. Got ${t}`);if(t/2>this._png.width*this._png.height)throw new Error(`tRNS chunk contains more alpha values than there are pixels (${t/2} vs ${this._png.width*this._png.height})`);this._hasTransparency=!0,this._transparency=new Uint16Array(t/2);for(let e=0;e<t/2;e++)this._transparency[e]=this.readUint16();break;case ColorType.INDEXED_COLOUR:{if(t>this._palette.length)throw new Error(`tRNS chunk contains more alpha values than there are palette colors (${t} vs ${this._palette.length})`);let e=0;for(;e<t;e++){const t=this.readByte();this._palette[e].push(t)}for(;e<this._palette.length;e++)this._palette[e].push(255);break}case ColorType.UNKNOWN:case ColorType.GREYSCALE_ALPHA:case ColorType.TRUECOLOUR_ALPHA:default:throw new Error(`tRNS chunk is not supported for color type ${this._colorType}`)}}decodeiCCP(t){const e=readKeyword(this),r=this.readUint8();if(r!==CompressionMethod.DEFLATE)throw new Error(`Unsupported iCCP compression method: ${r}`);const n=this.readBytes(t-e.length-2);this._png.iccEmbeddedProfile={name:e,profile:unzlibSync(n)}}decodepHYs(){const t=this.readUint32(),e=this.readUint32(),r=this.readByte();this._png.resolution={x:t,y:e,unit:r}}decodeApngImage(){this._apng.width=this._png.width,this._apng.height=this._png.height,this._apng.channels=this._png.channels,this._apng.depth=this._png.depth,this._apng.numberOfFrames=this._numberOfFrames,this._apng.numberOfPlays=this._numberOfPlays,this._apng.text=this._png.text,this._apng.resolution=this._png.resolution;for(let t=0;t<this._numberOfFrames;t++){const e={sequenceNumber:this._frames[t].sequenceNumber,delayNumber:this._frames[t].delayNumber,delayDenominator:this._frames[t].delayDenominator,data:8===this._apng.depth?new Uint8Array(this._apng.width*this._apng.height*this._apng.channels):new Uint16Array(this._apng.width*this._apng.height*this._apng.channels)},r=this._frames.at(t);if(r){if(r.data=decodeInterlaceNull({data:r.data,width:r.width,height:r.height,channels:this._apng.channels,depth:this._apng.depth}),this._hasPalette&&(this._apng.palette=this._palette),this._hasTransparency&&(this._apng.transparency=this._transparency),0===t||0===r.xOffset&&0===r.yOffset&&r.width===this._png.width&&r.height===this._png.height)e.data=r.data;else{const n=this._apng.frames.at(t-1);this.disposeFrame(r,n,e),this.addFrameDataToCanvas(e,r)}this._apng.frames.push(e)}}return this._apng}disposeFrame(t,e,r){switch(t.disposeOp){case DisposeOpType.NONE:break;case DisposeOpType.BACKGROUND:for(let e=0;e<this._png.height;e++)for(let n=0;n<this._png.width;n++){const o=(e*t.width+n)*this._png.channels;for(let t=0;t<this._png.channels;t++)r.data[o+t]=0}break;case DisposeOpType.PREVIOUS:r.data.set(e.data);break;default:throw new Error("Unknown disposeOp")}}addFrameDataToCanvas(t,e){const r=1<<this._png.depth,n=(t,r)=>({index:((t+e.yOffset)*this._png.width+e.xOffset+r)*this._png.channels,frameIndex:(t*e.width+r)*this._png.channels});switch(e.blendOp){case BlendOpType.SOURCE:for(let r=0;r<e.height;r++)for(let o=0;o<e.width;o++){const{index:i,frameIndex:s}=n(r,o);for(let r=0;r<this._png.channels;r++)t.data[i+r]=e.data[s+r]}break;case BlendOpType.OVER:for(let o=0;o<e.height;o++)for(let i=0;i<e.width;i++){const{index:s,frameIndex:a}=n(o,i);for(let n=0;n<this._png.channels;n++){const o=e.data[a+this._png.channels-1]/r,i=n%(this._png.channels-1)==0?1:e.data[a+n],l=Math.floor(o*i+(1-o)*t.data[s+n]);t.data[s+n]+=l}}break;default:throw new Error("Unknown blendOp")}}decodeImage(){const t=this._inflatorResult;if(this._filterMethod!==FilterMethod.ADAPTIVE)throw new Error(`Filter method ${this._filterMethod} not supported`);if(this._interlaceMethod===InterlaceMethod.NO_INTERLACE)this._png.data=decodeInterlaceNull({data:t,width:this._png.width,height:this._png.height,channels:this._png.channels,depth:this._png.depth});else{if(this._interlaceMethod!==InterlaceMethod.ADAM7)throw new Error(`Interlace method ${this._interlaceMethod} not supported`);this._png.data=decodeInterlaceAdam7({data:t,width:this._png.width,height:this._png.height,channels:this._png.channels,depth:this._png.depth})}this._hasPalette&&(this._png.palette=this._palette),this._hasTransparency&&(this._png.transparency=this._transparency)}pushDataToFrame(){this._inflator.push(new Uint8Array(0),!0);const t=this._inflatorResult,e=this._frames.at(-1);e?e.data=t:this._frames.push({sequenceNumber:0,width:this._png.width,height:this._png.height,xOffset:0,yOffset:0,delayNumber:0,delayDenominator:0,disposeOp:DisposeOpType.NONE,blendOp:BlendOpType.SOURCE,data:t}),this._inflator=new Unzlib((t,e)=>{if(this._chunks.push(t),e){const t=this._chunks.reduce((t,e)=>t+e.length,0);this._inflatorResult=new Uint8Array(t);let e=0;for(const t of this._chunks)this._inflatorResult.set(t,e),e+=t.length;this._chunks=[]}}),this._chunks=[],this._writingDataChunks=!1}}function checkBitDepth(t){if(1!==t&&2!==t&&4!==t&&8!==t&&16!==t)throw new Error(`invalid bit depth: ${t}`);return t}const defaultZlibOptions={level:3};class PngEncoder extends IOBuffer{_png;_zlibOptions;_colorType;_interlaceMethod;constructor(t,e={}){super(),this._colorType=ColorType.UNKNOWN,this._zlibOptions={...defaultZlibOptions,...e.zlib},this._png=this._checkData(t),this._interlaceMethod=("Adam7"===e.interlace?InterlaceMethod.ADAM7:InterlaceMethod.NO_INTERLACE)??InterlaceMethod.NO_INTERLACE,this.setBigEndian()}encode(){if(writeSignature(this),this.encodeIHDR(),this._png.palette&&(this.encodePLTE(),4===this._png.palette[0].length&&this.encodeTRNS()),this.encodeData(),this._png.text)for(const[t,e]of Object.entries(this._png.text))encodetEXt(this,t,e);return this.encodeIEND(),this.toArray()}encodeIHDR(){this.writeUint32(13),this.writeChars("IHDR"),this.writeUint32(this._png.width),this.writeUint32(this._png.height),this.writeByte(this._png.depth),this.writeByte(this._colorType),this.writeByte(CompressionMethod.DEFLATE),this.writeByte(FilterMethod.ADAPTIVE),this.writeByte(this._interlaceMethod),writeCrc(this,17)}encodeIEND(){this.writeUint32(0),this.writeChars("IEND"),writeCrc(this,4)}encodePLTE(){const t=3*this._png.palette?.length;this.writeUint32(t),this.writeChars("PLTE");for(const t of this._png.palette)this.writeByte(t[0]),this.writeByte(t[1]),this.writeByte(t[2]);writeCrc(this,4+t)}encodeTRNS(){const t=this._png.palette.filter(t=>255!==t.at(-1));this.writeUint32(t.length),this.writeChars("tRNS");for(const e of t)this.writeByte(e.at(-1));writeCrc(this,4+t.length)}encodeIDAT(t){this.writeUint32(t.length),this.writeChars("IDAT"),this.writeBytes(t),writeCrc(this,t.length+4)}encodeData(){const{width:t,height:e,channels:r,depth:n,data:o}=this._png,i=n<=8?Math.ceil(t*n/8)*r:Math.ceil(t*n/8*r/2),s=(new IOBuffer).setBigEndian();let a=0;if(this._interlaceMethod===InterlaceMethod.NO_INTERLACE)for(let t=0;t<e;t++)s.writeByte(0),a=16===n?writeDataUint16(o,s,i,a):writeDataBytes(o,s,i,a);else this._interlaceMethod===InterlaceMethod.ADAM7&&(a=writeDataInterlaced(this._png,o,s,a));const l=zlibSync(s.toArray(),this._zlibOptions);this.encodeIDAT(l)}_checkData(t){const{colorType:e,channels:r,depth:n}=getColorType(t,t.palette),o={width:checkInteger(t.width,"width"),height:checkInteger(t.height,"height"),channels:r,data:t.data,depth:n,text:t.text,palette:t.palette};this._colorType=e;const i=n<8?Math.ceil(o.width*n/8)*o.height*r:o.width*o.height*r;if(o.data.length!==i)throw new RangeError(`wrong data size. Found ${o.data.length}, expected ${i}`);return o}}function checkInteger(t,e){if(Number.isInteger(t)&&t>0)return t;throw new TypeError(`${e} must be a positive integer`)}function getColorType(t,e){const{channels:r=4,depth:n=8}=t;if(4!==r&&3!==r&&2!==r&&1!==r)throw new RangeError(`unsupported number of channels: ${r}`);const o={channels:r,depth:n,colorType:ColorType.UNKNOWN};switch(r){case 4:o.colorType=ColorType.TRUECOLOUR_ALPHA;break;case 3:o.colorType=ColorType.TRUECOLOUR;break;case 1:o.colorType=e?ColorType.INDEXED_COLOUR:ColorType.GREYSCALE;break;case 2:o.colorType=ColorType.GREYSCALE_ALPHA;break;default:throw new Error("unsupported number of channels")}return o}function writeDataBytes(t,e,r,n){for(let o=0;o<r;o++)e.writeByte(t[n++]);return n}function writeDataInterlaced(t,e,r,n){const o=[{x:0,y:0,xStep:8,yStep:8},{x:4,y:0,xStep:8,yStep:8},{x:0,y:4,xStep:4,yStep:8},{x:2,y:0,xStep:4,yStep:4},{x:0,y:2,xStep:2,yStep:4},{x:1,y:0,xStep:2,yStep:2},{x:0,y:1,xStep:1,yStep:2}],{width:i,height:s,channels:a,depth:l}=t;let h;h=16===l?a*l/8/2:a*l/8;for(let t=0;t<7;t++){const n=o[t],a=Math.floor((i-n.x+n.xStep-1)/n.xStep),c=Math.floor((s-n.y+n.yStep-1)/n.yStep);if(a<=0||c<=0)continue;const u=a*h;for(let t=0;t<c;t++){const o=n.y+t*n.yStep,c=l<=8?new Uint8Array(u):new Uint16Array(u);let f=0;for(let t=0;t<a;t++){const r=n.x+t*n.xStep;if(r<i&&o<s){const t=(o*i+r)*h;for(let r=0;r<h;r++)c[f++]=e[t+r]}}if(r.writeByte(0),8===l)r.writeBytes(c);else if(16===l)for(const t of c)r.writeByte(t>>8&255),r.writeByte(255&t)}}return n}function writeDataUint16(t,e,r,n){for(let o=0;o<r;o++)e.writeUint16(t[n++]);return n}function decodePng$1(t,e){return new PngDecoder(t,e).decode()}function encodePng$1(t,e){return new PngEncoder(t,e).encode()}function decodeApng(t,e){return new PngDecoder(t,e).decodeApng()}function encodePng(t,e){("RGB"!==t.colorModel&&"RGBA"!==t.colorModel&&"GREY"!==t.colorModel&&"GREYA"!==t.colorModel||t instanceof Mask)&&(t=t.convertColor("GREY"));const{bitDepth:r,...n}=t.getRawImage();return encodePng$1({depth:r,...n},e)}const ImageFormat={PNG:"png",JPG:"jpg",JPEG:"jpeg",BMP:"bmp"},defaultPng={format:"png"};function encode(t,e=defaultPng){return M$2(e).with({format:"png"},e=>encodePng(t,e.encoderOptions)).with({format:z.union("jpg","jpeg")},e=>encodeJpeg(t,e.encoderOptions)).with({format:"bmp"},()=>encodeBmp(t)).exhaustive()}function getNodeApiOrThrow(t){if(!isNode())throw new Error(`${t} is only implemented for Node.js`);return{fs:process.getBuiltinModule("node:fs"),path:process.getBuiltinModule("node:path"),url:process.getBuiltinModule("node:url")}}let CanvasCtorBrowser,CanvasCtorNode,requireFn;function getCanvasContext(t,e){if(isNode()){if(!CanvasCtorNode)try{CanvasCtorNode=getRequireFn()("skia-canvas").Canvas}catch(t){throw new Error('drawText on Node.js requires the optional "skia-canvas" package. Install it with: npm install skia-canvas',{cause:t})}return new CanvasCtorNode(t,e).getContext("2d")}{CanvasCtorBrowser??=globalThis.OffscreenCanvas;const r=new CanvasCtorBrowser(t,e).getContext("2d");if(!r)throw new Error("Failed to create canvas context");return r}}function isNode(){return"undefined"!=typeof process&&"function"==typeof process.getBuiltinModule}function getRequireFn(){return requireFn??=process.getBuiltinModule("node:module").createRequire("undefined"==typeof document&&"undefined"==typeof location?require("url").pathToFileURL(__filename).href:"undefined"==typeof document?location.href:_documentCurrentScript&&"SCRIPT"===_documentCurrentScript.tagName.toUpperCase()&&_documentCurrentScript.src||new URL("image-js.umd.min.js",document.baseURI).href),requireFn}async function write(t,e,r){const n=getNodeApiOrThrow("write");"string"!=typeof t&&(t=n.url.fileURLToPath(t)),e instanceof Mask&&(e=e.convertColor("GREY"));const o=getDataToWrite(t,e,r,n);if(r?.recursive){const e=n.path.dirname(t);await n.fs.promises.mkdir(e,{recursive:!0})}await n.fs.promises.writeFile(t,o)}function writeSync(t,e,r){const n=getNodeApiOrThrow("writeSync");"string"!=typeof t&&(t=n.url.fileURLToPath(t));const o=getDataToWrite(t,e,r,n);if(r?.recursive){const e=n.path.dirname(t);n.fs.mkdirSync(e,{recursive:!0})}n.fs.writeFileSync(t,o)}function getDataToWrite(t,e,r,n){if(r&&"format"in r)return encode(e,r);{const o=n.path.extname(t).slice(1).toLowerCase();if("png"===o||"jpg"===o||"jpeg"===o||"bmp"===o)return encode(e,{...r,format:o});throw new RangeError("image format could not be determined from file extension. Use a supported extension or specify the format option")}}function writeCanvas(t,e,r={}){"RGBA"!==t.colorModel&&(t=t.convertColor("RGBA")),8!==t.bitDepth&&t instanceof Image&&(t=t.convertBitDepth(8));const{resizeCanvas:n=!0,dx:o=0,dy:i=0,dirtyX:s=0,dirtyY:a=0,dirtyWidth:l=t.width,dirtyHeight:h=t.height}=r;n&&(e.width=t.width,e.height=t.height);const c=e.getContext("2d");assert(c);const u=t.getRawImage().data;c.putImageData(new ImageData(new Uint8ClampedArray(u.buffer,u.byteOffset,u.byteLength),t.width,t.height),o,i,s,a,l,h)}Uint8Array.from([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]);const base64codes=Uint8Array.from([65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47]),base64codes1=new Uint32Array(4096);for(let t=0;t<64;t++)for(let e=0;e<64;e++){const r=t<<2|(48&e)>>4|(15&e)<<8;base64codes1[r]=base64codes[t]|base64codes[e]<<8}const base64codes2=new Uint32Array(4096);for(let t=0;t<64;t++)for(let e=0;e<64;e++){const r=t<<6|e;base64codes2[r]=base64codes[t]<<16|base64codes[e]<<24}function encodeFast(t){const e=new Uint32Array(Math.ceil(t.length/3));let r,n;for(r=2,n=0;r<t.length;r+=3,n++)e[n]=base64codes1[t[r-2]|(240&t[r-1])<<4]|base64codes2[t[r]|(15&t[r-1])<<8];r===t.length+1&&(e[n]=base64codes[t[r-2]>>2]|base64codes[(3&t[r-2])<<4]<<8|15677<<16),r===t.length&&(e[n]=base64codes[t[r-2]>>2]|base64codes[(3&t[r-2])<<4|t[r-1]>>4]<<8|base64codes[(15&t[r-1])<<2]<<16|61<<24);return new Uint8Array(e.buffer)}function encodeDataURL(t,e=defaultPng){const r=encodeFast(encode(t,e)),n=(new TextDecoder).decode(r);return`data:image/${e.format};base64,${n}`}function getMatrixFromPoints(t){const e=new Matrix(3,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r].column),e.set(1,r,t[r].row),e.set(2,r,1);return e}function affineFitFunction(t,e){const r=getAffineTransform$1(getMatrixFromPoints(t),getMatrixFromPoints(e));return[r.rotation,r.translation.x,r.translation.y,r.scale]}function createAffineTransformModel(t){if(4!==t.length)throw new Error("Transform had wrong number of parameters");return e=>{const r=t[0]*Math.PI/180,n=t[1],o=t[2],i=t[3];return{column:i*(Math.cos(r)*e.column-Math.sin(r)*e.row)+n,row:i*(Math.sin(r)*e.column+Math.cos(r)*e.row)+o}}}function getEuclideanDistance(t,e){return Math.hypot(t.row-e.row,t.column-e.column)}function getSourceWithoutMargins(t,e,r){const n=Math.min(e.width,t.width-r.column),o=Math.min(e.height,t.height-r.row);return t.crop({origin:{row:r.row,column:r.column},width:n,height:o})}function getAffineTransform(t,e,r={}){const{centroidPatchDiameter:n=31,bestKeypointRadius:o=5,enhanceContrast:i=!0,crosscheck:s=!0,destinationOrigin:a={column:0,row:0},maxRansacNbIterations:l,debug:h=!1,debugImagePath:c}=r;if(t.colorModel!==ImageColorModel.GREY&&(t=t.grey()),e.colorModel!==ImageColorModel.GREY&&(e=e.grey()),i){const r=getMinMax(getSourceWithoutMargins(t,e,a));t.level({inputMin:r.min[0],inputMax:r.max[0],out:t});const n=getMinMax(e);e.level({inputMin:n.min[0],inputMax:n.max[0],out:e})}const u=getBrief(t,{centroidPatchDiameter:n,bestKptRadius:o}),f=getBrief(e,{centroidPatchDiameter:n,bestKptRadius:o}),d=u.keypoints.length,m=f.keypoints.length;let g=[];if(s?g=getCrosscheckMatches(u.descriptors,f.descriptors):(g=bruteForceOneMatch(u.descriptors,f.descriptors),g=filterEuclideanDistance(g,u.keypoints,f.keypoints,{origin:a})),g.length<2)throw new Error("Insufficient number of matches found to compute affine transform (less than 2).");let p=[],w=[];for(const t of g)p.push(u.keypoints[t.sourceIndex].origin),w.push(f.keypoints[t.destinationIndex].origin);let y=p.length,b=0,x=[0,1];if(p.length>2){const t=ransac(p,w,{distanceFunction:getEuclideanDistance,modelFunction:createAffineTransformModel,fitFunction:affineFitFunction,maxNbIterations:l});b=t.nbIterations,x=t.inliers,y=x.length;const e=[],r=[];for(const t of x)e.push(p[t]),r.push(w[t]);p=e,w=r}if(h){if(!c)throw new RangeError("Debug image file path is not specified.");const r=new Montage(t,e,{disposition:MontageDisposition.VERTICAL});r.drawMatches(g,u.keypoints,f.keypoints,{showDistance:!0});const n=[];for(const t of x)n.push(g[t]);r.drawMatches(n,u.keypoints,f.keypoints,{strokeColor:[0,0,255]});const o={fill:!0,color:[0,255,0],showScore:!0,markerSize:3};r.drawKeypoints(u.keypoints,o),r.drawKeypoints(f.keypoints,{origin:r.destinationOrigin,...o}),writeSync(c,r.image)}const M=getAffineTransform$1(getMatrixFromPoints(p),getMatrixFromPoints(w));return{transform:{rotation:M.rotation,scale:M.scale,translation:{column:Math.round(M.translation.x),row:Math.round(M.translation.y)}},stats:{nbMatches:g.length,nbInliers:y,nbRansacIterations:b,nbSourceKeypoints:d,nbDestinationKeypoints:m}}}function alignMinDifference(t,e,r={}){checkProcessable$1(t,{bitDepth:[8,16]});const n=e.width-t.width,o=e.height-t.height,{startStep:i=Math.max(Math.round(Math.min(t.width,t.height,Math.max(n,o))/4),1),mask:s}=r;if(n<0||o<0)throw new Error("Source image must fit entirely in destination image");let a=Number.POSITIVE_INFINITY,l=0,h=0,c=i,u=0,f=0,d=n,m=o;if(s&&s.size!==t.size)throw new Error("Mask size must be equal to source size");const g=s?s.getNbNonZeroPixels():t.size;for(;c>=1;){c=Math.round(c);for(let r=u;r<=d;r+=c)for(let n=f;n<=m;n+=c){let o=0;t:for(let i=0;i<t.width;i++)for(let l=0;l<t.height;l++)if(!s||s.getBit(i,l))for(let s=0;s<t.channels;s++){const h=t.getValue(i,l,s)-e.getValue(i+r,l+n,s);if(h<0?o-=h:o+=h,o>a)break t}o<a&&(a=o,l=r,h=n)}c/=2,u=Math.round(Math.max(0,l-c)),f=Math.round(Math.max(0,h-c)),d=Math.round(Math.min(n,l+c)),m=Math.round(Math.min(o,h+c))}return{row:h,column:l,similarity:1-a/(g*t.maxValue)}}function removeClosePoints(t,e,r){const n=r?.distance||0,o=r?.kind||"maximum";if(void 0===r?.channel&&e.channels>1)throw new Error("image channel must be specified or image must have only one channel");const i=r?.channel||0,s="maximum"===o,a=t.slice();if(a.sort(getSort(e,i,s)),n>0)for(let t=0;t<a.length;t++)for(let e=t+1;e<a.length;e++)Math.hypot(a[t].column-a[e].column,a[t].row-a[e].row)<n&&(a.splice(e,1),e--);return a}function getSort(t,e,r){return r?function(r,n){return t.getValue(n.column,n.row,e)-t.getValue(r.column,r.row,e)}:function(r,n){return t.getValue(r.column,r.row,e)-t.getValue(n.column,n.row,e)}}var imageType$2={exports:{}},fileType$1={exports:{}};(function(module){const toBytes=t=>[...t].map(t=>t.charCodeAt(0)),xpiZipFilename=toBytes("META-INF/mozilla.rsa"),oxmlContentTypes=toBytes("[Content_Types].xml"),oxmlRels=toBytes("_rels/.rels");function readUInt64LE(t,e=0){let r=t[e],n=1,o=0;for(;++o<8;)n*=256,r+=t[e+o]*n;return r}const fileType=t=>{if(!(t instanceof Uint8Array||t instanceof ArrayBuffer||Buffer.isBuffer(t)))throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof t}\``);const e=t instanceof Uint8Array?t:new Uint8Array(t);if(!(e&&e.length>1))return null;const r=(t,r)=>{r=Object.assign({offset:0},r);for(let n=0;n<t.length;n++)if(r.mask){if(t[n]!==(r.mask[n]&e[n+r.offset]))return!1}else if(t[n]!==e[n+r.offset])return!1;return!0},n=(t,e)=>r(toBytes(t),e);if(r([255,216,255]))return{ext:"jpg",mime:"image/jpeg"};if(r([137,80,78,71,13,10,26,10]))return{ext:"png",mime:"image/png"};if(r([71,73,70]))return{ext:"gif",mime:"image/gif"};if(r([87,69,66,80],{offset:8}))return{ext:"webp",mime:"image/webp"};if(r([70,76,73,70]))return{ext:"flif",mime:"image/flif"};if((r([73,73,42,0])||r([77,77,0,42]))&&r([67,82],{offset:8}))return{ext:"cr2",mime:"image/x-canon-cr2"};if(r([73,73,42,0])||r([77,77,0,42]))return{ext:"tif",mime:"image/tiff"};if(r([66,77]))return{ext:"bmp",mime:"image/bmp"};if(r([73,73,188]))return{ext:"jxr",mime:"image/vnd.ms-photo"};if(r([56,66,80,83]))return{ext:"psd",mime:"image/vnd.adobe.photoshop"};if(r([80,75,3,4])){if(r([109,105,109,101,116,121,112,101,97,112,112,108,105,99,97,116,105,111,110,47,101,112,117,98,43,122,105,112],{offset:30}))return{ext:"epub",mime:"application/epub+zip"};if(r(xpiZipFilename,{offset:30}))return{ext:"xpi",mime:"application/x-xpinstall"};if(n("mimetypeapplication/vnd.oasis.opendocument.text",{offset:30}))return{ext:"odt",mime:"application/vnd.oasis.opendocument.text"};if(n("mimetypeapplication/vnd.oasis.opendocument.spreadsheet",{offset:30}))return{ext:"ods",mime:"application/vnd.oasis.opendocument.spreadsheet"};if(n("mimetypeapplication/vnd.oasis.opendocument.presentation",{offset:30}))return{ext:"odp",mime:"application/vnd.oasis.opendocument.presentation"};const t=(t,e=0)=>t.findIndex((t,r,n)=>r>=e&&80===n[r]&&75===n[r+1]&&3===n[r+2]&&4===n[r+3]);let o=0,i=!1,s=null;do{const a=o+30;if(i||(i=r(oxmlContentTypes,{offset:a})||r(oxmlRels,{offset:a})),s||(n("word/",{offset:a})?s={ext:"docx",mime:"application/vnd.openxmlformats-officedocument.wordprocessingml.document"}:n("ppt/",{offset:a})?s={ext:"pptx",mime:"application/vnd.openxmlformats-officedocument.presentationml.presentation"}:n("xl/",{offset:a})&&(s={ext:"xlsx",mime:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})),i&&s)return s;o=t(e,a)}while(o>=0);if(s)return s}if(r([80,75])&&(3===e[2]||5===e[2]||7===e[2])&&(4===e[3]||6===e[3]||8===e[3]))return{ext:"zip",mime:"application/zip"};if(r([117,115,116,97,114],{offset:257}))return{ext:"tar",mime:"application/x-tar"};if(r([82,97,114,33,26,7])&&(0===e[6]||1===e[6]))return{ext:"rar",mime:"application/x-rar-compressed"};if(r([31,139,8]))return{ext:"gz",mime:"application/gzip"};if(r([66,90,104]))return{ext:"bz2",mime:"application/x-bzip2"};if(r([55,122,188,175,39,28]))return{ext:"7z",mime:"application/x-7z-compressed"};if(r([120,1]))return{ext:"dmg",mime:"application/x-apple-diskimage"};if(r([51,103,112,53])||r([0,0,0])&&r([102,116,121,112],{offset:4})&&(r([109,112,52,49],{offset:8})||r([109,112,52,50],{offset:8})||r([105,115,111,109],{offset:8})||r([105,115,111,50],{offset:8})||r([109,109,112,52],{offset:8})||r([77,52,86],{offset:8})||r([100,97,115,104],{offset:8})))return{ext:"mp4",mime:"video/mp4"};if(r([77,84,104,100]))return{ext:"mid",mime:"audio/midi"};if(r([26,69,223,163])){const t=e.subarray(4,4100),r=t.findIndex((t,e,r)=>66===r[e]&&130===r[e+1]);if(-1!==r){const e=r+3,n=r=>[...r].every((r,n)=>t[e+n]===r.charCodeAt(0));if(n("matroska"))return{ext:"mkv",mime:"video/x-matroska"};if(n("webm"))return{ext:"webm",mime:"video/webm"}}}if(r([0,0,0,20,102,116,121,112,113,116,32,32])||r([102,114,101,101],{offset:4})||r([102,116,121,112,113,116,32,32],{offset:4})||r([109,100,97,116],{offset:4})||r([109,111,111,118],{offset:4})||r([119,105,100,101],{offset:4}))return{ext:"mov",mime:"video/quicktime"};if(r([82,73,70,70])){if(r([65,86,73],{offset:8}))return{ext:"avi",mime:"video/vnd.avi"};if(r([87,65,86,69],{offset:8}))return{ext:"wav",mime:"audio/vnd.wave"};if(r([81,76,67,77],{offset:8}))return{ext:"qcp",mime:"audio/qcelp"}}if(r([48,38,178,117,142,102,207,17,166,217])){let t=30;do{const n=readUInt64LE(e,t+16);if(r([145,7,220,183,183,169,207,17,142,230,0,192,12,32,83,101],{offset:t})){if(r([64,158,105,248,77,91,207,17,168,253,0,128,95,92,68,43],{offset:t+24}))return{ext:"wma",mime:"audio/x-ms-wma"};if(r([192,239,25,188,77,91,207,17,168,253,0,128,95,92,68,43],{offset:t+24}))return{ext:"wmv",mime:"video/x-ms-asf"};break}t+=n}while(t+24<=e.length);return{ext:"asf",mime:"application/vnd.ms-asf"}}if(r([0,0,1,186])||r([0,0,1,179]))return{ext:"mpg",mime:"video/mpeg"};if(r([102,116,121,112,51,103],{offset:4}))return{ext:"3gp",mime:"video/3gpp"};for(let t=0;t<2&&t<e.length-16;t++){if(r([73,68,51],{offset:t})||r([255,226],{offset:t,mask:[255,226]}))return{ext:"mp3",mime:"audio/mpeg"};if(r([255,228],{offset:t,mask:[255,228]}))return{ext:"mp2",mime:"audio/mpeg"};if(r([255,248],{offset:t,mask:[255,252]}))return{ext:"mp2",mime:"audio/mpeg"};if(r([255,240],{offset:t,mask:[255,252]}))return{ext:"mp4",mime:"audio/mpeg"}}if(r([102,116,121,112,77,52,65],{offset:4}))return{ext:"m4a",mime:"audio/mp4"};if(r([79,112,117,115,72,101,97,100],{offset:28}))return{ext:"opus",mime:"audio/opus"};if(r([79,103,103,83]))return r([128,116,104,101,111,114,97],{offset:28})?{ext:"ogv",mime:"video/ogg"}:r([1,118,105,100,101,111,0],{offset:28})?{ext:"ogm",mime:"video/ogg"}:r([127,70,76,65,67],{offset:28})?{ext:"oga",mime:"audio/ogg"}:r([83,112,101,101,120,32,32],{offset:28})?{ext:"spx",mime:"audio/ogg"}:r([1,118,111,114,98,105,115],{offset:28})?{ext:"ogg",mime:"audio/ogg"}:{ext:"ogx",mime:"application/ogg"};if(r([102,76,97,67]))return{ext:"flac",mime:"audio/x-flac"};if(r([77,65,67,32]))return{ext:"ape",mime:"audio/ape"};if(r([119,118,112,107]))return{ext:"wv",mime:"audio/wavpack"};if(r([35,33,65,77,82,10]))return{ext:"amr",mime:"audio/amr"};if(r([37,80,68,70]))return{ext:"pdf",mime:"application/pdf"};if(r([77,90]))return{ext:"exe",mime:"application/x-msdownload"};if((67===e[0]||70===e[0])&&r([87,83],{offset:1}))return{ext:"swf",mime:"application/x-shockwave-flash"};if(r([123,92,114,116,102]))return{ext:"rtf",mime:"application/rtf"};if(r([0,97,115,109]))return{ext:"wasm",mime:"application/wasm"};if(r([119,79,70,70])&&(r([0,1,0,0],{offset:4})||r([79,84,84,79],{offset:4})))return{ext:"woff",mime:"font/woff"};if(r([119,79,70,50])&&(r([0,1,0,0],{offset:4})||r([79,84,84,79],{offset:4})))return{ext:"woff2",mime:"font/woff2"};if(r([76,80],{offset:34})&&(r([0,0,1],{offset:8})||r([1,0,2],{offset:8})||r([2,0,2],{offset:8})))return{ext:"eot",mime:"application/vnd.ms-fontobject"};if(r([0,1,0,0,0]))return{ext:"ttf",mime:"font/ttf"};if(r([79,84,84,79,0]))return{ext:"otf",mime:"font/otf"};if(r([0,0,1,0]))return{ext:"ico",mime:"image/x-icon"};if(r([0,0,2,0]))return{ext:"cur",mime:"image/x-icon"};if(r([70,76,86,1]))return{ext:"flv",mime:"video/x-flv"};if(r([37,33]))return{ext:"ps",mime:"application/postscript"};if(r([253,55,122,88,90,0]))return{ext:"xz",mime:"application/x-xz"};if(r([83,81,76,105]))return{ext:"sqlite",mime:"application/x-sqlite3"};if(r([78,69,83,26]))return{ext:"nes",mime:"application/x-nintendo-nes-rom"};if(r([67,114,50,52]))return{ext:"crx",mime:"application/x-google-chrome-extension"};if(r([77,83,67,70])||r([73,83,99,40]))return{ext:"cab",mime:"application/vnd.ms-cab-compressed"};if(r([33,60,97,114,99,104,62,10,100,101,98,105,97,110,45,98,105,110,97,114,121]))return{ext:"deb",mime:"application/x-deb"};if(r([33,60,97,114,99,104,62]))return{ext:"ar",mime:"application/x-unix-archive"};if(r([237,171,238,219]))return{ext:"rpm",mime:"application/x-rpm"};if(r([31,160])||r([31,157]))return{ext:"Z",mime:"application/x-compress"};if(r([76,90,73,80]))return{ext:"lz",mime:"application/x-lzip"};if(r([208,207,17,224,161,177,26,225]))return{ext:"msi",mime:"application/x-msi"};if(r([6,14,43,52,2,5,1,1,13,1,2,1,1,2]))return{ext:"mxf",mime:"application/mxf"};if(r([71],{offset:4})&&(r([71],{offset:192})||r([71],{offset:196})))return{ext:"mts",mime:"video/mp2t"};if(r([66,76,69,78,68,69,82]))return{ext:"blend",mime:"application/x-blender"};if(r([66,80,71,251]))return{ext:"bpg",mime:"image/bpg"};if(r([0,0,0,12,106,80,32,32,13,10,135,10])){if(r([106,112,50,32],{offset:20}))return{ext:"jp2",mime:"image/jp2"};if(r([106,112,120,32],{offset:20}))return{ext:"jpx",mime:"image/jpx"};if(r([106,112,109,32],{offset:20}))return{ext:"jpm",mime:"image/jpm"};if(r([109,106,112,50],{offset:20}))return{ext:"mj2",mime:"image/mj2"}}if(r([70,79,82,77]))return{ext:"aif",mime:"audio/aiff"};if(n("<?xml "))return{ext:"xml",mime:"application/xml"};if(r([66,79,79,75,77,79,66,73],{offset:60}))return{ext:"mobi",mime:"application/x-mobipocket-ebook"};if(r([102,116,121,112],{offset:4})){if(r([109,105,102,49],{offset:8}))return{ext:"heic",mime:"image/heif"};if(r([109,115,102,49],{offset:8}))return{ext:"heic",mime:"image/heif-sequence"};if(r([104,101,105,99],{offset:8})||r([104,101,105,120],{offset:8}))return{ext:"heic",mime:"image/heic"};if(r([104,101,118,99],{offset:8})||r([104,101,118,120],{offset:8}))return{ext:"heic",mime:"image/heic-sequence"}}return r([171,75,84,88,32,49,49,187,13,10,26,10])?{ext:"ktx",mime:"image/ktx"}:r([68,73,67,77],{offset:128})?{ext:"dcm",mime:"application/dicom"}:r([77,80,43])||r([77,80,67,75])?{ext:"mpc",mime:"audio/x-musepack"}:r([66,69,71,73,78,58])?{ext:"ics",mime:"text/calendar"}:r([103,108,84,70,2,0,0,0])?{ext:"glb",mime:"model/gltf-binary"}:r([212,195,178,161])||r([161,178,195,212])?{ext:"pcap",mime:"application/vnd.tcpdump.pcap"}:null};module.exports=fileType,module.exports.default=fileType,Object.defineProperty(fileType,"minimumBytes",{value:4100}),module.exports.stream=readableStream=>new Promise((resolve,reject)=>{const stream=eval("require")("stream");readableStream.once("readable",()=>{const t=new stream.PassThrough,e=readableStream.read(module.exports.minimumBytes)||readableStream.read();try{t.fileType=fileType(e)}catch(t){reject(t)}readableStream.unshift(e),stream.pipeline?resolve(stream.pipeline(readableStream,t,()=>{})):resolve(readableStream.pipe(t))})})})(fileType$1);var fileTypeExports=fileType$1.exports;const fileType=fileTypeExports,imageExts=new Set(["jpg","png","gif","webp","flif","cr2","tif","bmp","jxr","psd","ico","bpg","jp2","jpm","jpx","heic","cur","dcm"]),imageType=t=>{const e=fileType(t);return imageExts.has(e&&e.ext)?e:null};imageType$2.exports=imageType,imageType$2.exports.default=imageType,Object.defineProperty(imageType,"minimumBytes",{value:fileType.minimumBytes});var imageTypeExports=imageType$2.exports,imageType$1=getDefaultExportFromCjs(imageTypeExports);function decodeBmp(t){const e=decode$3(t);if(1===e.bitsPerPixel){return new Mask(e.width,e.height,{data:e.data}).convertColor("GREY")}{let t;switch(e.channels){case 1:t="GREY";break;case 3:t="RGB";break;default:t="RGBA"}return new Image(e.width,e.height,{colorModel:t,data:e.data,resolution:getBmpResolution(e)})}}function getBmpResolution(t){if(t.xPixelsPerMeter&&t.yPixelsPerMeter)return{x:t.xPixelsPerMeter,y:t.yPixelsPerMeter,unit:"meter"}}const tagsById$2={33434:"ExposureTime",33437:"FNumber",34850:"ExposureProgram",34852:"SpectralSensitivity",34855:"ISOSpeedRatings",34856:"OECF",34864:"SensitivityType",34865:"StandardOutputSensitivity",34866:"RecommendedExposureIndex",34867:"ISOSpeed",34868:"ISOSpeedLatitudeyyy",34869:"ISOSpeedLatitudezzz",36864:"ExifVersion",36867:"DateTimeOriginal",36868:"DateTimeDigitized",37121:"ComponentsConfiguration",37122:"CompressedBitsPerPixel",37377:"ShutterSpeedValue",37378:"ApertureValue",37379:"BrightnessValue",37380:"ExposureBiasValue",37381:"MaxApertureValue",37382:"SubjectDistance",37383:"MeteringMode",37384:"LightSource",37385:"Flash",37386:"FocalLength",37396:"SubjectArea",37500:"MakerNote",37510:"UserComment",37520:"SubsecTime",37521:"SubsecTimeOriginal",37522:"SubsecTimeDigitized",40960:"FlashpixVersion",40961:"ColorSpace",40962:"PixelXDimension",40963:"PixelYDimension",40964:"RelatedSoundFile",41483:"FlashEnergy",41484:"SpatialFrequencyResponse",41486:"FocalPlaneXResolution",41487:"FocalPlaneYResolution",41488:"FocalPlaneResolutionUnit",41492:"SubjectLocation",41493:"ExposureIndex",41495:"SensingMethod",41728:"FileSource",41729:"SceneType",41730:"CFAPattern",41985:"CustomRendered",41986:"ExposureMode",41987:"WhiteBalance",41988:"DigitalZoomRatio",41989:"FocalLengthIn35mmFilm",41990:"SceneCaptureType",41991:"GainControl",41992:"Contrast",41993:"Saturation",41994:"Sharpness",41995:"DeviceSettingDescription",41996:"SubjectDistanceRange",42016:"ImageUniqueID",42032:"CameraOwnerName",42033:"BodySerialNumber",42034:"LensSpecification",42035:"LensMake",42036:"LensModel",42037:"LensSerialNumber",42240:"Gamma"},tagsByName$2={};for(const t in tagsById$2)tagsByName$2[tagsById$2[t]]=Number(t);var exif=Object.freeze({__proto__:null,tagsById:tagsById$2,tagsByName:tagsByName$2});const tagsById$1={0:"GPSVersionID",1:"GPSLatitudeRef",2:"GPSLatitude",3:"GPSLongitudeRef",4:"GPSLongitude",5:"GPSAltitudeRef",6:"GPSAltitude",7:"GPSTimeStamp",8:"GPSSatellites",9:"GPSStatus",10:"GPSMeasureMode",11:"GPSDOP",12:"GPSSpeedRef",13:"GPSSpeed",14:"GPSTrackRef",15:"GPSTrack",16:"GPSImgDirectionRef",17:"GPSImgDirection",18:"GPSMapDatum",19:"GPSDestLatitudeRef",20:"GPSDestLatitude",21:"GPSDestLongitudeRef",22:"GPSDestLongitude",23:"GPSDestBearingRef",24:"GPSDestBearing",25:"GPSDestDistanceRef",26:"GPSDestDistance",27:"GPSProcessingMethod",28:"GPSAreaInformation",29:"GPSDateStamp",30:"GPSDifferential",31:"GPSHPositioningError"},tagsByName$1={};for(const t in tagsById$1)tagsByName$1[tagsById$1[t]]=Number(t);var gps=Object.freeze({__proto__:null,tagsById:tagsById$1,tagsByName:tagsByName$1});const tagsById={254:"NewSubfileType",255:"SubfileType",256:"ImageWidth",257:"ImageLength",258:"BitsPerSample",259:"Compression",262:"PhotometricInterpretation",263:"Threshholding",264:"CellWidth",265:"CellLength",266:"FillOrder",270:"ImageDescription",271:"Make",272:"Model",273:"StripOffsets",274:"Orientation",277:"SamplesPerPixel",278:"RowsPerStrip",279:"StripByteCounts",280:"MinSampleValue",281:"MaxSampleValue",282:"XResolution",283:"YResolution",284:"PlanarConfiguration",288:"FreeOffsets",289:"FreeByteCounts",290:"GrayResponseUnit",291:"GrayResponseCurve",296:"ResolutionUnit",305:"Software",306:"DateTime",315:"Artist",316:"HostComputer",320:"ColorMap",338:"ExtraSamples",33432:"Copyright",269:"DocumentName",285:"PageName",286:"XPosition",287:"YPosition",292:"T4Options",293:"T6Options",297:"PageNumber",301:"TransferFunction",317:"Predictor",318:"WhitePoint",319:"PrimaryChromaticities",321:"HalftoneHints",322:"TileWidth",323:"TileLength",324:"TileOffsets",325:"TileByteCounts",326:"BadFaxLines",327:"CleanFaxData",328:"ConsecutiveBadFaxLines",330:"SubIFDs",332:"InkSet",333:"InkNames",334:"NumberOfInks",336:"DotRange",337:"TargetPrinter",339:"SampleFormat",340:"SMinSampleValue",341:"SMaxSampleValue",342:"TransferRange",343:"ClipPath",344:"XClipPathUnits",345:"YClipPathUnits",346:"Indexed",347:"JPEGTables",351:"OPIProxy",400:"GlobalParametersIFD",401:"ProfileType",402:"FaxProfile",403:"CodingMethods",404:"VersionYear",405:"ModeNumber",433:"Decode",434:"DefaultImageColor",512:"JPEGProc",513:"JPEGInterchangeFormat",514:"JPEGInterchangeFormatLength",515:"JPEGRestartInterval",517:"JPEGLosslessPredictors",518:"JPEGPointTransforms",519:"JPEGQTables",520:"JPEGDCTables",521:"JPEGACTables",529:"YCbCrCoefficients",530:"YCbCrSubSampling",531:"YCbCrPositioning",532:"ReferenceBlackWhite",559:"StripRowCounts",700:"XMP",32781:"ImageID",34732:"ImageLayer",32932:"WangAnnotatio",33445:"MDFileTag",33446:"MDScalePixel",33447:"MDColorTable",33448:"MDLabName",33449:"MDSampleInfo",33450:"MDPrepDate",33451:"MDPrepTime",33452:"MDFileUnits",33550:"ModelPixelScaleTag",33723:"IPTC",33918:"INGRPacketDataTag",33919:"INGRFlagRegisters",33920:"IrasBTransformationMatrix",33922:"ModelTiepointTag",34264:"ModelTransformationTag",34377:"Photoshop",34665:"ExifIFD",34675:"ICCProfile",34735:"GeoKeyDirectoryTag",34736:"GeoDoubleParamsTag",34737:"GeoAsciiParamsTag",34853:"GPSIFD",34908:"HylaFAXFaxRecvParams",34909:"HylaFAXFaxSubAddress",34910:"HylaFAXFaxRecvTime",37724:"ImageSourceData",40965:"InteroperabilityIFD",42112:"GDAL_METADATA",42113:"GDAL_NODATA",50215:"OceScanjobDescription",50216:"OceApplicationSelector",50217:"OceIdentificationNumber",50218:"OceImageLogicCharacteristics",50706:"DNGVersion",50707:"DNGBackwardVersion",50708:"UniqueCameraModel",50709:"LocalizedCameraModel",50710:"CFAPlaneColor",50711:"CFALayout",50712:"LinearizationTable",50713:"BlackLevelRepeatDim",50714:"BlackLevel",50715:"BlackLevelDeltaH",50716:"BlackLevelDeltaV",50717:"WhiteLevel",50718:"DefaultScale",50719:"DefaultCropOrigin",50720:"DefaultCropSize",50721:"ColorMatrix1",50722:"ColorMatrix2",50723:"CameraCalibration1",50724:"CameraCalibration2",50725:"ReductionMatrix1",50726:"ReductionMatrix2",50727:"AnalogBalance",50728:"AsShotNeutral",50729:"AsShotWhiteXY",50730:"BaselineExposure",50731:"BaselineNoise",50732:"BaselineSharpness",50733:"BayerGreenSplit",50734:"LinearResponseLimit",50735:"CameraSerialNumber",50736:"LensInfo",50737:"ChromaBlurRadius",50738:"AntiAliasStrength",50740:"DNGPrivateData",50741:"MakerNoteSafety",50778:"CalibrationIlluminant1",50779:"CalibrationIlluminant2",50780:"BestQualityScale",50784:"AliasLayerMetadata"},tagsByName={};for(const t in tagsById)tagsByName[tagsById[t]]=Number(t);var standard=Object.freeze({__proto__:null,tagsById:tagsById,tagsByName:tagsByName});function guessStripByteCounts(t){if(1!==t.compression)throw new Error("missing mandatory StripByteCounts field in compressed image");const e=t.rowsPerStrip*t.width*t.samplesPerPixel*(t.bitsPerSample/8);return new Array(t.stripOffsets.length).fill(e)}function applyHorizontalDifferencing8Bit(t,e,r){let n=0;for(;n<t.length;){for(let o=r;o<e*r;o+=r)for(let e=0;e<r;e++)t[n+o+e]=t[n+o+e]+t[n+o-(r-e)]&255;n+=e*r}}function applyHorizontalDifferencing16Bit(t,e,r){let n=0;for(;n<t.length;){for(let o=r;o<e*r;o+=r)for(let e=0;e<r;e++)t[n+o+e]=t[n+o+e]+t[n+o-(r-e)]&65535;n+=e*r}}const tags={standard:standard,exif:exif,gps:gps};class IFD{kind;data;fields;exif;gps;_hasMap;_map;constructor(t){if(!t)throw new Error("missing kind");this.data=new Uint8Array,this.fields=new Map,this.kind=t,this._hasMap=!1,this._map={}}get(t){if("number"==typeof t)return this.fields.get(t);if("string"==typeof t)return this.fields.get(tags[this.kind].tagsByName[t]);throw new Error("expected a number or string")}get map(){if(!this._hasMap){const t=tags[this.kind].tagsById;for(const e of this.fields.keys())t[e]&&(this._map[t[e]]=this.fields.get(e));this._hasMap=!0}return this._map}}const types=new Map([[1,[1,readByte]],[2,[1,readASCII]],[3,[2,readShort]],[4,[4,readLong]],[5,[8,readRational]],[6,[1,readSByte]],[7,[1,readByte]],[8,[2,readSShort]],[9,[4,readSLong]],[10,[8,readSRational]],[11,[4,readFloat]],[12,[8,readDouble]]]);function getByteLength(t,e){const r=types.get(t);if(!r)throw new Error(`type not found: ${t}`);return r[0]*e}function readData(t,e,r){const n=types.get(e);if(!n)throw new Error(`type not found: ${e}`);return n[1](t,r)}function readByte(t,e){if(1===e)return t.readUint8();const r=new Uint8Array(e);for(let n=0;n<e;n++)r[n]=t.readUint8();return r}function readASCII(t,e){const r=[];let n="";for(let o=0;o<e;o++){const e=String.fromCharCode(t.readUint8());"\0"===e?(r.push(n),n=""):n+=e}return 1===r.length?r[0]:r}function readShort(t,e){if(1===e)return t.readUint16();const r=new Uint16Array(e);for(let n=0;n<e;n++)r[n]=t.readUint16();return r}function readLong(t,e){if(1===e)return t.readUint32();const r=new Uint32Array(e);for(let n=0;n<e;n++)r[n]=t.readUint32();return r}function readRational(t,e){if(1===e)return t.readUint32()/t.readUint32();const r=new Array(e);for(let n=0;n<e;n++)r[n]=t.readUint32()/t.readUint32();return r}function readSByte(t,e){if(1===e)return t.readInt8();const r=new Int8Array(e);for(let n=0;n<e;n++)r[n]=t.readInt8();return r}function readSShort(t,e){if(1===e)return t.readInt16();const r=new Int16Array(e);for(let n=0;n<e;n++)r[n]=t.readInt16();return r}function readSLong(t,e){if(1===e)return t.readInt32();const r=new Int32Array(e);for(let n=0;n<e;n++)r[n]=t.readInt32();return r}function readSRational(t,e){if(1===e)return t.readInt32()/t.readInt32();const r=new Array(e);for(let n=0;n<e;n++)r[n]=t.readInt32()/t.readInt32();return r}function readFloat(t,e){if(1===e)return t.readFloat32();const r=new Float32Array(e);for(let n=0;n<e;n++)r[n]=t.readFloat32();return r}function readDouble(t,e){if(1===e)return t.readFloat64();const r=new Float64Array(e);for(let n=0;n<e;n++)r[n]=t.readFloat64();return r}const CLEAR_CODE=256,EOI_CODE=257,TABLE_START=258,MIN_BIT_LENGTH=9;let stringTable=[];function initializeStringTable(){if(0===stringTable.length){for(let t=0;t<256;t++)stringTable.push([t]);const t=[];for(let e=256;e<4096;e++)stringTable.push(t)}}const andTable=[511,1023,2047,4095],bitJumps=[0,0,0,0,0,0,0,0,0,511,1023,2047,4095];class LzwDecoder{stripArray;nextData=0;nextBits=0;bytePointer=0;tableLength=TABLE_START;currentBitLength=MIN_BIT_LENGTH;outData;constructor(t){this.stripArray=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),this.outData=new IOBuffer(t.byteLength),this.initializeTable()}decode(){let t=0,e=0;for(;(t=this.getNextCode())!==EOI_CODE;)if(t===CLEAR_CODE){if(this.initializeTable(),t=this.getNextCode(),t===EOI_CODE)break;this.writeString(this.stringFromCode(t)),e=t}else if(this.isInTable(t))this.writeString(this.stringFromCode(t)),this.addStringToTable(this.stringFromCode(e).concat(this.stringFromCode(t)[0])),e=t;else{const r=this.stringFromCode(e).concat(this.stringFromCode(e)[0]);this.writeString(r),this.addStringToTable(r),e=t}const r=this.outData.toArray();return new DataView(r.buffer,r.byteOffset,r.byteLength)}initializeTable(){initializeStringTable(),this.tableLength=TABLE_START,this.currentBitLength=MIN_BIT_LENGTH}writeString(t){this.outData.writeBytes(t)}stringFromCode(t){return stringTable[t]}isInTable(t){return t<this.tableLength}addStringToTable(t){if(stringTable[this.tableLength++]=t,stringTable.length>4096)throw stringTable=[],new Error("LZW decoding error. Please open an issue at https://github.com/image-js/tiff/issues/new/choose (include a test image).");this.tableLength===bitJumps[this.currentBitLength]&&this.currentBitLength++}getNextCode(){this.nextData=this.nextData<<8|255&this.stripArray[this.bytePointer++],this.nextBits+=8,this.nextBits<this.currentBitLength&&(this.nextData=this.nextData<<8|255&this.stripArray[this.bytePointer++],this.nextBits+=8);const t=this.nextData>>this.nextBits-this.currentBitLength&andTable[this.currentBitLength-9];return this.nextBits-=this.currentBitLength,this.bytePointer>this.stripArray.length?257:t}}function decompressLzw(t){return new LzwDecoder(t).decode()}const dateTimeRegex=/^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;class TiffIfd extends IFD{constructor(){super("standard")}get size(){return this.width*this.height}get width(){return this.imageWidth}get height(){return this.imageLength}get components(){return this.samplesPerPixel}get date(){const t=new Date,e=dateTimeRegex.exec(this.dateTime);if(null===e)throw new Error(`invalid dateTime: ${this.dateTime}`);return t.setFullYear(Number(e[1]),Number(e[2])-1,Number(e[3])),t.setHours(Number(e[4]),Number(e[5]),Number(e[6])),t}get newSubfileType(){return this.get("NewSubfileType")}get imageWidth(){return this.get("ImageWidth")}get imageLength(){return this.get("ImageLength")}get bitsPerSample(){const t=this.get("BitsPerSample");return t&&"number"!=typeof t?t[0]:t}get alpha(){const t=this.extraSamples;return!!t&&0!==t[0]}get associatedAlpha(){const t=this.extraSamples;return!!t&&1===t[0]}get extraSamples(){return alwaysArray(this.get("ExtraSamples"))}get compression(){return this.get("Compression")||1}get type(){return this.get("PhotometricInterpretation")}get fillOrder(){return this.get("FillOrder")||1}get documentName(){return this.get("DocumentName")}get imageDescription(){return this.get("ImageDescription")}get stripOffsets(){return alwaysArray(this.get("StripOffsets"))}get orientation(){return this.get("Orientation")}get samplesPerPixel(){return this.get("SamplesPerPixel")||1}get rowsPerStrip(){return this.get("RowsPerStrip")||2**32-1}get stripByteCounts(){return alwaysArray(this.get("StripByteCounts"))}get minSampleValue(){return this.get("MinSampleValue")||0}get maxSampleValue(){return this.get("MaxSampleValue")||2**this.bitsPerSample-1}get xResolution(){return this.get("XResolution")}get yResolution(){return this.get("YResolution")}get planarConfiguration(){return this.get("PlanarConfiguration")||1}get resolutionUnit(){return this.get("ResolutionUnit")||2}get dateTime(){return this.get("DateTime")}get predictor(){return this.get("Predictor")||1}get sampleFormat(){return alwaysArray(this.get("SampleFormat")||1)[0]}get sMinSampleValue(){return this.get("SMinSampleValue")||this.minSampleValue}get sMaxSampleValue(){return this.get("SMaxSampleValue")||this.maxSampleValue}get palette(){const t=2**this.bitsPerSample,e=this.get("ColorMap");if(!e)return;if(e.length!==3*t)throw new Error(`ColorMap size must be ${t}`);const r=[];for(let n=0;n<t;n++)r.push([e[n],e[n+t],e[n+2*t]]);return r}get tileWidth(){return this.get("TileWidth")}get tileHeight(){return this.get("TileLength")}get tileOffsets(){return alwaysArray(this.get("TileOffsets"))}get tileByteCounts(){return alwaysArray(this.get("TileByteCounts"))}get tiled(){return void 0!==this.tileWidth&&void 0!==this.tileHeight&&void 0!==this.tileOffsets&&void 0!==this.tileByteCounts}}function alwaysArray(t){return"number"==typeof t?[t]:t}function decompressZlib(t){const e=decompressSync(new Uint8Array(t.buffer,t.byteOffset,t.byteLength));return new DataView(e.buffer,e.byteOffset,e.byteLength)}const defaultOptions={ignoreImageData:!1};class TIFFDecoder extends IOBuffer{_nextIFD;constructor(t){super(t),this._nextIFD=0}get isMultiPage(){let t=0;for(this.decodeHeader();this._nextIFD;)if(t++,this.decodeIFD({ignoreImageData:!0},!0),2===t)return!0;if(1===t)return!1;throw unsupported("ifdCount",t)}get pageCount(){let t=0;for(this.decodeHeader();this._nextIFD;)t++,this.decodeIFD({ignoreImageData:!0},!0);if(t>0)return t;throw unsupported("ifdCount",t)}decode(t={}){const{pages:e}=t;checkPages(e);const r=e?Math.max(...e):1/0;t={...defaultOptions,...t};const n=[];this.decodeHeader();let o=0;for(;this._nextIFD;){if(e){if(e.includes(o)?n.push(this.decodeIFD(t,!0)):this.decodeIFD({ignoreImageData:!0},!0),o===r)break}else n.push(this.decodeIFD(t,!0));o++}if(o<r&&r!==1/0)throw new RangeError(`Index ${r} is out of bounds. The stack only contains ${o} images.`);return n}decodeHeader(){const t=this.readUint16();if(18761===t)this.setLittleEndian();else{if(19789!==t)throw new Error(`invalid byte order: 0x${t.toString(16)}`);this.setBigEndian()}if(42!==this.readUint16())throw new Error("not a TIFF file");this._nextIFD=this.readUint32()}decodeIFD(t,e){let r;if(this.seek(this._nextIFD),e)r=new TiffIfd;else{if(!t.kind)throw new Error("kind is missing");r=new IFD(t.kind)}const n=this.readUint16();for(let t=0;t<n;t++)this.decodeIFDEntry(r);if(!t.ignoreImageData){if(!(r instanceof TiffIfd))throw new Error("must be a tiff ifd");this.decodeImageData(r)}return this._nextIFD=this.readUint32(),r}decodeIFDEntry(t){const e=this.offset,r=this.readUint16(),n=this.readUint16(),o=this.readUint32();if(n<1||n>12)return void this.skip(4);getByteLength(n,o)>4&&this.seek(this.readUint32());const i=readData(this,n,o);if(t.fields.set(r,i),34665===r||34853===r){const e=this.offset;let n="exif";34665===r?n="exif":34853===r&&(n="gps"),this._nextIFD=i,t[n]=this.decodeIFD({kind:n,ignoreImageData:!0},!1),this.offset=e}this.seek(e),this.skip(12)}decodeImageData(t){const e=t.orientation;if(e&&1!==e)throw unsupported("orientation",e);switch(t.type){case 0:case 1:case 2:case 3:t.tiled?this.readTileData(t):this.readStripData(t);break;default:throw unsupported("image type",t.type)}if(this.applyPredictor(t),this.convertAlpha(t),1===t.bitsPerSample&&this.split1BitData(t),0===t.type){const e=2**t.bitsPerSample-1;for(let r=0;r<t.data.length;r++)t.data[r]=e-t.data[r]}}split1BitData(t){const{imageWidth:e,imageLength:r,samplesPerPixel:n}=t,o=new Uint8Array(r*e*n),i=Math.ceil(e*n/8);let s=0;for(let a=0;a<r;a++){const r=a*i;for(let i=0;i<e*n;i++){const e=r+Math.floor(i/8),n=7-i%8,a=t.data[e]>>n&1;o[s++]=a}}t.data=o}static uncompress(t,e=1){switch(e){case 1:return t;case 5:return decompressLzw(t);case 8:case 32946:return decompressZlib(t);case 2:throw unsupported("Compression","CCITT Group 3");case 32773:throw unsupported("Compression","PackBits");default:throw unsupported("Compression",e)}}createSampleReader(t,e,r){if(8===e||1===e)return(t,e)=>t.getUint8(e);if(16===e)return(t,e)=>t.getUint16(2*e,r);if(32===e&&3===t)return(t,e)=>t.getFloat32(4*e,r);if(64===e&&3===t)return(t,e)=>t.getFloat64(8*e,r);throw unsupported("bitDepth",e)}readStripData(t){const e=t.width,r=t.height,n=1!==t.bitsPerSample?e*t.samplesPerPixel*r:Math.ceil(e*t.samplesPerPixel/8)*r,o=t.stripOffsets,i=t.stripByteCounts||guessStripByteCounts(t),s=this.isLittleEndian(),a=1!==t.bitsPerSample?e*t.samplesPerPixel*t.rowsPerStrip:Math.ceil(e*t.samplesPerPixel/8)*t.rowsPerStrip,l=this.createSampleReader(t.sampleFormat,t.bitsPerSample,s),h=getDataArray(n,t.bitsPerSample,t.sampleFormat);let c=0;for(let e=0;e<o.length;e++){const r=new DataView(this.buffer,this.byteOffset+o[e],i[e]),s=TIFFDecoder.uncompress(r,t.compression),u=Math.min(a,n-c);for(let t=0;t<u;++t){const e=l(s,t);h[c+t]=e}c+=u}t.data=h}readTileData(t){if(!t.tileWidth||!t.tileHeight)return;const e=t.width,r=t.height,n=1!==t.bitsPerSample?e*r*t.samplesPerPixel:Math.ceil(e*t.samplesPerPixel/8)*r,o=t.tileWidth,i=t.tileHeight,s=Math.ceil(e/o),a=Math.ceil(r/i),l=t.tileOffsets,h=t.tileByteCounts,c=this.isLittleEndian(),u=this.createSampleReader(t.sampleFormat,t.bitsPerSample,c),f=getDataArray(n,t.bitsPerSample,t.sampleFormat);for(let n=0;n<s;++n)for(let c=0;c<a;++c){const a=c*s+n,d=new DataView(this.buffer,this.byteOffset+l[a],h[a]),m=TIFFDecoder.uncompress(d,t.compression);if(1===t.bitsPerSample){const t=Math.ceil(e/8),s=Math.ceil(o/8);for(let e=0;e<i&&c*i+e<r;e++){const r=e*s,a=(c*i+e)*t+Math.floor(n*o/8),l=Math.min(s,t-Math.floor(n*o/8));for(let t=0;t<l;t++)f[a+t]=u(m,r+t)}}else for(let s=0;s<i;s++)for(let a=0;a<o;a++){const l=n*o+a,h=c*i+s;if(l>=e||h>=r)continue;const d=u(m,s*o+a);f[(h*e+l)*t.samplesPerPixel]=d}}t.data=f}applyPredictor(t){const e=t.bitsPerSample;switch(t.predictor){case 1:break;case 2:if(8===e)applyHorizontalDifferencing8Bit(t.data,t.width,t.components);else{if(16!==e)throw new Error(`Horizontal differencing is only supported for images with a bit depth of ${e}`);applyHorizontalDifferencing16Bit(t.data,t.width,t.components)}break;default:throw new Error(`invalid predictor: ${t.predictor}`)}}convertAlpha(t){if(t.alpha&&t.associatedAlpha){const{data:e,components:r,maxSampleValue:n}=t;for(let t=0;t<e.length;t+=r){const o=e[t+r-1];for(let i=0;i<r-1;i++)e[t+i]=Math.round(e[t+i]*n/o)}}}}function getDataArray(t,e,r){if(8===e||1===e)return new Uint8Array(t);if(16===e)return new Uint16Array(t);if(32===e&&3===r)return new Float32Array(t);if(64===e&&3===r)return new Float64Array(t);throw unsupported("bit depth / sample format",`${e} / ${r}`)}function unsupported(t,e){return new Error(`Unsupported ${t}: ${e}`)}function checkPages(t){if(t)for(const e of t)if(e<0||!Number.isInteger(e))throw new RangeError(`Index ${e} is invalid. Must be a positive integer.`)}function decodeTIFF(t,e){return new TIFFDecoder(t).decode(e)}function decode$1(t){const e=new IOBuffer(t),r={};e.setBigEndian();if(65496!==e.readUint16())throw new Error("SOI marker not found. Not a valid JPEG file");if(65505===e.readUint16()){e.skip(2);const t=e.readBytes(6);69===t[0]&&120===t[1]&&105===t[2]&&102===t[3]&&0===t[4]&&0===t[5]&&(r.exif=decodeTIFF(new Uint8Array(e.buffer,e.byteOffset+12,e.byteLength-12),{pages:[0],ignoreImageData:!0}))}return r}function getMetadata(t){return{tiff:{fields:t.fields,tags:t.map},exif:t.exif}}function decodeJpeg(t){const e=jpegJs.decode(t,{useTArray:!0,maxMemoryUsageInMB:Number.POSITIVE_INFINITY,maxResolutionInMP:Number.POSITIVE_INFINITY}),r=decode$1(t),n=r.exif?.[0]?getMetadata(r.exif[0]):void 0;return new Image(e.width,e.height,{data:e.data,colorModel:"RGBA",meta:n})}function decodePng(t){const e=decodePng$1(t);let r;const n=e.depth;if(e.palette)return loadPalettePng(e);if(1===n)return new Image(e.width,e.height,{data:decodeBinary(e),colorModel:"GREY"});switch(e.channels){case 1:r="GREY";break;case 2:r="GREYA";break;case 3:r="RGB";break;case 4:r="RGBA";break;default:throw new RangeError(`invalid number of channels: ${e.channels}`)}const o=getResolution(e);return new Image(e.width,e.height,{colorModel:r,bitDepth:n,data:e.data,resolution:o})}function loadPalettePng(t){assert(t.palette);const e=t.width*t.height,r=new Uint8Array(e*t.palette[0].length),n=8/t.depth,o=t.depth<8?n:1,i=Number.parseInt("1".repeat(t.depth),2);let s=0;for(let a=0;a<e;a++){const e=Math.floor(a/o);let l=t.data[e];t.depth<8&&(l=l>>>t.depth*(n-1-a%n)&i);const h=t.palette[l];for(const t of h)r[s++]=t}const a=getResolution(t);return new Image(t.width,t.height,{data:r,colorModel:4===t.palette[0].length?"RGBA":"RGB",resolution:a})}function decodeBinary(t){const e=t.width*t.height,r=new Uint8Array(e),n=t.data,o=t.width%8,i=Math.ceil(t.width/8);let s=0;for(let t=0;t<n.length&&s<e;t++){const a=n[t],l=t%i===0?8-o:0;for(let t=7;t>=l&&s<e;t--){const e=a>>t&1;r[s++]=255*e}}return r}function getResolution(t){return t.resolution?1===t.resolution.unit?{x:t.resolution.x,y:t.resolution.y,unit:"meter"}:{x:t.resolution.x,y:t.resolution.y,unit:"unknown"}:void 0}function decodeTiff(t){return getImageFromIFD(decodeTIFF(t,{pages:[0]})[0])}function getImageFromIFD(t){if(t.data instanceof Float32Array||t.data instanceof Float64Array)throw new Error("Float TIFF data is not supported.");const e=getTiffResolution(t);if(3===t.type){const r=2===t.samplesPerPixel,n=new Uint16Array((r?4:3)*t.width*t.height),o=t.palette;let i=0;if(r)for(let e=0;e<t.data.length;e+=t.samplesPerPixel){const r=o[t.data[e]];n[i++]=r[0],n[i++]=r[1],n[i++]=r[2],n[i++]=Math.round(t.data[e+1]/2**t.bitsPerSample*65535)}else for(let e=0;e<t.data.length;e+=t.samplesPerPixel){const r=o[t.data[e]];n[i++]=r[0],n[i++]=r[1],n[i++]=r[2]}return new Image(t.width,t.height,{data:n,colorModel:r?"RGBA":"RGB",bitDepth:16,meta:getMetadata(t),resolution:e})}return 1===t.type||0===t.type?1!==t.bitsPerSample?new Image(t.width,t.height,{data:t.data,bitDepth:t.bitsPerSample,colorModel:t.alpha?"GREYA":"GREY",meta:getMetadata(t),resolution:e}):new Image(t.width,t.height,{data:t.data.map(t=>255*t),bitDepth:8,colorModel:"GREY",meta:getMetadata(t),resolution:e}):new Image(t.width,t.height,{data:t.data,bitDepth:t.bitsPerSample,colorModel:t.alpha?"RGBA":"RGB",meta:getMetadata(t),resolution:e})}function getTiffResolution(t){if(t.xResolution&&t.yResolution)switch(t.resolutionUnit){case 1:return{x:t.xResolution,y:t.yResolution,unit:"unknown"};case 3:return{x:t.xResolution,y:t.yResolution,unit:"centimeter"};default:return{x:t.xResolution,y:t.yResolution,unit:"inch"}}}function decode(t){const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),r=imageType$1(e);return M$2(r).with({mime:"image/png"},()=>decodePng(e)).with({mime:"image/jpeg"},()=>decodeJpeg(e)).with({mime:"image/tiff"},()=>decodeTiff(e)).with({mime:"image/bmp"},()=>decodeBmp(e)).otherwise(()=>{throw new RangeError(`invalid data format: ${r?.mime}`)})}async function read(t){const e=getNodeApiOrThrow("read");return decode(await e.fs.promises.readFile(t))}function readSync(t){return decode(getNodeApiOrThrow("readSync").fs.readFileSync(t))}function readCanvas(t){const e=t.getContext("2d");assert(e);const r=e.getImageData(0,0,t.width,t.height);return new Image(r.width,r.height,{data:new Uint8Array(r.data.buffer,r.data.byteOffset,r.data.byteLength),colorModel:"RGBA"})}function readImg(t){const e=document.createElement("canvas");e.width=t.width,e.height=t.height;const r=e.getContext("2d");return assert(r),r.drawImage(t,0,0),readCanvas(e)}async function fetchURL(t){const e=await fetch(t),r=await e.arrayBuffer();return decode(new DataView(r))}function hsvToRgb(t){const e=t[0],r=t[1]/255,n=t[2]/255,o=r*n,i=o*(1-Math.abs(e/60%2-1)),s=n-o;let a,l,h;e>=0&&e<60?(a=o,l=i,h=0):e>=60&&e<120?(a=i,l=o,h=0):e>=120&&e<180?(a=0,l=o,h=i):e>=180&&e<240?(a=0,l=i,h=o):e>=240&&e<300?(a=i,l=0,h=o):(a=o,l=0,h=i);const c=new Uint8Array(3);return c[0]=255*(a+s),c[1]=255*(l+s),c[2]=255*(h+s),c}function rgbToNumber(t){return 4278190080+(t[2]<<16)+(t[1]<<8)+t[0]}function getBinaryMap(t){const{nbNegative:e,nbPositive:r,whiteHue:n=120,blackHue:o=0,roiKind:i="bw"}=t,s=new Uint32Array(e+r+1);if("bw"===i||"black"===i)for(let t=-e;t<0;t++){const r=[o,255,255];s[t+e]=rgbToNumber(hsvToRgb(r))}if("bw"===i||"white"===i)for(let t=1;t<=r;t++){const r=[n,255,255];s[t+e]=rgbToNumber(hsvToRgb(r))}return s}function getRainbowMap(t){const{nbNegative:e,nbPositive:r,roiKind:n="bw"}=t,o=new Uint32Array(e+r+1),i=M$2(n).with("bw",()=>360/(e+r)).with("black",()=>360/e).with("white",()=>360/r).exhaustive();let s=0;if("bw"===n||"black"===n)for(let t=-e;t<0;t++){const r=[s,255,255];o[t+e]=rgbToNumber(hsvToRgb(r)),s+=i}if("bw"===n||"white"===n)for(let t=1;t<=r;t++){const r=[s,255,255];o[t+e]=rgbToNumber(hsvToRgb(r)),s+=i}return o}function getSaturationMap(t){const{nbNegative:e,nbPositive:r,roiKind:n="bw",whiteHue:o=0,blackHue:i=240}=t,s=new Uint32Array(e+r+1),a=192/e,l=192/r;let h=0;if("bw"===n||"black"===n)for(let t=-e;t<0;t++){const r=[i,255-h++*a,255];s[t+e]=rgbToNumber(hsvToRgb(r))}if(h=0,"bw"===n||"white"===n)for(let t=1;t<=r;t++){const r=[o,255-h++*l,255];s[t+e]=rgbToNumber(hsvToRgb(r))}return s}function getColorMap(t){const{mode:e="binary"}=t;return t={roiKind:"bw",...t},M$2(e).with("binary",()=>getBinaryMap(t)).with("saturation",()=>getSaturationMap(t)).with("rainbow",()=>getRainbowMap(t)).exhaustive()}const RoisColorMode={BINARY:"binary",SATURATION:"saturation",RAINBOW:"rainbow"};function colorRois(t,e={}){const{roiKind:r="bw",mode:n="binary"}=e,o=t.getMap(),i=new Image(o.width,o.height,{colorModel:"RGBA"}),s=getColorMap({roiKind:r,mode:n,nbNegative:o.nbNegative,nbPositive:o.nbPositive}),a=new Uint32Array(i.getRawImage().data.buffer);for(let t=0;t<i.size;t++)a[t]=s[o.data[t]+o.nbNegative];return i}function getBorderPoints(t,e={}){return t.getMask().getBorderPoints(e)}function getExternalContour(t,e){return t.getMask().getExternalContour(e)}function getMask(t,e={}){const{solidFill:r=!1}=e,n=new Mask(t.width,t.height,{origin:t.origin});for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++)t.getMapValue(t.origin.column+r,t.origin.row+e)===t.id?n.setBit(r,e,1):n.setBit(r,e,0);return r&&n.solidFill({out:n}),n}function getEllipse(t){const e=t.centroid.column,r=t.centroid.row,n=t.relativePoints.map(t=>t.column-e),o=t.relativePoints.map(t=>t.row-r),i=xVariance(n,{unbiased:!1}),s=xVariance(o,{unbiased:!1}),a=xyCovariance({x:n,y:o},{unbiased:!1}),l=new EigenvalueDecomposition([[i,a],[a,s]]),h=l.realEigenvalues,c=l.eigenvectorMatrix;assert(h[0]<=h[1]);let u=Math.sqrt(h[1]),f=Math.sqrt(h[0]);const d=c.getColumn(1),m=c.getColumn(0);let g={column:e+u*d[0],row:r+u*d[1]},p={column:e-u*d[0],row:r-u*d[1]},w={column:e+f*m[0],row:r+f*m[1]},y={column:e-f*m[0],row:r-f*m[1]},b=Math.hypot(g.column-p.column,g.row-p.row),x=Math.hypot(w.column-p.column,w.row-y.row),M=x/2*b/2*Math.PI;if(M!==t.surface){const n=Math.sqrt(t.surface/M);u*=n,f*=n,g={column:e+u*d[0],row:r+u*d[1]},p={column:e-u*d[0],row:r-u*d[1]},w={column:e+f*m[0],row:r+f*m[1]},y={column:e-f*m[0],row:r-f*m[1]},b*=n,x*=n,M*=n**2}return{center:{column:e,row:r},majorAxis:{points:[g,p],length:b,angle:toDegrees(getAngle(g,p))},minorAxis:{points:[w,y],length:x,angle:toDegrees(getAngle(w,y))},surface:M}}class Roi{map;id;origin;width;height;surface;#a;constructor(t,e,r,n,o,i){this.map=t,this.id=e,this.origin=o,this.width=r,this.height=n,this.surface=i,this.#a={}}getMapValue(t,e){return this.map.data[this.map.width*e+t]}getRatio(){return this.width/this.height}getMask(t){return getMask(this,t)}get ped(){return this.perimeter/Math.PI}getBorderPoints(t){return getBorderPoints(this,t)}getExternalContour(t){return getExternalContour(this,t)}get internalIDs(){return this.#l("internalIDs",()=>{const t=[this.id],e=this.map,r=e.data;if(this.height>2)for(let n=0;n<this.width;n++){const o=this.#h(0,n);if(t.includes(r[o])){const n=r[o+e.width];t.includes(n)||this.boxIDs.includes(n)||t.push(n)}}const n=new Array(4);for(let o=1;o<this.width-1;o++)for(let i=1;i<this.height-1;i++){const s=this.#h(i,o);if(t.includes(r[s])){n[0]=r[s-1],n[1]=r[s+1],n[2]=r[s-e.width],n[3]=r[s+e.width];for(let e=0;e<4;e++){const r=n[e];t.includes(r)||this.boxIDs.includes(r)||t.push(r)}}}return t})}get externalBorders(){return this.#l("externalBorders",()=>{const t=this.borders,e=[],r=[],n=this.internalIDs;for(const o of t)if(!n.includes(o.connectedID)){const t={connectedID:o.connectedID,length:o.length};r.push(t.connectedID),e.push(t)}return e})}get perimeterInfo(){return this.#l("perimeterInfo",()=>{const t=this.map,e=t.data;let r=0,n=0,o=0,i=0;const s=new Set(this.externalBorders.map(t=>t.connectedID));for(let a=0;a<this.width;a++)for(let l=0;l<this.height;l++){const h=this.#h(l,a);if(e[h]===this.id){let c=0;switch((0===a||s.has(e[h-1]))&&c++,(a===t.width-1||s.has(e[h+1]))&&c++,(0===l||s.has(e[h-t.width]))&&c++,(l===t.height-1||s.has(e[h+t.width]))&&c++,c){case 1:r++;break;case 2:n++;break;case 3:o++;break;case 4:i++}}}return{one:r,two:n,three:o,four:i}})}get perimeter(){const t=this.perimeterInfo,e=2-Math.sqrt(2);return t.one+2*t.two+3*t.three+4*t.four-e*(t.two+2*t.three+t.four)}get relativePoints(){return this.#l("relativePoints",()=>Array.from(this.points(!1)))}get absolutePoints(){return this.#l("absolutePoints",()=>Array.from(this.points(!0)))}get boxIDs(){return this.#l("boxIDs",()=>{const t=new Set,e=this.map,r=e.data;for(const n of[0,this.height-1])for(let o=0;o<this.width;o++){const i=this.#h(n,o);if(o-this.origin.column>0&&r[i]===this.id&&r[i-1]!==this.id){const e=r[i-1];t.add(e)}if(e.width-o-this.origin.column>1&&r[i]===this.id&&r[i+1]!==this.id){const e=r[i+1];t.add(e)}}for(const n of[0,this.width-1])for(let o=0;o<this.height;o++){const i=this.#h(o,n);if(o-this.origin.row>0&&r[i]===this.id&&r[i-e.width]!==this.id){const n=r[i-e.width];t.add(n)}if(e.height-o-this.origin.row>1&&r[i]===this.id&&r[i+e.width]!==this.id){const n=r[i+e.width];t.add(n)}}return Array.from(t)})}get eqpc(){return 2*Math.sqrt(this.surface/Math.PI)}get ellipse(){return this.#l("ellipse",()=>getEllipse(this))}get holesInfo(){return this.#l("holesInfo",()=>{let t=0;const e=this.map.data;for(let r=1;r<this.width-1;r++)for(let n=1;n<this.height-1;n++){const o=this.#h(n,r);this.internalIDs.includes(e[o])&&e[o]!==this.id&&t++}return{number:this.internalIDs.length-1,surface:t}})}get borders(){return this.#l("borders",()=>{const t=this.map,e=t.data,r=new Set,n=new Map,o=new Set,i=[1,0,-1,0],s=[0,1,0,-1];for(let a=this.origin.column;a<=this.origin.column+this.width;a++)for(let l=this.origin.row;l<=this.origin.row+this.height;l++){if(e[a+l*t.width]===this.id)for(let h=0;h<4;h++){const c=a+i[h],u=l+s[h];if(c>=0&&u>=0&&c<t.width&&u<t.height){const i=c+u*t.width;if(e[i]!==this.id&&!o.has(i)){o.add(i),r.add(e[i]);let t=n.get(e[i]);t?n.set(e[i],++t):n.set(e[i],1)}}}}return Array.from(r).map(t=>({connectedID:t,length:n.get(t)}))})}get fillRatio(){return this.surface/(this.surface+this.holesInfo.surface)}get sphericity(){return 2*Math.sqrt(this.surface*Math.PI)/this.perimeter}get filledSurface(){return this.surface+this.holesInfo.surface}get solidity(){return this.surface/this.convexHull.surface}get convexHull(){return this.#l("convexHull",()=>getConvexHull(this.getMask()))}get mbr(){return this.#l("mbr",()=>getMbr(this.getMask()))}get roundness(){return 4*this.surface/(Math.PI*this.feret.maxDiameter.length**2)}get feret(){return this.#l("feret",()=>getFeret(this.getMask()))}toJSON(){return{id:this.id,origin:this.origin,height:this.height,width:this.width,surface:this.surface,eqpc:this.eqpc,ped:this.ped,feret:this.feret,fillRatio:this.fillRatio,sphericity:this.sphericity,roundness:this.roundness,solidity:this.solidity,perimeter:this.perimeter,convexHull:this.convexHull,mbr:this.mbr,filledSurface:this.filledSurface,centroid:this.centroid}}get centroid(){return this.#l("centroid",()=>{const t=this.map.data;let e=0,r=0;for(let n=0;n<this.width;n++)for(let o=0;o<this.height;o++){t[this.#h(o,n)]===this.id&&(e+=n,r+=o)}return{column:e/this.surface+this.origin.column,row:r/this.surface+this.origin.row}})}#l(t,e){if(void 0===this.#a[t]){const r=e();return this.#a[t]=r,r}return this.#a[t]}#h(t,e){const r=this.map;return(t+this.origin.row)*r.width+e+this.origin.column}*points(t){for(let e=0;e<this.height;e++)for(let r=0;r<this.width;r++){const n=(e+this.origin.row)*this.map.width+r+this.origin.column;this.map.data[n]===this.id&&(t?yield{column:this.origin.column+r,row:this.origin.row+e}:yield{column:r,row:e})}}}function computeRois(t){const e=t.getMap(),r=new Array(e.nbPositive),n=new Array(e.nbNegative);for(let t=0;t<e.nbPositive;t++)r[t]={minRow:e.height,minColumn:e.width,maxRow:-1,maxColumn:-1,surface:0,id:t+1};for(let t=0;t<e.nbNegative;t++)n[t]={borderLengths:[],borderIDs:[],minRow:e.height,minColumn:e.width,maxRow:-1,maxColumn:-1,surface:0,id:-t-1};for(let o=0;o<e.height;o++)for(let i=0;i<e.width;i++){const e=t.getMapValue(i,o);if(0===e)continue;let s;s=e<0?n[-e-1]:r[e-1],s.surface++,o<s.minRow&&(s.minRow=o),o>s.maxRow&&(s.maxRow=o),i<s.minColumn&&(s.minColumn=i),i>s.maxColumn&&(s.maxColumn=i)}t.whiteRois=new Array(e.nbPositive),t.blackRois=new Array(e.nbNegative);for(let r=0;r<e.nbNegative;r++){const o=n[r].maxColumn-n[r].minColumn+1,i=n[r].maxRow-n[r].minRow+1,s={row:n[r].minRow,column:n[r].minColumn},a=n[r].id,l=n[r].surface,h=new Roi(e,a,o,i,s,l);t.blackRois[r]=h}for(let n=0;n<e.nbPositive;n++){const o=r[n].maxColumn-r[n].minColumn+1,i=r[n].maxRow-r[n].minRow+1,s={row:r[n].minRow,column:r[n].minColumn},a=r[n].id,l=r[n].surface,h=new Roi(e,a,o,i,s,l);t.whiteRois[n]=h}}const RoiKind={BLACK:"black",WHITE:"white",BW:"bw"};function getRois(t,e={}){const{minSurface:r=0,maxSurface:n=Number.MAX_SAFE_INTEGER,kind:o="white"}=e;0===t.blackRois.length&&0===t.whiteRois.length&&computeRois(t);return M$2(o).with("black",()=>t.blackRois).with("white",()=>t.whiteRois).with("bw",()=>[...t.whiteRois,...t.blackRois]).exhaustive().filter(t=>t.surface>=r&&t.surface<=n)}class RoiMapManager{map;whiteRois;blackRois;constructor(t){this.map=t,this.whiteRois=[],this.blackRois=[]}getMap(){return this.map}getMapValue(t,e){return this.map.data[this.map.width*e+t]}getMapMatrix(){return Matrix.from1DArray(this.map.height,this.map.width,this.map.data).to2DArray()}getRois(t={}){return getRois(this,t)}getRoiById(t){const e=this.getRois({kind:"bw"}).find(e=>e.id===t);if(!e)throw new Error(`invalid ID: ${t}`);return e}static fromMask(t,e={}){return fromMask(t,e)}}function fromMask(t,e={}){const{allowCorners:r=!1}=e,n=65535,o=-2147483647,i=new Int32Array(t.size);let s=0,a=0;const l=new Uint16Array(65536),h=new Uint16Array(65536);for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)0===i[r*t.width+e]&&c(e,r);function c(e,c){let u=0,f=0;const d=t.getBit(e,c),m=d?++s:--a;for(assert(s<=2147483647&&a>=o,"too many regions of interest"),l[0]=e,h[0]=c;u<=f;){const e=l[u&n],s=h[u&n];i[s*t.width+e]=m,e>0&&0===i[s*t.width+e-1]&&t.getBit(e-1,s)===d&&(f++,l[f&n]=e-1,h[f&n]=s,i[s*t.width+e-1]=o),s>0&&0===i[(s-1)*t.width+e]&&t.getBit(e,s-1)===d&&(f++,l[f&n]=e,h[f&n]=s-1,i[(s-1)*t.width+e]=o),e<t.width-1&&0===i[s*t.width+e+1]&&t.getBit(e+1,s)===d&&(f++,l[f&n]=e+1,h[f&n]=s,i[s*t.width+e+1]=o),s<t.height-1&&0===i[(s+1)*t.width+e]&&t.getBit(e,s+1)===d&&(f++,l[f&n]=e,h[f&n]=s+1,i[(s+1)*t.width+e]=o),r&&(e>0&&s>0&&0===i[(s-1)*t.width+e-1]&&t.getBit(e-1,s-1)===d&&(f++,l[f&n]=e-1,h[f&n]=s-1,i[(s-1)*t.width+e-1]=o),e<t.width-1&&s>0&&0===i[(s-1)*t.width+e+1]&&t.getBit(e+1,s-1)===d&&(f++,l[f&n]=e+1,h[f&n]=s-1,i[(s-1)*t.width+e+1]=o),e>0&&s<t.height-1&&0===i[(s+1)*t.width+e-1]&&t.getBit(e-1,s+1)===d&&(f++,l[f&n]=e-1,h[f&n]=s+1,i[(s+1)*t.width+e-1]=o),e<t.width-1&&s<t.height-1&&0===i[(s+1)*t.width+e+1]&&t.getBit(e+1,s+1)===d&&(f++,l[f&n]=e+1,h[f&n]=s+1,i[(s+1)*t.width+e+1]=o)),u++,assert(f-u<=n,"fromMask can not finish, the array to manage internal data is not big enough.You could improve mask by changing MAX_ARRAY")}}return new RoiMapManager({width:t.width,height:t.height,data:i,nbNegative:Math.abs(a),nbPositive:s})}function commonjsRequire(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var priorityQueue={exports:{}};!function(t,e){t.exports=function t(e,r,n){function o(s,a){if(!r[s]){if(!e[s]){var l="function"==typeof commonjsRequire&&commonjsRequire;if(!a&&l)return l(s,!0);if(i)return i(s,!0);var h=new Error("Cannot find module '"+s+"'");throw h.code="MODULE_NOT_FOUND",h}var c=r[s]={exports:{}};e[s][0].call(c.exports,function(t){var r=e[s][1][t];return o(r||t)},c,c.exports,t,e,r,n)}return r[s].exports}for(var i="function"==typeof commonjsRequire&&commonjsRequire,s=0;s<n.length;s++)o(n[s]);return o}({1:[function(t,e,r){var n,o,i,s,a,l=function(t,e){for(var r in e)h.call(e,r)&&(t[r]=e[r]);function n(){this.constructor=t}return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},h={}.hasOwnProperty;n=t("./PriorityQueue/AbstractPriorityQueue"),o=t("./PriorityQueue/ArrayStrategy"),s=t("./PriorityQueue/BinaryHeapStrategy"),i=t("./PriorityQueue/BHeapStrategy"),a=function(t){function e(t){t||(t={}),t.strategy||(t.strategy=s),t.comparator||(t.comparator=function(t,e){return(t||0)-(e||0)}),e.__super__.constructor.call(this,t)}return l(e,t),e}(n),a.ArrayStrategy=o,a.BinaryHeapStrategy=s,a.BHeapStrategy=i,e.exports=a},{"./PriorityQueue/AbstractPriorityQueue":2,"./PriorityQueue/ArrayStrategy":3,"./PriorityQueue/BHeapStrategy":4,"./PriorityQueue/BinaryHeapStrategy":5}],2:[function(t,e,r){e.exports=function(){function t(t){var e;if(null==(null!=t?t.strategy:void 0))throw"Must pass options.strategy, a strategy";if(null==(null!=t?t.comparator:void 0))throw"Must pass options.comparator, a comparator";this.priv=new t.strategy(t),this.length=(null!=t&&null!=(e=t.initialValues)?e.length:void 0)||0}return t.prototype.queue=function(t){this.length++,this.priv.queue(t)},t.prototype.dequeue=function(t){if(!this.length)throw"Empty queue";return this.length--,this.priv.dequeue()},t.prototype.peek=function(t){if(!this.length)throw"Empty queue";return this.priv.peek()},t.prototype.clear=function(){return this.length=0,this.priv.clear()},t}()},{}],3:[function(t,e,r){var n;n=function(t,e,r){var n,o,i;for(o=0,n=t.length;o<n;)r(t[i=o+n>>>1],e)>=0?o=i+1:n=i;return o},e.exports=function(){function t(t){var e;this.options=t,this.comparator=this.options.comparator,this.data=(null!=(e=this.options.initialValues)?e.slice(0):void 0)||[],this.data.sort(this.comparator).reverse()}return t.prototype.queue=function(t){var e;e=n(this.data,t,this.comparator),this.data.splice(e,0,t)},t.prototype.dequeue=function(){return this.data.pop()},t.prototype.peek=function(){return this.data[this.data.length-1]},t.prototype.clear=function(){this.data.length=0},t}()},{}],4:[function(t,e,r){e.exports=function(){function t(t){var e,r,n,o,i,s,a,l;for(this.comparator=(null!=t?t.comparator:void 0)||function(t,e){return t-e},this.pageSize=(null!=t?t.pageSize:void 0)||512,this.length=0,a=0;1<<a<this.pageSize;)a+=1;if(1<<a!==this.pageSize)throw"pageSize must be a power of two";for(this._shift=a,this._emptyMemoryPageTemplate=e=[],r=0,i=this.pageSize;0<=i?r<i:r>i;0<=i?++r:--r)e.push(null);if(this._memory=[],this._mask=this.pageSize-1,t.initialValues)for(n=0,o=(s=t.initialValues).length;n<o;n++)l=s[n],this.queue(l)}return t.prototype.queue=function(t){this.length+=1,this._write(this.length,t),this._bubbleUp(this.length,t)},t.prototype.dequeue=function(){var t,e;return t=this._read(1),e=this._read(this.length),this.length-=1,this.length>0&&(this._write(1,e),this._bubbleDown(1,e)),t},t.prototype.peek=function(){return this._read(1)},t.prototype.clear=function(){this.length=0,this._memory.length=0},t.prototype._write=function(t,e){var r;for(r=t>>this._shift;r>=this._memory.length;)this._memory.push(this._emptyMemoryPageTemplate.slice(0));return this._memory[r][t&this._mask]=e},t.prototype._read=function(t){return this._memory[t>>this._shift][t&this._mask]},t.prototype._bubbleUp=function(t,e){var r,n,o,i;for(r=this.comparator;t>1&&(n=t&this._mask,t<this.pageSize||n>3?o=t&~this._mask|n>>1:n<2?(o=t-this.pageSize>>this._shift,o+=o&~(this._mask>>1),o|=this.pageSize>>1):o=t-2,!(r(i=this._read(o),e)<0));)this._write(o,e),this._write(t,i),t=o},t.prototype._bubbleDown=function(t,e){var r,n,o,i,s;for(s=this.comparator;t<this.length;)if(t>this._mask&&!(t&this._mask-1)?r=n=t+2:t&this.pageSize>>1?(r=(t&~this._mask)>>1,n=1+(r=1+(r|=t&this._mask>>1)<<this._shift)):n=(r=t+(t&this._mask))+1,r!==n&&n<=this.length)if(o=this._read(r),i=this._read(n),s(o,e)<0&&s(o,i)<=0)this._write(r,e),this._write(t,o),t=r;else{if(!(s(i,e)<0))break;this._write(n,e),this._write(t,i),t=n}else{if(!(r<=this.length))break;if(!(s(o=this._read(r),e)<0))break;this._write(r,e),this._write(t,o),t=r}},t}()},{}],5:[function(t,e,r){e.exports=function(){function t(t){var e;this.comparator=(null!=t?t.comparator:void 0)||function(t,e){return t-e},this.length=0,this.data=(null!=(e=t.initialValues)?e.slice(0):void 0)||[],this._heapify()}return t.prototype._heapify=function(){var t,e,r;if(this.data.length>0)for(t=e=1,r=this.data.length;1<=r?e<r:e>r;t=1<=r?++e:--e)this._bubbleUp(t)},t.prototype.queue=function(t){this.data.push(t),this._bubbleUp(this.data.length-1)},t.prototype.dequeue=function(){var t,e;return e=this.data[0],t=this.data.pop(),this.data.length>0&&(this.data[0]=t,this._bubbleDown(0)),e},t.prototype.peek=function(){return this.data[0]},t.prototype.clear=function(){this.length=0,this.data.length=0},t.prototype._bubbleUp=function(t){for(var e,r;t>0&&(e=t-1>>>1,this.comparator(this.data[t],this.data[e])<0);)r=this.data[e],this.data[e]=this.data[t],this.data[t]=r,t=e},t.prototype._bubbleDown=function(t){var e,r,n,o,i;for(e=this.data.length-1;o=1+(r=1+(t<<1)),n=t,r<=e&&this.comparator(this.data[r],this.data[n])<0&&(n=r),o<=e&&this.comparator(this.data[o],this.data[n])<0&&(n=o),n!==t;)i=this.data[n],this.data[n]=this.data[t],this.data[t]=i,t=n},t}()},{}]},{},[1])(1)}(priorityQueue);var priorityQueueExports=priorityQueue.exports,PriorityQueue=getDefaultExportFromCjs(priorityQueueExports);function waterShed(t,e){let{points:r}=e;const{mask:n,threshold:o=1}=e,i=t;checkProcessable$1(t,{bitDepth:[8,16],components:1});const s=o*t.maxValue;r||(r=getExtrema(t,{kind:"minimum",mask:n}));const a=new Int32Array(i.size),l=i.width,h=i.height,c=new PriorityQueue({comparator:(t,e)=>t.intensity-e.intensity,strategy:PriorityQueue.BinaryHeapStrategy});for(let t=0;t<r.length;t++){const e=r[t].column+r[t].row*l;a[e]=-t-1;const n=i.getValueByIndex(e,0);n<=s&&c.queue({column:r[t].column,row:r[t].row,intensity:n})}const u=[1,0,-1,0,1,1,-1,-1],f=[0,1,0,-1,1,-1,1,-1];for(;c.length>0;){const t=c.dequeue(),e=t.column+t.row*l;for(let r=0;r<4;r++){const o=t.column+u[r],d=t.row+f[r];if(o>=0&&d>=0&&o<l&&d<h){const h=o+d*l;if(!n||0===n.getBitByIndex(h)){const n=i.getValueByIndex(h,0);n<=s&&0===a[h]&&(a[h]=a[e],c.queue({column:t.column+u[r],row:t.row+f[r],intensity:n}))}}}}const d=r.length;return new RoiMapManager({data:a,nbPositive:0,nbNegative:d,width:t.width,height:t.height})}function checkProcessable(t,e={}){const{sameDimensions:r=!1,alpha:n}=e;let{bitDepth:o}=e;if(r){const e=t.getImage(0).width,r=t.getImage(0).height;for(let n=1;n<t.size;n++){const o=t.getImage(n);if(o.width!==e||o.height!==r)throw new RangeError("images must all have same dimensions to apply this algorithm")}}if(void 0!==n&&n!==t.alpha)throw new RangeError(`stack images ${n?"should":"should not"} have an alpha channel to apply this algorithm`);if(o&&(Array.isArray(o)||(o=[o]),!o.includes(t.bitDepth)))throw new RangeError(`image bitDepth must be ${format(o)} to apply this algorithm`)}function histogram(t,e){checkProcessable(t,{bitDepth:[8,16]});const{slots:r=2**t.bitDepth,channel:n=0}=e,o=new Uint32Array(r);for(let e=0;e<t.size;e++){const i=t.getImage(e).histogram({channel:n,slots:r});for(const[t,e]of i.entries())o[t]+=e}return o}function maxImage(t){checkProcessable(t,{sameDimensions:!0});const e=Image.createFrom(t.getImage(0)),r=e.channels;for(let n=0;n<t.size;n++)for(let o=0;o<e.size;o++)for(let i=0;i<r;i++)e.setValueByIndex(o,i,Math.max(e.getValueByIndex(o,i),t.getValueByIndex(n,o,i)));return e}function meanImage(t){checkProcessable(t,{sameDimensions:!0,bitDepth:[8,16]});const e=t.getImage(0),r=e.size*t.channels,n=new Uint32Array(r).fill(0);for(let r=0;r<t.size;r++)for(let o=0;o<e.size;o++)for(let e=0;e<t.channels;e++)n[o*t.channels+e]+=t.getValueByIndex(r,o,e);const o=Image.createFrom(e);for(let r=0;r<e.size;r++)for(let e=0;e<t.channels;e++){const i=r*t.channels+e;o.setValueByIndex(r,e,n[i]/t.size)}return o}function medianImage(t){checkProcessable(t,{sameDimensions:!0,bitDepth:[8,16]});const e=t.getImage(0),r=Image.createFrom(e);for(let n=0;n<e.size;n++)for(let e=0;e<t.channels;e++){const o=new Array(t.size);for(let r=0;r<t.size;r++)o[r]=t.getValueByIndex(r,n,e);r.setValueByIndex(n,e,quickMedian(o))}return r}function decodeStackFromApng(t){const e=decodeApng(t),r=[];let n;switch(e.channels){case 1:n=e.palette?3===e.palette[0].length?"RGB":"RGBA":"GREY";break;case 2:n="GREYA";break;case 3:n="RGB";break;default:n="RGBA"}if(e.palette)for(const t of e.frames)r.push(new Image(e.width,e.height,{data:convertIndexedData(t.data,e.palette),colorModel:n}));else for(const t of e.frames)r.push(new Image(e.width,e.height,{data:t.data,colorModel:n}));return new Stack(r)}function convertIndexedData(t,e){const r=new Uint8Array(t.length*e[0].length);for(let n=0;n<t.length;n++)for(let o=0;o<e[0].length;o++)r[n*e[0].length+o]=e[t[n]][o];return r}function decodeStackFromTiff(t){const e=decodeTIFF(t),r=[];for(const t of e)r.push(getImageFromIFD(t));return new Stack(r)}function decodeStack(t){const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),r=imageType$1(e);return M$2(r).with({mime:"image/tiff"},()=>decodeStackFromTiff(e)).with({mime:"image/png"},()=>decodeStackFromApng(e)).otherwise(()=>{throw new RangeError(`invalid data format: ${r?.mime}`)})}function minImage(t){checkProcessable(t,{sameDimensions:!0});const e=Image.createFrom(t.getImage(0));e.fill(e.maxValue);const r=e.channels;for(let n=0;n<t.size;n++)for(let o=0;o<e.size;o++)for(let i=0;i<r;i++)e.setValueByIndex(o,i,Math.min(e.getValueByIndex(o,i),t.getValueByIndex(n,o,i)));return e}function sum(t){if(checkProcessable(t,{sameDimensions:!0,bitDepth:8}),t.size>257)throw new Error("Maximal valid stack size is 257");const e=t.getImage(0),r=e.size*t.channels,n=new Uint16Array(r).fill(0);for(let r=0;r<t.size;r++)for(let o=0;o<e.size;o++)for(let e=0;e<t.channels;e++)n[o*t.channels+e]+=t.getValueByIndex(r,o,e);return new Image(e.width,e.height,{data:n,colorModel:t.colorModel,bitDepth:16})}function checkImagesValid(t){const e=t[0].colorModel,r=t[0].bitDepth;for(const n of t)if(n.colorModel!==e||n.bitDepth!==r)throw new RangeError("images must all have the same bit depth and color model")}function verifySameDimensions(t){const e=t[0].width,r=t[0].height;for(const n of t)if(n.width!==e||n.height!==r)return!1;return!0}class Stack{images;size;alpha;colorModel;bitDepth;sameDimensions;channels;constructor(t){checkImagesValid(t),this.images=t,this.size=t.length,this.alpha=t[0].alpha,this.colorModel=t[0].colorModel,this.channels=t[0].channels,this.bitDepth=t[0].bitDepth,this.sameDimensions=verifySameDimensions(t)}*[Symbol.iterator](){for(const t of this.images)yield t}clone(){return new Stack(this.images.map(t=>t.clone()))}getImages(){return this.images}getImage(t){return this.images[t]}getValue(t,e,r,n){return this.images[t].getValue(e,r,n)}getValueByIndex(t,e,r){return this.images[t].getValueByIndex(e,r)}minImage(){return minImage(this)}maxImage(){return maxImage(this)}medianImage(){return medianImage(this)}meanImage(){return meanImage(this)}sum(){return sum(this)}histogram(t={}){return histogram(this,t)}map(t){return new Stack(this.images.map(t))}filter(t){return new Stack(this.images.filter(t))}}const channelLabels={GREY:["Grey"],GREYA:["Grey","Alpha"],RGB:["Red","Green","Blue"],RGBA:["Red","Green","Blue","Alpha"],BINARY:["Mask"]};function drawText(t,e,r){if(Array.isArray(e)&&0===e.length)throw new Error("At least one text element must be provided");const n=getOutputImage(t,r,{clone:!0}),o=r?.font??"12px Helvetica",i=r?.fontColor??[255,255,255,255],s=getCanvasContext(t.width,t.height);if(Array.isArray(e))for(const r of e)drawTextElement(t,s,r,o,i);else drawTextElement(t,s,e,o,i);return layerCanvas(n.getRawImage().data,s.getImageData(0,0,t.width,t.height).data,t.channels,t.bitDepth),n}function drawTextElement(t,e,r,n,o){e.font=r.font??n;const i=r.fontColor??o;validateValues(i,t);const s=i[3]?i[3]/255:1,a=[i[0]??0,i[1]??0,i[2]??0,s];e.fillStyle=`rgba(${a.join(",")})`,e.fillText(String(r.content),r.position.column,r.position.row)}function layerCanvas(t,e,r,n){const o=CHANNEL_CONFIGS[r]||CHANNEL_CONFIGS[1],i=e.length>>>2,s=n-8;let a=0,l=0;for(let n=0;n<i;n++){const n=e[l+3]/255;if(0===n){a+=r,l+=4;continue}const i=1-n;for(const r of o.channelOffsets){const o=a+r;t[o]=Math.round(e[l+r]*n+t[o]*i)<<s}if(o.hasAlpha){const e=a+o.alphaOffset,r=n+(t[e]>>>s)/255*(1-n);t[e]=Math.round(255*r)<<s}a+=r,l+=4}}const CHANNEL_CONFIGS={1:{channelOffsets:[0],hasAlpha:!1,alphaOffset:void 0},2:{channelOffsets:[0],hasAlpha:!0,alphaOffset:1},3:{channelOffsets:[0,1,2],hasAlpha:!1,alphaOffset:void 0},4:{channelOffsets:[0,1,2],hasAlpha:!0,alphaOffset:3}};exports.BorderType=BorderType,exports.DerivativeFilter=DerivativeFilter,exports.GreyAlgorithm=GreyAlgorithm,exports.Image=Image,exports.ImageColorModel=ImageColorModel,exports.ImageCoordinates=ImageCoordinates,exports.ImageFormat=ImageFormat,exports.InterpolationType=InterpolationType,exports.Mask=Mask,exports.Montage=Montage,exports.MontageDisposition=MontageDisposition,exports.Roi=Roi,exports.RoiKind=RoiKind,exports.RoiMapManager=RoiMapManager,exports.RoisColorMode=RoisColorMode,exports.Stack=Stack,exports.ThresholdAlgorithm=ThresholdAlgorithm,exports.add=add,exports.alignMinDifference=alignMinDifference,exports.and=and,exports.blur=blur,exports.bottomHat=bottomHat,exports.bruteForceOneMatch=bruteForceOneMatch,exports.cannyEdgeDetector=cannyEdgeDetector,exports.channelLabels=channelLabels,exports.clearBorder=clearBorder,exports.close=close,exports.colorModels=colorModels,exports.colorRois=colorRois,exports.computeConvolutionValue=computeConvolutionValue,exports.computeMse=computeMse,exports.computePsnr=computePsnr,exports.computeRmse=computeRmse,exports.computeRois=computeRois,exports.computeThreshold=computeThreshold,exports.convertBinaryToGrey=convertBinaryToGrey,exports.convertBinaryToRgb=convertBinaryToRgb,exports.convertBitDepth=convertBitDepth,exports.convertColor=convertColor,exports.copyAlpha=copyAlpha,exports.copyTo=copyTo,exports.correctBackground=correctBackground,exports.correctColor=correctColor,exports.crop=crop,exports.cropAlpha=cropAlpha,exports.cropRectangle=cropRectangle,exports.crosscheck=crosscheck,exports.decode=decode,exports.decodeJpeg=decodeJpeg,exports.decodePng=decodePng,exports.decodeStack=decodeStack,exports.decodeTiff=decodeTiff,exports.defaultPng=defaultPng,exports.derivativeFilter=derivativeFilter,exports.dilate=dilate,exports.directConvolution=directConvolution,exports.divide=divide,exports.drawCircleOnImage=drawCircleOnImage,exports.drawKeypoints=drawKeypoints,exports.drawLineOnImage=drawLineOnImage,exports.drawLineOnMask=drawLineOnMask,exports.drawMarker=drawMarker,exports.drawMarkers=drawMarkers,exports.drawMatches=drawMatches,exports.drawPoints=drawPoints,exports.drawPolygonOnImage=drawPolygonOnImage,exports.drawPolygonOnMask=drawPolygonOnMask,exports.drawPolylineOnImage=drawPolylineOnImage,exports.drawPolylineOnMask=drawPolylineOnMask,exports.drawRectangle=drawRectangle,exports.drawText=drawText,exports.encode=encode,exports.encodeDataURL=encodeDataURL,exports.encodeJpeg=encodeJpeg,exports.encodePng=encodePng,exports.erode=erode,exports.extendBorders=extendBorders,exports.extract=extract,exports.fetchURL=fetchURL,exports.flip=flip,exports.floodFill=floodFill,exports.fromMask=fromMask,exports.gaussianBlur=gaussianBlur,exports.getAffineTransform=getAffineTransform,exports.getBestKeypointsInRadius=getBestKeypointsInRadius,exports.getBorderPoints=getBorderPoints,exports.getBriefDescriptors=getBriefDescriptors,exports.getCrosscheckMatches=getCrosscheckMatches,exports.getDirection=getDirection,exports.getExtrema=getExtrema,exports.getFastKeypoints=getFastKeypoints,exports.getHarrisScore=getHarrisScore,exports.getImageFromIFD=getImageFromIFD,exports.getIntensityCentroid=getIntensityCentroid,exports.getMask=getMask,exports.getOrientedFastKeypoints=getOrientedFastKeypoints,exports.getPerspectiveWarp=getPerspectiveWarp,exports.getRegressionVariables=getRegressionVariables,exports.getRois=getRois,exports.getShiTomasiScore=getShiTomasiScore,exports.gradientFilter=gradientFilter,exports.grey=grey,exports.histogram=histogram$1,exports.hypotenuse=hypotenuse,exports.increaseContrast=increaseContrast,exports.invert=invert,exports.level=level,exports.mean=mean,exports.median=median,exports.medianFilter=medianFilter,exports.merge=merge,exports.morphologicalGradient=morphologicalGradient,exports.multiply=multiply,exports.open=open,exports.or=or,exports.order4Points=order4Points,exports.overlapImages=overlapImages,exports.paintMaskOnImage=paintMaskOnImage,exports.paintMaskOnMask=paintMaskOnMask,exports.pixelate=pixelate,exports.rawDirectConvolution=rawDirectConvolution,exports.read=read,exports.readCanvas=readCanvas,exports.readImg=readImg,exports.readSync=readSync,exports.removeClosePoints=removeClosePoints,exports.resize=resize,exports.rotate=rotate,exports.separableConvolution=separableConvolution,exports.solidFill=solidFill,exports.split=split,exports.subtract=subtract,exports.threshold=threshold,exports.topHat=topHat,exports.transform=transform,exports.transformRotate=transformRotate,exports.variance=variance,exports.waterShed=waterShed,exports.write=write,exports.writeCanvas=writeCanvas,exports.writeSync=writeSync});
1
+ /* image-js 1.7.0 | MIT | https://github.com/image-js/image-js#readme */
2
+ !function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).IJS={})}(this,function(exports){"use strict";var _documentCurrentScript="undefined"!=typeof document?document.currentScript:null;function getDefaultExportFromCjs(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function getAugmentedNamespace(t){if(Object.prototype.hasOwnProperty.call(t,"__esModule"))return t;var e=t.default;if("function"==typeof e){var r=function t(){var r=!1;try{r=this instanceof t}catch{}return r?Reflect.construct(e,arguments,this.constructor):e.apply(this,arguments)};r.prototype=e.prototype}else r={};return Object.defineProperty(r,"__esModule",{value:!0}),Object.keys(t).forEach(function(e){var n=Object.getOwnPropertyDescriptor(t,e);Object.defineProperty(r,e,n.get?n:{enumerable:!0,get:function(){return t[e]}})}),r}var matrix={};const toString=Object.prototype.toString;function isAnyArray$1(t){const e=toString.call(t);return e.endsWith("Array]")&&!e.includes("Big")}var libEsm=Object.freeze({__proto__:null,isAnyArray:isAnyArray$1}),require$$0=getAugmentedNamespace(libEsm);function max$2(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,n=void 0===r?0:r,o=e.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var s=t[n],a=n+1;a<i;a++)t[a]>s&&(s=t[a]);return s}function min$1(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");var r=e.fromIndex,n=void 0===r?0:r,o=e.toIndex,i=void 0===o?t.length:o;if(n<0||n>=t.length||!Number.isInteger(n))throw new Error("fromIndex must be a positive integer smaller than length");if(i<=n||i>t.length||!Number.isInteger(i))throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");for(var s=t[n],a=n+1;a<i;a++)t[a]<s&&(s=t[a]);return s}function rescale$1(t){var e,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if(void 0!==r.output){if(!isAnyArray$1(r.output))throw new TypeError("output option must be an array if specified");e=r.output}else e=new Array(t.length);var n=min$1(t),o=max$2(t);if(n===o)throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");var i=r.min,s=void 0===i?r.autoMinMax?n:0:i,a=r.max,h=void 0===a?r.autoMinMax?o:1:a;if(s>=h)throw new RangeError("min option must be smaller than max option");for(var l=(h-s)/(o-n),c=0;c<t.length;c++)e[c]=(t[c]-n)*l+s;return e}var libEs6=Object.freeze({__proto__:null,default:rescale$1}),require$$1=getAugmentedNamespace(libEs6);Object.defineProperty(matrix,"__esModule",{value:!0});var isAnyArray=require$$0,rescale=require$$1;const indent=" ".repeat(2),indentData=" ".repeat(4);function inspectMatrix(){return inspectMatrixWithOptions(this)}function inspectMatrixWithOptions(t,e={}){const{maxRows:r=15,maxColumns:n=10,maxNumSize:o=8,padMinus:i="auto"}=e;return`${t.constructor.name} {\n${indent}[\n${indentData}${inspectData(t,r,n,o,i)}\n${indent}]\n${indent}rows: ${t.rows}\n${indent}columns: ${t.columns}\n}`}function inspectData(t,e,r,n,o){const{rows:i,columns:s}=t,a=Math.min(i,e),h=Math.min(s,r),l=[];if("auto"===o){o=!1;t:for(let e=0;e<a;e++)for(let r=0;r<h;r++)if(t.get(e,r)<0){o=!0;break t}}for(let e=0;e<a;e++){let r=[];for(let i=0;i<h;i++)r.push(formatNumber(t.get(e,i),n,o));l.push(`${r.join(" ")}`)}return h!==s&&(l[l.length-1]+=` ... ${s-r} more columns`),a!==i&&l.push(`... ${i-e} more rows`),l.join(`\n${indentData}`)}function formatNumber(t,e,r){return(t>=0&&r?` ${formatNumber2(t,e-1)}`:formatNumber2(t,e)).padEnd(e)}function formatNumber2(t,e){let r=t.toString();if(r.length<=e)return r;let n=t.toFixed(e);if(n.length>e&&(n=t.toFixed(Math.max(0,e-(n.length-e)))),n.length<=e&&!n.startsWith("0.000")&&!n.startsWith("-0.000"))return n;let o=t.toExponential(e);return o.length>e&&(o=t.toExponential(Math.max(0,e-(o.length-e)))),o.slice(0)}function installMathOperations(t,e){t.prototype.add=function(t){return"number"==typeof t?this.addS(t):this.addM(t)},t.prototype.addS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t);return this},t.prototype.addM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t.get(e,r));return this},t.add=function(t,r){return new e(t).add(r)},t.prototype.sub=function(t){return"number"==typeof t?this.subS(t):this.subM(t)},t.prototype.subS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t);return this},t.prototype.subM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t.get(e,r));return this},t.sub=function(t,r){return new e(t).sub(r)},t.prototype.subtract=t.prototype.sub,t.prototype.subtractS=t.prototype.subS,t.prototype.subtractM=t.prototype.subM,t.subtract=t.sub,t.prototype.mul=function(t){return"number"==typeof t?this.mulS(t):this.mulM(t)},t.prototype.mulS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t);return this},t.prototype.mulM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t.get(e,r));return this},t.mul=function(t,r){return new e(t).mul(r)},t.prototype.multiply=t.prototype.mul,t.prototype.multiplyS=t.prototype.mulS,t.prototype.multiplyM=t.prototype.mulM,t.multiply=t.mul,t.prototype.div=function(t){return"number"==typeof t?this.divS(t):this.divM(t)},t.prototype.divS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t);return this},t.prototype.divM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t.get(e,r));return this},t.div=function(t,r){return new e(t).div(r)},t.prototype.divide=t.prototype.div,t.prototype.divideS=t.prototype.divS,t.prototype.divideM=t.prototype.divM,t.divide=t.div,t.prototype.mod=function(t){return"number"==typeof t?this.modS(t):this.modM(t)},t.prototype.modS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t);return this},t.prototype.modM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)%t.get(e,r));return this},t.mod=function(t,r){return new e(t).mod(r)},t.prototype.modulus=t.prototype.mod,t.prototype.modulusS=t.prototype.modS,t.prototype.modulusM=t.prototype.modM,t.modulus=t.mod,t.prototype.and=function(t){return"number"==typeof t?this.andS(t):this.andM(t)},t.prototype.andS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t);return this},t.prototype.andM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)&t.get(e,r));return this},t.and=function(t,r){return new e(t).and(r)},t.prototype.or=function(t){return"number"==typeof t?this.orS(t):this.orM(t)},t.prototype.orS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t);return this},t.prototype.orM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)|t.get(e,r));return this},t.or=function(t,r){return new e(t).or(r)},t.prototype.xor=function(t){return"number"==typeof t?this.xorS(t):this.xorM(t)},t.prototype.xorS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t);return this},t.prototype.xorM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)^t.get(e,r));return this},t.xor=function(t,r){return new e(t).xor(r)},t.prototype.leftShift=function(t){return"number"==typeof t?this.leftShiftS(t):this.leftShiftM(t)},t.prototype.leftShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t);return this},t.prototype.leftShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)<<t.get(e,r));return this},t.leftShift=function(t,r){return new e(t).leftShift(r)},t.prototype.signPropagatingRightShift=function(t){return"number"==typeof t?this.signPropagatingRightShiftS(t):this.signPropagatingRightShiftM(t)},t.prototype.signPropagatingRightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t);return this},t.prototype.signPropagatingRightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>t.get(e,r));return this},t.signPropagatingRightShift=function(t,r){return new e(t).signPropagatingRightShift(r)},t.prototype.rightShift=function(t){return"number"==typeof t?this.rightShiftS(t):this.rightShiftM(t)},t.prototype.rightShiftS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t);return this},t.prototype.rightShiftM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)>>>t.get(e,r));return this},t.rightShift=function(t,r){return new e(t).rightShift(r)},t.prototype.zeroFillRightShift=t.prototype.rightShift,t.prototype.zeroFillRightShiftS=t.prototype.rightShiftS,t.prototype.zeroFillRightShiftM=t.prototype.rightShiftM,t.zeroFillRightShift=t.rightShift,t.prototype.not=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,~this.get(t,e));return this},t.not=function(t){return new e(t).not()},t.prototype.abs=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.abs(this.get(t,e)));return this},t.abs=function(t){return new e(t).abs()},t.prototype.acos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acos(this.get(t,e)));return this},t.acos=function(t){return new e(t).acos()},t.prototype.acosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.acosh(this.get(t,e)));return this},t.acosh=function(t){return new e(t).acosh()},t.prototype.asin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asin(this.get(t,e)));return this},t.asin=function(t){return new e(t).asin()},t.prototype.asinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.asinh(this.get(t,e)));return this},t.asinh=function(t){return new e(t).asinh()},t.prototype.atan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atan(this.get(t,e)));return this},t.atan=function(t){return new e(t).atan()},t.prototype.atanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.atanh(this.get(t,e)));return this},t.atanh=function(t){return new e(t).atanh()},t.prototype.cbrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cbrt(this.get(t,e)));return this},t.cbrt=function(t){return new e(t).cbrt()},t.prototype.ceil=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.ceil(this.get(t,e)));return this},t.ceil=function(t){return new e(t).ceil()},t.prototype.clz32=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.clz32(this.get(t,e)));return this},t.clz32=function(t){return new e(t).clz32()},t.prototype.cos=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cos(this.get(t,e)));return this},t.cos=function(t){return new e(t).cos()},t.prototype.cosh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.cosh(this.get(t,e)));return this},t.cosh=function(t){return new e(t).cosh()},t.prototype.exp=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.exp(this.get(t,e)));return this},t.exp=function(t){return new e(t).exp()},t.prototype.expm1=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.expm1(this.get(t,e)));return this},t.expm1=function(t){return new e(t).expm1()},t.prototype.floor=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.floor(this.get(t,e)));return this},t.floor=function(t){return new e(t).floor()},t.prototype.fround=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.fround(this.get(t,e)));return this},t.fround=function(t){return new e(t).fround()},t.prototype.log=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log(this.get(t,e)));return this},t.log=function(t){return new e(t).log()},t.prototype.log1p=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log1p(this.get(t,e)));return this},t.log1p=function(t){return new e(t).log1p()},t.prototype.log10=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log10(this.get(t,e)));return this},t.log10=function(t){return new e(t).log10()},t.prototype.log2=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.log2(this.get(t,e)));return this},t.log2=function(t){return new e(t).log2()},t.prototype.round=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.round(this.get(t,e)));return this},t.round=function(t){return new e(t).round()},t.prototype.sign=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sign(this.get(t,e)));return this},t.sign=function(t){return new e(t).sign()},t.prototype.sin=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sin(this.get(t,e)));return this},t.sin=function(t){return new e(t).sin()},t.prototype.sinh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sinh(this.get(t,e)));return this},t.sinh=function(t){return new e(t).sinh()},t.prototype.sqrt=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.sqrt(this.get(t,e)));return this},t.sqrt=function(t){return new e(t).sqrt()},t.prototype.tan=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tan(this.get(t,e)));return this},t.tan=function(t){return new e(t).tan()},t.prototype.tanh=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.tanh(this.get(t,e)));return this},t.tanh=function(t){return new e(t).tanh()},t.prototype.trunc=function(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)this.set(t,e,Math.trunc(this.get(t,e)));return this},t.trunc=function(t){return new e(t).trunc()},t.pow=function(t,r){return new e(t).pow(r)},t.prototype.pow=function(t){return"number"==typeof t?this.powS(t):this.powM(t)},t.prototype.powS=function(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t);return this},t.prototype.powM=function(t){if(t=e.checkMatrix(t),this.rows!==t.rows||this.columns!==t.columns)throw new RangeError("Matrices dimensions must be equal");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)**t.get(e,r));return this}}function checkRowIndex(t,e,r){let n=r?t.rows:t.rows-1;if(e<0||e>n)throw new RangeError("Row index out of range")}function checkColumnIndex(t,e,r){let n=r?t.columns:t.columns-1;if(e<0||e>n)throw new RangeError("Column index out of range")}function checkRowVector(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.columns)throw new RangeError("vector size must be the same as the number of columns");return e}function checkColumnVector(t,e){if(e.to1DArray&&(e=e.to1DArray()),e.length!==t.rows)throw new RangeError("vector size must be the same as the number of rows");return e}function checkRowIndices(t,e){if(!isAnyArray.isAnyArray(e))throw new TypeError("row indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.rows)throw new RangeError("row indices are out of range")}function checkColumnIndices(t,e){if(!isAnyArray.isAnyArray(e))throw new TypeError("column indices must be an array");for(let r=0;r<e.length;r++)if(e[r]<0||e[r]>=t.columns)throw new RangeError("column indices are out of range")}function checkRange$1(t,e,r,n,o){if(5!==arguments.length)throw new RangeError("expected 4 arguments");if(checkNumber("startRow",e),checkNumber("endRow",r),checkNumber("startColumn",n),checkNumber("endColumn",o),e>r||n>o||e<0||e>=t.rows||r<0||r>=t.rows||n<0||n>=t.columns||o<0||o>=t.columns)throw new RangeError("Submatrix indices are out of range")}function newArray(t,e=0){let r=[];for(let n=0;n<t;n++)r.push(e);return r}function checkNumber(t,e){if("number"!=typeof e)throw new TypeError(`${t} must be a number`)}function checkNonEmpty(t){if(t.isEmpty())throw new Error("Empty matrix has no elements to index")}function sumByRow(t){let e=newArray(t.rows);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]+=t.get(r,n);return e}function sumByColumn(t){let e=newArray(t.columns);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]+=t.get(r,n);return e}function sumAll(t){let e=0;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e+=t.get(r,n);return e}function productByRow(t){let e=newArray(t.rows,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[r]*=t.get(r,n);return e}function productByColumn(t){let e=newArray(t.columns,1);for(let r=0;r<t.rows;++r)for(let n=0;n<t.columns;++n)e[n]*=t.get(r,n);return e}function productAll(t){let e=1;for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)e*=t.get(r,n);return e}function varianceByRow(t,e,r){const n=t.rows,o=t.columns,i=[];for(let s=0;s<n;s++){let n=0,a=0,h=0;for(let e=0;e<o;e++)h=t.get(s,e)-r[s],n+=h,a+=h*h;e?i.push((a-n*n/o)/(o-1)):i.push((a-n*n/o)/o)}return i}function varianceByColumn(t,e,r){const n=t.rows,o=t.columns,i=[];for(let s=0;s<o;s++){let o=0,a=0,h=0;for(let e=0;e<n;e++)h=t.get(e,s)-r[s],o+=h,a+=h*h;e?i.push((a-o*o/n)/(n-1)):i.push((a-o*o/n)/n)}return i}function varianceAll(t,e,r){const n=t.rows,o=t.columns,i=n*o;let s=0,a=0,h=0;for(let e=0;e<n;e++)for(let n=0;n<o;n++)h=t.get(e,n)-r,s+=h,a+=h*h;return e?(a-s*s/i)/(i-1):(a-s*s/i)/i}function centerByRow(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[r])}function centerByColumn(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e[n])}function centerAll(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)-e)}function getScaleByRow(t){const e=[];for(let r=0;r<t.rows;r++){let n=0;for(let e=0;e<t.columns;e++)n+=t.get(r,e)**2/(t.columns-1);e.push(Math.sqrt(n))}return e}function scaleByRow(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[r])}function getScaleByColumn(t){const e=[];for(let r=0;r<t.columns;r++){let n=0;for(let e=0;e<t.rows;e++)n+=t.get(e,r)**2/(t.rows-1);e.push(Math.sqrt(n))}return e}function scaleByColumn(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e[n])}function getScaleAll(t){const e=t.size-1;let r=0;for(let n=0;n<t.columns;n++)for(let o=0;o<t.rows;o++)r+=t.get(o,n)**2/e;return Math.sqrt(r)}function scaleAll(t,e){for(let r=0;r<t.rows;r++)for(let n=0;n<t.columns;n++)t.set(r,n,t.get(r,n)/e)}class AbstractMatrix{static from1DArray(t,e,r){if(t*e!==r.length)throw new RangeError("data length does not match given dimensions");let n=new Matrix$2(t,e);for(let o=0;o<t;o++)for(let t=0;t<e;t++)n.set(o,t,r[o*e+t]);return n}static rowVector(t){let e=new Matrix$2(1,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r]);return e}static columnVector(t){let e=new Matrix$2(t.length,1);for(let r=0;r<t.length;r++)e.set(r,0,t[r]);return e}static zeros(t,e){return new Matrix$2(t,e)}static ones(t,e){return new Matrix$2(t,e).fill(1)}static rand(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{random:n=Math.random}=r;let o=new Matrix$2(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++)o.set(r,t,n());return o}static randInt(t,e,r={}){if("object"!=typeof r)throw new TypeError("options must be an object");const{min:n=0,max:o=1e3,random:i=Math.random}=r;if(!Number.isInteger(n))throw new TypeError("min must be an integer");if(!Number.isInteger(o))throw new TypeError("max must be an integer");if(n>=o)throw new RangeError("min must be smaller than max");let s=o-n,a=new Matrix$2(t,e);for(let r=0;r<t;r++)for(let t=0;t<e;t++){let e=n+Math.round(i()*s);a.set(r,t,e)}return a}static eye(t,e,r){void 0===e&&(e=t),void 0===r&&(r=1);let n=Math.min(t,e),o=this.zeros(t,e);for(let t=0;t<n;t++)o.set(t,t,r);return o}static diag(t,e,r){let n=t.length;void 0===e&&(e=n),void 0===r&&(r=e);let o=Math.min(n,e,r),i=this.zeros(e,r);for(let e=0;e<o;e++)i.set(e,e,t[e]);return i}static min(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new Matrix$2(r,n);for(let i=0;i<r;i++)for(let r=0;r<n;r++)o.set(i,r,Math.min(t.get(i,r),e.get(i,r)));return o}static max(t,e){t=this.checkMatrix(t),e=this.checkMatrix(e);let r=t.rows,n=t.columns,o=new this(r,n);for(let i=0;i<r;i++)for(let r=0;r<n;r++)o.set(i,r,Math.max(t.get(i,r),e.get(i,r)));return o}static checkMatrix(t){return AbstractMatrix.isMatrix(t)?t:new Matrix$2(t)}static isMatrix(t){return null!=t&&"Matrix"===t.klass}get size(){return this.rows*this.columns}apply(t){if("function"!=typeof t)throw new TypeError("callback must be a function");for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.call(this,e,r);return this}to1DArray(){let t=[];for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.push(this.get(e,r));return t}to2DArray(){let t=[];for(let e=0;e<this.rows;e++){t.push([]);for(let r=0;r<this.columns;r++)t[e].push(this.get(e,r))}return t}toJSON(){return this.to2DArray()}isRowVector(){return 1===this.rows}isColumnVector(){return 1===this.columns}isVector(){return 1===this.rows||1===this.columns}isSquare(){return this.rows===this.columns}isEmpty(){return 0===this.rows||0===this.columns}isSymmetric(){if(this.isSquare()){for(let t=0;t<this.rows;t++)for(let e=0;e<=t;e++)if(this.get(t,e)!==this.get(e,t))return!1;return!0}return!1}isDistance(){if(!this.isSymmetric())return!1;for(let t=0;t<this.rows;t++)if(0!==this.get(t,t))return!1;return!0}isEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);t++}return n}isReducedEchelonForm(){let t=0,e=0,r=-1,n=!0,o=!1;for(;t<this.rows&&n;){for(e=0,o=!1;e<this.columns&&!1===o;)0===this.get(t,e)?e++:1===this.get(t,e)&&e>r?(o=!0,r=e):(n=!1,o=!0);for(let r=e+1;r<this.rows;r++)0!==this.get(t,r)&&(n=!1);t++}return n}echelonForm(){let t=this.clone(),e=0,r=0;for(;e<t.rows&&r<t.columns;){let n=e;for(let o=e;o<t.rows;o++)t.get(o,r)>t.get(n,r)&&(n=o);if(0===t.get(n,r))r++;else{t.swapRows(e,n);let o=t.get(e,r);for(let n=r;n<t.columns;n++)t.set(e,n,t.get(e,n)/o);for(let n=e+1;n<t.rows;n++){let o=t.get(n,r)/t.get(e,r);t.set(n,r,0);for(let i=r+1;i<t.columns;i++)t.set(n,i,t.get(n,i)-t.get(e,i)*o)}e++,r++}}return t}reducedEchelonForm(){let t=this.echelonForm(),e=t.columns,r=t.rows,n=r-1;for(;n>=0;)if(0===t.maxRow(n))n--;else{let o=0,i=!1;for(;o<r&&!1===i;)1===t.get(n,o)?i=!0:o++;for(let r=0;r<n;r++){let i=t.get(r,o);for(let s=o;s<e;s++){let e=t.get(r,s)-i*t.get(n,s);t.set(r,s,e)}}n--}return t}set(){throw new Error("set method is unimplemented")}get(){throw new Error("get method is unimplemented")}repeat(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{rows:e=1,columns:r=1}=t;if(!Number.isInteger(e)||e<=0)throw new TypeError("rows must be a positive integer");if(!Number.isInteger(r)||r<=0)throw new TypeError("columns must be a positive integer");let n=new Matrix$2(this.rows*e,this.columns*r);for(let t=0;t<e;t++)for(let e=0;e<r;e++)n.setSubMatrix(this,this.rows*t,this.columns*e);return n}fill(t){for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,t);return this}neg(){return this.mulS(-1)}getRow(t){checkRowIndex(this,t);let e=[];for(let r=0;r<this.columns;r++)e.push(this.get(t,r));return e}getRowVector(t){return Matrix$2.rowVector(this.getRow(t))}setRow(t,e){checkRowIndex(this,t),e=checkRowVector(this,e);for(let r=0;r<this.columns;r++)this.set(t,r,e[r]);return this}swapRows(t,e){checkRowIndex(this,t),checkRowIndex(this,e);for(let r=0;r<this.columns;r++){let n=this.get(t,r);this.set(t,r,this.get(e,r)),this.set(e,r,n)}return this}getColumn(t){checkColumnIndex(this,t);let e=[];for(let r=0;r<this.rows;r++)e.push(this.get(r,t));return e}getColumnVector(t){return Matrix$2.columnVector(this.getColumn(t))}setColumn(t,e){checkColumnIndex(this,t),e=checkColumnVector(this,e);for(let r=0;r<this.rows;r++)this.set(r,t,e[r]);return this}swapColumns(t,e){checkColumnIndex(this,t),checkColumnIndex(this,e);for(let r=0;r<this.rows;r++){let n=this.get(r,t);this.set(r,t,this.get(r,e)),this.set(r,e,n)}return this}addRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[r]);return this}subRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[r]);return this}mulRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[r]);return this}divRowVector(t){t=checkRowVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[r]);return this}addColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)+t[e]);return this}subColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)-t[e]);return this}mulColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)*t[e]);return this}divColumnVector(t){t=checkColumnVector(this,t);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.set(e,r,this.get(e,r)/t[e]);return this}mulRow(t,e){checkRowIndex(this,t);for(let r=0;r<this.columns;r++)this.set(t,r,this.get(t,r)*e);return this}mulColumn(t,e){checkColumnIndex(this,t);for(let r=0;r<this.rows;r++)this.set(r,t,this.get(r,t)*e);return this}max(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.NEGATIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)>t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}maxIndex(){checkNonEmpty(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)>t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}min(t){if(this.isEmpty())return NaN;switch(t){case"row":{const t=new Array(this.rows).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[e]&&(t[e]=this.get(e,r));return t}case"column":{const t=new Array(this.columns).fill(Number.POSITIVE_INFINITY);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t[r]&&(t[r]=this.get(e,r));return t}case void 0:{let t=this.get(0,0);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)this.get(e,r)<t&&(t=this.get(e,r));return t}default:throw new Error(`invalid option: ${t}`)}}minIndex(){checkNonEmpty(this);let t=this.get(0,0),e=[0,0];for(let r=0;r<this.rows;r++)for(let n=0;n<this.columns;n++)this.get(r,n)<t&&(t=this.get(r,n),e[0]=r,e[1]=n);return e}maxRow(t){if(checkRowIndex(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)>e&&(e=this.get(t,r));return e}maxRowIndex(t){checkRowIndex(this,t),checkNonEmpty(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)>e&&(e=this.get(t,n),r[1]=n);return r}minRow(t){if(checkRowIndex(this,t),this.isEmpty())return NaN;let e=this.get(t,0);for(let r=1;r<this.columns;r++)this.get(t,r)<e&&(e=this.get(t,r));return e}minRowIndex(t){checkRowIndex(this,t),checkNonEmpty(this);let e=this.get(t,0),r=[t,0];for(let n=1;n<this.columns;n++)this.get(t,n)<e&&(e=this.get(t,n),r[1]=n);return r}maxColumn(t){if(checkColumnIndex(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)>e&&(e=this.get(r,t));return e}maxColumnIndex(t){checkColumnIndex(this,t),checkNonEmpty(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)>e&&(e=this.get(n,t),r[0]=n);return r}minColumn(t){if(checkColumnIndex(this,t),this.isEmpty())return NaN;let e=this.get(0,t);for(let r=1;r<this.rows;r++)this.get(r,t)<e&&(e=this.get(r,t));return e}minColumnIndex(t){checkColumnIndex(this,t),checkNonEmpty(this);let e=this.get(0,t),r=[0,t];for(let n=1;n<this.rows;n++)this.get(n,t)<e&&(e=this.get(n,t),r[0]=n);return r}diag(){let t=Math.min(this.rows,this.columns),e=[];for(let r=0;r<t;r++)e.push(this.get(r,r));return e}norm(t="frobenius"){switch(t){case"max":return this.max();case"frobenius":return Math.sqrt(this.dot(this));default:throw new RangeError(`unknown norm type: ${t}`)}}cumulativeSum(){let t=0;for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t+=this.get(e,r),this.set(e,r,t);return this}dot(t){AbstractMatrix.isMatrix(t)&&(t=t.to1DArray());let e=this.to1DArray();if(e.length!==t.length)throw new RangeError("vectors do not have the same size");let r=0;for(let n=0;n<e.length;n++)r+=e[n]*t[n];return r}mmul(t){t=Matrix$2.checkMatrix(t);let e=this.rows,r=this.columns,n=t.columns,o=new Matrix$2(e,n),i=new Float64Array(r);for(let s=0;s<n;s++){for(let e=0;e<r;e++)i[e]=t.get(e,s);for(let t=0;t<e;t++){let e=0;for(let n=0;n<r;n++)e+=this.get(t,n)*i[n];o.set(t,s,e)}}return o}mpow(t){if(!this.isSquare())throw new RangeError("Matrix must be square");if(!Number.isInteger(t)||t<0)throw new RangeError("Exponent must be a non-negative integer");let e=Matrix$2.eye(this.rows),r=this;for(let n=t;n>=1;n/=2)1&n&&(e=e.mmul(r)),r=r.mmul(r);return e}strassen2x2(t){t=Matrix$2.checkMatrix(t);let e=new Matrix$2(2,2);const r=this.get(0,0),n=t.get(0,0),o=this.get(0,1),i=t.get(0,1),s=this.get(1,0),a=t.get(1,0),h=this.get(1,1),l=t.get(1,1),c=(r+h)*(n+l),u=(s+h)*n,f=r*(i-l),d=h*(a-n),g=(r+o)*l,m=c+d-g+(o-h)*(a+l),p=f+g,w=u+d,y=c-u+f+(s-r)*(n+i);return e.set(0,0,m),e.set(0,1,p),e.set(1,0,w),e.set(1,1,y),e}strassen3x3(t){t=Matrix$2.checkMatrix(t);let e=new Matrix$2(3,3);const r=this.get(0,0),n=this.get(0,1),o=this.get(0,2),i=this.get(1,0),s=this.get(1,1),a=this.get(1,2),h=this.get(2,0),l=this.get(2,1),c=this.get(2,2),u=t.get(0,0),f=t.get(0,1),d=t.get(0,2),g=t.get(1,0),m=t.get(1,1),p=t.get(1,2),w=t.get(2,0),y=t.get(2,1),b=t.get(2,2),x=(r-i)*(-f+m),M=(-r+i+s)*(u-f+m),v=(i+s)*(-u+f),I=r*u,A=(-r+h+l)*(u-d+p),k=(-r+h)*(d-p),R=(h+l)*(-u+d),E=(-o+l+c)*(m+w-y),P=(o-c)*(m-y),S=o*w,$=(l+c)*(-w+y),C=(-o+s+a)*(p+w-b),B=(o-a)*(p-b),T=(s+a)*(-w+b),D=I+S+n*g,_=(r+n+o-i-s-l-c)*m+M+v+I+E+S+$,O=I+A+R+(r+n+o-s-a-h-l)*p+S+C+T,V=x+s*(-u+f+g-m-p-w+b)+M+I+S+C+B,N=x+M+v+I+a*y,F=S+C+B+T+i*d,L=I+A+k+l*(-u+d+g-m-p-w+y)+E+P+S,U=E+P+S+$+h*f,z=I+A+k+R+c*b;return e.set(0,0,D),e.set(0,1,_),e.set(0,2,O),e.set(1,0,V),e.set(1,1,N),e.set(1,2,F),e.set(2,0,L),e.set(2,1,U),e.set(2,2,z),e}mmulStrassen(t){t=Matrix$2.checkMatrix(t);let e=this.clone(),r=e.rows,n=e.columns,o=t.rows,i=t.columns;function s(t,e,r){let n=t.rows,o=t.columns;if(n===e&&o===r)return t;{let n=AbstractMatrix.zeros(e,r);return n=n.setSubMatrix(t,0,0),n}}n!==o&&console.warn(`Multiplying ${r} x ${n} and ${o} x ${i} matrix: dimensions do not match.`);let a=Math.max(r,o),h=Math.max(n,i);return e=s(e,a,h),function t(e,r,n,o){if(n<=512||o<=512)return e.mmul(r);n%2==1&&o%2==1?(e=s(e,n+1,o+1),r=s(r,n+1,o+1)):n%2==1?(e=s(e,n+1,o),r=s(r,n+1,o)):o%2==1&&(e=s(e,n,o+1),r=s(r,n,o+1));let i=parseInt(e.rows/2,10),a=parseInt(e.columns/2,10),h=e.subMatrix(0,i-1,0,a-1),l=r.subMatrix(0,i-1,0,a-1),c=e.subMatrix(0,i-1,a,e.columns-1),u=r.subMatrix(0,i-1,a,r.columns-1),f=e.subMatrix(i,e.rows-1,0,a-1),d=r.subMatrix(i,r.rows-1,0,a-1),g=e.subMatrix(i,e.rows-1,a,e.columns-1),m=r.subMatrix(i,r.rows-1,a,r.columns-1),p=t(AbstractMatrix.add(h,g),AbstractMatrix.add(l,m),i,a),w=t(AbstractMatrix.add(f,g),l,i,a),y=t(h,AbstractMatrix.sub(u,m),i,a),b=t(g,AbstractMatrix.sub(d,l),i,a),x=t(AbstractMatrix.add(h,c),m,i,a),M=t(AbstractMatrix.sub(f,h),AbstractMatrix.add(l,u),i,a),v=t(AbstractMatrix.sub(c,g),AbstractMatrix.add(d,m),i,a),I=AbstractMatrix.add(p,b);I.sub(x),I.add(v);let A=AbstractMatrix.add(y,x),k=AbstractMatrix.add(w,b),R=AbstractMatrix.sub(p,w);R.add(y),R.add(M);let E=AbstractMatrix.zeros(2*I.rows,2*I.columns);return E=E.setSubMatrix(I,0,0),E=E.setSubMatrix(A,I.rows,0),E=E.setSubMatrix(k,0,I.columns),E=E.setSubMatrix(R,I.rows,I.columns),E.subMatrix(0,n-1,0,o-1)}(e,t=s(t,a,h),a,h)}scaleRows(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new Matrix$2(this.rows,this.columns);for(let t=0;t<this.rows;t++){const o=this.getRow(t);o.length>0&&rescale(o,{min:e,max:r,output:o}),n.setRow(t,o)}return n}scaleColumns(t={}){if("object"!=typeof t)throw new TypeError("options must be an object");const{min:e=0,max:r=1}=t;if(!Number.isFinite(e))throw new TypeError("min must be a number");if(!Number.isFinite(r))throw new TypeError("max must be a number");if(e>=r)throw new RangeError("min must be smaller than max");let n=new Matrix$2(this.rows,this.columns);for(let t=0;t<this.columns;t++){const o=this.getColumn(t);o.length&&rescale(o,{min:e,max:r,output:o}),n.setColumn(t,o)}return n}flipRows(){const t=Math.ceil(this.columns/2);for(let e=0;e<this.rows;e++)for(let r=0;r<t;r++){let t=this.get(e,r),n=this.get(e,this.columns-1-r);this.set(e,r,n),this.set(e,this.columns-1-r,t)}return this}flipColumns(){const t=Math.ceil(this.rows/2);for(let e=0;e<this.columns;e++)for(let r=0;r<t;r++){let t=this.get(r,e),n=this.get(this.rows-1-r,e);this.set(r,e,n),this.set(this.rows-1-r,e,t)}return this}kroneckerProduct(t){t=Matrix$2.checkMatrix(t);let e=this.rows,r=this.columns,n=t.rows,o=t.columns,i=new Matrix$2(e*n,r*o);for(let s=0;s<e;s++)for(let e=0;e<r;e++)for(let r=0;r<n;r++)for(let a=0;a<o;a++)i.set(n*s+r,o*e+a,this.get(s,e)*t.get(r,a));return i}kroneckerSum(t){if(t=Matrix$2.checkMatrix(t),!this.isSquare()||!t.isSquare())throw new Error("Kronecker Sum needs two Square Matrices");let e=this.rows,r=t.rows,n=this.kroneckerProduct(Matrix$2.eye(r,r)),o=Matrix$2.eye(e,e).kroneckerProduct(t);return n.add(o)}transpose(){let t=new Matrix$2(this.columns,this.rows);for(let e=0;e<this.rows;e++)for(let r=0;r<this.columns;r++)t.set(r,e,this.get(e,r));return t}sortRows(t=compareNumbers){for(let e=0;e<this.rows;e++)this.setRow(e,this.getRow(e).sort(t));return this}sortColumns(t=compareNumbers){for(let e=0;e<this.columns;e++)this.setColumn(e,this.getColumn(e).sort(t));return this}subMatrix(t,e,r,n){checkRange$1(this,t,e,r,n);let o=new Matrix$2(e-t+1,n-r+1);for(let i=t;i<=e;i++)for(let e=r;e<=n;e++)o.set(i-t,e-r,this.get(i,e));return o}subMatrixRow(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.columns-1),e>r||e<0||e>=this.columns||r<0||r>=this.columns)throw new RangeError("Argument out of range");let n=new Matrix$2(t.length,r-e+1);for(let o=0;o<t.length;o++)for(let i=e;i<=r;i++){if(t[o]<0||t[o]>=this.rows)throw new RangeError(`Row index out of range: ${t[o]}`);n.set(o,i-e,this.get(t[o],i))}return n}subMatrixColumn(t,e,r){if(void 0===e&&(e=0),void 0===r&&(r=this.rows-1),e>r||e<0||e>=this.rows||r<0||r>=this.rows)throw new RangeError("Argument out of range");let n=new Matrix$2(r-e+1,t.length);for(let o=0;o<t.length;o++)for(let i=e;i<=r;i++){if(t[o]<0||t[o]>=this.columns)throw new RangeError(`Column index out of range: ${t[o]}`);n.set(i-e,o,this.get(i,t[o]))}return n}setSubMatrix(t,e,r){if((t=Matrix$2.checkMatrix(t)).isEmpty())return this;checkRange$1(this,e,e+t.rows-1,r,r+t.columns-1);for(let n=0;n<t.rows;n++)for(let o=0;o<t.columns;o++)this.set(e+n,r+o,t.get(n,o));return this}selection(t,e){checkRowIndices(this,t),checkColumnIndices(this,e);let r=new Matrix$2(t.length,e.length);for(let n=0;n<t.length;n++){let o=t[n];for(let t=0;t<e.length;t++){let i=e[t];r.set(n,t,this.get(o,i))}}return r}trace(){let t=Math.min(this.rows,this.columns),e=0;for(let r=0;r<t;r++)e+=this.get(r,r);return e}clone(){return this.constructor.copy(this,new Matrix$2(this.rows,this.columns))}static copy(t,e){for(const[r,n,o]of t.entries())e.set(r,n,o);return e}sum(t){switch(t){case"row":return sumByRow(this);case"column":return sumByColumn(this);case void 0:return sumAll(this);default:throw new Error(`invalid option: ${t}`)}}product(t){switch(t){case"row":return productByRow(this);case"column":return productByColumn(this);case void 0:return productAll(this);default:throw new Error(`invalid option: ${t}`)}}mean(t){const e=this.sum(t);switch(t){case"row":for(let t=0;t<this.rows;t++)e[t]/=this.columns;return e;case"column":for(let t=0;t<this.columns;t++)e[t]/=this.rows;return e;case void 0:return e/this.size;default:throw new Error(`invalid option: ${t}`)}}variance(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{unbiased:r=!0,mean:n=this.mean(t)}=e;if("boolean"!=typeof r)throw new TypeError("unbiased must be a boolean");switch(t){case"row":if(!isAnyArray.isAnyArray(n))throw new TypeError("mean must be an array");return varianceByRow(this,r,n);case"column":if(!isAnyArray.isAnyArray(n))throw new TypeError("mean must be an array");return varianceByColumn(this,r,n);case void 0:if("number"!=typeof n)throw new TypeError("mean must be a number");return varianceAll(this,r,n);default:throw new Error(`invalid option: ${t}`)}}standardDeviation(t,e){"object"==typeof t&&(e=t,t=void 0);const r=this.variance(t,e);if(void 0===t)return Math.sqrt(r);for(let t=0;t<r.length;t++)r[t]=Math.sqrt(r[t]);return r}center(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");const{center:r=this.mean(t)}=e;switch(t){case"row":if(!isAnyArray.isAnyArray(r))throw new TypeError("center must be an array");return centerByRow(this,r),this;case"column":if(!isAnyArray.isAnyArray(r))throw new TypeError("center must be an array");return centerByColumn(this,r),this;case void 0:if("number"!=typeof r)throw new TypeError("center must be a number");return centerAll(this,r),this;default:throw new Error(`invalid option: ${t}`)}}scale(t,e={}){if("object"==typeof t&&(e=t,t=void 0),"object"!=typeof e)throw new TypeError("options must be an object");let r=e.scale;switch(t){case"row":if(void 0===r)r=getScaleByRow(this);else if(!isAnyArray.isAnyArray(r))throw new TypeError("scale must be an array");return scaleByRow(this,r),this;case"column":if(void 0===r)r=getScaleByColumn(this);else if(!isAnyArray.isAnyArray(r))throw new TypeError("scale must be an array");return scaleByColumn(this,r),this;case void 0:if(void 0===r)r=getScaleAll(this);else if("number"!=typeof r)throw new TypeError("scale must be a number");return scaleAll(this,r),this;default:throw new Error(`invalid option: ${t}`)}}toString(t){return inspectMatrixWithOptions(this,t)}[Symbol.iterator](){return this.entries()}*entries(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield[t,e,this.get(t,e)]}*values(){for(let t=0;t<this.rows;t++)for(let e=0;e<this.columns;e++)yield this.get(t,e)}}function compareNumbers(t,e){return t-e}function isArrayOfNumbers(t){return t.every(t=>"number"==typeof t)}AbstractMatrix.prototype.klass="Matrix","undefined"!=typeof Symbol&&(AbstractMatrix.prototype[Symbol.for("nodejs.util.inspect.custom")]=inspectMatrix),AbstractMatrix.random=AbstractMatrix.rand,AbstractMatrix.randomInt=AbstractMatrix.randInt,AbstractMatrix.diagonal=AbstractMatrix.diag,AbstractMatrix.prototype.diagonal=AbstractMatrix.prototype.diag,AbstractMatrix.identity=AbstractMatrix.eye,AbstractMatrix.prototype.negate=AbstractMatrix.prototype.neg,AbstractMatrix.prototype.tensorProduct=AbstractMatrix.prototype.kroneckerProduct;let Matrix$2=class t extends AbstractMatrix{data;#t(t,e){if(this.data=[],!(Number.isInteger(e)&&e>=0))throw new TypeError("nColumns must be a positive integer");for(let r=0;r<t;r++)this.data.push(new Float64Array(e));this.rows=t,this.columns=e}constructor(e,r){if(super(),t.isMatrix(e))this.#t(e.rows,e.columns),t.copy(e,this);else if(Number.isInteger(e)&&e>=0)this.#t(e,r);else{if(!isAnyArray.isAnyArray(e))throw new TypeError("First argument must be a positive number or an array");{const t=e;if("number"!=typeof(r=(e=t.length)?t[0].length:0))throw new TypeError("Data must be a 2D array with at least one element");this.data=[];for(let n=0;n<e;n++){if(t[n].length!==r)throw new RangeError("Inconsistent array dimensions");if(!isArrayOfNumbers(t[n]))throw new TypeError("Input data contains non-numeric values");this.data.push(Float64Array.from(t[n]))}this.rows=e,this.columns=r}}}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}removeRow(t){return checkRowIndex(this,t),this.data.splice(t,1),this.rows-=1,this}addRow(t,e){return void 0===e&&(e=t,t=this.rows),checkRowIndex(this,t,!0),e=Float64Array.from(checkRowVector(this,e)),this.data.splice(t,0,e),this.rows+=1,this}removeColumn(t){checkColumnIndex(this,t);for(let e=0;e<this.rows;e++){const r=new Float64Array(this.columns-1);for(let n=0;n<t;n++)r[n]=this.data[e][n];for(let n=t+1;n<this.columns;n++)r[n-1]=this.data[e][n];this.data[e]=r}return this.columns-=1,this}addColumn(t,e){void 0===e&&(e=t,t=this.columns),checkColumnIndex(this,t,!0),e=checkColumnVector(this,e);for(let r=0;r<this.rows;r++){const n=new Float64Array(this.columns+1);let o=0;for(;o<t;o++)n[o]=this.data[r][o];for(n[o++]=e[r];o<this.columns+1;o++)n[o]=this.data[r][o-1];this.data[r]=n}return this.columns+=1,this}};installMathOperations(AbstractMatrix,Matrix$2);class SymmetricMatrix extends AbstractMatrix{#e;get size(){return this.#e.size}get rows(){return this.#e.rows}get columns(){return this.#e.columns}get diagonalSize(){return this.rows}static isSymmetricMatrix(t){return Matrix$2.isMatrix(t)&&"SymmetricMatrix"===t.klassType}static zeros(t){return new this(t)}static ones(t){return new this(t).fill(1)}constructor(t){if(super(),Matrix$2.isMatrix(t)){if(!t.isSymmetric())throw new TypeError("not symmetric data");this.#e=Matrix$2.copy(t,new Matrix$2(t.rows,t.rows))}else if(Number.isInteger(t)&&t>=0)this.#e=new Matrix$2(t,t);else if(this.#e=new Matrix$2(t),!this.isSymmetric())throw new TypeError("not symmetric data")}clone(){const t=new SymmetricMatrix(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())t.set(e,r,n);return t}toMatrix(){return new Matrix$2(this)}get(t,e){return this.#e.get(t,e)}set(t,e,r){return this.#e.set(t,e,r),this.#e.set(e,t,r),this}removeCross(t){return this.#e.removeRow(t),this.#e.removeColumn(t),this}addCross(t,e){void 0===e&&(e=t,t=this.diagonalSize);const r=e.slice();return r.splice(t,1),this.#e.addRow(t,r),this.#e.addColumn(t,e),this}applyMask(t){if(t.length!==this.diagonalSize)throw new RangeError("Mask size do not match with matrix size");const e=[];for(const[r,n]of t.entries())n||e.push(r);e.reverse();for(const t of e)this.removeCross(t);return this}toCompact(){const{diagonalSize:t}=this,e=new Array(t*(t+1)/2);for(let r=0,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=++n);return e}static fromCompact(t){const e=t.length,r=(Math.sqrt(8*e+1)-1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a Symmetric Matrix, ${JSON.stringify(t)}`);const n=new SymmetricMatrix(r);for(let o=0,i=0,s=0;s<e;s++)n.set(o,i,t[s]),++o>=r&&(o=++i);return n}*upperRightEntries(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield[t,e,r],++e>=this.diagonalSize&&(e=++t)}}*upperRightValues(){for(let t=0,e=0;t<this.diagonalSize;void 0){const r=this.get(t,e);yield r,++e>=this.diagonalSize&&(e=++t)}}}SymmetricMatrix.prototype.klassType="SymmetricMatrix";class DistanceMatrix extends SymmetricMatrix{static isDistanceMatrix(t){return SymmetricMatrix.isSymmetricMatrix(t)&&"DistanceMatrix"===t.klassSubType}constructor(t){if(super(t),!this.isDistance())throw new TypeError("Provided arguments do no produce a distance matrix")}set(t,e,r){return t===e&&(r=0),super.set(t,e,r)}addCross(t,e){return void 0===e&&(e=t,t=this.diagonalSize),(e=e.slice())[t]=0,super.addCross(t,e)}toSymmetricMatrix(){return new SymmetricMatrix(this)}clone(){const t=new DistanceMatrix(this.diagonalSize);for(const[e,r,n]of this.upperRightEntries())e!==r&&t.set(e,r,n);return t}toCompact(){const{diagonalSize:t}=this,e=new Array((t-1)*t/2);for(let r=1,n=0,o=0;o<e.length;o++)e[o]=this.get(n,r),++r>=t&&(r=1+ ++n);return e}static fromCompact(t){const e=t.length;if(0===e)return new this(0);const r=(Math.sqrt(8*e+1)+1)/2;if(!Number.isInteger(r))throw new TypeError(`This array is not a compact representation of a DistanceMatrix, ${JSON.stringify(t)}`);const n=new this(r);for(let o=1,i=0,s=0;s<e;s++)n.set(o,i,t[s]),++o>=r&&(o=1+ ++i);return n}}DistanceMatrix.prototype.klassSubType="DistanceMatrix";class BaseView extends AbstractMatrix{constructor(t,e,r){super(),this.matrix=t,this.rows=e,this.columns=r}}class MatrixColumnView extends BaseView{constructor(t,e){checkColumnIndex(t,e),super(t,t.rows,1),this.column=e}set(t,e,r){return this.matrix.set(t,this.column,r),this}get(t){return this.matrix.get(t,this.column)}}class MatrixColumnSelectionView extends BaseView{constructor(t,e){checkColumnIndices(t,e),super(t,t.rows,e.length),this.columnIndices=e}set(t,e,r){return this.matrix.set(t,this.columnIndices[e],r),this}get(t,e){return this.matrix.get(t,this.columnIndices[e])}}class MatrixFlipColumnView extends BaseView{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(t,this.columns-e-1,r),this}get(t,e){return this.matrix.get(t,this.columns-e-1)}}class MatrixFlipRowView extends BaseView{constructor(t){super(t,t.rows,t.columns)}set(t,e,r){return this.matrix.set(this.rows-t-1,e,r),this}get(t,e){return this.matrix.get(this.rows-t-1,e)}}class MatrixRowView extends BaseView{constructor(t,e){checkRowIndex(t,e),super(t,1,t.columns),this.row=e}set(t,e,r){return this.matrix.set(this.row,e,r),this}get(t,e){return this.matrix.get(this.row,e)}}class MatrixRowSelectionView extends BaseView{constructor(t,e){checkRowIndices(t,e),super(t,e.length,t.columns),this.rowIndices=e}set(t,e,r){return this.matrix.set(this.rowIndices[t],e,r),this}get(t,e){return this.matrix.get(this.rowIndices[t],e)}}class MatrixSelectionView extends BaseView{constructor(t,e,r){checkRowIndices(t,e),checkColumnIndices(t,r),super(t,e.length,r.length),this.rowIndices=e,this.columnIndices=r}set(t,e,r){return this.matrix.set(this.rowIndices[t],this.columnIndices[e],r),this}get(t,e){return this.matrix.get(this.rowIndices[t],this.columnIndices[e])}}class MatrixSubView extends BaseView{constructor(t,e,r,n,o){checkRange$1(t,e,r,n,o),super(t,r-e+1,o-n+1),this.startRow=e,this.startColumn=n}set(t,e,r){return this.matrix.set(this.startRow+t,this.startColumn+e,r),this}get(t,e){return this.matrix.get(this.startRow+t,this.startColumn+e)}}class MatrixTransposeView extends BaseView{constructor(t){super(t,t.columns,t.rows)}set(t,e,r){return this.matrix.set(e,t,r),this}get(t,e){return this.matrix.get(e,t)}}class WrapperMatrix1D extends AbstractMatrix{constructor(t,e={}){const{rows:r=1}=e;if(t.length%r!==0)throw new Error("the data length is not divisible by the number of rows");super(),this.rows=r,this.columns=t.length/r,this.data=t}set(t,e,r){let n=this._calculateIndex(t,e);return this.data[n]=r,this}get(t,e){let r=this._calculateIndex(t,e);return this.data[r]}_calculateIndex(t,e){return t*this.columns+e}}class WrapperMatrix2D extends AbstractMatrix{constructor(t){super(),this.data=t,this.rows=t.length,this.columns=t[0].length}set(t,e,r){return this.data[t][e]=r,this}get(t,e){return this.data[t][e]}}function wrap(t,e){if(isAnyArray.isAnyArray(t))return t[0]&&isAnyArray.isAnyArray(t[0])?new WrapperMatrix2D(t):new WrapperMatrix1D(t,e);throw new Error("the argument is not an array")}class LuDecomposition{constructor(t){let e,r,n,o,i,s,a,h,l,c=(t=WrapperMatrix2D.checkMatrix(t)).clone(),u=c.rows,f=c.columns,d=new Float64Array(u),g=1;for(e=0;e<u;e++)d[e]=e;for(h=new Float64Array(u),r=0;r<f;r++){for(e=0;e<u;e++)h[e]=c.get(e,r);for(e=0;e<u;e++){for(l=Math.min(e,r),i=0,n=0;n<l;n++)i+=c.get(e,n)*h[n];h[e]-=i,c.set(e,r,h[e])}for(o=r,e=r+1;e<u;e++)Math.abs(h[e])>Math.abs(h[o])&&(o=e);if(o!==r){for(n=0;n<f;n++)s=c.get(o,n),c.set(o,n,c.get(r,n)),c.set(r,n,s);a=d[o],d[o]=d[r],d[r]=a,g=-g}if(r<u&&0!==c.get(r,r))for(e=r+1;e<u;e++)c.set(e,r,c.get(e,r)/c.get(r,r))}this.LU=c,this.pivotVector=d,this.pivotSign=g}isSingular(){let t=this.LU,e=t.columns;for(let r=0;r<e;r++)if(0===t.get(r,r))return!0;return!1}solve(t){t=Matrix$2.checkMatrix(t);let e=this.LU;if(e.rows!==t.rows)throw new Error("Invalid matrix dimensions");if(this.isSingular())throw new Error("LU matrix is singular");let r,n,o,i=t.columns,s=t.subMatrixRow(this.pivotVector,0,i-1),a=e.columns;for(o=0;o<a;o++)for(r=o+1;r<a;r++)for(n=0;n<i;n++)s.set(r,n,s.get(r,n)-s.get(o,n)*e.get(r,o));for(o=a-1;o>=0;o--){for(n=0;n<i;n++)s.set(o,n,s.get(o,n)/e.get(o,o));for(r=0;r<o;r++)for(n=0;n<i;n++)s.set(r,n,s.get(r,n)-s.get(o,n)*e.get(r,o))}return s}get determinant(){let t=this.LU;if(!t.isSquare())throw new Error("Matrix must be square");let e=this.pivotSign,r=t.columns;for(let n=0;n<r;n++)e*=t.get(n,n);return e}get lowerTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new Matrix$2(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o>e?n.set(o,e,t.get(o,e)):o===e?n.set(o,e,1):n.set(o,e,0);return n}get upperTriangularMatrix(){let t=this.LU,e=t.rows,r=t.columns,n=new Matrix$2(e,r);for(let o=0;o<e;o++)for(let e=0;e<r;e++)o<=e?n.set(o,e,t.get(o,e)):n.set(o,e,0);return n}get pivotPermutationVector(){return Array.from(this.pivotVector)}}function hypotenuse$1(t,e){let r=0;return Math.abs(t)>Math.abs(e)?(r=e/t,Math.abs(t)*Math.sqrt(1+r*r)):0!==e?(r=t/e,Math.abs(e)*Math.sqrt(1+r*r)):0}class QrDecomposition{constructor(t){let e,r,n,o,i=(t=WrapperMatrix2D.checkMatrix(t)).clone(),s=t.rows,a=t.columns,h=new Float64Array(a);for(n=0;n<a;n++){let t=0;for(e=n;e<s;e++)t=hypotenuse$1(t,i.get(e,n));if(0!==t){for(i.get(n,n)<0&&(t=-t),e=n;e<s;e++)i.set(e,n,i.get(e,n)/t);for(i.set(n,n,i.get(n,n)+1),r=n+1;r<a;r++){for(o=0,e=n;e<s;e++)o+=i.get(e,n)*i.get(e,r);for(o=-o/i.get(n,n),e=n;e<s;e++)i.set(e,r,i.get(e,r)+o*i.get(e,n))}}h[n]=-t}this.QR=i,this.Rdiag=h}solve(t){t=Matrix$2.checkMatrix(t);let e=this.QR,r=e.rows;if(t.rows!==r)throw new Error("Matrix row dimensions must agree");if(!this.isFullRank())throw new Error("Matrix is rank deficient");let n,o,i,s,a=t.columns,h=t.clone(),l=e.columns;for(i=0;i<l;i++)for(o=0;o<a;o++){for(s=0,n=i;n<r;n++)s+=e.get(n,i)*h.get(n,o);for(s=-s/e.get(i,i),n=i;n<r;n++)h.set(n,o,h.get(n,o)+s*e.get(n,i))}for(i=l-1;i>=0;i--){for(o=0;o<a;o++)h.set(i,o,h.get(i,o)/this.Rdiag[i]);for(n=0;n<i;n++)for(o=0;o<a;o++)h.set(n,o,h.get(n,o)-h.get(i,o)*e.get(n,i))}return h.subMatrix(0,l-1,0,a-1)}isFullRank(){let t=this.QR.columns;for(let e=0;e<t;e++)if(0===this.Rdiag[e])return!1;return!0}get upperTriangularMatrix(){let t,e,r=this.QR,n=r.columns,o=new Matrix$2(n,n);for(t=0;t<n;t++)for(e=0;e<n;e++)t<e?o.set(t,e,r.get(t,e)):t===e?o.set(t,e,this.Rdiag[t]):o.set(t,e,0);return o}get orthogonalMatrix(){let t,e,r,n,o=this.QR,i=o.rows,s=o.columns,a=new Matrix$2(i,s);for(r=s-1;r>=0;r--){for(t=0;t<i;t++)a.set(t,r,0);for(a.set(r,r,1),e=r;e<s;e++)if(0!==o.get(r,r)){for(n=0,t=r;t<i;t++)n+=o.get(t,r)*a.get(t,e);for(n=-n/o.get(r,r),t=r;t<i;t++)a.set(t,e,a.get(t,e)+n*o.get(t,r))}}return a}}let SingularValueDecomposition$1=class{constructor(t,e={}){if((t=WrapperMatrix2D.checkMatrix(t)).isEmpty())throw new Error("Matrix must be non-empty");let r=t.rows,n=t.columns;const{computeLeftSingularVectors:o=!0,computeRightSingularVectors:i=!0,autoTranspose:s=!1}=e;let a,h=Boolean(o),l=Boolean(i),c=!1;if(r<n)if(s){a=t.transpose(),r=a.rows,n=a.columns,c=!0;let e=h;h=l,l=e}else a=t.clone(),console.warn("Computing SVD on a matrix with more columns than rows. Consider enabling autoTranspose");else a=t.clone();let u=Math.min(r,n),f=Math.min(r+1,n),d=new Float64Array(f),g=new Matrix$2(r,u),m=new Matrix$2(n,n),p=new Float64Array(n),w=new Float64Array(r),y=new Float64Array(f);for(let t=0;t<f;t++)y[t]=t;let b=Math.min(r-1,n),x=Math.max(0,Math.min(n-2,r)),M=Math.max(b,x);for(let t=0;t<M;t++){if(t<b){d[t]=0;for(let e=t;e<r;e++)d[t]=hypotenuse$1(d[t],a.get(e,t));if(0!==d[t]){a.get(t,t)<0&&(d[t]=-d[t]);for(let e=t;e<r;e++)a.set(e,t,a.get(e,t)/d[t]);a.set(t,t,a.get(t,t)+1)}d[t]=-d[t]}for(let e=t+1;e<n;e++){if(t<b&&0!==d[t]){let n=0;for(let o=t;o<r;o++)n+=a.get(o,t)*a.get(o,e);n=-n/a.get(t,t);for(let o=t;o<r;o++)a.set(o,e,a.get(o,e)+n*a.get(o,t))}p[e]=a.get(t,e)}if(h&&t<b)for(let e=t;e<r;e++)g.set(e,t,a.get(e,t));if(t<x){p[t]=0;for(let e=t+1;e<n;e++)p[t]=hypotenuse$1(p[t],p[e]);if(0!==p[t]){p[t+1]<0&&(p[t]=0-p[t]);for(let e=t+1;e<n;e++)p[e]/=p[t];p[t+1]+=1}if(p[t]=-p[t],t+1<r&&0!==p[t]){for(let e=t+1;e<r;e++)w[e]=0;for(let e=t+1;e<r;e++)for(let r=t+1;r<n;r++)w[e]+=p[r]*a.get(e,r);for(let e=t+1;e<n;e++){let n=-p[e]/p[t+1];for(let o=t+1;o<r;o++)a.set(o,e,a.get(o,e)+n*w[o])}}if(l)for(let e=t+1;e<n;e++)m.set(e,t,p[e])}}let v=Math.min(n,r+1);if(b<n&&(d[b]=a.get(b,b)),r<v&&(d[v-1]=0),x+1<v&&(p[x]=a.get(x,v-1)),p[v-1]=0,h){for(let t=b;t<u;t++){for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}for(let t=b-1;t>=0;t--)if(0!==d[t]){for(let e=t+1;e<u;e++){let n=0;for(let o=t;o<r;o++)n+=g.get(o,t)*g.get(o,e);n=-n/g.get(t,t);for(let o=t;o<r;o++)g.set(o,e,g.get(o,e)+n*g.get(o,t))}for(let e=t;e<r;e++)g.set(e,t,-g.get(e,t));g.set(t,t,1+g.get(t,t));for(let e=0;e<t-1;e++)g.set(e,t,0)}else{for(let e=0;e<r;e++)g.set(e,t,0);g.set(t,t,1)}}if(l)for(let t=n-1;t>=0;t--){if(t<x&&0!==p[t])for(let e=t+1;e<n;e++){let r=0;for(let o=t+1;o<n;o++)r+=m.get(o,t)*m.get(o,e);r=-r/m.get(t+1,t);for(let o=t+1;o<n;o++)m.set(o,e,m.get(o,e)+r*m.get(o,t))}for(let e=0;e<n;e++)m.set(e,t,0);m.set(t,t,1)}let I=v-1,A=Number.EPSILON;for(;v>0;){let t,e;for(t=v-2;t>=-1&&-1!==t;t--){const e=Number.MIN_VALUE+A*Math.abs(d[t]+Math.abs(d[t+1]));if(Math.abs(p[t])<=e||Number.isNaN(p[t])){p[t]=0;break}}if(t===v-2)e=4;else{let r;for(r=v-1;r>=t&&r!==t;r--){let e=(r!==v?Math.abs(p[r]):0)+(r!==t+1?Math.abs(p[r-1]):0);if(Math.abs(d[r])<=A*e){d[r]=0;break}}r===t?e=3:r===v-1?e=1:(e=2,t=r)}switch(t++,e){case 1:{let e=p[v-2];p[v-2]=0;for(let r=v-2;r>=t;r--){let o=hypotenuse$1(d[r],e),i=d[r]/o,s=e/o;if(d[r]=o,r!==t&&(e=-s*p[r-1],p[r-1]=i*p[r-1]),l)for(let t=0;t<n;t++)o=i*m.get(t,r)+s*m.get(t,v-1),m.set(t,v-1,-s*m.get(t,r)+i*m.get(t,v-1)),m.set(t,r,o)}break}case 2:{let e=p[t-1];p[t-1]=0;for(let n=t;n<v;n++){let o=hypotenuse$1(d[n],e),i=d[n]/o,s=e/o;if(d[n]=o,e=-s*p[n],p[n]=i*p[n],h)for(let e=0;e<r;e++)o=i*g.get(e,n)+s*g.get(e,t-1),g.set(e,t-1,-s*g.get(e,n)+i*g.get(e,t-1)),g.set(e,n,o)}break}case 3:{const e=Math.max(Math.abs(d[v-1]),Math.abs(d[v-2]),Math.abs(p[v-2]),Math.abs(d[t]),Math.abs(p[t])),o=d[v-1]/e,i=d[v-2]/e,s=p[v-2]/e,a=d[t]/e,c=p[t]/e,u=((i+o)*(i-o)+s*s)/2,f=o*s*(o*s);let w=0;0===u&&0===f||(w=u<0?0-Math.sqrt(u*u+f):Math.sqrt(u*u+f),w=f/(u+w));let y=(a+o)*(a-o)+w,b=a*c;for(let e=t;e<v-1;e++){let o=hypotenuse$1(y,b);0===o&&(o=Number.MIN_VALUE);let i=y/o,s=b/o;if(e!==t&&(p[e-1]=o),y=i*d[e]+s*p[e],p[e]=i*p[e]-s*d[e],b=s*d[e+1],d[e+1]=i*d[e+1],l)for(let t=0;t<n;t++)o=i*m.get(t,e)+s*m.get(t,e+1),m.set(t,e+1,-s*m.get(t,e)+i*m.get(t,e+1)),m.set(t,e,o);if(o=hypotenuse$1(y,b),0===o&&(o=Number.MIN_VALUE),i=y/o,s=b/o,d[e]=o,y=i*p[e]+s*d[e+1],d[e+1]=-s*p[e]+i*d[e+1],b=s*p[e+1],p[e+1]=i*p[e+1],h&&e<r-1)for(let t=0;t<r;t++)o=i*g.get(t,e)+s*g.get(t,e+1),g.set(t,e+1,-s*g.get(t,e)+i*g.get(t,e+1)),g.set(t,e,o)}p[v-2]=y;break}case 4:if(d[t]<=0&&(d[t]=d[t]<0?-d[t]:0,l))for(let e=0;e<=I;e++)m.set(e,t,-m.get(e,t));for(;t<I&&!(d[t]>=d[t+1]);){let e=d[t];if(d[t]=d[t+1],d[t+1]=e,l&&t<n-1)for(let r=0;r<n;r++)e=m.get(r,t+1),m.set(r,t+1,m.get(r,t)),m.set(r,t,e);if(h&&t<r-1)for(let n=0;n<r;n++)e=g.get(n,t+1),g.set(n,t+1,g.get(n,t)),g.set(n,t,e);t++}v--}}if(c){let t=m;m=g,g=t}this.m=r,this.n=n,this.s=d,this.U=g,this.V=m}solve(t){let e=t,r=this.threshold,n=this.s.length,o=Matrix$2.zeros(n,n);for(let t=0;t<n;t++)Math.abs(this.s[t])<=r?o.set(t,t,0):o.set(t,t,1/this.s[t]);let i=this.U,s=this.rightSingularVectors,a=s.mmul(o),h=s.rows,l=i.rows,c=Matrix$2.zeros(h,l);for(let t=0;t<h;t++)for(let e=0;e<l;e++){let r=0;for(let o=0;o<n;o++)r+=a.get(t,o)*i.get(e,o);c.set(t,e,r)}return c.mmul(e)}solveForDiagonal(t){return this.solve(Matrix$2.diag(t))}inverse(){let t=this.V,e=this.threshold,r=t.rows,n=t.columns,o=new Matrix$2(r,this.s.length);for(let i=0;i<r;i++)for(let r=0;r<n;r++)Math.abs(this.s[r])>e&&o.set(i,r,t.get(i,r)/this.s[r]);let i=this.U,s=i.rows,a=i.columns,h=new Matrix$2(r,s);for(let t=0;t<r;t++)for(let e=0;e<s;e++){let r=0;for(let n=0;n<a;n++)r+=o.get(t,n)*i.get(e,n);h.set(t,e,r)}return h}get condition(){return this.s[0]/this.s[Math.min(this.m,this.n)-1]}get norm2(){return this.s[0]}get rank(){let t=Math.max(this.m,this.n)*this.s[0]*Number.EPSILON,e=0,r=this.s;for(let n=0,o=r.length;n<o;n++)r[n]>t&&e++;return e}get diagonal(){return Array.from(this.s)}get threshold(){return Number.EPSILON/2*Math.max(this.m,this.n)*this.s[0]}get leftSingularVectors(){return this.U}get rightSingularVectors(){return this.V}get diagonalMatrix(){return Matrix$2.diag(this.s)}};function inverse$1(t,e=!1){return t=WrapperMatrix2D.checkMatrix(t),e?new SingularValueDecomposition$1(t).inverse():solve(t,Matrix$2.eye(t.rows))}function solve(t,e,r=!1){return t=WrapperMatrix2D.checkMatrix(t),e=WrapperMatrix2D.checkMatrix(e),r?new SingularValueDecomposition$1(t).solve(e):t.isSquare()?new LuDecomposition(t).solve(e):new QrDecomposition(t).solve(e)}function determinant$1(t){if((t=Matrix$2.checkMatrix(t)).isSquare()){if(0===t.columns)return 1;let e,r,n,o;if(2===t.columns)return e=t.get(0,0),r=t.get(0,1),n=t.get(1,0),o=t.get(1,1),e*o-r*n;if(3===t.columns){let o,i,s;return o=new MatrixSelectionView(t,[1,2],[1,2]),i=new MatrixSelectionView(t,[1,2],[0,2]),s=new MatrixSelectionView(t,[1,2],[0,1]),e=t.get(0,0),r=t.get(0,1),n=t.get(0,2),e*determinant$1(o)-r*determinant$1(i)+n*determinant$1(s)}return new LuDecomposition(t).determinant}throw Error("determinant can only be calculated for a square matrix")}function xrange(t,e){let r=[];for(let n=0;n<t;n++)n!==e&&r.push(n);return r}function dependenciesOneRow(t,e,r,n=1e-9,o=1e-9){if(t>o)return new Array(e.rows+1).fill(0);{let t=e.addRow(r,[0]);for(let e=0;e<t.rows;e++)Math.abs(t.get(e,0))<n&&t.set(e,0,0);return t.to1DArray()}}function linearDependencies(t,e={}){const{thresholdValue:r=1e-9,thresholdError:n=1e-9}=e;let o=(t=Matrix$2.checkMatrix(t)).rows,i=new Matrix$2(o,o);for(let e=0;e<o;e++){let s=Matrix$2.columnVector(t.getRow(e)),a=t.subMatrixRow(xrange(o,e)).transpose(),h=new SingularValueDecomposition$1(a).solve(s),l=Matrix$2.sub(s,a.mmul(h)).abs().max();i.setRow(e,dependenciesOneRow(l,h,e,r,n))}return i}function pseudoInverse$1(t,e=Number.EPSILON){if((t=Matrix$2.checkMatrix(t)).isEmpty())return t.transpose();let r=new SingularValueDecomposition$1(t,{autoTranspose:!0}),n=r.leftSingularVectors,o=r.rightSingularVectors,i=r.diagonal;for(let t=0;t<i.length;t++)Math.abs(i[t])>e?i[t]=1/i[t]:i[t]=0;return o.mmul(Matrix$2.diag(i).mmul(n.transpose()))}function covariance(t,e=t,r={}){t=new Matrix$2(t);let n=!1;if("object"!=typeof e||Matrix$2.isMatrix(e)||isAnyArray.isAnyArray(e)?e=new Matrix$2(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0}=r;o&&(t=t.center("column"),n||(e=e.center("column")));const i=t.transpose().mmul(e);for(let e=0;e<i.rows;e++)for(let r=0;r<i.columns;r++)i.set(e,r,i.get(e,r)*(1/(t.rows-1)));return i}function correlation(t,e=t,r={}){t=new Matrix$2(t);let n=!1;if("object"!=typeof e||Matrix$2.isMatrix(e)||isAnyArray.isAnyArray(e)?e=new Matrix$2(e):(r=e,e=t,n=!0),t.rows!==e.rows)throw new TypeError("Both matrices must have the same number of rows");const{center:o=!0,scale:i=!0}=r;o&&(t.center("column"),n||e.center("column")),i&&(t.scale("column"),n||e.scale("column"));const s=t.standardDeviation("column",{unbiased:!0}),a=n?s:e.standardDeviation("column",{unbiased:!0}),h=t.transpose().mmul(e);for(let e=0;e<h.rows;e++)for(let r=0;r<h.columns;r++)h.set(e,r,h.get(e,r)*(1/(s[e]*a[r]))*(1/(t.rows-1)));return h}let EigenvalueDecomposition$1=class{constructor(t,e={}){const{assumeSymmetric:r=!1}=e;if(!(t=WrapperMatrix2D.checkMatrix(t)).isSquare())throw new Error("Matrix is not a square matrix");if(t.isEmpty())throw new Error("Matrix must be non-empty");let n,o,i=t.columns,s=new Matrix$2(i,i),a=new Float64Array(i),h=new Float64Array(i),l=t,c=!1;if(c=!!r||t.isSymmetric(),c){for(n=0;n<i;n++)for(o=0;o<i;o++)s.set(n,o,l.get(n,o));tred2(i,h,a,s),tql2(i,h,a,s)}else{let t=new Matrix$2(i,i),e=new Float64Array(i);for(o=0;o<i;o++)for(n=0;n<i;n++)t.set(n,o,l.get(n,o));orthes(i,t,e,s),hqr2(i,h,a,s,t)}this.n=i,this.e=h,this.d=a,this.V=s}get realEigenvalues(){return Array.from(this.d)}get imaginaryEigenvalues(){return Array.from(this.e)}get eigenvectorMatrix(){return this.V}get diagonalMatrix(){let t,e,r=this.n,n=this.e,o=this.d,i=new Matrix$2(r,r);for(t=0;t<r;t++){for(e=0;e<r;e++)i.set(t,e,0);i.set(t,t,o[t]),n[t]>0?i.set(t,t+1,n[t]):n[t]<0&&i.set(t,t-1,n[t])}return i}};function tred2(t,e,r,n){let o,i,s,a,h,l,c,u;for(h=0;h<t;h++)r[h]=n.get(t-1,h);for(a=t-1;a>0;a--){for(u=0,s=0,l=0;l<a;l++)u+=Math.abs(r[l]);if(0===u)for(e[a]=r[a-1],h=0;h<a;h++)r[h]=n.get(a-1,h),n.set(a,h,0),n.set(h,a,0);else{for(l=0;l<a;l++)r[l]/=u,s+=r[l]*r[l];for(o=r[a-1],i=Math.sqrt(s),o>0&&(i=-i),e[a]=u*i,s-=o*i,r[a-1]=o-i,h=0;h<a;h++)e[h]=0;for(h=0;h<a;h++){for(o=r[h],n.set(h,a,o),i=e[h]+n.get(h,h)*o,l=h+1;l<=a-1;l++)i+=n.get(l,h)*r[l],e[l]+=n.get(l,h)*o;e[h]=i}for(o=0,h=0;h<a;h++)e[h]/=s,o+=e[h]*r[h];for(c=o/(s+s),h=0;h<a;h++)e[h]-=c*r[h];for(h=0;h<a;h++){for(o=r[h],i=e[h],l=h;l<=a-1;l++)n.set(l,h,n.get(l,h)-(o*e[l]+i*r[l]));r[h]=n.get(a-1,h),n.set(a,h,0)}}r[a]=s}for(a=0;a<t-1;a++){if(n.set(t-1,a,n.get(a,a)),n.set(a,a,1),s=r[a+1],0!==s){for(l=0;l<=a;l++)r[l]=n.get(l,a+1)/s;for(h=0;h<=a;h++){for(i=0,l=0;l<=a;l++)i+=n.get(l,a+1)*n.get(l,h);for(l=0;l<=a;l++)n.set(l,h,n.get(l,h)-i*r[l])}}for(l=0;l<=a;l++)n.set(l,a+1,0)}for(h=0;h<t;h++)r[h]=n.get(t-1,h),n.set(t-1,h,0);n.set(t-1,t-1,1),e[0]=0}function tql2(t,e,r,n){let o,i,s,a,h,l,c,u,f,d,g,m,p,w,y,b;for(s=1;s<t;s++)e[s-1]=e[s];e[t-1]=0;let x=0,M=0,v=Number.EPSILON;for(l=0;l<t;l++){for(M=Math.max(M,Math.abs(r[l])+Math.abs(e[l])),c=l;c<t&&!(Math.abs(e[c])<=v*M);)c++;if(c>l)do{for(o=r[l],u=(r[l+1]-o)/(2*e[l]),f=hypotenuse$1(u,1),u<0&&(f=-f),r[l]=e[l]/(u+f),r[l+1]=e[l]*(u+f),d=r[l+1],i=o-r[l],s=l+2;s<t;s++)r[s]-=i;for(x+=i,u=r[c],g=1,m=g,p=g,w=e[l+1],y=0,b=0,s=c-1;s>=l;s--)for(p=m,m=g,b=y,o=g*e[s],i=g*u,f=hypotenuse$1(u,e[s]),e[s+1]=y*f,y=e[s]/f,g=u/f,u=g*r[s]-y*o,r[s+1]=i+y*(g*o+y*r[s]),h=0;h<t;h++)i=n.get(h,s+1),n.set(h,s+1,y*n.get(h,s)+g*i),n.set(h,s,g*n.get(h,s)-y*i);u=-y*b*p*w*e[l]/d,e[l]=y*u,r[l]=g*u}while(Math.abs(e[l])>v*M);r[l]=r[l]+x,e[l]=0}for(s=0;s<t-1;s++){for(h=s,u=r[s],a=s+1;a<t;a++)r[a]<u&&(h=a,u=r[a]);if(h!==s)for(r[h]=r[s],r[s]=u,a=0;a<t;a++)u=n.get(a,s),n.set(a,s,n.get(a,h)),n.set(a,h,u)}}function orthes(t,e,r,n){let o,i,s,a,h,l,c,u=t-1;for(l=1;l<=u-1;l++){for(c=0,a=l;a<=u;a++)c+=Math.abs(e.get(a,l-1));if(0!==c){for(s=0,a=u;a>=l;a--)r[a]=e.get(a,l-1)/c,s+=r[a]*r[a];for(i=Math.sqrt(s),r[l]>0&&(i=-i),s-=r[l]*i,r[l]=r[l]-i,h=l;h<t;h++){for(o=0,a=u;a>=l;a--)o+=r[a]*e.get(a,h);for(o/=s,a=l;a<=u;a++)e.set(a,h,e.get(a,h)-o*r[a])}for(a=0;a<=u;a++){for(o=0,h=u;h>=l;h--)o+=r[h]*e.get(a,h);for(o/=s,h=l;h<=u;h++)e.set(a,h,e.get(a,h)-o*r[h])}r[l]=c*r[l],e.set(l,l-1,c*i)}}for(a=0;a<t;a++)for(h=0;h<t;h++)n.set(a,h,a===h?1:0);for(l=u-1;l>=1;l--)if(0!==e.get(l,l-1)){for(a=l+1;a<=u;a++)r[a]=e.get(a,l-1);for(h=l;h<=u;h++){for(i=0,a=l;a<=u;a++)i+=r[a]*n.get(a,h);for(i=i/r[l]/e.get(l,l-1),a=l;a<=u;a++)n.set(a,h,n.get(a,h)+i*r[a])}}}function hqr2(t,e,r,n,o){let i,s,a,h,l,c,u,f,d,g,m,p,w,y,b,x=t-1,M=t-1,v=Number.EPSILON,I=0,A=0,k=0,R=0,E=0,P=0,S=0,$=0;for(i=0;i<t;i++)for((i<0||i>M)&&(r[i]=o.get(i,i),e[i]=0),s=Math.max(i-1,0);s<t;s++)A+=Math.abs(o.get(i,s));for(;x>=0;){for(h=x;h>0&&(P=Math.abs(o.get(h-1,h-1))+Math.abs(o.get(h,h)),0===P&&(P=A),!(Math.abs(o.get(h,h-1))<v*P));)h--;if(h===x)o.set(x,x,o.get(x,x)+I),r[x]=o.get(x,x),e[x]=0,x--,$=0;else if(h===x-1){if(u=o.get(x,x-1)*o.get(x-1,x),k=(o.get(x-1,x-1)-o.get(x,x))/2,R=k*k+u,S=Math.sqrt(Math.abs(R)),o.set(x,x,o.get(x,x)+I),o.set(x-1,x-1,o.get(x-1,x-1)+I),f=o.get(x,x),R>=0){for(S=k>=0?k+S:k-S,r[x-1]=f+S,r[x]=r[x-1],0!==S&&(r[x]=f-u/S),e[x-1]=0,e[x]=0,f=o.get(x,x-1),P=Math.abs(f)+Math.abs(S),k=f/P,R=S/P,E=Math.sqrt(k*k+R*R),k/=E,R/=E,s=x-1;s<t;s++)S=o.get(x-1,s),o.set(x-1,s,R*S+k*o.get(x,s)),o.set(x,s,R*o.get(x,s)-k*S);for(i=0;i<=x;i++)S=o.get(i,x-1),o.set(i,x-1,R*S+k*o.get(i,x)),o.set(i,x,R*o.get(i,x)-k*S);for(i=0;i<=M;i++)S=n.get(i,x-1),n.set(i,x-1,R*S+k*n.get(i,x)),n.set(i,x,R*n.get(i,x)-k*S)}else r[x-1]=f+k,r[x]=f+k,e[x-1]=S,e[x]=-S;x-=2,$=0}else{if(f=o.get(x,x),d=0,u=0,h<x&&(d=o.get(x-1,x-1),u=o.get(x,x-1)*o.get(x-1,x)),10===$){for(I+=f,i=0;i<=x;i++)o.set(i,i,o.get(i,i)-f);P=Math.abs(o.get(x,x-1))+Math.abs(o.get(x-1,x-2)),f=d=.75*P,u=-.4375*P*P}if(30===$&&(P=(d-f)/2,P=P*P+u,P>0)){for(P=Math.sqrt(P),d<f&&(P=-P),P=f-u/((d-f)/2+P),i=0;i<=x;i++)o.set(i,i,o.get(i,i)-P);I+=P,f=d=u=.964}for($+=1,l=x-2;l>=h&&(S=o.get(l,l),E=f-S,P=d-S,k=(E*P-u)/o.get(l+1,l)+o.get(l,l+1),R=o.get(l+1,l+1)-S-E-P,E=o.get(l+2,l+1),P=Math.abs(k)+Math.abs(R)+Math.abs(E),k/=P,R/=P,E/=P,l!==h)&&!(Math.abs(o.get(l,l-1))*(Math.abs(R)+Math.abs(E))<v*(Math.abs(k)*(Math.abs(o.get(l-1,l-1))+Math.abs(S)+Math.abs(o.get(l+1,l+1)))));)l--;for(i=l+2;i<=x;i++)o.set(i,i-2,0),i>l+2&&o.set(i,i-3,0);for(a=l;a<=x-1&&(y=a!==x-1,a!==l&&(k=o.get(a,a-1),R=o.get(a+1,a-1),E=y?o.get(a+2,a-1):0,f=Math.abs(k)+Math.abs(R)+Math.abs(E),0!==f&&(k/=f,R/=f,E/=f)),0!==f);a++)if(P=Math.sqrt(k*k+R*R+E*E),k<0&&(P=-P),0!==P){for(a!==l?o.set(a,a-1,-P*f):h!==l&&o.set(a,a-1,-o.get(a,a-1)),k+=P,f=k/P,d=R/P,S=E/P,R/=k,E/=k,s=a;s<t;s++)k=o.get(a,s)+R*o.get(a+1,s),y&&(k+=E*o.get(a+2,s),o.set(a+2,s,o.get(a+2,s)-k*S)),o.set(a,s,o.get(a,s)-k*f),o.set(a+1,s,o.get(a+1,s)-k*d);for(i=0;i<=Math.min(x,a+3);i++)k=f*o.get(i,a)+d*o.get(i,a+1),y&&(k+=S*o.get(i,a+2),o.set(i,a+2,o.get(i,a+2)-k*E)),o.set(i,a,o.get(i,a)-k),o.set(i,a+1,o.get(i,a+1)-k*R);for(i=0;i<=M;i++)k=f*n.get(i,a)+d*n.get(i,a+1),y&&(k+=S*n.get(i,a+2),n.set(i,a+2,n.get(i,a+2)-k*E)),n.set(i,a,n.get(i,a)-k),n.set(i,a+1,n.get(i,a+1)-k*R)}}}if(0!==A){for(x=t-1;x>=0;x--)if(k=r[x],R=e[x],0===R)for(h=x,o.set(x,x,1),i=x-1;i>=0;i--){for(u=o.get(i,i)-k,E=0,s=h;s<=x;s++)E+=o.get(i,s)*o.get(s,x);if(e[i]<0)S=u,P=E;else if(h=i,0===e[i]?o.set(i,x,0!==u?-E/u:-E/(v*A)):(f=o.get(i,i+1),d=o.get(i+1,i),R=(r[i]-k)*(r[i]-k)+e[i]*e[i],c=(f*P-S*E)/R,o.set(i,x,c),o.set(i+1,x,Math.abs(f)>Math.abs(S)?(-E-u*c)/f:(-P-d*c)/S)),c=Math.abs(o.get(i,x)),v*c*c>1)for(s=i;s<=x;s++)o.set(s,x,o.get(s,x)/c)}else if(R<0)for(h=x-1,Math.abs(o.get(x,x-1))>Math.abs(o.get(x-1,x))?(o.set(x-1,x-1,R/o.get(x,x-1)),o.set(x-1,x,-(o.get(x,x)-k)/o.get(x,x-1))):(b=cdiv(0,-o.get(x-1,x),o.get(x-1,x-1)-k,R),o.set(x-1,x-1,b[0]),o.set(x-1,x,b[1])),o.set(x,x-1,0),o.set(x,x,1),i=x-2;i>=0;i--){for(g=0,m=0,s=h;s<=x;s++)g+=o.get(i,s)*o.get(s,x-1),m+=o.get(i,s)*o.get(s,x);if(u=o.get(i,i)-k,e[i]<0)S=u,E=g,P=m;else if(h=i,0===e[i]?(b=cdiv(-g,-m,u,R),o.set(i,x-1,b[0]),o.set(i,x,b[1])):(f=o.get(i,i+1),d=o.get(i+1,i),p=(r[i]-k)*(r[i]-k)+e[i]*e[i]-R*R,w=2*(r[i]-k)*R,0===p&&0===w&&(p=v*A*(Math.abs(u)+Math.abs(R)+Math.abs(f)+Math.abs(d)+Math.abs(S))),b=cdiv(f*E-S*g+R*m,f*P-S*m-R*g,p,w),o.set(i,x-1,b[0]),o.set(i,x,b[1]),Math.abs(f)>Math.abs(S)+Math.abs(R)?(o.set(i+1,x-1,(-g-u*o.get(i,x-1)+R*o.get(i,x))/f),o.set(i+1,x,(-m-u*o.get(i,x)-R*o.get(i,x-1))/f)):(b=cdiv(-E-d*o.get(i,x-1),-P-d*o.get(i,x),S,R),o.set(i+1,x-1,b[0]),o.set(i+1,x,b[1]))),c=Math.max(Math.abs(o.get(i,x-1)),Math.abs(o.get(i,x))),v*c*c>1)for(s=i;s<=x;s++)o.set(s,x-1,o.get(s,x-1)/c),o.set(s,x,o.get(s,x)/c)}for(i=0;i<t;i++)if(i<0||i>M)for(s=i;s<t;s++)n.set(i,s,o.get(i,s));for(s=t-1;s>=0;s--)for(i=0;i<=M;i++){for(S=0,a=0;a<=Math.min(s,M);a++)S+=n.get(i,a)*o.get(a,s);n.set(i,s,S)}}}function cdiv(t,e,r,n){let o,i;return Math.abs(r)>Math.abs(n)?(o=n/r,i=r+o*n,[(t+o*e)/i,(e-o*t)/i]):(o=r/n,i=n+o*r,[(o*t+e)/i,(o*e-t)/i])}class CholeskyDecomposition{constructor(t){if(!(t=WrapperMatrix2D.checkMatrix(t)).isSymmetric())throw new Error("Matrix is not symmetric");let e,r,n,o=t,i=o.rows,s=new Matrix$2(i,i),a=!0;for(r=0;r<i;r++){let t=0;for(n=0;n<r;n++){let i=0;for(e=0;e<n;e++)i+=s.get(n,e)*s.get(r,e);i=(o.get(r,n)-i)/s.get(n,n),s.set(r,n,i),t+=i*i}for(t=o.get(r,r)-t,a&&=t>0,s.set(r,r,Math.sqrt(Math.max(t,0))),n=r+1;n<i;n++)s.set(r,n,0)}this.L=s,this.positiveDefinite=a}isPositiveDefinite(){return this.positiveDefinite}solve(t){t=WrapperMatrix2D.checkMatrix(t);let e=this.L,r=e.rows;if(t.rows!==r)throw new Error("Matrix dimensions do not match");if(!1===this.isPositiveDefinite())throw new Error("Matrix is not positive definite");let n,o,i,s=t.columns,a=t.clone();for(i=0;i<r;i++)for(o=0;o<s;o++){for(n=0;n<i;n++)a.set(i,o,a.get(i,o)-a.get(n,o)*e.get(i,n));a.set(i,o,a.get(i,o)/e.get(i,i))}for(i=r-1;i>=0;i--)for(o=0;o<s;o++){for(n=i+1;n<r;n++)a.set(i,o,a.get(i,o)-a.get(n,o)*e.get(n,i));a.set(i,o,a.get(i,o)/e.get(i,i))}return a}get lowerTriangularMatrix(){return this.L}}class nipals{constructor(t,e={}){t=WrapperMatrix2D.checkMatrix(t);let{Y:r}=e;const{scaleScores:n=!1,maxIterations:o=1e3,terminationCriteria:i=1e-10}=e;let s;if(r){if(r=isAnyArray.isAnyArray(r)&&"number"==typeof r[0]?Matrix$2.columnVector(r):WrapperMatrix2D.checkMatrix(r),r.rows!==t.rows)throw new Error("Y should have the same number of rows as X");s=r.getColumnVector(0)}else s=t.getColumnVector(0);let a,h,l,c,u=1;for(let e=0;e<o&&u>i;e++)l=t.transpose().mmul(s).div(s.transpose().mmul(s).get(0,0)),l=l.div(l.norm()),a=t.mmul(l).div(l.transpose().mmul(l).get(0,0)),e>0&&(u=a.clone().sub(c).pow(2).sum()),c=a.clone(),r?(h=r.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),h=h.div(h.norm()),s=r.mmul(h).div(h.transpose().mmul(h).get(0,0))):s=a;if(r){let e=t.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0));e=e.div(e.norm());let n=t.clone().sub(a.clone().mmul(e.transpose())),o=s.transpose().mmul(a).div(a.transpose().mmul(a).get(0,0)),i=r.clone().sub(a.clone().mulS(o.get(0,0)).mmul(h.transpose()));this.t=a,this.p=e.transpose(),this.w=l.transpose(),this.q=h,this.u=s,this.s=a.transpose().mmul(a),this.xResidual=n,this.yResidual=i,this.betas=o}else this.w=l.transpose(),this.s=a.transpose().mmul(a).sqrt(),this.t=n?a.clone().div(this.s.get(0,0)):a,this.xResidual=t.sub(a.mmul(l.transpose()))}}matrix.AbstractMatrix=AbstractMatrix,matrix.CHO=CholeskyDecomposition,matrix.CholeskyDecomposition=CholeskyDecomposition,matrix.DistanceMatrix=DistanceMatrix,matrix.EVD=EigenvalueDecomposition$1;var EigenvalueDecomposition_1=matrix.EigenvalueDecomposition=EigenvalueDecomposition$1;matrix.LU=LuDecomposition,matrix.LuDecomposition=LuDecomposition;var Matrix_1=matrix.Matrix=Matrix$2;matrix.MatrixColumnSelectionView=MatrixColumnSelectionView,matrix.MatrixColumnView=MatrixColumnView,matrix.MatrixFlipColumnView=MatrixFlipColumnView,matrix.MatrixFlipRowView=MatrixFlipRowView,matrix.MatrixRowSelectionView=MatrixRowSelectionView,matrix.MatrixRowView=MatrixRowView,matrix.MatrixSelectionView=MatrixSelectionView,matrix.MatrixSubView=MatrixSubView,matrix.MatrixTransposeView=MatrixTransposeView,matrix.NIPALS=nipals,matrix.Nipals=nipals,matrix.QR=QrDecomposition,matrix.QrDecomposition=QrDecomposition;var SVD$1=matrix.SVD=SingularValueDecomposition$1,SingularValueDecomposition_1=matrix.SingularValueDecomposition=SingularValueDecomposition$1;matrix.SymmetricMatrix=SymmetricMatrix,matrix.WrapperMatrix1D=WrapperMatrix1D,matrix.WrapperMatrix2D=WrapperMatrix2D,matrix.correlation=correlation,matrix.covariance=covariance;var _default=matrix.default=Matrix$2,determinant_1=matrix.determinant=determinant$1,inverse_1=matrix.inverse=inverse$1;matrix.linearDependencies=linearDependencies;var pseudoInverse_1=matrix.pseudoInverse=pseudoInverse$1;matrix.solve=solve,matrix.wrap=wrap;const EigenvalueDecomposition=EigenvalueDecomposition_1,Matrix=Matrix_1,SVD=SVD$1,SingularValueDecomposition=SingularValueDecomposition_1;var Matrix$1=_default.Matrix?_default.Matrix:Matrix_1;const determinant=determinant_1,inverse=inverse_1,pseudoInverse=pseudoInverse_1;function getCentroid(t){let e=new Matrix$1(3,1);const r=t.columns;let n=0,o=0;for(let e=0;e<r;e++)n+=t.get(0,e),o+=t.get(1,e);return e.set(0,0,n/r),e.set(1,0,o/r),e}function getAffineTransform$1(t,e){if(t.columns!==e.columns)throw new Error("Source and destination matrices should have same dimensions (same number of points).");if(t.columns<2)throw new Error("Matrices should contain at least two points for the algorithm to run properly.");const r=getCentroid(t),n=getCentroid(e),o=t.clone().subColumnVector(r),i=e.clone().subColumnVector(n);let s=0;const a=t.columns;for(let t=0;t<a;t++){const e=o.getColumn(t);s+=getDistanceToOrigin(i.getColumn(t))/getDistanceToOrigin(e)}const h=s/a,l=Matrix$1.mul(o,h).mmul(i.transpose()),c=new SingularValueDecomposition(l),u=c.leftSingularVectors,f=c.rightSingularVectors;let d=f.mmul(u.transpose());if(determinant(d)<0){d=f.mulColumn(2,-1).mmul(u.transpose())}let g=180*Math.atan2(d.get(1,0),d.get(0,0))/Math.PI;-180===g&&(g=180);const m=Matrix$1.sub(n,Matrix$1.mul(d.mmul(r),h));return{translation:{x:m.get(0,0),y:m.get(1,0)},rotation:g,scale:h}}function getDistanceToOrigin(t){return Math.hypot(t[0],t[1])}var medianQuickselect_min={exports:{}};!function(t){!function(){function e(t){for(var e=0,o=t.length-1,i=void 0,s=void 0,a=void 0,h=n(e,o);;){if(o<=e)return t[h];if(o==e+1)return t[e]>t[o]&&r(t,e,o),t[h];for(t[i=n(e,o)]>t[o]&&r(t,i,o),t[e]>t[o]&&r(t,e,o),t[i]>t[e]&&r(t,i,e),r(t,i,e+1),s=e+1,a=o;;){do{s++}while(t[e]>t[s]);do{a--}while(t[a]>t[e]);if(a<s)break;r(t,s,a)}r(t,e,a),a<=h&&(e=s),a>=h&&(o=a-1)}}var r=function(t,e,r){var n;return n=[t[r],t[e]],t[e]=n[0],t[r]=n[1],n},n=function(t,e){return~~((t+e)/2)};t.exports?t.exports=e:window.median=e}()}(medianQuickselect_min);var medianQuickselect_minExports=medianQuickselect_min.exports,quickMedian=getDefaultExportFromCjs(medianQuickselect_minExports);function median$1(t){if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");return quickMedian(t.slice())}const LOOP$1=8,FLOAT_MUL$1=1/16777216,sh1$1=15,sh2$1=18,sh3$1=11;function multiply_uint32(t,e){const r=65535&(t>>>=0);return((t-r)*(e>>>=0)>>>0)+r*e>>>0}let XSadd$1=class{constructor(t=Date.now()){this.state=new Uint32Array(4),this.init(t),this.random=this.getFloat.bind(this)}getUint32(){return this.nextState(),this.state[3]+this.state[2]>>>0}getFloat(){return(this.getUint32()>>>8)*FLOAT_MUL$1}init(t){if(!Number.isInteger(t))throw new TypeError("seed must be an integer");this.state[0]=t,this.state[1]=0,this.state[2]=0,this.state[3]=0;for(let t=1;t<LOOP$1;t++)this.state[3&t]^=t+multiply_uint32(1812433253,this.state[t-1&3]^this.state[t-1&3]>>>30>>>0)>>>0;this.periodCertification();for(let t=0;t<LOOP$1;t++)this.nextState()}periodCertification(){0===this.state[0]&&0===this.state[1]&&0===this.state[2]&&0===this.state[3]&&(this.state[0]=88,this.state[1]=83,this.state[2]=65,this.state[3]=68)}nextState(){let t=this.state[0];t^=t<<sh1$1,t^=t>>>sh2$1,t^=this.state[3]<<sh3$1,this.state[0]=this.state[1],this.state[1]=this.state[2],this.state[2]=this.state[3],this.state[3]=t}};const PROB_TOLERANCE=1e-8;function randomChoice(t,e={},r=Math.random){const{size:n=1,replace:o=!1,probabilities:i}=e;let s,a;if(s="number"==typeof t?getArray(t):t.slice(),i){if(!o)throw new Error("choice with probabilities and no replacement is not implemented");if(i.length!==s.length)throw new Error("the length of probabilities option should be equal to the number of choices");a=[i[0]];for(let t=1;t<i.length;t++)a[t]=a[t-1]+i[t];if(Math.abs(1-a[a.length-1])>PROB_TOLERANCE)throw new Error(`probabilities should sum to 1, but instead sums to ${a[a.length-1]}`)}if(!1===o&&n>s.length)throw new Error("size option is too large");const h=[];for(let t=0;t<n;t++){const t=randomIndex(s.length,r,a);h.push(s[t]),o||s.splice(t,1)}return h}function getArray(t){const e=[];for(let r=0;r<t;r++)e.push(r);return e}function randomIndex(t,e,r){const n=e();if(r){let t=0;for(;n>r[t];)t++;return t}return Math.floor(n*t)}class Random{constructor(t=Math.random){if("number"==typeof t){const e=new XSadd$1(t);this.randomGenerator=e.random}else this.randomGenerator=t}choice(t,e){return randomChoice(t,e,this.randomGenerator)}random(){return this.randomGenerator()}randInt(t,e){return void 0===e&&(e=t,t=0),t+Math.floor(this.randomGenerator()*(e-t))}randomSample(t){const e=[];for(let r=0;r<t;r++)e.push(this.random());return e}}function getNbIterations(t,e,r){const n=Math.log10(1-t)/Math.log10(1-(1-e)**r);return Math.ceil(n)}function ransac(t,e,r){let n=0;n=r.minNbInliers?getNbValues(r.minNbInliers,t.length):t.length;const{sampleSize:o=2,threshold:i=1,fitFunction:s,distanceFunction:a,modelFunction:h,seed:l}=r;if(t.length!==e.length)throw new Error("source and destination data should have the same length");let c;if("outliersFraction"in r){const{stopProbabilty:t=.99}=r;c=getNbIterations(t,r.outliersFraction,o)}else c=r.maxNbIterations?r.maxNbIterations:100;let u=0,f=0,d=[],g=[],m=0,p=[];for(void 0!==l&&(p=new Random(l).choice(c,{size:c}));u<c;){let r;u++,r=void 0!==l?new Random(p[u]).choice(t.length,{size:o}):(new Random).choice(t.length,{size:o});const c=[],w=[];for(let n of r)c.push(t[n]),w.push(e[n]);const y=s(c,w),b=h(y);let x=[];for(let e of t)x.push(b(e));let M=0,v=[],I=[],A=0;for(let t=0;t<e.length;t++){if(r.includes(t)){M++,v.push(t);continue}const n=a(e[t],x[t]);I.push(n),n<i&&(M++,v.push(t))}if(A=median$1(I),M>f&&(f=M,d=v,g=y,m=A,M>=n))return{modelParameters:y,inliers:v,nbIterations:u,error:A}}return{modelParameters:g,inliers:d,nbIterations:c,error:m}}function getNbValues(t,e){return Number.isInteger(t)?t:Math.ceil(t*e)}function getDistanceMatrix(t){const e=t.length,r=new Array(e);for(let t=0;t<e;t++)r[t]=new Float64Array(e).fill(0);for(let n=0;n<e;n++){const o=t[n].origin;for(let i=n;i<e;i++){n===i&&(r[n][i]=0);const e=t[i].origin,s=(o.row-e.row)**2+(o.column-e.column)**2;r[n][i]=s,r[i][n]=s}}return r}function getKeypointsInRadius(t,e){const r=t.length,n=getDistanceMatrix(t),o=e**2,i=[];for(let t=0;t<r;t++){const e=[];for(let i=0;i<r;i++)t!==i&&n[t][i]<=o&&e.push(i);i.push(e)}return i}function getBestKeypointsInRadius(t,e=5){const r=t.length,n=getKeypointsInRadius(t,e),o=new Uint8Array(r).fill(0);for(let e=0;e<r;e++){const r=t[e];for(const i of n[e]){const n=t[i];r.score<n.score&&(o[e]=1)}}const i=[];for(let e=0;e<r;e++)o[e]||i.push(t[e]);return i}function difference(t,e){return{column:t.column-e.column,row:t.row-e.row}}function sum$1(t,e){return{column:t.column+e.column,row:t.row+e.row}}function normalize(t){const e=Math.hypot(t.column,t.row);return{column:t.column/e,row:t.row/e}}function rotate$1(t,e){const r=[],n=Math.cos(t),o=Math.sin(t);for(const t of e)r.push({column:n*t.column-o*t.row,row:o*t.column+n*t.row});return r}function sortByColumnRow(t){const e=t.slice();return e.sort((t,e)=>t.column<e.column?-1:t.column>e.column?1:t.row-e.row),e}function getAngle(t,e){const r=normalize(difference(e,t)),n=Math.acos(r.column);return r.row<0?-n:n}function getClockwiseAngle(t,e){const r=normalize(difference(e,t)),n=-Math.atan(r.row/r.column);return r.column<0?r.row<0?n-Math.PI:n+Math.PI:n}function toDegrees(t){return 180*t/Math.PI}function getRadius(t){if(t%2!=1||t<0)throw new RangeError("size must be positive and odd");return(t-1)/2}function checkBorderDistance(t,e,r){return e.column>=r&&e.row>=r&&t.width-e.column>r&&t.height-e.row>r}const t$2=Symbol.for("@ts-pattern/matcher"),e$2=Symbol.for("@ts-pattern/isVariadic"),n$2="@ts-pattern/anonymous-select-key",r$2=t=>Boolean(t&&"object"==typeof t),i$3=t=>t&&!!t[t$2],o$2=(t,e,r)=>{if(i$3(t)){const n=t[t$2](),{matched:o,selections:i}=n.match(e);return o&&i&&Object.keys(i).forEach(t=>r(t,i[t])),o}if(r$2(t)){if(!r$2(e))return!1;if(Array.isArray(t)){if(!Array.isArray(e))return!1;let n=[],o=[],i=[];for(const e of t.keys()){const r=t[e];i$3(r)&&r[e$2]?i.push(r):i.length?o.push(r):n.push(r)}if(i.length){if(i.length>1)throw new Error("Pattern error: Using `...P.array(...)` several times in a single pattern is not allowed.");if(e.length<n.length+o.length)return!1;const t=e.slice(0,n.length),s=0===o.length?[]:e.slice(-o.length),a=e.slice(n.length,0===o.length?1/0:-o.length);return n.every((e,n)=>o$2(e,t[n],r))&&o.every((t,e)=>o$2(t,s[e],r))&&(0===i.length||o$2(i[0],a,r))}return t.length===e.length&&t.every((t,n)=>o$2(t,e[n],r))}return Reflect.ownKeys(t).every(n=>{const o=t[n];return(n in e||i$3(i=o)&&"optional"===i[t$2]().matcherType)&&o$2(o,e[n],r);var i})}return Object.is(e,t)},s$1=t=>{var e,r,n;return r$2(t)?i$3(t)?null!=(e=null==(r=(n=t[t$2]()).getSelectionKeys)?void 0:r.call(n))?e:[]:Array.isArray(t)?c$1(t,s$1):c$1(Object.values(t),s$1):[]},c$1=(t,e)=>t.reduce((t,r)=>t.concat(e(r)),[]);function u$2(...t){if(1===t.length){const[e]=t;return t=>o$2(e,t,()=>{})}if(2===t.length){const[e,r]=t;return o$2(e,r,()=>{})}throw new Error(`isMatching wasn't given the right number of arguments: expected 1 or 2, received ${t.length}.`)}function a$2(t){return Object.assign(t,{optional:()=>h$2(t),and:e=>d$1(t,e),or:e=>y$1(t,e),select:e=>void 0===e?v$1(t):v$1(e,t)})}function l$2(t){return Object.assign((t=>Object.assign(t,{[Symbol.iterator](){let e=0;const r=[{value:Object.assign(t,{[e$2]:!0}),done:!1},{done:!0,value:void 0}];return{next:()=>{var t;return null!=(t=r[e++])?t:r.at(-1)}}}}))(t),{optional:()=>l$2(h$2(t)),select:e=>l$2(void 0===e?v$1(t):v$1(e,t))})}function h$2(t){return a$2({[t$2]:()=>({match:e=>{let r={};const n=(t,e)=>{r[t]=e};return void 0===e?(s$1(t).forEach(t=>n(t,void 0)),{matched:!0,selections:r}):{matched:o$2(t,e,n),selections:r}},getSelectionKeys:()=>s$1(t),matcherType:"optional"})})}const f$1=(t,e)=>{for(const r of t)if(!e(r))return!1;return!0},g$1=(t,e)=>{for(const[r,n]of t.entries())if(!e(n,r))return!1;return!0},m$1=(t,e)=>{const r=Reflect.ownKeys(t);for(const n of r)if(!e(n,t[n]))return!1;return!0};function d$1(...t){return a$2({[t$2]:()=>({match:e=>{let r={};const n=(t,e)=>{r[t]=e};return{matched:t.every(t=>o$2(t,e,n)),selections:r}},getSelectionKeys:()=>c$1(t,s$1),matcherType:"and"})})}function y$1(...t){return a$2({[t$2]:()=>({match:e=>{let r={};const n=(t,e)=>{r[t]=e};return c$1(t,s$1).forEach(t=>n(t,void 0)),{matched:t.some(t=>o$2(t,e,n)),selections:r}},getSelectionKeys:()=>c$1(t,s$1),matcherType:"or"})})}function p$2(t){return{[t$2]:()=>({match:e=>({matched:Boolean(t(e))})})}}function v$1(...t){const e="string"==typeof t[0]?t[0]:void 0,r=2===t.length?t[1]:"string"==typeof t[0]?void 0:t[0];return a$2({[t$2]:()=>({match:t=>{let n={[null!=e?e:n$2]:t};return{matched:void 0===r||o$2(r,t,(t,e)=>{n[t]=e}),selections:n}},getSelectionKeys:()=>[null!=e?e:n$2].concat(void 0===r?[]:s$1(r))})})}function b$2(t){return!0}function w$2(t){return"number"==typeof t}function S$1(t){return"string"==typeof t}function j$1(t){return"bigint"==typeof t}const K=a$2(p$2(b$2)),O=a$2(p$2(b$2)),E=K,x$2=t=>Object.assign(a$2(t),{startsWith:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&t.startsWith(r)))));var r},endsWith:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&t.endsWith(r)))));var r},minLength:e=>x$2(d$1(t,(t=>p$2(e=>S$1(e)&&e.length>=t))(e))),length:e=>x$2(d$1(t,(t=>p$2(e=>S$1(e)&&e.length===t))(e))),maxLength:e=>x$2(d$1(t,(t=>p$2(e=>S$1(e)&&e.length<=t))(e))),includes:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&t.includes(r)))));var r},regex:e=>{return x$2(d$1(t,(r=e,p$2(t=>S$1(t)&&Boolean(t.match(r))))));var r}}),A=x$2(p$2(S$1)),N$1=t=>Object.assign(a$2(t),{between:(e,r)=>N$1(d$1(t,((t,e)=>p$2(r=>w$2(r)&&t<=r&&e>=r))(e,r))),lt:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e<t))(e))),gt:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e>t))(e))),lte:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e<=t))(e))),gte:e=>N$1(d$1(t,(t=>p$2(e=>w$2(e)&&e>=t))(e))),int:()=>N$1(d$1(t,p$2(t=>w$2(t)&&Number.isInteger(t)))),finite:()=>N$1(d$1(t,p$2(t=>w$2(t)&&Number.isFinite(t)))),positive:()=>N$1(d$1(t,p$2(t=>w$2(t)&&t>0))),negative:()=>N$1(d$1(t,p$2(t=>w$2(t)&&t<0)))}),P=N$1(p$2(w$2)),k$1=t=>Object.assign(a$2(t),{between:(e,r)=>k$1(d$1(t,((t,e)=>p$2(r=>j$1(r)&&t<=r&&e>=r))(e,r))),lt:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e<t))(e))),gt:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e>t))(e))),lte:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e<=t))(e))),gte:e=>k$1(d$1(t,(t=>p$2(e=>j$1(e)&&e>=t))(e))),positive:()=>k$1(d$1(t,p$2(t=>j$1(t)&&t>0))),negative:()=>k$1(d$1(t,p$2(t=>j$1(t)&&t<0)))}),T=k$1(p$2(j$1)),B=a$2(p$2(function(t){return"boolean"==typeof t})),_=a$2(p$2(function(t){return"symbol"==typeof t})),W=a$2(p$2(function(t){return null==t})),$$1=a$2(p$2(function(t){return null!=t}));var z={__proto__:null,matcher:t$2,optional:h$2,array:function(...t){return l$2({[t$2]:()=>({match:e=>{if(!Array.isArray(e))return{matched:!1};if(0===t.length)return{matched:!0};const r=t[0];let n={};if(0===e.length)return s$1(r).forEach(t=>{n[t]=[]}),{matched:!0,selections:n};const o=(t,e)=>{n[t]=(n[t]||[]).concat([e])};return{matched:e.every(t=>o$2(r,t,o)),selections:n}},getSelectionKeys:()=>0===t.length?[]:s$1(t[0])})})},set:function(...t){return a$2({[t$2]:()=>({match:e=>{if(!(e instanceof Set))return{matched:!1};let r={};if(0===e.size)return{matched:!0,selections:r};if(0===t.length)return{matched:!0};const n=(t,e)=>{r[t]=(r[t]||[]).concat([e])},o=t[0];return{matched:f$1(e,t=>o$2(o,t,n)),selections:r}},getSelectionKeys:()=>0===t.length?[]:s$1(t[0])})})},map:function(...t){return a$2({[t$2]:()=>({match:e=>{if(!(e instanceof Map))return{matched:!1};let r={};if(0===e.size)return{matched:!0,selections:r};const n=(t,e)=>{r[t]=(r[t]||[]).concat([e])};if(0===t.length)return{matched:!0};var o;if(1===t.length)throw new Error(`\`P.map\` wasn't given enough arguments. Expected (key, value), received ${null==(o=t[0])?void 0:o.toString()}`);const[i,s]=t;return{matched:g$1(e,(t,e)=>{const r=o$2(i,e,n),o=o$2(s,t,n);return r&&o}),selections:r}},getSelectionKeys:()=>0===t.length?[]:[...s$1(t[0]),...s$1(t[1])]})})},record:function(...t){return a$2({[t$2]:()=>({match:e=>{if(null===e||"object"!=typeof e||Array.isArray(e))return{matched:!1};var r;if(0===t.length)throw new Error(`\`P.record\` wasn't given enough arguments. Expected (value) or (key, value), received ${null==(r=t[0])?void 0:r.toString()}`);let n={};const o=(t,e)=>{n[t]=(n[t]||[]).concat([e])},[i,s]=1===t.length?[A,t[0]]:t;return{matched:m$1(e,(t,e)=>{const r="string"!=typeof t||Number.isNaN(Number(t))?null:Number(t),n=null!==r&&o$2(i,r,o),a=o$2(i,t,o),h=o$2(s,e,o);return(a||n)&&h}),selections:n}},getSelectionKeys:()=>0===t.length?[]:[...s$1(t[0]),...s$1(t[1])]})})},intersection:d$1,union:y$1,not:function(t){return a$2({[t$2]:()=>({match:e=>({matched:!o$2(t,e,()=>{})}),getSelectionKeys:()=>[],matcherType:"not"})})},when:p$2,select:v$1,any:K,unknown:O,_:E,string:A,number:P,bigint:T,boolean:B,symbol:_,nullish:W,nonNullable:$$1,instanceOf:function(t){return a$2(p$2(function(t){return e=>e instanceof t}(t)))},shape:function(t){return a$2(p$2(u$2(t)))}};class I extends Error{constructor(t){let e;try{e=JSON.stringify(t)}catch(r){e=t}super(`Pattern matching error: no pattern matches value ${e}`),this.input=void 0,this.input=t}}const L={matched:!1,value:void 0};function M$2(t){return new R(t,L)}class R{constructor(t,e){this.input=void 0,this.state=void 0,this.input=t,this.state=e}with(...t){if(this.state.matched)return this;const e=t[t.length-1],r=[t[0]];let n;3===t.length&&"function"==typeof t[1]?n=t[1]:t.length>2&&r.push(...t.slice(1,t.length-1));let o=!1,i={};const s=(t,e)=>{o=!0,i[t]=e},a=!r.some(t=>o$2(t,this.input,s))||n&&!Boolean(n(this.input))?L:{matched:!0,value:e(o?n$2 in i?i[n$2]:i:this.input,this.input)};return new R(this.input,a)}when(t,e){if(this.state.matched)return this;const r=Boolean(t(this.input));return new R(this.input,r?{matched:!0,value:e(this.input,this.input)}:L)}otherwise(t){return this.state.matched?this.state.value:t(this.input)}exhaustive(t=F){return this.state.matched?this.state.value:t(this.input)}run(){return this.exhaustive()}returnType(){return this}narrow(){return this}}function F(t){throw new I(t)}var index_umd$1={exports:{}},index_umd=index_umd$1.exports,exports$1;exports$1=index_umd$1.exports,function(t){function e(t,e,r,n,o){const i=Math.abs(r-t),s=t<r?1:-1,a=-Math.abs(n-e),h=e<n?1:-1;let l,c=i+a;for(;;){if(o(t,e),l=2*c,l>=a){if(t===r)break;c+=a,t+=s}if(l<=i){if(e===n)break;c+=i,e+=h}}}function r(t,e,r,n,o){const i=t<r?1:-1,s=e<n?1:-1;let a,h=Math.abs(r-t),l=Math.abs(n-e),c=h*h+l*l,u=0===c?1:1/Math.sqrt(c);for(h*=u,l*=u,c=h-l;;){if(o(t,e,Math.abs(c-(h+l))>>16),u=c,a=t,2*u>=-h){if(t===r)break;u+l<16711680&&o(t,e+s,u+l>>16),c-=l,t+=i}if(2*u<=l){if(e===n)break;h-u<16711680&&o(a+i,e,h-u>>16),c+=h,e+=s}}}function n(t,e,r,n,o,i){let s,a,h,l=Math.abs(r-t),c=t<r?1:-1,u=Math.abs(n-e),f=e<n?1:-1,d=l-u,g=l+u==0?1:Math.sqrt(l*l+u*u);for(o=(o+1)/2;;){if(i(t,e,Math.max(0,255*(Math.abs(d-l+u)/g-o+1))),s=d,a=t,2*s>=-l){for(s+=u,h=e;s<g*o&&(n!=h||l>u);s+=l)i(t,h+=f,Math.max(0,255*(Math.abs(s)/g-o+1)));if(t==r)break;s=d,d-=u,t+=c}if(2*s<=u){for(s=l-s;s<g*o&&(r!=a||l<u);s+=u)i(a+=c,e,Math.max(0,255*(Math.abs(s)/g-o+1)));if(e==n)break;d+=l,e+=f}}}function o(t,e="assert error"){if(!t)throw new Error(e)}function i(t,e,r,n,i,a,h,l){var c,u,f,d=t-2*r+i,g=e-2*n+a,m=t-r,p=e-n;o(h>=0,"width is negative"),m*(i-r)>0&&(p*(a-n)>0&&Math.abs(m*g)>Math.abs(p*d)&&(t=i,i=m+r,e=a,a=p+n),t==i||1==h?u=(t-r)/d:(f=Math.sqrt(4*h*h*(t-r)*(i-r)+(i-t)*(i-t)),r<t&&(f=-f),u=(2*h*(t-r)-t+i+f)/(2*(1-h)*(i-t))),m=(u*u*(t-2*h*r+i)+2*u*(h*r-t)+t)*(f=1/(2*u*(1-u)*(h-1)+1)),p=(u*u*(e-2*h*n+a)+2*u*(h*n-e)+e)*f,c=u*(h-1)+1,c*=c*f,h=((1-u)*(h-1)+1)*Math.sqrt(f),d=Math.floor(m+.5),g=Math.floor(p+.5),p=(m-t)*(n-e)/(r-t)+e,s(t,e,d,Math.floor(p+.5),d,g,c,l),p=(m-i)*(n-a)/(r-i)+a,n=Math.floor(p+.5),t=r=d,e=g),(e-n)*(a-n)>0&&(e==a||1==h?u=(e-n)/(e-2*n+a):(f=Math.sqrt(4*h*h*(e-n)*(a-n)+(a-e)*(a-e)),n<e&&(f=-f),u=(2*h*(e-n)-e+a+f)/(2*(1-h)*(a-e))),m=(u*u*(t-2*h*r+i)+2*u*(h*r-t)+t)*(f=1/(2*u*(1-u)*(h-1)+1)),p=(u*u*(e-2*h*n+a)+2*u*(h*n-e)+e)*f,c=u*(h-1)+1,c*=c*f,h=((1-u)*(h-1)+1)*Math.sqrt(f),d=Math.floor(m+.5),g=Math.floor(p+.5),m=(r-t)*(p-e)/(n-e)+t,s(t,e,Math.floor(m+.5),g,d,g,c,l),m=(r-i)*(p-a)/(n-a)+i,r=Math.floor(m+.5),t=d,e=n=g),s(t,e,r,n,i,a,h*h,l)}function s(t,r,n,i,a,h,l,c){var u,f=a-n,d=h-i,g=t-a,m=r-h,p=t-n,w=r-i,y=p*d+w*f,b=p*d-w*f;if(o(p*f<=0&&w*d<=0,"sign of gradient must not change"),0!=b&&l>0){if(f*f+d*d>p*p+w*w&&(a=t,t-=g,h=r,r-=m,b=-b),p=2*(4*l*f*p+g*g),w=2*(4*l*d*w+m*m),y=-2*(f=t<a?1:-1)*(d=r<h?1:-1)*(2*l*y+g*m),b*f*d<0&&(p=-p,w=-w,y=-y,b=-b),g=4*l*(n-t)*d*b+p/2+y,m=4*l*(r-i)*f*b+w/2+y,l<.5&&(m>y||g<y))return b=(l+1)/2,y=1/((l=Math.sqrt(l))+1),f=Math.floor((t+2*l*n+a)*y/2+.5),d=Math.floor((r+2*l*i+h)*y/2+.5),s(t,r,g=Math.floor((l*n+t)*y+.5),m=Math.floor((i*l+r)*y+.5),f,d,b,c),void s(f,d,g=Math.floor((l*n+a)*y+.5),m=Math.floor((i*l+h)*y+.5),a,h,b,c);u=g+m-y;do{if(c(t,r),t==a&&r==h)return;n=2*u>m?1:0,i=2*(u+w)<-m?1:0,(2*u<g||i)&&(r+=d,m+=y,u+=g+=p),(2*u>g||n)&&(t+=f,g+=y,u+=m+=w)}while(m<=y&&g>=y)}e(t,r,a,h,c)}function a(t,e,n,i,s,h,l,c){var u,f,d,g=s-n,m=h-i,p=t-s,w=e-h,y=t-n,b=e-i,x=y*m+b*g,M=y*m-b*g;if(o(y*g<=0&&b*m<=0),0!=M&&l>0){if(g*g+m*m>y*y+b*b&&(s=t,t-=p,h=e,e-=w,M=-M),y=2*(4*l*g*y+p*p),b=2*(4*l*m*b+w*w),x=-2*(g=t<s?1:-1)*(m=e<h?1:-1)*(2*l*x+p*w),M*g*m<0&&(y=-y,b=-b,M=-M,x=-x),p=4*l*(n-t)*m*M+y/2+x,w=4*l*(e-i)*g*M+b/2+x,l<.5&&w>p)return M=(l+1)/2,x=1/((l=Math.sqrt(l))+1),g=Math.floor((t+2*l*n+s)*x/2+.5),m=Math.floor((e+2*l*i+h)*x/2+.5),a(t,e,p=Math.floor((l*n+t)*x+.5),w=Math.floor((i*l+e)*x+.5),g,m,M,c),a(g,m,p=Math.floor((l*n+s)*x+.5),w=Math.floor((i*l+h)*x+.5),s,h,M,c);u=p+w-x;do{if(M=Math.min(p-x,x-w),f=Math.max(p-x,x-w),f+=2*f*M*M/(4*f*f+M*M),(n=255*Math.abs(u-p-w+x)/f)<256&&c(t,e,n),d=2*u+w<0){if(e==h)return;p-u<f&&c(t+g,e,255*Math.abs(p-u)/f)}if(2*u+p>0){if(t==s)return;u-w<f&&c(t,e+m,255*Math.abs(u-w)/f),t+=g,p+=x,u+=w+=b}d&&(e+=m,w+=x,u+=p+=y)}while(w<p)}r(t,e,s,h,c)}function h(t,e,r,n,o){let i=-r,s=0,a=n*n,h=i*(2*a+i)+a;do{o(t-i,e+s),o(t+i,e+s),o(t+i,e-s),o(t-i,e-s),a=2*h,a>=(2*i+1)*n*n&&(h+=(2*++i+1)*n*n),a<=(2*s+1)*r*r&&(h+=(2*++s+1)*r*r)}while(i<=0);for(;s++<n;)o(t,e+s),o(t,e-s)}function l(t,e,r,n,o,i){let s=r*r,a=n*n;const h=Math.sin(o);let l=(s-a)*h;s=Math.sqrt(s-l*h),a=Math.sqrt(a+l*h),l=l*(r=s+.5)*(n=a+.5)/(s*a),c(t-r,e-n,t+r,e+n,4*l*Math.cos(o),i)}function c(t,e,r,n,i,a){let h=r-t,l=n-e,c=h*l;if(0===i)return u(t,e,r,n,a);0!==c&&(c=(c-i)/(c+c)),o(c<=1&&c>=0,"limit angle to |zd|<=xd*yd"),h=Math.floor(h*c+.5),l=Math.floor(l*c+.5),s(t,e+l,t,e,t+h,e,1-c,a),s(t,e+l,t,n,r-h,n,c,a),s(r,n-l,r,n,r-h,n,1-c,a),s(r,n-l,r,e,t+h,e,c,a)}function u(t,e,r,n,o){let i,s=Math.abs(r-t),a=Math.abs(n-e),h=1&a,l=4*(1-s)*a*a,c=4*(h+1)*s*s,u=l+c+h*s*s;t>r&&(t=r,r+=s),e>n&&(e=n),n=(e+=(a+1)/2)-h,s*=8*s,h=8*a*a;do{o(r,e),o(t,e),o(t,n),o(r,n),i=2*u,i<=c&&(e++,n--,u+=c+=s),(i>=l||2*u>c)&&(t++,r--,u+=l+=h)}while(t<=r);for(;e-n<=a;)o(t-1,e),o(r+1,e++),o(t-1,n),o(r+1,n--)}function f(t,e,r,n){var o=-r,i=0,s=2-2*r;do{n(t-o,e+i),n(t-i,e-o),n(t+o,e-i),n(t+i,e+o),(r=s)<=i&&(s+=2*++i+1),(r>o||s>i)&&(s+=2*++o+1)}while(o<0)}function d(t,e,r,n){var o,i,s,a=-r,h=0,l=2-2*r;r=1-l;do{n(t-a,e+h,o=255*Math.abs(l-2*(a+h)-2)/r),n(t-h,e-a,o),n(t+a,e-h,o),n(t+h,e+a,o),s=l,i=a,l+h>0&&((o=255*(l-2*a-1)/r)<256&&(n(t-a,e+h+1,o),n(t-h-1,e-a,o),n(t+a,e-h-1,o),n(t+h+1,e+a,o)),l+=2*++a+1),s+i<=0&&((o=255*(2*h+3-s)/r)<256&&(n(t-i-1,e+h,o),n(t-h,e-i-1,o),n(t+i+1,e-h,o),n(t+h,e+i+1,o)),l+=2*++h+1)}while(a<0)}function g(t,e,r,n,o,i,s){var a,h=t-r,l=e-n,c=t-2*r+o;h*(o-r)>0&&(l*(i-n)>0&&Math.abs((e-2*n+i)/c*h)>Math.abs(l)&&(t=o,o=h+r,e=i,i=l+n),a=(1-(c=(t-r)/c))*((1-c)*e+2*c*n)+c*c*i,c=(t*o-r*r)*c/(t-r),h=Math.floor(c+.5),l=Math.floor(a+.5),a=(n-e)*(c-t)/(r-t)+e,m(t,e,h,Math.floor(a+.5),h,l,s),a=(n-i)*(c-o)/(r-o)+i,t=r=h,e=l,n=Math.floor(a+.5)),(e-n)*(i-n)>0&&(a=(1-(c=(e-n)/(c=e-2*n+i)))*((1-c)*t+2*c*r)+c*c*o,c=(e*i-n*n)*c/(e-n),h=Math.floor(a+.5),l=Math.floor(c+.5),a=(r-t)*(c-e)/(n-e)+t,m(t,e,Math.floor(a+.5),l,h,l,s),a=(r-o)*(c-i)/(n-i)+o,t=h,r=Math.floor(a+.5),e=n=l),m(t,e,r,n,o,i,s)}function m(t,r,n,i,s,a,h){var l,c,u,f,d=s-n,g=a-i,m=t-n,p=r-i,w=m*g-p*d;if(o(m*d<=0&&p*g<=0,"sign of gradient must not change"),d*d+g*g>m*m+p*p&&(s=t,t=d+n,a=r,r=g+i,w=-w),0!=w){m+=d,p+=g,l=2*(m*=d=t<s?1:-1)*(p*=g=r<a?1:-1),m*=m,p*=p,w*d*g<0&&(m=-m,p=-p,l=-l,w=-w),c=4*g*w*(n-t)+m-l,u=4*d*w*(r-i)+p-l,m+=m,p+=p,f=c+u+l;do{if(h(t,r),t==s&&r==a)return;i=+(2*f<c),2*f>u&&(t+=d,c-=l,f+=u+=p),i&&(r+=g,u-=l,f+=c+=m)}while(u<0&&c>0)}e(t,r,s,a,h)}function p(t,e,r,n,o,i,s){var a,h=t-r,l=e-n,c=t-2*r+o;h*(o-r)>0&&(l*(i-n)>0&&Math.abs((e-2*n+i)/c*h)>Math.abs(l)&&(t=o,o=h+r,e=i,i=l+n),a=(1-(c=(t-r)/c))*((1-c)*e+2*c*n)+c*c*i,c=(t*o-r*r)*c/(t-r),h=Math.floor(c+.5),l=Math.floor(a+.5),a=(n-e)*(c-t)/(r-t)+e,w(t,e,h,Math.floor(a+.5),h,l,s),a=(n-i)*(c-o)/(r-o)+i,t=r=h,e=l,n=Math.floor(a+.5)),(e-n)*(i-n)>0&&(a=(1-(c=(e-n)/(c=e-2*n+i)))*((1-c)*t+2*c*r)+c*c*o,c=(e*i-n*n)*c/(e-n),h=Math.floor(a+.5),l=Math.floor(c+.5),a=(r-t)*(c-e)/(n-e)+t,w(t,e,Math.floor(a+.5),l,h,l,s),a=(r-o)*(c-i)/(n-i)+o,t=h,r=Math.floor(a+.5),e=n=l),w(t,e,r,n,o,i,s)}function w(t,e,n,o,i,s,a){var h,l,c,u,f,d=i-n,g=s-o,m=t-n,p=e-o,w=m*g-p*d;if(d*d+g*g>m*m+p*p&&(i=t,t=d+n,s=e,e=g+o,w=-w),0!=w){m+=d,p+=g,h=2*(m*=d=t<i?1:-1)*(p*=g=e<s?1:-1),m*=m,p*=p,w*d*g<0&&(m=-m,p=-p,h=-h,w=-w),l=4*g*(n-t)*w+m-h,c=4*d*(e-o)*w+p-h,m+=m,p+=p,u=l+c+h;do{if(w=Math.min(l+h,-h-c),f=Math.max(l+h,-h-c),f+=2*f*w*w/(4*f*f+w*w),a(t,e,255*Math.abs(u-l-c-h)/f),t==i||e==s)break;n=t,w=l-u,o=+(2*u+c<0),2*u+l>0&&(u-c<f&&a(t,e+g,255*Math.abs(u-c)/f),t+=d,l-=h,u+=c+=p),o&&(w<f&&a(n+d,e,255*Math.abs(w)/f),e+=g,c-=h,u+=l+=m)}while(c<l)}r(t,e,i,s,a)}function y(t,r,n,i,s,a,h,l,c){var u,f,d,g=1;let p,w,y,b,x,M,v,I,A,k,R,E=t<h?1:-1,P=r<l?1:-1,S=-Math.abs(t+n-s-h),$=S-4*E*(n-s),C=E*(t-n-s+h),B=-Math.abs(r+i-a-l),T=B-4*P*(i-a),D=P*(r-i-a+l),_=.01;if(o((n-t)*(s-h)<_&&((h-t)*(n-s)<_||C*C<$*S+_),"slope change"),o((i-r)*(a-l)<_&&((l-r)*(i-a)<_||D*D<T*B+_),"slope change"),0==$&&0==T)return E=Math.floor((3*n-t+1)/2),P=Math.floor((3*i-r+1)/2),m(t,r,E,P,h,l,c);n=(n-t)*(n-t)+(i-r)*(i-r)+1,s=(s-h)*(s-h)+(a-l)*(a-l)+1;do{p=$*D-C*T,w=$*B-S*T,y=C*B-S*D,k=p*(p+w-3*y)+w*w,p*=u=k>0?1:Math.sqrt(1+1024/n),w*=u,y*=u,k*=u*u,M=9*(p+w+y)/8,b=8*($-T),I=27*(8*p*(D*D-T*B)+k*(T+2*D+B))/64-T*T*(M-T),A=27*(8*p*(C*C-$*S)-k*($+2*C+S))/64-$*$*(M+$),x=3*(3*p*(3*D*D-T*T-2*T*B)-T*(3*w*(T+D)+T*b))/4,v=3*(3*p*(3*C*C-$*$-2*$*S)-$*(3*w*($+C)+$*b))/4,M=$*T*(6*p+6*w-3*y+b),w=T*T,b=$*$,M=3*(M+9*u*(b*D*B-C*S*w)-18*C*D*p)/8,k<0&&(I=-I,A=-A,x=-x,v=-v,M=-M,w=-w,b=-b),p=6*T*w,w*=-6*$,y=6*T*b,b*=-6*$,I+=M,k=I+A,A+=M;t:for(R=M,f=d=u;t!=h&&r!=l;){c(t,r);do{if(I>R||A<R)break t;i=2*k-A,2*k>=I&&(f--,k+=I+=x,A+=M+=w,v+=y,x+=p),i<=0&&(d--,k+=A+=v,I+=M+=y,x+=w,v+=b)}while(f>0&&d>0);2*f<=u&&(t+=E,f+=u),2*d<=u&&(r+=P,d+=u),R==M&&I<0&&A>0&&(R=_)}x=t,t=h,h=x,E=-E,C=-C,v=r,r=l,l=v,P=-P,D=-D,n=s}while(g--);e(t,r,h,l,c)}function b(t,e,n,i,s,a,h,l,c){let u,f,d,g,m,p,y,b,x,M,v,I,A,k,R,E,P,S=1,$=t<h?1:-1,C=e<l?1:-1,B=-Math.abs(t+n-s-h),T=B-4*$*(n-s),D=$*(t-n-s+h),_=-Math.abs(e+i-a-l),O=_-4*C*(i-a),V=C*(e-i-a+l);const N=.01;if(o((n-t)*(s-h)<N&&((h-t)*(n-s)<N||D*D<T*B+N)),o((i-e)*(a-l)<N&&((l-e)*(i-a)<N||V*V<O*_+N)),0===T&&0===O)return $=Math.floor((3*n-t+1)/2),C=Math.floor((3*i-e+1)/2),w(t,e,$,C,h,l,c);n=(n-t)*(n-t)+(i-e)*(i-e)+1,s=(s-h)*(s-h)+(a-l)*(a-l)+1;do{g=T*V-D*O,m=T*_-B*O,p=D*_-B*V,P=4*g*p-m*m,A=g*(g+m-3*p)+m*m,u=A>0?1:Math.sqrt(1+1024/n),g*=u,m*=u,p*=u,A*=u*u,x=9*(g+m+p)/8,y=8*(T-O),v=27*(8*g*(V*V-O*_)+A*(O+2*V+_))/64-O*O*(x-O),I=27*(8*g*(D*D-T*B)-A*(T+2*D+B))/64-T*T*(x+T),b=3*(3*g*(3*V*V-O*O-2*O*_)-O*(3*m*(O+V)+O*y))/4,M=3*(3*g*(3*D*D-T*T-2*T*B)-T*(3*m*(T+D)+T*y))/4,x=T*O*(6*g+6*m-3*p+y),m=O*O,y=T*T,x=3*(x+9*u*(y*V*_-D*B*m)-18*D*V*g)/8,A<0&&(v=-v,I=-I,b=-b,M=-M,x=-x,m=-m,y=-y),g=6*O*m,m*=-6*T,p=6*O*y,y*=-6*T,v+=x,A=v+I,I+=x;let r=!1;t:for(f=d=u;t!==h&&e!==l;){i=Math.min(Math.abs(x-v),Math.abs(I-x)),E=Math.max(Math.abs(x-v),Math.abs(I-x)),E=u*(E+2*E*i*i/(4*E*E+i*i)),(i=255*Math.abs(A-(u-f+1)*v-(u-d+1)*I+u*x)/E)<256&&c(t,e,i),k=Math.abs(A-(u-f+1)*v+(d-1)*I),R=Math.abs(A+(f-1)*v-(u-d+1)*I),a=e;do{if(P>=-N&&(v+b>x||I+M<x)){r=!0;break t}if(i=2*A+v,2*A+I>0)f--,A+=v+=b,I+=x+=m,M+=p,b+=g;else if(i>0){r=!0;break t}i<=0&&(d--,A+=I+=M,v+=x+=p,b+=m,M+=y)}while(f>0&&d>0);2*d<=u&&(R<E&&c(t+$,e,255*R/E),e+=C,d+=u),2*f<=u&&(k<E&&c(t,a+C,255*k/E),t+=$,f+=u)}r&&(2*A<I&&2*d<=u+2&&(R<E&&c(t+$,e,255*R/E),e+=C),2*A>v&&2*f<=u+2&&(k<E&&c(t,a+C,255*k/E),t+=$),b=t,t=h,h=b,$=-$,D=-D,M=e,e=l,l=M,C=-C,V=-V,n=s);break}while(S--);r(Math.floor(t),Math.floor(e),Math.floor(h),Math.floor(l),c)}function x(t,e,r,n,o,i,s,a,h,l){let c=0,u=0,f=t+r-o-s,d=f-4*(r-o),g=t-r-o+s,m=g+4*(r+o),p=e+n-i-a,w=p-4*(n-i),y=e-n-i+a,b=y+4*(n+i);var x,M,v,I,A,k,R=t,E=e;let P,S=g*g-d*f;const $=[0,0,0,0,0];for(0==d?Math.abs(f)<2*Math.abs(g)&&($[c++]=f/(2*g)):S>0&&(P=Math.sqrt(S),S=(g-P)/d,Math.abs(S)<1&&($[c++]=S),S=(g+P)/d,Math.abs(S)<1&&($[c++]=S)),S=y*y-w*p,0==w?Math.abs(p)<2*Math.abs(y)&&($[c++]=p/(2*y)):S>0&&(P=Math.sqrt(S),S=(y-P)/w,Math.abs(S)<1&&($[c++]=S),S=(y+P)/w,Math.abs(S)<1&&($[c++]=S)),u=1;u<c;u++)(S=$[u-1])>$[u]&&($[u-1]=$[u],$[u]=S,u=0);for(S=-1,$[c]=1,u=0;u<=c;u++)P=$[u],x=(S*(S*g-2*f)-P*(S*(S*d-2*g)+f)+m)/8-R,I=(S*(S*y-2*p)-P*(S*(S*w-2*y)+p)+b)/8-E,M=(P*(P*g-2*f)-S*(P*(P*d-2*g)+f)+m)/8-R,A=(P*(P*y-2*p)-S*(P*(P*w-2*y)+p)+b)/8-E,R-=v=(P*(P*(3*g-P*d)-3*f)+m)/8,E-=k=(P*(P*(3*y-P*w)-3*p)+b)/8,s=Math.floor(v+.5),a=Math.floor(k+.5),0!=R&&(x*=R=(t-s)/R,M*=R),0!=E&&(I*=E=(e-a)/E,A*=E),(t!=s||e!=a)&&h(t,e,t+x,e+I,t+M,e+A,s,a,l),t=s,e=a,R=v,E=k,S=P}function M(t,e,r,n,o,i,s,a,h){x(t,e,r,n,o,i,s,a,y,h)}function v(t,e,r,n,o,i,s,a,h){x(t,e,r,n,o,i,s,a,b,h)}t.circle=f,t.circleAA=d,t.cubicBezier=M,t.cubicBezierAA=v,t.cubicBezierSegment=y,t.cubicBezierSegmentAA=b,t.ellipse=h,t.ellipseRect=u,t.line=e,t.lineAA=r,t.lineWidth=n,t.quadBezier=g,t.quadBezierAA=p,t.quadBezierSegment=m,t.quadBezierSegmentAA=w,t.quadRationalBezier=i,t.quadRationalBezierSegment=s,t.quadRationalBezierSegmentAA=a,t.rotatedEllipse=l,t.rotatedEllipseRect=c,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})}(exports$1);var index_umdExports=index_umd$1.exports;function deleteDuplicates(t){const e=[];for(let r=0;r<t.length;r++)t[r].column===t[(r+1)%t.length].column&&t[r].row===t[(r+1)%t.length].row||e.push(t[r]);return e}function getCirclePoints(t){const e=[];index_umdExports.circle(t,t,t,(r,n)=>{e.push({row:n-t,column:r-t})});const r=[],n=[],o=[],i=[];for(let t=0;t<e.length;t+=4)r.push(e[t%e.length]),n.push(e[(t+1)%e.length]),o.push(e[(t+2)%e.length]),i.push(e[(t+3)%e.length]);return r.concat(n,o,i)}const zeroPoint={row:0,column:0};function getFilledCirclePoints(t,e=zeroPoint){const r=[];if(0===t)return[e];1===t&&r.push(e),index_umdExports.circle(e.column,e.row,t,(t,n)=>{r.push({row:n,column:t}),t-1>e.column?r.push(...getLinePoints({row:n,column:t-1},{row:n,column:e.column})):t+1<e.column&&r.push(...getLinePoints({row:n,column:t+1},{row:n,column:e.column}))});return deleteDuplicates(sortByColumnRow(r))}function getLinePoints(t,e){const r=[];return index_umdExports.line(t.column,t.row,e.column,e.row,(t,e)=>{r.push({row:e,column:t})}),r}function getCompassPoints(t){return index_umdExports.circle(t,t,t,(t,e)=>{}),[{row:0,column:t},{row:t,column:0},{row:0,column:-t},{row:-t,column:0}]}function getIndex(t,e,r,n=0){return(e*r.width+t)*r.channels+n}const surroundingPixels=[{row:0,column:1},{row:1,column:1},{row:1,column:0},{row:1,column:-1},{row:0,column:-1},{row:-1,column:-1},{row:-1,column:0},{row:-1,column:1}],formatter=new Intl.ListFormat("en",{type:"disjunction"});function checkProcessable$1(t,e={}){let{bitDepth:r,alpha:n,colorModel:o,components:i,channels:s}=e;if(r&&(Array.isArray(r)||(r=[r]),!r.includes(t.bitDepth)))throw new RangeError(`image bitDepth must be ${format(r)} to apply this algorithm`);if(n&&(Array.isArray(n)||(n=[n]),!n.includes(t.alpha)))throw new RangeError(`image alpha must be ${format(n)} to apply this algorithm`);if(o&&(Array.isArray(o)||(o=[o]),!o.includes(t.colorModel)))throw new RangeError(`image colorModel must be ${format(o)} to apply this algorithm`);if(i&&(Array.isArray(i)||(i=[i]),!i.includes(t.components))){const t=`image components must be ${format(i)} to apply this algorithm`;throw 1===i.length&&1===i[0]?new RangeError(`${t}. The image can be converted using "image.grey()"`):new RangeError(t)}if(s&&(Array.isArray(s)||(s=[s]),!s.includes(t.channels)))throw new RangeError(`image channels must be ${format(s)} to apply this algorithm`)}function format(t){return formatter.format(t.map(String))}function getFastScore(t,e,r,n){const o=t.getValueByPoint(e,0);let i=0,s=0;for(const a of n){const n=t.getValue(e.column+a.column,e.row+a.row,0);n>=o+r?i+=Math.abs(n-o)-r:n<=o-r&&(s+=Math.abs(o-n)-r)}return Math.max(i,s)}function checkSize(t){if(!Number.isInteger(t)||t<1)throw new TypeError(`size must be a positive integer. Got ${t}`)}function checkKernel$1(t){if(0===t.length||t.length%2!=1)throw new RangeError(`kernel must have an odd positive length. Got ${t.length}`)}function checkBorderType(t){if("CONSTANT"!==t&&"CUT"!==t)throw new RangeError(`unexpected border type: ${t}`)}function checkInputLength(t,e){if(t!==e)throw new RangeError(`input length (${t}) does not match setup size (${e})`)}function createArray(t){const e=[];for(var r=0;r<t;r++)e.push(0);return e}class DirectConvolution{constructor(t,e,r="CONSTANT"){checkSize(t),checkKernel$1(e),checkBorderType(r),this.size=t,this.kernelOffset=(e.length-1)/2,this.outputSize="CONSTANT"===r?t:t-2*this.kernelOffset,this.output=createArray(this.outputSize),this.kernel=e,this.kernelSize=e.length,this.borderType=r}convolve(t){return checkInputLength(t.length,this.size),this.output.fill(0),"CONSTANT"===this.borderType?this._convolutionBorder0(t):this._convolutionBorderCut(t),this.output}_convolutionBorder0(t){for(let e=0;e<this.size;e++)for(let r=0;r<this.kernelSize;r++)this.output[e]+=interpolateInput(t,e-this.kernelOffset+r)*this.kernel[r]}_convolutionBorderCut(t){for(let e=this.kernelOffset;e<this.size-this.kernelOffset;e++){const r=e-this.kernelOffset;for(let e=0;e<this.kernelSize;e++)this.output[r]+=t[r+e]*this.kernel[e]}}}function interpolateInput(t,e){return e<0||e>=t.length?0:t[e]}function FFT(t){if(this.size=0|t,this.size<=1||this.size&this.size-1)throw new Error("FFT size must be a power of two and bigger than 1");this._csize=t<<1;for(var e=new Array(2*this.size),r=0;r<e.length;r+=2){const t=Math.PI*r/this.size;e[r]=Math.cos(t),e[r+1]=-Math.sin(t)}this.table=e;for(var n=0,o=1;this.size>o;o<<=1)n++;this._width=n%2==0?n-1:n,this._bitrev=new Array(1<<this._width);for(var i=0;i<this._bitrev.length;i++){this._bitrev[i]=0;for(var s=0;s<this._width;s+=2){var a=this._width-s-2;this._bitrev[i]|=(i>>>s&3)<<a}}this._out=null,this._data=null,this._inv=0}FFT.prototype.fromComplexArray=function(t,e){for(var r=e||new Array(t.length>>>1),n=0;n<t.length;n+=2)r[n>>>1]=t[n];return r},FFT.prototype.createComplexArray=function(){const t=new Array(this._csize);for(var e=0;e<t.length;e++)t[e]=0;return t},FFT.prototype.toComplexArray=function(t,e){for(var r=e||this.createComplexArray(),n=0;n<r.length;n+=2)r[n]=t[n>>>1],r[n+1]=0;return r},FFT.prototype.completeSpectrum=function(t){for(var e=this._csize,r=e>>>1,n=2;n<r;n+=2)t[e-n]=t[n],t[e-n+1]=-t[n+1]},FFT.prototype.transform=function(t,e){if(t===e)throw new Error("Input and output buffers must be different");this._out=t,this._data=e,this._inv=0,this._transform4(),this._out=null,this._data=null},FFT.prototype.realTransform=function(t,e){if(t===e)throw new Error("Input and output buffers must be different");this._out=t,this._data=e,this._inv=0,this._realTransform4(),this._out=null,this._data=null},FFT.prototype.inverseTransform=function(t,e){if(t===e)throw new Error("Input and output buffers must be different");this._out=t,this._data=e,this._inv=1,this._transform4();for(var r=0;r<t.length;r++)t[r]/=this.size;this._out=null,this._data=null},FFT.prototype._transform4=function(){var t,e,r=this._out,n=this._csize,o=1<<this._width,i=n/o<<1,s=this._bitrev;if(4===i)for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleTransform2(t,r,o)}else for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleTransform4(t,r,o)}var a=this._inv?-1:1,h=this.table;for(o>>=2;o>=2;o>>=2){var l=(i=n/o<<1)>>>2;for(t=0;t<n;t+=i)for(var c=t+l,u=t,f=0;u<c;u+=2,f+=o){const t=u,e=t+l,n=e+l,o=n+l,i=r[t],s=r[t+1],c=r[e],d=r[e+1],g=r[n],m=r[n+1],p=r[o],w=r[o+1],y=i,b=s,x=h[f],M=a*h[f+1],v=c*x-d*M,I=c*M+d*x,A=h[2*f],k=a*h[2*f+1],R=g*A-m*k,E=g*k+m*A,P=h[3*f],S=a*h[3*f+1],$=p*P-w*S,C=p*S+w*P,B=y+R,T=b+E,D=y-R,_=b-E,O=v+$,V=I+C,N=a*(v-$),F=a*(I-C),L=B+O,U=T+V,z=B-O,G=T-V,q=D+F,j=_-N,Y=D-F,H=_+N;r[t]=L,r[t+1]=U,r[e]=q,r[e+1]=j,r[n]=z,r[n+1]=G,r[o]=Y,r[o+1]=H}}},FFT.prototype._singleTransform2=function(t,e,r){const n=this._out,o=this._data,i=o[e],s=o[e+1],a=o[e+r],h=o[e+r+1],l=i+a,c=s+h,u=i-a,f=s-h;n[t]=l,n[t+1]=c,n[t+2]=u,n[t+3]=f},FFT.prototype._singleTransform4=function(t,e,r){const n=this._out,o=this._data,i=this._inv?-1:1,s=2*r,a=3*r,h=o[e],l=o[e+1],c=o[e+r],u=o[e+r+1],f=o[e+s],d=o[e+s+1],g=o[e+a],m=o[e+a+1],p=h+f,w=l+d,y=h-f,b=l-d,x=c+g,M=u+m,v=i*(c-g),I=i*(u-m),A=p+x,k=w+M,R=y+I,E=b-v,P=p-x,S=w-M,$=y-I,C=b+v;n[t]=A,n[t+1]=k,n[t+2]=R,n[t+3]=E,n[t+4]=P,n[t+5]=S,n[t+6]=$,n[t+7]=C},FFT.prototype._realTransform4=function(){var t,e,r=this._out,n=this._csize,o=1<<this._width,i=n/o<<1,s=this._bitrev;if(4===i)for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleRealTransform2(t,r>>>1,o>>>1)}else for(t=0,e=0;t<n;t+=i,e++){const r=s[e];this._singleRealTransform4(t,r>>>1,o>>>1)}var a=this._inv?-1:1,h=this.table;for(o>>=2;o>=2;o>>=2){var l=(i=n/o<<1)>>>1,c=l>>>1,u=c>>>1;for(t=0;t<n;t+=i)for(var f=0,d=0;f<=u;f+=2,d+=o){var g=t+f,m=g+c,p=m+c,w=p+c,y=r[g],b=r[g+1],x=r[m],M=r[m+1],v=r[p],I=r[p+1],A=r[w],k=r[w+1],R=y,E=b,P=h[d],S=a*h[d+1],$=x*P-M*S,C=x*S+M*P,B=h[2*d],T=a*h[2*d+1],D=v*B-I*T,_=v*T+I*B,O=h[3*d],V=a*h[3*d+1],N=A*O-k*V,F=A*V+k*O,L=R+D,U=E+_,z=R-D,G=E-_,q=$+N,j=C+F,Y=a*($-N),H=a*(C-F),W=L+q,K=U+j,X=z+H,J=G-Y;if(r[g]=W,r[g+1]=K,r[m]=X,r[m+1]=J,0!==f){if(f!==u){var Q=z+-a*H,Z=-G+-a*Y,tt=L+-a*q,et=-U- -a*j,rt=t+c-f,nt=t+l-f;r[rt]=Q,r[rt+1]=Z,r[nt]=tt,r[nt+1]=et}}else{var ot=L-q,it=U-j;r[p]=ot,r[p+1]=it}}}},FFT.prototype._singleRealTransform2=function(t,e,r){const n=this._out,o=this._data,i=o[e],s=o[e+r],a=i+s,h=i-s;n[t]=a,n[t+1]=0,n[t+2]=h,n[t+3]=0},FFT.prototype._singleRealTransform4=function(t,e,r){const n=this._out,o=this._data,i=this._inv?-1:1,s=2*r,a=3*r,h=o[e],l=o[e+r],c=o[e+s],u=o[e+a],f=h+c,d=h-c,g=l+u,m=i*(l-u),p=f+g,w=d,y=-m,b=f-g,x=d,M=m;n[t]=p,n[t+1]=0,n[t+2]=w,n[t+3]=y,n[t+4]=b,n[t+5]=0,n[t+6]=x,n[t+7]=M};const BorderType$1={CUT:"CUT"};function computeRmse(t,e){const r=computeMse(t,e);return Math.sqrt(r)}function computeMse(t,e){const r=t.subtract(e,{absolute:!0});let n=0;for(let e=0;e<t.size;e++)for(let o=0;o<t.channels;o++){n+=r.getValueByIndex(e,o)**2}return n/(t.size*t.channels)}function computePsnr(t,e){const r=computeRmse(t,e);return 20*Math.log10(t.maxValue/(r+Number.MIN_VALUE))}function validateChannels(t,e){for(const r of t)validateChannel(r,e)}function validateChannel(t,e){if(!Number.isInteger(t)||t>=e.channels||t<0)throw new RangeError(`invalid channel: ${t}. It must be a positive integer smaller than ${e.channels}`)}function validateValues(t,e){for(const r of t)validateValue(r,e)}function validateValue(t,e){if(t>e.maxValue||t<0)throw new RangeError(`invalid value: ${t}. It must be a positive value smaller than ${e.maxValue+1}`)}function validateForComparison(t,e){if(t.width!==e.width||t.height!==e.height)throw new RangeError("both images must have the same size");if(t.alpha!==e.alpha||t.bitDepth!==e.bitDepth)throw new RangeError("both images must have the same alpha and bitDepth");if(t.channels!==e.channels)throw new RangeError("both images must have the same number of channels")}function validateColor(t,e){validateChannel(t.length-1,e),validateValues(t,e)}function subtract(t,e,r={}){const{absolute:n=!1}=r;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:[1,3],alpha:!1}),validateForComparison(t,e);const o=t.clone();if(o instanceof Image)for(let r=0;r<t.size;r++)for(let i=0;i<t.channels;i++){const s=t.getValueByIndex(r,i)-e.getValueByIndex(r,i);n?s<0?o.setValueByIndex(r,i,-s):o.setValueByIndex(r,i,s):s<0?o.setValueByIndex(r,i,0):o.setValueByIndex(r,i,s)}else if(t instanceof Mask&&e instanceof Mask)for(let r=0;r<t.size;r++){const i=t.getBitByIndex(r)-e.getBitByIndex(r);n?o.setBitByIndex(r,i?1:0):o.setBitByIndex(r,i>0?1:0)}return o}function assert(t,e){if(!t)throw new Error(e||"unreachable")}function assertUnreachable(t){throw new Error(`unreachable: ${String(t)}`)}function getClamp(t){return 255===t.maxValue?clamp255:(assert(65535===t.maxValue),clamp65535)}function clamp255(t){return Math.min(Math.max(t,0),255)}function clamp65535(t){return Math.min(Math.max(t,0),65535)}function getClampFromTo(t,e){return function(r){return Math.max(Math.min(r,e),t)}}function add(t,e){t instanceof Image&&checkProcessable$1(t,{bitDepth:[8,16],components:[1,3],alpha:!1}),validateForComparison(t,e);const r=t.clone(),n=getClamp(t);for(let o=0;o<t.size;o++)for(let i=0;i<t.channels;i++){const s=t.getValueByIndex(o,i)+e.getValueByIndex(o,i);r.setValueByIndex(o,i,n(s))}return r}function multiply(t,e,r={}){const{channels:n=new Array(t.channels).fill(0).map((t,e)=>e)}=r;validateChannels(n,t);const o=getOutputImage(t,r,{clone:!0});if(0===n.length)return o;for(const t of n)for(let r=0;r<o.height;r++)for(let n=0;n<o.width;n++){const i=o.getValue(n,r,t)*e;o.setClampedValue(n,r,t,i)}return o}function getDefaultColor(t){return M$2(t.colorModel).with("GREY",()=>[0]).with("GREYA",()=>[0,t.maxValue]).with("RGB",()=>[0,0,0]).with("RGBA",()=>[0,0,0,t.maxValue]).with("BINARY",()=>[1]).exhaustive()}function setBlendedPixel(t,e,r,n){if(n=n??getDefaultColor(t),t.alpha){assert(t instanceof Image);const o=n.at(-1);if(o===t.maxValue)return void t.setPixel(e,r,n);const i=t.getValue(e,r,t.channels-1),s=o+i*(1-o/t.maxValue);t.setValue(e,r,t.channels-1,s);for(let a=0;a<t.components;a++){const h=(n[a]*o+t.getValue(e,r,a)*i*(1-o/t.maxValue))/s;t.setValue(e,r,a,h)}}else t.setPixel(e,r,n)}function setBlendedVisiblePixel(t,e,r,n){e>=0&&e<t.width&&r>=0&&r<t.height&&setBlendedPixel(t,e,r,n)}function roundPoint(t){return{row:Math.round(t.row),column:Math.round(t.column)}}function drawCircleOnImage(t,e,r,n={}){const o=getOutputImage(t,n,{clone:!0}),{strokeColor:i=getDefaultColor(o),fillColor:s}=n;if(validateColor(i,o),checkProcessable$1(o,{bitDepth:[8,16]}),r<0)throw new RangeError("circle radius must be positive");if(e=roundPoint(e),0===(r=Math.round(r)))return setBlendedVisiblePixel(o,e.column,e.row,i),o;if(s){1===r&&setBlendedVisiblePixel(o,e.column,e.row,s);let t=e.row+r,n=0;index_umdExports.circle(e.column,e.row,r,(r,a)=>{setBlendedVisiblePixel(o,r,a,i),0===n&&o.drawLine({row:a,column:r-1},{row:a,column:e.column-(r-e.column-1)},{strokeColor:s,out:o}),n%4==1&&t!==a&&(o.drawLine({row:a,column:r+1},{row:a,column:e.column-(r-e.column+1)},{strokeColor:s,out:o}),t=a,o.drawLine({row:e.row-(a-e.row),column:r+1},{row:e.row-(a-e.row),column:e.column-(r-e.column+1)},{strokeColor:s,out:o})),n++})}else index_umdExports.circle(e.column,e.row,r,(t,e)=>{setBlendedVisiblePixel(o,t,e,i)});return o}function drawLineOnImage(t,e,r,n={}){const o=getOutputImage(t,n,{clone:!0}),{strokeColor:i=getDefaultColor(o),origin:s={column:0,row:0}}=n;return validateColor(i,o),checkProcessable$1(o,{bitDepth:[8,16]}),index_umdExports.line(Math.round(s.column+e.column),Math.round(s.row+e.row),Math.round(s.column+r.column),Math.round(s.row+r.row),(t,e)=>{setBlendedVisiblePixel(o,t,e,i)}),o}function drawLineOnMask(t,e,r,n={}){const{origin:o={column:0,row:0}}=n,i=maskToOutputMask(t,n,{clone:!0});return index_umdExports.line(Math.round(o.column+e.column),Math.round(o.row+e.row),Math.round(o.column+r.column),Math.round(o.row+r.row),(t,e)=>{i.setVisiblePixel(t,e,[1])}),i}function drawPolylineOnImage(t,e,r={}){const{strokeColor:n=getDefaultColor(t),origin:o={column:0,row:0}}=r;checkProcessable$1(t,{bitDepth:[8,16]});const i=getOutputImage(t,r,{clone:!0});validateColor(n,i);for(let t=0;t<e.length-1;t++){const r=e[t],s=e[t+1];i.drawLine(r,s,{out:i,strokeColor:n,origin:o})}return i}function drawPolylineOnMask(t,e,r={}){const n=maskToOutputMask(t,r,{clone:!0});for(let t=0;t<e.length-1;t++){const o=e[t],i=e[t+1];n.drawLine(o,i,{out:n,origin:r.origin})}return n}var orientation={exports:{}},twoProduct_1=twoProduct$1,SPLITTER=+(Math.pow(2,27)+1);function twoProduct$1(t,e,r){var n=t*e,o=SPLITTER*t,i=o-(o-t),s=t-i,a=SPLITTER*e,h=a-(a-e),l=e-h,c=s*l-(n-i*h-s*h-i*l);return r?(r[0]=c,r[1]=n,r):[c,n]}var robustSum=linearExpansionSum;function scalarScalar$1(t,e){var r=t+e,n=r-t,o=t-(r-n)+(e-n);return o?[o,r]:[r]}function linearExpansionSum(t,e){var r=0|t.length,n=0|e.length;if(1===r&&1===n)return scalarScalar$1(t[0],e[0]);var o,i,s=new Array(r+n),a=0,h=0,l=0,c=Math.abs,u=t[h],f=c(u),d=e[l],g=c(d);f<g?(i=u,(h+=1)<r&&(f=c(u=t[h]))):(i=d,(l+=1)<n&&(g=c(d=e[l]))),h<r&&f<g||l>=n?(o=u,(h+=1)<r&&(f=c(u=t[h]))):(o=d,(l+=1)<n&&(g=c(d=e[l])));for(var m,p,w=o+i,y=w-o,b=i-y,x=b,M=w;h<r&&l<n;)f<g?(o=u,(h+=1)<r&&(f=c(u=t[h]))):(o=d,(l+=1)<n&&(g=c(d=e[l]))),(b=(i=x)-(y=(w=o+i)-o))&&(s[a++]=b),x=M-((m=M+w)-(p=m-M))+(w-p),M=m;for(;h<r;)(b=(i=x)-(y=(w=(o=u)+i)-o))&&(s[a++]=b),x=M-((m=M+w)-(p=m-M))+(w-p),M=m,(h+=1)<r&&(u=t[h]);for(;l<n;)(b=(i=x)-(y=(w=(o=d)+i)-o))&&(s[a++]=b),x=M-((m=M+w)-(p=m-M))+(w-p),M=m,(l+=1)<n&&(d=e[l]);return x&&(s[a++]=x),M&&(s[a++]=M),a||(s[a++]=0),s.length=a,s}var twoSum$1=fastTwoSum;function fastTwoSum(t,e,r){var n=t+e,o=n-t,i=e-o,s=t-(n-o);return r?(r[0]=s+i,r[1]=n,r):[s+i,n]}var twoProduct=twoProduct_1,twoSum=twoSum$1,robustScale=scaleLinearExpansion;function scaleLinearExpansion(t,e){var r=t.length;if(1===r){var n=twoProduct(t[0],e);return n[0]?n:[n[1]]}var o=new Array(2*r),i=[.1,.1],s=[.1,.1],a=0;twoProduct(t[0],e,i),i[0]&&(o[a++]=i[0]);for(var h=1;h<r;++h){twoProduct(t[h],e,s);var l=i[1];twoSum(l,s[0],i),i[0]&&(o[a++]=i[0]);var c=s[1],u=i[1],f=c+u,d=u-(f-c);i[1]=f,d&&(o[a++]=d)}return i[1]&&(o[a++]=i[1]),0===a&&(o[a++]=0),o.length=a,o}var robustDiff=robustSubtract;function scalarScalar(t,e){var r=t+e,n=r-t,o=t-(r-n)+(e-n);return o?[o,r]:[r]}function robustSubtract(t,e){var r=0|t.length,n=0|e.length;if(1===r&&1===n)return scalarScalar(t[0],-e[0]);var o,i,s=new Array(r+n),a=0,h=0,l=0,c=Math.abs,u=t[h],f=c(u),d=-e[l],g=c(d);f<g?(i=u,(h+=1)<r&&(f=c(u=t[h]))):(i=d,(l+=1)<n&&(g=c(d=-e[l]))),h<r&&f<g||l>=n?(o=u,(h+=1)<r&&(f=c(u=t[h]))):(o=d,(l+=1)<n&&(g=c(d=-e[l])));for(var m,p,w=o+i,y=w-o,b=i-y,x=b,M=w;h<r&&l<n;)f<g?(o=u,(h+=1)<r&&(f=c(u=t[h]))):(o=d,(l+=1)<n&&(g=c(d=-e[l]))),(b=(i=x)-(y=(w=o+i)-o))&&(s[a++]=b),x=M-((m=M+w)-(p=m-M))+(w-p),M=m;for(;h<r;)(b=(i=x)-(y=(w=(o=u)+i)-o))&&(s[a++]=b),x=M-((m=M+w)-(p=m-M))+(w-p),M=m,(h+=1)<r&&(u=t[h]);for(;l<n;)(b=(i=x)-(y=(w=(o=d)+i)-o))&&(s[a++]=b),x=M-((m=M+w)-(p=m-M))+(w-p),M=m,(l+=1)<n&&(d=-e[l]);return x&&(s[a++]=x),M&&(s[a++]=M),a||(s[a++]=0),s.length=a,s}!function(t){var e=twoProduct_1,r=robustSum,n=robustScale,o=robustDiff;function i(t,e,r,n){return function(r,o,i){var s=t(t(e(o[1],i[0]),e(-i[1],o[0])),t(e(r[1],o[0]),e(-o[1],r[0]))),a=t(e(r[1],i[0]),e(-i[1],r[0])),h=n(s,a);return h[h.length-1]}}function s(t,e,r,n){return function(o,i,s,a){var h=t(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),i[2]),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),a[2]))),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),a[2])))),l=t(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),a[2]))),t(r(t(e(i[1],s[0]),e(-s[1],i[0])),o[2]),t(r(t(e(o[1],s[0]),e(-s[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),s[2])))),c=n(h,l);return c[c.length-1]}}function a(t,e,r,n){return function(o,i,s,a,h){var l=t(t(t(r(t(r(t(e(a[1],h[0]),e(-h[1],a[0])),s[2]),t(r(t(e(s[1],h[0]),e(-h[1],s[0])),-a[2]),r(t(e(s[1],a[0]),e(-a[1],s[0])),h[2]))),i[3]),t(r(t(r(t(e(a[1],h[0]),e(-h[1],a[0])),i[2]),t(r(t(e(i[1],h[0]),e(-h[1],i[0])),-a[2]),r(t(e(i[1],a[0]),e(-a[1],i[0])),h[2]))),-s[3]),r(t(r(t(e(s[1],h[0]),e(-h[1],s[0])),i[2]),t(r(t(e(i[1],h[0]),e(-h[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),h[2]))),a[3]))),t(r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),i[2]),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),a[2]))),-h[3]),t(r(t(r(t(e(a[1],h[0]),e(-h[1],a[0])),i[2]),t(r(t(e(i[1],h[0]),e(-h[1],i[0])),-a[2]),r(t(e(i[1],a[0]),e(-a[1],i[0])),h[2]))),o[3]),r(t(r(t(e(a[1],h[0]),e(-h[1],a[0])),o[2]),t(r(t(e(o[1],h[0]),e(-h[1],o[0])),-a[2]),r(t(e(o[1],a[0]),e(-a[1],o[0])),h[2]))),-i[3])))),t(t(r(t(r(t(e(i[1],h[0]),e(-h[1],i[0])),o[2]),t(r(t(e(o[1],h[0]),e(-h[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),h[2]))),a[3]),t(r(t(r(t(e(i[1],a[0]),e(-a[1],i[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),a[2]))),-h[3]),r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),i[2]),t(r(t(e(i[1],a[0]),e(-a[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),a[2]))),o[3]))),t(r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),a[2]))),-i[3]),t(r(t(r(t(e(i[1],a[0]),e(-a[1],i[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),a[2]))),s[3]),r(t(r(t(e(i[1],s[0]),e(-s[1],i[0])),o[2]),t(r(t(e(o[1],s[0]),e(-s[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),s[2]))),-a[3]))))),c=t(t(t(r(t(r(t(e(a[1],h[0]),e(-h[1],a[0])),s[2]),t(r(t(e(s[1],h[0]),e(-h[1],s[0])),-a[2]),r(t(e(s[1],a[0]),e(-a[1],s[0])),h[2]))),o[3]),r(t(r(t(e(a[1],h[0]),e(-h[1],a[0])),o[2]),t(r(t(e(o[1],h[0]),e(-h[1],o[0])),-a[2]),r(t(e(o[1],a[0]),e(-a[1],o[0])),h[2]))),-s[3])),t(r(t(r(t(e(s[1],h[0]),e(-h[1],s[0])),o[2]),t(r(t(e(o[1],h[0]),e(-h[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),h[2]))),a[3]),r(t(r(t(e(s[1],a[0]),e(-a[1],s[0])),o[2]),t(r(t(e(o[1],a[0]),e(-a[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),a[2]))),-h[3]))),t(t(r(t(r(t(e(s[1],h[0]),e(-h[1],s[0])),i[2]),t(r(t(e(i[1],h[0]),e(-h[1],i[0])),-s[2]),r(t(e(i[1],s[0]),e(-s[1],i[0])),h[2]))),o[3]),r(t(r(t(e(s[1],h[0]),e(-h[1],s[0])),o[2]),t(r(t(e(o[1],h[0]),e(-h[1],o[0])),-s[2]),r(t(e(o[1],s[0]),e(-s[1],o[0])),h[2]))),-i[3])),t(r(t(r(t(e(i[1],h[0]),e(-h[1],i[0])),o[2]),t(r(t(e(o[1],h[0]),e(-h[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),h[2]))),s[3]),r(t(r(t(e(i[1],s[0]),e(-s[1],i[0])),o[2]),t(r(t(e(o[1],s[0]),e(-s[1],o[0])),-i[2]),r(t(e(o[1],i[0]),e(-i[1],o[0])),s[2]))),-h[3])))),u=n(l,c);return u[u.length-1]}}function h(t){return(3===t?i:4===t?s:a)(r,e,n,o)}var l=h(3),c=h(4),u=[function(){return 0},function(){return 0},function(t,e){return e[0]-t[0]},function(t,e,r){var n,o=(t[1]-r[1])*(e[0]-r[0]),i=(t[0]-r[0])*(e[1]-r[1]),s=o-i;if(o>0){if(i<=0)return s;n=o+i}else{if(!(o<0))return s;if(i>=0)return s;n=-(o+i)}var a=33306690738754716e-32*n;return s>=a||s<=-a?s:l(t,e,r)},function(t,e,r,n){var o=t[0]-n[0],i=e[0]-n[0],s=r[0]-n[0],a=t[1]-n[1],h=e[1]-n[1],l=r[1]-n[1],u=t[2]-n[2],f=e[2]-n[2],d=r[2]-n[2],g=i*l,m=s*h,p=s*a,w=o*l,y=o*h,b=i*a,x=u*(g-m)+f*(p-w)+d*(y-b),M=7771561172376103e-31*((Math.abs(g)+Math.abs(m))*Math.abs(u)+(Math.abs(p)+Math.abs(w))*Math.abs(f)+(Math.abs(y)+Math.abs(b))*Math.abs(d));return x>M||-x>M?x:c(t,e,r,n)}];function f(t){var e=u[t.length];return e||(e=u[t.length]=h(t.length)),e.apply(void 0,t)}function d(t,e,r,n,o,i,s){return function(e,r,a,h,l){switch(arguments.length){case 0:case 1:return 0;case 2:return n(e,r);case 3:return o(e,r,a);case 4:return i(e,r,a,h);case 5:return s(e,r,a,h,l)}for(var c=new Array(arguments.length),u=0;u<arguments.length;++u)c[u]=arguments[u];return t(c)}}!function(){for(;u.length<=5;)u.push(h(u.length));t.exports=d.apply(void 0,[f].concat(u));for(var e=0;e<=5;++e)t.exports[e]=u[e]}()}(orientation);var orientationExports=orientation.exports,robustPnp=robustPointInPolygon,orient=orientationExports;function robustPointInPolygon(t,e){for(var r=e[0],n=e[1],o=t.length,i=1,s=o,a=0,h=o-1;a<s;h=a++){var l=t[a],c=t[h],u=l[1],f=c[1];if(f<u){if(f<n&&n<u){if(0===(d=orient(l,c,e)))return 0;i^=0<d|0}else if(n===u){if(u<t[(a+1)%o][1]){if(0===(d=orient(l,c,e)))return 0;i^=0<d|0}}}else if(u<f){if(u<n&&n<f){if(0===(d=orient(l,c,e)))return 0;i^=d<0|0}else if(n===u){if(t[(a+1)%o][1]<u){var d;if(0===(d=orient(l,c,e)))return 0;i^=d<0|0}}}else if(n===u){var g=Math.min(l[0],c[0]),m=Math.max(l[0],c[0]);if(0===a){for(;h>0;){var p=(h+o-1)%o;if((b=t[p])[1]!==n)break;var w=b[0];g=Math.min(g,w),m=Math.max(m,w),h=p}if(0===h)return g<=r&&r<=m?0:1;s=h+1}for(var y=t[(h+o-1)%o][1];a+1<s;){var b;if((b=t[a+1])[1]!==n)break;w=b[0];g=Math.min(g,w),m=Math.max(m,w),a+=1}if(g<=r&&r<=m)return 0;var x=t[(a+1)%o][1];r<g&&y<n!=x<n&&(i^=1)}}return 2*i-1}var robustPointInPolygon$1=getDefaultExportFromCjs(robustPnp);function arrayPointsToObjects(t){const e=[];for(const r of t)e.push([r.column,r.row]);return e}function drawPolygonOnImage(t,e,r={}){const{fillColor:n,origin:o={column:0,row:0},...i}=r;checkProcessable$1(t,{bitDepth:[8,16]});const s=getOutputImage(t,r,{clone:!0});if(void 0===n)return s.drawPolyline([...e,e[0]],{origin:o,...i});{validateColor(n,s);const t=arrayPointsToObjects(deleteDuplicates(e));for(let e=0;e<s.height;e++)for(let r=0;r<s.width;r++)-1===robustPointInPolygon$1(t,[r,e])&&setBlendedVisiblePixel(s,Math.round(o.column)+r,Math.round(o.row)+e,n)}return s.drawPolyline([...e,e[0]],{origin:o,...i})}function drawPolygonOnMask(t,e,r={}){const{filled:n=!1,origin:o={column:0,row:0},...i}=r,s=maskToOutputMask(t,r,{clone:!0});if(!n)return s.drawPolyline([...e,e[0]],{origin:o,...i});const a=arrayPointsToObjects(deleteDuplicates(e));for(let t=0;t<s.height;t++)for(let e=0;e<s.width;e++)-1===robustPointInPolygon$1(a,[e,t])&&s.setBit(Math.round(o.column)+e,Math.round(o.row)+t,1);return s.drawPolyline([...e,e[0]],{origin:o,...i})}function drawRectangle(t,e={}){const{width:r=t.width,height:n=t.height,origin:o={column:0,row:0},strokeColor:i=getDefaultColor(t),fillColor:s}=e,a=Math.round(r),h=Math.round(n),l=Math.round(o.column),c=Math.round(o.row);let u;t instanceof Image?(checkProcessable$1(t,{bitDepth:[8,16]}),validateColor(i,t),s&&validateColor(s,t),u=getOutputImage(t,e,{clone:!0})):u=maskToOutputMask(t,e,{clone:!0});for(let t=l;t<l+a;t++)setBlendedVisiblePixel(u,t,c,i),setBlendedVisiblePixel(u,t,c+h-1,i);for(let t=c+1;t<c+h-1;t++)setBlendedVisiblePixel(u,l,t,i),setBlendedVisiblePixel(u,l+a-1,t,i);if(s)for(let t=c+1;t<c+h-1;t++)for(let e=l+1;e<l+a-1;e++)setBlendedVisiblePixel(u,e,t,s);return u}function drawPoints(t,e,r={}){const{color:n=getDefaultColor(t),origin:o={row:0,column:0}}=r;let i;t instanceof Image?(i=getOutputImage(t,r,{clone:!0}),validateColor(n,i)):i=maskToOutputMask(t,r,{clone:!0}),checkProcessable$1(i,{bitDepth:[1,8,16]});for(const t of e)setBlendedVisiblePixel(i,Math.round(o.column+t.column),Math.round(o.row+t.row),n);return i}function drawMarker(t,e,r){const n=getOutputImage(t,r,{clone:!0}),{strokeColor:o=getDefaultColor(n),fillColor:i,shape:s="cross",size:a=1}=r,h=Math.round(a);if(validateColor(o,n),i&&validateColor(i,n),checkProcessable$1(n,{bitDepth:[8,16]}),"circle"===s&&n.drawCircle(e,h,{strokeColor:o,fillColor:i,out:n}),"triangle"===s){const t=[{row:e.row-h,column:e.column},{row:e.row,column:e.column+h},{row:e.row,column:e.column-h}];n.drawPolygon(t,{strokeColor:o,fillColor:i,out:n})}if("cross"===s&&(n.drawLine({row:e.row-h,column:e.column},{row:e.row+h,column:e.column},{strokeColor:o,out:n}),n.drawLine({row:e.row,column:e.column-h},{row:e.row,column:e.column+h},{strokeColor:o,out:n})),"square"===s){const t={row:e.row-(h-1)/2,column:e.column-(h-1)/2};n.drawRectangle({origin:t,width:h,height:h,strokeColor:o,fillColor:i,out:n})}return n}function drawMarkers(t,e,r={}){const n=getOutputImage(t,r,{clone:!0});for(const t of e)drawMarker(n,t,{...r,out:n});return n}function and(t,e,r){const n=maskToOutputMask(t,r);if(t.width!==e.width||t.height!==e.height)throw new RangeError("both masks must have the same size");for(let r=0;r<n.size;r++)t.getBitByIndex(r)&&e.getBitByIndex(r)?n.setBitByIndex(r,1):n.setBitByIndex(r,0);return n}function blur(t,e){const{width:r,height:n}=e;if(r<1||r%2==0)throw new RangeError(`Invalid property "width". Must be an odd number greater than 0. Received ${r}.`);if(n<1||n%2==0)throw new RangeError(`Invalid property "height". Must be an odd number greater than 0. Received ${n}.`);return separableConvolution(t,new Array(r).fill(1),new Array(n).fill(1),{normalize:!0,...e})}const SOBEL_X=[[-1,0,1],[-2,0,2],[-1,0,1]],SOBEL_Y=[[-1,-2,-1],[0,0,0],[1,2,1]],SCHARR_X=[[3,0,-3],[10,0,-10],[3,0,-3]],SCHARR_Y=[[3,10,3],[0,0,0],[-3,-10,-3]],PREWITT_X=[[1,0,-1],[1,0,-1],[1,0,-1]],PREWITT_Y=[[1,1,1],[0,0,0],[-1,-1,-1]],DerivativeFilter={SOBEL:"sobel",SCHARR:"scharr",PREWITT:"prewitt"};function derivativeFilter(t,e={}){const{filter:r="sobel"}=e,n=M$2(r).with("sobel",()=>({kernelX:SOBEL_X,kernelY:SOBEL_Y})).with("scharr",()=>({kernelX:SCHARR_X,kernelY:SCHARR_Y})).with("prewitt",()=>({kernelX:PREWITT_X,kernelY:PREWITT_Y})).exhaustive();return t.gradientFilter({...n,...e})}const ImageColorModel={GREY:"GREY",GREYA:"GREYA",RGB:"RGB",RGBA:"RGBA",BINARY:"BINARY"},colorModels={GREY:{components:1,alpha:!1,channels:1},GREYA:{components:1,alpha:!0,channels:2},RGB:{components:3,alpha:!1,channels:3},RGBA:{components:3,alpha:!0,channels:4},BINARY:{components:1,alpha:!1,channels:1}};function increaseContrast(t,e={}){const{uniform:r=!1}=e;checkProcessable$1(t,{bitDepth:[8,16]});const n=t.minMax();let o=n.min,i=n.max;if(r){let t=-1,e=-1;for(let r=0;r<n.max.length;r++){const o=n.max[r]-n.min[r];o>e&&(t=r,e=o)}o=n.min[t],i=n.max[t]}let s=new Array(t.components).fill(0).map((t,e)=>e);return t.colorModel===ImageColorModel.GREYA?s=[0]:t.colorModel===ImageColorModel.RGBA&&(s=[0,1,2]),t.level({inputMin:o,inputMax:i,outputMin:0,outputMax:t.maxValue,channels:s,...e})}function gaussianBlur(t,e){if("sigma"in e){const{sigma:r,size:n=getSize$1(r),borderType:o}=e,i=getKernel(getRadius(n),r);return separableConvolution(t,i,i,{borderType:o})}if("sigmaX"in e&&"sigmaY"in e){const{sigmaX:r,sigmaY:n,sizeX:o=getSize$1(r),sizeY:i=getSize$1(n),borderType:s}=e,a=getRadius(o),h=getRadius(i);return separableConvolution(t,getKernel(a,r),getKernel(h,n),{borderType:s})}throw new TypeError("you must either define sigma or sigmaX and sigmaY in the options argument")}function getKernel(t,e){const r=2*t+1,n=new Array(r),o=e||.3*(.5*(r-1)-1)+.8,i=-.5/(o*o);let s=0;for(let e=0;e<r;e++){const r=e-t,o=Math.exp(i*r*r);n[e]=o,s+=o}for(let t=0;t<r;t++)n[t]/=s;return n}function getSize$1(t){return 2*Math.ceil(2*t)+1}function gradientFilter(t,e){const{borderType:r="replicate",borderValue:n=0}=e;if(checkProcessable$1(t,{bitDepth:[8,16],colorModel:"GREY"}),"kernelX"in e&&"kernelY"in e){const{kernelX:o,kernelY:i}=e,s=t.rawDirectConvolution(o,{borderType:r,borderValue:n}),a=t.rawDirectConvolution(i,{borderType:r,borderValue:n}),h=new Image(t.width,t.height,{colorModel:"GREY"});for(let e=0;e<t.size;e++)h.setValueByIndex(e,0,Math.hypot(s[e],a[e]));return h}if("kernelX"in e)return t.directConvolution(e.kernelX,{borderType:r,borderValue:n});if("kernelY"in e)return t.directConvolution(e.kernelY,{borderType:r,borderValue:n});throw new TypeError("kernelX and KernelY are not defined")}function hypotenuse(t,e,r={}){const{channels:n=new Array(t.components).fill(0).map((t,e)=>e)}=r;if(checkProcessable$1(t,{bitDepth:[8,16]}),t.width!==e.width||t.height!==e.height)throw new RangeError("both images must have the same size");if(t.alpha!==e.alpha||t.bitDepth!==e.bitDepth)throw new RangeError("both images must have the same alpha and bitDepth");if(t.channels!==e.channels)throw new RangeError("both images must have the same number of channels");validateChannels(n,t);const o=getOutputImage(t,{},{clone:!0});for(const r of n)for(let n=0;n<t.size;n++){const i=Math.hypot(t.getValueByIndex(n,r),e.getValueByIndex(n,r));o.setValueByIndex(n,r,Math.min(i,o.maxValue))}return o}function convertColor(t,e,r={}){const n=new Map([["GREY",["GREYA","RGB","RGBA"]],["GREYA",["GREY","RGB","RGBA"]],["RGB",["GREY","GREYA","RGBA"]],["RGBA",["GREY","GREYA","RGB"]],["BINARY",["GREY","RGB","RGBA"]]]);if(t.colorModel===e&&"BINARY"!==e)return getOutputImage(t,{out:r.out},{clone:!0});const o=n.get(t.colorModel);if(!o?.includes(e))throw new RangeError(`conversion from ${t.colorModel} to ${e} not implemented`);if(t instanceof Image){const n=getOutputImage(t,r,{newParameters:{colorModel:e}});return"GREY"!==t.colorModel&&"GREYA"!==t.colorModel||convertGreyToAny(t,n),"RGB"!==t.colorModel&&"RGBA"!==t.colorModel||("RGB"===e||"RGBA"===e?convertRgbToRgb(t,n):convertRgbToGrey(t,n)),!t.alpha&&n.alpha&&n.fillAlpha(n.maxValue),t.alpha&&n.alpha&&copyAlpha(t,n),n}if("GREY"===e){const e=maskToOutputImage(t,r);return convertBinaryToGrey(t,e),e}{const r=new Image(t.width,t.height,{colorModel:e});return convertBinaryToRgb(t,r),r}}function copyAlpha(t,e){if(t.size!==e.size)throw new RangeError("source and destination have different sizes");if(!t.alpha)throw new RangeError("source image does not have alpha");if(!e.alpha)throw new RangeError("destination does not have alpha");for(let r=0;r<e.size;r++)e.setValueByIndex(r,e.channels-1,t.getValueByIndex(r,t.channels-1))}function convertGreyToAny(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<e.components;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,0))}function convertRgbToRgb(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<3;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,n))}function convertRgbToGrey(t,e){for(let r=0;r<t.size;r++){const n=t.getValueByIndex(r,0),o=t.getValueByIndex(r,1),i=t.getValueByIndex(r,2);e.setValueByIndex(r,0,Math.round(.299*n+.587*o+.114*i))}}function convertBinaryToGrey(t,e){for(let r=0;r<t.size;r++)e.setValueByIndex(r,0,t.getBitByIndex(r)?e.maxValue:0)}function convertBinaryToRgb(t,e){const r=new Array(e.components).fill(0),n=new Array(e.components).fill(e.maxValue);e.alpha&&(r.push(e.maxValue),n.push(e.maxValue));for(let o=0;o<t.size;o++)e.setPixelByIndex(o,t.getBitByIndex(o)?n:r)}function convertBitDepth(t,e,r={}){if(t.bitDepth===e)return getOutputImage(t,r,{clone:!0});if(8!==e&&16!==e)throw new RangeError(`This image bit depth is not supported: ${e}`);const n=getOutputImage(t,r,{clone:!1,newParameters:{bitDepth:e,colorModel:t.colorModel}});return 8===e?convertToUint8(t,n):convertToUint16(t,n)}function convertToUint16(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<e.channels;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,n)<<8);return e}function convertToUint8(t,e){for(let r=0;r<t.size;r++)for(let n=0;n<e.channels;n++)e.setValueByIndex(r,n,t.getValueByIndex(r,n)>>8);return e}function checkPointIsInteger(t,e="Point"){if(!Number.isInteger(t.row)||!Number.isInteger(t.column))throw new TypeError(`${e} row and column must be integers`)}function extract(t,e,r={}){const{origin:n={row:0,column:0}}=r,{row:o,column:i}=n;assert(o<t.height&&i<t.width&&o+e.width>0&&i+e.height>0,"extract: image and mask have no overlap"),checkPointIsInteger(n,"Origin");const s=Math.min(t.width,e.width+i)-Math.max(0,i),a=Math.min(t.height,e.height+o)-Math.max(0,o);let h=new Image(s,a,{colorModel:t.colorModel,origin:n});h.alpha&&(h=h.fillAlpha(0));const l=Math.max(0,o),c=Math.max(0,i),u=o<0?-o:0,f=i<0?-i:0;for(let r=0;r<a;r++)for(let n=0;n<s;n++)e.getBit(f+n,u+r)&&h.setPixel(n,r,t.getPixel(c+n,l+r));return h}function merge(t){const e=t.length;let r;switch(e){case 2:r="GREYA";break;case 3:r="RGB";break;case 4:r="RGBA";break;default:throw new RangeError(`merge expects an array of two to four images. Received ${e}`)}const n=t[0];if(1!==n.channels)throw new RangeError(`each image must have one channel. Received ${n.channels}`);for(let r=1;r<e;r++){const e=t[r];if(1!==e.channels)throw new RangeError(`each image must have one channel. Received ${e.channels}`);if(e.width!==n.width||e.height!==n.height||e.bitDepth!==n.bitDepth)throw new RangeError("all images must have the same width, height and bitDepth")}const o=Image.createFrom(n,{colorModel:r});for(let r=0;r<e;r++){const e=t[r];for(let t=0;t<o.size;t++)o.setValueByIndex(t,r,e.getValueByIndex(t,0))}return o}function split(t){const e=[];for(let r=0;r<t.channels;r++){const n=Image.createFrom(t,{colorModel:"GREY"});for(let e=0;e<n.size;e++)n.setValueByIndex(e,0,t.getValueByIndex(e,r));e.push(n)}return e}function huang(t){let e=0;for(let r=0;r<t.length;r++)if(0!==t[r]){e=r;break}let r=t.length-1;for(let n=t.length-1;n>=e;n--)if(0!==t[n]){r=n;break}const n=1/(r-e),o=new Array(t.length);let i=0,s=0;for(let r=e;r<t.length;r++)i+=r*t[r],s+=t[r],o[r]=i/s;const a=new Array(t.length);i=0,s=0;for(let e=r;e>0;e--)i+=e*t[e],s+=t[e],a[e-1]=i/s;let h=-1,l=Number.MAX_VALUE;for(let e=0;e<t.length;e++){let r,i=0;for(let s=0;s<=e;s++)r=1/(1+n*Math.abs(s-o[e])),r<1e-6||r>.999999||(i+=t[s]*(-r*Math.log(r)-(1-r)*Math.log(1-r)));for(let o=e+1;o<t.length;o++)r=1/(1+n*Math.abs(o-a[e])),r<1e-6||r>.999999||(i+=t[o]*(-r*Math.log(r)-(1-r)*Math.log(1-r)));i<l&&(l=i,h=e)}return h}function intermodes(t){const e=t.slice();let r=0;for(;!bimodalTest$1(e);){let n=0,o=0,i=e[0];for(let r=0;r<t.length-1;r++)n=o,o=i,i=e[r+1],e[r]=(n+o+i)/3;e[t.length-1]=(o+i)/3,r++,assert(r<1e3,"Intermodes threshold not found after 1000 iterations")}let n=0;for(let r=1;r<t.length-1;r++)e[r-1]<e[r]&&e[r+1]<e[r]&&(n+=r);return Math.floor(n/2)}function bimodalTest$1(t){let e=!1,r=0;for(let e=1;e<t.length-1;e++)if(t[e-1]<t[e]&&t[e+1]<t[e]&&(r++,r>2))return!1;return 2===r&&(e=!0),e}function isodata(t){let e,r,n,o,i=0;for(let e=1;e<t.length;e++)if(t[e]>0){i=e+1;break}for(;;){e=0,n=0;for(let r=0;r<i;r++)n+=t[r],e+=t[r]*r;o=0,r=0;for(let e=i+1;e<t.length;e++)r+=t[e],o+=t[e]*e;if(n>0&&r>0&&(e/=n,o/=r,i===Math.round((e+o)/2)))break;i++,assert(i<=t.length-2,"Threshold not found")}return i}function li(t,e){let r,n,o,i,s,a,h,l,c,u;let f;u=0;for(let e=0;e<t.length;e++)u+=e*t[e];u/=e,h=u;do{a=h,r=a+.5|0,n=0,i=0;for(let e=0;e<=r;e++)n+=e*t[e],i+=t[e];l=0===i?0:n/i,o=0,s=0;for(let e=r+1;e<t.length;e++)o+=e*t[e],s+=t[e];c=0===s?0:o/s,f=(l-c)/(Math.log(l)-Math.log(c)),h=f<-Number.EPSILON?f-.5|0:f+.5|0}while(Math.abs(h-a)>.5);return r}function maxEntropy(t,e){const r=new Array(t.length);for(let n=0;n<t.length;n++)r[n]=t[n]/e;const n=new Array(t.length),o=new Array(t.length);n[0]=r[0],o[0]=1-n[0];for(let e=1;e<t.length;e++)n[e]=n[e-1]+r[e],o[e]=1-n[e];let i=0;for(let e=0;e<t.length;e++)if(Math.abs(n[e])>=Number.EPSILON){i=e;break}let s=t.length-1;for(let e=t.length-1;e>=i;e--)if(Math.abs(o[e])>=Number.EPSILON){s=e;break}let a,h,l,c=-1,u=Number.MIN_VALUE;for(let e=i;e<=s;e++){h=0;for(let o=0;o<=e;o++)0!==t[o]&&(h-=r[o]/n[e]*Math.log(r[o]/n[e]));l=0;for(let n=e+1;n<t.length;n++)0!==t[n]&&(l-=r[n]/o[e]*Math.log(r[n]/o[e]));a=h+l,u<a&&(u=a,c=e)}return c}function mean$1(t,e){let r=0;for(let e=0;e<t.length;e++)r+=e*t[e];return Math.floor(r/e)}function minError(t,e){let r,n,o,i,s,a,h,l,c,u,f,d,g=-2,m=0;for(let e=0;e<t.length;e++)m+=e*t[e];for(m/=e,r=m;r!==g;){const e=sumA(t,r),m=sumA(t,t.length-1),p=sumB(t,r),w=sumB(t,t.length-1),y=sumC(t,r);if(n=p/e,o=(w-p)/(m-e),i=e/m,s=(m-e)/m,a=y/e-n*n,h=(sumC(t,t.length-1)-y)/(m-e)-o*o,l=1/a-1/h,c=n/a-o/h,u=n*n/a-o*o/h+Math.log10(a*(s*s)/(h*(i*i))),f=c*c-l*u,f<0)return r;g=r,d=(c+Math.sqrt(f))/l,r=Number.isNaN(d)?g:Math.floor(d)}return r}function sumA(t,e){let r=0;for(let n=0;n<=e;n++)r+=t[n];return r}function sumB(t,e){let r=0;for(let n=0;n<=e;n++)r+=n*t[n];return r}function sumC(t,e){let r=0;for(let n=0;n<=e;n++)r+=n*n*t[n];return r}function minimum(t){if(t.length<2)return 0;let e=0,r=-1,n=-1,o=new Array(t.length);for(let e=0;e<t.length;e++)o[e]=t[e],t[e]>0&&(n=e);for(;!bimodalTest(o);)if(o=smoothed(o),e++,e>1e4)return r;for(let t=1;t<n;t++)if(o[t-1]>o[t]&&o[t+1]>=o[t]){r=t;break}return r}function smoothed(t){const e=new Array(t.length);for(let r=1;r<t.length-1;r++)e[r]=(t[r-1]+t[r]+t[r+1])/3;return e[0]=(t[0]+t[1])/3,e[t.length-1]=(t.at(-2)+t.at(-1))/3,e}function bimodalTest(t){const e=t.length;let r=!1,n=0;for(let r=1;r<e-1;r++)if(t[r-1]<t[r]&&t[r+1]<t[r]&&(n++,n>2))return!1;return 2===n&&(r=!0),r}function moments(t,e){let r=0,n=0,o=0,i=0,s=-1;const a=t.length,h=[];for(let r=0;r<a;r++)h.push(t[r]/e);for(let t=0;t<a;t++)r+=t*h[t],n+=t*t*h[t],o+=t*t*t*h[t];const l=1*n-r*r,c=(-n*n+r*o)/l,u=(1*-o+n*r)/l,f=.5*(-u-Math.sqrt(u*u-4*c)),d=.5*(-u+Math.sqrt(u*u-4*c)),g=(d-r)/(d-f);for(let t=0;t<a;t++)if(i+=h[t],i>g){s=t;break}return s}function otsu(t,e){let r=0,n=0,o=0,i=0,s=0;for(let e=0;e<t.length;e++)s+=e*t[e];for(let a=0;a<t.length;a++){n+=t[a];const h=e-n;if(0===n||0===h)continue;r+=a*t[a];const l=(s-r)/h,c=n*h*(r/n-l)*(r/n-l);c>=o&&(i=a,o=c)}return i}function percentile(t){let e=-1;const r=new Array(t.length),n=partialSum(t,t.length-1);let o=1;for(let i=0;i<t.length;i++)r[i]=Math.abs(partialSum(t,i)/n-.5),r[i]<o&&(o=r[i],e=i);return e}function partialSum(t,e){let r=0;for(let n=0;n<=e;n++)r+=t[n];return r}function renyiEntropy(t,e){let r,n;const o=new Array(t.length),i=new Array(t.length),s=new Array(t.length);let a=0,h=0,l=0,c=0,u=0,f=0;for(let r=0;r<t.length;r++)o[r]=t[r]/e;i[0]=o[0],s[0]=1-i[0];for(let e=1;e<t.length;e++)i[e]=i[e-1]+o[e],s[e]=1-i[e];r=0;for(let e=0;e<t.length;e++)if(Math.abs(i[e])>=Number.EPSILON){r=e;break}n=t.length-1;for(let e=t.length-1;e>=r;e--)if(Math.abs(s[e])>=Number.EPSILON){n=e;break}for(let e=r;e<=n;e++){let r=0,n=0,d=0;for(let s=0;s<=e;s++)0!==t[s]&&(r-=o[s]/i[e]*Math.log(o[s]/i[e])),n+=Math.sqrt(o[s]/i[e]),d+=o[s]*o[s]/(i[e]*i[e]);let g=0,m=0,p=0;for(let r=e+1;r<t.length;r++)0!==t[r]&&(g-=o[r]/s[e]*Math.log(o[r]/s[e])),m+=Math.sqrt(o[r]/s[e]),p+=o[r]*o[r]/(s[e]*s[e]);const w=r+g,y=2*(n*m>0?Math.log(n*m):0),b=-1*(d*p>0?Math.log(d*p):0);w>c&&(c=w,a=e),y>u&&(u=y,h=e),b>f&&(f=b,l=e)}const d=new Uint32Array([a,h,l]);let g;d.sort(),g=Math.abs(d[0]-d[1])<=5?Math.abs(d[1]-d[2])<=5?[1,2,1]:[0,1,3]:Math.abs(d[1]-d[2])<=5?[3,1,0]:[1,2,1];const m=i[d[2]]-i[d[0]];return Math.round(d[0]*(i[d[0]]+.25*m*g[0])+.25*d[1]*m*g[1]+d[2]*(s[d[2]]+.25*m*g[2]))}function shanbhag(t,e){const r=new Array(t.length);for(let n=0;n<t.length;n++)r[n]=t[n]/e;const n=new Array(t.length),o=new Array(t.length);n[0]=r[0],o[0]=1-n[0];for(let e=1;e<t.length;e++)n[e]=n[e-1]+r[e],o[e]=1-n[e];let i=0;for(let e=0;e<t.length;e++)if(Math.abs(n[e])>=Number.EPSILON){i=e;break}let s=t.length-1;for(let e=t.length-1;e>=i;e--)if(Math.abs(o[e])>=Number.EPSILON){s=e;break}let a,h,l,c,u=-1,f=Number.MAX_VALUE;for(let e=i;e<=s;e++){l=0,a=.5/n[e];for(let t=1;t<=e;t++)l-=r[t]*Math.log(1-a*n[t-1]);l*=a,c=0,a=.5/o[e];for(let n=e+1;n<t.length;n++)c-=r[n]*Math.log(1-a*o[n]);c*=a,h=Math.abs(l-c),h<f&&(f=h,u=e)}return u}function triangle(t){let e=0,r=0,n=0,o=0;for(let r=0;r<t.length;r++)if(t[r]>0){e=r;break}e>0&&e--;for(let e=t.length-1;e>0;e--)if(t[e]>0){o=e;break}o<t.length-1&&o++;for(let e=0;e<t.length;e++)t[e]>r&&(n=e,r=t[e]);let i,s,a,h=!1;if(n-e<o-n){h=!0;let r=0,i=t.length-1;for(;r<i;){const e=t[r];t[r]=t[i],t[i]=e,r++,i--}e=t.length-1-o,n=t.length-1-n}if(e===n)return e;i=t[n],s=e-n,a=Math.hypot(i,s),i/=a,s/=a,a=i*e+s*t[e];let l=e,c=0;for(let r=e+1;r<=n;r++){const e=i*r+s*t[r]-a;e>c&&(l=r,c=e)}if(l--,h){let e=0,r=t.length-1;for(;e<r;){const n=t[e];t[e]=t[r],t[r]=n,e++,r--}return t.length-1-l}return l}function yen(t,e){const r=new Array(t.length);for(let n=0;n<t.length;n++)r[n]=t[n]/e;const n=new Array(t.length);n[0]=r[0];for(let e=1;e<t.length;e++)n[e]=n[e-1]+r[e];const o=new Array(t.length);o[0]=r[0]*r[0];for(let e=1;e<t.length;e++)o[e]=o[e-1]+r[e]*r[e];const i=new Array(t.length);i[t.length-1]=0;for(let e=t.length-2;e>=0;e--)i[e]=i[e+1]+r[e+1]*r[e+1];let s,a=-1,h=Number.MIN_VALUE;for(let e=0;e<t.length;e++)s=-1*(o[e]*i[e]>0?Math.log(o[e]*i[e]):0)+2*(n[e]*(1-n[e])>0?Math.log(n[e]*(1-n[e])):0),s>h&&(h=s,a=e);return a}const ThresholdAlgorithm={HUANG:"huang",INTERMODES:"intermodes",ISODATA:"isodata",LI:"li",MAX_ENTROPY:"maxEntropy",MEAN:"mean",MIN_ERROR:"minError",MINIMUM:"minimum",MOMENTS:"moments",OTSU:"otsu",PERCENTILE:"percentile",RENYI_ENTROPY:"renyiEntropy",SHANBHAG:"shanbhag",TRIANGLE:"triangle",YEN:"yen"};function computeThreshold(t,e={}){const{algorithm:r="otsu",slots:n}=e;if(1!==t.channels)throw new TypeError("threshold can only be computed on images with one channel");const o=t.histogram({slots:n}),i=n?2**t.bitDepth/n:1;return M$2(r).with("huang",()=>huang(o)*i).with("intermodes",()=>intermodes(o)*i).with("isodata",()=>isodata(o)*i).with("li",()=>li(o,t.size)*i).with("maxEntropy",()=>maxEntropy(o,t.size)*i).with("mean",()=>mean$1(o,t.size)*i).with("minimum",()=>minimum(o)*i).with("minError",()=>minError(o,t.size)*i).with("moments",()=>moments(o,t.size)*i).with("otsu",()=>otsu(o,t.size)*i).with("percentile",()=>percentile(o)*i).with("renyiEntropy",()=>renyiEntropy(o,t.size)*i).with("shanbhag",()=>shanbhag(o,t.size)*i).with("triangle",()=>triangle(o)*i).with("yen",()=>yen(o,t.size)*i).exhaustive()}function threshold(t,e={}){let r;if("threshold"in e){const n=e.threshold;if(n<0||n>1)throw new RangeError("threshold must be a value between 0 and 1");r=n*t.maxValue}else r=computeThreshold(t,e);const n=imageToOutputMask(t,e);for(let e=0;e<t.size;e++)n.setBitByIndex(e,t.getValueByIndex(e,0)>r?1:0);return n}function luma709(t,e,r){return 6966*t+23436*e+2366*r>>15}function luma601(t,e,r){return 9798*t+19235*e+3735*r>>15}function max$1(t,e,r){return Math.max(t,e,r)}function min(t,e,r){return Math.min(t,e,r)}function average(t,e,r){return(t+e+r)/3|0}function minmax(t,e,r){return(Math.max(t,e,r)+Math.min(t,e,r))/2}function red(t){return t}function green(t,e){return e}function blue(t,e,r){return r}function black(t,e,r,n){return Math.min(n.maxValue-t,n.maxValue-e,n.maxValue-r)}function cyan(t,e,r,n){const o=black(t,e,r,n);return(n.maxValue-t-o)/(1-o/n.maxValue)|0}function magenta(t,e,r,n){const o=black(t,e,r,n);return(n.maxValue-e-o)/(1-o/n.maxValue)|0}function yellow(t,e,r,n){const o=black(t,e,r,n);return(n.maxValue-r-o)/(1-o/n.maxValue)|0}function hue(t,e,r,n){const o=min(t,e,r),i=max$1(t,e,r);if(i===o)return 0;let s=0;const a=i-o;return i===t?s=(e-r)/a+(e<r?6:0):i===e?s=(r-t)/a+2:(assert(i===r),s=(t-e)/a+4),s/6*n.maxValue|0}function saturation(t,e,r,n){const o=min(t,e,r),i=max$1(t,e,r);return 0===i?0:(i-o)/i*n.maxValue}function lightness(t,e,r){const n=min(t,e,r);return(max$1(t,e,r)+n)/2}var greyAlgorithms=Object.freeze({__proto__:null,average:average,black:black,blue:blue,cyan:cyan,green:green,hue:hue,lightness:lightness,luma601:luma601,luma709:luma709,magenta:magenta,max:max$1,min:min,minmax:minmax,red:red,saturation:saturation,yellow:yellow});const GreyAlgorithm={LUMA_709:"luma709",LUMA_601:"luma601",MAX:"max",MIN:"min",AVERAGE:"average",MINMAX:"minmax",RED:"red",GREEN:"green",BLUE:"blue",BLACK:"black",CYAN:"cyan",MAGENTA:"magenta",YELLOW:"yellow",HUE:"hue",SATURATION:"saturation",LIGHTNESS:"lightness"};{const t=new Set(Object.values(GreyAlgorithm));for(const e of Object.keys(greyAlgorithms))assert(t.has(e),`Grey algorithm ${e} is missing in the GreyAlgorithm enum`)}function grey(t,e={}){let{keepAlpha:r=!1,mergeAlpha:n=!0}=e;const{algorithm:o="luma709"}=e;checkProcessable$1(t,{colorModel:["RGB","RGBA"]}),r=r&&t.alpha,n=n&&t.alpha,r&&(n=!1);const i=getOutputImage(t,e,{newParameters:{colorModel:r?"GREYA":"GREY"}});let s;s="function"==typeof o?o:greyAlgorithms[o];const a=getClamp(i);for(let e=0;e<t.size;e++){const o=t.getValueByIndex(e,0),h=t.getValueByIndex(e,1),l=t.getValueByIndex(e,2);let c;if(n){const r=t.getValueByIndex(e,3);c=a(s(o,h,l,t)*r/t.maxValue)}else if(c=a(s(o,h,l,t)),r){const r=t.getValueByIndex(e,3);i.setValueByIndex(e,1,r)}i.setValueByIndex(e,0,c)}return i}function copyTo(t,e,r={}){const{origin:n={column:0,row:0}}=r,{column:o,row:i}=n;if(t.colorModel!==e.colorModel)throw new RangeError("source and target must have the same color model");let s;checkPointIsInteger(n,"Origin"),s=e instanceof Image?getOutputImage(e,r,{clone:!0}):maskToOutputMask(e,r,{clone:!0});for(let r=Math.max(i,0);r<Math.min(t.height+i,e.height);r++)for(let n=Math.max(o,0);n<Math.min(t.width+o,e.width);n++){setBlendedPixel(s,n,r,t.getPixel(n-o,r-i))}return s}function crop(t,e={}){const{origin:r={row:0,column:0},width:n=t.width-r.column,height:o=t.height-r.row}=e,{column:i,row:s}=r;if(checkProcessable$1(t,{bitDepth:[8,16]}),checkPointIsInteger(r,"Origin"),!Number.isInteger(n)||!Number.isInteger(o))throw new RangeError(`Width and height (width:${n}; height:${o}) must be integers`);if(s>t.height-1||i>t.width-1)throw new RangeError(`Origin (row:${s}, column:${i}) out of range (${t.width-1}; ${t.height-1})`);if(n<=0||o<=0)throw new RangeError(`Width and height (width:${n}; height:${o}) must be positive numbers`);if(s<0||i<0)throw new RangeError(`Row and column (row:${s}, column:${i}) must be positive numbers`);if(n>t.width-i||o>t.height-s)throw new RangeError(`Size is out of range (row:${s}, column:${i}, width:${n}, height:${o})`);const a=Image.createFrom(t,{width:n,height:o});for(let e=0;e<o;e++)for(let r=0;r<n;r++)a.setPixel(r,e,t.getPixel(i+r,s+e));return a}function cropAlpha(t,e={}){checkProcessable$1(t,{alpha:!0});const{threshold:r=t.maxValue}=e,n=findLeft(t,r,t.components);if(-1===n)throw new RangeError(`could not find new dimensions. Threshold may be too high: ${r}`);const o=findTop(t,r,t.components,n),i=findBottom(t,r,t.components,n),s=findRight(t,r,t.components,n,o,i);return t.crop({origin:{column:n,row:o},width:s-n+1,height:i-o+1})}function findLeft(t,e,r){for(let n=0;n<t.width;n++)for(let o=0;o<t.height;o++)if(t.getValue(n,o,r)>=e)return n;return-1}function findTop(t,e,r,n){for(let o=0;o<t.height;o++)for(let i=n;i<t.width;i++)if(t.getValue(i,o,r)>=e)return o;return-1}function findBottom(t,e,r,n){for(let o=t.height-1;o>=0;o--)for(let i=n;i<t.width;i++)if(t.getValue(i,o,r)>=e)return o;return-1}function findRight(t,e,r,n,o,i){for(let s=t.width-1;s>=n;s--)for(let n=o;n<=i;n++)if(t.getValue(s,n,r)>=e)return s;return-1}const BorderType={CONSTANT:"constant",REPLICATE:"replicate",REFLECT:"reflect",WRAP:"wrap",REFLECT_101:"reflect101"};function getBorderInterpolation(t,e){return"number"==typeof e&&(e=new Array(4).fill(e)),M$2(t).with("constant",()=>getInterpolateConstant(e)).with("replicate",()=>interpolateReplicate).with("reflect",()=>interpolateReflect).with("reflect101",()=>interpolateReflect101).with("wrap",()=>interpolateWrap).exhaustive()}function checkRange(t,e){if(t<=0-e||t>=e+e-1)throw new RangeError("border must be smaller than the original image")}function getInterpolateConstant(t){return function(e,r,n,o){const i=interpolateConstantPoint(e,o.width),s=interpolateConstantPoint(r,o.height);return-1===i||-1===s?t[n]:o.getValue(i,s,n)}}function interpolateConstantPoint(t,e){return t>=0&&t<e?t:-1}function interpolateReplicate(t,e,r,n){return n.getValue(interpolateReplicatePoint(t,n.width),interpolateReplicatePoint(e,n.height),r)}function interpolateReplicatePoint(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?0:e-1)}function interpolateReflect(t,e,r,n){return n.getValue(interpolateReflectPoint(t,n.width),interpolateReflectPoint(e,n.height),r)}function interpolateReflectPoint(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?-1-t:e+e-1-t)}function interpolateWrap(t,e,r,n){return n.getValue(interpolateWrapPoint(t,n.width),interpolateWrapPoint(e,n.height),r)}function interpolateWrapPoint(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?e+t:t-e)}function interpolateReflect101(t,e,r,n){return n.getValue(interpolateReflect101Point(t,n.width),interpolateReflect101Point(e,n.height),r)}function interpolateReflect101Point(t,e){return t>=0&&t<e?t:(checkRange(t,e),t<0?0-t:e+e-2-t)}function round(t){const e=Math.trunc(t),r=t-e;return r<.5?e:r>.5?e+1:e%2==0?e:e+1}const InterpolationType={NEAREST:"nearest",BILINEAR:"bilinear",BICUBIC:"bicubic"};function getInterpolationFunction(t){return M$2(t).with("nearest",()=>interpolateNearest).with("bilinear",()=>interpolateBilinear).with("bicubic",()=>interpolateBicubic).exhaustive()}function interpolateNearest(t,e,r,n,o){return o(e=Math.round(e),r=Math.round(r),n,t)}function interpolateBilinear(t,e,r,n,o){const i=Math.floor(e),s=Math.floor(r),a=i+1,h=s+1;if(a<t.width&&h<t.height&&i>=0&&s>=0){return round((h-r)*((a-e)*t.getValue(i,s,n)+(e-i)*t.getValue(a,s,n))+(r-s)*((a-e)*t.getValue(i,h,n)+(e-i)*t.getValue(a,h,n)))}return round((h-r)*((a-e)*o(i,s,n,t)+(e-i)*o(a,s,n,t))+(r-s)*((a-e)*o(i,h,n,t)+(e-i)*o(a,h,n,t)))}function interpolateBicubic(t,e,r,n,o,i){const s=Math.floor(e),a=Math.floor(r);if(s===e&&a===r)return o(s,a,n,t);const h=e-s,l=r-a,c=cubic(o(s-1,a-1,n,t),o(s,a-1,n,t),o(s+1,a-1,n,t),o(s+2,a-1,n,t),h),u=cubic(o(s-1,a,n,t),o(s,a,n,t),o(s+1,a,n,t),o(s+2,a,n,t),h),f=cubic(o(s-1,a+1,n,t),o(s,a+1,n,t),o(s+1,a+1,n,t),o(s+2,a+1,n,t),h),d=cubic(o(s-1,a+2,n,t),o(s,a+2,n,t),o(s+1,a+2,n,t),o(s+2,a+2,n,t),h);return round(i(cubic(c,u,f,d,l)))}function cubic(t,e,r,n,o){return e+.5*o*(r-t+o*(2*t-5*e+4*r-n+o*(3*(e-r)+n-t)))}function transform(t,e,r={}){const{borderValue:n=getDefaultColor(t),borderType:o="constant",interpolationType:i="bilinear",fullImage:s}=r;let{width:a=t.width,height:h=t.height}=r;if(Array.isArray(n)&&validateColor(n,t),!isValidMatrix(e))throw new TypeError(`transformation matrix must be 2x3 or 3x3. Received ${e.length}x${e[1].length}`);if(2===e.length&&e.push([0,0,1]),s){(e=e.map(t=>t.slice()))[0][2]=0,e[1][2]=0;const r=[t.getCoordinates("top-left"),t.getCoordinates("top-right"),t.getCoordinates("bottom-right"),t.getCoordinates("bottom-left")];r[1].column+=1,r[2].column+=1,r[2].row+=1,r[3].row+=1;const n=r.map(t=>[transformPoint(e[0],e[2],t.column,t.row),transformPoint(e[1],e[2],t.column,t.row)]),o=n.map(t=>t[0]),i=n.map(t=>t[1]),s=Math.max(...o),l=Math.max(...i),c=Math.min(...o),u=Math.min(...i),f=[(t.width-1)/2,(t.height-1)/2];a=s-c,h=l-u;const d=(a-1)/2-transformPoint(e[0],e[2],f[0],f[1]),g=(h-1)/2-transformPoint(e[1],e[2],f[0],f[1]);e[0][2]=d,e[1][2]=g,a=Math.round(a),h=Math.round(h)}r.inverse||(e=inverse(new Matrix(e)).to2DArray());const l=Image.createFrom(t,{width:a,height:h}),c=getBorderInterpolation(o,n),u=getClamp(l),f=getInterpolationFunction(i);for(let r=0;r<l.height;r++)for(let n=0;n<l.width;n++){const o=transformPoint(e[0],e[2],n,r),i=transformPoint(e[1],e[2],n,r);for(let e=0;e<l.channels;e++){const s=f(t,o,i,e,c,u);l.setValue(n,r,e,s)}}return l}function transformPoint(t,e,r,n){return(t[0]*r+t[1]*n+t[2])/(e[0]*r+e[1]*n+e[2])}function isValidMatrix(t){return 3===t.length&&3===t[0].length&&3===t[1].length&&3===t[2].length||2===t.length&&3===t[0].length&&3===t[1].length}function resize(t,e){const{interpolationType:r="bilinear",borderType:n="replicate",borderValue:o=0}=e,{width:i,height:s,xFactor:a,yFactor:h}=checkOptions(t,e);return transform(t,[[a,0,a/2],[0,h,h/2]],{interpolationType:r,borderType:n,borderValue:o,height:s,width:i})}function checkOptions(t,e){const{width:r,height:n,xFactor:o,yFactor:i,preserveAspectRatio:s=!0}=e;if(void 0===r&&void 0===n&&void 0===o&&void 0===i)throw new TypeError("at least one of the width, height, xFactor or yFactor options must be passed");let a,h;const l=getSize(r,o,t.width,s),c=getSize(n,i,t.height,s);return void 0===l?(assert(void 0!==c),a=Math.round(c*(t.width/t.height))):a=l,void 0===c?(assert(void 0!==l),h=Math.round(l*(t.height/t.width))):h=c,{width:a,height:h,xFactor:o??a/t.width,yFactor:i??h/t.height}}function getSize(t,e,r,n){if(void 0!==t){if(void 0!==e)throw new TypeError("factor and size cannot be passed together");return t}return void 0!==e?Math.round(r*e):n?void 0:r}function rotate(t,e){const r=e%180==0?t.width:t.height,n=e%180==0?t.height:t.width,o=Image.createFrom(t,{width:r,height:n});if(90===e||-270===e)for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.channels;n++)o.setValue(o.width-r-1,e,n,t.getValue(e,r,n));else if(180===e||-180===e)for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.channels;n++)o.setValue(o.width-e-1,o.height-r-1,n,t.getValue(e,r,n));else{if(270!==e&&-90!==e)throw new RangeError(`invalid angle: ${e}`);for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.channels;n++)o.setValue(r,o.height-e-1,n,t.getValue(e,r,n))}return o}function transformRotate(t,e,r={}){const{center:n="center",scale:o=1,...i}=r;let s;s="string"==typeof n?t.getCoordinates(n):n;return transform(t,getRotationMatrix(e,s,o),i)}function getRotationMatrix(t,e,r){const n=t*Math.PI/180,o=r*Math.cos(n),i=r*Math.sin(n);return[[o,i,(1-o)*e.column-i*e.row],[-i,o,i*e.column+(1-o)*e.row],[0,0,1]]}function getPerspectiveWarp(t,e={}){if(4!==t.length)throw new Error(`The array pts must have four elements, which are the four corners. Currently, pts have ${t.length} elements`);const{width:r,height:n}=e,[o,i,s,a]=order4Points(t);let h,l;if(n&&r)h=r,l=n;else{if(n||r)throw new Error(`Invalid dimensions: ${r?"`height`":"`width`"} is missing. Either provide both width and height, or omit both to auto-calculate dimensions.`);h=Math.ceil(Math.max(distance2Points(o,i),distance2Points(a,s))),l=Math.ceil(Math.max(distance2Points(o,a),distance2Points(i,s)))}const[c,u]=[0,0],[f,d]=[h-1,0],[g,m]=[h-1,l-1],[p,w]=[0,l-1],y=new Matrix([[c,u,1,0,0,0,-c*o.column,-u*o.column],[f,d,1,0,0,0,-f*i.column,-d*i.column],[g,m,1,0,0,0,-g*s.column,-m*s.column],[p,w,1,0,0,0,-p*a.column,-w*a.column],[0,0,0,c,u,1,-c*o.row,-u*o.row],[0,0,0,f,d,1,-f*i.row,-d*i.row],[0,0,0,g,m,1,-g*s.row,-m*s.row],[0,0,0,p,w,1,-p*a.row,-w*a.row]]),b=Matrix.columnVector([o.column,i.column,s.column,a.column,o.row,i.row,s.row,a.row]),x=new SingularValueDecomposition(y).solve(b).to1DArray();x.push(1);const M=[];for(let t=0;t<3;t++){const e=[];for(let r=0;r<3;r++)e.push(x[3*t+r]);M.push(e)}return{matrix:M,width:h,height:l}}function order4Points(t){let e,r,n,o,i=t[0].column,s=0;for(let e=1;e<t.length;e++)t[e].column<i&&(i=t[e].column,s=e);let a=t[(s+1)%t.length].column,h=(s+1)%t.length;for(let e=0;e<t.length;e++)t[e].column<a&&e!==s&&(a=t[e].column,h=e);return t[h].row<t[s].row?(e=t[h],o=t[s],s!==(h+1)%4?(r=t[(h+1)%4],n=t[(h+2)%4]):(r=t[(h+2)%4],n=t[(h+3)%4])):(o=t[h],e=t[s],h!==(s+1)%4?(r=t[(s+1)%4],n=t[(s+2)%4]):(r=t[(s+2)%4],n=t[(s+3)%4])),[e,r,n,o]}function distance2Points(t,e){return Math.hypot(t.column-e.column,t.row-e.row)}function rotatePoint(t,e,r){const n=Math.cos(r),o=Math.sin(r);return{column:t.column*n-t.row*o+(1-n)*e.column+e.row*o,row:t.column*o+t.row*n+(1-n)*e.row-e.column*o}}function cropRectangle(t,e,r){if(4!==e.length)throw new Error("The points array must contain 4 points");const n=getSmallestAngle(e),o={row:(e[0].row+e[2].row)/2,column:(e[0].column+e[2].column)/2},i=e.map(t=>rotatePoint(t,o,n)),[s,a,h]=i,l=Math.max(Math.abs(s.column-a.column),Math.abs(a.column-h.column)),c=Math.max(Math.abs(s.row-a.row),Math.abs(a.row-h.row)),u=Math.min(Math.ceil(l),Math.ceil(l-1e-10)),f=Math.min(Math.ceil(c),Math.ceil(c-1e-10)),d={row:Math.min(...i.map(t=>t.row))-(f-c)/2,column:Math.min(...i.map(t=>t.column))-(u-l)/2},g=rotatePoint(d,o,-n),m=Math.cos(-n),p=Math.sin(-n);return transform(t,[[m,-p,g.column],[p,m,g.row]],{inverse:!0,width:u,height:f,...r})}function getSmallestAngle(t){let e=-getAngle(t[1],t[0]);e>Math.PI/2?e-=Math.PI:e<-Math.PI/2&&(e+=Math.PI);let r=e;return Math.abs(e)>Math.PI/4&&(r=e>0?-Math.PI/2+e:Math.PI/2+e),r}function paintMaskOnImage(t,e,r={}){const{origin:n={row:0,column:0},color:o=getDefaultColor(t),blend:i=!0}=r,{column:s,row:a}=n;if(o.length!==t.channels)throw new RangeError("the given color is not compatible with the image");checkPointIsInteger(n,"Origin");const h=getOutputImage(t,r,{clone:!0});if(i){checkColorIsNumberArray(o);for(let r=Math.max(a,0);r<Math.min(e.height+a,t.height);r++)for(let n=Math.max(s,0);n<Math.min(e.width+s,t.width);n++)e.getBit(n-s,r-a)&&setBlendedPixel(h,n,r,o)}else for(let r=Math.max(a,0);r<Math.min(e.height+a,t.height);r++)for(let n=Math.max(s,0);n<Math.min(e.width+s,t.width);n++)if(e.getBit(n-s,r-a))for(let e=0;e<t.channels;e++){const t=o[e];"number"==typeof t&&h.setValue(n,r,e,t)}return h}function checkColorIsNumberArray(t){for(const e of t)if("number"!=typeof e)throw new TypeError("cannot have null channels in color if blend is true")}function paintMaskOnMask(t,e,r={}){const{origin:n={row:0,column:0},value:o=1}=r,{column:i,row:s}=n;checkPointIsInteger(n,"Origin");const a=maskToOutputMask(t,r,{clone:!0});for(let r=Math.max(s,0);r<Math.min(e.height+s,t.height);r++)for(let n=Math.max(i,0);n<Math.min(e.width+i,t.width);n++)e.getBit(n-i,r-s)&&a.setBit(n,r,o);return a}function maybeToPrecision(t,e){return t<0?(t=0-t,"number"==typeof e?`- ${t.toPrecision(e)}`:`- ${t.toString()}`):"number"==typeof e?t.toPrecision(e):t.toString()}function checkArrayLength(t,e){if(!isAnyArray$1(t.x)||!isAnyArray$1(t.y)||!isAnyArray$1(e))throw new TypeError("x, y and outputs must be arrays");if(t.x.length<2)throw new RangeError("explanatory variable should be two element per point");if(t.x.length!==t.y.length)throw new RangeError("x and y data must have the same length");if(t.x.length!==e.length)throw new RangeError("input and outputs must have the same length")}class BaseRegression2D{constructor(){if(new.target===BaseRegression2D)throw new Error("BaseRegression must be subclassed")}predict(t){if(isOnePoint(t))return this._predict(t);if(isAnyArray$1(t.x)){const{x:e,y:r}=t,n=new Float64Array(e.length);for(let t=0;t<e.length;t++)n[t]=this._predict({x:e[t],y:r[t]});return n}throw new TypeError("x must be a number or array")}_predict(t){throw new Error("_predict must be implemented")}train(){}toString(t){return""}toLaTeX(t){return""}getScore(t,e){checkArrayLength(t,e);const r=this.predict(t);let n=0,o=0,i=0,s=0,a=0,h=0,l=0;const c=e.length;for(let t=0;t<c;t++)n+=r[t],o+=e[t],a+=r[t]*r[t],h+=e[t]*e[t],l+=r[t]*e[t],0!==e[t]&&(i+=(e[t]-r[t])*(e[t]-r[t])/e[t]),s+=(e[t]-r[t])*(e[t]-r[t]);const u=(c*l-n*o)/Math.sqrt((c*a-n*n)*(c*h-o*o));return{r:u,r2:u*u,chi2:i,rmsd:Math.sqrt(s/c)}}}function isOnePoint(t){return!isAnyArray$1(t.x)}class PolynomialRegression2D extends BaseRegression2D{order;coefficients;score;constructor(t,e,r={}){if(super(),!0===t)this.coefficients=Matrix.columnVector(e.coefficients),this.order=e.order,this.score=e.score;else{checkArrayLength(t,e);const{order:n=2}=r;this.order=n,this.coefficients=train(t,e,n),this.score=this.getScore(t,e)}}_predict(t){const{x:e,y:r}=t;let n=0,o=0;for(let t=0;t<=this.order;t++)for(let i=0;i<=this.order-t;i++)n+=e**t*r**i*this.coefficients.get(o,0),o++;return n}toString(t){return this._toFormula(t,!1)}toLaTeX(t){return this._toFormula(t,!0)}_toFormula(t,e){let r="^",n="",o=" * ";e&&(r="^{",n="}",o="");let i="",s="",a=0;for(let e=0;e<=this.order;e++)for(let h=0;h<=this.order-e;h++){s="";const l=this.coefficients.get(a,0);0!==l&&(s+=maybeToPrecision(l,t),1===e?s+=`${o}x`:e>1&&(s+=`${o}x${r}${e}${n}`),1===h?s+=`${o}y`:h>1&&(s+=`${o}y${r}${h}${n}`),s=l>0?` + ${s}`:` ${s}`),a++,i=s+i}return`f(x, y) = ${i.startsWith("+")?i.slice(1):i}`}toJSON(){return{name:"polyfit2D",order:this.order,score:this.score,coefficients:this.coefficients}}static load(t){if("polyfit2D"!==t.name)throw new TypeError("not a polyfit2D model");return new PolynomialRegression2D(!0,t)}}function powColVector(t,e){const r=t.clone();for(let n=0;n<t.rows;++n)r.set(n,0,r.get(n,0)**e);return r}function train(t,e,r){Matrix.isMatrix(e)||(e=Matrix.columnVector(e));const n=new Matrix(e.rows,2);if(n.setColumn(0,t.x),n.setColumn(1,t.y),e.rows!==n.rows&&(e=e.transpose()),2!==n.columns)throw new RangeError(`You give x with ${n.columns} columns and it must be 2`);if(n.rows!==e.rows)throw new RangeError("x and y must have the same rows");const o=n.rows,i=(r+2)*(r+1)/2;if(o<i)throw new TypeError("Insufficient number of points to create regression model.");const s=n.getColumnVector(0),a=n.getColumnVector(1),h=1/s.clone().abs().max(),l=1/a.clone().abs().max(),c=1/e.clone().abs().max();s.mulColumn(0,h),a.mulColumn(0,l),e.mulColumn(0,c);const u=new Matrix(o,i);let f=0;for(let t=0;t<=r;++t){const e=r-t;for(let r=0;r<=e;++r){const e=powColVector(s,t).mulColumnVector(powColVector(a,r));u.setColumn(f,e),f++}}const d=new SVD(u.transpose(),{computeLeftSingularVectors:!0,computeRightSingularVectors:!0,autoTranspose:!1});let g=Matrix.rowVector(d.diagonal);g=g.apply((t,e)=>{g.get(t,e)>=1e-15?g.set(t,e,1/g.get(t,e)):g.set(t,e,0)});const m=Matrix.zeros(o,i);for(let t=0;t<i;++t)m.set(t,t,g.get(0,t));g=m;const p=d.rightSingularVectors,w=d.leftSingularVectors.mmul(g.transpose()).mmul(p.transpose()).mmul(e);for(let t=0,e=0;t<=i;++t){const n=r-t;for(let r=0;r<=n;++r)w.set(e,0,w.get(e,0)*h**t*l**r/c),e++}return w}function correctBackground(t,e){const{background:r,order:n=2,backgroundKind:o="light"}=e;checkProcessable$1(t,{colorModel:["GREY"]});const i=new Array,s=new Array,a=new Array;for(const e of r)i.push(e.column),s.push(e.row),a.push(t.getValueByPoint(e,0));const h=new PolynomialRegression2D({x:i,y:s},a,{order:n}),l={x:[],y:[]};for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++)l.x.push(r),l.y.push(e);const c=h.predict(l);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++){const n=Math.abs(t.getValue(r,e,0)-c[e*t.width+r]);t.setValue(r,e,0,n)}return"light"===o?t.invert():t}function extendBorders(t,e){const{horizontal:r,vertical:n,borderType:o="reflect101",borderValue:i=getDefaultColor(t)}=e;Array.isArray(i)&&validateColor(i,t);const s=getBorderInterpolation(o,i),a=Image.createFrom(t,{width:t.width+2*r,height:t.height+2*n});t.copyTo(a,{origin:{column:r,row:n},out:a});for(let e=0;e<n;e++)for(let o=0;o<a.width;o++)for(let i=0;i<t.channels;i++){const h=s(o-r,e-n,i,t);a.setValue(o,e,i,h)}for(let e=a.height-n;e<a.height;e++)for(let o=0;o<a.width;o++)for(let i=0;i<t.channels;i++){const h=s(o-r,e-n,i,t);a.setValue(o,e,i,h)}for(let e=n;e<a.height-n;e++)for(let o=0;o<r;o++)for(let i=0;i<t.channels;i++){const h=s(o-r,e-n,i,t);a.setValue(o,e,i,h)}for(let e=n;e<a.height-n;e++)for(let o=a.width-r;o<a.width;o++)for(let i=0;i<t.channels;i++){const h=s(o-r,e-n,i,t);a.setValue(o,e,i,h)}return a}function invert(t,e){if(t instanceof Image){const r=getOutputImage(t,e);t.alpha&&copyAlpha(t,r);const{maxValue:n,size:o}=r;for(let e=0;e<o;e++)for(let o=0;o<t.components;o++)r.setValueByIndex(e,o,n-t.getValueByIndex(e,o));return r}{const r=maskToOutputMask(t,e);for(let e=0;e<r.size;e++)r.setBitByIndex(e,!t.getBitByIndex(e));return r}}function level(t,e={}){const r=t.minMax();let{inputMin:n=r.min,inputMax:o=r.max,outputMin:i=0,outputMax:s=t.maxValue,gamma:a=1}=e;const{channels:h=new Array(t.components).fill(0).map((t,e)=>e)}=e;validateChannels(h,t),checkProcessable$1(t,{bitDepth:[8,16]});const l=getOutputImage(t,e,{clone:!0}),c=getClamp(t);n=getValueArray(n,t.channels),o=getValueArray(o,t.channels),i=getValueArray(i,t.channels),s=getValueArray(s,t.channels),a=getValueArray(a,t.channels);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++)for(const u of h){const h=t.getValue(r,e,u);let f=c((Math.max(Math.min(h,o[u]),n[u])-n[u])/(o[u]-n[u]));Number.isNaN(f)&&(f=0);const d=c(f**(1/a[u])*(s[u]-i[u])+i[u]);l.setValue(r,e,u,d)}return l}function getValueArray(t,e){if(Array.isArray(t)){if(t.length===e)return t;throw new RangeError("array length is not compatible with channel option")}return new Array(e).fill(t)}function or(t,e,r){const n=maskToOutputMask(t,r);if(t.width!==e.width||t.height!==e.height)throw new RangeError("both masks must have the same size");for(let r=0;r<n.size;r++)t.getBitByIndex(r)||e.getBitByIndex(r)?n.setBitByIndex(r,1):n.setBitByIndex(r,0);return n}function flipX(t){checkProcessable$1(t,{bitDepth:[8,16]});for(let e=0;e<t.height;e++)for(let r=0;r<Math.floor(t.width/2);r++){const n=r,o=t.width-r-1;for(let r=0;r<t.channels;r++){const i=t.getValue(n,e,r);t.setValue(n,e,r,t.getValue(o,e,r)),t.setValue(o,e,r,i)}}return t}function flipY(t){checkProcessable$1(t,{bitDepth:[8,16]});for(let e=0;e<Math.floor(t.height/2);e++)for(let r=0;r<t.width;r++){const n=e,o=t.height-e-1;for(let e=0;e<t.channels;e++){const i=t.getValue(r,n,e);t.setValue(r,n,e,t.getValue(r,o,e)),t.setValue(r,o,e,i)}}return t}function flip(t,e={}){const{axis:r="horizontal"}=e,n=getOutputImage(t,e,{clone:!0});return"horizontal"===r?flipX(n):flipY("vertical"===r?n:flipX(n))}function xMedian(t,e={}){if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");const{exact:r=!1}=e||{},n=t.slice(),o=calcMiddle(0,n.length-1),i=quickSelect(n,o);if(n.length%2==1||!r)return i;return(i+quickSelect(n,o+1))/2}function quickSelect(t,e){let r=0,n=t.length-1,o=0,i=0,s=0;for(;;){if(n<=r)return t[e];if(n===r+1)return t[r]>t[n]&&swap(t,r,n),t[e];for(o=calcMiddle(r,n),t[o]>t[n]&&swap(t,o,n),t[r]>t[n]&&swap(t,r,n),t[o]>t[r]&&swap(t,o,r),swap(t,o,r+1),i=r+1,s=n;;){do{i++}while(t[r]>t[i]);do{s--}while(t[s]>t[r]);if(s<i)break;swap(t,i,s)}swap(t,r,s),s<=e&&(r=i),s>=e&&(n=s-1)}}function swap(t,e,r){const n=t[r];t[r]=t[e],t[e]=n}function calcMiddle(t,e){return Math.floor((t+e)/2)}function xCheck(t,e={}){const{minLength:r}=e;if(!isAnyArray$1(t))throw new TypeError("input must be an array");if(0===t.length)throw new TypeError("input must not be empty");if("number"!=typeof t[0])throw new TypeError("input must contain numbers");if(r&&t.length<r)throw new Error(`input must have a length of at least ${r}`)}function xFindClosestIndex(t,e,r={}){const{sorted:n=!0}=r;if(n){let r=0,n=t.length-1,o=0;for(;n-r>1;)if(o=r+(n-r>>1),t[o]<e)r=o;else{if(!(t[o]>e))return o;n=o}return r<t.length-1?Math.abs(e-t[r])<Math.abs(t[r+1]-e)?r:r+1:r}{let r=0,n=Number.POSITIVE_INFINITY;for(let o=0;o<t.length;o++){const i=Math.abs(t[o]-e);i<n&&(n=i,r=o)}return r}}function xGetFromToIndex(t,e={}){let{fromIndex:r,toIndex:n}=e;const{from:o,to:i}=e;return void 0===r&&(r=void 0!==o?xFindClosestIndex(t,o):0),void 0===n&&(n=void 0!==i?xFindClosestIndex(t,i):t.length-1),r<0&&(r=0),n<0&&(n=0),r>=t.length&&(r=t.length-1),n>=t.length&&(n=t.length-1),r>n&&([r,n]=[n,r]),{fromIndex:r,toIndex:n}}function xMean(t,e={}){xCheck(t);const{fromIndex:r,toIndex:n}=xGetFromToIndex(t,e);let o=t[r];for(let e=r+1;e<=n;e++)o+=t[e];return o/(n-r+1)}function xVariance(t,e={}){if(!isAnyArray$1(t))throw new TypeError("input must be an array");const{unbiased:r=!0,mean:n=xMean(t)}=e;let o=0;for(const e of t){const t=e-n;o+=t*t}return r?o/(t.length-1):o/t.length}const LOOP=8,FLOAT_MUL=1/16777216,sh1=15,sh2=18,sh3=11;function multiplyUint32(t,e){const r=65535&(t>>>=0);return((t-r)*(e>>>=0)>>>0)+r*e>>>0}class XSadd{random;state;constructor(t=Date.now()){this.state=new Uint32Array(4),this.init(t),this.random=this.getFloat.bind(this)}getUint32(){return this.nextState(),this.state[3]+this.state[2]>>>0}getFloat(){return(this.getUint32()>>>8)*FLOAT_MUL}init(t){if(!Number.isInteger(t))throw new TypeError("seed must be an integer");this.state[0]=t,this.state[1]=0,this.state[2]=0,this.state[3]=0;for(let t=1;t<LOOP;t++)this.state[3&t]^=t+multiplyUint32(1812433253,this.state[t-1&3]^this.state[t-1&3]>>>30>>>0)>>>0;this.periodCertification();for(let t=0;t<LOOP;t++)this.nextState()}periodCertification(){0===this.state[0]&&0===this.state[1]&&0===this.state[2]&&0===this.state[3]&&(this.state[0]=88,this.state[1]=83,this.state[2]=65,this.state[3]=68)}nextState(){let t=this.state[0];t^=t<<sh1,t^=t>>>sh2,t^=this.state[3]<<sh3,this.state[0]=this.state[1],this.state[1]=this.state[2],this.state[2]=this.state[3],this.state[3]=t}}function createRandomArray(t={}){const{mean:e=0,standardDeviation:r=1,length:n=1e3,range:o=1,seed:i,distribution:s="normal"}=t,a=new XSadd(i),h=new Float64Array(n);switch(s){case"normal":{const t=new GaussianGenerator(e,r,a);for(let e=0;e<n;e++)h[e]=t.generateGaussian();break}case"uniform":for(let t=0;t<n;t++)h[t]=(a.random()-.5)*o+e;break;default:throw new Error(`unknown distribution: ${String(s)}`)}return h}class GaussianGenerator{#r=0;#n=!1;#o;#i;#s;constructor(t,e,r){this.#o=t,this.#i=e,this.#s=r}generateGaussian(){let t,e,r,n;if(this.#n)this.#n=!1,t=this.#r*this.#i+this.#o;else{do{e=2*this.#s.random()-1,r=2*this.#s.random()-1,n=e*e+r*r}while(n>=1||0===n);n=Math.sqrt(-2*Math.log(n)/n),this.#r=r*n,this.#n=!0,t=this.#o+this.#i*e*n}return t}}function xyCovariance(t,e={}){const{x:r,y:n}=t,{unbiased:o=!0}=e,i=xMean(r),s=xMean(n);let a=0;for(let t=0;t<r.length;t++)a+=(r[t]-i)*(n[t]-s);return o?a/(r.length-1):a/r.length}function medianFilter(t,e={}){const{cellSize:r=3,borderType:n="reflect101",borderValue:o=getDefaultColor(t)}=e;if(checkProcessable$1(t,{bitDepth:[8,16]}),r<1)throw new RangeError(`Invalid property "cellSize". Must be greater than 0. Received ${r}.`);if(r%2==0)throw new RangeError(`Invalid property "cellSize". Must be an odd number. Received ${r}.`);const i=getBorderInterpolation(n,o),s=Image.createFrom(t),a=new Uint16Array(r**2),h=(r-1)/2;for(let e=0;e<t.channels;e++)for(let r=0;r<t.height;r++)for(let n=0;n<t.width;n++){let o=0;for(let s=-h;s<=h;s++)for(let l=-h;l<=h;l++)a[o++]=i(n+l,r+s,e,t);s.setValue(n,r,e,xMedian(a))}return s}function pixelate(t,e){const{cellSize:r,algorithm:n="center"}=e;if(!Number.isInteger(r))throw new TypeError("cellSize must be an integer");if(r<2)throw new RangeError("cellSize must be greater than 1");const o=getOutputImage(t,e),i=getCellValueFunction(n);for(let e=0;e<t.channels;e++)for(let n=0;n<t.width;n+=r)for(let s=0;s<t.height;s+=r){const a=Math.min(r,t.width-n),h=Math.min(r,t.height-s),l=i(t,e,{width:a,height:h,origin:{column:n,row:s}});for(let t=n;t<n+a;t++)for(let r=s;r<s+h;r++)o.setValue(t,r,e,l)}return o}function getCellCenter(t,e,r){const n={column:Math.floor((r.origin.column+r.origin.column+r.width-1)/2),row:Math.floor((r.origin.row+r.origin.row+r.height-1)/2)};return t.getValue(n.column,n.row,e)}function getCellMean(t,e,r){let n=0;for(let o=r.origin.column;o<r.origin.column+r.width;o++)for(let i=r.origin.row;i<r.origin.row+r.height;i++)n+=t.getValue(o,i,e);return Math.round(n/(r.width*r.height))}function getCellMedian(t,e,r){const n=[];for(let o=r.origin.column;o<r.origin.column+r.width;o++)for(let i=r.origin.row;i<r.origin.row+r.height;i++)n.push(t.getValue(o,i,e));return xMedian(n)}function getCellValueFunction(t){switch(t){case"mean":return getCellMean;case"median":return getCellMedian;case"center":return getCellCenter;default:assertUnreachable(t)}}function getBorderPoints$1(t,e={}){const{innerBorders:r=!1,allowCorners:n=!1}=e;r||(t=t.solidFill());const o=[];for(let e=0;e<t.width;e++)t.getBit(e,0)&&o.push({column:e,row:0}),t.getBit(e,t.height-1)&&o.push({column:e,row:t.height-1});for(let e=0;e<t.height;e++)t.getBit(0,e)&&o.push({column:0,row:e}),t.getBit(t.width-1,e)&&o.push({column:t.width-1,row:e});for(let e=1;e<t.height-1;e++)for(let r=1;r<t.width-1;r++)t.getBit(r,e)&&(0!==t.getBit(r-1,e)&&0!==t.getBit(r,e-1)&&0!==t.getBit(r+1,e)&&0!==t.getBit(r,e+1)||o.push({column:r,row:e}),!n||0!==t.getBit(r-1,e-1)&&0!==t.getBit(r-1,e+1)&&0!==t.getBit(r+1,e-1)&&0!==t.getBit(r+1,e+1)||o.push({column:r,row:e}));return o}function getLineLength(t,e){return Math.hypot(t.column-e.column,t.row-e.row)}function getPolygonPerimeter(t){let e=0;for(let r=0;r<t.length;r++)e+=getLineLength(t[(r+1)%t.length],t[r]);return e}function getPolygonArea(t){let e=0;for(let r=0;r<t.length;r++){const n=(r+1)%t.length;e+=t[r].column*t[n].row,e-=t[r].row*t[n].column}return Math.abs(e/2)}function getCoordsFromIndex(t,e){return[Math.floor(t/e),t%e]}function getBitSafe(t,e,r){return isInBounds(t,e,r)?t.getBit(e,r):0}function isInBounds(t,e,r){return e>=0&&e<t.width&&r>=0&&r<t.height}const directions=[{dc:-1,dr:0},{dc:-1,dr:-1},{dc:0,dr:-1},{dc:1,dr:-1},{dc:1,dr:0},{dc:1,dr:1},{dc:0,dr:1},{dc:-1,dr:1}],directionIndexLookup=new Int8Array(9).fill(-1);for(let t=0;t<directions.length;t++){const{dc:e,dr:r}=directions[t];directionIndexLookup[3*(r+1)+(e+1)]=t}function getContourMoore(t){let e=0;const r=[],n=new Uint8Array(t.width*t.height);for(;1!==t.getBitByIndex(e)&&e!==t.size;)e++;if(e===t.size)return r;const[o,i]=getCoordsFromIndex(e,t.width),s={column:i,row:o};let a=s;n[e]||(n[e]=1,r.push(a));const h=findNextPoint(t,a,{column:i-1,row:o});if(!h)return r;let l=h.prevPoint,c=h.prevPoint;for(a=h.currPoint;;){e=a.row*t.width+a.column,n[e]||(n[e]=1,r.push(a));const o=findNextPoint(t,a,c);if(assert(o,"Next border point is undefined."),c=o.prevPoint,a=o.currPoint,a.column===s.column&&a.row===s.row){if(c.column===l.column&&c.row===l.row)break;l=c}}return r}function findNextPoint(t,e,r){let n=r;const o=r.column-e.column,i=r.row-e.row,s=directionIndexLookup[3*(i+1)+(o+1)];for(let r=0;r<directions.length;r++){const{dc:o,dr:i}=directions.at((s+r)%directions.length),a=e.column+o,h=e.row+i;if(1===getBitSafe(t,a,h))return{prevPoint:n,currPoint:{column:a,row:h}};n={column:a,row:h}}}const DR=[-1,0,1,0],DC=[0,1,0,-1];function turnLeft(t){return(t+3)%4}function turnRight(t){return(t+1)%4}function getContourPavlidis(t){let e=0;for(;e<t.size&&1!==t.getBitByIndex(e);)e++;if(e===t.size)return[];const[r,n]=getCoordsFromIndex(e,t.width);return traceFrom(t,n,r)}function traceFrom(t,e,r){let n=r,o=e,i=2,s=0;const a=new Uint8Array(t.width*t.height),h=[];let l=0;for(;;){const c=n*t.width+o;if(a[c]){if(n===r&&o===e){if(2===l)break;l++}}else a[c]=1,h.push({row:n,column:o});const u=turnLeft(i),f=turnRight(i),d=n+DR[u],g=o+DC[u],m=n+DR[i],p=o+DC[i],w=n+DR[f],y=o+DC[f];if(1===getBitSafe(t,g,d))i=u,n=d,o=g,s=0;else if(1===getBitSafe(t,p,m))n=m,o=p,s=0;else if(1===getBitSafe(t,y,w))i=f,n=w,o=y,s=0;else if(i=f,s++,s>=4)break}return h}function getExternalContour$1(t,e={}){const{allowCorners:r=!1}=e;return r?getContourPavlidis(t):getContourMoore(t)}function getExtendedBorderPoints(t){const e=getExternalContour$1(t),r=[];for(const t of e)r.push(t,{column:t.column+1,row:t.row},{column:t.column+1,row:t.row+1},{column:t.column,row:t.row+1});return r}function monotoneChainConvexHull(t,e={}){const{sorted:r=!1}=e;r||(t=t.slice()).sort(byXThenY);const n=t.length,o=new Array(2*n);let i=0;for(let e=0;e<n;e++){const r=t[e];for(;i>=2&&cw(o[i-2],o[i-1],r)<=0;)i--;o[i++]=r}const s=i+1;for(let e=n-2;e>=0;e--){const r=t[e];for(;i>=s&&cw(o[i-2],o[i-1],r)<=0;)i--;o[i++]=r}return o.slice(0,i-1)}function cw(t,e,r){return(e.row-t.row)*(r.column-t.column)-(e.column-t.column)*(r.row-t.row)}function byXThenY(t,e){return t.column===e.column?t.row-e.row:t.column-e.column}function getConvexHull(t){const e=getExtendedBorderPoints(t);if(0===e.length)return{points:[],surface:0,perimeter:0};const r=monotoneChainConvexHull(e);return{points:r,perimeter:getPolygonPerimeter(r),surface:getPolygonArea(r)}}function getFeret(t){const e=t.getConvexHull(),r=e.points;if(0===e.surface)return{minDiameter:{length:0,points:[{column:0,row:0},{column:0,row:0}],angle:0,calliperLines:[[{column:0,row:0},{column:0,row:0}],[{column:0,row:0},{column:0,row:0}]]},maxDiameter:{length:0,points:[{column:0,row:0},{column:0,row:0}],angle:0,calliperLines:[[{column:0,row:0},{column:0,row:0}],[{column:0,row:0},{column:0,row:0}]]},aspectRatio:1};let n,o=Number.POSITIVE_INFINITY,i=0,s=[];for(let t=0;t<r.length;t++){const e=getAngle(r[t],r[(t+1)%r.length]),a=rotate$1(-e,r);let h=0,l=[];for(let e=0;e<r.length;e++){const r=Math.abs(a[t].row-a[e].row);r>h&&(h=r,l=[a[t],a[e]])}if(h<o){o=h,i=e,s=l;const{minIndex:t,maxIndex:r}=findPointIndexesOfExtremeColumns(a);n=getMinLines(i,t,r,a,s)}}const a={points:rotate$1(i,s),length:o,angle:toDegrees(i),calliperLines:n};let h=[],l=0,c=[];for(let t=0;t<r.length-1;t++)for(let e=t+1;e<r.length;e++){const n=(r[t].column-r[e].column)**2+(r[t].row-r[e].row)**2;n>l&&(l=n,h=[r[t],r[e]],c=[t,e])}const u=getAngle(h[0],h[1]),f=rotate$1(-u,r),{minIndex:d,maxIndex:g}=findPointsIndexesOfExtremeRows(f),m=getMaxLines(u,d,g,f,c),p={length:Math.sqrt(l),angle:toDegrees(getAngle(h[0],h[1])),points:h,calliperLines:m};return{minDiameter:a,maxDiameter:p,aspectRatio:a.length/p.length}}function findPointIndexesOfExtremeColumns(t){let e=0,r=0;for(let n=0;n<t.length;n++)t[n].column>t[e].column&&(e=n),t[n].column<t[r].column&&(r=n);return{minIndex:r,maxIndex:e}}function findPointsIndexesOfExtremeRows(t){let e=0,r=0;for(let n=0;n<t.length;n++)t[n].row>t[e].row&&(e=n),t[n].row<t[r].row&&(r=n);return{minIndex:r,maxIndex:e}}function getMinLines(t,e,r,n,o){const i=[{column:n[e].column,row:o[0].row},{column:n[r].column,row:o[0].row}],s=[{column:n[e].column,row:o[1].row},{column:n[r].column,row:o[1].row}];return[rotate$1(t,i),rotate$1(t,s)]}function getMaxLines(t,e,r,n,o){const i=[{column:n[o[0]].column,row:n[e].row},{column:n[o[0]].column,row:n[r].row}],s=[{column:n[o[1]].column,row:n[e].row},{column:n[o[1]].column,row:n[r].row}];return[rotate$1(t,i),rotate$1(t,s)]}const leftFirst=(t,e)=>t.column<=e.column?-1:1,topFirst=(t,e)=>t.row>=e.row?-1:1;function getMbrAngle(t){const e=t.slice();e.sort(leftFirst);const r=e.slice(0,2),n=e.slice(2,4);r.sort(topFirst),n.sort(topFirst);return-toDegrees(getAngle(r[0],n[0]))}function getMbrFromPoints(t){if(0===t.length)return{points:[],angle:0,width:0,height:0,surface:0,perimeter:0,aspectRatio:0};if(1===t.length)return{points:[t[0],t[0],t[0],t[0]],perimeter:0,surface:0,angle:0,width:0,height:0,aspectRatio:1};let e=[],r=Number.POSITIVE_INFINITY,n=0,o=[];for(let i=0;i<t.length;i++){const s=getAngle(t[i],t[(i+1)%t.length]);e=rotate$1(-s,t);const a=e[i].column,h=e[i].row,l=e[(i+1)%e.length].column,c=e[(i+1)%e.length].row;let u=!0,f=0,d=0,g=0;for(const t of e){const e=(t.column-a)/(l-a);u?(u=!1,f=e,d=e):(e<f&&(f=e),e>d&&(d=e));const r=(-(l-a)*t.row+l*h-c*a)/(l-a);Math.abs(r)>Math.abs(g)&&(g=r)}const m={column:a+f*(l-a),row:h},p={column:a+d*(l-a),row:h},w=Math.abs(g*(f-d)*(l-a));w<r&&(n=s,r=w,o=[p,m,{column:m.column,row:m.row-g},{column:p.column,row:p.row-g}])}const i=rotate$1(n,o),s=[Math.hypot(o[0].column-o[1].column,o[0].row-o[1].row),Math.hypot(o[0].column-o[3].column,o[0].row-o[3].row)],a=Math.max(...s),h=Math.min(...s);return{points:i,surface:r,angle:getMbrAngle(i),width:a,height:h,perimeter:2*a+2*h,aspectRatio:h/a}}function getMbr(t){return getMbrFromPoints(monotoneChainConvexHull(getExtendedBorderPoints(t)))}function checkKernel(t){if(t.length%2==0||t[0].length%2==0)throw new TypeError("the number of rows and columns of the kernel must be odd")}function erode(t,e={}){let r=!1;void 0===e.kernel&&(r=!0);const{kernel:n=[[1,1,1],[1,1,1],[1,1,1]],iterations:o=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(n);let i=!0;if(!r)t:for(const t of n)for(const e of t)if(1!==e){i=!1;break t}let s=t;for(let e=0;e<o;e++)if(s instanceof Mask)if(i){const t=s.clone();s=erodeMaskOnlyOnes(s,t,n[0].length,n.length)}else{s=erodeMask(s,Mask.createFrom(t),n)}else if(i){s=erodeGreyOnlyOnes(s,Image.createFrom(t),n[0].length,n.length)}else{s=erodeGrey(s,Image.createFrom(t),n)}return s}function erodeGrey(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let h=0;h<t.width;h++){let l=t.maxValue;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+h,c=e-s+a;if(n<0||c<0||n>=t.width||c>=t.height)continue;const u=t.getValue(n,c,0);u<l&&(l=u)}e.setValue(h,a,0,l)}return e}function erodeGreyOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(0);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){let n=t.maxValue;for(let o=Math.max(0,r-i);o<Math.min(t.height,r+i+1);o++){const r=t.getValue(e,o,0);r<n&&(n=r)}s[e]=n}for(let n=0;n<t.width;n++){let i=t.maxValue;for(let e=Math.max(0,n-o);e<Math.min(t.width,n+o+1);e++)s[e]<i&&(i=s[e]);e.setValue(n,r,0,i)}}return e}function erodeMask(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let h=0;h<t.width;h++){let l=1;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+h,c=e-s+a;if(c<0||n<0||n>=t.width||c>=t.height)continue;if(0===t.getBit(n,c)){l=0;break}}1===l&&e.setBit(h,a,1)}return e}function erodeMaskOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(0);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){s[e]=1;for(let n=Math.max(0,r-i);n<Math.min(t.height,r+i+1);n++)if(0===t.getBit(e,n)){s[e]=0;break}}for(let n=0;n<t.width;n++)if(0!==e.getBit(n,r))for(let i=Math.max(0,n-o);i<Math.min(t.width,n+o+1);i++)if(0===s[i]){e.setBit(n,r,0);break}}return e}function dilate(t,e={}){let r=!1;void 0===e.kernel&&(r=!0);const{kernel:n=[[1,1,1],[1,1,1],[1,1,1]],iterations:o=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(n);let i=!0;if(!r)t:for(const t of n)for(const e of t)if(1!==e){i=!1;break t}let s=t;for(let t=0;t<o;t++)if(s instanceof Mask)if(i){const t=s.clone();s=dilatMaskOnlyOnes(s,t,n[0].length,n.length)}else{s=dilateMask(s,Mask.createFrom(s),n)}else if(i){s=dilateGreyOnlyOnes(s,Image.createFrom(s),n[0].length,n.length)}else{s=dilateGrey(s,Image.createFrom(s),n)}return s}function dilateGrey(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let h=0;h<t.width;h++){let l=0;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+h,c=e-s+a;if(n<0||c<0||n>=t.width||c>=t.height)continue;const u=t.getValue(n,c,0);u>l&&(l=u)}e.setValue(h,a,0,l)}return e}function dilateGreyOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(0);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){let n=0;for(let o=Math.max(0,r-i);o<Math.min(t.height,r+i+1);o++){const r=t.getValue(e,o,0);r>n&&(n=r)}s[e]=n}for(let n=0;n<t.width;n++){let i=0;for(let e=Math.max(0,n-o);e<Math.min(t.width,n+o+1);e++)s[e]>i&&(i=s[e]);e.setValue(n,r,0,i)}}return e}function dilateMask(t,e,r){const n=r[0].length,o=r.length,i=(n-1)/2,s=(o-1)/2;for(let a=0;a<t.height;a++)for(let h=0;h<t.width;h++){let l=0;for(let e=0;e<o;e++)for(let o=0;o<n;o++){if(1!==r[e][o])continue;const n=o-i+h,c=e-s+a;if(c<0||n<0||n>=t.width||c>=t.height)continue;if(1===t.getBit(n,c)){l=1;break}}1===l&&e.setBit(h,a,1)}return e}function dilatMaskOnlyOnes(t,e,r,n){const o=(r-1)/2,i=(n-1)/2,s=[];for(let e=0;e<t.width;e++)s.push(1);for(let r=0;r<t.height;r++){for(let e=0;e<t.width;e++){s[e]=0;for(let n=Math.max(0,r-i);n<Math.min(t.height,r+i+1);n++)if(1===t.getBit(e,n)){s[e]=1;break}}for(let n=0;n<t.width;n++)if(1!==e.getBit(n,r))for(let i=Math.max(0,n-o);i<Math.min(t.width,n+o+1);i++)if(1===s[i]){e.setBit(n,r,1);break}}return e}function open(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++)o=o.erode({kernel:r}),o=o.dilate({kernel:r});return o}function close(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++)o=o.dilate({kernel:r}).erode({kernel:r});return o}function topHat(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++){const t=o.open({kernel:r});o=subtract(t,o,{absolute:!0})}return o}function bottomHat(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++){const t=o.close({kernel:r});o=subtract(t,o,{absolute:!0})}return o}function morphologicalGradient(t,e={}){const{kernel:r=[[1,1,1],[1,1,1],[1,1,1]],iterations:n=1}=e;t instanceof Image&&checkProcessable$1(t,{bitDepth:[1,8,16],components:1,alpha:!1}),checkKernel(r);let o=t;for(let t=0;t<n;t++){const t=o.dilate({kernel:r}),e=o.erode({kernel:r});o=subtract(t,e,{absolute:!0})}return o}function*borderIterator(t){for(let e=0;e<t.width;e++)yield e;for(let e=2;e<t.height;e++)yield e*t.width-1;for(let e=0;e<t.width;e++)yield t.width*t.height-e-1;for(let e=t.height-2;e>=1;e--)yield e*t.width}const MAX_ARRAY=65536,toProcess=new Uint32Array(MAX_ARRAY);function multipleFloodFill(t,e={}){const{startPixels:r=[0],startPixelValue:n=0,newPixelValue:o=1,allowCorners:i=!1}=e,s=maskToOutputMask(t,e,{clone:!0}),a=Mask.createFrom(t);let h=0,l=0;for(const t of r)s.getBitByIndex(t)===n&&(toProcess[l++%MAX_ARRAY]=t,a.setBitByIndex(t,1),s.setBitByIndex(t,o));for(;h<l;){assert(l-h<=MAX_ARRAY);const e=toProcess[h++%MAX_ARRAY];s.setBitByIndex(e,o);const r=e<t.width,n=e%t.width===0,a=e%t.width===t.width-1,u=e>t.size-t.width;if(!u){c(e+t.width)}if(!n){c(e-1)}if(!r){c(e-t.width)}if(!a){c(e+1)}if(i){if(!r){if(!n){c(e-t.width-1)}if(!a){c(e-t.width+1)}}if(!u){if(!n){c(e+t.width-1)}if(!a){c(e+t.width+1)}}}}function c(t){a.getBitByIndex(t)||s.getBitByIndex(t)===n&&(toProcess[l++%MAX_ARRAY]=t,a.setBitByIndex(t,1))}return s}function clearBorder(t,e={}){const{allowCorners:r=!1,out:n,color:o="white"}=e;return multipleFloodFill(t,{startPixels:borderIterator(t),startPixelValue:"white"===o?1:0,newPixelValue:"white"===o?0:1,allowCorners:r,out:n})}const kernelX=[[-1,0,1],[-2,0,2],[-1,0,1]],kernelY=[[-1,-2,-1],[0,0,0],[1,2,1]];function cannyEdgeDetector(t,e={}){const{hysteresis:r=!0,lowThreshold:n=.04,highThreshold:o=.1,gaussianBlurOptions:i={sigma:1}}=e,s=n*t.maxValue,a=o*t.maxValue;checkProcessable$1(t,{colorModel:"GREY"});const h=t.width,l=t.height,c=t.gaussianBlur(i),u=c.rawDirectConvolution(kernelY),f=c.rawDirectConvolution(kernelX),d=new Float64Array(t.size);for(let e=0;e<t.size;e++)d[e]=Math.hypot(u[e],f[e]);const g=new Float64Array(t.size),m=new Float64Array(t.size),p=imageToOutputMask(t,e);for(let e=1;e<h-1;e++)for(let r=1;r<l-1;r++){const n=getDirection(u[getIndex(e,r,t,0)],f[getIndex(e,r,t,0)]),o=d[getIndex(e,r,t,0)];(0===n&&o>=d[getIndex(e,r-1,t,0)]&&o>=d[getIndex(e,r+1,t,0)]||1===n&&o>=d[getIndex(e-1,r-1,t,0)]&&o>=d[getIndex(e+1,r+1,t,0)]||2===n&&o>=d[getIndex(e-1,r,t,0)]&&o>=d[getIndex(e+1,r,t,0)]||3===n&&o>=d[getIndex(e-1,r+1,t,0)]&&o>=d[getIndex(e+1,r-1,t,0)])&&(g[getIndex(e,r,t,0)]=o)}for(let t=0;t<h*l;++t){const e=g[t];let r=0;e>a&&(r++,p.setBitByIndex(t,1)),e>s&&r++,m[t]=r}if(r){let e=[];for(let r=1;r<h-1;++r)for(let n=1;n<l-1;++n)if(1===m[getIndex(r,n,t,0)])t:for(let o=r-1;o<r+2;++o)for(let i=n-1;i<n+2;++i)if(2===m[getIndex(o,i,t,0)]){e.push([r,n]),p.setValue(r,n,0,1);break t}for(;e.length>0;){const r=[];for(const n of e)for(let e=-1;e<2;++e)for(let o=-1;o<2;++o){if(0===e&&0===o)continue;const i=n[0]+e,s=n[1]+o;1===m[getIndex(s,i,t,0)]&&0===p.getValue(s,i,0)&&(r.push([i,s]),p.setValue(s,i,0,1))}e=r}}return p}function getDirection(t,e){return(Math.round(Math.atan2(e,t)*(4/Math.PI))+4)%4}function floodFill(t,e={}){const{origin:r={row:0,column:0},allowCorners:n=!1,out:o}=e;return multipleFloodFill(t,{startPixels:[getIndex(r.column,r.row,t)],allowCorners:n,out:o})}function solidFill(t,e={}){const{allowCorners:r=!1}=e,n=maskToOutputMask(t,e,{clone:!0}),o=t.invert().clearBorder({allowCorners:r});return n.or(o,{out:n})}function boolToNumber(t){let e;return e="boolean"==typeof t?t?1:0:t,e}class Mask{width;height;size;bitDepth;colorModel;components;channels;alpha;maxValue;origin;data;constructor(t,e,r={}){const{data:n,origin:o={row:0,column:0}}=r;if(t<1||!Number.isInteger(t))throw new RangeError(`width must be an integer and at least 1. Received ${t}`);if(e<1||!Number.isInteger(e))throw new RangeError(`height must be an integer and at least 1. Received ${e}`);this.width=t,this.height=e,this.size=t*e,this.bitDepth=1,this.colorModel="BINARY",this.origin=o;const i=colorModels[this.colorModel];if(this.components=i.components,this.alpha=i.alpha,this.channels=i.channels,this.maxValue=1,void 0===n)this.data=new Uint8Array(this.size);else{const t=this.size*this.channels;if(n.length!==t)throw new RangeError(`incorrect data size: ${n.length}. Expected ${t}`);this.data=n}}static createFrom(t,e={}){const{width:r=t.width,height:n=t.height,origin:o=t.origin}=e;return new Mask(r,n,{origin:o,...e})}getPixel(t,e){const r=[],n=e*this.width+t;return r.push(this.data[n]),r}setPixel(t,e,r){const n=e*this.width+t;this.data[n]=r[0]}setVisiblePixel(t,e,r){t>=0&&t<this.width&&e>=0&&e<this.height&&this.setPixel(t,e,r)}getPixelByIndex(t){return[this.data[t]]}setPixelByIndex(t,e){this.data[t]=e[0]}static fromPoints(t,e,r){const n=new Mask(t,e);for(const t of r)n.setBit(t.column,t.row,1);return n}clone(){return Mask.createFrom(this,{data:this.data.slice()})}getBit(t,e){const r=e*this.width+t;return this.data[r]}setBit(t,e,r){const n=e*this.width+t;this.data[n]=r}getBitByIndex(t){return this.data[t*this.channels]}setBitByIndex(t,e){this.data[t*this.channels]=boolToNumber(e)}getNbNonZeroPixels(){let t=0;for(const e of this.data)e&&t++;return t}getValue(t,e,r){return checkChannel(r),this.getBit(t,e)}setValue(t,e,r,n){checkChannel(r),this.setBit(t,e,n)}getValueByIndex(t,e){return checkChannel(e),this.getBitByIndex(t)}setValueByIndex(t,e,r){checkChannel(e),this.setBitByIndex(t,r)}getValueByPoint(t){return this.getValue(t.column,t.row,0)}setValueByPoint(t,e){this.setValue(t.column,t.row,0,e)}getRawImage(){return{width:this.width,height:this.height,data:this.data}}[Symbol.for("nodejs.util.inspect.custom")](){let t;return t=this.height>20||this.width>20?"[...]":printData$1(this),`Mask {\n width: ${this.width}\n height: ${this.height}\n data: ${t}\n}`}fill(t){const e=boolToNumber(t);return this.data.fill(e),this}convertColor(t){return convertColor(this,t)}invert(t){return invert(this,t)}subtract(t,e){return subtract(this,t,e)}and(t,e){return and(this,t,e)}or(t,e){return or(this,t,e)}getBorderPoints(t){return getBorderPoints$1(this,t)}getExternalContour(t){return getExternalContour$1(this,t)}getConvexHull(){return getConvexHull(this)}getMbr(){return getMbr(this)}getFeret(){return getFeret(this)}erode(t){return erode(this,t)}dilate(t){return dilate(this,t)}open(t){return open(this,t)}close(t){return close(this,t)}topHat(t){return topHat(this,t)}bottomHat(t){return bottomHat(this,t)}morphologicalGradient(t){return morphologicalGradient(this,t)}clearBorder(t){return clearBorder(this,t)}floodFill(t){return floodFill(this,t)}solidFill(t){return solidFill(this,t)}drawPoints(t,e={}){return drawPoints(this,t,e)}drawLine(t,e,r={}){return drawLineOnMask(this,t,e,r)}drawPolyline(t,e={}){return drawPolylineOnMask(this,t,e)}drawPolygon(t,e={}){return drawPolygonOnMask(this,t,e)}drawRectangle(t={}){return drawRectangle(this,t)}copyTo(t,e={}){return copyTo(this,t,e)}paintMask(t,e){return paintMaskOnMask(this,t,e)}}function printData$1(t){const e=[];for(let r=0;r<t.height;r++){const n=[];for(let e=0;e<t.width;e++)n.push(String(t.getBit(e,r)));e.push(`[${n.join(" ")}]`)}return e.join("\n ")}function checkChannel(t){if(0!==t)throw new RangeError(`channel value must be 0 on type Mask. Received ${t}`)}function copyData(t,e){if(e.width!==t.width||e.height!==t.height||e.colorModel!==t.colorModel)throw new RangeError("images width, height or color model is different");e.data=t.data.slice()}function getOutputImage(t,e={},r={}){const{out:n}=e,{newParameters:o,clone:i}=r;if(void 0===n)return i?t.clone():Image.createFrom(t,o);if(!(n instanceof Image))throw new TypeError("out must be an Image");return checkRequirements({width:t.width,height:t.height,bitDepth:t.bitDepth,colorModel:t.colorModel,...o},n),i&&t!==n&&copyData(t,n),n}function maskToOutputImage(t,e={}){const{out:r}=e;if(void 0===r)return Image.createFrom(t,{colorModel:"GREY"});if(!(r instanceof Image))throw new TypeError("out must be an Image");return checkRequirements({width:t.width,height:t.height,bitDepth:8,colorModel:"GREY"},r),r}function imageToOutputMask(t,e={}){const{out:r}=e;if(void 0===r)return Mask.createFrom(t);if(!(r instanceof Mask))throw new TypeError("out must be a Mask");return checkRequirements({width:t.width,height:t.height,bitDepth:1,colorModel:"BINARY"},r),r}function checkRequirements(t,e){for(const r in t){const n=r;if(e[n]!==t[n])throw new RangeError(`cannot use out image. Its ${r} property must be ${t[n]}. Received ${e[n]}`)}}function maskToOutputMask(t,e={},r={}){const{out:n}=e,{newParameters:o,clone:i}=r;if(void 0===n)return i?t.clone():Mask.createFrom(t,o);if(!(n instanceof Mask))throw new TypeError("out must be a Mask");return checkRequirements({width:t.width,height:t.height,bitDepth:1,colorModel:"BINARY"},n),i&&t!==n&&copyData(t,n),n}function divide(t,e,r={}){const{channels:n=new Array(t.channels).fill(0).map((t,e)=>e)}=r;if(validateChannels(n,t),0===e)throw new TypeError("Cannot divide by 0");const o=getOutputImage(t,r,{clone:!0});if(0===n.length)return o;for(const t of n)for(let r=0;r<o.height;r++)for(let n=0;n<o.width;n++){const i=o.getValue(n,r,t)/e;o.setClampedValue(n,r,t,i)}return o}function mean(t,e){const r=new Array(t.channels).fill(0),n=e?e.points.length:t.size;if(0===n)throw new RangeError("Array of coordinates is empty.");if(e)for(const n of e.points)for(let e=0;e<t.channels;e++){if(n.column<0||n.column>=t.width||n.row<0||n.row>=t.height)throw new RangeError(`Invalid coordinate: {column: ${n.column}, row: ${n.row}}.`);r[e]+=t.getValueByPoint(n,e)}else for(let e=0;e<t.height;e++)for(let n=0;n<t.width;n++)for(let o=0;o<t.channels;o++)r[o]+=t.getValue(n,e,o);return r.map(t=>t/n)}function histogram$1(t,e={}){let{channel:r}=e;const{slots:n=2**t.bitDepth}=e;if(0===n||n&n-1)throw new RangeError("slots must be a power of 2, for example: 64, 256, 1024");if("number"!=typeof r){if(1!==t.channels)throw new TypeError("channel option is mandatory for multi-channel images");r=0}validateChannel(r,t);const o=new Uint32Array(n);let i=0;const s=Math.log2(n);i=t.bitDepth-s;for(let e=0;e<t.size;e++)o[t.getValueByIndex(e,r)>>i]++;return o}function median(t,e){const r=new Array(t.channels).fill(0);if(e){if(0===e.points.length)throw new RangeError("Array of coordinates is empty.");for(let n=0;n<t.channels;n++){const o=[];for(const r of e.points){if(r.column<0||r.column>=t.width||r.row<0||r.row>=t.height)throw new RangeError(`Invalid coordinate: {column: ${r.column}, row: ${r.row}}.`);o.push(t.getValueByPoint(r,n))}r[n]=quickMedian(o)}}else for(let e=0;e<t.channels;e++){const n=t.getChannel(e);r[e]=quickMedian(n)}return r}function getExtrema(t,e){const{kind:r="maximum",mask:n,algorithm:o="star",maxEquals:i=2}=e;checkProcessable$1(t,{bitDepth:[8,16]});const s="minimum"===r,a=s?0:1,h=[1,0,-1,0,1,1,-1,-1,2,0,-2,0,2,2,-2,-2],l=[0,1,0,-1,1,-1,1,-1,0,2,0,-2,2,-2,2,-2];switch(o){case"cross":h.length=4,l.length=4;break;case"square":h.length=8,l.length=8;break;case"star":break;default:assertUnreachable(o)}const c=h.length<=8?1:2,u=[];for(let e=0;e<t.channels;e++)for(let r=c;r<t.height-c;r++)for(let o=c;o<t.width-c;o++){if(n&&n.getBit(o,r)!==a)continue;let c=0,f=0;const d=t.getValue(o,r,e);for(let n=0;n<h.length;n++){const i=t.getValue(o+h[n],r+l[n],e);s?i>d&&c++:i<d&&c++,i===d&&f++}c+f===h.length&&f<=i&&u.push({column:o,row:r})}return u}function variance(t,e){const r=t.mean(e),n=new Array(t.channels).fill(0);if(e)for(const o of e.points)for(let e=0;e<t.channels;e++)n[e]+=(t.getValue(o.column,o.row,e)-r[e])**2;else for(let e=0;e<t.size;e++)for(let o=0;o<t.channels;o++)n[o]+=(t.getValueByIndex(e,o)-r[o])**2;const o=e?e.points.length*t.channels:t.size*t.channels;return n.map(t=>t/o)}class MultivariateLinearRegression{constructor(t,e,r={}){const{intercept:n=!0,statistics:o=!0}=r;if(this.statistics=o,!0===t)this.weights=e.weights,this.inputs=e.inputs,this.outputs=e.outputs,this.intercept=e.intercept;else{t=new Matrix(t),e=new Matrix(e),n&&t.addColumn(new Array(t.rows).fill(1));let r=t.transpose();const i=r.mmul(t),s=r.mmul(e),a=new SVD(i).inverse(),h=s.transpose().mmul(a).transpose();if(this.weights=h.to2DArray(),this.inputs=t.columns,this.outputs=e.columns,n&&this.inputs--,this.intercept=n,o){const r=t.mmul(h),n=e.clone().addM(r.neg()).to2DArray().map(t=>Math.pow(t[0],2)).reduce((t,e)=>t+e)/(e.rows-t.columns);this.stdError=Math.sqrt(n),this.stdErrorMatrix=pseudoInverse(i).mul(n),this.stdErrors=this.stdErrorMatrix.diagonal().map(t=>Math.sqrt(t)),this.tStats=this.weights.map((t,e)=>0===this.stdErrors[e]?0:t[0]/this.stdErrors[e])}}}predict(t){if(Array.isArray(t)){if("number"==typeof t[0])return this._predict(t);if(Array.isArray(t[0])){const e=new Array(t.length);for(let r=0;r<t.length;r++)e[r]=this._predict(t[r]);return e}}else if(Matrix.isMatrix(t)){const e=new Matrix(t.rows,this.outputs);for(let r=0;r<t.rows;r++)e.setRow(r,this._predict(t.getRow(r)));return e}throw new TypeError("x must be a matrix or array of numbers")}_predict(t){const e=new Array(this.outputs);if(this.intercept)for(let t=0;t<this.outputs;t++)e[t]=this.weights[this.inputs][t];else e.fill(0);for(let r=0;r<this.inputs;r++)for(let n=0;n<this.outputs;n++)e[n]+=this.weights[r][n]*t[r];return e}score(){throw new Error("score method is not implemented yet")}toJSON(){return{name:"multivariateLinearRegression",weights:this.weights,inputs:this.inputs,outputs:this.outputs,intercept:this.intercept,summary:this.statistics?{regressionStatistics:{standardError:this.stdError,observations:this.outputs},variables:this.weights.map((t,e)=>({label:e===this.weights.length-1?"Intercept":`X Variable ${e+1}`,coefficients:t,standardError:this.stdErrors[e],tStat:this.tStats[e]}))}:void 0}}static load(t){if("multivariateLinearRegression"!==t.name)throw new Error("not a MLR model");return new MultivariateLinearRegression(!0,t)}}var r$1={grad:.9,turn:360,rad:360/(2*Math.PI)},t$1=function(t){return"string"==typeof t?t.length>0:"number"==typeof t},n$1=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=Math.pow(10,e)),Math.round(r*t)/r+0},e$1=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=1),t>r?r:t>e?t:e},u$1=function(t){return(t=isFinite(t)?t%360:0)>0?t:t+360},a$1=function(t){return{r:e$1(t.r,0,255),g:e$1(t.g,0,255),b:e$1(t.b,0,255),a:e$1(t.a)}},o$1=function(t){return{r:n$1(t.r),g:n$1(t.g),b:n$1(t.b),a:n$1(t.a,3)}},i$2=/^#([0-9a-f]{3,8})$/i,s=function(t){var e=t.toString(16);return e.length<2?"0"+e:e},h$1=function(t){var e=t.r,r=t.g,n=t.b,o=t.a,i=Math.max(e,r,n),s=i-Math.min(e,r,n),a=s?i===e?(r-n)/s:i===r?2+(n-e)/s:4+(e-r)/s:0;return{h:60*(a<0?a+6:a),s:i?s/i*100:0,v:i/255*100,a:o}},b$1=function(t){var e=t.h,r=t.s,n=t.v,o=t.a;e=e/360*6,r/=100,n/=100;var i=Math.floor(e),s=n*(1-r),a=n*(1-(e-i)*r),h=n*(1-(1-e+i)*r),l=i%6;return{r:255*[n,a,s,s,h,n][l],g:255*[h,n,n,a,s,s][l],b:255*[s,s,h,n,n,a][l],a:o}},g=function(t){return{h:u$1(t.h),s:e$1(t.s,0,100),l:e$1(t.l,0,100),a:e$1(t.a)}},d=function(t){return{h:n$1(t.h),s:n$1(t.s),l:n$1(t.l),a:n$1(t.a,3)}},f=function(t){return b$1((r=(e=t).s,{h:e.h,s:(r*=((n=e.l)<50?n:100-n)/100)>0?2*r/(n+r)*100:0,v:n+r,a:e.a}));var e,r,n},c=function(t){return{h:(e=h$1(t)).h,s:(o=(200-(r=e.s))*(n=e.v)/100)>0&&o<200?r*n/100/(o<=100?o:200-o)*100:0,l:o/2,a:e.a};var e,r,n,o},l$1=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s*,\s*([+-]?\d*\.?\d+)%\s*,\s*([+-]?\d*\.?\d+)%\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,p$1=/^hsla?\(\s*([+-]?\d*\.?\d+)(deg|rad|grad|turn)?\s+([+-]?\d*\.?\d+)%\s+([+-]?\d*\.?\d+)%\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,v=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*,\s*([+-]?\d*\.?\d+)(%)?\s*(?:,\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,m=/^rgba?\(\s*([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s+([+-]?\d*\.?\d+)(%)?\s*(?:\/\s*([+-]?\d*\.?\d+)(%)?\s*)?\)$/i,y={string:[[function(t){var e=i$2.exec(t);return e?(t=e[1]).length<=4?{r:parseInt(t[0]+t[0],16),g:parseInt(t[1]+t[1],16),b:parseInt(t[2]+t[2],16),a:4===t.length?n$1(parseInt(t[3]+t[3],16)/255,2):1}:6===t.length||8===t.length?{r:parseInt(t.substr(0,2),16),g:parseInt(t.substr(2,2),16),b:parseInt(t.substr(4,2),16),a:8===t.length?n$1(parseInt(t.substr(6,2),16)/255,2):1}:null:null},"hex"],[function(t){var e=v.exec(t)||m.exec(t);return e?e[2]!==e[4]||e[4]!==e[6]?null:a$1({r:Number(e[1])/(e[2]?100/255:1),g:Number(e[3])/(e[4]?100/255:1),b:Number(e[5])/(e[6]?100/255:1),a:void 0===e[7]?1:Number(e[7])/(e[8]?100:1)}):null},"rgb"],[function(t){var e=l$1.exec(t)||p$1.exec(t);if(!e)return null;var r,n,o=g({h:(r=e[1],n=e[2],void 0===n&&(n="deg"),Number(r)*(r$1[n]||1)),s:Number(e[3]),l:Number(e[4]),a:void 0===e[5]?1:Number(e[5])/(e[6]?100:1)});return f(o)},"hsl"]],object:[[function(t){var e=t.r,r=t.g,n=t.b,o=t.a,i=void 0===o?1:o;return t$1(e)&&t$1(r)&&t$1(n)?a$1({r:Number(e),g:Number(r),b:Number(n),a:Number(i)}):null},"rgb"],[function(t){var e=t.h,r=t.s,n=t.l,o=t.a,i=void 0===o?1:o;if(!t$1(e)||!t$1(r)||!t$1(n))return null;var s=g({h:Number(e),s:Number(r),l:Number(n),a:Number(i)});return f(s)},"hsl"],[function(t){var e=t.h,r=t.s,n=t.v,o=t.a,i=void 0===o?1:o;if(!t$1(e)||!t$1(r)||!t$1(n))return null;var s=function(t){return{h:u$1(t.h),s:e$1(t.s,0,100),v:e$1(t.v,0,100),a:e$1(t.a)}}({h:Number(e),s:Number(r),v:Number(n),a:Number(i)});return b$1(s)},"hsv"]]},N=function(t,e){for(var r=0;r<e.length;r++){var n=e[r][0](t);if(n)return[n,e[r][1]]}return[null,void 0]},x$1=function(t){return"string"==typeof t?N(t.trim(),y.string):"object"==typeof t&&null!==t?N(t,y.object):[null,void 0]},M$1=function(t,e){var r=c(t);return{h:r.h,s:e$1(r.s+100*e,0,100),l:r.l,a:r.a}},H=function(t){return(299*t.r+587*t.g+114*t.b)/1e3/255},$=function(t,e){var r=c(t);return{h:r.h,s:r.s,l:e$1(r.l+100*e,0,100),a:r.a}},j=function(){function t(t){this.parsed=x$1(t)[0],this.rgba=this.parsed||{r:0,g:0,b:0,a:1}}return t.prototype.isValid=function(){return null!==this.parsed},t.prototype.brightness=function(){return n$1(H(this.rgba),2)},t.prototype.isDark=function(){return H(this.rgba)<.5},t.prototype.isLight=function(){return H(this.rgba)>=.5},t.prototype.toHex=function(){return e=(t=o$1(this.rgba)).r,r=t.g,n=t.b,i=(o=t.a)<1?s(n$1(255*o)):"","#"+s(e)+s(r)+s(n)+i;var t,e,r,n,o,i},t.prototype.toRgb=function(){return o$1(this.rgba)},t.prototype.toRgbString=function(){return e=(t=o$1(this.rgba)).r,r=t.g,n=t.b,(o=t.a)<1?"rgba("+e+", "+r+", "+n+", "+o+")":"rgb("+e+", "+r+", "+n+")";var t,e,r,n,o},t.prototype.toHsl=function(){return d(c(this.rgba))},t.prototype.toHslString=function(){return e=(t=d(c(this.rgba))).h,r=t.s,n=t.l,(o=t.a)<1?"hsla("+e+", "+r+"%, "+n+"%, "+o+")":"hsl("+e+", "+r+"%, "+n+"%)";var t,e,r,n,o},t.prototype.toHsv=function(){return t=h$1(this.rgba),{h:n$1(t.h),s:n$1(t.s),v:n$1(t.v),a:n$1(t.a,3)};var t},t.prototype.invert=function(){return w$1({r:255-(t=this.rgba).r,g:255-t.g,b:255-t.b,a:t.a});var t},t.prototype.saturate=function(t){return void 0===t&&(t=.1),w$1(M$1(this.rgba,t))},t.prototype.desaturate=function(t){return void 0===t&&(t=.1),w$1(M$1(this.rgba,-t))},t.prototype.grayscale=function(){return w$1(M$1(this.rgba,-1))},t.prototype.lighten=function(t){return void 0===t&&(t=.1),w$1($(this.rgba,t))},t.prototype.darken=function(t){return void 0===t&&(t=.1),w$1($(this.rgba,-t))},t.prototype.rotate=function(t){return void 0===t&&(t=15),this.hue(this.hue()+t)},t.prototype.alpha=function(t){return"number"==typeof t?w$1({r:(e=this.rgba).r,g:e.g,b:e.b,a:t}):n$1(this.rgba.a,3);var e},t.prototype.hue=function(t){var e=c(this.rgba);return"number"==typeof t?w$1({h:t,s:e.s,l:e.l,a:e.a}):n$1(e.h)},t.prototype.isEqual=function(t){return this.toHex()===w$1(t).toHex()},t}(),w$1=function(t){return t instanceof j?t:new j(t)},S=[],k=function(t){t.forEach(function(t){S.indexOf(t)<0&&(t(j,y),S.push(t))})},a=function(t){return"string"==typeof t?t.length>0:"number"==typeof t},t=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=Math.pow(10,e)),Math.round(r*t)/r+0},o=function(t,e,r){return void 0===e&&(e=0),void 0===r&&(r=1),t>r?r:t>e?t:e},r=function(t){var e=t/255;return e<.04045?e/12.92:Math.pow((e+.055)/1.055,2.4)},h=function(t){return 255*(t>.0031308?1.055*Math.pow(t,1/2.4)-.055:12.92*t)},n=96.422,p=100,M=82.521,u=function(t){var e,r,n=.9555766*(e=t).x+-.0230393*e.y+.0631636*e.z,i=-.0282895*e.x+1.0099416*e.y+.0210077*e.z,s=.0122982*e.x+-.020483*e.y+1.3299098*e.z;return r={r:h(.032404542*n-.015371385*i-.004985314*s),g:h(-.00969266*n+.018760108*i+41556e-8*s),b:h(556434e-9*n-.002040259*i+.010572252*s),a:t.a},{r:o(r.r,0,255),g:o(r.g,0,255),b:o(r.b,0,255),a:o(r.a)}},e=function(t){var e=r(t.r),i=r(t.g),s=r(t.b);return function(t){return{x:o(t.x,0,n),y:o(t.y,0,p),z:o(t.z,0,M),a:o(t.a)}}(function(t){return{x:1.0478112*t.x+.0228866*t.y+-.050127*t.z,y:.0295424*t.x+.9904844*t.y+-.0170491*t.z,z:-.0092345*t.x+.0150436*t.y+.7521316*t.z,a:t.a}}({x:100*(.4124564*e+.3575761*i+.1804375*s),y:100*(.2126729*e+.7151522*i+.072175*s),z:100*(.0193339*e+.119192*i+.9503041*s),a:t.a}))},w=216/24389,b=24389/27,i$1=function(t){var e=t.l,r=t.a,n=t.b,i=t.alpha,s=void 0===i?1:i;if(!a(e)||!a(r)||!a(n))return null;var h=function(t){return{l:o(t.l,0,400),a:t.a,b:t.b,alpha:o(t.alpha)}}({l:Number(e),a:Number(r),b:Number(n),alpha:Number(s)});return l(h)},l=function(t){var e=(t.l+16)/116,r=t.a/500+e,o=e-t.b/200;return u({x:(Math.pow(r,3)>w?Math.pow(r,3):(116*r-16)/b)*n,y:(t.l>8?Math.pow((t.l+16)/116,3):t.l/b)*p,z:(Math.pow(o,3)>w?Math.pow(o,3):(116*o-16)/b)*M,a:t.alpha})};function labPlugin(r,i){r.prototype.toLab=function(){return s=(o=e(this.rgba)).y/p,a=o.z/M,i=(i=o.x/n)>w?Math.cbrt(i):(b*i+16)/116,r={l:116*(s=s>w?Math.cbrt(s):(b*s+16)/116)-16,a:500*(i-s),b:200*(s-(a=a>w?Math.cbrt(a):(b*a+16)/116)),alpha:o.a},{l:t(r.l,2),a:t(r.a,2),b:t(r.b,2),alpha:t(r.alpha,3)};var r,o,i,s,a},r.prototype.delta=function(e){void 0===e&&(e="#FFF");var n=e instanceof r?e:new r(e),i=function(t,e){var r=t.l,n=t.a,o=t.b,i=e.l,s=e.a,a=e.b,h=180/Math.PI,l=Math.PI/180,c=Math.pow(Math.pow(n,2)+Math.pow(o,2),.5),u=Math.pow(Math.pow(s,2)+Math.pow(a,2),.5),f=(r+i)/2,d=Math.pow((c+u)/2,7),g=.5*(1-Math.pow(d/(d+Math.pow(25,7)),.5)),m=n*(1+g),p=s*(1+g),w=Math.pow(Math.pow(m,2)+Math.pow(o,2),.5),y=Math.pow(Math.pow(p,2)+Math.pow(a,2),.5),b=(w+y)/2,x=0===m&&0===o?0:Math.atan2(o,m)*h,M=0===p&&0===a?0:Math.atan2(a,p)*h;x<0&&(x+=360),M<0&&(M+=360);var v=M-x,I=Math.abs(M-x);I>180&&M<=x?v+=360:I>180&&M>x&&(v-=360);var A=x+M;I<=180?A/=2:A=(x+M<360?A+360:A-360)/2;var k=1-.17*Math.cos(l*(A-30))+.24*Math.cos(2*l*A)+.32*Math.cos(l*(3*A+6))-.2*Math.cos(l*(4*A-63)),R=i-r,E=y-w,P=2*Math.sin(l*v/2)*Math.pow(w*y,.5),S=1+.015*Math.pow(f-50,2)/Math.pow(20+Math.pow(f-50,2),.5),$=1+.045*b,C=1+.015*b*k,B=30*Math.exp(-1*Math.pow((A-275)/25,2)),T=-2*Math.pow(d/(d+Math.pow(25,7)),.5)*Math.sin(2*l*B);return Math.pow(Math.pow(R/1/S,2)+Math.pow(E/1/$,2)+Math.pow(P/1/C,2)+T*E*P/(1*$*1*C),.5)}(this.toLab(),n.toLab())/100;return o(t(i,3))},i.object.push([i$1,"lab"])}function formatReferenceForMlr(t,e){const r={r:[],g:[],b:[]};for(const n of t)r.r.push([n.r/e]),r.g.push([n.g/e]),r.b.push([n.b/e]);return r}function formatInputForMlr(t,e){const r=[];for(const n of t)r.push(getRegressionVariables(n.r,n.g,n.b,e));return r}function correctColor(t,e,r){if(checkProcessable$1(t,{colorModel:["RGB","RGBA"]}),e.length!==r.length)throw new RangeError("number of measured colors and reference colors must be the same");const n=formatInputForMlr(e,t.maxValue),o=formatReferenceForMlr(r,t.maxValue),i=new MultivariateLinearRegression(n,o.r),s=new MultivariateLinearRegression(n,o.g),a=new MultivariateLinearRegression(n,o.b),h=Image.createFrom(t);for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++){const n=t.getPixel(r,e),o=getRegressionVariables(n[0],n[1],n[2],t.maxValue),l=getClamp(t),c=[0,0,0],u=i.predict(o)[0]*t.maxValue,f=s.predict(o)[0]*t.maxValue,d=a.predict(o)[0]*t.maxValue;c[0]=l(u),c[1]=l(f),c[2]=l(d),t.alpha&&(c[3]=t.getValue(r,e,3)),h.setPixel(r,e,c)}return h}function getRegressionVariables(t,e,r,n){return[t/=n,e/=n,r/=n,t**2,e**2,r**2,t**3,e**3,r**3,t*e,t*r,r*e]}function getMinMax(t){const e=new Array(t.channels).fill(t.maxValue),r=new Array(t.channels).fill(0);for(let n=0;n<t.height;n++)for(let o=0;o<t.width;o++)for(let i=0;i<t.channels;i++){const s=t.getValue(o,n,i);s<e[i]&&(e[i]=s),s>r[i]&&(r[i]=s)}return{min:e,max:r}}k([labPlugin]);const ImageCoordinates={CENTER:"center",TOP_LEFT:"top-left",TOP_RIGHT:"top-right",BOTTOM_LEFT:"bottom-left",BOTTOM_RIGHT:"bottom-right"};class Image{width;height;size;bitDepth;colorModel;components;channels;alpha;maxValue;origin;originalResolution;meta;data;constructor(t,e,r={}){const{bitDepth:n=8,data:o,colorModel:i="RGB",origin:s={row:0,column:0},meta:a,resolution:h}=r;if(t<1||!Number.isInteger(t))throw new RangeError(`width must be an integer and at least 1. Received ${t}`);if(e<1||!Number.isInteger(e))throw new RangeError(`height must be an integer and at least 1. Received ${e}`);this.width=t,this.height=e,this.size=t*e,this.bitDepth=n,this.colorModel=i,this.origin=s,this.meta=a,this.originalResolution=h;const l=colorModels[i];if(this.components=l.components,this.alpha=l.alpha,this.channels=l.channels,this.maxValue=2**n-1,void 0===o)this.data=createPixelArray(this.size,this.channels,this.alpha,this.bitDepth,this.maxValue);else{if(8===n&&o instanceof Uint16Array)throw new RangeError(`bitDepth is ${n} but data is Uint16Array`);if(16===n&&o instanceof Uint8Array)throw new RangeError(`bitDepth is ${n} but data is Uint8Array`);const t=this.size*this.channels;if(o.length!==t)throw new RangeError(`incorrect data size: ${o.length}. Expected ${t}`);this.data=o}}get normalizedResolution(){if(!this.originalResolution)return;switch(this.originalResolution.unit){case"inch":return{x:this.originalResolution.x/2.54,y:this.originalResolution.y/2.54};case"centimeter":return{x:this.originalResolution.x,y:this.originalResolution.y};case"meter":return{x:this.originalResolution.x/100,y:this.originalResolution.y/100};case"unknown":return null;default:throw new Error("Unknown resolution unit.")}}static createFrom(t,e={}){const{width:r=t.width,height:n=t.height}=e;let o;return o=t instanceof Image?t.bitDepth:8,new Image(r,n,{bitDepth:o,colorModel:t.colorModel,origin:t.origin,...e})}getPixel(t,e){const r=[],n=(e*this.width+t)*this.channels;for(let t=0;t<this.channels;t++)r.push(this.data[n+t]);return r}getColumn(t){const e=[];for(let r=0;r<this.channels;r++){const n=[];for(let e=0;e<this.height;e++)n.push(this.getValue(t,e,r));e.push(n)}return e}getRow(t){const e=[];for(let r=0;r<this.channels;r++){const n=[];for(let e=0;e<this.width;e++)n.push(this.getValue(e,t,r));e.push(n)}return e}setPixel(t,e,r){const n=(e*this.width+t)*this.channels;for(let t=0;t<this.channels;t++)this.data[n+t]=r[t]}setVisiblePixel(t,e,r){t>=0&&t<this.width&&e>=0&&e<this.height&&this.setPixel(t,e,r)}getPixelByIndex(t){const e=[],r=t*this.channels;for(let t=0;t<this.channels;t++)e.push(this.data[r+t]);return e}setPixelByIndex(t,e){const r=t*this.channels;for(let t=0;t<this.channels;t++)this.data[r+t]=e[t]}getValue(t,e,r){return this.data[(e*this.width+t)*this.channels+r]}setValue(t,e,r,n){this.data[(e*this.width+t)*this.channels+r]=n}setClampedValue(t,e,r,n){n<0?n=0:n>this.maxValue&&(n=this.maxValue),this.data[(e*this.width+t)*this.channels+r]=n}getValueByIndex(t,e){return this.data[t*this.channels+e]}setValueByIndex(t,e,r){this.data[t*this.channels+e]=r}setClampedValueByIndex(t,e,r){r<0?r=0:r>this.maxValue&&(r=this.maxValue),this.data[t*this.channels+e]=r}getValueByPoint(t,e){return this.getValue(t.column,t.row,e)}setValueByPoint(t,e,r){this.setValue(t.column,t.row,e,r)}minMax(){return getMinMax(this)}getRawImage(){return{width:this.width,height:this.height,data:this.data,channels:this.channels,bitDepth:this.bitDepth}}[Symbol.for("nodejs.util.inspect.custom")](){let t;return t=this.height>20||this.width>20?"[...]":printData(this),`Image {\n width: ${this.width}\n height: ${this.height}\n bitDepth: ${this.bitDepth}\n colorModel: ${this.colorModel}\n channels: ${this.channels}\n data: ${t}\n}`}fill(t){if("number"==typeof t)return validateValue(t,this),this.data.fill(t),this;if(t.length!==this.channels)throw new RangeError(`the size of value must match the number of channels (${this.channels}). Received ${t.length}`);for(const e of t)validateValue(e,this);for(let e=0;e<this.data.length;e+=this.channels)for(let r=0;r<=this.channels;r++)this.data[e+r]=t[r];return this}fillChannel(t,e){validateChannel(t,this),validateValue(e,this);for(let r=t;r<this.data.length;r+=this.channels)this.data[r]=e;return this}getChannel(t){validateChannel(t,this);const e=new Array(this.size);for(let r=0;r<this.size;r++)e[r]=this.data[t+r*this.channels];return e}fillAlpha(t){if(validateValue(t,this),!this.alpha)throw new TypeError("fillAlpha can only be called if the image has an alpha channel");const e=this.channels-1;return this.fillChannel(e,t)}clone(){return Image.createFrom(this,{data:this.data.slice()})}changeEach(t){for(let e=0;e<this.data.length;e++)this.data[e]=t(this.data[e])}getCoordinates(t,e=!1){return M$2(t).with("center",()=>{const t=(this.width-1)/2,r=(this.height-1)/2;return e?{column:Math.round(t),row:Math.round(r)}:{column:t,row:r}}).with("top-left",()=>({column:0,row:0})).with("top-right",()=>({column:this.width-1,row:0})).with("bottom-left",()=>({column:0,row:this.height-1})).with("bottom-right",()=>({column:this.width-1,row:this.height-1})).exhaustive()}subtract(t,e={}){return subtract(this,t,e)}add(t){return add(this,t)}multiply(t,e={}){return multiply(this,t,e)}divide(t,e={}){return divide(this,t,e)}histogram(t){return histogram$1(this,t)}mean(t){return mean(this,t)}median(t){return median(this,t)}variance(t){return variance(this,t)}drawPoints(t,e={}){return drawPoints(this,t,e)}drawLine(t,e,r={}){return drawLineOnImage(this,t,e,r)}drawRectangle(t={}){return drawRectangle(this,t)}drawPolyline(t,e={}){return drawPolylineOnImage(this,t,e)}drawPolygon(t,e={}){return drawPolygonOnImage(this,t,e)}drawCircle(t,e,r={}){return drawCircleOnImage(this,t,e,r)}drawMarker(t,e={}){return drawMarker(this,t,e)}drawMarkers(t,e={}){return drawMarkers(this,t,e)}split(){return split(this)}convertColor(t,e){return convertColor(this,t,e)}convertBitDepth(t,e){return convertBitDepth(this,t,e)}grey(t){return grey(this,t)}copyTo(t,e={}){return copyTo(this,t,e)}threshold(t={}){return threshold(this,t)}crop(t){return crop(this,t)}cropRectangle(t,e){return cropRectangle(this,t,e)}cropAlpha(t={}){return cropAlpha(this,t)}extract(t,e){return extract(this,t,e)}paintMask(t,e){return paintMaskOnImage(this,t,e)}blur(t){return blur(this,t)}pixelate(t){return pixelate(this,t)}directConvolution(t,e){return directConvolution(this,t,e)}rawDirectConvolution(t,e){return rawDirectConvolution(this,t,e)}separableConvolution(t,e,r){return separableConvolution(this,t,e,r)}gaussianBlur(t){return gaussianBlur(this,t)}flip(t){return flip(this,t)}invert(t){return invert(this,t)}hypotenuse(t,e){return hypotenuse(this,t,e)}gradientFilter(t){return gradientFilter(this,t)}derivativeFilter(t){return derivativeFilter(this,t)}level(t){return level(this,t)}increaseContrast(t={}){return increaseContrast(this,t)}correctColor(t,e){return correctColor(this,t,e)}medianFilter(t){return medianFilter(this,t)}resize(t){return resize(this,t)}rotate(t){return rotate(this,t)}transform(t,e){return transform(this,t,e)}transformRotate(t,e){return transformRotate(this,t,e)}erode(t){return erode(this,t)}dilate(t){return dilate(this,t)}open(t){return open(this,t)}close(t){return close(this,t)}topHat(t){return topHat(this,t)}bottomHat(t){return bottomHat(this,t)}morphologicalGradient(t){return morphologicalGradient(this,t)}cannyEdgeDetector(t){return cannyEdgeDetector(this,t)}}function createPixelArray(t,e,r,n,o){const i=e*t,s=M$2(n).with(8,()=>new Uint8Array(i)).with(16,()=>new Uint16Array(i)).otherwise(()=>{throw new RangeError(`invalid bitDepth: ${n}`)});if(r)for(let t=e-1;t<i;t+=e)s[t]=o;return s}function printData(t){const e=[],r=8===t.bitDepth?3:5;for(let n=0;n<t.height;n++){const o=[];for(let e=0;e<t.width;e++)for(let i=0;i<t.channels;i++)o.push(String(t.getValue(e,n,i)).padStart(r," "));e.push(`[${o.join(" ")}]`)}return`{\n [\n ${e.join("\n ")}\n ]\n }`}function directConvolution(t,e,r={}){const{borderType:n="reflect101",borderValue:o=0}=r,i=rawDirectConvolution(t,e,{borderType:n,borderValue:o}),s=getOutputImage(t,r),a=getClamp(s);for(let e=0;e<t.size;e++)for(let r=0;r<t.channels;r++){const n=round(a(i[e*t.channels+r]));s.setValueByIndex(e,r,n)}return s}function rawDirectConvolution(t,e,r={}){const{borderType:n="reflect101",borderValue:o=getDefaultColor(t)}=r;Array.isArray(o)&&validateColor(o,t);const i=getBorderInterpolation(n,o),s=new Float64Array(t.size*t.channels);for(let r=0;r<t.channels;r++)for(let n=0;n<t.height;n++)for(let o=0;o<t.width;o++){s[getIndex(o,n,t,r)]=computeConvolutionValue(o,n,r,t,e,i,{returnRawValue:!0})}return s}function separableConvolution(t,e,r,n={}){const{normalize:o,borderType:i="reflect101",borderValue:s=0}=n;o&&([e,r]=normalizeSeparatedKernel(e,r));const a=extendBorders(t,{horizontal:(e.length-1)/2,vertical:(r.length-1)/2,borderType:i,borderValue:s}),h=Image.createFrom(t),l=getClamp(h),c=new DirectConvolution(a.width,e,BorderType$1.CUT),u=new DirectConvolution(a.height,r,BorderType$1.CUT),f=new Float64Array(a.width),d=new Float64Array(a.height),g=new Float64Array(t.width*a.height);for(let e=0;e<a.channels;e++){for(let r=0;r<a.height;r++){for(let t=0;t<a.width;t++)f[t]=a.getValue(t,r,e);const n=c.convolve(f);for(let e=0;e<t.width;e++)g[r*t.width+e]=n[e]}for(let r=0;r<t.width;r++){for(let e=0;e<a.height;e++)d[e]=g[e*t.width+r];const n=u.convolve(d);for(let o=0;o<t.height;o++)h.setValue(r,o,e,round(l(n[o])))}}return h}function computeConvolutionValue(t,e,r,n,o,i,s={}){let{clamp:a}=s;const{returnRawValue:h=!1}=s;h&&(a=void 0);let l=0;const c=o[0].length,u=o.length,f=(c-1)/2,d=(u-1)/2;for(let s=0;s<u;s++)for(let a=0;a<c;a++){l+=o[s][a]*i(t+a-f,e+s-d,r,n)}return a?round(a(l)):l}function normalizeSeparatedKernel(t,e){const r=t.reduce((t,e)=>t+e,0)*e.reduce((t,e)=>t+e,0);if(r<0)throw new RangeError("this separated kernel cannot be normalized");const n=1/Math.sqrt(Math.abs(r));return[t.map(t=>t*n),e.map(t=>t*n)]}function getEigenvaluesForScore(t,e,r=5){if(!(r%2))throw new TypeError("windowSize must be an odd integer");const n=(SOBEL_X.length-1)/2,o=(r-1)/2,i=r+2*n,s={row:e.row-o-n,column:e.column-o-n},a=t.crop({origin:s,width:i,height:i}),h=rawDirectConvolution(a,SOBEL_X),l=rawDirectConvolution(a,SOBEL_Y);let c=0,u=0,f=0;for(let t=n;t<a.height-n;t++)for(let e=n;e<a.width-n;e++){const r=t*a.width+e,n=h[r],o=l[r];c+=n*n,u+=n*o,f+=o*o}const d=new Matrix([[c,u],[u,f]]);return new EigenvalueDecomposition(d).realEigenvalues}function getHarrisScore(t,e,r={}){const{harrisConstant:n=.04,windowSize:o=5}=r,i=getEigenvaluesForScore(t,e,o);return i[0]*i[1]-n*(i[0]+i[1])**2}function getShiTomasiScore(t,e,r={}){const{windowSize:n=5}=r,o=getEigenvaluesForScore(t,e,n);return Math.min(o[0],o[1])}function isFastKeypoint(t,e,r,n,o={}){const{nbContiguousPixels:i=12,threshold:s=20}=o,a=e.getValueByPoint(t,0);let h=0,l=0;if(i>=12){for(const r of n){const n=e.getValue(t.column+r.column,t.row+r.row,0);a-n>s?l++:n-a>s&&h++}if(l<3&&h<3)return!1}const c=[];for(const n of r){const r=e.getValue(t.column+n.column,t.row+n.row,0);a+s<=r?c.push(-1):r<=a-s?c.push(1):c.push(0)}let u=1;const f=[];for(let t=0;t<c.length;t++){c[t]===c[(t+1)%c.length]?t===c.length-1?0===f.length?f.push(u):f[0]+=u:u++:(f.push(u),u=1)}return Math.max(...f)>=i}function getFastKeypoints(t,e={}){const{fastRadius:r=3,scoreAlgorithm:n="FAST",scoreOptions:o}=e,i=o?.windowSize??7,s=getCirclePoints(r),a=getCompassPoints(r),{maxNbFeatures:h=500,nbContiguousPixels:l=3/4*s.length,threshold:c=20,nonMaxSuppression:u=!0,qualityThreshold:f=0}=e;function d(t,e){return getHarrisScore(t,e,o)}function g(t,e){return getFastScore(t,e,c,s)}function m(t,e){return getShiTomasiScore(t,e,o)}checkProcessable$1(t,{channels:[1],alpha:!1});const p=M$2(n).with("HARRIS",()=>d).with("FAST",()=>g).with("TOMASI",()=>m).exhaustive(),w="FAST"===n?0:(i+1)/2,y=[],b=new Float64Array(t.size).fill(Number.NEGATIVE_INFINITY);for(let e=w;e<t.height-w;e++)for(let r=w;r<t.width-w;r++){const n={row:e,column:r};if(isFastKeypoint(n,t,s,a,{nbContiguousPixels:l,threshold:c})){const e=p(t,n);b[getIndex(n.column,n.row,t,0)]=e,y.push({origin:n,score:e})}}let x=[];if(u)for(const e of y){const r=b[getIndex(e.origin.column,e.origin.row,t,0)];for(let n=0;n<surroundingPixels.length;n++){const o=surroundingPixels[n];if(b[getIndex(e.origin.column+o.column,e.origin.row+o.row,t,0)]>r)break;n===surroundingPixels.length-1&&x.push(e)}}else x=y;let M=-1/0;for(const t of x)t.score>M&&(M=t.score);const v=[],I=f*M;for(const t of x)t.score>=I&&(t.score/=M,v.push(t));return v.toSorted((t,e)=>e.score-t.score).slice(0,h)}function getPatchIntensityMoment(t,e,r,n={}){const{center:o=t.getCoordinates("center"),radius:i=3}=n;if(!checkBorderDistance(t,o,i))throw new RangeError("desired patch is too close to image border");const s=new Array(t.channels).fill(0),a=getFilledCirclePoints(i);for(const n of a)for(let i=0;i<t.channels;i++){const a=t.getValue(n.column+o.column,n.row+o.row,i);s[i]+=n.column**e*n.row**r*a}return s}function getPatchIntensityCentroid(t,e={}){const r=getPatchIntensityMoment(t,1,0,e),n=getPatchIntensityMoment(t,0,1,e),o=getPatchIntensityMoment(t,0,0,e),i=[];for(let e=0;e<t.channels;e++)0===o[e]?i.push({column:0,row:0}):i.push({column:r[e]/o[e],row:n[e]/o[e]});return i}function getOrientedFastKeypoints(t,e={}){const{centroidPatchDiameter:r=7}=e,n=getFastKeypoints(t,e),o=getRadius(r),i=[];for(const e of n)checkBorderDistance(t,e.origin,o)&&i.push(e);const s=[];for(const e of i){const r=toDegrees(getClockwiseAngle({column:0,row:0},getPatchIntensityCentroid(t,{center:e.origin,radius:o})[0]));s.push({...e,angle:r})}return s}function compareIntensity(t,e,r,n={}){const{origin:o=t.getCoordinates("center")}=n,i=sum$1(e,o),s=sum$1(r,o);return t.getValueByPoint(i,0)<t.getValueByPoint(s,0)}function getGaussianPoints(t,e,r={}){const{nbPoints:n=1024,xSeed:o=0,ySeed:i=1,sigma:s}=r,a=getGaussianValues(t,o,n,s),h=getGaussianValues(e,i,n,s),l=[];for(let t=0;t<n;t++)l.push({column:a[t],row:h[t]});return l}function getGaussianValues(t,e,r,n=t/5){const o=(t-1)/2,i=createRandomArray({distribution:"normal",seed:e,length:r,standardDeviation:n,mean:0}),s=getClampFromTo(-o,o);return i.map(t=>{const e=Math.round(t);return s(e)})}function extractSquareImage(t,e,r){const n=(r-1)/2,o={column:e.column-n,row:e.row-n};return t.crop({origin:o,width:r,height:r})}function getKeypointPatch(t,e,r={}){const{patchSize:n=31}=r,o=e.angle*Math.PI/180,i=Math.floor(n*(Math.abs(Math.cos(o))+Math.abs(Math.sin(o)))),s=i%2?i:i+1,a=getRadius(s);if(!checkBorderDistance(t,e.origin,a))throw new RangeError("keypoint is too close to border for given patch size");const h=extractSquareImage(t,e.origin,s).transformRotate(-e.angle,{center:"center",interpolationType:"nearest"}),l=h.getCoordinates("center");return extractSquareImage(h,l,n)}function getBriefDescriptors(t,e,r={}){const{patchSize:n=31,descriptorLength:o=256,smoothingOptions:i={sigma:Math.sqrt(2),size:Math.min(t.height,t.width,9)},pointsDistributionOptions:s}=r;if(checkProcessable$1(t,{alpha:!1,colorModel:"GREY"}),!(n%2))throw new TypeError("patchSize must be an odd integer");if(Math.min(t.width,t.height)<n)throw new RangeError(`image is too small for patchSize = ${n}`);const a=getGaussianPoints(n,n,{...s,nbPoints:2*o}),h=t.gaussianBlur(i),l=[],c=[];for(const t of e){let e;try{e=getKeypointPatch(h,t,{patchSize:n})}catch{continue}if(null===e)continue;const r=new Uint8Array(o);for(let t=0;t<o;t++){const n=a[t],i=a[t+o];r[t]=Number(compareIntensity(e,n,i))}l.push(r),c.push(t)}return{keypoints:c,descriptors:l}}function getBrief(t,e={}){const{centroidPatchDiameter:r=15,bestKptRadius:n=10,minScore:o}=e,i=getBriefDescriptors(t,getBestKeypointsInRadius(getOrientedFastKeypoints(t,{centroidPatchDiameter:r}),n));if(o)for(let t=0;t<i.keypoints.length;t++)if(i.keypoints[t].score<o)return{keypoints:i.keypoints.slice(0,t-1),descriptors:i.descriptors.slice(0,t-1)};return i}function getIntensityMoment(t,e,r,n={}){const{origin:o=t.getCoordinates("center")}=n,i=new Array(t.channels).fill(0);for(let n=0;n<t.height;n++)for(let s=0;s<t.width;s++){const a=s-o.column,h=n-o.row;for(let o=0;o<t.channels;o++){const l=t.getValue(s,n,o);i[o]+=a**e*h**r*l}}return i}function getIntensityCentroid(t){const e=getIntensityMoment(t,1,0),r=getIntensityMoment(t,0,1),n=getIntensityMoment(t,0,0),o=[];for(let i=0;i<t.channels;i++)0===n[i]?o.push({column:0,row:0}):o.push({column:e[i]/n[i],row:r[i]/n[i]});return o}function getHammingDistance(t,e){let r=0;for(let n=0;n<t.length;n++)r+=t[n]^e[n];return r}function bruteForceOneMatch(t,e,r={}){const{sort:n=!!r.nbBestMatches,nbBestMatches:o=t.length}=r,i=[];for(let r=0;r<t.length;r++){let n=Number.POSITIVE_INFINITY,o=0;for(let i=0;i<e.length;i++){const s=getHammingDistance(t[r],e[i]);s<n&&(n=s,o=i)}i.push({sourceIndex:r,destinationIndex:o,distance:n})}return n&&i.sort((t,e)=>t.distance-e.distance),i.slice(0,o)}function sortBySourceDest(t){const e=t.slice();return e.sort((t,e)=>t.sourceIndex<e.sourceIndex?-1:t.sourceIndex>e.sourceIndex?1:t.destinationIndex-e.destinationIndex),e}function sortByDestSource(t){const e=t.slice();return e.sort((t,e)=>t.destinationIndex<e.destinationIndex?-1:t.destinationIndex>e.destinationIndex?1:t.sourceIndex-e.sourceIndex),e}function getCrosscheckMatches(t,e){return crosscheck(bruteForceOneMatch(t,e),bruteForceOneMatch(e,t))}function crosscheck(t,e){const r=[],n=sortBySourceDest(t),o=sortByDestSource(e);let i=0,s=0;for(;i<n.length&&s<o.length;){const t=n[i],e=o[s];t.sourceIndex>e.destinationIndex?s++:t.sourceIndex<e.destinationIndex?i++:t.destinationIndex>e.sourceIndex?s++:t.destinationIndex<e.sourceIndex?i++:(r.push({distance:Math.max(t.distance,e.distance),sourceIndex:t.sourceIndex,destinationIndex:t.destinationIndex}),i++,s++)}return r}function getNodeApiOrThrow(t){if(!isNode())throw new Error(`${t} is only implemented for Node.js`);return{fs:process.getBuiltinModule("node:fs"),path:process.getBuiltinModule("node:path"),url:process.getBuiltinModule("node:url")}}let CanvasCtorBrowser,CanvasCtorNode,requireFn;function getCanvasContext(t,e){if(isNode()){if(!CanvasCtorNode)try{CanvasCtorNode=getRequireFn()("skia-canvas").Canvas}catch(t){throw new Error('drawText on Node.js requires the optional "skia-canvas" package. Install it with: npm install skia-canvas',{cause:t})}return new CanvasCtorNode(t,e).getContext("2d")}{CanvasCtorBrowser??=globalThis.OffscreenCanvas;const r=new CanvasCtorBrowser(t,e).getContext("2d");if(!r)throw new Error("Failed to create canvas context");return r}}function isNode(){return"undefined"!=typeof process&&"function"==typeof process.getBuiltinModule}function getRequireFn(){return requireFn??=process.getBuiltinModule("node:module").createRequire("undefined"==typeof document&&"undefined"==typeof location?require("url").pathToFileURL(__filename).href:"undefined"==typeof document?location.href:_documentCurrentScript&&"SCRIPT"===_documentCurrentScript.tagName.toUpperCase()&&_documentCurrentScript.src||new URL("image-js.umd.min.js",document.baseURI).href),requireFn}function drawText(t,e,r){if(Array.isArray(e)&&0===e.length)throw new Error("At least one text element must be provided");const n=getOutputImage(t,r,{clone:!0}),o=r?.font??"12px Helvetica",i=r?.fontColor??[255,255,255,255],s=getCanvasContext(t.width,t.height);if(Array.isArray(e))for(const r of e)drawTextElement(t,s,r,o,i);else drawTextElement(t,s,e,o,i);return layerCanvas(n.getRawImage().data,s.getImageData(0,0,t.width,t.height).data,t.channels,t.bitDepth),n}function drawTextElement(t,e,r,n,o){e.font=r.font??n;const i=r.fontColor??o;validateValues(i,t);const s=i[3]?i[3]/255:1,a=[i[0]??0,i[1]??0,i[2]??0,s];e.fillStyle=`rgba(${a.join(",")})`,e.fillText(String(r.content),r.position.column,r.position.row)}function layerCanvas(t,e,r,n){const o=CHANNEL_CONFIGS[r]||CHANNEL_CONFIGS[1],i=e.length>>>2,s=n-8;let a=0,h=0;for(let n=0;n<i;n++){const n=e[h+3]/255;if(0===n){a+=r,h+=4;continue}const i=1-n;for(const r of o.channelOffsets){const o=a+r;t[o]=Math.round(e[h+r]*n+t[o]*i)<<s}if(o.hasAlpha){const e=a+o.alphaOffset,r=n+(t[e]>>>s)/255*(1-n);t[e]=Math.round(255*r)<<s}a+=r,h+=4}}const CHANNEL_CONFIGS={1:{channelOffsets:[0],hasAlpha:!1,alphaOffset:void 0},2:{channelOffsets:[0],hasAlpha:!0,alphaOffset:1},3:{channelOffsets:[0,1,2],hasAlpha:!1,alphaOffset:void 0},4:{channelOffsets:[0,1,2],hasAlpha:!0,alphaOffset:3}};var ssim_web={exports:{}};function getColors(t,e,r={}){const{nbShades:n=6,minValueFactor:o=.2}=r,i=Math.max(...e),s=i*o,a=Math.floor((i-s)/(n-1)),h=getClampFromTo(0,t.maxValue),l=[];for(let t=0;t<n;t++){const r=[];for(const n of e)r.push(h(n-t*a));l.push(r)}return l}function getKeypointColor(t,e,r){const n=t[0].score,o=t.at(-1).score;if(o===n)return r[0];const i=t[e].score;return r[r.length-1-Math.floor((r.length-1)*(i-o)/(n-o))]}function drawKeypoints(t,e,r={}){const{markerSize:n=10,fill:o=!1,showScore:i=!1,origin:s={row:0,column:0},showScoreOptions:a}=r;let{maxNbKeypoints:h=e.length}=r;const{strokeColor:l=[255,0,0]}=r,c=a?.fontColor??[0,0,255],u=a?.font??"20px Helvetica";h>e.length&&(h=e.length);let f=getOutputImage(t,r,{clone:!0});"RGB"!==t.colorModel&&(f=f.convertColor("RGB"));const d=getColors(t,l,a),g=Math.ceil(n/2);for(let t=0;t<h;t++){const n=e[t];let a=l;i&&(a=getKeypointColor(e,t,d));const h=o?a:void 0,c=sum$1(n.origin,s);if(f.drawCircle(c,g,{fillColor:h,strokeColor:a,out:f}),isOrientedFastKeypoint(n)&&r.showOrientation){const t=c,e=n.angle*Math.PI/180,r={column:t.column+Math.round(g*Math.cos(e)),row:t.row-Math.round(g*Math.sin(e))};f.drawLine(t,r,{strokeColor:a,out:f})}}return i&&drawText(f,e.map(t=>({content:(100*t.score).toFixed(2),position:sum$1(t.origin,s),font:u,fontColor:[...c,255]})),{out:f}),f}function isOrientedFastKeypoint(t){return"angle"in t&&"number"==typeof t.angle}function getMatchColor(t,e,r){const n=t.at(-1).distance,o=t[0].distance;if(o===n)return r[0];const i=t[e].distance;return r[Math.floor((r.length-1)*(i-o)/(n-o))]}function sortByDistance(t){const e=t.slice();return e.sort((t,e)=>t.distance-e.distance),e}function scaleKeypoints(t,e){return 1===e?t.slice():t.map(t=>({origin:{row:t.origin.row*e,column:t.origin.column*e},score:t.score}))}function drawMatches(t,e,r,n,o={}){const{circleDiameter:i=10,strokeColor:s=[255,0,0],showDistance:a=!1,showDistanceOptions:h}=o;let{maxNbMatches:l=e.length}=o;l>e.length&&(l=e.length);const c=scaleKeypoints(r,t.scale),u=scaleKeypoints(n,t.scale),f=t.image,d=getColors(f,s,h),g=Math.ceil(i/2),m=sortByDistance(e);for(let r=0;r<l;r++){let n=s;a&&(n=getMatchColor(m,r,d));const o=c[e[r].sourceIndex].origin;f.drawCircle(o,g,{strokeColor:n,out:f});const i=u[e[r].destinationIndex].origin,h={column:i.column+t.destinationOrigin.column,row:i.row+t.destinationOrigin.row};f.drawCircle(h,g,{strokeColor:n,out:f}),f.drawLine(o,h,{out:f,strokeColor:n})}return f}!function(t,e){self,t.exports=(()=>{var t={132:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.bezkrovnySsim=void 0;var n=r(490),o=r(971);function i(t,e,r){var o=t.data,i=e.data,s=r.bitDepth,a=r.k1,h=r.k2,l=Math.pow(2,s)-1,c=Math.pow(a*l,2),u=Math.pow(h*l,2),f=n.average(o),d=n.average(i),g=n.variance(o,f),m=n.variance(i,d);return(2*f*d+c)*(2*n.covariance(o,i,f,d)+u)/((Math.pow(f,2)+Math.pow(d,2)+c)*(g+m+u))}e.bezkrovnySsim=function(t,e,r){for(var n=r.windowSize,s=Math.ceil(t.width/n),a=Math.ceil(t.height/n),h=new Array(s*a),l=0,c=0;c<t.height;c+=n)for(var u=0;u<t.width;u+=n){var f=Math.min(n,t.width-u),d=Math.min(n,t.height-c),g=o.sub(t,u,d,c,f),m=o.sub(e,u,d,c,f);h[l++]=i(g,m,r)}return{data:h,width:s,height:a}}},63:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.defaults=void 0,e.defaults={windowSize:11,k1:.01,k2:.03,bitDepth:8,downsample:"original",ssim:"weber",maxSize:256,rgb2grayVersion:"integer"}},441:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.downsample=void 0;var n=r(490),o=r(971);function i(t,e,r){var n=o.imfilter(t,e,"symmetric","same");return o.skip2d(n,[0,r,n.height],[0,r,n.width])}e.downsample=function(t,e){return"original"===e.downsample?function(t,e,r){void 0===r&&(r=256);var s=Math.min(t.width,e.height)/r,a=Math.round(s);if(a>1){var h=o.ones(a);t=i(t,h=n.divide2d(h,n.sum2d(h)),a),e=i(e,h,a)}return[t,e]}(t[0],t[1],e.maxSize):t}},607:function(t,e,r){var n=this&&this.__assign||function(){return(n=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};Object.defineProperty(e,"__esModule",{value:!0}),e.ssim=e.getOptions=void 0;var o=r(971),i=r(490),s=r(773),a=r(595),h=r(132),l=r(441),c=r(63),u=r(535),f={fast:s.ssim,original:a.originalSsim,bezkrovny:h.bezkrovnySsim,weber:u.weberSsim};function d(t){var e=n(n({},c.defaults),t);return function(t){if(Object.keys(t).forEach(function(t){if(!(t in c.defaults))throw new Error('"'+t+'" is not a valid option')}),"k1"in t&&("number"!=typeof t.k1||t.k1<0))throw new Error("Invalid k1 value. Default is "+c.defaults.k1);if("k2"in t&&("number"!=typeof t.k2||t.k2<0))throw new Error("Invalid k2 value. Default is "+c.defaults.k2);if(!(t.ssim in f))throw new Error("Invalid ssim option (use: "+Object.keys(f).join(", ")+")")}(e),e}function g(t,e,r){var n,s,a,h,c=(new Date).getTime(),u=function(t){var e=t[0],r=t[1],n=t[2];return f[n.ssim](e,r,n)}(function(t){var e=t[0],r=t[1],n=t[2],o=l.downsample([e,r],n);return[o[0],o[1],n]}((n=function(t){var e=t[0],r=t[1],n=t[2];if(e.width!==r.width||e.height!==r.height)throw new Error("Image dimensions do not match");return[e,r,n]}([t,e,d(r)]),s=n[0],a=n[1],"original"===(h=n[2]).rgb2grayVersion?[o.rgb2gray(s),o.rgb2gray(a),h]:[o.rgb2grayInteger(s),o.rgb2grayInteger(a),h])));return{mssim:void 0!==u.mssim?u.mssim:i.mean2d(u),ssim_map:u,performance:(new Date).getTime()-c}}e.getOptions=d,e.ssim=g,e.default=g},490:(t,e)=>{function r(t){return n(t)/t.length}function n(t){for(var e=0,r=0;r<t.length;r++)e+=t[r];return e}function o(t){for(var e=t.data,r=0,n=0;n<e.length;n++)r+=e[n];return r}function i(t,e){for(var r=t.data,n=t.width,o=t.height,i=new Array(r.length),s=0;s<r.length;s++)i[s]=r[s]+e;return{data:i,width:n,height:o}}function s(t,e){return"number"==typeof e?function(t,e){for(var r=t.data,n=t.width,o=t.height,i=new Array(r.length),s=0;s<r.length;s++)i[s]=r[s]*e;return{data:i,width:n,height:o}}(t,e):function(t,e){for(var r=t.data,n=t.width,o=t.height,i=e.data,s=new Array(r.length),a=0;a<r.length;a++)s[a]=r[a]*i[a];return{data:s,width:n,height:o}}(t,e)}Object.defineProperty(e,"__esModule",{value:!0}),e.covariance=e.variance=e.mean2d=e.square2d=e.multiply2d=e.divide2d=e.subtract2d=e.add2d=e.sum2d=e.floor=e.sum=e.average=void 0,e.average=r,e.sum=n,e.floor=function(t){for(var e=new Array(t.length),r=0;r<t.length;r++)e[r]=Math.floor(t[r]);return e},e.sum2d=o,e.add2d=function(t,e){return"number"==typeof e?i(t,e):function(t,e){for(var r=t.data,n=t.width,o=t.height,i=e.data,s=new Array(r.length),a=0;a<o;a++)for(var h=a*n,l=0;l<n;l++)s[h+l]=r[h+l]+i[h+l];return{data:s,width:n,height:o}}(t,e)},e.subtract2d=function(t,e){return"number"==typeof e?i(t,-e):function(t,e){for(var r=t.data,n=t.width,o=t.height,i=e.data,s=new Array(r.length),a=0;a<o;a++)for(var h=a*n,l=0;l<n;l++)s[h+l]=r[h+l]-i[h+l];return{data:s,width:n,height:o}}(t,e)},e.divide2d=function(t,e){return"number"==typeof e?function(t,e){for(var r=t.data,n=t.width,o=t.height,i=new Array(r.length),s=0;s<r.length;s++)i[s]=r[s]/e;return{data:i,width:n,height:o}}(t,e):function(t,e){for(var r=t.data,n=t.width,o=t.height,i=e.data,s=new Array(r.length),a=0;a<r.length;a++)s[a]=r[a]/i[a];return{data:s,width:n,height:o}}(t,e)},e.multiply2d=s,e.square2d=function(t){return s(t,t)},e.mean2d=function(t){return o(t)/t.data.length},e.variance=function(t,e){void 0===e&&(e=r(t));for(var n=0,o=t.length;o--;)n+=Math.pow(t[o]-e,2);return n/t.length},e.covariance=function(t,e,n,o){void 0===n&&(n=r(t)),void 0===o&&(o=r(e));for(var i=0,s=t.length;s--;)i+=(t[s]-n)*(e[s]-o);return i/t.length}},687:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.conv2=void 0;var n=r(490),o=r(298),i=r(118),s=r(799);function a(t,e,r){var n=t.data,o=t.width,i=t.height;void 0===r&&(r="full");for(var a=o+e.width-1,h=i+e.height-1,l=s.zeros(h,a).data,c=0;c<e.height;c++)for(var f=0;f<e.width;f++){var d=e.data[c*e.width+f];if(d)for(var g=0;g<i;g++)for(var m=0;m<o;m++)l[(g+c)*a+m+f]+=n[g*o+m]*d}return u({data:l,width:a,height:h},r,i,e.height,o,e.width)}function h(t,e,r){var i=e.data,s=e.width,a=e.height;void 0===r&&(r="full");var h=c(t,o.ones(a,1),o.ones(1,s),r);return n.multiply2d(h,i[0])}function l(t){for(var e=t.data,r=e[0],n=1;n<e.length;n++)if(e[n]!==r)return!1;return!0}function c(t,e,r,n){void 0===n&&(n="full");var o=Math.max(e.height,e.width),i=Math.max(r.height,r.width),s=a(t,e,"full");return u(a(s,r,"full"),n,t.height,o,t.width,i)}function u(t,e,r,n,o,s){if("full"===e)return t;if("same"===e){var a=Math.ceil((t.height-r)/2),h=Math.ceil((t.width-o)/2);return i.sub(t,a,r,h,o)}return i.sub(t,n-1,r-n+1,s-1,o-s+1)}e.conv2=function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];return t[2]&&t[2].data?c.apply(void 0,t):l(t[1])?h.apply(void 0,t):a.apply(void 0,t)}},346:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.filter2=void 0;var n=r(687);e.filter2=function(t,e,r){return void 0===r&&(r="same"),n.conv2(e,function(t){for(var e=t.data,r=t.width,n=t.height,o=new Array(e.length),i=0;i<n;i++)for(var s=0;s<r;s++)o[i*r+s]=e[(n-1-i)*r+r-1-s];return{data:o,width:r,height:n}}(t),r)}},470:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.fspecial=void 0;var n=r(490);e.fspecial=function(t,e,r){void 0===e&&(e=3),void 0===r&&(r=1.5);var o=function(t,e){for(var r=t.data,n=t.width,o=t.height,i=new Array(r.length),s=0;s<r.length;s++)i[s]=Math.exp(-r[s]/(2*Math.pow(e,2)));return{data:i,width:n,height:o}}(function(t){for(var e=2*t+1,r=new Array(Math.pow(e,2)),n=0;n<e;n++)for(var o=0;o<e;o++)r[n*e+o]=Math.pow(n-t,2)+Math.pow(o-t,2);return{data:r,width:e,height:e}}(e=(e-1)/2),r),i=n.sum2d(o);return n.divide2d(o,i)}},521:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.imfilter=void 0;var n=r(20),o=r(389),i=r(490),s=r(346);e.imfilter=function(t,e,r,a){return void 0===r&&(r="symmetric"),void 0===a&&(a="same"),t=function(t,e,r,s){if(t=o.padarray(t,i.floor([e/2,r/2]),s),0===n.mod(e,2)&&(t.data=t.data.slice(0,-t.width),t.height--),0===n.mod(r,2)){for(var a=[],h=0;h<t.data.length;h++)(h+1)%t.width!=0&&a.push(t.data[h]);t.data=a,t.width--}return t}(t,e.width,e.height,r),a=function(t){return"same"===t&&(t="valid"),t}(a),s.filter2(e,t,a)}},971:function(t,e,r){var n=this&&this.__createBinding||(Object.create?function(t,e,r,n){void 0===n&&(n=r),Object.defineProperty(t,n,{enumerable:!0,get:function(){return e[r]}})}:function(t,e,r,n){void 0===n&&(n=r),t[n]=e[r]}),o=this&&this.__exportStar||function(t,e){for(var r in t)"default"===r||Object.prototype.hasOwnProperty.call(e,r)||n(e,t,r)};Object.defineProperty(e,"__esModule",{value:!0}),o(r(687),e),o(r(346),e),o(r(470),e),o(r(521),e),o(r(150),e),o(r(298),e),o(r(389),e),o(r(582),e),o(r(439),e),o(r(118),e),o(r(240),e),o(r(799),e)},928:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.numbers=void 0,e.numbers=function(t,e,r){for(var n=e*t,o=new Array(n),i=0;i<n;i++)o[i]=r;return{data:o,width:e,height:t}}},20:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.mod=void 0,e.mod=function(t,e){return t-e*Math.floor(t/e)}},150:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.normpdf=void 0,e.normpdf=function(t,e,r){var n=t.data,o=t.width,i=t.height;void 0===e&&(e=0),void 0===r&&(r=1);for(var s=new Array(n.length),a=0;a<n.length;a++){var h=(n[a]-e)/r;s[a]=Math.exp(-Math.pow(h,2)/2)/(2.5066282746310007*r)}return{data:s,width:o,height:i}}},298:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ones=void 0;var n=r(928);e.ones=function(t,e){return void 0===e&&(e=t),n.numbers(t,e,1)}},389:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.padarray=void 0;var n=r(20);e.padarray=function(t,e,r,o){var i=e[0],s=e[1];return t.height>=i&&t.width>=s?function(t,e){for(var r=e[0],n=e[1],o=t.width+2*n,i=t.height+2*r,s=new Array(o*i),a=-r;a<0;a++){for(var h=-n;h<0;h++)s[(a+r)*o+h+n]=t.data[(Math.abs(a)-1)*t.width+Math.abs(h)-1];for(h=0;h<t.width;h++)s[(a+r)*o+h+n]=t.data[(Math.abs(a)-1)*t.width+h];for(h=t.width;h<t.width+n;h++)s[(a+r)*o+h+n]=t.data[(Math.abs(a)-1)*t.width+2*t.width-h-1]}for(a=0;a<t.height;a++){for(h=-n;h<0;h++)s[(a+r)*o+h+n]=t.data[a*t.width+Math.abs(h)-1];for(h=0;h<t.width;h++)s[(a+r)*o+h+n]=t.data[a*t.width+h];for(h=t.width;h<t.width+n;h++)s[(a+r)*o+h+n]=t.data[a*t.width+2*t.width-h-1]}for(a=t.height;a<t.height+r;a++){for(h=-n;h<0;h++)s[(a+r)*o+h+n]=t.data[(2*t.height-a-1)*t.width+Math.abs(h)-1];for(h=0;h<t.width;h++)s[(a+r)*o+h+n]=t.data[(2*t.height-a-1)*t.width+h];for(h=t.width;h<t.width+n;h++)s[(a+r)*o+h+n]=t.data[(2*t.height-a-1)*t.width+2*t.width-h-1]}return{data:s,width:o,height:i}}(t,[i,s]):function(t,e){for(var r=function(t,e){return{data:t.data.concat(e.data),height:t.height+e.height,width:t.width}}(t,function(t){for(var e=t.data,r=t.width,n=t.height,o=new Array(e.length),i=0;i<n;i++)for(var s=0;s<r;s++)o[i*r+s]=e[(n-1-i)*r+s];return{data:o,width:r,height:n}}(t)),o=t.height+2*e,i=new Array(t.width*o),s=-e;s<t.height+e;s++)for(var a=0;a<t.width;a++)i[(s+e)*t.width+a]=r.data[n.mod(s,r.height)*t.width+a];return{data:i,width:t.width,height:o}}(function(t,e){for(var r=t.width+2*e,o=new Array(r*t.height),i=function(t,e){for(var r=t.width+e.width,n=new Array(t.height*r),o=0;o<t.height;o++){for(var i=0;i<t.width;i++)n[o*r+i]=t.data[o*t.width+i];for(i=0;i<e.width;i++)n[o*r+i+t.width]=e.data[o*e.width+i]}return{data:n,width:r,height:t.height}}(t,function(t){for(var e=t.data,r=t.width,n=t.height,o=new Array(e.length),i=0;i<n;i++)for(var s=0;s<r;s++)o[i*r+s]=e[i*r+r-1-s];return{data:o,width:r,height:n}}(t)),s=0;s<t.height;s++)for(var a=-e;a<t.width+e;a++)o[s*r+a+e]=i.data[s*i.width+n.mod(a,i.width)];return{data:o,width:r,height:t.height}}(t,s),i)}},582:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.rgb2grayInteger=e.rgb2gray=void 0,e.rgb2gray=function(t){for(var e=t.data,r=t.width,n=t.height,o=new Uint8Array(r*n),i=0;i<e.length;i+=4)o[i/4]=.29894*e[i]+.58704*e[i+1]+.11402*e[i+2]+.5;return{data:Array.from(o),width:r,height:n}},e.rgb2grayInteger=function(t){for(var e=t.data,r=t.width,n=t.height,o=new Array(r*n),i=0;i<e.length;i+=4)o[i/4]=77*e[i]+150*e[i+1]+29*e[i+2]+128>>8;return{data:o,width:r,height:n}}},439:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.skip2d=void 0,e.skip2d=function(t,e,r){for(var n=e[0],o=e[1],i=e[2],s=r[0],a=r[1],h=r[2],l=Math.ceil((h-s)/a),c=Math.ceil((i-n)/o),u=new Array(l*c),f=0;f<c;f++)for(var d=0;d<l;d++){var g=n+f*o,m=s+d*a;u[f*l+d]=t.data[g*t.width+m]}return{data:u,width:l,height:c}}},118:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.sub=void 0,e.sub=function(t,e,r,n,o){for(var i=t.data,s=t.width,a=new Array(o*r),h=0;h<r;h++)for(var l=0;l<o;l++)a[h*o+l]=i[(n+h)*s+e+l];return{data:a,width:o,height:r}}},240:(t,e)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.transpose=void 0,e.transpose=function(t){for(var e=t.data,r=t.width,n=t.height,o=new Array(r*n),i=0;i<n;i++)for(var s=0;s<r;s++)o[s*n+i]=e[i*r+s];return{data:o,height:r,width:n}}},799:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.zeros=void 0;var n=r(928);e.zeros=function(t,e){return void 0===e&&(e=t),n.numbers(t,e,0)}},595:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.originalSsim=void 0;var n=r(490),o=r(971);e.originalSsim=function(t,e,r){var i=o.fspecial("gaussian",r.windowSize,1.5),s=Math.pow(2,r.bitDepth)-1,a=Math.pow(r.k1*s,2),h=Math.pow(r.k2*s,2);i=n.divide2d(i,n.sum2d(i));var l=o.filter2(i,t,"valid"),c=o.filter2(i,e,"valid"),u=n.square2d(l),f=n.square2d(c),d=n.multiply2d(l,c),g=n.square2d(t),m=n.square2d(e),p=n.subtract2d(o.filter2(i,g,"valid"),u),w=n.subtract2d(o.filter2(i,m,"valid"),f),y=n.subtract2d(o.filter2(i,n.multiply2d(t,e),"valid"),d);if(a>0&&h>0){var b=n.add2d(n.multiply2d(d,2),a),x=n.add2d(n.multiply2d(y,2),h),M=n.add2d(n.add2d(u,f),a),v=n.add2d(n.add2d(p,w),h);return n.divide2d(n.multiply2d(b,x),n.multiply2d(M,v))}var I=n.multiply2d(d,2),A=n.multiply2d(y,2),k=n.add2d(u,f),R=n.add2d(p,w);return n.divide2d(n.multiply2d(I,A),n.multiply2d(k,R))}},773:(t,e,r)=>{Object.defineProperty(e,"__esModule",{value:!0}),e.ssim=void 0;var n=r(490),o=r(971);e.ssim=function(t,e,r){var i=o.normpdf(function(t){for(var e=Math.floor(t/2),r=new Array(2*e+1),n=-e;n<=e;n++)r[n+e]=Math.abs(n);return{data:r,width:r.length,height:1}}(r.windowSize),0,1.5),s=Math.pow(2,r.bitDepth)-1,a=Math.pow(r.k1*s,2),h=Math.pow(r.k2*s,2);i=n.divide2d(i,n.sum2d(i));var l=o.transpose(i),c=o.conv2(t,i,l,"valid"),u=o.conv2(e,i,l,"valid"),f=n.square2d(c),d=n.square2d(u),g=n.multiply2d(c,u),m=n.square2d(t),p=n.square2d(e),w=n.subtract2d(o.conv2(m,i,l,"valid"),f),y=n.subtract2d(o.conv2(p,i,l,"valid"),d),b=n.subtract2d(o.conv2(n.multiply2d(t,e),i,l,"valid"),g);return a>0&&h>0?function(t,e,r,o,i,s,a,h){var l=n.add2d(n.multiply2d(t,2),a),c=n.add2d(n.multiply2d(e,2),h),u=n.add2d(n.add2d(r,o),a),f=n.add2d(n.add2d(i,s),h);return n.divide2d(n.multiply2d(l,c),n.multiply2d(u,f))}(g,b,f,d,w,y,a,h):function(t,e,r,o,i,s){var a=n.multiply2d(t,2),h=n.multiply2d(e,2),l=n.add2d(r,o),c=n.add2d(i,s);return n.divide2d(n.multiply2d(a,h),n.multiply2d(l,c))}(g,b,f,d,w,y)}},535:function(t,e){var r=this&&this.__assign||function(){return(r=Object.assign||function(t){for(var e,r=1,n=arguments.length;r<n;r++)for(var o in e=arguments[r])Object.prototype.hasOwnProperty.call(e,o)&&(t[o]=e[o]);return t}).apply(this,arguments)};function n(t,e,r,n){return{rightEdge:r[e*n+t+1],bottomEdge:r[(e+1)*n+t],bottomRightEdge:r[(e+1)*n+t+1]}}function o(t,e){for(var r=t.width,o=t.height,i=t.data,s=r+1,a=o+1,h=new Int32Array(s*a),l=o-1;l>=0;--l)for(var c=r-1;c>=0;--c){var u=n(c,l,h,s),f=u.rightEdge,d=u.bottomEdge,g=u.bottomRightEdge;h[l*s+c]=e(i[l*r+c],c,l)+f+d-g}return{data:h,height:a,width:s}}function i(t,e,r){for(var o=t.width,i=t.height,s=t.data,a=e.data,h=o+1,l=i+1,c=new Int32Array(h*l),u=i-1;u>=0;--u)for(var f=o-1;f>=0;--f){var d=n(f,u,c,h),g=d.rightEdge,m=d.bottomEdge,p=d.bottomRightEdge,w=u*o+f;c[u*h+f]=r(s[w],a[w],f,u)+g+m-p}return{data:c,height:l,width:h}}function s(t,e,r){for(var n=t.width,o=t.height,i=t.data,s=n-1,a=o-1,h=s-e+1,l=a-e+1,c=new Int32Array(h*l),u=0;u<a;++u)for(var f=0;f<s;++f)if(f<h&&u<l){var d=i[n*u+f]-i[n*u+f+e]-i[n*(u+e)+f]+i[n*(u+e)+f+e];c[u*h+f]=d/r}return{height:l,width:h,data:c}}function a(t,e){return s(o(t,function(t){return t}),e,1)}function h(t,e,r){for(var n=r*r,i=s(o(t,function(t){return t*t}),r,1),a=0;a<e.data.length;++a){var h=e.data[a]/n,l=i.data[a]/n,c=h*h;i.data[a]=1024*(l-c)}return i}function l(t,e,r,n,o){for(var a=o*o,h=s(i(t,e,function(t,e){return t*e}),o,1),l=0;l<r.data.length;++l)h.data[l]=1024*(h.data[l]/a-r.data[l]/a*(n.data[l]/a));return h}Object.defineProperty(e,"__esModule",{value:!0}),e.weberSsim=e.windowCovariance=e.windowVariance=e.windowSums=e.windowMatrix=e.partialSumMatrix2=e.partialSumMatrix1=void 0,e.partialSumMatrix1=o,e.partialSumMatrix2=i,e.windowMatrix=s,e.windowSums=a,e.windowVariance=h,e.windowCovariance=l,e.weberSsim=function(t,e,n){for(var o=n.bitDepth,i=n.k1,s=n.k2,c=n.windowSize,u=Math.pow(2,o)-1,f=i*u*(i*u),d=s*u*(s*u),g=c*c,m=r(r({},t),{data:Int32Array.from(t.data,function(t){return t+.5})}),p=r(r({},e),{data:Int32Array.from(e.data,function(t){return t+.5})}),w=a(m,c),y=h(m,w,c),b=a(p,c),x=h(p,b,c),M=l(m,p,w,b,c),v=w.data.length,I=0,A=new Array(v),k=0;k<v;++k){var R=w.data[k]/g,E=b.data[k]/g,P=y.data[k]/1024,S=x.data[k]/1024,$=(2*R*E+f)*(M.data[k]/1024*2+d)/(R*R+E*E+f)/(P+S+d);A[k]=$,0==k?I=$:I+=($-I)/(k+1)}return{data:A,width:w.width,height:w.height,mssim:I}}}},e={};return function r(n){if(e[n])return e[n].exports;var o=e[n]={exports:{}};return t[n].call(o.exports,o,o.exports,r),o.exports}(607)})()}(ssim_web);const MontageDisposition={HORIZONTAL:"horizontal",VERTICAL:"vertical"};class Montage{sourceWidth;sourceHeight;destinationWidth;destinationHeight;destinationOrigin;width;height;scale;disposition;image;constructor(t,e,r={}){const{scale:n=1,disposition:o="horizontal"}=r;if(!Number.isInteger(n))throw new TypeError("scale must be an integer");if(this.scale=n,this.disposition=o,this.sourceWidth=n*t.width,this.destinationWidth=n*e.width,this.sourceHeight=n*t.height,this.destinationHeight=n*e.height,"horizontal"===o)this.destinationOrigin={row:0,column:this.sourceWidth},this.width=this.sourceWidth+this.destinationWidth,this.height=Math.max(this.sourceHeight,this.destinationHeight);else{if("vertical"!==o)throw new RangeError(`invalid disposition type: ${o}`);this.destinationOrigin={row:this.sourceHeight,column:0},this.width=Math.max(this.sourceWidth,this.destinationWidth),this.height=this.sourceHeight+this.destinationHeight}"RGB"!==t.colorModel&&(t=t.convertColor("RGB")),"RGB"!==e.colorModel&&(e=e.convertColor("RGB"));const i=new Image(this.width,this.height);t.resize({xFactor:n,yFactor:n}).copyTo(i,{out:i}),e.resize({xFactor:n,yFactor:n}).copyTo(i,{out:i,origin:this.destinationOrigin}),this.image=i}drawKeypoints(t,e={}){const r=scaleKeypoints(t,this.scale);this.image=drawKeypoints(this.image,r,e)}drawMatches(t,e,r,n={}){this.image=drawMatches(this,t,e,r,n)}}function overlapImages(t,e,r={}){const{origin:n={row:0,column:0},angle:o=0,scale:i=1}=r;if(0===i)throw new Error("Scale cannot be 0");t.colorModel!==ImageColorModel.GREY&&(t=t.grey()),e.colorModel!==ImageColorModel.GREY&&(e=e.grey());const s=t.invert(),a=e.invert(),h=s.transformRotate(o,{center:ImageCoordinates.TOP_LEFT}).resize({xFactor:i,yFactor:i}),l=Image.createFrom(a);return merge([h.copyTo(l,{origin:n}),a,l])}function filterEuclideanDistance(t,e,r,n={}){const{origin:o={column:0,row:0}}=n,i=t.slice();i.sort((t,e)=>t.destinationIndex-e.destinationIndex);const s=[];let a=[],h=i[0].destinationIndex;for(const t of i)t.destinationIndex===h?a.push(t):(a.sort((t,e)=>l(t)-l(e)),s.push(...a.slice(0,1)),h=t.destinationIndex,a=[t]);return a.sort((t,e)=>l(t)-l(e)),s.push(...a.slice(0,1)),s;function l(t){return(e[t.sourceIndex].origin.row-(r[t.destinationIndex].origin.row-o.row))**2+(e[t.sourceIndex].origin.column-r[t.destinationIndex].origin.column-o.column)**2}}function decode$4(t,e="utf8"){return new TextDecoder(e).decode(t)}const encoder$1=new TextEncoder;function encode$3(t){return encoder$1.encode(t)}const defaultByteLength=8192,hostBigEndian=(()=>{const t=new Uint8Array(4);return!((new Uint32Array(t.buffer)[0]=1)&t[0])})(),typedArrays={int8:globalThis.Int8Array,uint8:globalThis.Uint8Array,int16:globalThis.Int16Array,uint16:globalThis.Uint16Array,int32:globalThis.Int32Array,uint32:globalThis.Uint32Array,uint64:globalThis.BigUint64Array,int64:globalThis.BigInt64Array,float32:globalThis.Float32Array,float64:globalThis.Float64Array};class IOBuffer{buffer;byteLength;byteOffset;length;offset;lastWrittenByte;littleEndian;_data;_mark;_marks;constructor(t=defaultByteLength,e={}){let r=!1;"number"==typeof t?t=new ArrayBuffer(t):(r=!0,this.lastWrittenByte=t.byteLength);const n=e.offset?e.offset>>>0:0,o=t.byteLength-n;let i=n;(ArrayBuffer.isView(t)||t instanceof IOBuffer)&&(t.byteLength!==t.buffer.byteLength&&(i=t.byteOffset+n),t=t.buffer),this.lastWrittenByte=r?o:0,this.buffer=t,this.length=o,this.byteLength=o,this.byteOffset=i,this.offset=0,this.littleEndian=!0,this._data=new DataView(this.buffer,i,o),this._mark=0,this._marks=[]}available(t=1){return this.offset+t<=this.length}isLittleEndian(){return this.littleEndian}setLittleEndian(){return this.littleEndian=!0,this}isBigEndian(){return!this.littleEndian}setBigEndian(){return this.littleEndian=!1,this}skip(t=1){return this.offset+=t,this}back(t=1){return this.offset-=t,this}seek(t){return this.offset=t,this}mark(){return this._mark=this.offset,this}reset(){return this.offset=this._mark,this}pushMark(){return this._marks.push(this.offset),this}popMark(){const t=this._marks.pop();if(void 0===t)throw new Error("Mark stack empty");return this.seek(t),this}rewind(){return this.offset=0,this}ensureAvailable(t=1){if(!this.available(t)){const e=2*(this.offset+t),r=new Uint8Array(e);r.set(new Uint8Array(this.buffer)),this.buffer=r.buffer,this.length=e,this.byteLength=e,this._data=new DataView(this.buffer)}return this}readBoolean(){return 0!==this.readUint8()}readInt8(){return this._data.getInt8(this.offset++)}readUint8(){return this._data.getUint8(this.offset++)}readByte(){return this.readUint8()}readBytes(t=1){return this.readArray(t,"uint8")}readArray(t,e){const r=typedArrays[e].BYTES_PER_ELEMENT*t,n=this.byteOffset+this.offset,o=this.buffer.slice(n,n+r);if(this.littleEndian===hostBigEndian&&"uint8"!==e&&"int8"!==e){const t=new Uint8Array(this.buffer.slice(n,n+r));t.reverse();const o=new typedArrays[e](t.buffer);return this.offset+=r,o.reverse(),o}const i=new typedArrays[e](o);return this.offset+=r,i}readInt16(){const t=this._data.getInt16(this.offset,this.littleEndian);return this.offset+=2,t}readUint16(){const t=this._data.getUint16(this.offset,this.littleEndian);return this.offset+=2,t}readInt32(){const t=this._data.getInt32(this.offset,this.littleEndian);return this.offset+=4,t}readUint32(){const t=this._data.getUint32(this.offset,this.littleEndian);return this.offset+=4,t}readFloat32(){const t=this._data.getFloat32(this.offset,this.littleEndian);return this.offset+=4,t}readFloat64(){const t=this._data.getFloat64(this.offset,this.littleEndian);return this.offset+=8,t}readBigInt64(){const t=this._data.getBigInt64(this.offset,this.littleEndian);return this.offset+=8,t}readBigUint64(){const t=this._data.getBigUint64(this.offset,this.littleEndian);return this.offset+=8,t}readChar(){return String.fromCharCode(this.readInt8())}readChars(t=1){let e="";for(let r=0;r<t;r++)e+=this.readChar();return e}readUtf8(t=1){return decode$4(this.readBytes(t))}decodeText(t=1,e="utf8"){return decode$4(this.readBytes(t),e)}writeBoolean(t){return this.writeUint8(t?255:0),this}writeInt8(t){return this.ensureAvailable(1),this._data.setInt8(this.offset++,t),this._updateLastWrittenByte(),this}writeUint8(t){return this.ensureAvailable(1),this._data.setUint8(this.offset++,t),this._updateLastWrittenByte(),this}writeByte(t){return this.writeUint8(t)}writeBytes(t){this.ensureAvailable(t.length);for(let e=0;e<t.length;e++)this._data.setUint8(this.offset++,t[e]);return this._updateLastWrittenByte(),this}writeInt16(t){return this.ensureAvailable(2),this._data.setInt16(this.offset,t,this.littleEndian),this.offset+=2,this._updateLastWrittenByte(),this}writeUint16(t){return this.ensureAvailable(2),this._data.setUint16(this.offset,t,this.littleEndian),this.offset+=2,this._updateLastWrittenByte(),this}writeInt32(t){return this.ensureAvailable(4),this._data.setInt32(this.offset,t,this.littleEndian),this.offset+=4,this._updateLastWrittenByte(),this}writeUint32(t){return this.ensureAvailable(4),this._data.setUint32(this.offset,t,this.littleEndian),this.offset+=4,this._updateLastWrittenByte(),this}writeFloat32(t){return this.ensureAvailable(4),this._data.setFloat32(this.offset,t,this.littleEndian),this.offset+=4,this._updateLastWrittenByte(),this}writeFloat64(t){return this.ensureAvailable(8),this._data.setFloat64(this.offset,t,this.littleEndian),this.offset+=8,this._updateLastWrittenByte(),this}writeBigInt64(t){return this.ensureAvailable(8),this._data.setBigInt64(this.offset,t,this.littleEndian),this.offset+=8,this._updateLastWrittenByte(),this}writeBigUint64(t){return this.ensureAvailable(8),this._data.setBigUint64(this.offset,t,this.littleEndian),this.offset+=8,this._updateLastWrittenByte(),this}writeChar(t){return this.writeUint8(t.charCodeAt(0))}writeChars(t){for(let e=0;e<t.length;e++)this.writeUint8(t.charCodeAt(e));return this}writeUtf8(t){return this.writeBytes(encode$3(t))}toArray(){return new Uint8Array(this.buffer,this.byteOffset,this.lastWrittenByte)}getWrittenByteLength(){return this.lastWrittenByte-this.byteOffset}_updateLastWrittenByte(){this.offset>this.lastWrittenByte&&(this.lastWrittenByte=this.offset)}}class BMPDecoder{bufferData;pixelDataOffset;width;height;bitsPerPixel;xPixelsPerMeter;yPixelsPerMeter;compression;colorMasks;constructor(t){this.bufferData=new IOBuffer(t);const e=this.bufferData.readBytes(2);if(66!==e[0]&&77!==e[1])throw new Error("This is not a BMP image or the encoding is not correct.");if(this.pixelDataOffset=this.bufferData.skip(8).readUint32(),this.width=this.bufferData.skip(4).readUint32(),this.height=this.bufferData.readUint32(),this.bitsPerPixel=this.bufferData.seek(28).readUint16(),1!==this.bitsPerPixel&&8!==this.bitsPerPixel&&24!==this.bitsPerPixel&&32!==this.bitsPerPixel)throw new Error(`Invalid number of bits per pixel. Supported number of bits per pixel: 1, 8, 24, 32. Received: ${this.bitsPerPixel}`);if(this.compression=this.bufferData.readUint32(),0!==this.compression&&3!==this.compression)throw new Error("Only BI_RGB and BI_BITFIELDS compression methods are allowed. ");if(this.colorMasks=[this.bufferData.seek(54).readUint32(),this.bufferData.readUint32(),this.bufferData.readUint32()],32===this.bitsPerPixel&&(16711680!==this.colorMasks[0]||65280!==this.colorMasks[1]||255!==this.colorMasks[2]))throw new Error(`Unsupported color masks detected in 32-bit BMP image. Only standard RGBA (${16711680..toString(16)}, ${65280..toString(16)}, ${255..toString(16)}) masks are supported. Received: ${this.colorMasks[0].toString(16)},${this.colorMasks[1].toString(16)},${this.colorMasks[2].toString(16)}.`);this.bufferData.skip(1),this.xPixelsPerMeter=this.bufferData.seek(38).readInt32(),this.yPixelsPerMeter=this.bufferData.readInt32(),this.bufferData.skip(1)}decode(){this.bufferData.seek(this.pixelDataOffset),this.bufferData.setBigEndian();const t=Math.ceil(this.bitsPerPixel/8),e=t%2==0?t-1:t,r=this.decodePixelData(t,e);return{width:this.width,height:this.height,bitsPerPixel:this.bitsPerPixel,compression:this.compression,colorMasks:this.colorMasks,channels:t,components:e,data:r,yPixelsPerMeter:this.yPixelsPerMeter,xPixelsPerMeter:this.xPixelsPerMeter}}decodePixelData(t,e){const r=new Uint8Array(this.height*this.width*t);return 1===this.bitsPerPixel?this.decodeBitDepth1Pixels(r):t===e?this.decodeStandardPixels(r,t):this.decodePixelsWithAlpha(r,t,e),r}decodeBitDepth1Pixels(t){let e=0;for(let r=0;r<this.height;r++)for(let n=0;n<this.width;n++){const o=n%32;0===o&&(e=this.bufferData.readUint32()),e&1<<31-o&&(t[(this.height-r-1)*this.width+n]=1)}}decodeStandardPixels(t,e){const r=this.calculatePadding(e);for(let n=0;n<this.height;n++){const o=(this.height-n-1)*this.width;for(let r=0;r<this.width;r++)for(let n=e-1;n>=0;n--)t[(o+r)*e+n]=this.bufferData.readByte();this.bufferData.skip(r)}}decodePixelsWithAlpha(t,e,r){for(let n=0;n<this.height;n++){const o=(this.height-n-1)*this.width;for(let n=0;n<this.width;n++){const i=(o+n)*e;for(let e=r-1;e>=0;e--)t[i+e]=this.bufferData.readByte();t[i+r]=this.bufferData.readByte()}}}calculatePadding(t){return this.width*t%4==0?0:4-this.width*t%4}}const BITMAPV5HEADER={LogicalColorSpace:{LCS_sRGB:1934772034},GamutMappingIntent:{LCS_GM_GRAPHICS:2},DEFAULT_PIXELS_PER_METER:2835};class BMPEncoder{width;height;bitsPerPixel;channels;components;data;xPixelsPerMeter;yPixelsPerMeter;encoded=new IOBuffer;compression;colorMasks;constructor(t){if(!t.height||!t.width)throw new Error("ImageData width and height are required.");if(this.data=t.data,this.width=t.width,this.height=t.height,this.data.length!==t.width*t.height*t.channels)throw new Error("Invalid data length.");if(this.bitsPerPixel=t.bitsPerPixel,1!==this.bitsPerPixel&&8!==this.bitsPerPixel&&24!==this.bitsPerPixel&&32!==this.bitsPerPixel)throw new Error(`Invalid number of bits per pixel. Supported number of bits per pixel: 1, 8, 24, 32. Received: ${this.bitsPerPixel}`);this.channels=t.channels,this.components=this.channels%2==0?this.channels-1:this.channels,this.xPixelsPerMeter=t.xPixelsPerMeter??BITMAPV5HEADER.DEFAULT_PIXELS_PER_METER,this.yPixelsPerMeter=t.yPixelsPerMeter??BITMAPV5HEADER.DEFAULT_PIXELS_PER_METER,this.compression=t.compression??0,this.colorMasks=t.colorMasks??[16711680,65280,255]}encode(){this.encoded=new IOBuffer,this.encoded.skip(14),this.writeBitmapV5Header(),this.bitsPerPixel<=8&&this.writeColorTable();const t=this.encoded.offset;this.writePixelArray();const e=this.encoded.getWrittenByteLength();return this.encoded.rewind(),this.writeBitmapFileHeader(t,e),this.encoded.toArray()}writePixelArray(){this.encoded.setBigEndian(),1===this.bitsPerPixel?this.writeBitDepth1Pixels():this.channels===this.components?this.writeStandardPixels():this.writePixelsWithAlpha(),this.encoded.setLittleEndian()}writeBitDepth1Pixels(){let t=0;for(let e=this.height-1;e>=0;e--){for(let r=0;r<this.width;r++)r%32==0&&0!==r&&(this.encoded.writeUint32(t),t=0),t|=this.data[e*this.width+r]<<31-r%32;this.encoded.writeUint32(t),t=0}}writeStandardPixels(){for(let t=0;t<this.height;t++){const e=this.width*(this.height-t-1)*this.channels;for(let t=0;t<this.width;t++)for(let r=this.channels-1;r>=0;r--)this.encoded.writeByte(this.data[e+t*this.channels+r]);this.writePadding()}}writePixelsWithAlpha(){for(let t=0;t<this.height;t++){const e=this.width*(this.height-t-1);for(let t=0;t<this.width;t++){const r=(e+t)*this.channels;this.encoded.writeUint32(this.data[r+2]<<24|this.data[r+1]<<16|this.data[r]<<8|this.data[r+3])}}}writePadding(){const t=this.width*this.channels%4==0?0:4-this.width*this.channels%4;for(let e=0;e<t;e++)this.encoded.writeByte(0)}writeColorTable(){if(1===this.bitsPerPixel)this.encoded.writeUint32(0).writeUint32(16777215);else for(let t=0;t<256;t++)this.encoded.writeUint32(t<<16|t<<8|t)}writeBitmapFileHeader(t,e){this.encoded.writeChars("BM").writeInt32(e).writeUint16(0).writeUint16(0).writeUint32(t)}writeBitmapV5Header(){const t=4*Math.floor((this.bitsPerPixel*this.width+31)/32)*this.height;if(this.encoded.writeUint32(124).writeInt32(this.width).writeInt32(this.height).writeUint16(1).writeUint16(this.bitsPerPixel).writeUint32(this.compression).writeUint32(t).writeInt32(this.xPixelsPerMeter).writeInt32(this.yPixelsPerMeter).writeUint32(this.bitsPerPixel<=8?2**this.bitsPerPixel:0).writeUint32(this.bitsPerPixel<=8?2**this.bitsPerPixel:0),32===this.bitsPerPixel&&(16711680!==this.colorMasks[0]||65280!==this.colorMasks[1]||255!==this.colorMasks[2]))throw new Error(`Unsupported color masks detected in 32-bit BMP image. Only standard RGBA (${16711680..toString(16)}, ${65280..toString(16)}, ${255..toString(16)}) masks are supported. Received: ${this.colorMasks[0].toString(16)},${this.colorMasks[1].toString(16)},${this.colorMasks[2].toString(16)}.`);this.encoded.writeUint32(this.colorMasks[0]).writeUint32(this.colorMasks[1]).writeUint32(this.colorMasks[2]).writeUint32(this.channels===this.components?0:4278190080).writeUint32(BITMAPV5HEADER.LogicalColorSpace.LCS_sRGB).skip(36).skip(12).writeUint32(BITMAPV5HEADER.GamutMappingIntent.LCS_GM_GRAPHICS).skip(12)}}function encode$2(t){return new BMPEncoder(t).encode()}function decode$3(t){return new BMPDecoder(t).decode()}function encodeBmp(t){return encode$2(t instanceof Image?{width:t.width,height:t.height,components:t.components,bitsPerPixel:t.channels*t.bitDepth,channels:t.channels,data:t.getRawImage().data}:{width:t.width,height:t.height,components:1,bitsPerPixel:1,channels:1,data:t.getRawImage().data})}var encoder={exports:{}};!function(t){function e(t){var e,r,n,o,i,s=Math.floor,a=new Array(64),h=new Array(64),l=new Array(64),c=new Array(64),u=new Array(65535),f=new Array(65535),d=new Array(64),g=new Array(64),m=[],p=0,w=7,y=new Array(64),b=new Array(64),x=new Array(64),M=new Array(256),v=new Array(2048),I=[0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18,24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63],A=[0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0],k=[0,1,2,3,4,5,6,7,8,9,10,11],R=[0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,125],E=[1,2,3,0,4,17,5,18,33,49,65,6,19,81,97,7,34,113,20,50,129,145,161,8,35,66,177,193,21,82,209,240,36,51,98,114,130,9,10,22,23,24,25,26,37,38,39,40,41,42,52,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,225,226,227,228,229,230,231,232,233,234,241,242,243,244,245,246,247,248,249,250],P=[0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0],S=[0,1,2,3,4,5,6,7,8,9,10,11],$=[0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,119],C=[0,1,2,3,17,4,5,33,49,6,18,65,81,7,97,113,19,34,50,129,8,20,66,145,161,177,193,9,35,51,82,240,21,98,114,209,10,22,36,52,225,37,241,23,24,25,26,38,39,40,41,42,53,54,55,56,57,58,67,68,69,70,71,72,73,74,83,84,85,86,87,88,89,90,99,100,101,102,103,104,105,106,115,116,117,118,119,120,121,122,130,131,132,133,134,135,136,137,138,146,147,148,149,150,151,152,153,154,162,163,164,165,166,167,168,169,170,178,179,180,181,182,183,184,185,186,194,195,196,197,198,199,200,201,202,210,211,212,213,214,215,216,217,218,226,227,228,229,230,231,232,233,234,242,243,244,245,246,247,248,249,250];function B(t,e){for(var r=0,n=0,o=new Array,i=1;i<=16;i++){for(var s=1;s<=t[i];s++)o[e[n]]=[],o[e[n]][0]=r,o[e[n]][1]=i,n++,r++;r*=2}return o}function T(t){for(var e=t[0],r=t[1]-1;r>=0;)e&1<<r&&(p|=1<<w),r--,--w<0&&(255==p?(D(255),D(0)):D(p),w=7,p=0)}function D(t){m.push(t)}function _(t){D(t>>8&255),D(255&t)}function O(t,e,r,n,o){for(var i,s=o[0],a=o[240],h=function(t,e){var r,n,o,i,s,a,h,l,c,u,f=0;for(c=0;c<8;++c){r=t[f],n=t[f+1],o=t[f+2],i=t[f+3],s=t[f+4],a=t[f+5],h=t[f+6];var g=r+(l=t[f+7]),m=r-l,p=n+h,w=n-h,y=o+a,b=o-a,x=i+s,M=i-s,v=g+x,I=g-x,A=p+y,k=p-y;t[f]=v+A,t[f+4]=v-A;var R=.707106781*(k+I);t[f+2]=I+R,t[f+6]=I-R;var E=.382683433*((v=M+b)-(k=w+m)),P=.5411961*v+E,S=1.306562965*k+E,$=.707106781*(A=b+w),C=m+$,B=m-$;t[f+5]=B+P,t[f+3]=B-P,t[f+1]=C+S,t[f+7]=C-S,f+=8}for(f=0,c=0;c<8;++c){r=t[f],n=t[f+8],o=t[f+16],i=t[f+24],s=t[f+32],a=t[f+40],h=t[f+48];var T=r+(l=t[f+56]),D=r-l,_=n+h,O=n-h,V=o+a,N=o-a,F=i+s,L=i-s,U=T+F,z=T-F,G=_+V,q=_-V;t[f]=U+G,t[f+32]=U-G;var j=.707106781*(q+z);t[f+16]=z+j,t[f+48]=z-j;var Y=.382683433*((U=L+N)-(q=O+D)),H=.5411961*U+Y,W=1.306562965*q+Y,K=.707106781*(G=N+O),X=D+K,J=D-K;t[f+40]=J+H,t[f+24]=J-H,t[f+8]=X+W,t[f+56]=X-W,f++}for(c=0;c<64;++c)u=t[c]*e[c],d[c]=u>0?u+.5|0:u-.5|0;return d}(t,e),l=0;l<64;++l)g[I[l]]=h[l];var c=g[0]-r;r=g[0],0==c?T(n[0]):(T(n[f[i=32767+c]]),T(u[i]));for(var m=63;m>0&&0==g[m];m--);if(0==m)return T(s),r;for(var p,w=1;w<=m;){for(var y=w;0==g[w]&&w<=m;++w);var b=w-y;if(b>=16){p=b>>4;for(var x=1;x<=p;++x)T(a);b&=15}i=32767+g[w],T(o[(b<<4)+f[i]]),T(u[i]),w++}return 63!=m&&T(s),r}function V(t){if(t<=0&&(t=1),t>100&&(t=100),i!=t){(function(t){for(var e=[16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22,37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99],r=0;r<64;r++){var n=s((e[r]*t+50)/100);n<1?n=1:n>255&&(n=255),a[I[r]]=n}for(var o=[17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99],i=0;i<64;i++){var u=s((o[i]*t+50)/100);u<1?u=1:u>255&&(u=255),h[I[i]]=u}for(var f=[1,1.387039845,1.306562965,1.175875602,1,.785694958,.5411961,.275899379],d=0,g=0;g<8;g++)for(var m=0;m<8;m++)l[d]=1/(a[I[d]]*f[g]*f[m]*8),c[d]=1/(h[I[d]]*f[g]*f[m]*8),d++})(t<50?Math.floor(5e3/t):Math.floor(200-2*t)),i=t}}this.encode=function(t,i){var s;(new Date).getTime(),i&&V(i),m=new Array,p=0,w=7,_(65496),_(65504),_(16),D(74),D(70),D(73),D(70),D(0),D(1),D(1),D(0),_(1),_(1),D(0),D(0),void 0!==(s=t.comments)&&s.constructor===Array&&s.forEach(t=>{if("string"==typeof t){_(65534);var e,r=t.length;for(_(r+2),e=0;e<r;e++)D(t.charCodeAt(e))}}),function(t){if(t){_(65505),69===t[0]&&120===t[1]&&105===t[2]&&102===t[3]?_(t.length+2):(_(t.length+5+2),D(69),D(120),D(105),D(102),D(0));for(var e=0;e<t.length;e++)D(t[e])}}(t.exifBuffer),function(){_(65499),_(132),D(0);for(var t=0;t<64;t++)D(a[t]);D(1);for(var e=0;e<64;e++)D(h[e])}(),function(t,e){_(65472),_(17),D(8),_(e),_(t),D(3),D(1),D(17),D(0),D(2),D(17),D(1),D(3),D(17),D(1)}(t.width,t.height),function(){_(65476),_(418),D(0);for(var t=0;t<16;t++)D(A[t+1]);for(var e=0;e<=11;e++)D(k[e]);D(16);for(var r=0;r<16;r++)D(R[r+1]);for(var n=0;n<=161;n++)D(E[n]);D(1);for(var o=0;o<16;o++)D(P[o+1]);for(var i=0;i<=11;i++)D(S[i]);D(17);for(var s=0;s<16;s++)D($[s+1]);for(var a=0;a<=161;a++)D(C[a])}(),_(65498),_(12),D(3),D(1),D(0),D(2),D(17),D(3),D(17),D(0),D(63),D(0);var u=0,f=0,d=0;p=0,w=7,this.encode.displayName="_encode_";for(var g,M,I,B,N,F,L,U,z,G=t.data,q=t.width,j=t.height,Y=4*q,H=0;H<j;){for(g=0;g<Y;){for(F=N=Y*H+g,L=-1,U=0,z=0;z<64;z++)F=N+(U=z>>3)*Y+(L=4*(7&z)),H+U>=j&&(F-=Y*(H+1+U-j)),g+L>=Y&&(F-=g+L-Y+4),M=G[F++],I=G[F++],B=G[F++],y[z]=(v[M]+v[I+256|0]+v[B+512|0]>>16)-128,b[z]=(v[M+768|0]+v[I+1024|0]+v[B+1280|0]>>16)-128,x[z]=(v[M+1280|0]+v[I+1536|0]+v[B+1792|0]>>16)-128;u=O(y,l,u,e,n),f=O(b,c,f,r,o),d=O(x,c,d,r,o),g+=32}H+=8}if(w>=0){var W=[];W[1]=w+1,W[0]=(1<<w+1)-1,T(W)}return _(65497),Buffer.from(m)},(new Date).getTime(),t||(t=50),function(){for(var t=String.fromCharCode,e=0;e<256;e++)M[e]=t(e)}(),e=B(A,k),r=B(P,S),n=B(R,E),o=B($,C),function(){for(var t=1,e=2,r=1;r<=15;r++){for(var n=t;n<e;n++)f[32767+n]=r,u[32767+n]=[],u[32767+n][1]=r,u[32767+n][0]=n;for(var o=-(e-1);o<=-t;o++)f[32767+o]=r,u[32767+o]=[],u[32767+o][1]=r,u[32767+o][0]=e-1+o;t<<=1,e<<=1}}(),function(){for(var t=0;t<256;t++)v[t]=19595*t,v[t+256|0]=38470*t,v[t+512|0]=7471*t+32768,v[t+768|0]=-11059*t,v[t+1024|0]=-21709*t,v[t+1280|0]=32768*t+8421375,v[t+1536|0]=-27439*t,v[t+1792|0]=-5329*t}(),V(t),(new Date).getTime()}t.exports=function(t,r){void 0===r&&(r=50);var n=new e(r),o=n.encode(t,r);return{data:o,width:t.width,height:t.height}}}(encoder);var encoderExports=encoder.exports,decoder={exports:{}};!function(t){var e=function(){var t=new Int32Array([0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63]),e=4017,r=799,n=3406,o=2276,i=1567,s=3784,a=5793,h=2896;function l(){}function c(t,e){for(var r,n,o=0,i=[],s=16;s>0&&!t[s-1];)s--;i.push({children:[],index:0});var a,h=i[0];for(r=0;r<s;r++){for(n=0;n<t[r];n++){for((h=i.pop()).children[h.index]=e[o];h.index>0;){if(0===i.length)throw new Error("Could not recreate Huffman Table");h=i.pop()}for(h.index++,i.push(h);i.length<=r;)i.push(a={children:[],index:0}),h.children[h.index]=a.children,h=a;o++}r+1<s&&(i.push(a={children:[],index:0}),h.children[h.index]=a.children,h=a)}return i[0].children}function u(e,r,n,o,i,s,a,h,l,c){n.precision,n.samplesPerLine,n.scanLines;var u=n.mcusPerLine,f=n.progressive;n.maxH,n.maxV;var d=r,g=0,m=0;function p(){if(m>0)return m--,g>>m&1;if(255==(g=e[r++])){var t=e[r++];if(t)throw new Error("unexpected marker: "+(g<<8|t).toString(16))}return m=7,g>>>7}function w(t){for(var e,r=t;null!==(e=p());){if("number"==typeof(r=r[e]))return r;if("object"!=typeof r)throw new Error("invalid huffman sequence")}return null}function y(t){for(var e=0;t>0;){var r=p();if(null===r)return;e=e<<1|r,t--}return e}function b(t){var e=y(t);return e>=1<<t-1?e:e+(-1<<t)+1}var x=0;var M,v=0;function I(t,e,r,n,o){var i=r%u,s=(r/u|0)*t.v+n,a=i*t.h+o;void 0===t.blocks[s]&&c.tolerantDecoding||e(t,t.blocks[s][a])}function A(t,e,r){var n=r/t.blocksPerLine|0,o=r%t.blocksPerLine;void 0===t.blocks[n]&&c.tolerantDecoding||e(t,t.blocks[n][o])}var k,R,E,P,S,$,C=o.length;$=f?0===s?0===h?function(t,e){var r=w(t.huffmanTableDC),n=0===r?0:b(r)<<l;e[0]=t.pred+=n}:function(t,e){e[0]|=p()<<l}:0===h?function(e,r){if(x>0)x--;else for(var n=s,o=a;n<=o;){var i=w(e.huffmanTableAC),h=15&i,c=i>>4;if(0!==h)r[t[n+=c]]=b(h)*(1<<l),n++;else{if(c<15){x=y(c)+(1<<c)-1;break}n+=16}}}:function(e,r){for(var n=s,o=a,i=0;n<=o;){var h=t[n],c=r[h]<0?-1:1;switch(v){case 0:var u=w(e.huffmanTableAC),f=15&u;if(i=u>>4,0===f)i<15?(x=y(i)+(1<<i),v=4):(i=16,v=1);else{if(1!==f)throw new Error("invalid ACn encoding");M=b(f),v=i?2:3}continue;case 1:case 2:r[h]?r[h]+=(p()<<l)*c:0===--i&&(v=2==v?3:0);break;case 3:r[h]?r[h]+=(p()<<l)*c:(r[h]=M<<l,v=0);break;case 4:r[h]&&(r[h]+=(p()<<l)*c)}n++}4===v&&0===--x&&(v=0)}:function(e,r){var n=w(e.huffmanTableDC),o=0===n?0:b(n);r[0]=e.pred+=o;for(var i=1;i<64;){var s=w(e.huffmanTableAC),a=15&s,h=s>>4;if(0!==a)r[t[i+=h]]=b(a),i++;else{if(h<15)break;i+=16}}};var B,T,D,_,O=0;for(T=1==C?o[0].blocksPerLine*o[0].blocksPerColumn:u*n.mcusPerColumn,i||(i=T);O<T;){for(R=0;R<C;R++)o[R].pred=0;if(x=0,1==C)for(k=o[0],S=0;S<i;S++)A(k,$,O),O++;else for(S=0;S<i;S++){for(R=0;R<C;R++)for(D=(k=o[R]).h,_=k.v,E=0;E<_;E++)for(P=0;P<D;P++)I(k,$,O,E,P);if(++O===T)break}if(O===T)do{if(255===e[r]&&0!==e[r+1])break;r+=1}while(r<e.length-2);if(m=0,(B=e[r]<<8|e[r+1])<65280)throw new Error("marker was not found");if(!(B>=65488&&B<=65495))break;r+=2}return r-d}function f(t,l){var c,u,f=[],d=l.blocksPerLine,g=l.blocksPerColumn,m=d<<3,w=new Int32Array(64),y=new Uint8Array(64);function b(t,c,u){var f,d,g,m,p,w,y,b,x,M,v=l.quantizationTable,I=u;for(M=0;M<64;M++)I[M]=t[M]*v[M];for(M=0;M<8;++M){var A=8*M;0!=I[1+A]||0!=I[2+A]||0!=I[3+A]||0!=I[4+A]||0!=I[5+A]||0!=I[6+A]||0!=I[7+A]?(f=a*I[0+A]+128>>8,d=a*I[4+A]+128>>8,g=I[2+A],m=I[6+A],p=h*(I[1+A]-I[7+A])+128>>8,b=h*(I[1+A]+I[7+A])+128>>8,w=I[3+A]<<4,y=I[5+A]<<4,x=f-d+1>>1,f=f+d+1>>1,d=x,x=g*s+m*i+128>>8,g=g*i-m*s+128>>8,m=x,x=p-y+1>>1,p=p+y+1>>1,y=x,x=b+w+1>>1,w=b-w+1>>1,b=x,x=f-m+1>>1,f=f+m+1>>1,m=x,x=d-g+1>>1,d=d+g+1>>1,g=x,x=p*o+b*n+2048>>12,p=p*n-b*o+2048>>12,b=x,x=w*r+y*e+2048>>12,w=w*e-y*r+2048>>12,y=x,I[0+A]=f+b,I[7+A]=f-b,I[1+A]=d+y,I[6+A]=d-y,I[2+A]=g+w,I[5+A]=g-w,I[3+A]=m+p,I[4+A]=m-p):(x=a*I[0+A]+512>>10,I[0+A]=x,I[1+A]=x,I[2+A]=x,I[3+A]=x,I[4+A]=x,I[5+A]=x,I[6+A]=x,I[7+A]=x)}for(M=0;M<8;++M){var k=M;0!=I[8+k]||0!=I[16+k]||0!=I[24+k]||0!=I[32+k]||0!=I[40+k]||0!=I[48+k]||0!=I[56+k]?(f=a*I[0+k]+2048>>12,d=a*I[32+k]+2048>>12,g=I[16+k],m=I[48+k],p=h*(I[8+k]-I[56+k])+2048>>12,b=h*(I[8+k]+I[56+k])+2048>>12,w=I[24+k],y=I[40+k],x=f-d+1>>1,f=f+d+1>>1,d=x,x=g*s+m*i+2048>>12,g=g*i-m*s+2048>>12,m=x,x=p-y+1>>1,p=p+y+1>>1,y=x,x=b+w+1>>1,w=b-w+1>>1,b=x,x=f-m+1>>1,f=f+m+1>>1,m=x,x=d-g+1>>1,d=d+g+1>>1,g=x,x=p*o+b*n+2048>>12,p=p*n-b*o+2048>>12,b=x,x=w*r+y*e+2048>>12,w=w*e-y*r+2048>>12,y=x,I[0+k]=f+b,I[56+k]=f-b,I[8+k]=d+y,I[48+k]=d-y,I[16+k]=g+w,I[40+k]=g-w,I[24+k]=m+p,I[32+k]=m-p):(x=a*u[M+0]+8192>>14,I[0+k]=x,I[8+k]=x,I[16+k]=x,I[24+k]=x,I[32+k]=x,I[40+k]=x,I[48+k]=x,I[56+k]=x)}for(M=0;M<64;++M){var R=128+(I[M]+8>>4);c[M]=R<0?0:R>255?255:R}}p(m*g*8);for(var x=0;x<g;x++){var M=x<<3;for(c=0;c<8;c++)f.push(new Uint8Array(m));for(var v=0;v<d;v++){b(l.blocks[x][v],y,w);var I=0,A=v<<3;for(u=0;u<8;u++){var k=f[M+u];for(c=0;c<8;c++)k[A+c]=y[I++]}}}return f}function d(t){return t<0?0:t>255?255:t}l.prototype={load:function(t){var e=new XMLHttpRequest;e.open("GET",t,!0),e.responseType="arraybuffer",e.onload=function(){var t=new Uint8Array(e.response||e.mozResponseArrayBuffer);this.parse(t),this.onload&&this.onload()}.bind(this),e.send(null)},parse:function(e){var r=1e3*this.opts.maxResolutionInMP*1e3,n=0;function o(){var t=e[n]<<8|e[n+1];return n+=2,t}function i(){var t=o(),r=e.subarray(n,n+t-2);return n+=r.length,r}function s(t){var e,r,n=1,o=1;for(r in t.components)t.components.hasOwnProperty(r)&&(n<(e=t.components[r]).h&&(n=e.h),o<e.v&&(o=e.v));var i=Math.ceil(t.samplesPerLine/8/n),s=Math.ceil(t.scanLines/8/o);for(r in t.components)if(t.components.hasOwnProperty(r)){e=t.components[r];var a=Math.ceil(Math.ceil(t.samplesPerLine/8)*e.h/n),h=Math.ceil(Math.ceil(t.scanLines/8)*e.v/o),l=i*e.h,c=s*e.v,u=[];p(256*(c*l));for(var f=0;f<c;f++){for(var d=[],g=0;g<l;g++)d.push(new Int32Array(64));u.push(d)}e.blocksPerLine=a,e.blocksPerColumn=h,e.blocks=u}t.maxH=n,t.maxV=o,t.mcusPerLine=i,t.mcusPerColumn=s}e.length;var a,h,l=null,d=null,g=[],m=[],w=[],y=[],b=o(),x=-1;if(this.comments=[],65496!=b)throw new Error("SOI not found");for(b=o();65497!=b;){switch(b){case 65280:break;case 65504:case 65505:case 65506:case 65507:case 65508:case 65509:case 65510:case 65511:case 65512:case 65513:case 65514:case 65515:case 65516:case 65517:case 65518:case 65519:case 65534:var M=i();if(65534===b){var v=String.fromCharCode.apply(null,M);this.comments.push(v)}65504===b&&74===M[0]&&70===M[1]&&73===M[2]&&70===M[3]&&0===M[4]&&(l={version:{major:M[5],minor:M[6]},densityUnits:M[7],xDensity:M[8]<<8|M[9],yDensity:M[10]<<8|M[11],thumbWidth:M[12],thumbHeight:M[13],thumbData:M.subarray(14,14+3*M[12]*M[13])}),65505===b&&69===M[0]&&120===M[1]&&105===M[2]&&102===M[3]&&0===M[4]&&(this.exifBuffer=M.subarray(5,M.length)),65518===b&&65===M[0]&&100===M[1]&&111===M[2]&&98===M[3]&&101===M[4]&&0===M[5]&&(d={version:M[6],flags0:M[7]<<8|M[8],flags1:M[9]<<8|M[10],transformCode:M[11]});break;case 65499:for(var I=o()+n-2;n<I;){var A=e[n++];p(256);var k=new Int32Array(64);if(A>>4){if(A>>4!=1)throw new Error("DQT: invalid table spec");for(H=0;H<64;H++){k[t[H]]=o()}}else for(H=0;H<64;H++){k[t[H]]=e[n++]}g[15&A]=k}break;case 65472:case 65473:case 65474:o(),(a={}).extended=65473===b,a.progressive=65474===b,a.precision=e[n++],a.scanLines=o(),a.samplesPerLine=o(),a.components={},a.componentsOrder=[];var R=a.scanLines*a.samplesPerLine;if(R>r){var E=Math.ceil((R-r)/1e6);throw new Error(`maxResolutionInMP limit exceeded by ${E}MP`)}var P,S=e[n++];for(j=0;j<S;j++){P=e[n];var $=e[n+1]>>4,C=15&e[n+1],B=e[n+2];if($<=0||C<=0)throw new Error("Invalid sampling factor, expected values above 0");a.componentsOrder.push(P),a.components[P]={h:$,v:C,quantizationIdx:B},n+=3}s(a),m.push(a);break;case 65476:var T=o();for(j=2;j<T;){var D=e[n++],_=new Uint8Array(16),O=0;for(H=0;H<16;H++,n++)O+=_[H]=e[n];p(16+O);var V=new Uint8Array(O);for(H=0;H<O;H++,n++)V[H]=e[n];j+=17+O,(D>>4?w:y)[15&D]=c(_,V)}break;case 65501:o(),h=o();break;case 65500:o(),o();break;case 65498:o();var N=e[n++],F=[];for(j=0;j<N;j++){W=a.components[e[n++]];var L=e[n++];W.huffmanTableDC=y[L>>4],W.huffmanTableAC=w[15&L],F.push(W)}var U=e[n++],z=e[n++],G=e[n++],q=u(e,n,a,F,h,U,z,G>>4,15&G,this.opts);n+=q;break;case 65535:255!==e[n]&&n--;break;default:if(255==e[n-3]&&e[n-2]>=192&&e[n-2]<=254){n-=3;break}if(224===b||225==b){if(-1!==x)throw new Error(`first unknown JPEG marker at offset ${x.toString(16)}, second unknown JPEG marker ${b.toString(16)} at offset ${(n-1).toString(16)}`);x=n-1;const t=o();if(255===e[n+t-2]){n+=t-2;break}}throw new Error("unknown JPEG marker "+b.toString(16))}b=o()}if(1!=m.length)throw new Error("only single frame JPEGs supported");for(var j=0;j<m.length;j++){var Y=m[j].components;for(var H in Y)Y[H].quantizationTable=g[Y[H].quantizationIdx],delete Y[H].quantizationIdx}this.width=a.samplesPerLine,this.height=a.scanLines,this.jfif=l,this.adobe=d,this.components=[];for(j=0;j<a.componentsOrder.length;j++){var W=a.components[a.componentsOrder[j]];this.components.push({lines:f(0,W),scaleX:W.h/a.maxH,scaleY:W.v/a.maxV})}},getData:function(t,e){var r,n,o,i,s,a,h,l,c,u,f,g,m,w,y,b,x,M,v,I,A,k=this.width/t,R=this.height/e,E=0,P=t*e*this.components.length;p(P);var S=new Uint8Array(P);switch(this.components.length){case 1:for(r=this.components[0],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],c=0;c<t;c++)f=s[0|c*r.scaleX*k],S[E++]=f;break;case 2:for(r=this.components[0],n=this.components[1],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],a=n.lines[0|u*n.scaleY*R],c=0;c<t;c++)f=s[0|c*r.scaleX*k],S[E++]=f,f=a[0|c*n.scaleX*k],S[E++]=f;break;case 3:for(A=!0,this.adobe&&this.adobe.transformCode?A=!0:void 0!==this.opts.colorTransform&&(A=!!this.opts.colorTransform),r=this.components[0],n=this.components[1],o=this.components[2],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],a=n.lines[0|u*n.scaleY*R],h=o.lines[0|u*o.scaleY*R],c=0;c<t;c++)A?(f=s[0|c*r.scaleX*k],g=a[0|c*n.scaleX*k],M=d(f+1.402*((m=h[0|c*o.scaleX*k])-128)),v=d(f-.3441363*(g-128)-.71413636*(m-128)),I=d(f+1.772*(g-128))):(M=s[0|c*r.scaleX*k],v=a[0|c*n.scaleX*k],I=h[0|c*o.scaleX*k]),S[E++]=M,S[E++]=v,S[E++]=I;break;case 4:if(!this.adobe)throw new Error("Unsupported color mode (4 components)");for(A=!1,this.adobe&&this.adobe.transformCode?A=!0:void 0!==this.opts.colorTransform&&(A=!!this.opts.colorTransform),r=this.components[0],n=this.components[1],o=this.components[2],i=this.components[3],u=0;u<e;u++)for(s=r.lines[0|u*r.scaleY*R],a=n.lines[0|u*n.scaleY*R],h=o.lines[0|u*o.scaleY*R],l=i.lines[0|u*i.scaleY*R],c=0;c<t;c++)A?(f=s[0|c*r.scaleX*k],g=a[0|c*n.scaleX*k],m=h[0|c*o.scaleX*k],w=l[0|c*i.scaleX*k],y=255-d(f+1.402*(m-128)),b=255-d(f-.3441363*(g-128)-.71413636*(m-128)),x=255-d(f+1.772*(g-128))):(y=s[0|c*r.scaleX*k],b=a[0|c*n.scaleX*k],x=h[0|c*o.scaleX*k],w=l[0|c*i.scaleX*k]),S[E++]=255-y,S[E++]=255-b,S[E++]=255-x,S[E++]=255-w;break;default:throw new Error("Unsupported color mode")}return S},copyToImageData:function(t,e){var r,n,o,i,s,a,h,l,c,u=t.width,f=t.height,g=t.data,m=this.getData(u,f),p=0,w=0;switch(this.components.length){case 1:for(n=0;n<f;n++)for(r=0;r<u;r++)o=m[p++],g[w++]=o,g[w++]=o,g[w++]=o,e&&(g[w++]=255);break;case 3:for(n=0;n<f;n++)for(r=0;r<u;r++)h=m[p++],l=m[p++],c=m[p++],g[w++]=h,g[w++]=l,g[w++]=c,e&&(g[w++]=255);break;case 4:for(n=0;n<f;n++)for(r=0;r<u;r++)s=m[p++],a=m[p++],o=m[p++],h=255-d(s*(1-(i=m[p++])/255)+i),l=255-d(a*(1-i/255)+i),c=255-d(o*(1-i/255)+i),g[w++]=h,g[w++]=l,g[w++]=c,e&&(g[w++]=255);break;default:throw new Error("Unsupported color mode")}}};var g=0,m=0;function p(t=0){var e=g+t;if(e>m){var r=Math.ceil((e-m)/1024/1024);throw new Error(`maxMemoryUsageInMB limit exceeded by at least ${r}MB`)}g=e}return l.resetMaxMemoryUsage=function(t){g=0,m=t},l.getBytesAllocated=function(){return g},l.requestMemoryAllocation=p,l}();t.exports=function(t,r={}){var n={colorTransform:void 0,useTArray:!1,formatAsRGBA:!0,tolerantDecoding:!0,maxResolutionInMP:100,maxMemoryUsageInMB:512,...r},o=new Uint8Array(t),i=new e;i.opts=n,e.resetMaxMemoryUsage(1024*n.maxMemoryUsageInMB*1024),i.parse(o);var s=n.formatAsRGBA?4:3,a=i.width*i.height*s;try{e.requestMemoryAllocation(a);var h={width:i.width,height:i.height,exifBuffer:i.exifBuffer,data:n.useTArray?new Uint8Array(a):Buffer.alloc(a)};i.comments.length>0&&(h.comments=i.comments)}catch(t){if(t instanceof RangeError)throw new Error("Could not allocate enough memory for the image. Required: "+a);if(t instanceof ReferenceError&&"Buffer is not defined"===t.message)throw new Error("Buffer is not globally defined in this environment. Consider setting useTArray to true");throw t}return i.copyToImageData(h,n.formatAsRGBA),h}}(decoder);var decoderExports=decoder.exports,encode$1=encoderExports,decode$2=decoderExports,jpegJs={encode:encode$1,decode:decode$2};function encodeJpeg(t,e={}){const{quality:r=50}=e;("RGBA"!==t.colorModel||t instanceof Mask)&&(t=t.convertColor("RGBA")),8!==t.bitDepth&&(t=t.convertBitDepth(8));const n=jpegJs.encode(t.getRawImage(),r).data;return new Uint8Array(n.buffer,n.byteOffset,n.byteLength)}var u8=Uint8Array,u16=Uint16Array,i32=Int32Array,fleb=new u8([0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0,0]),fdeb=new u8([0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,0,0]),clim=new u8([16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15]),freb=function(t,e){for(var r=new u16(31),n=0;n<31;++n)r[n]=e+=1<<t[n-1];var o=new i32(r[30]);for(n=1;n<30;++n)for(var i=r[n];i<r[n+1];++i)o[i]=i-r[n]<<5|n;return{b:r,r:o}},_a=freb(fleb,2),fl=_a.b,revfl=_a.r;fl[28]=258,revfl[258]=28;for(var _b=freb(fdeb,0),fd=_b.b,revfd=_b.r,rev=new u16(32768),i=0;i<32768;++i){var x=(43690&i)>>1|(21845&i)<<1;x=(52428&x)>>2|(13107&x)<<2,x=(61680&x)>>4|(3855&x)<<4,rev[i]=((65280&x)>>8|(255&x)<<8)>>1}for(var hMap=function(t,e,r){for(var n=t.length,o=0,i=new u16(e);o<n;++o)t[o]&&++i[t[o]-1];var s,a=new u16(e);for(o=1;o<e;++o)a[o]=a[o-1]+i[o-1]<<1;if(r){s=new u16(1<<e);var h=15-e;for(o=0;o<n;++o)if(t[o])for(var l=o<<4|t[o],c=e-t[o],u=a[t[o]-1]++<<c,f=u|(1<<c)-1;u<=f;++u)s[rev[u]>>h]=l}else for(s=new u16(n),o=0;o<n;++o)t[o]&&(s[o]=rev[a[t[o]-1]++]>>15-t[o]);return s},flt=new u8(288),i=0;i<144;++i)flt[i]=8;for(var i=144;i<256;++i)flt[i]=9;for(var i=256;i<280;++i)flt[i]=7;for(var i=280;i<288;++i)flt[i]=8;for(var fdt=new u8(32),i=0;i<32;++i)fdt[i]=5;var flm=hMap(flt,9,0),flrm=hMap(flt,9,1),fdm=hMap(fdt,5,0),fdrm=hMap(fdt,5,1),max=function(t){for(var e=t[0],r=1;r<t.length;++r)t[r]>e&&(e=t[r]);return e},bits=function(t,e,r){var n=e/8|0;return(t[n]|t[n+1]<<8)>>(7&e)&r},bits16=function(t,e){var r=e/8|0;return(t[r]|t[r+1]<<8|t[r+2]<<16)>>(7&e)},shft=function(t){return(t+7)/8|0},slc=function(t,e,r){return(null==e||e<0)&&(e=0),(null==r||r>t.length)&&(r=t.length),new u8(t.subarray(e,r))},ec=["unexpected EOF","invalid block type","invalid length/literal","invalid distance","stream finished","no stream handler",,"no callback","invalid UTF-8 data","extra field too long","date not in range 1980-2099","filename too long","stream finishing","invalid zip data"],err=function(t,e,r){var n=new Error(e||ec[t]);if(n.code=t,Error.captureStackTrace&&Error.captureStackTrace(n,err),!r)throw n;return n},inflt=function(t,e,r,n){var o=t.length;if(!o||e.f&&!e.l)return r||new u8(0);var i=!r,s=i||2!=e.i,a=e.i;i&&(r=new u8(3*o));var h=function(t){var e=r.length;if(t>e){var n=new u8(Math.max(2*e,t));n.set(r),r=n}},l=e.f||0,c=e.p||0,u=e.b||0,f=e.l,d=e.d,g=e.m,m=e.n,p=8*o;do{if(!f){l=bits(t,c,1);var w=bits(t,c+1,3);if(c+=3,!w){var y=t[(S=shft(c)+4)-4]|t[S-3]<<8,b=S+y;if(b>o){a&&err(0);break}s&&h(u+y),r.set(t.subarray(S,b),u),e.b=u+=y,e.p=c=8*b,e.f=l;continue}if(1==w)f=flrm,d=fdrm,g=9,m=5;else if(2==w){var x=bits(t,c,31)+257,M=bits(t,c+10,15)+4,v=x+bits(t,c+5,31)+1;c+=14;for(var I=new u8(v),A=new u8(19),k=0;k<M;++k)A[clim[k]]=bits(t,c+3*k,7);c+=3*M;var R=max(A),E=(1<<R)-1,P=hMap(A,R,1);for(k=0;k<v;){var S,$=P[bits(t,c,E)];if(c+=15&$,(S=$>>4)<16)I[k++]=S;else{var C=0,B=0;for(16==S?(B=3+bits(t,c,3),c+=2,C=I[k-1]):17==S?(B=3+bits(t,c,7),c+=3):18==S&&(B=11+bits(t,c,127),c+=7);B--;)I[k++]=C}}var T=I.subarray(0,x),D=I.subarray(x);g=max(T),m=max(D),f=hMap(T,g,1),d=hMap(D,m,1)}else err(1);if(c>p){a&&err(0);break}}s&&h(u+131072);for(var _=(1<<g)-1,O=(1<<m)-1,V=c;;V=c){var N=(C=f[bits16(t,c)&_])>>4;if((c+=15&C)>p){a&&err(0);break}if(C||err(2),N<256)r[u++]=N;else{if(256==N){V=c,f=null;break}var F=N-254;if(N>264){var L=fleb[k=N-257];F=bits(t,c,(1<<L)-1)+fl[k],c+=L}var U=d[bits16(t,c)&O],z=U>>4;U||err(3),c+=15&U;D=fd[z];if(z>3){L=fdeb[z];D+=bits16(t,c)&(1<<L)-1,c+=L}if(c>p){a&&err(0);break}s&&h(u+131072);var G=u+F;if(u<D){var q=0-D,j=Math.min(D,G);for(q+u<0&&err(3);u<j;++u)r[u]=n[q+u]}for(;u<G;++u)r[u]=r[u-D]}}e.l=f,e.p=V,e.b=u,e.f=l,f&&(l=1,e.m=g,e.d=d,e.n=m)}while(!l);return u!=r.length&&i?slc(r,0,u):r.subarray(0,u)},wbits=function(t,e,r){r<<=7&e;var n=e/8|0;t[n]|=r,t[n+1]|=r>>8},wbits16=function(t,e,r){r<<=7&e;var n=e/8|0;t[n]|=r,t[n+1]|=r>>8,t[n+2]|=r>>16},hTree=function(t,e){for(var r=[],n=0;n<t.length;++n)t[n]&&r.push({s:n,f:t[n]});var o=r.length,i=r.slice();if(!o)return{t:et,l:0};if(1==o){var s=new u8(r[0].s+1);return s[r[0].s]=1,{t:s,l:1}}r.sort(function(t,e){return t.f-e.f}),r.push({s:-1,f:25001});var a=r[0],h=r[1],l=0,c=1,u=2;for(r[0]={s:-1,f:a.f+h.f,l:a,r:h};c!=o-1;)a=r[r[l].f<r[u].f?l++:u++],h=r[l!=c&&r[l].f<r[u].f?l++:u++],r[c++]={s:-1,f:a.f+h.f,l:a,r:h};var f=i[0].s;for(n=1;n<o;++n)i[n].s>f&&(f=i[n].s);var d=new u16(f+1),g=ln(r[c-1],d,0);if(g>e){n=0;var m=0,p=g-e,w=1<<p;for(i.sort(function(t,e){return d[e.s]-d[t.s]||t.f-e.f});n<o;++n){var y=i[n].s;if(!(d[y]>e))break;m+=w-(1<<g-d[y]),d[y]=e}for(m>>=p;m>0;){var b=i[n].s;d[b]<e?m-=1<<e-d[b]++-1:++n}for(;n>=0&&m;--n){var x=i[n].s;d[x]==e&&(--d[x],++m)}g=e}return{t:new u8(d),l:g}},ln=function(t,e,r){return-1==t.s?Math.max(ln(t.l,e,r+1),ln(t.r,e,r+1)):e[t.s]=r},lc=function(t){for(var e=t.length;e&&!t[--e];);for(var r=new u16(++e),n=0,o=t[0],i=1,s=function(t){r[n++]=t},a=1;a<=e;++a)if(t[a]==o&&a!=e)++i;else{if(!o&&i>2){for(;i>138;i-=138)s(32754);i>2&&(s(i>10?i-11<<5|28690:i-3<<5|12305),i=0)}else if(i>3){for(s(o),--i;i>6;i-=6)s(8304);i>2&&(s(i-3<<5|8208),i=0)}for(;i--;)s(o);i=1,o=t[a]}return{c:r.subarray(0,n),n:e}},clen=function(t,e){for(var r=0,n=0;n<e.length;++n)r+=t[n]*e[n];return r},wfblk=function(t,e,r){var n=r.length,o=shft(e+2);t[o]=255&n,t[o+1]=n>>8,t[o+2]=255^t[o],t[o+3]=255^t[o+1];for(var i=0;i<n;++i)t[o+i+4]=r[i];return 8*(o+4+n)},wblk=function(t,e,r,n,o,i,s,a,h,l,c){wbits(e,c++,r),++o[256];for(var u=hTree(o,15),f=u.t,d=u.l,g=hTree(i,15),m=g.t,p=g.l,w=lc(f),y=w.c,b=w.n,x=lc(m),M=x.c,v=x.n,I=new u16(19),A=0;A<y.length;++A)++I[31&y[A]];for(A=0;A<M.length;++A)++I[31&M[A]];for(var k=hTree(I,7),R=k.t,E=k.l,P=19;P>4&&!R[clim[P-1]];--P);var S,$,C,B,T=l+5<<3,D=clen(o,flt)+clen(i,fdt)+s,_=clen(o,f)+clen(i,m)+s+14+3*P+clen(I,R)+2*I[16]+3*I[17]+7*I[18];if(h>=0&&T<=D&&T<=_)return wfblk(e,c,t.subarray(h,h+l));if(wbits(e,c,1+(_<D)),c+=2,_<D){S=hMap(f,d,0),$=f,C=hMap(m,p,0),B=m;var O=hMap(R,E,0);wbits(e,c,b-257),wbits(e,c+5,v-1),wbits(e,c+10,P-4),c+=14;for(A=0;A<P;++A)wbits(e,c+3*A,R[clim[A]]);c+=3*P;for(var V=[y,M],N=0;N<2;++N){var F=V[N];for(A=0;A<F.length;++A){var L=31&F[A];wbits(e,c,O[L]),c+=R[L],L>15&&(wbits(e,c,F[A]>>5&127),c+=F[A]>>12)}}}else S=flm,$=flt,C=fdm,B=fdt;for(A=0;A<a;++A){var U=n[A];if(U>255){wbits16(e,c,S[(L=U>>18&31)+257]),c+=$[L+257],L>7&&(wbits(e,c,U>>23&31),c+=fleb[L]);var z=31&U;wbits16(e,c,C[z]),c+=B[z],z>3&&(wbits16(e,c,U>>5&8191),c+=fdeb[z])}else wbits16(e,c,S[U]),c+=$[U]}return wbits16(e,c,S[256]),c+$[256]},deo=new i32([65540,131080,131088,131104,262176,1048704,1048832,2114560,2117632]),et=new u8(0),dflt=function(t,e,r,n,o,i){var s=i.z||t.length,a=new u8(n+s+5*(1+Math.ceil(s/7e3))+o),h=a.subarray(n,a.length-o),l=i.l,c=7&(i.r||0);if(e){c&&(h[0]=i.r>>3);for(var u=deo[e-1],f=u>>13,d=8191&u,g=(1<<r)-1,m=i.p||new u16(32768),p=i.h||new u16(g+1),w=Math.ceil(r/3),y=2*w,b=function(e){return(t[e]^t[e+1]<<w^t[e+2]<<y)&g},x=new i32(25e3),M=new u16(288),v=new u16(32),I=0,A=0,k=i.i||0,R=0,E=i.w||0,P=0;k+2<s;++k){var S=b(k),$=32767&k,C=p[S];if(m[$]=C,p[S]=$,E<=k){var B=s-k;if((I>7e3||R>24576)&&(B>423||!l)){c=wblk(t,h,0,x,M,v,A,R,P,k-P,c),R=I=A=0,P=k;for(var T=0;T<286;++T)M[T]=0;for(T=0;T<30;++T)v[T]=0}var D=2,_=0,O=d,V=$-C&32767;if(B>2&&S==b(k-V))for(var N=Math.min(f,B)-1,F=Math.min(32767,k),L=Math.min(258,B);V<=F&&--O&&$!=C;){if(t[k+D]==t[k+D-V]){for(var U=0;U<L&&t[k+U]==t[k+U-V];++U);if(U>D){if(D=U,_=V,U>N)break;var z=Math.min(V,U-2),G=0;for(T=0;T<z;++T){var q=k-V+T&32767,j=q-m[q]&32767;j>G&&(G=j,C=q)}}}V+=($=C)-(C=m[$])&32767}if(_){x[R++]=268435456|revfl[D]<<18|revfd[_];var Y=31&revfl[D],H=31&revfd[_];A+=fleb[Y]+fdeb[H],++M[257+Y],++v[H],E=k+D,++I}else x[R++]=t[k],++M[t[k]]}}for(k=Math.max(k,E);k<s;++k)x[R++]=t[k],++M[t[k]];c=wblk(t,h,l,x,M,v,A,R,P,k-P,c),l||(i.r=7&c|h[c/8|0]<<3,c-=7,i.h=p,i.p=m,i.i=k,i.w=E)}else{for(k=i.w||0;k<s+l;k+=65535){var W=k+65535;W>=s&&(h[c/8|0]=l,W=s),c=wfblk(h,c+1,t.subarray(k,W))}i.i=s}return slc(a,0,n+shft(c)+o)},adler=function(){var t=1,e=0;return{p:function(r){for(var n=t,o=e,i=0|r.length,s=0;s!=i;){for(var a=Math.min(s+2655,i);s<a;++s)o+=n+=r[s];n=(65535&n)+15*(n>>16),o=(65535&o)+15*(o>>16)}t=n,e=o},d:function(){return(255&(t%=65521))<<24|(65280&t)<<8|(255&(e%=65521))<<8|e>>8}}},dopt=function(t,e,r,n,o){if(!o&&(o={l:1},e.dictionary)){var i=e.dictionary.subarray(-32768),s=new u8(i.length+t.length);s.set(i),s.set(t,i.length),t=s,o.w=i.length}return dflt(t,null==e.level?6:e.level,null==e.mem?o.l?Math.ceil(1.5*Math.max(8,Math.min(13,Math.log(t.length)))):20:12+e.mem,r,n,o)},wbytes=function(t,e,r){for(;r;++e)t[e]=r,r>>>=8},gzs=function(t){31==t[0]&&139==t[1]&&8==t[2]||err(6,"invalid gzip data");var e=t[3],r=10;4&e&&(r+=2+(t[10]|t[11]<<8));for(var n=(e>>3&1)+(e>>4&1);n>0;n-=!t[r++]);return r+(2&e)},gzl=function(t){var e=t.length;return(t[e-4]|t[e-3]<<8|t[e-2]<<16|t[e-1]<<24)>>>0},zlh=function(t,e){var r=e.level,n=0==r?0:r<6?1:9==r?3:2;if(t[0]=120,t[1]=n<<6|(e.dictionary&&32),t[1]|=31-(t[0]<<8|t[1])%31,e.dictionary){var o=adler();o.p(e.dictionary),wbytes(t,2,o.d())}},zls=function(t,e){return(8!=(15&t[0])||t[0]>>4>7||(t[0]<<8|t[1])%31)&&err(6,"invalid zlib data"),(t[1]>>5&1)==+!e&&err(6,"invalid zlib data: "+(32&t[1]?"need":"unexpected")+" dictionary"),2+(t[1]>>3&4)},Inflate=function(){function t(t,e){"function"==typeof t&&(e=t,t={}),this.ondata=e;var r=t&&t.dictionary&&t.dictionary.subarray(-32768);this.s={i:0,b:r?r.length:0},this.o=new u8(32768),this.p=new u8(0),r&&this.o.set(r)}return t.prototype.e=function(t){if(this.ondata||err(5),this.d&&err(4),this.p.length){if(t.length){var e=new u8(this.p.length+t.length);e.set(this.p),e.set(t,this.p.length),this.p=e}}else this.p=t},t.prototype.c=function(t){this.s.i=+(this.d=t||!1);var e=this.s.b,r=inflt(this.p,this.s,this.o);this.ondata(slc(r,e,this.s.b),this.d),this.o=slc(r,this.s.b-32768),this.s.b=this.o.length,this.p=slc(this.p,this.s.p/8|0),this.s.p&=7},t.prototype.push=function(t,e){this.e(t),this.c(e)},t}();function inflateSync(t,e){return inflt(t,{i:2},e,e)}function gunzipSync(t,e){var r=gzs(t);return r+8>t.length&&err(6,"invalid gzip data"),inflt(t.subarray(r,-8),{i:2},new u8(gzl(t)),e)}function zlibSync(t,e){e||(e={});var r=adler();r.p(t);var n=dopt(t,e,e.dictionary?6:2,4);return zlh(n,e),wbytes(n,n.length-4,r.d()),n}var Unzlib=function(){function t(t,e){Inflate.call(this,t,e),this.v=t&&t.dictionary?2:1}return t.prototype.push=function(t,e){if(Inflate.prototype.e.call(this,t),this.v){if(this.p.length<6&&!e)return;this.p=this.p.subarray(zls(this.p,this.v-1)),this.v=0}e&&(this.p.length<4&&err(6,"invalid zlib data"),this.p=this.p.subarray(0,-4)),Inflate.prototype.c.call(this,e)},t}();function unzlibSync(t,e){return inflt(t.subarray(zls(t,e),-4),{i:2},e,e)}function decompressSync(t,e){return 31==t[0]&&139==t[1]&&8==t[2]?gunzipSync(t,e):8!=(15&t[0])||t[0]>>4>7||(t[0]<<8|t[1])%31?inflateSync(t,e):unzlibSync(t,e)}var td="undefined"!=typeof TextDecoder&&new TextDecoder,tds=0;try{td.decode(et,{stream:!0}),tds=1}catch(e){}const crcTable=[];for(let t=0;t<256;t++){let e=t;for(let t=0;t<8;t++)1&e?e=3988292384^e>>>1:e>>>=1;crcTable[t]=e}const initialCrc=4294967295;function updateCrc(t,e,r){let n=t;for(let t=0;t<r;t++)n=crcTable[255&(n^e[t])]^n>>>8;return n}function crc(t,e){return(updateCrc(initialCrc,t,e)^initialCrc)>>>0}function checkCrc(t,e,r){const n=t.readUint32(),o=crc(new Uint8Array(t.buffer,t.byteOffset+t.offset-e-4,e),e);if(o!==n)throw new Error(`CRC mismatch for chunk ${r}. Expected ${n}, found ${o}`)}function writeCrc(t,e){t.writeUint32(crc(new Uint8Array(t.buffer,t.byteOffset+t.offset-e,e),e))}function unfilterNone(t,e,r){for(let n=0;n<r;n++)e[n]=t[n]}function unfilterSub(t,e,r,n){let o=0;for(;o<n;o++)e[o]=t[o];for(;o<r;o++)e[o]=t[o]+e[o-n]&255}function unfilterUp(t,e,r,n){let o=0;if(0===r.length)for(;o<n;o++)e[o]=t[o];else for(;o<n;o++)e[o]=t[o]+r[o]&255}function unfilterAverage(t,e,r,n,o){let i=0;if(0===r.length){for(;i<o;i++)e[i]=t[i];for(;i<n;i++)e[i]=t[i]+(e[i-o]>>1)&255}else{for(;i<o;i++)e[i]=t[i]+(r[i]>>1)&255;for(;i<n;i++)e[i]=t[i]+(e[i-o]+r[i]>>1)&255}}function unfilterPaeth(t,e,r,n,o){let i=0;if(0===r.length){for(;i<o;i++)e[i]=t[i];for(;i<n;i++)e[i]=t[i]+e[i-o]&255}else{for(;i<o;i++)e[i]=t[i]+r[i]&255;for(;i<n;i++)e[i]=t[i]+paethPredictor(e[i-o],r[i],r[i-o])&255}}function paethPredictor(t,e,r){const n=t+e-r,o=Math.abs(n-t),i=Math.abs(n-e),s=Math.abs(n-r);return o<=i&&o<=s?t:i<=s?e:r}function applyUnfilter(t,e,r,n,o,i){switch(t){case 0:unfilterNone(e,r,o);break;case 1:unfilterSub(e,r,o,i);break;case 2:unfilterUp(e,r,n,o);break;case 3:unfilterAverage(e,r,n,o,i);break;case 4:unfilterPaeth(e,r,n,o,i);break;default:throw new Error(`Unsupported filter: ${t}`)}}const uint16$1=new Uint16Array([255]),uint8$1=new Uint8Array(uint16$1.buffer),osIsLittleEndian$1=255===uint8$1[0];function decodeInterlaceAdam7(t){const{data:e,width:r,height:n,channels:o,depth:i}=t,s=[{x:0,y:0,xStep:8,yStep:8},{x:4,y:0,xStep:8,yStep:8},{x:0,y:4,xStep:4,yStep:8},{x:2,y:0,xStep:4,yStep:4},{x:0,y:2,xStep:2,yStep:4},{x:1,y:0,xStep:2,yStep:2},{x:0,y:1,xStep:1,yStep:2}],a=Math.ceil(i/8)*o,h=new Uint8Array(n*r*a);let l=0;for(let t=0;t<7;t++){const o=s[t],i=Math.ceil((r-o.x)/o.xStep),c=Math.ceil((n-o.y)/o.yStep);if(i<=0||c<=0)continue;const u=i*a,f=new Uint8Array(u);for(let t=0;t<c;t++){const s=e[l++],c=e.subarray(l,l+u);l+=u;const d=new Uint8Array(u);applyUnfilter(s,c,d,f,u,a),f.set(d);for(let e=0;e<i;e++){const i=o.x+e*o.xStep,s=o.y+t*o.yStep;if(!(i>=r||s>=n))for(let t=0;t<a;t++)h[(s*r+i)*a+t]=d[e*a+t]}}}if(16===i){const t=new Uint16Array(h.buffer);if(osIsLittleEndian$1)for(let e=0;e<t.length;e++)t[e]=swap16$1(t[e]);return t}return h}function swap16$1(t){return(255&t)<<8|t>>8&255}const uint16=new Uint16Array([255]),uint8=new Uint8Array(uint16.buffer),osIsLittleEndian=255===uint8[0],empty=new Uint8Array(0);function decodeInterlaceNull(t){const{data:e,width:r,height:n,channels:o,depth:i}=t,s=Math.ceil(i/8)*o,a=Math.ceil(i/8*o*r),h=new Uint8Array(n*a);let l,c,u=empty,f=0;for(let t=0;t<n;t++){switch(l=e.subarray(f+1,f+1+a),c=h.subarray(t*a,(t+1)*a),e[f]){case 0:unfilterNone(l,c,a);break;case 1:unfilterSub(l,c,a,s);break;case 2:unfilterUp(l,c,u,a);break;case 3:unfilterAverage(l,c,u,a,s);break;case 4:unfilterPaeth(l,c,u,a,s);break;default:throw new Error(`Unsupported filter: ${e[f]}`)}u=c,f+=a+1}if(16===i){const t=new Uint16Array(h.buffer);if(osIsLittleEndian)for(let e=0;e<t.length;e++)t[e]=swap16(t[e]);return t}return h}function swap16(t){return(255&t)<<8|t>>8&255}const pngSignature=Uint8Array.of(137,80,78,71,13,10,26,10);function writeSignature(t){t.writeBytes(pngSignature)}function checkSignature(t){if(!hasPngSignature(t.readBytes(pngSignature.length)))throw new Error("wrong PNG signature")}function hasPngSignature(t){if(t.length<pngSignature.length)return!1;for(let e=0;e<pngSignature.length;e++)if(t[e]!==pngSignature[e])return!1;return!0}const textChunkName="tEXt",NULL=0,latin1Decoder=new TextDecoder("latin1");function validateKeyword(t){if(validateLatin1(t),0===t.length||t.length>79)throw new Error("keyword length must be between 1 and 79")}const latin1Regex=/^[\u0000-\u00FF]*$/;function validateLatin1(t){if(!latin1Regex.test(t))throw new Error("invalid latin1 text")}function decodetEXt(t,e,r){const n=readKeyword(e);t[n]=readLatin1(e,r-n.length-1)}function encodetEXt(t,e,r){validateKeyword(e),validateLatin1(r);const n=e.length+1+r.length;t.writeUint32(n),t.writeChars(textChunkName),t.writeChars(e),t.writeByte(NULL),t.writeChars(r),writeCrc(t,n+4)}function readKeyword(t){for(t.mark();t.readByte()!==NULL;);const e=t.offset;t.reset();const r=latin1Decoder.decode(t.readBytes(e-t.offset-1));return t.skip(1),validateKeyword(r),r}function readLatin1(t,e){return latin1Decoder.decode(t.readBytes(e))}const ColorType={UNKNOWN:-1,GREYSCALE:0,TRUECOLOUR:2,INDEXED_COLOUR:3,GREYSCALE_ALPHA:4,TRUECOLOUR_ALPHA:6},CompressionMethod={UNKNOWN:-1,DEFLATE:0},FilterMethod={UNKNOWN:-1,ADAPTIVE:0},InterlaceMethod={UNKNOWN:-1,NO_INTERLACE:0,ADAM7:1},DisposeOpType={NONE:0,BACKGROUND:1,PREVIOUS:2},BlendOpType={SOURCE:0,OVER:1};class PngDecoder extends IOBuffer{_checkCrc;_inflator;_png;_apng;_end;_hasPalette;_palette;_hasTransparency;_transparency;_compressionMethod;_filterMethod;_interlaceMethod;_colorType;_isAnimated;_numberOfFrames;_numberOfPlays;_frames;_writingDataChunks;_chunks;_inflatorResult;constructor(t,e={}){super(t);const{checkCrc:r=!1}=e;this._checkCrc=r,this._inflator=new Unzlib((t,e)=>{if(this._chunks.push(t),e){const t=this._chunks.reduce((t,e)=>t+e.length,0);this._inflatorResult=new Uint8Array(t);let e=0;for(const t of this._chunks)this._inflatorResult.set(t,e),e+=t.length;this._chunks=[]}}),this._chunks=[],this._png={width:-1,height:-1,channels:-1,data:new Uint8Array(0),depth:1,text:{}},this._apng={width:-1,height:-1,channels:-1,depth:1,numberOfFrames:1,numberOfPlays:0,text:{},frames:[]},this._end=!1,this._hasPalette=!1,this._palette=[],this._hasTransparency=!1,this._transparency=new Uint16Array(0),this._compressionMethod=CompressionMethod.UNKNOWN,this._filterMethod=FilterMethod.UNKNOWN,this._interlaceMethod=InterlaceMethod.UNKNOWN,this._colorType=ColorType.UNKNOWN,this._isAnimated=!1,this._numberOfFrames=1,this._numberOfPlays=0,this._frames=[],this._writingDataChunks=!1,this._inflatorResult=new Uint8Array(0),this.setBigEndian()}decode(){for(checkSignature(this);!this._end;){const t=this.readUint32(),e=this.readChars(4);this.decodeChunk(t,e)}return this._inflator.push(new Uint8Array(0),!0),this.decodeImage(),this._png}decodeApng(){for(checkSignature(this);!this._end;){const t=this.readUint32(),e=this.readChars(4);this.decodeApngChunk(t,e)}return this.decodeApngImage(),this._apng}decodeChunk(t,e){const r=this.offset;switch(e){case"IHDR":this.decodeIHDR();break;case"PLTE":this.decodePLTE(t);break;case"IDAT":this.decodeIDAT(t);break;case"IEND":this._end=!0;break;case"tRNS":this.decodetRNS(t);break;case"iCCP":this.decodeiCCP(t);break;case textChunkName:decodetEXt(this._png.text,this,t);break;case"pHYs":this.decodepHYs();break;default:this.skip(t)}if(this.offset-r!==t)throw new Error(`Length mismatch while decoding chunk ${e}`);this._checkCrc?checkCrc(this,t+4,e):this.skip(4)}decodeApngChunk(t,e){const r=this.offset;switch("fdAT"!==e&&"IDAT"!==e&&this._writingDataChunks&&this.pushDataToFrame(),e){case"acTL":this.decodeACTL();break;case"fcTL":this.decodeFCTL();break;case"fdAT":this.decodeFDAT(t);break;default:this.decodeChunk(t,e),this.offset=r+t}if(this.offset-r!==t)throw new Error(`Length mismatch while decoding chunk ${e}`);this._checkCrc?checkCrc(this,t+4,e):this.skip(4)}decodeIHDR(){const t=this._png;t.width=this.readUint32(),t.height=this.readUint32(),t.depth=checkBitDepth(this.readUint8());const e=this.readUint8();let r;switch(this._colorType=e,e){case ColorType.GREYSCALE:r=1;break;case ColorType.TRUECOLOUR:r=3;break;case ColorType.INDEXED_COLOUR:r=1;break;case ColorType.GREYSCALE_ALPHA:r=2;break;case ColorType.TRUECOLOUR_ALPHA:r=4;break;case ColorType.UNKNOWN:default:throw new Error(`Unknown color type: ${e}`)}if(this._png.channels=r,this._compressionMethod=this.readUint8(),this._compressionMethod!==CompressionMethod.DEFLATE)throw new Error(`Unsupported compression method: ${this._compressionMethod}`);this._filterMethod=this.readUint8(),this._interlaceMethod=this.readUint8()}decodeACTL(){this._numberOfFrames=this.readUint32(),this._numberOfPlays=this.readUint32(),this._isAnimated=!0}decodeFCTL(){const t={sequenceNumber:this.readUint32(),width:this.readUint32(),height:this.readUint32(),xOffset:this.readUint32(),yOffset:this.readUint32(),delayNumber:this.readUint16(),delayDenominator:this.readUint16(),disposeOp:this.readUint8(),blendOp:this.readUint8(),data:new Uint8Array(0)};this._frames.push(t)}decodePLTE(t){if(t%3!=0)throw new RangeError(`PLTE field length must be a multiple of 3. Got ${t}`);const e=t/3;this._hasPalette=!0;const r=[];this._palette=r;for(let t=0;t<e;t++)r.push([this.readUint8(),this.readUint8(),this.readUint8()])}decodeIDAT(t){this._writingDataChunks=!0;const e=t,r=this.offset+this.byteOffset;try{this._inflator.push(new Uint8Array(this.buffer,r,e),!1)}catch(t){throw new Error("Error while decompressing the data:",{cause:t})}this.skip(t)}decodeFDAT(t){this._writingDataChunks=!0;let e=t,r=this.offset+this.byteOffset;r+=4,e-=4;try{this._inflator.push(new Uint8Array(this.buffer,r,e),!1)}catch(t){throw new Error("Error while decompressing the data:",{cause:t})}this.skip(t)}decodetRNS(t){switch(this._colorType){case ColorType.GREYSCALE:case ColorType.TRUECOLOUR:if(t%2!=0)throw new RangeError(`tRNS chunk length must be a multiple of 2. Got ${t}`);if(t/2>this._png.width*this._png.height)throw new Error(`tRNS chunk contains more alpha values than there are pixels (${t/2} vs ${this._png.width*this._png.height})`);this._hasTransparency=!0,this._transparency=new Uint16Array(t/2);for(let e=0;e<t/2;e++)this._transparency[e]=this.readUint16();break;case ColorType.INDEXED_COLOUR:{if(t>this._palette.length)throw new Error(`tRNS chunk contains more alpha values than there are palette colors (${t} vs ${this._palette.length})`);let e=0;for(;e<t;e++){const t=this.readByte();this._palette[e].push(t)}for(;e<this._palette.length;e++)this._palette[e].push(255);break}case ColorType.UNKNOWN:case ColorType.GREYSCALE_ALPHA:case ColorType.TRUECOLOUR_ALPHA:default:throw new Error(`tRNS chunk is not supported for color type ${this._colorType}`)}}decodeiCCP(t){const e=readKeyword(this),r=this.readUint8();if(r!==CompressionMethod.DEFLATE)throw new Error(`Unsupported iCCP compression method: ${r}`);const n=this.readBytes(t-e.length-2);this._png.iccEmbeddedProfile={name:e,profile:unzlibSync(n)}}decodepHYs(){const t=this.readUint32(),e=this.readUint32(),r=this.readByte();this._png.resolution={x:t,y:e,unit:r}}decodeApngImage(){this._apng.width=this._png.width,this._apng.height=this._png.height,this._apng.channels=this._png.channels,this._apng.depth=this._png.depth,this._apng.numberOfFrames=this._numberOfFrames,this._apng.numberOfPlays=this._numberOfPlays,this._apng.text=this._png.text,this._apng.resolution=this._png.resolution;for(let t=0;t<this._numberOfFrames;t++){const e={sequenceNumber:this._frames[t].sequenceNumber,delayNumber:this._frames[t].delayNumber,delayDenominator:this._frames[t].delayDenominator,data:8===this._apng.depth?new Uint8Array(this._apng.width*this._apng.height*this._apng.channels):new Uint16Array(this._apng.width*this._apng.height*this._apng.channels)},r=this._frames.at(t);if(r){if(r.data=decodeInterlaceNull({data:r.data,width:r.width,height:r.height,channels:this._apng.channels,depth:this._apng.depth}),this._hasPalette&&(this._apng.palette=this._palette),this._hasTransparency&&(this._apng.transparency=this._transparency),0===t||0===r.xOffset&&0===r.yOffset&&r.width===this._png.width&&r.height===this._png.height)e.data=r.data;else{const n=this._apng.frames.at(t-1);this.disposeFrame(r,n,e),this.addFrameDataToCanvas(e,r)}this._apng.frames.push(e)}}return this._apng}disposeFrame(t,e,r){switch(t.disposeOp){case DisposeOpType.NONE:break;case DisposeOpType.BACKGROUND:for(let e=0;e<this._png.height;e++)for(let n=0;n<this._png.width;n++){const o=(e*t.width+n)*this._png.channels;for(let t=0;t<this._png.channels;t++)r.data[o+t]=0}break;case DisposeOpType.PREVIOUS:r.data.set(e.data);break;default:throw new Error("Unknown disposeOp")}}addFrameDataToCanvas(t,e){const r=1<<this._png.depth,n=(t,r)=>({index:((t+e.yOffset)*this._png.width+e.xOffset+r)*this._png.channels,frameIndex:(t*e.width+r)*this._png.channels});switch(e.blendOp){case BlendOpType.SOURCE:for(let r=0;r<e.height;r++)for(let o=0;o<e.width;o++){const{index:i,frameIndex:s}=n(r,o);for(let r=0;r<this._png.channels;r++)t.data[i+r]=e.data[s+r]}break;case BlendOpType.OVER:for(let o=0;o<e.height;o++)for(let i=0;i<e.width;i++){const{index:s,frameIndex:a}=n(o,i);for(let n=0;n<this._png.channels;n++){const o=e.data[a+this._png.channels-1]/r,i=n%(this._png.channels-1)==0?1:e.data[a+n],h=Math.floor(o*i+(1-o)*t.data[s+n]);t.data[s+n]+=h}}break;default:throw new Error("Unknown blendOp")}}decodeImage(){const t=this._inflatorResult;if(this._filterMethod!==FilterMethod.ADAPTIVE)throw new Error(`Filter method ${this._filterMethod} not supported`);if(this._interlaceMethod===InterlaceMethod.NO_INTERLACE)this._png.data=decodeInterlaceNull({data:t,width:this._png.width,height:this._png.height,channels:this._png.channels,depth:this._png.depth});else{if(this._interlaceMethod!==InterlaceMethod.ADAM7)throw new Error(`Interlace method ${this._interlaceMethod} not supported`);this._png.data=decodeInterlaceAdam7({data:t,width:this._png.width,height:this._png.height,channels:this._png.channels,depth:this._png.depth})}this._hasPalette&&(this._png.palette=this._palette),this._hasTransparency&&(this._png.transparency=this._transparency)}pushDataToFrame(){this._inflator.push(new Uint8Array(0),!0);const t=this._inflatorResult,e=this._frames.at(-1);e?e.data=t:this._frames.push({sequenceNumber:0,width:this._png.width,height:this._png.height,xOffset:0,yOffset:0,delayNumber:0,delayDenominator:0,disposeOp:DisposeOpType.NONE,blendOp:BlendOpType.SOURCE,data:t}),this._inflator=new Unzlib((t,e)=>{if(this._chunks.push(t),e){const t=this._chunks.reduce((t,e)=>t+e.length,0);this._inflatorResult=new Uint8Array(t);let e=0;for(const t of this._chunks)this._inflatorResult.set(t,e),e+=t.length;this._chunks=[]}}),this._chunks=[],this._writingDataChunks=!1}}function checkBitDepth(t){if(1!==t&&2!==t&&4!==t&&8!==t&&16!==t)throw new Error(`invalid bit depth: ${t}`);return t}const defaultZlibOptions={level:3};class PngEncoder extends IOBuffer{_png;_zlibOptions;_colorType;_interlaceMethod;constructor(t,e={}){super(),this._colorType=ColorType.UNKNOWN,this._zlibOptions={...defaultZlibOptions,...e.zlib},this._png=this._checkData(t),this._interlaceMethod=("Adam7"===e.interlace?InterlaceMethod.ADAM7:InterlaceMethod.NO_INTERLACE)??InterlaceMethod.NO_INTERLACE,this.setBigEndian()}encode(){if(writeSignature(this),this.encodeIHDR(),this._png.palette&&(this.encodePLTE(),4===this._png.palette[0].length&&this.encodeTRNS()),this.encodeData(),this._png.text)for(const[t,e]of Object.entries(this._png.text))encodetEXt(this,t,e);return this.encodeIEND(),this.toArray()}encodeIHDR(){this.writeUint32(13),this.writeChars("IHDR"),this.writeUint32(this._png.width),this.writeUint32(this._png.height),this.writeByte(this._png.depth),this.writeByte(this._colorType),this.writeByte(CompressionMethod.DEFLATE),this.writeByte(FilterMethod.ADAPTIVE),this.writeByte(this._interlaceMethod),writeCrc(this,17)}encodeIEND(){this.writeUint32(0),this.writeChars("IEND"),writeCrc(this,4)}encodePLTE(){const t=3*this._png.palette?.length;this.writeUint32(t),this.writeChars("PLTE");for(const t of this._png.palette)this.writeByte(t[0]),this.writeByte(t[1]),this.writeByte(t[2]);writeCrc(this,4+t)}encodeTRNS(){const t=this._png.palette.filter(t=>255!==t.at(-1));this.writeUint32(t.length),this.writeChars("tRNS");for(const e of t)this.writeByte(e.at(-1));writeCrc(this,4+t.length)}encodeIDAT(t){this.writeUint32(t.length),this.writeChars("IDAT"),this.writeBytes(t),writeCrc(this,t.length+4)}encodeData(){const{width:t,height:e,channels:r,depth:n,data:o}=this._png,i=n<=8?Math.ceil(t*n/8)*r:Math.ceil(t*n/8*r/2),s=(new IOBuffer).setBigEndian();let a=0;if(this._interlaceMethod===InterlaceMethod.NO_INTERLACE)for(let t=0;t<e;t++)s.writeByte(0),a=16===n?writeDataUint16(o,s,i,a):writeDataBytes(o,s,i,a);else this._interlaceMethod===InterlaceMethod.ADAM7&&(a=writeDataInterlaced(this._png,o,s,a));const h=zlibSync(s.toArray(),this._zlibOptions);this.encodeIDAT(h)}_checkData(t){const{colorType:e,channels:r,depth:n}=getColorType(t,t.palette),o={width:checkInteger(t.width,"width"),height:checkInteger(t.height,"height"),channels:r,data:t.data,depth:n,text:t.text,palette:t.palette};this._colorType=e;const i=n<8?Math.ceil(o.width*n/8)*o.height*r:o.width*o.height*r;if(o.data.length!==i)throw new RangeError(`wrong data size. Found ${o.data.length}, expected ${i}`);return o}}function checkInteger(t,e){if(Number.isInteger(t)&&t>0)return t;throw new TypeError(`${e} must be a positive integer`)}function getColorType(t,e){const{channels:r=4,depth:n=8}=t;if(4!==r&&3!==r&&2!==r&&1!==r)throw new RangeError(`unsupported number of channels: ${r}`);const o={channels:r,depth:n,colorType:ColorType.UNKNOWN};switch(r){case 4:o.colorType=ColorType.TRUECOLOUR_ALPHA;break;case 3:o.colorType=ColorType.TRUECOLOUR;break;case 1:o.colorType=e?ColorType.INDEXED_COLOUR:ColorType.GREYSCALE;break;case 2:o.colorType=ColorType.GREYSCALE_ALPHA;break;default:throw new Error("unsupported number of channels")}return o}function writeDataBytes(t,e,r,n){for(let o=0;o<r;o++)e.writeByte(t[n++]);return n}function writeDataInterlaced(t,e,r,n){const o=[{x:0,y:0,xStep:8,yStep:8},{x:4,y:0,xStep:8,yStep:8},{x:0,y:4,xStep:4,yStep:8},{x:2,y:0,xStep:4,yStep:4},{x:0,y:2,xStep:2,yStep:4},{x:1,y:0,xStep:2,yStep:2},{x:0,y:1,xStep:1,yStep:2}],{width:i,height:s,channels:a,depth:h}=t;let l;l=16===h?a*h/8/2:a*h/8;for(let t=0;t<7;t++){const n=o[t],a=Math.floor((i-n.x+n.xStep-1)/n.xStep),c=Math.floor((s-n.y+n.yStep-1)/n.yStep);if(a<=0||c<=0)continue;const u=a*l;for(let t=0;t<c;t++){const o=n.y+t*n.yStep,c=h<=8?new Uint8Array(u):new Uint16Array(u);let f=0;for(let t=0;t<a;t++){const r=n.x+t*n.xStep;if(r<i&&o<s){const t=(o*i+r)*l;for(let r=0;r<l;r++)c[f++]=e[t+r]}}if(r.writeByte(0),8===h)r.writeBytes(c);else if(16===h)for(const t of c)r.writeByte(t>>8&255),r.writeByte(255&t)}}return n}function writeDataUint16(t,e,r,n){for(let o=0;o<r;o++)e.writeUint16(t[n++]);return n}function decodePng$1(t,e){return new PngDecoder(t,e).decode()}function encodePng$1(t,e){return new PngEncoder(t,e).encode()}function decodeApng(t,e){return new PngDecoder(t,e).decodeApng()}function encodePng(t,e){("RGB"!==t.colorModel&&"RGBA"!==t.colorModel&&"GREY"!==t.colorModel&&"GREYA"!==t.colorModel||t instanceof Mask)&&(t=t.convertColor("GREY"));const{bitDepth:r,...n}=t.getRawImage();return encodePng$1({depth:r,...n},e)}const ImageFormat={PNG:"png",JPG:"jpg",JPEG:"jpeg",BMP:"bmp"},defaultPng={format:"png"};function encode(t,e=defaultPng){return M$2(e).with({format:"png"},e=>encodePng(t,e.encoderOptions)).with({format:z.union("jpg","jpeg")},e=>encodeJpeg(t,e.encoderOptions)).with({format:"bmp"},()=>encodeBmp(t)).exhaustive()}async function write(t,e,r){const n=getNodeApiOrThrow("write");"string"!=typeof t&&(t=n.url.fileURLToPath(t)),e instanceof Mask&&(e=e.convertColor("GREY"));const o=getDataToWrite(t,e,r,n);if(r?.recursive){const e=n.path.dirname(t);await n.fs.promises.mkdir(e,{recursive:!0})}await n.fs.promises.writeFile(t,o)}function writeSync(t,e,r){const n=getNodeApiOrThrow("writeSync");"string"!=typeof t&&(t=n.url.fileURLToPath(t));const o=getDataToWrite(t,e,r,n);if(r?.recursive){const e=n.path.dirname(t);n.fs.mkdirSync(e,{recursive:!0})}n.fs.writeFileSync(t,o)}function getDataToWrite(t,e,r,n){if(r&&"format"in r)return encode(e,r);{const o=n.path.extname(t).slice(1).toLowerCase();if("png"===o||"jpg"===o||"jpeg"===o||"bmp"===o)return encode(e,{...r,format:o});throw new RangeError("image format could not be determined from file extension. Use a supported extension or specify the format option")}}function writeCanvas(t,e,r={}){"RGBA"!==t.colorModel&&(t=t.convertColor("RGBA")),8!==t.bitDepth&&t instanceof Image&&(t=t.convertBitDepth(8));const{resizeCanvas:n=!0,dx:o=0,dy:i=0,dirtyX:s=0,dirtyY:a=0,dirtyWidth:h=t.width,dirtyHeight:l=t.height}=r;n&&(e.width=t.width,e.height=t.height);const c=e.getContext("2d");assert(c);const u=t.getRawImage().data;c.putImageData(new ImageData(new Uint8ClampedArray(u.buffer,u.byteOffset,u.byteLength),t.width,t.height),o,i,s,a,h,l)}Uint8Array.from([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]);const base64codes=Uint8Array.from([65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,48,49,50,51,52,53,54,55,56,57,43,47]),base64codes1=new Uint32Array(4096);for(let t=0;t<64;t++)for(let e=0;e<64;e++){const r=t<<2|(48&e)>>4|(15&e)<<8;base64codes1[r]=base64codes[t]|base64codes[e]<<8}const base64codes2=new Uint32Array(4096);for(let t=0;t<64;t++)for(let e=0;e<64;e++){const r=t<<6|e;base64codes2[r]=base64codes[t]<<16|base64codes[e]<<24}function encodeFast(t){const e=new Uint32Array(Math.ceil(t.length/3));let r,n;for(r=2,n=0;r<t.length;r+=3,n++)e[n]=base64codes1[t[r-2]|(240&t[r-1])<<4]|base64codes2[t[r]|(15&t[r-1])<<8];r===t.length+1&&(e[n]=base64codes[t[r-2]>>2]|base64codes[(3&t[r-2])<<4]<<8|15677<<16),r===t.length&&(e[n]=base64codes[t[r-2]>>2]|base64codes[(3&t[r-2])<<4|t[r-1]>>4]<<8|base64codes[(15&t[r-1])<<2]<<16|61<<24);return new Uint8Array(e.buffer)}function encodeDataURL(t,e=defaultPng){const r=encodeFast(encode(t,e)),n=(new TextDecoder).decode(r);return`data:image/${e.format};base64,${n}`}function getMatrixFromPoints(t){const e=new Matrix(3,t.length);for(let r=0;r<t.length;r++)e.set(0,r,t[r].column),e.set(1,r,t[r].row),e.set(2,r,1);return e}function affineFitFunction(t,e){const r=getAffineTransform$1(getMatrixFromPoints(t),getMatrixFromPoints(e));return[r.rotation,r.translation.x,r.translation.y,r.scale]}function createAffineTransformModel(t){if(4!==t.length)throw new Error("Transform had wrong number of parameters");return e=>{const r=t[0]*Math.PI/180,n=t[1],o=t[2],i=t[3];return{column:i*(Math.cos(r)*e.column-Math.sin(r)*e.row)+n,row:i*(Math.sin(r)*e.column+Math.cos(r)*e.row)+o}}}function getEuclideanDistance(t,e){return Math.hypot(t.row-e.row,t.column-e.column)}function getSourceWithoutMargins(t,e,r){const n=Math.min(e.width,t.width-r.column),o=Math.min(e.height,t.height-r.row);return t.crop({origin:{row:r.row,column:r.column},width:n,height:o})}function getAffineTransform(t,e,r={}){const{centroidPatchDiameter:n=31,bestKeypointRadius:o=5,enhanceContrast:i=!0,crosscheck:s=!0,destinationOrigin:a={column:0,row:0},maxRansacNbIterations:h,debug:l=!1,debugImagePath:c}=r;if(t.colorModel!==ImageColorModel.GREY&&(t=t.grey()),e.colorModel!==ImageColorModel.GREY&&(e=e.grey()),i){const r=getMinMax(getSourceWithoutMargins(t,e,a));t.level({inputMin:r.min[0],inputMax:r.max[0],out:t});const n=getMinMax(e);e.level({inputMin:n.min[0],inputMax:n.max[0],out:e})}const u=getBrief(t,{centroidPatchDiameter:n,bestKptRadius:o}),f=getBrief(e,{centroidPatchDiameter:n,bestKptRadius:o}),d=u.keypoints.length,g=f.keypoints.length;let m=[];if(s?m=getCrosscheckMatches(u.descriptors,f.descriptors):(m=bruteForceOneMatch(u.descriptors,f.descriptors),m=filterEuclideanDistance(m,u.keypoints,f.keypoints,{origin:a})),m.length<2)throw new Error("Insufficient number of matches found to compute affine transform (less than 2).");let p=[],w=[];for(const t of m)p.push(u.keypoints[t.sourceIndex].origin),w.push(f.keypoints[t.destinationIndex].origin);let y=p.length,b=0,x=[0,1];if(p.length>2){const t=ransac(p,w,{distanceFunction:getEuclideanDistance,modelFunction:createAffineTransformModel,fitFunction:affineFitFunction,maxNbIterations:h});b=t.nbIterations,x=t.inliers,y=x.length;const e=[],r=[];for(const t of x)e.push(p[t]),r.push(w[t]);p=e,w=r}if(l){if(!c)throw new RangeError("Debug image file path is not specified.");const r=new Montage(t,e,{disposition:MontageDisposition.VERTICAL});r.drawMatches(m,u.keypoints,f.keypoints,{showDistance:!0});const n=[];for(const t of x)n.push(m[t]);r.drawMatches(n,u.keypoints,f.keypoints,{strokeColor:[0,0,255]});const o={fill:!0,color:[0,255,0],showScore:!0,markerSize:3};r.drawKeypoints(u.keypoints,o),r.drawKeypoints(f.keypoints,{origin:r.destinationOrigin,...o}),writeSync(c,r.image)}const M=getAffineTransform$1(getMatrixFromPoints(p),getMatrixFromPoints(w));return{transform:{rotation:M.rotation,scale:M.scale,translation:{column:Math.round(M.translation.x),row:Math.round(M.translation.y)}},stats:{nbMatches:m.length,nbInliers:y,nbRansacIterations:b,nbSourceKeypoints:d,nbDestinationKeypoints:g}}}function alignMinDifference(t,e,r={}){checkProcessable$1(t,{bitDepth:[8,16]});const n=e.width-t.width,o=e.height-t.height,{startStep:i=Math.max(Math.round(Math.min(t.width,t.height,Math.max(n,o))/4),1),mask:s}=r;if(n<0||o<0)throw new Error("Source image must fit entirely in destination image");let a=Number.POSITIVE_INFINITY,h=0,l=0,c=i,u=0,f=0,d=n,g=o;if(s&&s.size!==t.size)throw new Error("Mask size must be equal to source size");const m=s?s.getNbNonZeroPixels():t.size;for(;c>=1;){c=Math.round(c);for(let r=u;r<=d;r+=c)for(let n=f;n<=g;n+=c){let o=0;t:for(let i=0;i<t.width;i++)for(let h=0;h<t.height;h++)if(!s||s.getBit(i,h))for(let s=0;s<t.channels;s++){const l=t.getValue(i,h,s)-e.getValue(i+r,h+n,s);if(l<0?o-=l:o+=l,o>a)break t}o<a&&(a=o,h=r,l=n)}c/=2,u=Math.round(Math.max(0,h-c)),f=Math.round(Math.max(0,l-c)),d=Math.round(Math.min(n,h+c)),g=Math.round(Math.min(o,l+c))}return{row:l,column:h,similarity:1-a/(m*t.maxValue)}}function removeClosePoints(t,e,r){const n=r?.distance||0,o=r?.kind||"maximum";if(void 0===r?.channel&&e.channels>1)throw new Error("image channel must be specified or image must have only one channel");const i=r?.channel||0,s="maximum"===o,a=t.slice();if(a.sort(getSort(e,i,s)),n>0)for(let t=0;t<a.length;t++)for(let e=t+1;e<a.length;e++)Math.hypot(a[t].column-a[e].column,a[t].row-a[e].row)<n&&(a.splice(e,1),e--);return a}function getSort(t,e,r){return r?function(r,n){return t.getValue(n.column,n.row,e)-t.getValue(r.column,r.row,e)}:function(r,n){return t.getValue(r.column,r.row,e)-t.getValue(n.column,n.row,e)}}var imageType$2={exports:{}},fileType$1={exports:{}};(function(module){const toBytes=t=>[...t].map(t=>t.charCodeAt(0)),xpiZipFilename=toBytes("META-INF/mozilla.rsa"),oxmlContentTypes=toBytes("[Content_Types].xml"),oxmlRels=toBytes("_rels/.rels");function readUInt64LE(t,e=0){let r=t[e],n=1,o=0;for(;++o<8;)n*=256,r+=t[e+o]*n;return r}const fileType=t=>{if(!(t instanceof Uint8Array||t instanceof ArrayBuffer||Buffer.isBuffer(t)))throw new TypeError(`Expected the \`input\` argument to be of type \`Uint8Array\` or \`Buffer\` or \`ArrayBuffer\`, got \`${typeof t}\``);const e=t instanceof Uint8Array?t:new Uint8Array(t);if(!(e&&e.length>1))return null;const r=(t,r)=>{r=Object.assign({offset:0},r);for(let n=0;n<t.length;n++)if(r.mask){if(t[n]!==(r.mask[n]&e[n+r.offset]))return!1}else if(t[n]!==e[n+r.offset])return!1;return!0},n=(t,e)=>r(toBytes(t),e);if(r([255,216,255]))return{ext:"jpg",mime:"image/jpeg"};if(r([137,80,78,71,13,10,26,10]))return{ext:"png",mime:"image/png"};if(r([71,73,70]))return{ext:"gif",mime:"image/gif"};if(r([87,69,66,80],{offset:8}))return{ext:"webp",mime:"image/webp"};if(r([70,76,73,70]))return{ext:"flif",mime:"image/flif"};if((r([73,73,42,0])||r([77,77,0,42]))&&r([67,82],{offset:8}))return{ext:"cr2",mime:"image/x-canon-cr2"};if(r([73,73,42,0])||r([77,77,0,42]))return{ext:"tif",mime:"image/tiff"};if(r([66,77]))return{ext:"bmp",mime:"image/bmp"};if(r([73,73,188]))return{ext:"jxr",mime:"image/vnd.ms-photo"};if(r([56,66,80,83]))return{ext:"psd",mime:"image/vnd.adobe.photoshop"};if(r([80,75,3,4])){if(r([109,105,109,101,116,121,112,101,97,112,112,108,105,99,97,116,105,111,110,47,101,112,117,98,43,122,105,112],{offset:30}))return{ext:"epub",mime:"application/epub+zip"};if(r(xpiZipFilename,{offset:30}))return{ext:"xpi",mime:"application/x-xpinstall"};if(n("mimetypeapplication/vnd.oasis.opendocument.text",{offset:30}))return{ext:"odt",mime:"application/vnd.oasis.opendocument.text"};if(n("mimetypeapplication/vnd.oasis.opendocument.spreadsheet",{offset:30}))return{ext:"ods",mime:"application/vnd.oasis.opendocument.spreadsheet"};if(n("mimetypeapplication/vnd.oasis.opendocument.presentation",{offset:30}))return{ext:"odp",mime:"application/vnd.oasis.opendocument.presentation"};const t=(t,e=0)=>t.findIndex((t,r,n)=>r>=e&&80===n[r]&&75===n[r+1]&&3===n[r+2]&&4===n[r+3]);let o=0,i=!1,s=null;do{const a=o+30;if(i||(i=r(oxmlContentTypes,{offset:a})||r(oxmlRels,{offset:a})),s||(n("word/",{offset:a})?s={ext:"docx",mime:"application/vnd.openxmlformats-officedocument.wordprocessingml.document"}:n("ppt/",{offset:a})?s={ext:"pptx",mime:"application/vnd.openxmlformats-officedocument.presentationml.presentation"}:n("xl/",{offset:a})&&(s={ext:"xlsx",mime:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"})),i&&s)return s;o=t(e,a)}while(o>=0);if(s)return s}if(r([80,75])&&(3===e[2]||5===e[2]||7===e[2])&&(4===e[3]||6===e[3]||8===e[3]))return{ext:"zip",mime:"application/zip"};if(r([117,115,116,97,114],{offset:257}))return{ext:"tar",mime:"application/x-tar"};if(r([82,97,114,33,26,7])&&(0===e[6]||1===e[6]))return{ext:"rar",mime:"application/x-rar-compressed"};if(r([31,139,8]))return{ext:"gz",mime:"application/gzip"};if(r([66,90,104]))return{ext:"bz2",mime:"application/x-bzip2"};if(r([55,122,188,175,39,28]))return{ext:"7z",mime:"application/x-7z-compressed"};if(r([120,1]))return{ext:"dmg",mime:"application/x-apple-diskimage"};if(r([51,103,112,53])||r([0,0,0])&&r([102,116,121,112],{offset:4})&&(r([109,112,52,49],{offset:8})||r([109,112,52,50],{offset:8})||r([105,115,111,109],{offset:8})||r([105,115,111,50],{offset:8})||r([109,109,112,52],{offset:8})||r([77,52,86],{offset:8})||r([100,97,115,104],{offset:8})))return{ext:"mp4",mime:"video/mp4"};if(r([77,84,104,100]))return{ext:"mid",mime:"audio/midi"};if(r([26,69,223,163])){const t=e.subarray(4,4100),r=t.findIndex((t,e,r)=>66===r[e]&&130===r[e+1]);if(-1!==r){const e=r+3,n=r=>[...r].every((r,n)=>t[e+n]===r.charCodeAt(0));if(n("matroska"))return{ext:"mkv",mime:"video/x-matroska"};if(n("webm"))return{ext:"webm",mime:"video/webm"}}}if(r([0,0,0,20,102,116,121,112,113,116,32,32])||r([102,114,101,101],{offset:4})||r([102,116,121,112,113,116,32,32],{offset:4})||r([109,100,97,116],{offset:4})||r([109,111,111,118],{offset:4})||r([119,105,100,101],{offset:4}))return{ext:"mov",mime:"video/quicktime"};if(r([82,73,70,70])){if(r([65,86,73],{offset:8}))return{ext:"avi",mime:"video/vnd.avi"};if(r([87,65,86,69],{offset:8}))return{ext:"wav",mime:"audio/vnd.wave"};if(r([81,76,67,77],{offset:8}))return{ext:"qcp",mime:"audio/qcelp"}}if(r([48,38,178,117,142,102,207,17,166,217])){let t=30;do{const n=readUInt64LE(e,t+16);if(r([145,7,220,183,183,169,207,17,142,230,0,192,12,32,83,101],{offset:t})){if(r([64,158,105,248,77,91,207,17,168,253,0,128,95,92,68,43],{offset:t+24}))return{ext:"wma",mime:"audio/x-ms-wma"};if(r([192,239,25,188,77,91,207,17,168,253,0,128,95,92,68,43],{offset:t+24}))return{ext:"wmv",mime:"video/x-ms-asf"};break}t+=n}while(t+24<=e.length);return{ext:"asf",mime:"application/vnd.ms-asf"}}if(r([0,0,1,186])||r([0,0,1,179]))return{ext:"mpg",mime:"video/mpeg"};if(r([102,116,121,112,51,103],{offset:4}))return{ext:"3gp",mime:"video/3gpp"};for(let t=0;t<2&&t<e.length-16;t++){if(r([73,68,51],{offset:t})||r([255,226],{offset:t,mask:[255,226]}))return{ext:"mp3",mime:"audio/mpeg"};if(r([255,228],{offset:t,mask:[255,228]}))return{ext:"mp2",mime:"audio/mpeg"};if(r([255,248],{offset:t,mask:[255,252]}))return{ext:"mp2",mime:"audio/mpeg"};if(r([255,240],{offset:t,mask:[255,252]}))return{ext:"mp4",mime:"audio/mpeg"}}if(r([102,116,121,112,77,52,65],{offset:4}))return{ext:"m4a",mime:"audio/mp4"};if(r([79,112,117,115,72,101,97,100],{offset:28}))return{ext:"opus",mime:"audio/opus"};if(r([79,103,103,83]))return r([128,116,104,101,111,114,97],{offset:28})?{ext:"ogv",mime:"video/ogg"}:r([1,118,105,100,101,111,0],{offset:28})?{ext:"ogm",mime:"video/ogg"}:r([127,70,76,65,67],{offset:28})?{ext:"oga",mime:"audio/ogg"}:r([83,112,101,101,120,32,32],{offset:28})?{ext:"spx",mime:"audio/ogg"}:r([1,118,111,114,98,105,115],{offset:28})?{ext:"ogg",mime:"audio/ogg"}:{ext:"ogx",mime:"application/ogg"};if(r([102,76,97,67]))return{ext:"flac",mime:"audio/x-flac"};if(r([77,65,67,32]))return{ext:"ape",mime:"audio/ape"};if(r([119,118,112,107]))return{ext:"wv",mime:"audio/wavpack"};if(r([35,33,65,77,82,10]))return{ext:"amr",mime:"audio/amr"};if(r([37,80,68,70]))return{ext:"pdf",mime:"application/pdf"};if(r([77,90]))return{ext:"exe",mime:"application/x-msdownload"};if((67===e[0]||70===e[0])&&r([87,83],{offset:1}))return{ext:"swf",mime:"application/x-shockwave-flash"};if(r([123,92,114,116,102]))return{ext:"rtf",mime:"application/rtf"};if(r([0,97,115,109]))return{ext:"wasm",mime:"application/wasm"};if(r([119,79,70,70])&&(r([0,1,0,0],{offset:4})||r([79,84,84,79],{offset:4})))return{ext:"woff",mime:"font/woff"};if(r([119,79,70,50])&&(r([0,1,0,0],{offset:4})||r([79,84,84,79],{offset:4})))return{ext:"woff2",mime:"font/woff2"};if(r([76,80],{offset:34})&&(r([0,0,1],{offset:8})||r([1,0,2],{offset:8})||r([2,0,2],{offset:8})))return{ext:"eot",mime:"application/vnd.ms-fontobject"};if(r([0,1,0,0,0]))return{ext:"ttf",mime:"font/ttf"};if(r([79,84,84,79,0]))return{ext:"otf",mime:"font/otf"};if(r([0,0,1,0]))return{ext:"ico",mime:"image/x-icon"};if(r([0,0,2,0]))return{ext:"cur",mime:"image/x-icon"};if(r([70,76,86,1]))return{ext:"flv",mime:"video/x-flv"};if(r([37,33]))return{ext:"ps",mime:"application/postscript"};if(r([253,55,122,88,90,0]))return{ext:"xz",mime:"application/x-xz"};if(r([83,81,76,105]))return{ext:"sqlite",mime:"application/x-sqlite3"};if(r([78,69,83,26]))return{ext:"nes",mime:"application/x-nintendo-nes-rom"};if(r([67,114,50,52]))return{ext:"crx",mime:"application/x-google-chrome-extension"};if(r([77,83,67,70])||r([73,83,99,40]))return{ext:"cab",mime:"application/vnd.ms-cab-compressed"};if(r([33,60,97,114,99,104,62,10,100,101,98,105,97,110,45,98,105,110,97,114,121]))return{ext:"deb",mime:"application/x-deb"};if(r([33,60,97,114,99,104,62]))return{ext:"ar",mime:"application/x-unix-archive"};if(r([237,171,238,219]))return{ext:"rpm",mime:"application/x-rpm"};if(r([31,160])||r([31,157]))return{ext:"Z",mime:"application/x-compress"};if(r([76,90,73,80]))return{ext:"lz",mime:"application/x-lzip"};if(r([208,207,17,224,161,177,26,225]))return{ext:"msi",mime:"application/x-msi"};if(r([6,14,43,52,2,5,1,1,13,1,2,1,1,2]))return{ext:"mxf",mime:"application/mxf"};if(r([71],{offset:4})&&(r([71],{offset:192})||r([71],{offset:196})))return{ext:"mts",mime:"video/mp2t"};if(r([66,76,69,78,68,69,82]))return{ext:"blend",mime:"application/x-blender"};if(r([66,80,71,251]))return{ext:"bpg",mime:"image/bpg"};if(r([0,0,0,12,106,80,32,32,13,10,135,10])){if(r([106,112,50,32],{offset:20}))return{ext:"jp2",mime:"image/jp2"};if(r([106,112,120,32],{offset:20}))return{ext:"jpx",mime:"image/jpx"};if(r([106,112,109,32],{offset:20}))return{ext:"jpm",mime:"image/jpm"};if(r([109,106,112,50],{offset:20}))return{ext:"mj2",mime:"image/mj2"}}if(r([70,79,82,77]))return{ext:"aif",mime:"audio/aiff"};if(n("<?xml "))return{ext:"xml",mime:"application/xml"};if(r([66,79,79,75,77,79,66,73],{offset:60}))return{ext:"mobi",mime:"application/x-mobipocket-ebook"};if(r([102,116,121,112],{offset:4})){if(r([109,105,102,49],{offset:8}))return{ext:"heic",mime:"image/heif"};if(r([109,115,102,49],{offset:8}))return{ext:"heic",mime:"image/heif-sequence"};if(r([104,101,105,99],{offset:8})||r([104,101,105,120],{offset:8}))return{ext:"heic",mime:"image/heic"};if(r([104,101,118,99],{offset:8})||r([104,101,118,120],{offset:8}))return{ext:"heic",mime:"image/heic-sequence"}}return r([171,75,84,88,32,49,49,187,13,10,26,10])?{ext:"ktx",mime:"image/ktx"}:r([68,73,67,77],{offset:128})?{ext:"dcm",mime:"application/dicom"}:r([77,80,43])||r([77,80,67,75])?{ext:"mpc",mime:"audio/x-musepack"}:r([66,69,71,73,78,58])?{ext:"ics",mime:"text/calendar"}:r([103,108,84,70,2,0,0,0])?{ext:"glb",mime:"model/gltf-binary"}:r([212,195,178,161])||r([161,178,195,212])?{ext:"pcap",mime:"application/vnd.tcpdump.pcap"}:null};module.exports=fileType,module.exports.default=fileType,Object.defineProperty(fileType,"minimumBytes",{value:4100}),module.exports.stream=readableStream=>new Promise((resolve,reject)=>{const stream=eval("require")("stream");readableStream.once("readable",()=>{const t=new stream.PassThrough,e=readableStream.read(module.exports.minimumBytes)||readableStream.read();try{t.fileType=fileType(e)}catch(t){reject(t)}readableStream.unshift(e),stream.pipeline?resolve(stream.pipeline(readableStream,t,()=>{})):resolve(readableStream.pipe(t))})})})(fileType$1);var fileTypeExports=fileType$1.exports;const fileType=fileTypeExports,imageExts=new Set(["jpg","png","gif","webp","flif","cr2","tif","bmp","jxr","psd","ico","bpg","jp2","jpm","jpx","heic","cur","dcm"]),imageType=t=>{const e=fileType(t);return imageExts.has(e&&e.ext)?e:null};imageType$2.exports=imageType,imageType$2.exports.default=imageType,Object.defineProperty(imageType,"minimumBytes",{value:fileType.minimumBytes});var imageTypeExports=imageType$2.exports,imageType$1=getDefaultExportFromCjs(imageTypeExports);function decodeBmp(t){const e=decode$3(t);if(1===e.bitsPerPixel){return new Mask(e.width,e.height,{data:e.data}).convertColor("GREY")}{let t;switch(e.channels){case 1:t="GREY";break;case 3:t="RGB";break;default:t="RGBA"}return new Image(e.width,e.height,{colorModel:t,data:e.data,resolution:getBmpResolution(e)})}}function getBmpResolution(t){if(t.xPixelsPerMeter&&t.yPixelsPerMeter)return{x:t.xPixelsPerMeter,y:t.yPixelsPerMeter,unit:"meter"}}const tagsById$2={33434:"ExposureTime",33437:"FNumber",34850:"ExposureProgram",34852:"SpectralSensitivity",34855:"ISOSpeedRatings",34856:"OECF",34864:"SensitivityType",34865:"StandardOutputSensitivity",34866:"RecommendedExposureIndex",34867:"ISOSpeed",34868:"ISOSpeedLatitudeyyy",34869:"ISOSpeedLatitudezzz",36864:"ExifVersion",36867:"DateTimeOriginal",36868:"DateTimeDigitized",37121:"ComponentsConfiguration",37122:"CompressedBitsPerPixel",37377:"ShutterSpeedValue",37378:"ApertureValue",37379:"BrightnessValue",37380:"ExposureBiasValue",37381:"MaxApertureValue",37382:"SubjectDistance",37383:"MeteringMode",37384:"LightSource",37385:"Flash",37386:"FocalLength",37396:"SubjectArea",37500:"MakerNote",37510:"UserComment",37520:"SubsecTime",37521:"SubsecTimeOriginal",37522:"SubsecTimeDigitized",40960:"FlashpixVersion",40961:"ColorSpace",40962:"PixelXDimension",40963:"PixelYDimension",40964:"RelatedSoundFile",41483:"FlashEnergy",41484:"SpatialFrequencyResponse",41486:"FocalPlaneXResolution",41487:"FocalPlaneYResolution",41488:"FocalPlaneResolutionUnit",41492:"SubjectLocation",41493:"ExposureIndex",41495:"SensingMethod",41728:"FileSource",41729:"SceneType",41730:"CFAPattern",41985:"CustomRendered",41986:"ExposureMode",41987:"WhiteBalance",41988:"DigitalZoomRatio",41989:"FocalLengthIn35mmFilm",41990:"SceneCaptureType",41991:"GainControl",41992:"Contrast",41993:"Saturation",41994:"Sharpness",41995:"DeviceSettingDescription",41996:"SubjectDistanceRange",42016:"ImageUniqueID",42032:"CameraOwnerName",42033:"BodySerialNumber",42034:"LensSpecification",42035:"LensMake",42036:"LensModel",42037:"LensSerialNumber",42240:"Gamma"},tagsByName$2={};for(const t in tagsById$2)tagsByName$2[tagsById$2[t]]=Number(t);var exif=Object.freeze({__proto__:null,tagsById:tagsById$2,tagsByName:tagsByName$2});const tagsById$1={0:"GPSVersionID",1:"GPSLatitudeRef",2:"GPSLatitude",3:"GPSLongitudeRef",4:"GPSLongitude",5:"GPSAltitudeRef",6:"GPSAltitude",7:"GPSTimeStamp",8:"GPSSatellites",9:"GPSStatus",10:"GPSMeasureMode",11:"GPSDOP",12:"GPSSpeedRef",13:"GPSSpeed",14:"GPSTrackRef",15:"GPSTrack",16:"GPSImgDirectionRef",17:"GPSImgDirection",18:"GPSMapDatum",19:"GPSDestLatitudeRef",20:"GPSDestLatitude",21:"GPSDestLongitudeRef",22:"GPSDestLongitude",23:"GPSDestBearingRef",24:"GPSDestBearing",25:"GPSDestDistanceRef",26:"GPSDestDistance",27:"GPSProcessingMethod",28:"GPSAreaInformation",29:"GPSDateStamp",30:"GPSDifferential",31:"GPSHPositioningError"},tagsByName$1={};for(const t in tagsById$1)tagsByName$1[tagsById$1[t]]=Number(t);var gps=Object.freeze({__proto__:null,tagsById:tagsById$1,tagsByName:tagsByName$1});const tagsById={254:"NewSubfileType",255:"SubfileType",256:"ImageWidth",257:"ImageLength",258:"BitsPerSample",259:"Compression",262:"PhotometricInterpretation",263:"Threshholding",264:"CellWidth",265:"CellLength",266:"FillOrder",270:"ImageDescription",271:"Make",272:"Model",273:"StripOffsets",274:"Orientation",277:"SamplesPerPixel",278:"RowsPerStrip",279:"StripByteCounts",280:"MinSampleValue",281:"MaxSampleValue",282:"XResolution",283:"YResolution",284:"PlanarConfiguration",288:"FreeOffsets",289:"FreeByteCounts",290:"GrayResponseUnit",291:"GrayResponseCurve",296:"ResolutionUnit",305:"Software",306:"DateTime",315:"Artist",316:"HostComputer",320:"ColorMap",338:"ExtraSamples",33432:"Copyright",269:"DocumentName",285:"PageName",286:"XPosition",287:"YPosition",292:"T4Options",293:"T6Options",297:"PageNumber",301:"TransferFunction",317:"Predictor",318:"WhitePoint",319:"PrimaryChromaticities",321:"HalftoneHints",322:"TileWidth",323:"TileLength",324:"TileOffsets",325:"TileByteCounts",326:"BadFaxLines",327:"CleanFaxData",328:"ConsecutiveBadFaxLines",330:"SubIFDs",332:"InkSet",333:"InkNames",334:"NumberOfInks",336:"DotRange",337:"TargetPrinter",339:"SampleFormat",340:"SMinSampleValue",341:"SMaxSampleValue",342:"TransferRange",343:"ClipPath",344:"XClipPathUnits",345:"YClipPathUnits",346:"Indexed",347:"JPEGTables",351:"OPIProxy",400:"GlobalParametersIFD",401:"ProfileType",402:"FaxProfile",403:"CodingMethods",404:"VersionYear",405:"ModeNumber",433:"Decode",434:"DefaultImageColor",512:"JPEGProc",513:"JPEGInterchangeFormat",514:"JPEGInterchangeFormatLength",515:"JPEGRestartInterval",517:"JPEGLosslessPredictors",518:"JPEGPointTransforms",519:"JPEGQTables",520:"JPEGDCTables",521:"JPEGACTables",529:"YCbCrCoefficients",530:"YCbCrSubSampling",531:"YCbCrPositioning",532:"ReferenceBlackWhite",559:"StripRowCounts",700:"XMP",32781:"ImageID",34732:"ImageLayer",32932:"WangAnnotatio",33445:"MDFileTag",33446:"MDScalePixel",33447:"MDColorTable",33448:"MDLabName",33449:"MDSampleInfo",33450:"MDPrepDate",33451:"MDPrepTime",33452:"MDFileUnits",33550:"ModelPixelScaleTag",33723:"IPTC",33918:"INGRPacketDataTag",33919:"INGRFlagRegisters",33920:"IrasBTransformationMatrix",33922:"ModelTiepointTag",34264:"ModelTransformationTag",34377:"Photoshop",34665:"ExifIFD",34675:"ICCProfile",34735:"GeoKeyDirectoryTag",34736:"GeoDoubleParamsTag",34737:"GeoAsciiParamsTag",34853:"GPSIFD",34908:"HylaFAXFaxRecvParams",34909:"HylaFAXFaxSubAddress",34910:"HylaFAXFaxRecvTime",37724:"ImageSourceData",40965:"InteroperabilityIFD",42112:"GDAL_METADATA",42113:"GDAL_NODATA",50215:"OceScanjobDescription",50216:"OceApplicationSelector",50217:"OceIdentificationNumber",50218:"OceImageLogicCharacteristics",50706:"DNGVersion",50707:"DNGBackwardVersion",50708:"UniqueCameraModel",50709:"LocalizedCameraModel",50710:"CFAPlaneColor",50711:"CFALayout",50712:"LinearizationTable",50713:"BlackLevelRepeatDim",50714:"BlackLevel",50715:"BlackLevelDeltaH",50716:"BlackLevelDeltaV",50717:"WhiteLevel",50718:"DefaultScale",50719:"DefaultCropOrigin",50720:"DefaultCropSize",50721:"ColorMatrix1",50722:"ColorMatrix2",50723:"CameraCalibration1",50724:"CameraCalibration2",50725:"ReductionMatrix1",50726:"ReductionMatrix2",50727:"AnalogBalance",50728:"AsShotNeutral",50729:"AsShotWhiteXY",50730:"BaselineExposure",50731:"BaselineNoise",50732:"BaselineSharpness",50733:"BayerGreenSplit",50734:"LinearResponseLimit",50735:"CameraSerialNumber",50736:"LensInfo",50737:"ChromaBlurRadius",50738:"AntiAliasStrength",50740:"DNGPrivateData",50741:"MakerNoteSafety",50778:"CalibrationIlluminant1",50779:"CalibrationIlluminant2",50780:"BestQualityScale",50784:"AliasLayerMetadata"},tagsByName={};for(const t in tagsById)tagsByName[tagsById[t]]=Number(t);var standard=Object.freeze({__proto__:null,tagsById:tagsById,tagsByName:tagsByName});function guessStripByteCounts(t){if(1!==t.compression)throw new Error("missing mandatory StripByteCounts field in compressed image");const e=t.rowsPerStrip*t.width*t.samplesPerPixel*(t.bitsPerSample/8);return new Array(t.stripOffsets.length).fill(e)}function applyHorizontalDifferencing8Bit(t,e,r){let n=0;for(;n<t.length;){for(let o=r;o<e*r;o+=r)for(let e=0;e<r;e++)t[n+o+e]=t[n+o+e]+t[n+o-(r-e)]&255;n+=e*r}}function applyHorizontalDifferencing16Bit(t,e,r){let n=0;for(;n<t.length;){for(let o=r;o<e*r;o+=r)for(let e=0;e<r;e++)t[n+o+e]=t[n+o+e]+t[n+o-(r-e)]&65535;n+=e*r}}const tags={standard:standard,exif:exif,gps:gps};class IFD{kind;data;fields;exif;gps;_hasMap;_map;constructor(t){if(!t)throw new Error("missing kind");this.data=new Uint8Array,this.fields=new Map,this.kind=t,this._hasMap=!1,this._map={}}get(t){if("number"==typeof t)return this.fields.get(t);if("string"==typeof t)return this.fields.get(tags[this.kind].tagsByName[t]);throw new Error("expected a number or string")}get map(){if(!this._hasMap){const t=tags[this.kind].tagsById;for(const e of this.fields.keys())t[e]&&(this._map[t[e]]=this.fields.get(e));this._hasMap=!0}return this._map}}const types=new Map([[1,[1,readByte]],[2,[1,readASCII]],[3,[2,readShort]],[4,[4,readLong]],[5,[8,readRational]],[6,[1,readSByte]],[7,[1,readByte]],[8,[2,readSShort]],[9,[4,readSLong]],[10,[8,readSRational]],[11,[4,readFloat]],[12,[8,readDouble]]]);function getByteLength(t,e){const r=types.get(t);if(!r)throw new Error(`type not found: ${t}`);return r[0]*e}function readData(t,e,r){const n=types.get(e);if(!n)throw new Error(`type not found: ${e}`);return n[1](t,r)}function readByte(t,e){if(1===e)return t.readUint8();const r=new Uint8Array(e);for(let n=0;n<e;n++)r[n]=t.readUint8();return r}function readASCII(t,e){const r=[];let n="";for(let o=0;o<e;o++){const e=String.fromCharCode(t.readUint8());"\0"===e?(r.push(n),n=""):n+=e}return 1===r.length?r[0]:r}function readShort(t,e){if(1===e)return t.readUint16();const r=new Uint16Array(e);for(let n=0;n<e;n++)r[n]=t.readUint16();return r}function readLong(t,e){if(1===e)return t.readUint32();const r=new Uint32Array(e);for(let n=0;n<e;n++)r[n]=t.readUint32();return r}function readRational(t,e){if(1===e)return t.readUint32()/t.readUint32();const r=new Array(e);for(let n=0;n<e;n++)r[n]=t.readUint32()/t.readUint32();return r}function readSByte(t,e){if(1===e)return t.readInt8();const r=new Int8Array(e);for(let n=0;n<e;n++)r[n]=t.readInt8();return r}function readSShort(t,e){if(1===e)return t.readInt16();const r=new Int16Array(e);for(let n=0;n<e;n++)r[n]=t.readInt16();return r}function readSLong(t,e){if(1===e)return t.readInt32();const r=new Int32Array(e);for(let n=0;n<e;n++)r[n]=t.readInt32();return r}function readSRational(t,e){if(1===e)return t.readInt32()/t.readInt32();const r=new Array(e);for(let n=0;n<e;n++)r[n]=t.readInt32()/t.readInt32();return r}function readFloat(t,e){if(1===e)return t.readFloat32();const r=new Float32Array(e);for(let n=0;n<e;n++)r[n]=t.readFloat32();return r}function readDouble(t,e){if(1===e)return t.readFloat64();const r=new Float64Array(e);for(let n=0;n<e;n++)r[n]=t.readFloat64();return r}const CLEAR_CODE=256,EOI_CODE=257,TABLE_START=258,MIN_BIT_LENGTH=9;let stringTable=[];function initializeStringTable(){if(0===stringTable.length){for(let t=0;t<256;t++)stringTable.push([t]);const t=[];for(let e=256;e<4096;e++)stringTable.push(t)}}const andTable=[511,1023,2047,4095],bitJumps=[0,0,0,0,0,0,0,0,0,511,1023,2047,4095];class LzwDecoder{stripArray;nextData=0;nextBits=0;bytePointer=0;tableLength=TABLE_START;currentBitLength=MIN_BIT_LENGTH;outData;constructor(t){this.stripArray=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),this.outData=new IOBuffer(t.byteLength),this.initializeTable()}decode(){let t=0,e=0;for(;(t=this.getNextCode())!==EOI_CODE;)if(t===CLEAR_CODE){if(this.initializeTable(),t=this.getNextCode(),t===EOI_CODE)break;this.writeString(this.stringFromCode(t)),e=t}else if(this.isInTable(t))this.writeString(this.stringFromCode(t)),this.addStringToTable(this.stringFromCode(e).concat(this.stringFromCode(t)[0])),e=t;else{const r=this.stringFromCode(e).concat(this.stringFromCode(e)[0]);this.writeString(r),this.addStringToTable(r),e=t}const r=this.outData.toArray();return new DataView(r.buffer,r.byteOffset,r.byteLength)}initializeTable(){initializeStringTable(),this.tableLength=TABLE_START,this.currentBitLength=MIN_BIT_LENGTH}writeString(t){this.outData.writeBytes(t)}stringFromCode(t){return stringTable[t]}isInTable(t){return t<this.tableLength}addStringToTable(t){if(stringTable[this.tableLength++]=t,stringTable.length>4096)throw stringTable=[],new Error("LZW decoding error. Please open an issue at https://github.com/image-js/tiff/issues/new/choose (include a test image).");this.tableLength===bitJumps[this.currentBitLength]&&this.currentBitLength++}getNextCode(){this.nextData=this.nextData<<8|255&this.stripArray[this.bytePointer++],this.nextBits+=8,this.nextBits<this.currentBitLength&&(this.nextData=this.nextData<<8|255&this.stripArray[this.bytePointer++],this.nextBits+=8);const t=this.nextData>>this.nextBits-this.currentBitLength&andTable[this.currentBitLength-9];return this.nextBits-=this.currentBitLength,this.bytePointer>this.stripArray.length?257:t}}function decompressLzw(t){return new LzwDecoder(t).decode()}const dateTimeRegex=/^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/;class TiffIfd extends IFD{constructor(){super("standard")}get size(){return this.width*this.height}get width(){return this.imageWidth}get height(){return this.imageLength}get components(){return this.samplesPerPixel}get date(){const t=new Date,e=dateTimeRegex.exec(this.dateTime);if(null===e)throw new Error(`invalid dateTime: ${this.dateTime}`);return t.setFullYear(Number(e[1]),Number(e[2])-1,Number(e[3])),t.setHours(Number(e[4]),Number(e[5]),Number(e[6])),t}get newSubfileType(){return this.get("NewSubfileType")}get imageWidth(){return this.get("ImageWidth")}get imageLength(){return this.get("ImageLength")}get bitsPerSample(){const t=this.get("BitsPerSample");return t&&"number"!=typeof t?t[0]:t}get alpha(){const t=this.extraSamples;return!!t&&0!==t[0]}get associatedAlpha(){const t=this.extraSamples;return!!t&&1===t[0]}get extraSamples(){return alwaysArray(this.get("ExtraSamples"))}get compression(){return this.get("Compression")||1}get type(){return this.get("PhotometricInterpretation")}get fillOrder(){return this.get("FillOrder")||1}get documentName(){return this.get("DocumentName")}get imageDescription(){return this.get("ImageDescription")}get stripOffsets(){return alwaysArray(this.get("StripOffsets"))}get orientation(){return this.get("Orientation")}get samplesPerPixel(){return this.get("SamplesPerPixel")||1}get rowsPerStrip(){return this.get("RowsPerStrip")||2**32-1}get stripByteCounts(){return alwaysArray(this.get("StripByteCounts"))}get minSampleValue(){return this.get("MinSampleValue")||0}get maxSampleValue(){return this.get("MaxSampleValue")||2**this.bitsPerSample-1}get xResolution(){return this.get("XResolution")}get yResolution(){return this.get("YResolution")}get planarConfiguration(){return this.get("PlanarConfiguration")||1}get resolutionUnit(){return this.get("ResolutionUnit")||2}get dateTime(){return this.get("DateTime")}get predictor(){return this.get("Predictor")||1}get sampleFormat(){return alwaysArray(this.get("SampleFormat")||1)[0]}get sMinSampleValue(){return this.get("SMinSampleValue")||this.minSampleValue}get sMaxSampleValue(){return this.get("SMaxSampleValue")||this.maxSampleValue}get palette(){const t=2**this.bitsPerSample,e=this.get("ColorMap");if(!e)return;if(e.length!==3*t)throw new Error(`ColorMap size must be ${t}`);const r=[];for(let n=0;n<t;n++)r.push([e[n],e[n+t],e[n+2*t]]);return r}get tileWidth(){return this.get("TileWidth")}get tileHeight(){return this.get("TileLength")}get tileOffsets(){return alwaysArray(this.get("TileOffsets"))}get tileByteCounts(){return alwaysArray(this.get("TileByteCounts"))}get tiled(){return void 0!==this.tileWidth&&void 0!==this.tileHeight&&void 0!==this.tileOffsets&&void 0!==this.tileByteCounts}}function alwaysArray(t){return"number"==typeof t?[t]:t}function decompressZlib(t){const e=decompressSync(new Uint8Array(t.buffer,t.byteOffset,t.byteLength));return new DataView(e.buffer,e.byteOffset,e.byteLength)}const defaultOptions={ignoreImageData:!1};class TIFFDecoder extends IOBuffer{_nextIFD;constructor(t){super(t),this._nextIFD=0}get isMultiPage(){let t=0;for(this.decodeHeader();this._nextIFD;)if(t++,this.decodeIFD({ignoreImageData:!0},!0),2===t)return!0;if(1===t)return!1;throw unsupported("ifdCount",t)}get pageCount(){let t=0;for(this.decodeHeader();this._nextIFD;)t++,this.decodeIFD({ignoreImageData:!0},!0);if(t>0)return t;throw unsupported("ifdCount",t)}decode(t={}){const{pages:e}=t;checkPages(e);const r=e?Math.max(...e):1/0;t={...defaultOptions,...t};const n=[];this.decodeHeader();let o=0;for(;this._nextIFD;){if(e){if(e.includes(o)?n.push(this.decodeIFD(t,!0)):this.decodeIFD({ignoreImageData:!0},!0),o===r)break}else n.push(this.decodeIFD(t,!0));o++}if(o<r&&r!==1/0)throw new RangeError(`Index ${r} is out of bounds. The stack only contains ${o} images.`);return n}decodeHeader(){const t=this.readUint16();if(18761===t)this.setLittleEndian();else{if(19789!==t)throw new Error(`invalid byte order: 0x${t.toString(16)}`);this.setBigEndian()}if(42!==this.readUint16())throw new Error("not a TIFF file");this._nextIFD=this.readUint32()}decodeIFD(t,e){let r;if(this.seek(this._nextIFD),e)r=new TiffIfd;else{if(!t.kind)throw new Error("kind is missing");r=new IFD(t.kind)}const n=this.readUint16();for(let t=0;t<n;t++)this.decodeIFDEntry(r);if(!t.ignoreImageData){if(!(r instanceof TiffIfd))throw new Error("must be a tiff ifd");this.decodeImageData(r)}return this._nextIFD=this.readUint32(),r}decodeIFDEntry(t){const e=this.offset,r=this.readUint16(),n=this.readUint16(),o=this.readUint32();if(n<1||n>12)return void this.skip(4);getByteLength(n,o)>4&&this.seek(this.readUint32());const i=readData(this,n,o);if(t.fields.set(r,i),34665===r||34853===r){const e=this.offset;let n="exif";34665===r?n="exif":34853===r&&(n="gps"),this._nextIFD=i,t[n]=this.decodeIFD({kind:n,ignoreImageData:!0},!1),this.offset=e}this.seek(e),this.skip(12)}decodeImageData(t){const e=t.orientation;if(e&&1!==e)throw unsupported("orientation",e);switch(t.type){case 0:case 1:case 2:case 3:t.tiled?this.readTileData(t):this.readStripData(t);break;default:throw unsupported("image type",t.type)}if(this.applyPredictor(t),this.convertAlpha(t),1===t.bitsPerSample&&this.split1BitData(t),0===t.type){const e=2**t.bitsPerSample-1;for(let r=0;r<t.data.length;r++)t.data[r]=e-t.data[r]}}split1BitData(t){const{imageWidth:e,imageLength:r,samplesPerPixel:n}=t,o=new Uint8Array(r*e*n),i=Math.ceil(e*n/8);let s=0;for(let a=0;a<r;a++){const r=a*i;for(let i=0;i<e*n;i++){const e=r+Math.floor(i/8),n=7-i%8,a=t.data[e]>>n&1;o[s++]=a}}t.data=o}static uncompress(t,e=1){switch(e){case 1:return t;case 5:return decompressLzw(t);case 8:case 32946:return decompressZlib(t);case 2:throw unsupported("Compression","CCITT Group 3");case 32773:throw unsupported("Compression","PackBits");default:throw unsupported("Compression",e)}}createSampleReader(t,e,r){if(8===e||1===e)return(t,e)=>t.getUint8(e);if(16===e)return(t,e)=>t.getUint16(2*e,r);if(32===e&&3===t)return(t,e)=>t.getFloat32(4*e,r);if(64===e&&3===t)return(t,e)=>t.getFloat64(8*e,r);throw unsupported("bitDepth",e)}readStripData(t){const e=t.width,r=t.height,n=1!==t.bitsPerSample?e*t.samplesPerPixel*r:Math.ceil(e*t.samplesPerPixel/8)*r,o=t.stripOffsets,i=t.stripByteCounts||guessStripByteCounts(t),s=this.isLittleEndian(),a=1!==t.bitsPerSample?e*t.samplesPerPixel*t.rowsPerStrip:Math.ceil(e*t.samplesPerPixel/8)*t.rowsPerStrip,h=this.createSampleReader(t.sampleFormat,t.bitsPerSample,s),l=getDataArray(n,t.bitsPerSample,t.sampleFormat);let c=0;for(let e=0;e<o.length;e++){const r=new DataView(this.buffer,this.byteOffset+o[e],i[e]),s=TIFFDecoder.uncompress(r,t.compression),u=Math.min(a,n-c);for(let t=0;t<u;++t){const e=h(s,t);l[c+t]=e}c+=u}t.data=l}readTileData(t){if(!t.tileWidth||!t.tileHeight)return;const e=t.width,r=t.height,n=1!==t.bitsPerSample?e*r*t.samplesPerPixel:Math.ceil(e*t.samplesPerPixel/8)*r,o=t.tileWidth,i=t.tileHeight,s=Math.ceil(e/o),a=Math.ceil(r/i),h=t.tileOffsets,l=t.tileByteCounts,c=this.isLittleEndian(),u=this.createSampleReader(t.sampleFormat,t.bitsPerSample,c),f=getDataArray(n,t.bitsPerSample,t.sampleFormat);for(let n=0;n<s;++n)for(let c=0;c<a;++c){const a=c*s+n,d=new DataView(this.buffer,this.byteOffset+h[a],l[a]),g=TIFFDecoder.uncompress(d,t.compression);if(1===t.bitsPerSample){const t=Math.ceil(e/8),s=Math.ceil(o/8);for(let e=0;e<i&&c*i+e<r;e++){const r=e*s,a=(c*i+e)*t+Math.floor(n*o/8),h=Math.min(s,t-Math.floor(n*o/8));for(let t=0;t<h;t++)f[a+t]=u(g,r+t)}}else for(let s=0;s<i;s++)for(let a=0;a<o;a++){const h=n*o+a,l=c*i+s;if(h>=e||l>=r)continue;const d=u(g,s*o+a);f[(l*e+h)*t.samplesPerPixel]=d}}t.data=f}applyPredictor(t){const e=t.bitsPerSample;switch(t.predictor){case 1:break;case 2:if(8===e)applyHorizontalDifferencing8Bit(t.data,t.width,t.components);else{if(16!==e)throw new Error(`Horizontal differencing is only supported for images with a bit depth of ${e}`);applyHorizontalDifferencing16Bit(t.data,t.width,t.components)}break;default:throw new Error(`invalid predictor: ${t.predictor}`)}}convertAlpha(t){if(t.alpha&&t.associatedAlpha){const{data:e,components:r,maxSampleValue:n}=t;for(let t=0;t<e.length;t+=r){const o=e[t+r-1];for(let i=0;i<r-1;i++)e[t+i]=Math.round(e[t+i]*n/o)}}}}function getDataArray(t,e,r){if(8===e||1===e)return new Uint8Array(t);if(16===e)return new Uint16Array(t);if(32===e&&3===r)return new Float32Array(t);if(64===e&&3===r)return new Float64Array(t);throw unsupported("bit depth / sample format",`${e} / ${r}`)}function unsupported(t,e){return new Error(`Unsupported ${t}: ${e}`)}function checkPages(t){if(t)for(const e of t)if(e<0||!Number.isInteger(e))throw new RangeError(`Index ${e} is invalid. Must be a positive integer.`)}function decodeTIFF(t,e){return new TIFFDecoder(t).decode(e)}function decode$1(t){const e=new IOBuffer(t),r={};e.setBigEndian();if(65496!==e.readUint16())throw new Error("SOI marker not found. Not a valid JPEG file");if(65505===e.readUint16()){e.skip(2);const t=e.readBytes(6);69===t[0]&&120===t[1]&&105===t[2]&&102===t[3]&&0===t[4]&&0===t[5]&&(r.exif=decodeTIFF(new Uint8Array(e.buffer,e.byteOffset+12,e.byteLength-12),{pages:[0],ignoreImageData:!0}))}return r}function getMetadata(t){return{tiff:{fields:t.fields,tags:t.map},exif:t.exif}}function decodeJpeg(t){const e=jpegJs.decode(t,{useTArray:!0,maxMemoryUsageInMB:Number.POSITIVE_INFINITY,maxResolutionInMP:Number.POSITIVE_INFINITY}),r=decode$1(t),n=r.exif?.[0]?getMetadata(r.exif[0]):void 0;return new Image(e.width,e.height,{data:e.data,colorModel:"RGBA",meta:n})}function decodePng(t){const e=decodePng$1(t);let r;const n=e.depth;if(e.palette)return loadPalettePng(e);if(1===n)return new Image(e.width,e.height,{data:decodeBinary(e),colorModel:"GREY"});switch(e.channels){case 1:r="GREY";break;case 2:r="GREYA";break;case 3:r="RGB";break;case 4:r="RGBA";break;default:throw new RangeError(`invalid number of channels: ${e.channels}`)}const o=getResolution(e);return new Image(e.width,e.height,{colorModel:r,bitDepth:n,data:e.data,resolution:o})}function loadPalettePng(t){assert(t.palette);const e=t.width*t.height,r=new Uint8Array(e*t.palette[0].length),n=8/t.depth,o=t.depth<8?n:1,i=Number.parseInt("1".repeat(t.depth),2);let s=0;for(let a=0;a<e;a++){const e=Math.floor(a/o);let h=t.data[e];t.depth<8&&(h=h>>>t.depth*(n-1-a%n)&i);const l=t.palette[h];for(const t of l)r[s++]=t}const a=getResolution(t);return new Image(t.width,t.height,{data:r,colorModel:4===t.palette[0].length?"RGBA":"RGB",resolution:a})}function decodeBinary(t){const e=t.width*t.height,r=new Uint8Array(e),n=t.data,o=t.width%8,i=Math.ceil(t.width/8);let s=0;for(let t=0;t<n.length&&s<e;t++){const a=n[t],h=t%i===0?8-o:0;for(let t=7;t>=h&&s<e;t--){const e=a>>t&1;r[s++]=255*e}}return r}function getResolution(t){return t.resolution?1===t.resolution.unit?{x:t.resolution.x,y:t.resolution.y,unit:"meter"}:{x:t.resolution.x,y:t.resolution.y,unit:"unknown"}:void 0}function decodeTiff(t){return getImageFromIFD(decodeTIFF(t,{pages:[0]})[0])}function getImageFromIFD(t){if(t.data instanceof Float32Array||t.data instanceof Float64Array)throw new Error("Float TIFF data is not supported.");const e=getTiffResolution(t);if(3===t.type){const r=2===t.samplesPerPixel,n=new Uint16Array((r?4:3)*t.width*t.height),o=t.palette;let i=0;if(r)for(let e=0;e<t.data.length;e+=t.samplesPerPixel){const r=o[t.data[e]];n[i++]=r[0],n[i++]=r[1],n[i++]=r[2],n[i++]=Math.round(t.data[e+1]/2**t.bitsPerSample*65535)}else for(let e=0;e<t.data.length;e+=t.samplesPerPixel){const r=o[t.data[e]];n[i++]=r[0],n[i++]=r[1],n[i++]=r[2]}return new Image(t.width,t.height,{data:n,colorModel:r?"RGBA":"RGB",bitDepth:16,meta:getMetadata(t),resolution:e})}return 1===t.type||0===t.type?1!==t.bitsPerSample?new Image(t.width,t.height,{data:t.data,bitDepth:t.bitsPerSample,colorModel:t.alpha?"GREYA":"GREY",meta:getMetadata(t),resolution:e}):new Image(t.width,t.height,{data:t.data.map(t=>255*t),bitDepth:8,colorModel:"GREY",meta:getMetadata(t),resolution:e}):new Image(t.width,t.height,{data:t.data,bitDepth:t.bitsPerSample,colorModel:t.alpha?"RGBA":"RGB",meta:getMetadata(t),resolution:e})}function getTiffResolution(t){if(t.xResolution&&t.yResolution)switch(t.resolutionUnit){case 1:return{x:t.xResolution,y:t.yResolution,unit:"unknown"};case 3:return{x:t.xResolution,y:t.yResolution,unit:"centimeter"};default:return{x:t.xResolution,y:t.yResolution,unit:"inch"}}}function decode(t){const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),r=imageType$1(e);return M$2(r).with({mime:"image/png"},()=>decodePng(e)).with({mime:"image/jpeg"},()=>decodeJpeg(e)).with({mime:"image/tiff"},()=>decodeTiff(e)).with({mime:"image/bmp"},()=>decodeBmp(e)).otherwise(()=>{throw new RangeError(`invalid data format: ${r?.mime}`)})}async function read(t){const e=getNodeApiOrThrow("read");return decode(await e.fs.promises.readFile(t))}function readSync(t){return decode(getNodeApiOrThrow("readSync").fs.readFileSync(t))}function readCanvas(t){const e=t.getContext("2d");assert(e);const r=e.getImageData(0,0,t.width,t.height);return new Image(r.width,r.height,{data:new Uint8Array(r.data.buffer,r.data.byteOffset,r.data.byteLength),colorModel:"RGBA"})}function readImg(t){const e=document.createElement("canvas");e.width=t.width,e.height=t.height;const r=e.getContext("2d");return assert(r),r.drawImage(t,0,0),readCanvas(e)}async function fetchURL(t){const e=await fetch(t),r=await e.arrayBuffer();return decode(new DataView(r))}function getConvexityDefects(t,e,r={}){const{depthThreshold:n=0}=r,o=[];if(0===e.length)throw new RangeError("No hull points were defined for convexity defects detection.");if(0===t.length)throw new RangeError("No border points were defined for convexity defects detection.");const i=e.at(-1);let s=t.findIndex(t=>limitReached(t,i));if(-1===s)throw new RangeError("Could not find a border point matching the convex hull endpoint.");for(let r=e.length-1;r>=0;r--){const i=e.at(r),a=e.at(r-1);if(!i||!a)continue;const h=a.column-i.column,l=a.row-i.row,c=h*h+l*l;let u=0,f=-1;const d=s;let g=!1;for(let e=0;e<t.length;e++){const r=(d+e)%t.length,n=t[r];if(limitReached(n,a)){s=(r+1)%t.length,g=!0;break}const o=n.column-i.column,m=n.row-i.row,p=Math.max(0,Math.min(1,(o*h+m*l)/c)),w=i.column+p*h,y=i.row+p*l,b=Math.hypot(n.column-w,n.row-y);b>u&&(u=b,f=r)}if(!g)throw new RangeError("Could not reach the next hull point while scanning border points; hull and border may be inconsistent.");if(u>n){const e=t[f];e&&o.push(e)}}return o}function limitReached(t,e){return Math.abs(t.column-e.column)<=1&&Math.abs(t.row-e.row)<=1}function hsvToRgb(t){const e=t[0],r=t[1]/255,n=t[2]/255,o=r*n,i=o*(1-Math.abs(e/60%2-1)),s=n-o;let a,h,l;e>=0&&e<60?(a=o,h=i,l=0):e>=60&&e<120?(a=i,h=o,l=0):e>=120&&e<180?(a=0,h=o,l=i):e>=180&&e<240?(a=0,h=i,l=o):e>=240&&e<300?(a=i,h=0,l=o):(a=o,h=0,l=i);const c=new Uint8Array(3);return c[0]=255*(a+s),c[1]=255*(h+s),c[2]=255*(l+s),c}function rgbToNumber(t){return 4278190080+(t[2]<<16)+(t[1]<<8)+t[0]}function getBinaryMap(t){const{nbNegative:e,nbPositive:r,whiteHue:n=120,blackHue:o=0,roiKind:i="bw"}=t,s=new Uint32Array(e+r+1);if("bw"===i||"black"===i)for(let t=-e;t<0;t++){const r=[o,255,255];s[t+e]=rgbToNumber(hsvToRgb(r))}if("bw"===i||"white"===i)for(let t=1;t<=r;t++){const r=[n,255,255];s[t+e]=rgbToNumber(hsvToRgb(r))}return s}function getRainbowMap(t){const{nbNegative:e,nbPositive:r,roiKind:n="bw"}=t,o=new Uint32Array(e+r+1),i=M$2(n).with("bw",()=>360/(e+r)).with("black",()=>360/e).with("white",()=>360/r).exhaustive();let s=0;if("bw"===n||"black"===n)for(let t=-e;t<0;t++){const r=[s,255,255];o[t+e]=rgbToNumber(hsvToRgb(r)),s+=i}if("bw"===n||"white"===n)for(let t=1;t<=r;t++){const r=[s,255,255];o[t+e]=rgbToNumber(hsvToRgb(r)),s+=i}return o}function getSaturationMap(t){const{nbNegative:e,nbPositive:r,roiKind:n="bw",whiteHue:o=0,blackHue:i=240}=t,s=new Uint32Array(e+r+1),a=192/e,h=192/r;let l=0;if("bw"===n||"black"===n)for(let t=-e;t<0;t++){const r=[i,255-l++*a,255];s[t+e]=rgbToNumber(hsvToRgb(r))}if(l=0,"bw"===n||"white"===n)for(let t=1;t<=r;t++){const r=[o,255-l++*h,255];s[t+e]=rgbToNumber(hsvToRgb(r))}return s}function getColorMap(t){const{mode:e="binary"}=t;return t={roiKind:"bw",...t},M$2(e).with("binary",()=>getBinaryMap(t)).with("saturation",()=>getSaturationMap(t)).with("rainbow",()=>getRainbowMap(t)).exhaustive()}const RoisColorMode={BINARY:"binary",SATURATION:"saturation",RAINBOW:"rainbow"};function colorRois(t,e={}){const{roiKind:r="bw",mode:n="binary"}=e,o=t.getMap(),i=new Image(o.width,o.height,{colorModel:"RGBA"}),s=getColorMap({roiKind:r,mode:n,nbNegative:o.nbNegative,nbPositive:o.nbPositive}),a=new Uint32Array(i.getRawImage().data.buffer);for(let t=0;t<i.size;t++)a[t]=s[o.data[t]+o.nbNegative];return i}function getBorderPoints(t,e={}){return t.getMask().getBorderPoints(e)}function getExternalContour(t,e){return t.getMask().getExternalContour(e)}function getMask(t,e={}){const{solidFill:r=!1}=e,n=new Mask(t.width,t.height,{origin:t.origin});for(let e=0;e<t.height;e++)for(let r=0;r<t.width;r++)t.getMapValue(t.origin.column+r,t.origin.row+e)===t.id?n.setBit(r,e,1):n.setBit(r,e,0);return r&&n.solidFill({out:n}),n}function getEllipse(t){const e=t.centroid.column,r=t.centroid.row,n=t.relativePoints.map(t=>t.column-e),o=t.relativePoints.map(t=>t.row-r),i=xVariance(n,{unbiased:!1}),s=xVariance(o,{unbiased:!1}),a=xyCovariance({x:n,y:o},{unbiased:!1}),h=new EigenvalueDecomposition([[i,a],[a,s]]),l=h.realEigenvalues,c=h.eigenvectorMatrix;assert(l[0]<=l[1]);let u=Math.sqrt(l[1]),f=Math.sqrt(l[0]);const d=c.getColumn(1),g=c.getColumn(0);let m={column:e+u*d[0],row:r+u*d[1]},p={column:e-u*d[0],row:r-u*d[1]},w={column:e+f*g[0],row:r+f*g[1]},y={column:e-f*g[0],row:r-f*g[1]},b=Math.hypot(m.column-p.column,m.row-p.row),x=Math.hypot(w.column-p.column,w.row-y.row),M=x/2*b/2*Math.PI;if(M!==t.surface){const n=Math.sqrt(t.surface/M);u*=n,f*=n,m={column:e+u*d[0],row:r+u*d[1]},p={column:e-u*d[0],row:r-u*d[1]},w={column:e+f*g[0],row:r+f*g[1]},y={column:e-f*g[0],row:r-f*g[1]},b*=n,x*=n,M*=n**2}return{center:{column:e,row:r},majorAxis:{points:[m,p],length:b,angle:toDegrees(getAngle(m,p))},minorAxis:{points:[w,y],length:x,angle:toDegrees(getAngle(w,y))},surface:M}}class Roi{map;id;origin;width;height;surface;#a;constructor(t,e,r,n,o,i){this.map=t,this.id=e,this.origin=o,this.width=r,this.height=n,this.surface=i,this.#a={}}getMapValue(t,e){return this.map.data[this.map.width*e+t]}getRatio(){return this.width/this.height}getMask(t){return getMask(this,t)}get ped(){return this.perimeter/Math.PI}getBorderPoints(t){return getBorderPoints(this,t)}getExternalContour(t){return getExternalContour(this,t)}get internalIDs(){return this.#h("internalIDs",()=>{const t=[this.id],e=this.map,r=e.data;if(this.height>2)for(let n=0;n<this.width;n++){const o=this.#l(0,n);if(t.includes(r[o])){const n=r[o+e.width];t.includes(n)||this.boxIDs.includes(n)||t.push(n)}}const n=new Array(4);for(let o=1;o<this.width-1;o++)for(let i=1;i<this.height-1;i++){const s=this.#l(i,o);if(t.includes(r[s])){n[0]=r[s-1],n[1]=r[s+1],n[2]=r[s-e.width],n[3]=r[s+e.width];for(let e=0;e<4;e++){const r=n[e];t.includes(r)||this.boxIDs.includes(r)||t.push(r)}}}return t})}get externalBorders(){return this.#h("externalBorders",()=>{const t=this.borders,e=[],r=[],n=this.internalIDs;for(const o of t)if(!n.includes(o.connectedID)){const t={connectedID:o.connectedID,length:o.length};r.push(t.connectedID),e.push(t)}return e})}get perimeterInfo(){return this.#h("perimeterInfo",()=>{const t=this.map,e=t.data;let r=0,n=0,o=0,i=0;const s=new Set(this.externalBorders.map(t=>t.connectedID));for(let a=0;a<this.width;a++)for(let h=0;h<this.height;h++){const l=this.#l(h,a);if(e[l]===this.id){let c=0;switch((0===a||s.has(e[l-1]))&&c++,(a===t.width-1||s.has(e[l+1]))&&c++,(0===h||s.has(e[l-t.width]))&&c++,(h===t.height-1||s.has(e[l+t.width]))&&c++,c){case 1:r++;break;case 2:n++;break;case 3:o++;break;case 4:i++}}}return{one:r,two:n,three:o,four:i}})}get perimeter(){const t=this.perimeterInfo,e=2-Math.sqrt(2);return t.one+2*t.two+3*t.three+4*t.four-e*(t.two+2*t.three+t.four)}get relativePoints(){return this.#h("relativePoints",()=>Array.from(this.points(!1)))}get absolutePoints(){return this.#h("absolutePoints",()=>Array.from(this.points(!0)))}get boxIDs(){return this.#h("boxIDs",()=>{const t=new Set,e=this.map,r=e.data;for(const n of[0,this.height-1])for(let o=0;o<this.width;o++){const i=this.#l(n,o);if(o-this.origin.column>0&&r[i]===this.id&&r[i-1]!==this.id){const e=r[i-1];t.add(e)}if(e.width-o-this.origin.column>1&&r[i]===this.id&&r[i+1]!==this.id){const e=r[i+1];t.add(e)}}for(const n of[0,this.width-1])for(let o=0;o<this.height;o++){const i=this.#l(o,n);if(o-this.origin.row>0&&r[i]===this.id&&r[i-e.width]!==this.id){const n=r[i-e.width];t.add(n)}if(e.height-o-this.origin.row>1&&r[i]===this.id&&r[i+e.width]!==this.id){const n=r[i+e.width];t.add(n)}}return Array.from(t)})}get eqpc(){return 2*Math.sqrt(this.surface/Math.PI)}get ellipse(){return this.#h("ellipse",()=>getEllipse(this))}get holesInfo(){return this.#h("holesInfo",()=>{let t=0;const e=this.map.data;for(let r=1;r<this.width-1;r++)for(let n=1;n<this.height-1;n++){const o=this.#l(n,r);this.internalIDs.includes(e[o])&&e[o]!==this.id&&t++}return{number:this.internalIDs.length-1,surface:t}})}get borders(){return this.#h("borders",()=>{const t=this.map,e=t.data,r=new Set,n=new Map,o=new Set,i=[1,0,-1,0],s=[0,1,0,-1];for(let a=this.origin.column;a<=this.origin.column+this.width;a++)for(let h=this.origin.row;h<=this.origin.row+this.height;h++){if(e[a+h*t.width]===this.id)for(let l=0;l<4;l++){const c=a+i[l],u=h+s[l];if(c>=0&&u>=0&&c<t.width&&u<t.height){const i=c+u*t.width;if(e[i]!==this.id&&!o.has(i)){o.add(i),r.add(e[i]);let t=n.get(e[i]);t?n.set(e[i],++t):n.set(e[i],1)}}}}return Array.from(r).map(t=>({connectedID:t,length:n.get(t)}))})}get fillRatio(){return this.surface/(this.surface+this.holesInfo.surface)}get sphericity(){return 2*Math.sqrt(this.surface*Math.PI)/this.perimeter}get filledSurface(){return this.surface+this.holesInfo.surface}get solidity(){return this.surface/this.convexHull.surface}get convexHull(){return this.#h("convexHull",()=>getConvexHull(this.getMask()))}get mbr(){return this.#h("mbr",()=>getMbr(this.getMask()))}get roundness(){return 4*this.surface/(Math.PI*this.feret.maxDiameter.length**2)}get feret(){return this.#h("feret",()=>getFeret(this.getMask()))}toJSON(){return{id:this.id,origin:this.origin,height:this.height,width:this.width,surface:this.surface,eqpc:this.eqpc,ped:this.ped,feret:this.feret,fillRatio:this.fillRatio,sphericity:this.sphericity,roundness:this.roundness,solidity:this.solidity,perimeter:this.perimeter,convexHull:this.convexHull,mbr:this.mbr,filledSurface:this.filledSurface,centroid:this.centroid}}get centroid(){return this.#h("centroid",()=>{const t=this.map.data;let e=0,r=0;for(let n=0;n<this.width;n++)for(let o=0;o<this.height;o++){t[this.#l(o,n)]===this.id&&(e+=n,r+=o)}return{column:e/this.surface+this.origin.column,row:r/this.surface+this.origin.row}})}#h(t,e){if(void 0===this.#a[t]){const r=e();return this.#a[t]=r,r}return this.#a[t]}#l(t,e){const r=this.map;return(t+this.origin.row)*r.width+e+this.origin.column}*points(t){for(let e=0;e<this.height;e++)for(let r=0;r<this.width;r++){const n=(e+this.origin.row)*this.map.width+r+this.origin.column;this.map.data[n]===this.id&&(t?yield{column:this.origin.column+r,row:this.origin.row+e}:yield{column:r,row:e})}}}function computeRois(t){const e=t.getMap(),r=new Array(e.nbPositive),n=new Array(e.nbNegative);for(let t=0;t<e.nbPositive;t++)r[t]={minRow:e.height,minColumn:e.width,maxRow:-1,maxColumn:-1,surface:0,id:t+1};for(let t=0;t<e.nbNegative;t++)n[t]={borderLengths:[],borderIDs:[],minRow:e.height,minColumn:e.width,maxRow:-1,maxColumn:-1,surface:0,id:-t-1};for(let o=0;o<e.height;o++)for(let i=0;i<e.width;i++){const e=t.getMapValue(i,o);if(0===e)continue;let s;s=e<0?n[-e-1]:r[e-1],s.surface++,o<s.minRow&&(s.minRow=o),o>s.maxRow&&(s.maxRow=o),i<s.minColumn&&(s.minColumn=i),i>s.maxColumn&&(s.maxColumn=i)}t.whiteRois=new Array(e.nbPositive),t.blackRois=new Array(e.nbNegative);for(let r=0;r<e.nbNegative;r++){const o=n[r].maxColumn-n[r].minColumn+1,i=n[r].maxRow-n[r].minRow+1,s={row:n[r].minRow,column:n[r].minColumn},a=n[r].id,h=n[r].surface,l=new Roi(e,a,o,i,s,h);t.blackRois[r]=l}for(let n=0;n<e.nbPositive;n++){const o=r[n].maxColumn-r[n].minColumn+1,i=r[n].maxRow-r[n].minRow+1,s={row:r[n].minRow,column:r[n].minColumn},a=r[n].id,h=r[n].surface,l=new Roi(e,a,o,i,s,h);t.whiteRois[n]=l}}const RoiKind={BLACK:"black",WHITE:"white",BW:"bw"};function getRois(t,e={}){const{minSurface:r=0,maxSurface:n=Number.MAX_SAFE_INTEGER,kind:o="white"}=e;0===t.blackRois.length&&0===t.whiteRois.length&&computeRois(t);return M$2(o).with("black",()=>t.blackRois).with("white",()=>t.whiteRois).with("bw",()=>[...t.whiteRois,...t.blackRois]).exhaustive().filter(t=>t.surface>=r&&t.surface<=n)}class RoiMapManager{map;whiteRois;blackRois;constructor(t){this.map=t,this.whiteRois=[],this.blackRois=[]}getMap(){return this.map}getMapValue(t,e){return this.map.data[this.map.width*e+t]}getMapMatrix(){return Matrix.from1DArray(this.map.height,this.map.width,this.map.data).to2DArray()}getRois(t={}){return getRois(this,t)}getRoiById(t){const e=this.getRois({kind:"bw"}).find(e=>e.id===t);if(!e)throw new Error(`invalid ID: ${t}`);return e}static fromMask(t,e={}){return fromMask(t,e)}}function fromMask(t,e={}){const{allowCorners:r=!1}=e,n=65535,o=-2147483647,i=new Int32Array(t.size);let s=0,a=0;const h=new Uint16Array(65536),l=new Uint16Array(65536);for(let e=0;e<t.width;e++)for(let r=0;r<t.height;r++)0===i[r*t.width+e]&&c(e,r);function c(e,c){let u=0,f=0;const d=t.getBit(e,c),g=d?++s:--a;for(assert(s<=2147483647&&a>=o,"too many regions of interest"),h[0]=e,l[0]=c;u<=f;){const e=h[u&n],s=l[u&n];i[s*t.width+e]=g,e>0&&0===i[s*t.width+e-1]&&t.getBit(e-1,s)===d&&(f++,h[f&n]=e-1,l[f&n]=s,i[s*t.width+e-1]=o),s>0&&0===i[(s-1)*t.width+e]&&t.getBit(e,s-1)===d&&(f++,h[f&n]=e,l[f&n]=s-1,i[(s-1)*t.width+e]=o),e<t.width-1&&0===i[s*t.width+e+1]&&t.getBit(e+1,s)===d&&(f++,h[f&n]=e+1,l[f&n]=s,i[s*t.width+e+1]=o),s<t.height-1&&0===i[(s+1)*t.width+e]&&t.getBit(e,s+1)===d&&(f++,h[f&n]=e,l[f&n]=s+1,i[(s+1)*t.width+e]=o),r&&(e>0&&s>0&&0===i[(s-1)*t.width+e-1]&&t.getBit(e-1,s-1)===d&&(f++,h[f&n]=e-1,l[f&n]=s-1,i[(s-1)*t.width+e-1]=o),e<t.width-1&&s>0&&0===i[(s-1)*t.width+e+1]&&t.getBit(e+1,s-1)===d&&(f++,h[f&n]=e+1,l[f&n]=s-1,i[(s-1)*t.width+e+1]=o),e>0&&s<t.height-1&&0===i[(s+1)*t.width+e-1]&&t.getBit(e-1,s+1)===d&&(f++,h[f&n]=e-1,l[f&n]=s+1,i[(s+1)*t.width+e-1]=o),e<t.width-1&&s<t.height-1&&0===i[(s+1)*t.width+e+1]&&t.getBit(e+1,s+1)===d&&(f++,h[f&n]=e+1,l[f&n]=s+1,i[(s+1)*t.width+e+1]=o)),u++,assert(f-u<=n,"fromMask can not finish, the array to manage internal data is not big enough.You could improve mask by changing MAX_ARRAY")}}return new RoiMapManager({width:t.width,height:t.height,data:i,nbNegative:Math.abs(a),nbPositive:s})}function commonjsRequire(t){throw new Error('Could not dynamically require "'+t+'". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.')}var priorityQueue={exports:{}};!function(t,e){t.exports=function t(e,r,n){function o(s,a){if(!r[s]){if(!e[s]){var h="function"==typeof commonjsRequire&&commonjsRequire;if(!a&&h)return h(s,!0);if(i)return i(s,!0);var l=new Error("Cannot find module '"+s+"'");throw l.code="MODULE_NOT_FOUND",l}var c=r[s]={exports:{}};e[s][0].call(c.exports,function(t){var r=e[s][1][t];return o(r||t)},c,c.exports,t,e,r,n)}return r[s].exports}for(var i="function"==typeof commonjsRequire&&commonjsRequire,s=0;s<n.length;s++)o(n[s]);return o}({1:[function(t,e,r){var n,o,i,s,a,h=function(t,e){for(var r in e)l.call(e,r)&&(t[r]=e[r]);function n(){this.constructor=t}return n.prototype=e.prototype,t.prototype=new n,t.__super__=e.prototype,t},l={}.hasOwnProperty;n=t("./PriorityQueue/AbstractPriorityQueue"),o=t("./PriorityQueue/ArrayStrategy"),s=t("./PriorityQueue/BinaryHeapStrategy"),i=t("./PriorityQueue/BHeapStrategy"),a=function(t){function e(t){t||(t={}),t.strategy||(t.strategy=s),t.comparator||(t.comparator=function(t,e){return(t||0)-(e||0)}),e.__super__.constructor.call(this,t)}return h(e,t),e}(n),a.ArrayStrategy=o,a.BinaryHeapStrategy=s,a.BHeapStrategy=i,e.exports=a},{"./PriorityQueue/AbstractPriorityQueue":2,"./PriorityQueue/ArrayStrategy":3,"./PriorityQueue/BHeapStrategy":4,"./PriorityQueue/BinaryHeapStrategy":5}],2:[function(t,e,r){e.exports=function(){function t(t){var e;if(null==(null!=t?t.strategy:void 0))throw"Must pass options.strategy, a strategy";if(null==(null!=t?t.comparator:void 0))throw"Must pass options.comparator, a comparator";this.priv=new t.strategy(t),this.length=(null!=t&&null!=(e=t.initialValues)?e.length:void 0)||0}return t.prototype.queue=function(t){this.length++,this.priv.queue(t)},t.prototype.dequeue=function(t){if(!this.length)throw"Empty queue";return this.length--,this.priv.dequeue()},t.prototype.peek=function(t){if(!this.length)throw"Empty queue";return this.priv.peek()},t.prototype.clear=function(){return this.length=0,this.priv.clear()},t}()},{}],3:[function(t,e,r){var n;n=function(t,e,r){var n,o,i;for(o=0,n=t.length;o<n;)r(t[i=o+n>>>1],e)>=0?o=i+1:n=i;return o},e.exports=function(){function t(t){var e;this.options=t,this.comparator=this.options.comparator,this.data=(null!=(e=this.options.initialValues)?e.slice(0):void 0)||[],this.data.sort(this.comparator).reverse()}return t.prototype.queue=function(t){var e;e=n(this.data,t,this.comparator),this.data.splice(e,0,t)},t.prototype.dequeue=function(){return this.data.pop()},t.prototype.peek=function(){return this.data[this.data.length-1]},t.prototype.clear=function(){this.data.length=0},t}()},{}],4:[function(t,e,r){e.exports=function(){function t(t){var e,r,n,o,i,s,a,h;for(this.comparator=(null!=t?t.comparator:void 0)||function(t,e){return t-e},this.pageSize=(null!=t?t.pageSize:void 0)||512,this.length=0,a=0;1<<a<this.pageSize;)a+=1;if(1<<a!==this.pageSize)throw"pageSize must be a power of two";for(this._shift=a,this._emptyMemoryPageTemplate=e=[],r=0,i=this.pageSize;0<=i?r<i:r>i;0<=i?++r:--r)e.push(null);if(this._memory=[],this._mask=this.pageSize-1,t.initialValues)for(n=0,o=(s=t.initialValues).length;n<o;n++)h=s[n],this.queue(h)}return t.prototype.queue=function(t){this.length+=1,this._write(this.length,t),this._bubbleUp(this.length,t)},t.prototype.dequeue=function(){var t,e;return t=this._read(1),e=this._read(this.length),this.length-=1,this.length>0&&(this._write(1,e),this._bubbleDown(1,e)),t},t.prototype.peek=function(){return this._read(1)},t.prototype.clear=function(){this.length=0,this._memory.length=0},t.prototype._write=function(t,e){var r;for(r=t>>this._shift;r>=this._memory.length;)this._memory.push(this._emptyMemoryPageTemplate.slice(0));return this._memory[r][t&this._mask]=e},t.prototype._read=function(t){return this._memory[t>>this._shift][t&this._mask]},t.prototype._bubbleUp=function(t,e){var r,n,o,i;for(r=this.comparator;t>1&&(n=t&this._mask,t<this.pageSize||n>3?o=t&~this._mask|n>>1:n<2?(o=t-this.pageSize>>this._shift,o+=o&~(this._mask>>1),o|=this.pageSize>>1):o=t-2,!(r(i=this._read(o),e)<0));)this._write(o,e),this._write(t,i),t=o},t.prototype._bubbleDown=function(t,e){var r,n,o,i,s;for(s=this.comparator;t<this.length;)if(t>this._mask&&!(t&this._mask-1)?r=n=t+2:t&this.pageSize>>1?(r=(t&~this._mask)>>1,n=1+(r=1+(r|=t&this._mask>>1)<<this._shift)):n=(r=t+(t&this._mask))+1,r!==n&&n<=this.length)if(o=this._read(r),i=this._read(n),s(o,e)<0&&s(o,i)<=0)this._write(r,e),this._write(t,o),t=r;else{if(!(s(i,e)<0))break;this._write(n,e),this._write(t,i),t=n}else{if(!(r<=this.length))break;if(!(s(o=this._read(r),e)<0))break;this._write(r,e),this._write(t,o),t=r}},t}()},{}],5:[function(t,e,r){e.exports=function(){function t(t){var e;this.comparator=(null!=t?t.comparator:void 0)||function(t,e){return t-e},this.length=0,this.data=(null!=(e=t.initialValues)?e.slice(0):void 0)||[],this._heapify()}return t.prototype._heapify=function(){var t,e,r;if(this.data.length>0)for(t=e=1,r=this.data.length;1<=r?e<r:e>r;t=1<=r?++e:--e)this._bubbleUp(t)},t.prototype.queue=function(t){this.data.push(t),this._bubbleUp(this.data.length-1)},t.prototype.dequeue=function(){var t,e;return e=this.data[0],t=this.data.pop(),this.data.length>0&&(this.data[0]=t,this._bubbleDown(0)),e},t.prototype.peek=function(){return this.data[0]},t.prototype.clear=function(){this.length=0,this.data.length=0},t.prototype._bubbleUp=function(t){for(var e,r;t>0&&(e=t-1>>>1,this.comparator(this.data[t],this.data[e])<0);)r=this.data[e],this.data[e]=this.data[t],this.data[t]=r,t=e},t.prototype._bubbleDown=function(t){var e,r,n,o,i;for(e=this.data.length-1;o=1+(r=1+(t<<1)),n=t,r<=e&&this.comparator(this.data[r],this.data[n])<0&&(n=r),o<=e&&this.comparator(this.data[o],this.data[n])<0&&(n=o),n!==t;)i=this.data[n],this.data[n]=this.data[t],this.data[t]=i,t=n},t}()},{}]},{},[1])(1)}(priorityQueue);var priorityQueueExports=priorityQueue.exports,PriorityQueue=getDefaultExportFromCjs(priorityQueueExports);function waterShed(t,e){let{points:r}=e;const{mask:n,threshold:o=1}=e,i=t;checkProcessable$1(t,{bitDepth:[8,16],components:1});const s=o*t.maxValue;r||(r=getExtrema(t,{kind:"minimum",mask:n}));const a=new Int32Array(i.size),h=i.width,l=i.height,c=new PriorityQueue({comparator:(t,e)=>t.intensity-e.intensity,strategy:PriorityQueue.BinaryHeapStrategy});for(let t=0;t<r.length;t++){const e=r[t].column+r[t].row*h;a[e]=-t-1;const n=i.getValueByIndex(e,0);n<=s&&c.queue({column:r[t].column,row:r[t].row,intensity:n})}const u=[1,0,-1,0,1,1,-1,-1],f=[0,1,0,-1,1,-1,1,-1];for(;c.length>0;){const t=c.dequeue(),e=t.column+t.row*h;for(let r=0;r<4;r++){const o=t.column+u[r],d=t.row+f[r];if(o>=0&&d>=0&&o<h&&d<l){const l=o+d*h;if(!n||0===n.getBitByIndex(l)){const n=i.getValueByIndex(l,0);n<=s&&0===a[l]&&(a[l]=a[e],c.queue({column:t.column+u[r],row:t.row+f[r],intensity:n}))}}}}const d=r.length;return new RoiMapManager({data:a,nbPositive:0,nbNegative:d,width:t.width,height:t.height})}function checkProcessable(t,e={}){const{sameDimensions:r=!1,alpha:n}=e;let{bitDepth:o}=e;if(r){const e=t.getImage(0).width,r=t.getImage(0).height;for(let n=1;n<t.size;n++){const o=t.getImage(n);if(o.width!==e||o.height!==r)throw new RangeError("images must all have same dimensions to apply this algorithm")}}if(void 0!==n&&n!==t.alpha)throw new RangeError(`stack images ${n?"should":"should not"} have an alpha channel to apply this algorithm`);if(o&&(Array.isArray(o)||(o=[o]),!o.includes(t.bitDepth)))throw new RangeError(`image bitDepth must be ${format(o)} to apply this algorithm`)}function histogram(t,e){checkProcessable(t,{bitDepth:[8,16]});const{slots:r=2**t.bitDepth,channel:n=0}=e,o=new Uint32Array(r);for(let e=0;e<t.size;e++){const i=t.getImage(e).histogram({channel:n,slots:r});for(const[t,e]of i.entries())o[t]+=e}return o}function maxImage(t){checkProcessable(t,{sameDimensions:!0});const e=Image.createFrom(t.getImage(0)),r=e.channels;for(let n=0;n<t.size;n++)for(let o=0;o<e.size;o++)for(let i=0;i<r;i++)e.setValueByIndex(o,i,Math.max(e.getValueByIndex(o,i),t.getValueByIndex(n,o,i)));return e}function meanImage(t){checkProcessable(t,{sameDimensions:!0,bitDepth:[8,16]});const e=t.getImage(0),r=e.size*t.channels,n=new Uint32Array(r).fill(0);for(let r=0;r<t.size;r++)for(let o=0;o<e.size;o++)for(let e=0;e<t.channels;e++)n[o*t.channels+e]+=t.getValueByIndex(r,o,e);const o=Image.createFrom(e);for(let r=0;r<e.size;r++)for(let e=0;e<t.channels;e++){const i=r*t.channels+e;o.setValueByIndex(r,e,n[i]/t.size)}return o}function medianImage(t){checkProcessable(t,{sameDimensions:!0,bitDepth:[8,16]});const e=t.getImage(0),r=Image.createFrom(e);for(let n=0;n<e.size;n++)for(let e=0;e<t.channels;e++){const o=new Array(t.size);for(let r=0;r<t.size;r++)o[r]=t.getValueByIndex(r,n,e);r.setValueByIndex(n,e,quickMedian(o))}return r}function decodeStackFromApng(t){const e=decodeApng(t),r=[];let n;switch(e.channels){case 1:n=e.palette?3===e.palette[0].length?"RGB":"RGBA":"GREY";break;case 2:n="GREYA";break;case 3:n="RGB";break;default:n="RGBA"}if(e.palette)for(const t of e.frames)r.push(new Image(e.width,e.height,{data:convertIndexedData(t.data,e.palette),colorModel:n}));else for(const t of e.frames)r.push(new Image(e.width,e.height,{data:t.data,colorModel:n}));return new Stack(r)}function convertIndexedData(t,e){const r=new Uint8Array(t.length*e[0].length);for(let n=0;n<t.length;n++)for(let o=0;o<e[0].length;o++)r[n*e[0].length+o]=e[t[n]][o];return r}function decodeStackFromTiff(t){const e=decodeTIFF(t),r=[];for(const t of e)r.push(getImageFromIFD(t));return new Stack(r)}function decodeStack(t){const e=new Uint8Array(t.buffer,t.byteOffset,t.byteLength),r=imageType$1(e);return M$2(r).with({mime:"image/tiff"},()=>decodeStackFromTiff(e)).with({mime:"image/png"},()=>decodeStackFromApng(e)).otherwise(()=>{throw new RangeError(`invalid data format: ${r?.mime}`)})}function minImage(t){checkProcessable(t,{sameDimensions:!0});const e=Image.createFrom(t.getImage(0));e.fill(e.maxValue);const r=e.channels;for(let n=0;n<t.size;n++)for(let o=0;o<e.size;o++)for(let i=0;i<r;i++)e.setValueByIndex(o,i,Math.min(e.getValueByIndex(o,i),t.getValueByIndex(n,o,i)));return e}function sum(t){if(checkProcessable(t,{sameDimensions:!0,bitDepth:8}),t.size>257)throw new Error("Maximal valid stack size is 257");const e=t.getImage(0),r=e.size*t.channels,n=new Uint16Array(r).fill(0);for(let r=0;r<t.size;r++)for(let o=0;o<e.size;o++)for(let e=0;e<t.channels;e++)n[o*t.channels+e]+=t.getValueByIndex(r,o,e);return new Image(e.width,e.height,{data:n,colorModel:t.colorModel,bitDepth:16})}function checkImagesValid(t){const e=t[0].colorModel,r=t[0].bitDepth;for(const n of t)if(n.colorModel!==e||n.bitDepth!==r)throw new RangeError("images must all have the same bit depth and color model")}function verifySameDimensions(t){const e=t[0].width,r=t[0].height;for(const n of t)if(n.width!==e||n.height!==r)return!1;return!0}class Stack{images;size;alpha;colorModel;bitDepth;sameDimensions;channels;constructor(t){checkImagesValid(t),this.images=t,this.size=t.length,this.alpha=t[0].alpha,this.colorModel=t[0].colorModel,this.channels=t[0].channels,this.bitDepth=t[0].bitDepth,this.sameDimensions=verifySameDimensions(t)}*[Symbol.iterator](){for(const t of this.images)yield t}clone(){return new Stack(this.images.map(t=>t.clone()))}getImages(){return this.images}getImage(t){return this.images[t]}getValue(t,e,r,n){return this.images[t].getValue(e,r,n)}getValueByIndex(t,e,r){return this.images[t].getValueByIndex(e,r)}minImage(){return minImage(this)}maxImage(){return maxImage(this)}medianImage(){return medianImage(this)}meanImage(){return meanImage(this)}sum(){return sum(this)}histogram(t={}){return histogram(this,t)}map(t){return new Stack(this.images.map(t))}filter(t){return new Stack(this.images.filter(t))}}const channelLabels={GREY:["Grey"],GREYA:["Grey","Alpha"],RGB:["Red","Green","Blue"],RGBA:["Red","Green","Blue","Alpha"],BINARY:["Mask"]};exports.BorderType=BorderType,exports.DerivativeFilter=DerivativeFilter,exports.GreyAlgorithm=GreyAlgorithm,exports.Image=Image,exports.ImageColorModel=ImageColorModel,exports.ImageCoordinates=ImageCoordinates,exports.ImageFormat=ImageFormat,exports.InterpolationType=InterpolationType,exports.Mask=Mask,exports.Montage=Montage,exports.MontageDisposition=MontageDisposition,exports.Roi=Roi,exports.RoiKind=RoiKind,exports.RoiMapManager=RoiMapManager,exports.RoisColorMode=RoisColorMode,exports.Stack=Stack,exports.ThresholdAlgorithm=ThresholdAlgorithm,exports.add=add,exports.alignMinDifference=alignMinDifference,exports.and=and,exports.blur=blur,exports.bottomHat=bottomHat,exports.bruteForceOneMatch=bruteForceOneMatch,exports.cannyEdgeDetector=cannyEdgeDetector,exports.channelLabels=channelLabels,exports.clearBorder=clearBorder,exports.close=close,exports.colorModels=colorModels,exports.colorRois=colorRois,exports.computeConvolutionValue=computeConvolutionValue,exports.computeMse=computeMse,exports.computePsnr=computePsnr,exports.computeRmse=computeRmse,exports.computeRois=computeRois,exports.computeThreshold=computeThreshold,exports.convertBinaryToGrey=convertBinaryToGrey,exports.convertBinaryToRgb=convertBinaryToRgb,exports.convertBitDepth=convertBitDepth,exports.convertColor=convertColor,exports.copyAlpha=copyAlpha,exports.copyTo=copyTo,exports.correctBackground=correctBackground,exports.correctColor=correctColor,exports.crop=crop,exports.cropAlpha=cropAlpha,exports.cropRectangle=cropRectangle,exports.crosscheck=crosscheck,exports.decode=decode,exports.decodeJpeg=decodeJpeg,exports.decodePng=decodePng,exports.decodeStack=decodeStack,exports.decodeTiff=decodeTiff,exports.defaultPng=defaultPng,exports.derivativeFilter=derivativeFilter,exports.dilate=dilate,exports.directConvolution=directConvolution,exports.divide=divide,exports.drawCircleOnImage=drawCircleOnImage,exports.drawKeypoints=drawKeypoints,exports.drawLineOnImage=drawLineOnImage,exports.drawLineOnMask=drawLineOnMask,exports.drawMarker=drawMarker,exports.drawMarkers=drawMarkers,exports.drawMatches=drawMatches,exports.drawPoints=drawPoints,exports.drawPolygonOnImage=drawPolygonOnImage,exports.drawPolygonOnMask=drawPolygonOnMask,exports.drawPolylineOnImage=drawPolylineOnImage,exports.drawPolylineOnMask=drawPolylineOnMask,exports.drawRectangle=drawRectangle,exports.drawText=drawText,exports.encode=encode,exports.encodeDataURL=encodeDataURL,exports.encodeJpeg=encodeJpeg,exports.encodePng=encodePng,exports.erode=erode,exports.extendBorders=extendBorders,exports.extract=extract,exports.fetchURL=fetchURL,exports.flip=flip,exports.floodFill=floodFill,exports.fromMask=fromMask,exports.gaussianBlur=gaussianBlur,exports.getAffineTransform=getAffineTransform,exports.getBestKeypointsInRadius=getBestKeypointsInRadius,exports.getBorderPoints=getBorderPoints,exports.getBriefDescriptors=getBriefDescriptors,exports.getConvexityDefects=getConvexityDefects,exports.getCrosscheckMatches=getCrosscheckMatches,exports.getDirection=getDirection,exports.getExtrema=getExtrema,exports.getFastKeypoints=getFastKeypoints,exports.getHarrisScore=getHarrisScore,exports.getImageFromIFD=getImageFromIFD,exports.getIntensityCentroid=getIntensityCentroid,exports.getMask=getMask,exports.getOrientedFastKeypoints=getOrientedFastKeypoints,exports.getPerspectiveWarp=getPerspectiveWarp,exports.getRegressionVariables=getRegressionVariables,exports.getRois=getRois,exports.getShiTomasiScore=getShiTomasiScore,exports.gradientFilter=gradientFilter,exports.grey=grey,exports.histogram=histogram$1,exports.hypotenuse=hypotenuse,exports.increaseContrast=increaseContrast,exports.invert=invert,exports.level=level,exports.mean=mean,exports.median=median,exports.medianFilter=medianFilter,exports.merge=merge,exports.morphologicalGradient=morphologicalGradient,exports.multiply=multiply,exports.open=open,exports.or=or,exports.order4Points=order4Points,exports.overlapImages=overlapImages,exports.paintMaskOnImage=paintMaskOnImage,exports.paintMaskOnMask=paintMaskOnMask,exports.pixelate=pixelate,exports.rawDirectConvolution=rawDirectConvolution,exports.read=read,exports.readCanvas=readCanvas,exports.readImg=readImg,exports.readSync=readSync,exports.removeClosePoints=removeClosePoints,exports.resize=resize,exports.rotate=rotate,exports.separableConvolution=separableConvolution,exports.solidFill=solidFill,exports.split=split,exports.subtract=subtract,exports.threshold=threshold,exports.topHat=topHat,exports.transform=transform,exports.transformRotate=transformRotate,exports.variance=variance,exports.waterShed=waterShed,exports.write=write,exports.writeCanvas=writeCanvas,exports.writeSync=writeSync});
3
3
  //# sourceMappingURL=image-js.umd.min.js.map