"use strict";var C=Object.create;var h=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var B=Object.getOwnPropertyNames;var W=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var _=(e,r)=>{for(var t in r)h(e,t,{get:r[t],enumerable:!0})},I=(e,r,t,a)=>{if(r&&typeof r=="object"||typeof r=="function")for(let n of B(r))!T.call(e,n)&&n!==t&&h(e,n,{get:()=>r[n],enumerable:!(a=k(r,n))||a.enumerable});return e};var P=(e,r,t)=>(t=e!=null?C(W(e)):{},I(r||!e||!e.__esModule?h(t,"default",{value:e,enumerable:!0}):t,e)),R=e=>I(h({},"__esModule",{value:!0}),e);var H={};_(H,{bgrBufferToRgbaBuffer:()=>A,bgrToLab:()=>L,bgrToLabL:()=>v,originBgrBufferToSkiaImage:()=>G,prepareFreqLayersFromBgrBuffer:()=>E,prepareFreqLayersFromWorkMat:()=>w,preparePaintResourcesFromWorkBuffer:()=>V,releaseFreqLayerImages:()=>q});module.exports=R(H);var o=P(require("./opencvAdapter")),y=require("./maskSegmentRuntime");function v(e,r,t){return L(e,r,t).l}function L(e,r,t){let a=t/255,n=r/255,i=e/255;a=a>.04045?Math.pow((a+.055)/1.055,2.4):a/12.92,n=n>.04045?Math.pow((n+.055)/1.055,2.4):n/12.92,i=i>.04045?Math.pow((i+.055)/1.055,2.4):i/12.92;const s=a*.412453+n*.35758+i*.180423,l=a*.212671+n*.71516+i*.072169,m=a*.019334+n*.119193+i*.950227,c=.950456,f=1,g=1.088754,u=6/29,b=u*u*u;let M=s/c,p=l/f,d=m/g;M=M>b?Math.cbrt(M):M/(3*u*u)+4/29,p=p>b?Math.cbrt(p):p/(3*u*u)+4/29,d=d>b?Math.cbrt(d):d/(3*u*u)+4/29;const x=p*116-16,F=500*(M-p),S=200*(p-d);return{l:Math.max(0,Math.min(255,Math.round(x*255/100))),a:Math.max(0,Math.min(255,Math.round(F+128))),b:Math.max(0,Math.min(255,Math.round(S+128)))}}function A(e,r,t){const a=r*t,n=new Uint8Array(a*4);for(let i=0;i<a;i++){const s=i*3,l=i*4;n[l]=e[s+2],n[l+1]=e[s+1],n[l+2]=e[s],n[l+3]=255}return n}function q(e){e?.lowFreqImage.dispose(),e?.highFreqImage.dispose()}async function U(e,r,t,a,n){const i=o.default.createMat(t,a,1),s=o.default.createMat(t,a,1),l=o.default.createMat(t,a,1),m=o.default.createMat(t,a,1);try{return o.default.convertTo(e,i,o.default.CV_16SC1),o.default.convertTo(r,s,o.default.CV_16SC1),await o.default.subtract(i,s,l),await o.default.addWeighted(l,n,null,0,128,l),o.default.convertTo(l,m,o.default.CV_8UC1),m}finally{i.release(),s.release(),l.release()}}async function z(e,r,t){const a=(0,y.getMaskSegmentRuntimeConfig)().pipeline.paintFreqMaxLongSide,n=Math.max(r,t);if(n<=a)return{mat:e,cols:r,rows:t,owned:!1};const i=a/n,s=Math.floor(r*i),l=Math.floor(t*i),m=o.default.createMat(s,l,3);return await o.default.resize(e,m,{width:s,height:l},o.default.INTER_LINEAR),{mat:m,cols:s,rows:l,owned:!0}}async function w(e,r,t){const a=(0,y.getMaskSegmentRuntimeConfig)().paint,n=__DEV__?performance.now():0,i=await z(e,r,t),s=i.mat,l=i.cols,m=i.rows;let c=null,f=null,g=null,u=null;try{c=o.default.cvtColorBgr(s,o.default.COLOR_BGR2Lab),f=o.default.createMat(l,m,1),o.default.extractChannel(c,f,0),c.release(),c=null,g=o.default.createMat(l,m,1);const b=a.lLowBlurKernel;await o.default.GaussianBlur(f,g,{width:b,height:b},0),u=await U(f,g,l,m,a.lHighGain),await o.default.addWeighted(g,a.lLowContrast,null,0,128*(1-a.lLowContrast),g),await o.default.addWeighted(g,a.lLowBrightness,null,0,128*(1-a.lLowBrightness),g);const M=o.default.grayMatToSkiaImage(g),p=o.default.grayMatToSkiaImage(u);return!M||!p?(M?.dispose(),p?.dispose(),null):{lowFreqImage:M,highFreqImage:p}}finally{i.owned&&s.release(),c?.release(),f?.release(),g?.release(),u?.release()}}async function V(e,r,t,a){const n=r*t;if(e.length!==n*3)return null;const i=__DEV__?performance.now():0,s=o.default.bgrBufferToMat(e,r,t);try{const l=o.default.matToSkiaImage(s),c=await w(s,r,t);if(!c)return(await l)?.dispose(),null;a?.(c);const f=await l;return f?{originImage:f,layers:c}:(q(c),null)}finally{s.release()}}async function E(e,r,t){const a=r*t;if(e.length!==a*3)return null;const n=o.default.bgrBufferToMat(e,r,t);try{return await w(n,r,t)}finally{n.release()}}async function G(e,r,t){return o.default.bgrBufferToSkiaImage(e,r,t)}
