"use strict";var w=Object.create;var h=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var T=Object.getPrototypeOf,E=Object.prototype.hasOwnProperty;var A=(t,e,r)=>e in t?h(t,e,{enumerable:!0,configurable:!0,writable:!0,value:r}):t[e]=r;var S=(t,e)=>{for(var r in e)h(t,r,{get:e[r],enumerable:!0})},P=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let o of W(e))!E.call(t,o)&&o!==r&&h(t,o,{get:()=>e[o],enumerable:!(n=_(e,o))||n.enumerable});return t};var I=(t,e,r)=>(r=t!=null?w(T(t)):{},P(e||!t||!t.__esModule?h(r,"default",{value:t,enumerable:!0}):r,t)),k=t=>P(h({},"__esModule",{value:!0}),t);var b=(t,e,r)=>A(t,typeof e!="symbol"?e+"":e,r);var N={};S(N,{ContourWrapper:()=>v,WrappedMat:()=>l,default:()=>L});module.exports=k(N);var f=I(require("react-native-fs")),a=require("react-native-fast-opencv"),u=require("./pngImage"),C=require("./skiaImage");const g=0;class l{constructor(e,r,n,o=3){b(this,"mat");b(this,"cols");b(this,"rows");b(this,"channels");this.mat=e,this.cols=r,this.rows=n,this.channels=o}release(){a.OpenCV.releaseBuffers([this.mat.id])}async clone(){const e=a.OpenCV.invoke("clone",this.mat);return new l(e,this.cols,this.rows,this.channels)}}class v{constructor(e){b(this,"pointVector");this.pointVector=e}release(){a.OpenCV.releaseBuffers([this.pointVector.id])}}function B(t,e,r){return e===void 0?a.OpenCV.createObject(a.ObjectType.Scalar,t):r===void 0?a.OpenCV.createObject(a.ObjectType.Scalar,t,e,0):a.OpenCV.createObject(a.ObjectType.Scalar,t,e,r)}async function y(t,e=!1){const r=(0,u.normalizePath)(t),n=await f.default.readFile(r,"base64"),o=a.OpenCV.base64ToMat(n);let i;try{i=(0,u.readPngHeaderFromBase64)(n)}catch{}const{mat:s,extraReleaseIds:p}=(0,u.ensureMat8U)(o,i),m=a.OpenCV.toJSValue(s);if(e){const M=a.OpenCV.createObject(a.ObjectType.Mat,m.rows,m.cols,a.DataTypes.CV_8UC1);return a.OpenCV.invoke("cvtColor",s,M,a.ColorConversionCodes.COLOR_BGR2GRAY),a.OpenCV.releaseBuffers([...new Set([o.id,s.id,...p])]),new l(M,m.cols,m.rows,1)}const d=m.type===a.DataTypes.CV_8UC1?1:m.type===a.DataTypes.CV_8UC4?4:3;return s.id!==o.id&&a.OpenCV.releaseBuffers([o.id]),new l(s,m.cols,m.rows,d)}function R(t,e){return a.OpenCV.createObject(a.ObjectType.Size,t,e)}function O(t){const e=Math.max(1,Math.round(t));return e%2===0?e+1:e}const c={IMREAD_GRAYSCALE:g,THRESH_BINARY:a.ThresholdTypes.THRESH_BINARY,MORPH_RECT:a.MorphShapes.MORPH_RECT,MORPH_ELLIPSE:a.MorphShapes.MORPH_ELLIPSE,MORPH_OPEN:a.MorphTypes.MORPH_OPEN,MORPH_CLOSE:a.MorphTypes.MORPH_CLOSE,RETR_EXTERNAL:a.RetrievalModes.RETR_EXTERNAL,CHAIN_APPROX_SIMPLE:a.ContourApproximationModes.CHAIN_APPROX_SIMPLE,CHAIN_APPROX_NONE:a.ContourApproximationModes.CHAIN_APPROX_NONE,COLOR_BGR2GRAY:a.ColorConversionCodes.COLOR_BGR2GRAY,COLOR_BGR2Lab:a.ColorConversionCodes.COLOR_BGR2Lab,COLOR_Lab2BGR:a.ColorConversionCodes.COLOR_Lab2BGR,COLOR_GRAY2BGR:a.ColorConversionCodes.COLOR_GRAY2BGR,CV_8UC1:a.DataTypes.CV_8UC1,CV_16SC1:a.DataTypes.CV_16SC1,INTER_LINEAR:a.InterpolationFlags.INTER_LINEAR,INTER_NEAREST:a.InterpolationFlags.INTER_NEAREST,async ensurePngPath(t,e){const r=e??(0,u.pngCacheName)(t,"img");return(0,u.ensurePngFile)(t,r)},async imread(t,e){const r=(0,u.normalizePath)(t);if((0,u.isPngPath)(r)&&await f.default.exists(r)){if(e===g)return y(r,!0);const{buffer:p,cols:m,rows:d}=await(0,u.readPngBgrBuffer)(r);return c.bgrBufferToMat(p,m,d)}const n=await(0,u.ensurePngFile)(t,(0,u.pngCacheName)(t,"imread"));if(e===g)return y(n,!0);const{buffer:o,cols:i,rows:s}=await(0,u.readPngBgrBuffer)(n);return c.bgrBufferToMat(o,i,s)},createMat(t,e,r=1){const n=r===1?a.DataTypes.CV_8UC1:r===3?a.DataTypes.CV_8UC3:a.DataTypes.CV_8UC4,o=a.OpenCV.createObject(a.ObjectType.Mat,e,t,n);return new l(o,t,e,r)},async cvtColor(t,e){const r=c.createMat(t.cols,t.rows,1);return a.OpenCV.invoke("cvtColor",t.mat,r.mat,e),r},cvtColorBgr(t,e){const r=c.createMat(t.cols,t.rows,3);return a.OpenCV.invoke("cvtColor",t.mat,r.mat,e),r},grayToBgr(t){const e=c.createMat(t.cols,t.rows,3);return a.OpenCV.invoke("cvtColor",t.mat,e.mat,a.ColorConversionCodes.COLOR_GRAY2BGR),e},async ensureBgr3(t){if(t.channels===3)return t;const e=c.createMat(t.cols,t.rows,3),r=t.channels===4?a.ColorConversionCodes.COLOR_BGRA2BGR:a.ColorConversionCodes.COLOR_GRAY2BGR;return a.OpenCV.invoke("cvtColor",t.mat,e.mat,r),e},binaryBufferToMat(t,e,r){const n=a.OpenCV.bufferToMat("uint8",r,e,1,t);return new l(n,e,r,1)},bgrBufferToMat(t,e,r){const n=a.OpenCV.bufferToMat("uint8",r,e,3,t);return new l(n,e,r,3)},async grayBufferToMat(t,e,r){const n=`${f.default.CachesDirectoryPath}/seg_bin_${Date.now()}.pgm`,o=`P5
${e} ${r}
255
`,i=new TextEncoder().encode(o),s=new Uint8Array(i.length+t.length);s.set(i,0),s.set(t,i.length);let p="";const m=8192;for(let d=0;d<s.length;d+=m){const M=s.subarray(d,d+m);p+=String.fromCharCode(...M)}await f.default.writeFile(n,btoa(p),"base64");try{return y(n,!0)}finally{await f.default.exists(n)&&await f.default.unlink(n)}},matToBuffer(t){const e=a.OpenCV.invoke("clone",t.mat);try{const{buffer:r,cols:n,rows:o,channels:i}=a.OpenCV.matToBuffer(e,"uint8");return{buffer:r,cols:n,rows:o,channels:i}}finally{a.OpenCV.releaseBuffers([e.id])}},async inRangeBgr(t,e,r,n){const o=B(Math.max(0,e.b-r),Math.max(0,e.g-r),Math.max(0,e.r-r)),i=B(Math.min(255,e.b+r),Math.min(255,e.g+r),Math.min(255,e.r+r));a.OpenCV.invoke("inRange",t.mat,o,i,n.mat)},async resize(t,e,r,n=a.InterpolationFlags.INTER_LINEAR){const o=R(r.width,r.height);a.OpenCV.invoke("resize",t.mat,e.mat,o,0,0,n),e.cols=r.width,e.rows=r.height},async resizeBgrBuffer(t,e,r,n,o,i=a.InterpolationFlags.INTER_NEAREST){if(e===n&&r===o)return t;const s=c.bgrBufferToMat(t,e,r),p=c.createMat(n,o,3);try{return await c.resize(s,p,{width:n,height:o},i),c.matToBuffer(p).buffer}finally{s.release(),p.release()}},async matToRgbaBuffer(t){const e=c.createMat(t.cols,t.rows,4);try{a.OpenCV.invoke("cvtColor",t.mat,e.mat,a.ColorConversionCodes.COLOR_BGR2RGBA);const{buffer:r,cols:n,rows:o}=c.matToBuffer(e);return{buffer:r,cols:n,rows:o}}finally{e.release()}},async matToSkiaImage(t){const{buffer:e,cols:r,rows:n}=await c.matToRgbaBuffer(t);return(0,C.rgbaBufferToSkiaImage)(e,r,n)},grayMatToSkiaImage(t){const{buffer:e,cols:r,rows:n}=c.matToBuffer(t),o=r*n,i=new Uint8Array(o*4);for(let s=0;s<o;s++){const p=e[s],m=s*4;i[m]=p,i[m+1]=p,i[m+2]=p,i[m+3]=255}return(0,C.rgbaBufferToSkiaImage)(i,r,n)},async bgrBufferToSkiaImage(t,e,r){const n=c.bgrBufferToMat(t,e,r);try{return await c.matToSkiaImage(n)}finally{n.release()}},async threshold(t,e,r,n,o){a.OpenCV.invoke("threshold",t.mat,e.mat,r,n,o)},async getStructuringElement(t,e){const r=R(e.width,e.height),n=a.OpenCV.invoke("getStructuringElement",t,r);return new l(n,e.width,e.height,1)},async morphologyEx(t,e,r,n){a.OpenCV.invoke("morphologyEx",t.mat,e.mat,r,n.mat)},async findContours(t,e,r){const n=a.OpenCV.createObject(a.ObjectType.PointVectorOfVectors);a.OpenCV.invoke("findContours",t.mat,n,e,r);const i=a.OpenCV.toJSValue(n).array.map((s,p)=>{const m=a.OpenCV.copyObjectFromVector(n,p);return new v(m)});return a.OpenCV.releaseBuffers([n.id]),i},async contourArea(t){return a.OpenCV.invoke("contourArea",t.pointVector,!1).value},async boundingRect(t){const e=a.OpenCV.invoke("boundingRect",t.pointVector),r=a.OpenCV.toJSValue(e);return{x:r.x,y:r.y,width:r.width,height:r.height}},async arcLength(t,e){return a.OpenCV.invoke("arcLength",t.pointVector,e).value},async approxPolyDP(t,e,r){const n=a.OpenCV.createObject(a.ObjectType.PointVector);try{return a.OpenCV.invoke("approxPolyDP",t.pointVector,n,e,r),a.OpenCV.toJSValue(n).array}finally{a.OpenCV.releaseBuffers([n.id])}},async GaussianBlur(t,e,r,n){const o=O(r.width),i=O(r.height),s=R(o,i);a.OpenCV.invoke("GaussianBlur",t.mat,e.mat,s,n)},extractChannel(t,e,r){a.OpenCV.invoke("extractChannel",t.mat,e.mat,r)},convertTo(t,e,r,n=1,o=0){a.OpenCV.invoke("convertTo",t.mat,e.mat,r,n,o)},async subtract(t,e,r){a.OpenCV.invoke("subtract",t.mat,e.mat,r.mat)},async addWeighted(t,e,r,n,o,i){r?a.OpenCV.invoke("addWeighted",t.mat,e,r.mat,n,o,i.mat):a.OpenCV.invoke("addWeighted",t.mat,e,t.mat,0,o,i.mat)},async imwrite(t,e){const r=(0,u.normalizePath)(t);a.OpenCV.saveMatToFile(e.mat,r,"png",u.PNG_COMPRESSION)}};var L=c;
