react-native-mask-segment-c.../dist/utils/compositePaintedImage.js

2 lines
2.7 KiB
JavaScript
Raw Normal View History

"use strict";var U=Object.create;var y=Object.defineProperty;var E=Object.getOwnPropertyDescriptor;var V=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,v=Object.prototype.hasOwnProperty;var R=(e,t)=>{for(var r in t)y(e,r,{get:t[r],enumerable:!0})},A=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of V(t))!v.call(e,s)&&s!==r&&y(e,s,{get:()=>t[s],enumerable:!(n=E(t,s))||n.enumerable});return e};var S=(e,t,r)=>(r=e!=null?U(M(e)):{},A(t||!e||!e.__esModule?y(r,"default",{value:e,enumerable:!0}):r,e)),T=e=>A(y({},"__esModule",{value:!0}),e);var z={};R(z,{compositePaintedImage:()=>Z});module.exports=T(z);var C=require("buffer"),k=S(require("react-native-fs")),x=S(require("upng-js")),F=require("./paintShaderRuntime"),I=require("./exportUtils");function q(e,t,r,n,s){const m=n*s,c=new Uint8Array(m*4),d=new Map;for(const[a,g]of r)d.set(a+1,g);for(let a=0;a<m;a++){const g=t[a],l=g>0?d.get(g):void 0,i=a*4;if(l)c[i]=l.r,c[i+1]=l.g,c[i+2]=l.b,c[i+3]=255;else{const f=a*3;c[i]=e[f+2],c[i+1]=e[f+1],c[i+2]=e[f],c[i+3]=255}}const h=x.default.encode([c.buffer],n,s,0);return new Uint8Array(h)}async function Z(e){const{originBuffer:t,cols:r,rows:n,pickBuffer:s,paintedRegions:m,destDir:c,exportPngBase64:d,exportPngBytes:h,shaderTextures:a,renderWidth:g,renderHeight:l}=e;if(m.size===0)throw new Error("No painted regions, cannot save");if(s.length!==r*n){const o="pickMap size does not match image";throw console.error("[VIZ-SAVE] composite will throw:",o,{pickLen:s.length,expected:r*n,cols:r,rows:n}),new Error(o)}if(t.length!==r*n*3){const o="Original buffer size does not match image";throw console.error("[VIZ-SAVE] composite will throw:",o,{originLen:t.length,expected:r*n*3,cols:r,rows:n}),new Error(o)}let i,f,B=!1,P=!1;if(d&&d.length>0)f=d,B=!0;else if(h&&h.length>0)i=h,B=!0;else if(a&&g&&l)try{const o=await(0,F.renderPaintedImageOffscreen)({originImage:a.originImage,paintColorMap:a.paintColorMap,lowFreqImage:a.lowFreqImage,highFreqImage:a.highFreqImage,width:g,height:l,showOrigin:!1});if(o){let w="";try{const p=o.encodeToBase64;typeof p=="function"&&(w=p.call(o)||"")}catch(p){console.warn("[VIZ-SAVE] offscreen encodeToBase64 failed:",p)}w&&w.length>0&&(i=new Uint8Array(C.Buffer.from(w,"base64")),P=!0);try{const p=o.dispose;typeof p=="function"&&p.call(o)}catch{}}}catch(o){console.warn("[VIZ-SAVE] rich shader offscreen for export failed (will fallback):",o)}if(!f&&!i)try{i=q(t,s,m,r,n)}catch{throw new Error("CPU recolor PNG decoding failed")}const b=c??k.default.CachesDirectoryPath,u=`${b}/painted_${Date.now()}.png`;try{f?await(0,I.writePngBase64ToFile)(u,f):await(0,I.writePngBytesToFile)(u,i)}catch(o){throw console.error("[VIZ-SAVE] composite writeFile threw:",o,{filePath:u,dir:b}),o}return{filePath:u,width:r,height:n,paintedCount:m.size}}