@niivue/nv-ext-drawing 1.0.0-rc.21 → 1.0.0-rc.210

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.
package/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Drawing interpolation and segmentation tools for [NiiVue](https://github.com/niivue).
4
4
 
5
+ Documentation: https://niivue.github.io/mono/docs/packages/nv-ext-drawing/
6
+
5
7
  ## Installation
6
8
 
7
9
  ```bash
@@ -46,6 +48,7 @@ dr.update(filled)
46
48
  ```bash
47
49
  bunx nx build nv-ext-drawing # Build
48
50
  bunx nx typecheck nv-ext-drawing # Type-check
51
+ bunx nx test nv-ext-drawing # Test
49
52
  ```
50
53
 
51
54
  ## Part of the [NiiVue](https://github.com/niivue) ecosystem
package/dist/index.d.ts CHANGED
@@ -14,7 +14,9 @@ export declare function findDrawingBoundarySlices(sliceType: SliceType, drawBitm
14
14
  * Runs in a Web Worker. Returns a new Uint8Array with the interpolated bitmap.
15
15
  *
16
16
  * Image data (`imageData`) must be in RAS voxel order. Use
17
- * `ctx.backgroundVolume.imgRAS` from the extension context.
17
+ * `ctx.backgroundVolume.imgRAS` from the extension context. `maxVal` is a
18
+ * positive scale in the same raw units, normally its largest absolute value
19
+ * (`globalMin`/`globalMax` are scaled by scl_slope/scl_inter).
18
20
  */
19
21
  export declare function interpolateMaskSlices(drawBitmap: Uint8Array, dims: DrawingDims, imageData: ArrayLike<number> | null, maxVal: number, sliceIndexLow: number | undefined, sliceIndexHigh: number | undefined, options?: InterpolationOptions): Promise<Uint8Array>;
20
22
  /**
@@ -1,193 +1,133 @@
1
- import { NVWorker as p } from "@niivue/niivue";
2
- const g = '(function(){"use strict";const X={AXIAL:0,CORONAL:1};function ct(t,e){const{dimX:o,dimY:r,dimZ:s}=e;return t===X.AXIAL?{w:o,h:r,depth:s}:t===X.CORONAL?{w:o,h:s,depth:r}:{w:r,h:s,depth:o}}function Y(t,e,o,r){return t+e*r.dimX+o*r.dimX*r.dimY}function j(t,e,o,r,s){if(!s)return Y(t,e,o,r);const{img2RASstep:i,img2RASstart:n}=s;return n[0]+t*i[0]+n[1]+e*i[1]+n[2]+o*i[2]}function U(t,e,o,r){const{dimX:s,dimY:i,dimZ:n}=r;if(e===X.AXIAL){const c=new Float32Array(s*i),f=t*s*i;for(let a=0;a<c.length;a++)c[a]=o[f+a];return c}if(e===X.CORONAL){const c=new Float32Array(s*n);for(let f=0;f<n;f++)for(let a=0;a<s;a++)c[a+f*s]=o[Y(a,t,f,r)];return c}const l=new Float32Array(i*n);for(let c=0;c<n;c++)for(let f=0;f<i;f++)l[f+c*i]=o[Y(t,f,c,r)];return l}function J(t,e,o,r,s,i){const{dimX:n,dimY:l,dimZ:c}=r,f=1/s;if(e===X.AXIAL){const u=new Float32Array(n*l);for(let h=0;h<l;h++)for(let p=0;p<n;p++)u[p+h*n]=o[j(p,h,t,r,i)]*f;return u}if(e===X.CORONAL){const u=new Float32Array(n*c);for(let h=0;h<c;h++)for(let p=0;p<n;p++)u[p+h*n]=o[j(p,t,h,r,i)]*f;return u}const a=new Float32Array(l*c);for(let u=0;u<c;u++)for(let h=0;h<l;h++)a[h+u*l]=o[j(t,h,u,r,i)]*f;return a}function wt(t,e,o,r,s,i,n){const{dimX:l,dimY:c,dimZ:f}=s;if(o===X.AXIAL){const a=e*l*c;for(let u=0;u<t.length;u++)t[u]>=i&&(r[a+u]=n)}else if(o===X.CORONAL)for(let a=0;a<f;a++)for(let u=0;u<l;u++)t[u+a*l]>=i&&(r[Y(u,e,a,s)]=n);else for(let a=0;a<f;a++)for(let u=0;u<c;u++)t[u+a*c]>=i&&(r[Y(e,u,a,s)]=n)}function ft(t,e,o){if(e<3||o<3)return;const r=new Float32Array(t.length);for(let s=0;s<o;s++)for(let i=0;i<e;i++){const n=i+s*e;r[n]=i===0||i===e-1?t[n]:(t[n-1]+2*t[n]+t[n+1])*.25}for(let s=0;s<o;s++)for(let i=0;i<e;i++){const n=i+s*e;t[n]=s===0||s===o-1?r[n]:(r[n-e]+2*r[n]+r[n+e])*.25}}function pt(t,e,o,r){const s=Math.abs(o-t),i=Math.abs(o-e),n=Math.exp(-s*s/(2*r*r)),l=Math.exp(-i*i/(2*r*r)),c=n+l;return c<1e-6?.5:n/c}function bt(t,e,o){const{depth:r}=ct(t,o);let s=-1,i=-1;for(let n=0;n<r;n++){const l=U(n,t,e,o);let c=!1;for(let f=0;f<l.length;f++)if(l[f]>0){c=!0;break}c&&(s===-1&&(s=n),i=n)}return s===-1?null:{first:s,last:i}}function xt(t,e,o,r,s,i,n,l){const c=n.sliceType??X.AXIAL,{w:f,h:a,depth:u}=ct(c,e),h=u-1,p={intensityWeight:n.intensityWeight??.7,binaryThreshold:n.binaryThreshold??.375,intensitySigma:n.intensitySigma??.1,applySmoothingToSlices:n.applySmoothingToSlices??!0,useIntensityGuided:n.useIntensityGuided??!1};if(s!==void 0&&i!==void 0){if(s>=i)throw new Error("Low slice index must be less than high slice index");if(s<0||i>h)throw new Error(`Slice indices out of bounds [0, ${h}]`)}const C=new Map;for(let b=0;b<=h;b++){const N=U(b,c,t,e);for(let y=0;y<N.length;y++){const g=N[y];if(g>0){const x=C.get(g);x?(x.min=Math.min(x.min,b),x.max=Math.max(x.max,b)):C.set(g,{min:b,max:b})}}}for(const[b,N]of C){const y=s!==void 0?Math.max(s,N.min):N.min,g=i!==void 0?Math.min(i,N.max):N.max;if(y>=g||g-y<2)continue;const x=U(y,c,t,e),O=U(g,c,t,e),M=new Float32Array(x.length),I=new Float32Array(O.length);for(let A=0;A<x.length;A++)M[A]=x[A]===b?1:0,I[A]=O[A]===b?1:0;p.applySmoothingToSlices&&(ft(M,f,a),ft(I,f,a));for(let A=y+1;A<g;A++){const E=new Float32Array(f*a),d=(A-y)/(g-y),w=1-d;if(p.useIntensityGuided&&o){const m=J(y,c,o,e,r,l),v=J(g,c,o,e,r,l),nt=J(A,c,o,e,r,l),Z=p.intensityWeight;for(let F=0;F<M.length;F++)if(M[F]>0||I[F]>0){const et=pt(m[F],v[F],nt[F],p.intensitySigma),D=Z*et+(1-Z)*w,ot=1-D;E[F]=M[F]*D+I[F]*ot}else E[F]=M[F]*w+I[F]*d}else for(let m=0;m<M.length;m++)E[m]=M[m]*w+I[m]*d;wt(E,A,c,t,e,p.binaryThreshold,b)}}return t}function k(t,e,o,r){return t+e*r.dimX+o*r.dimX*r.dimY}function lt(t,e,o,r,s){if(!s)return k(t,e,o,r);const{img2RASstep:i,img2RASstart:n}=s;return n[0]+t*i[0]+n[1]+e*i[1]+n[2]+o*i[2]}const K=[[-1,0,0],[1,0,0],[0,-1,0],[0,1,0],[0,0,-1],[0,0,1]],at=[[-1,-1,0],[1,-1,0],[-1,1,0],[1,1,0],[0,-1,-1],[0,1,-1],[-1,0,-1],[1,0,-1],[0,-1,1],[0,1,1],[-1,0,1],[1,0,1]],Mt=[[-1,-1,-1],[1,-1,-1],[-1,1,-1],[1,1,-1],[-1,-1,1],[1,-1,1],[-1,1,1],[1,1,1]];function ut(t){return t>=26?[...K,...at,...Mt]:t>=18?[...K,...at]:[...K]}function dt(t,e,o,r,s={},i,n){const{tolerance:l=0,thresholdMode:c="symmetric",percent:f=0,calMin:a=0,calMax:u=1,connectivity:h=6,maxDistanceMM:p=Number.POSITIVE_INFINITY,is2D:C=!1,sliceAxis:b=0,penValue:N=1}=s,{dimX:y,dimY:g,dimZ:x}=o,O=y*g*x,[M,I,A]=t;if(M<0||I<0||A<0||M>=y||I>=g||A>=x)return{filledCount:0,seedIntensity:0,intensityMin:0,intensityMax:0};const E=lt(M,I,A,o,i),d=Number(r[E]);let w,m,v;if(s.intensityMin!==void 0&&s.intensityMax!==void 0)w=s.intensityMin,m=s.intensityMax;else{let L=c==="auto"?"symmetric":c;switch(c==="auto"&&(v=d>(a+u)*.5,L=v?"bright":"dark"),L){case"percent":{const S=d===0?.01:d;w=S*(1-f),m=S*(1+f);break}case"bright":if(f>0){const S=d===0?.01:d;w=S*(1-f),m=S*(1+f)}else l>0?(w=d,m=d+l):(w=d,m=Number.POSITIVE_INFINITY);break;case"dark":if(f>0){const S=d===0?.01:d;w=S*(1-f),m=S*(1+f)}else l>0?(w=d-l,m=d):(w=Number.NEGATIVE_INFINITY,m=d);break;default:w=d-l,m=d+l;break}}const nt=ut(h),Z=n?n[0]:1,F=n?n[1]:1,et=n?n[2]:1,D=p*p;function ot(L,S,H){if(!Number.isFinite(D))return!0;const P=(L-M)*Z,q=(S-I)*F,$=(H-A)*et;return P*P+q*q+$*$<=D}let z=-1,G=-1;C&&(b===0?(z=2,G=A):b===1?(z=1,G=I):b===2&&(z=0,G=M));const it=new Uint8Array(O),yt=k(M,I,A,o);if(d<w||d>m)return{filledCount:0,seedIntensity:d,intensityMin:w,intensityMax:m,isBright:v};const rt=[yt];it[yt]=1;let gt=0;for(;rt.length>0;){const L=rt.shift(),S=Math.floor(L/(y*g)),H=Math.floor((L-S*y*g)/y),P=L-S*y*g-H*y;e[L]=N,gt++;for(const[q,$,St]of nt){const B=P+q,W=H+$,T=S+St;if(B<0||W<0||T<0||B>=y||W>=g||T>=x||z>=0&&(z===0?B:z===1?W:T)!==G)continue;const st=k(B,W,T,o);if(it[st]||(it[st]=1,!ot(B,W,T)))continue;const At=Number(r[lt(B,W,T,o,i)]);At<w||At>m||rt.push(st)}}return{filledCount:gt,seedIntensity:d,intensityMin:w,intensityMax:m,isBright:v}}function It(t,e,o,r,s=6){const{dimX:i,dimY:n,dimZ:l}=o,[c,f,a]=t;if(c<0||f<0||a<0||c>=i||f>=n||a>=l)return 0;const u=k(c,f,a,o),h=e[u];if(h===r||h===0)return 0;const p=i*n*l,C=new Uint8Array(p),b=ut(s),N=[u];C[u]=1;let y=0;for(;N.length>0;){const g=N.shift();e[g]=r,y++;const x=Math.floor(g/(i*n)),O=Math.floor((g-x*i*n)/i),M=g-x*i*n-O*i;for(const[I,A,E]of b){const d=M+I,w=O+A,m=x+E;if(d<0||w<0||m<0||d>=i||w>=n||m>=l)continue;const v=k(d,w,m,o);C[v]||(C[v]=1,e[v]===h&&N.push(v))}}return y}const R=self.postMessage.bind(self),Ft={findBoundarySlices(t){const e=t.sliceType,o=new Uint8Array(t.drawBitmap),r=t.dims;return{result:bt(e,o,r)}},interpolateMaskSlices(t){const e=new Uint8Array(t.drawBitmap),o=t.dims,r=t.imageData?new Float32Array(t.imageData):null,s=t.maxVal??1,i=t.sliceIndexLow,n=t.sliceIndexHigh,l=t.options??{},c=t.rasMap??void 0;return{drawBitmap:xt(e,o,r,s,i,n,l,c).buffer}},magicWand(t){const e=t.seed,o=new Uint8Array(t.drawBitmap),r=t.dims,s=new Float32Array(t.imageData),i=t.options??{},n=t.rasMap??void 0,l=t.voxelSizeMM??void 0,c=dt(e,o,r,s,i,n,l);return{drawBitmap:o.buffer,result:c}},magicWandFromBitmap(t){const e=t.seed,o=new Uint8Array(t.drawBitmap),r=t.dims,s=t.newColor??0,i=t.connectivity??6,n=It(e,o,r,s,i);return{drawBitmap:o.buffer,count:n}}};let V=null,Q=null,_=null,tt=null,mt,ht;self.onmessage=t=>{const e=t.data;if(e.type==="initShared"){V=new Uint8Array(e.workingBuffer),Q=new Float32Array(e.imageBuffer),_=new Uint8Array(e.committedBuffer),tt=e.dims,mt=e.rasMap??void 0,ht=e.voxelSizeMM??void 0,R({type:"initSharedDone"});return}if(e.type==="updateCommitted"){_=new Uint8Array(e.committed);return}if(e.type==="magicWandShared"){if(!V||!Q||!_||!tt){R({type:"magicWandSharedResult",error:"Not initialized",gen:e.gen});return}V.set(_);const n=dt(e.seed,V,tt,Q,e.options??{},mt,ht);R({type:"magicWandSharedResult",result:n,gen:e.gen});return}const{_wbId:o,name:r,...s}=e,i=Ft[r];if(!i){R({_wbId:o,_wbError:`Unknown operation: ${r}`});return}try{const n=i(s),l=[];for(const c of Object.values(n))c instanceof ArrayBuffer&&l.push(c);R({_wbId:o,...n},l)}catch(n){R({_wbId:o,_wbError:n instanceof Error?n.message:String(n)})}}})();\n', h = typeof self < "u" && self.Blob && new Blob([g], { type: "text/javascript;charset=utf-8" });
3
- function w(e) {
4
- let t;
5
- try {
6
- if (t = h && (self.URL || self.webkitURL).createObjectURL(h), !t) throw "";
7
- const n = new Worker(t, {
8
- name: e == null ? void 0 : e.name
9
- });
10
- return n.addEventListener("error", () => {
11
- (self.URL || self.webkitURL).revokeObjectURL(t);
12
- }), n;
13
- } catch {
14
- return new Worker(
15
- "data:text/javascript;charset=utf-8," + encodeURIComponent(g),
16
- {
17
- name: e == null ? void 0 : e.name
18
- }
19
- );
20
- } finally {
21
- t && (self.URL || self.webkitURL).revokeObjectURL(t);
22
- }
1
+ import { NVWorker as e } from "@niivue/niivue";
2
+ // src/worker.ts?worker&inline
3
+ var t = "(function(){let e={AXIAL:0,CORONAL:1,SAGITTAL:2};function t(t,n){let{dimX:r,dimY:i,dimZ:a}=n;return t===e.AXIAL?{w:r,h:i,depth:a}:t===e.CORONAL?{w:r,h:a,depth:i}:{w:i,h:a,depth:r}}function n(e,t,n,r){return e+t*r.dimX+n*r.dimX*r.dimY}function r(e,t,r,i,a){if(!a)return n(e,t,r,i);let{img2RASstep:o,img2RASstart:s}=a;return s[0]+e*o[0]+s[1]+t*o[1]+s[2]+r*o[2]}function i(t,r,i,a){let{dimX:o,dimY:s,dimZ:c}=a;if(r===e.AXIAL){let e=new Float32Array(o*s),n=t*o*s;for(let t=0;t<e.length;t++)e[t]=i[n+t];return e}if(r===e.CORONAL){let e=new Float32Array(o*c);for(let r=0;r<c;r++)for(let s=0;s<o;s++)e[s+r*o]=i[n(s,t,r,a)];return e}let l=new Float32Array(s*c);for(let e=0;e<c;e++)for(let r=0;r<s;r++)l[r+e*s]=i[n(t,r,e,a)];return l}function a(t,n,i,a,o,s){let{dimX:c,dimY:l,dimZ:u}=a,d=1/o;if(n===e.AXIAL){let e=new Float32Array(c*l);for(let n=0;n<l;n++)for(let o=0;o<c;o++)e[o+n*c]=i[r(o,n,t,a,s)]*d;return e}if(n===e.CORONAL){let e=new Float32Array(c*u);for(let n=0;n<u;n++)for(let o=0;o<c;o++)e[o+n*c]=i[r(o,t,n,a,s)]*d;return e}let f=new Float32Array(l*u);for(let e=0;e<u;e++)for(let n=0;n<l;n++)f[n+e*l]=i[r(t,n,e,a,s)]*d;return f}function o(t,r,i,a,o,s,c){let{dimX:l,dimY:u,dimZ:d}=o;if(i===e.AXIAL){let e=r*l*u;for(let n=0;n<t.length;n++)t[n]>=s&&(a[e+n]=c)}else if(i===e.CORONAL)for(let e=0;e<d;e++)for(let i=0;i<l;i++)t[i+e*l]>=s&&(a[n(i,r,e,o)]=c);else for(let e=0;e<d;e++)for(let i=0;i<u;i++)t[i+e*u]>=s&&(a[n(r,i,e,o)]=c)}function s(e,t,n){if(t<3||n<3)return;let r=new Float32Array(e.length);for(let i=0;i<n;i++)for(let n=0;n<t;n++){let a=n+i*t;r[a]=n===0||n===t-1?e[a]:(e[a-1]+2*e[a]+e[a+1])*.25}for(let i=0;i<n;i++)for(let a=0;a<t;a++){let o=a+i*t;e[o]=i===0||i===n-1?r[o]:(r[o-t]+2*r[o]+r[o+t])*.25}}function c(e,t,n,r){let i=Math.abs(n-e),a=Math.abs(n-t),o=Math.exp(-i*i/(2*r*r)),s=o+Math.exp(-a*a/(2*r*r));return s<1e-6?.5:o/s}function l(e,n,r){let{depth:a}=t(e,r),o=-1,s=-1;for(let t=0;t<a;t++){let a=i(t,e,n,r),c=!1;for(let e=0;e<a.length;e++)if(a[e]>0){c=!0;break}c&&(o===-1&&(o=t),s=t)}return o===-1?null:{first:o,last:s}}function u(n,r,l,u,d,f,p,m){let h=p.sliceType??e.AXIAL,{w:g,h:_,depth:v}=t(h,r),y=v-1,b={intensityWeight:p.intensityWeight??.7,binaryThreshold:p.binaryThreshold??.375,intensitySigma:p.intensitySigma??.1,applySmoothingToSlices:p.applySmoothingToSlices??!0,useIntensityGuided:p.useIntensityGuided??!1};if(d!==void 0&&f!==void 0){if(d>=f)throw Error(`Low slice index must be less than high slice index`);if(d<0||f>y)throw Error(`Slice indices out of bounds [0, ${y}]`)}let x=/* @__PURE__ */ new Map;for(let e=0;e<=y;e++){let t=i(e,h,n,r);for(let n=0;n<t.length;n++){let r=t[n];if(r>0){let t=x.get(r);t?(t.min=Math.min(t.min,e),t.max=Math.max(t.max,e)):x.set(r,{min:e,max:e})}}}for(let[e,t]of x){let p=d===void 0?t.min:Math.max(d,t.min),v=f===void 0?t.max:Math.min(f,t.max);if(p>=v||v-p<2)continue;let y=i(p,h,n,r),x=i(v,h,n,r),S=new Float32Array(y.length),C=new Float32Array(x.length);for(let t=0;t<y.length;t++)S[t]=+(y[t]===e),C[t]=+(x[t]===e);b.applySmoothingToSlices&&(s(S,g,_),s(C,g,_));let w=b.useIntensityGuided&&l,T=w?a(p,h,l,r,u,m):null,E=w?a(v,h,l,r,u,m):null;for(let t=p+1;t<v;t++){let i=new Float32Array(g*_),s=(t-p)/(v-p),d=1-s;if(T&&E&&l){let e=a(t,h,l,r,u,m),n=b.intensityWeight;for(let t=0;t<S.length;t++)if(S[t]>0||C[t]>0){let r=n*c(T[t],E[t],e[t],b.intensitySigma)+(1-n)*d,a=1-r;i[t]=S[t]*r+C[t]*a}else i[t]=S[t]*d+C[t]*s}else for(let e=0;e<S.length;e++)i[e]=S[e]*d+C[e]*s;o(i,t,h,n,r,b.binaryThreshold,e)}}return n}function d(e,t,n,r){return e+t*r.dimX+n*r.dimX*r.dimY}function f(e,t,n,r,i){if(!i)return d(e,t,n,r);let{img2RASstep:a,img2RASstart:o}=i;return o[0]+e*a[0]+o[1]+t*a[1]+o[2]+n*a[2]}let p=[[-1,0,0],[1,0,0],[0,-1,0],[0,1,0],[0,0,-1],[0,0,1]],m=[[-1,-1,0],[1,-1,0],[-1,1,0],[1,1,0],[0,-1,-1],[0,1,-1],[-1,0,-1],[1,0,-1],[0,-1,1],[0,1,1],[-1,0,1],[1,0,1]],h=[[-1,-1,-1],[1,-1,-1],[-1,1,-1],[1,1,-1],[-1,-1,1],[1,-1,1],[-1,1,1],[1,1,1]];function g(e){return e>=26?[...p,...m,...h]:e>=18?[...p,...m]:[...p]}function _(e,t){return Math.abs(e===0?.01:e)*t}function v(e,t,n,r,i={},a,o){let{tolerance:s=0,thresholdMode:c=`symmetric`,percent:l=0,calMin:u=0,calMax:p=1,connectivity:m=6,maxDistanceMM:h=1/0,is2D:v=!1,sliceAxis:y=0,penValue:b=1}=i,{dimX:x,dimY:S,dimZ:C}=n,w=x*S*C,[T,E,D]=e;if(T<0||E<0||D<0||T>=x||E>=S||D>=C)return{filledCount:0,seedIntensity:0,intensityMin:0,intensityMax:0};let O=f(T,E,D,n,a),k=Number(r[O]),A,j,M;if(i.intensityMin!==void 0&&i.intensityMax!==void 0)A=i.intensityMin,j=i.intensityMax;else{let e=c===`auto`?`symmetric`:c;switch(c===`auto`&&(M=k>(u+p)*.5,e=M?`bright`:`dark`),e){case`percent`:{let e=_(k,l);A=k-e,j=k+e;break}case`bright`:l>0?(A=k,j=k+_(k,l)):s>0?(A=k,j=k+s):(A=k,j=1/0);break;case`dark`:l>0?(A=k-_(k,l),j=k):s>0?(A=k-s,j=k):(A=-1/0,j=k);break;default:A=k-s,j=k+s}}let N=g(m),P=o?o[0]:1,F=o?o[1]:1,I=o?o[2]:1,L=h*h;function R(e,t,n){if(!Number.isFinite(L))return!0;let r=(e-T)*P,i=(t-E)*F,a=(n-D)*I;return r*r+i*i+a*a<=L}let z=-1,B=-1;v&&(y===0?(z=2,B=D):y===1?(z=1,B=E):y===2&&(z=0,B=T));let V=new Uint8Array(w),H=d(T,E,D,n);if(k<A||k>j)return{filledCount:0,seedIntensity:k,intensityMin:A,intensityMax:j,isBright:M};let U=[H];V[H]=1;let W=0;for(;U.length>0;){let e=U.shift(),i=Math.floor(e/(x*S)),o=Math.floor((e-i*x*S)/x),s=e-i*x*S-o*x;t[e]=b,W++;for(let[e,t,c]of N){let l=s+e,u=o+t,p=i+c;if(l<0||u<0||p<0||l>=x||u>=S||p>=C||z>=0&&(z===0?l:z===1?u:p)!==B)continue;let m=d(l,u,p,n);if(V[m]||(V[m]=1,!R(l,u,p)))continue;let h=Number(r[f(l,u,p,n,a)]);h<A||h>j||U.push(m)}}return{filledCount:W,seedIntensity:k,intensityMin:A,intensityMax:j,isBright:M}}function y(e,t,n,r,i=6){let{dimX:a,dimY:o,dimZ:s}=n,[c,l,u]=e;if(c<0||l<0||u<0||c>=a||l>=o||u>=s)return 0;let f=d(c,l,u,n),p=t[f];if(p===r||p===0)return 0;let m=a*o*s,h=new Uint8Array(m),_=g(i),v=[f];h[f]=1;let y=0;for(;v.length>0;){let e=v.shift();t[e]=r,y++;let i=Math.floor(e/(a*o)),c=Math.floor((e-i*a*o)/a),l=e-i*a*o-c*a;for(let[e,r,u]of _){let f=l+e,m=c+r,g=i+u;if(f<0||m<0||g<0||f>=a||m>=o||g>=s)continue;let _=d(f,m,g,n);h[_]||(h[_]=1,t[_]===p&&v.push(_))}}return y}let b=self.postMessage.bind(self),x={findBoundarySlices(e){let t=e.sliceType,n=new Uint8Array(e.drawBitmap),r=e.dims;return{result:l(t,n,r)}},interpolateMaskSlices(e){let t=new Uint8Array(e.drawBitmap),n=e.dims,r=e.imageData?new Float32Array(e.imageData):null,i=e.maxVal??1,a=e.sliceIndexLow,o=e.sliceIndexHigh;return{drawBitmap:u(t,n,r,i,a,o,e.options??{},e.rasMap??void 0).buffer}},magicWand(e){let t=e.seed,n=new Uint8Array(e.drawBitmap),r=e.dims,i=v(t,n,r,new Float32Array(e.imageData),e.options??{},e.rasMap??void 0,e.voxelSizeMM??void 0);return{drawBitmap:n.buffer,result:i}},magicWandFromBitmap(e){let t=e.seed,n=new Uint8Array(e.drawBitmap),r=e.dims,i=y(t,n,r,e.newColor??0,e.connectivity??6);return{drawBitmap:n.buffer,count:i}}},S=null,C=null,w=null,T=null,E,D;self.onmessage=e=>{let t=e.data;if(t.type===`initShared`){S=new Uint8Array(t.workingBuffer),C=new Float32Array(t.imageBuffer),w=new Uint8Array(t.committedBuffer),T=t.dims,E=t.rasMap??void 0,D=t.voxelSizeMM??void 0,b({type:`initSharedDone`});return}if(t.type===`updateCommitted`){w=new Uint8Array(t.committed);return}if(t.type===`magicWandShared`){if(!S||!C||!w||!T){b({type:`magicWandSharedResult`,error:`Not initialized`,gen:t.gen});return}S.set(w);let e=v(t.seed,S,T,C,t.options??{},E,D);b({type:`magicWandSharedResult`,result:e,gen:t.gen});return}let{_wbId:n,name:r,...i}=t,a=x[r];if(!a)b({_wbId:n,_wbError:`Unknown operation: ${r}`});else try{let e=a(i),t=[];for(let n of Object.values(e))n instanceof ArrayBuffer&&t.push(n);b({_wbId:n,...e},t)}catch(e){b({_wbId:n,_wbError:e instanceof Error?e.message:String(e)})}}})();", n = typeof self < "u" && self.Blob && new Blob(["(self.URL || self.webkitURL).revokeObjectURL(self.location.href);", t], { type: "text/javascript;charset=utf-8" });
4
+ function r(e) {
5
+ let r;
6
+ try {
7
+ if (r = n && (self.URL || self.webkitURL).createObjectURL(n), !r) throw "";
8
+ let t = new Worker(r, { name: e?.name });
9
+ return t.addEventListener("error", () => {
10
+ (self.URL || self.webkitURL).revokeObjectURL(r);
11
+ }), t;
12
+ } catch {
13
+ return new Worker("data:text/javascript;charset=utf-8," + encodeURIComponent(t), { name: e?.name });
14
+ }
23
15
  }
24
- function y(e) {
25
- if (e instanceof Float32Array) return e;
26
- const t = new Float32Array(e.length);
27
- for (let n = 0; n < e.length; n++) t[n] = e[n];
28
- return t;
16
+ // src/index.ts
17
+ function i(e) {
18
+ if (e instanceof Float32Array) return e;
19
+ let t = new Float32Array(e.length);
20
+ for (let n = 0; n < e.length; n++) t[n] = e[n];
21
+ return t;
29
22
  }
30
- let m = null;
31
- function d() {
32
- return m || (m = new p(() => new w())), m;
23
+ var a = null;
24
+ function o() {
25
+ return a ||= new e(() => new r()), a;
33
26
  }
34
- async function M(e, t, n) {
35
- const r = t.buffer.slice(
36
- t.byteOffset,
37
- t.byteOffset + t.byteLength
38
- );
39
- return (await d().execute(
40
- {
41
- name: "findBoundarySlices",
42
- sliceType: e,
43
- drawBitmap: r,
44
- dims: n
45
- },
46
- [r]
47
- )).result;
27
+ async function s(e, t, n) {
28
+ let r = t.buffer.slice(t.byteOffset, t.byteOffset + t.byteLength);
29
+ return (await o().execute({
30
+ name: "findBoundarySlices",
31
+ sliceType: e,
32
+ drawBitmap: r,
33
+ dims: n
34
+ }, [r])).result;
48
35
  }
49
- async function S(e, t, n, r, o, s, i) {
50
- const a = e.buffer.slice(
51
- e.byteOffset,
52
- e.byteOffset + e.byteLength
53
- ), c = [a];
54
- let f = null;
55
- if (n) {
56
- const l = y(n);
57
- f = l.buffer.slice(
58
- l.byteOffset,
59
- l.byteOffset + l.byteLength
60
- ), c.push(f);
61
- }
62
- const u = await d().execute(
63
- {
64
- name: "interpolateMaskSlices",
65
- drawBitmap: a,
66
- dims: t,
67
- imageData: f,
68
- maxVal: r,
69
- sliceIndexLow: o,
70
- sliceIndexHigh: s,
71
- options: i ?? {},
72
- rasMap: null
73
- },
74
- c
75
- );
76
- return new Uint8Array(u.drawBitmap);
36
+ async function c(e, t, n, r, a, s, c) {
37
+ let l = e.buffer.slice(e.byteOffset, e.byteOffset + e.byteLength), u = [l], d = null;
38
+ if (n) {
39
+ let e = i(n);
40
+ d = e.buffer.slice(e.byteOffset, e.byteOffset + e.byteLength), u.push(d);
41
+ }
42
+ let f = await o().execute({
43
+ name: "interpolateMaskSlices",
44
+ drawBitmap: l,
45
+ dims: t,
46
+ imageData: d,
47
+ maxVal: r,
48
+ sliceIndexLow: a,
49
+ sliceIndexHigh: s,
50
+ options: c ?? {},
51
+ rasMap: null
52
+ }, u);
53
+ return new Uint8Array(f.drawBitmap);
77
54
  }
78
- async function x(e, t, n, r, o, s) {
79
- const i = t.buffer.slice(
80
- t.byteOffset,
81
- t.byteOffset + t.byteLength
82
- ), a = y(r), c = a.buffer.slice(
83
- a.byteOffset,
84
- a.byteOffset + a.byteLength
85
- ), f = [i, c], u = await d().execute(
86
- {
87
- name: "magicWand",
88
- seed: e,
89
- drawBitmap: i,
90
- dims: n,
91
- imageData: c,
92
- options: o ?? {},
93
- rasMap: null,
94
- voxelSizeMM: s ?? null
95
- },
96
- f
97
- );
98
- return { bitmap: new Uint8Array(u.drawBitmap), result: u.result };
55
+ async function l(e, t, n, r, a, s) {
56
+ let c = t.buffer.slice(t.byteOffset, t.byteOffset + t.byteLength), l = i(r), u = l.buffer.slice(l.byteOffset, l.byteOffset + l.byteLength), d = [c, u], f = await o().execute({
57
+ name: "magicWand",
58
+ seed: e,
59
+ drawBitmap: c,
60
+ dims: n,
61
+ imageData: u,
62
+ options: a ?? {},
63
+ rasMap: null,
64
+ voxelSizeMM: s ?? null
65
+ }, d);
66
+ return {
67
+ bitmap: new Uint8Array(f.drawBitmap),
68
+ result: f.result
69
+ };
99
70
  }
100
- async function I(e, t, n, r, o = 6) {
101
- const s = t.buffer.slice(
102
- t.byteOffset,
103
- t.byteOffset + t.byteLength
104
- ), i = await d().execute(
105
- {
106
- name: "magicWandFromBitmap",
107
- seed: e,
108
- drawBitmap: s,
109
- dims: n,
110
- newColor: r,
111
- connectivity: o
112
- },
113
- [s]
114
- );
115
- return { bitmap: new Uint8Array(i.drawBitmap), count: i.count };
71
+ async function u(e, t, n, r, i = 6) {
72
+ let a = t.buffer.slice(t.byteOffset, t.byteOffset + t.byteLength), s = await o().execute({
73
+ name: "magicWandFromBitmap",
74
+ seed: e,
75
+ drawBitmap: a,
76
+ dims: n,
77
+ newColor: r,
78
+ connectivity: i
79
+ }, [a]);
80
+ return {
81
+ bitmap: new Uint8Array(s.drawBitmap),
82
+ count: s.count
83
+ };
116
84
  }
117
- class L {
118
- constructor(t, n, r, o) {
119
- this._gen = 0;
120
- const s = t.dimX * t.dimY * t.dimZ, i = new SharedArrayBuffer(s);
121
- this.bitmap = new Uint8Array(i), this.bitmap.set(r);
122
- const a = y(n), c = new SharedArrayBuffer(a.byteLength);
123
- new Float32Array(c).set(a);
124
- const f = r.slice();
125
- this._worker = new w(), this.ready = new Promise((u) => {
126
- const l = (b) => {
127
- b.data.type === "initSharedDone" && (this._worker.removeEventListener("message", l), u());
128
- };
129
- this._worker.addEventListener("message", l);
130
- }), this._worker.postMessage(
131
- {
132
- type: "initShared",
133
- workingBuffer: i,
134
- imageBuffer: c,
135
- committedBuffer: f.buffer,
136
- dims: t,
137
- rasMap: null,
138
- voxelSizeMM: o ?? null
139
- },
140
- [f.buffer]
141
- );
142
- }
143
- /**
144
- * Send a new committed bitmap to the worker.
145
- * Call after commit, undo, or clear.
146
- */
147
- updateCommitted(t) {
148
- const n = t.slice();
149
- this._worker.postMessage(
150
- { type: "updateCommitted", committed: n.buffer },
151
- [n.buffer]
152
- );
153
- }
154
- /**
155
- * Run a magic wand preview. The worker restores the committed bitmap
156
- * into the shared working buffer, runs the flood fill, and returns
157
- * the result summary. By the time the promise resolves, `this.bitmap`
158
- * already contains the updated drawing.
159
- *
160
- * Returns `null` if a newer preview superseded this one.
161
- */
162
- preview(t, n) {
163
- const r = ++this._gen;
164
- return new Promise((o) => {
165
- const s = (i) => {
166
- if (i.data.type === "magicWandSharedResult") {
167
- if (this._worker.removeEventListener("message", s), i.data.gen !== r) {
168
- o(null);
169
- return;
170
- }
171
- o(i.data.result);
172
- }
173
- };
174
- this._worker.addEventListener("message", s), this._worker.postMessage({
175
- type: "magicWandShared",
176
- seed: t,
177
- options: n ?? {},
178
- gen: r
179
- });
180
- });
181
- }
182
- /** Terminate the worker and release resources. */
183
- dispose() {
184
- this._worker.terminate();
185
- }
186
- }
187
- export {
188
- L as MagicWandShared,
189
- M as findDrawingBoundarySlices,
190
- S as interpolateMaskSlices,
191
- x as magicWand,
192
- I as magicWandFromBitmap
85
+ var d = class {
86
+ constructor(e, t, n, a) {
87
+ this._gen = 0;
88
+ let o = e.dimX * e.dimY * e.dimZ, s = new SharedArrayBuffer(o);
89
+ this.bitmap = new Uint8Array(s), this.bitmap.set(n);
90
+ let c = i(t), l = new SharedArrayBuffer(c.byteLength);
91
+ new Float32Array(l).set(c);
92
+ let u = n.slice();
93
+ this._worker = new r(), this.ready = new Promise((e) => {
94
+ let t = (n) => {
95
+ n.data.type === "initSharedDone" && (this._worker.removeEventListener("message", t), e());
96
+ };
97
+ this._worker.addEventListener("message", t);
98
+ }), this._worker.postMessage({
99
+ type: "initShared",
100
+ workingBuffer: s,
101
+ imageBuffer: l,
102
+ committedBuffer: u.buffer,
103
+ dims: e,
104
+ rasMap: null,
105
+ voxelSizeMM: a ?? null
106
+ }, [u.buffer]);
107
+ }
108
+ updateCommitted(e) {
109
+ let t = e.slice();
110
+ this._worker.postMessage({
111
+ type: "updateCommitted",
112
+ committed: t.buffer
113
+ }, [t.buffer]);
114
+ }
115
+ preview(e, t) {
116
+ let n = ++this._gen;
117
+ return new Promise((r) => {
118
+ let i = (e) => {
119
+ e.data.type === "magicWandSharedResult" && (this._worker.removeEventListener("message", i), e.data.gen === n ? r(e.data.result) : r(null));
120
+ };
121
+ this._worker.addEventListener("message", i), this._worker.postMessage({
122
+ type: "magicWandShared",
123
+ seed: e,
124
+ options: t ?? {},
125
+ gen: n
126
+ });
127
+ });
128
+ }
129
+ dispose() {
130
+ this._worker.terminate();
131
+ }
193
132
  };
133
+ export { d as MagicWandShared, s as findDrawingBoundarySlices, c as interpolateMaskSlices, l as magicWand, u as magicWandFromBitmap };
@@ -5,7 +5,7 @@ export type Connectivity = 6 | 18 | 26;
5
5
  * Threshold mode controls how the intensity range is computed from the seed.
6
6
  *
7
7
  * - `"symmetric"` (default) — range = `[seed - tolerance, seed + tolerance]`.
8
- * - `"percent"` — range = `[seed * (1 - percent), seed * (1 + percent)]`.
8
+ * - `"percent"` — range = `seed ± |seed| * percent` (a zero seed uses 0.01).
9
9
  * Requires {@link MagicWandOptions.percent}.
10
10
  * - `"bright"` — only voxels **≥** seed are included:
11
11
  * range = `[seed, seed + tolerance]` (or `[seed, Infinity]` when tolerance is 0).
@@ -37,19 +37,22 @@ export interface MagicWandOptions {
37
37
  */
38
38
  thresholdMode?: ThresholdMode;
39
39
  /**
40
- * Percentage threshold (0–1) used when `thresholdMode` is `"percent"` or
41
- * `"auto"`. Range becomes `[seed * (1-pct), seed * (1+pct)]`.
40
+ * Percentage threshold (0–1) used when `thresholdMode` is `"percent"`
41
+ * (range `seed ± |seed|*pct`), or `"bright"`, `"dark"` or
42
+ * `"auto"` (one side only: `[seed, seed + |seed|*pct]` or `[seed - |seed|*pct, seed]`).
42
43
  * Equivalent to `clickToSegmentPercent` in the old niivue API.
43
44
  */
44
45
  percent?: number;
45
46
  /**
46
47
  * Volume calibration minimum — needed for `thresholdMode: "auto"` to
47
- * determine bright vs dark. Equivalent to `back.cal_min`.
48
+ * determine bright vs dark. In the image data's raw units, so
49
+ * `back.cal_min` (scaled by scl_slope/scl_inter) must be converted back.
48
50
  */
49
51
  calMin?: number;
50
52
  /**
51
53
  * Volume calibration maximum — needed for `thresholdMode: "auto"` to
52
- * determine bright vs dark. Equivalent to `back.cal_max`.
54
+ * determine bright vs dark. In the image data's raw units, so
55
+ * `back.cal_max` (scaled by scl_slope/scl_inter) must be converted back.
53
56
  */
54
57
  calMax?: number;
55
58
  /** Neighbour connectivity (default 6). */
@@ -75,7 +78,8 @@ export interface MagicWandResult {
75
78
  intensityMax: number;
76
79
  /**
77
80
  * Whether the auto-detect resolved to "bright" (`true`) or "dark" (`false`).
78
- * Only meaningful when `thresholdMode` was `"auto"`.
81
+ * Only meaningful when `thresholdMode` was `"auto"`. In raw units: with a
82
+ * negative scl_slope, raw "bright" is the calibrated dark side.
79
83
  */
80
84
  isBright?: boolean;
81
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@niivue/nv-ext-drawing",
3
- "version": "1.0.0-rc.21",
3
+ "version": "1.0.0-rc.210",
4
4
  "type": "module",
5
5
  "description": "Drawing interpolation and segmentation tools for NiiVue",
6
6
  "main": "./dist/nv-ext-drawing.js",
@@ -16,12 +16,12 @@
16
16
  "dist"
17
17
  ],
18
18
  "peerDependencies": {
19
- "@niivue/niivue": "1.0.0-rc.21"
19
+ "@niivue/niivue": "1.0.0-rc.208"
20
20
  },
21
21
  "devDependencies": {
22
- "@niivue/niivue": "1.0.0-rc.21",
23
- "typescript": "~5.8.3",
24
- "vite": "^6.3.2",
22
+ "@niivue/niivue": "1.0.0-rc.208",
23
+ "typescript": "^5.9.3",
24
+ "vite": "^8.3.8",
25
25
  "vite-plugin-dts": "^4.5.4"
26
26
  },
27
27
  "scripts": {