copper3d 3.7.4 → 3.8.1
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/dist/Controls/cameraTransitions.d.ts +87 -0
- package/dist/Controls/cameraTransitions.js +285 -0
- package/dist/Controls/cameraTransitions.js.map +1 -0
- package/dist/Controls/fitView.d.ts +45 -0
- package/dist/Controls/fitView.js +40 -0
- package/dist/Controls/fitView.js.map +1 -0
- package/dist/Controls/orbitFraming.d.ts +39 -0
- package/dist/Controls/orbitFraming.js +39 -0
- package/dist/Controls/orbitFraming.js.map +1 -1
- package/dist/Loader/fastSliceRepaint.d.ts +56 -0
- package/dist/Loader/fastSliceRepaint.js +159 -0
- package/dist/Loader/fastSliceRepaint.js.map +1 -0
- package/dist/Renderer/copperRendererOnDemond.d.ts +10 -2
- package/dist/Renderer/copperRendererOnDemond.js +14 -0
- package/dist/Renderer/copperRendererOnDemond.js.map +1 -1
- package/dist/Renderer/disposeScene.d.ts +47 -0
- package/dist/Renderer/disposeScene.js +46 -0
- package/dist/Renderer/disposeScene.js.map +1 -0
- package/dist/Renderer/sceneBudget.d.ts +40 -0
- package/dist/Renderer/sceneBudget.js +86 -0
- package/dist/Renderer/sceneBudget.js.map +1 -0
- package/dist/Scene/copperSceneOnDemond.d.ts +12 -0
- package/dist/Scene/copperSceneOnDemond.js +18 -0
- package/dist/Scene/copperSceneOnDemond.js.map +1 -1
- package/dist/Utils/dispose.d.ts +48 -0
- package/dist/Utils/dispose.js +60 -0
- package/dist/Utils/dispose.js.map +1 -0
- package/dist/Utils/modelCrossfade.d.ts +36 -0
- package/dist/Utils/modelCrossfade.js +46 -0
- package/dist/Utils/modelCrossfade.js.map +1 -0
- package/dist/Utils/segmentation/coreTools/divControlTools.js +19 -0
- package/dist/Utils/segmentation/coreTools/divControlTools.js.map +1 -1
- package/dist/Utils/volumeExposure.d.ts +32 -0
- package/dist/Utils/volumeExposure.js +104 -0
- package/dist/Utils/volumeExposure.js.map +1 -0
- package/dist/bundle.esm.js +31521 -10006
- package/dist/bundle.umd.js +31549 -10012
- package/dist/index.d.ts +19 -2
- package/dist/index.js +10 -1
- package/dist/index.js.map +1 -1
- package/dist/types/Controls/cameraTransitions.d.ts +87 -0
- package/dist/types/Controls/fitView.d.ts +45 -0
- package/dist/types/Controls/orbitFraming.d.ts +39 -0
- package/dist/types/Loader/fastSliceRepaint.d.ts +56 -0
- package/dist/types/Renderer/copperRendererOnDemond.d.ts +10 -2
- package/dist/types/Renderer/disposeScene.d.ts +47 -0
- package/dist/types/Renderer/sceneBudget.d.ts +40 -0
- package/dist/types/Scene/copperSceneOnDemond.d.ts +12 -0
- package/dist/types/Utils/dispose.d.ts +48 -0
- package/dist/types/Utils/modelCrossfade.d.ts +36 -0
- package/dist/types/Utils/volumeExposure.d.ts +32 -0
- package/dist/types/index.d.ts +19 -2
- package/package.json +7 -3
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { DisposableObject3D } from './dispose';
|
|
2
|
+
/**
|
|
3
|
+
* Crossfading one model into another in the same scene.
|
|
4
|
+
*
|
|
5
|
+
* Split into capture / apply / restore so the caller owns the timing: the
|
|
6
|
+
* fade is driven frame by frame from whatever animation loop it already has,
|
|
7
|
+
* rather than this starting a second one.
|
|
8
|
+
*/
|
|
9
|
+
export interface FadeableMaterial {
|
|
10
|
+
opacity: number;
|
|
11
|
+
transparent: boolean;
|
|
12
|
+
depthWrite: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** One mesh material enrolled in a crossfade, together with the appearance it
|
|
15
|
+
* had before the fade started. */
|
|
16
|
+
export interface FadeTarget {
|
|
17
|
+
material: FadeableMaterial;
|
|
18
|
+
opacity: number;
|
|
19
|
+
transparent: boolean;
|
|
20
|
+
depthWrite: boolean;
|
|
21
|
+
}
|
|
22
|
+
export declare function collectFadeTargets(root: DisposableObject3D): FadeTarget[];
|
|
23
|
+
/**
|
|
24
|
+
* Scales each material toward transparent by `factor`, RELATIVE to the
|
|
25
|
+
* opacity it already had. Scaling rather than assigning is what keeps an
|
|
26
|
+
* already-translucent material translucent: `opacity = factor` would end
|
|
27
|
+
* every crossfade with it fully opaque.
|
|
28
|
+
*
|
|
29
|
+
* `depthWrite` is suppressed for the whole fade and only restored at the
|
|
30
|
+
* fully-opaque end. A partially transparent mesh that still writes depth
|
|
31
|
+
* occludes everything drawn behind it, so a fade with depth writing left on
|
|
32
|
+
* shows the outgoing model punching holes in the incoming one.
|
|
33
|
+
*/
|
|
34
|
+
export declare function setFade(targets: FadeTarget[], factor: number): void;
|
|
35
|
+
/** Puts each material back exactly as `collectFadeTargets` found it. */
|
|
36
|
+
export declare function restoreFade(targets: FadeTarget[]): void;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
export function collectFadeTargets(root) {
|
|
2
|
+
const targets = [];
|
|
3
|
+
root.traverse((child) => {
|
|
4
|
+
if (!(child === null || child === void 0 ? void 0 : child.isMesh) || !child.material)
|
|
5
|
+
return;
|
|
6
|
+
const materials = Array.isArray(child.material)
|
|
7
|
+
? child.material
|
|
8
|
+
: [child.material];
|
|
9
|
+
for (const material of materials) {
|
|
10
|
+
targets.push({
|
|
11
|
+
material,
|
|
12
|
+
opacity: material.opacity,
|
|
13
|
+
transparent: material.transparent,
|
|
14
|
+
depthWrite: material.depthWrite,
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
return targets;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Scales each material toward transparent by `factor`, RELATIVE to the
|
|
22
|
+
* opacity it already had. Scaling rather than assigning is what keeps an
|
|
23
|
+
* already-translucent material translucent: `opacity = factor` would end
|
|
24
|
+
* every crossfade with it fully opaque.
|
|
25
|
+
*
|
|
26
|
+
* `depthWrite` is suppressed for the whole fade and only restored at the
|
|
27
|
+
* fully-opaque end. A partially transparent mesh that still writes depth
|
|
28
|
+
* occludes everything drawn behind it, so a fade with depth writing left on
|
|
29
|
+
* shows the outgoing model punching holes in the incoming one.
|
|
30
|
+
*/
|
|
31
|
+
export function setFade(targets, factor) {
|
|
32
|
+
for (const t of targets) {
|
|
33
|
+
t.material.transparent = true;
|
|
34
|
+
t.material.opacity = t.opacity * factor;
|
|
35
|
+
t.material.depthWrite = t.depthWrite && factor >= 1;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/** Puts each material back exactly as `collectFadeTargets` found it. */
|
|
39
|
+
export function restoreFade(targets) {
|
|
40
|
+
for (const t of targets) {
|
|
41
|
+
t.material.transparent = t.transparent;
|
|
42
|
+
t.material.opacity = t.opacity;
|
|
43
|
+
t.material.depthWrite = t.depthWrite;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=modelCrossfade.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modelCrossfade.js","sourceRoot":"","sources":["../../src/Utils/modelCrossfade.ts"],"names":[],"mappings":"AAyBA,MAAM,UAAU,kBAAkB,CAAC,IAAwB;IACzD,MAAM,OAAO,GAAiB,EAAE,CAAA;IAChC,IAAI,CAAC,QAAQ,CAAC,CAAC,KAAK,EAAE,EAAE;QACtB,IAAI,CAAC,CAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,MAAM,CAAA,IAAI,CAAC,KAAK,CAAC,QAAQ;YAAE,OAAM;QAC7C,MAAM,SAAS,GAAuB,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,QAAQ,CAAC;YACjE,CAAC,CAAC,KAAK,CAAC,QAAQ;YAChB,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;QACpB,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,OAAO,CAAC,IAAI,CAAC;gBACX,QAAQ;gBACR,OAAO,EAAE,QAAQ,CAAC,OAAO;gBACzB,WAAW,EAAE,QAAQ,CAAC,WAAW;gBACjC,UAAU,EAAE,QAAQ,CAAC,UAAU;aAChC,CAAC,CAAA;QACJ,CAAC;IACH,CAAC,CAAC,CAAA;IACF,OAAO,OAAO,CAAA;AAChB,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,OAAO,CAAC,OAAqB,EAAE,MAAc;IAC3D,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,CAAC,CAAC,QAAQ,CAAC,WAAW,GAAG,IAAI,CAAA;QAC7B,CAAC,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,GAAG,MAAM,CAAA;QACvC,CAAC,CAAC,QAAQ,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,IAAI,MAAM,IAAI,CAAC,CAAA;IACrD,CAAC;AACH,CAAC;AAED,wEAAwE;AACxE,MAAM,UAAU,WAAW,CAAC,OAAqB;IAC/C,KAAK,MAAM,CAAC,IAAI,OAAO,EAAE,CAAC;QACxB,CAAC,CAAC,QAAQ,CAAC,WAAW,GAAG,CAAC,CAAC,WAAW,CAAA;QACtC,CAAC,CAAC,QAAQ,CAAC,OAAO,GAAG,CAAC,CAAC,OAAO,CAAA;QAC9B,CAAC,CAAC,QAAQ,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAA;IACtC,CAAC;AACH,CAAC"}
|
|
@@ -13,6 +13,18 @@ function createShowSliceNumberDiv() {
|
|
|
13
13
|
sliceNumberDiv.style.left = "100px";
|
|
14
14
|
return sliceNumberDiv;
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Whether an element accepts typed input, and therefore must not have focus taken
|
|
18
|
+
* away from it.
|
|
19
|
+
*/
|
|
20
|
+
function isEditableElement(element) {
|
|
21
|
+
if (!element)
|
|
22
|
+
return false;
|
|
23
|
+
const tag = element.tagName;
|
|
24
|
+
if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT")
|
|
25
|
+
return true;
|
|
26
|
+
return element.isContentEditable === true;
|
|
27
|
+
}
|
|
16
28
|
/**
|
|
17
29
|
* set mouse to auto focus on the div
|
|
18
30
|
* @param container the operate container div
|
|
@@ -20,6 +32,13 @@ function createShowSliceNumberDiv() {
|
|
|
20
32
|
function autoFocusDiv(container) {
|
|
21
33
|
container.tabIndex = 10;
|
|
22
34
|
container.addEventListener("mouseover", () => {
|
|
35
|
+
// Never take focus from something the user is typing into. The container claims
|
|
36
|
+
// focus so its keyboard shortcuts work once the pointer is over the canvas, but
|
|
37
|
+
// hovering is passive: a form field anywhere else on the page would be blurred the
|
|
38
|
+
// moment the pointer drifted off it, which makes the field impossible to type in.
|
|
39
|
+
// Clicking the canvas still focuses it, so the shortcuts are unaffected.
|
|
40
|
+
if (isEditableElement(document.activeElement))
|
|
41
|
+
return;
|
|
23
42
|
container.focus();
|
|
24
43
|
});
|
|
25
44
|
container.style.outline = "none";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"divControlTools.js","sourceRoot":"","sources":["../../../../src/Utils/segmentation/coreTools/divControlTools.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,SAAS,wBAAwB;IAC/B,MAAM,cAAc,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACrD,cAAc,CAAC,SAAS,GAAG,sBAAsB,CAAC;IAClD,cAAc,CAAC,KAAK,CAAC,QAAQ,GAAG,UAAU,CAAC;IAC3C,cAAc,CAAC,KAAK,CAAC,MAAM,GAAG,KAAK,CAAC;IACpC,cAAc,CAAC,KAAK,CAAC,GAAG,GAAG,MAAM,CAAC;IAClC,cAAc,CAAC,KAAK,CAAC,IAAI,GAAG,OAAO,CAAC;IAEpC,OAAO,cAAc,CAAC;AACxB,CAAC;AAED;;;GAGG;AACH,SAAS,YAAY,CAAC,SAAyB;IAC7C,SAAS,CAAC,QAAQ,GAAG,EAAE,CAAC;IACxB,SAAS,CAAC,gBAAgB,CAAC,WAAW,EAAE,GAAG,EAAE;QAC3C,SAAS,CAAC,KAAK,EAAE,CAAC;IACpB,CAAC,CAAC,CAAC;IACH,SAAS,CAAC,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;AACnC,CAAC;AAED;;;GAGG;AACH,SAAS,cAAc,CAAC,MAA4B;IAClD,IAAI,cAAc,GAChB,QAAQ,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;IACnC,IAAI,aAAa,GAA6B,QAAQ,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;IAC1E,aAAa,CAAC,IAAI,GAAG,EAAE,CAAC;IACxB,aAAa,CAAC,MAAM,GAAG,QAAQ,CAAC;IAChC,aAAa,CAAC,QAAQ,GAAG,SAAS,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,iBAAiB,EAAE,CAAC;IAC7E,MAAM,WAAW,GAAG,cAAc,CAAC,UAAU,CAC3C,IAAI,CACuB,CAAC;IAC9B,cAAc,CAAC,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC;IAC1C,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC;IAE5C,WAAW,CAAC,SAAS,CACnB,MAAM,CAAC,aAAa,EACpB,CAAC,EACD,CAAC,EACD,MAAM,CAAC,WAAW,EAClB,MAAM,CAAC,YAAY,CACpB,CAAC;IACF,aAAa,CAAC,IAAI,GAAG,cAAc,CAAC,SAAS,EAAE,CAAC;IAChD,aAAa,CAAC,KAAK,EAAE,CAAC;IAEtB,qCAAqC;IACrC,cAAc,GAAG,IAAI,CAAC;IACtB,aAAa,GAAG,IAAI,CAAC;AACvB,CAAC;AAED,OAAO,EAAE,wBAAwB,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC"}
|
|
1
|
+
{"version":3,"file":"divControlTools.js","sourceRoot":"","sources":["../../../../src/Utils/segmentation/coreTools/divControlTools.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,SAAS,wBAAwB;IAC/B,MAAM,cAAc,GAAG,QAAQ,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACrD,cAAc,CAAC,SAAS,GAAG,sBAAsB,CAAC;IAClD,cAAc,CAAC,KAAK,CAAC,QAAQ,GA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AAG,SAAS,MAAM,CAAC,IAAI,KAAK,MAAM,CAAC,iBAAiB,EAAE,CAAC;IAC7E,MAAM,WAAW,GAAG,cAAc,CAAC,UAAU,CAC3C,IAAI,CACuB,CAAC;IAC9B,cAAc,CAAC,KAAK,GAAG,MAAM,CAAC,WAAW,CAAC;IAC1C,cAAc,CAAC,MAAM,GAAG,MAAM,CAAC,YAAY,CAAC;IAE5C,WAAW,CAAC,SAAS,CACnB,MAAM,CAAC,aAAa,EACpB,CAAC,EACD,CAAC,EACD,MAAM,CAAC,WAAW,EAClB,MAAM,CAAC,YAAY,CACpB,CAAC;IACF,aAAa,CAAC,IAAI,GAAG,cAAc,CAAC,SAAS,EAAE,CAAC;IAChD,aAAa,CAAC,KAAK,EAAE,CAAC;IAEtB,qCAAqC;IACrC,cAAc,GAAG,IAAI,CAAC;IACtB,aAAa,GAAG,IAAI,CAAC;AACvB,CAAC;AAED,OAAO,EAAE,wBAAwB,EAAE,YAAY,EAAE,cAAc,EAAE,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How much to lift a volume's mid-tones so its tissue is actually readable.
|
|
3
|
+
*
|
|
4
|
+
* A slice is painted with `(raw - windowLow) * 255 / (windowHigh - windowLow)`
|
|
5
|
+
* and the NRRD loader sets that window to the volume's own min/max. When the
|
|
6
|
+
* max is a handful of bright outliers -- normal on a contrast-enhanced MRI --
|
|
7
|
+
* the tissue ends up in the bottom of the range and the image reads as dark.
|
|
8
|
+
*
|
|
9
|
+
* The answer here is a gamma curve, `out = 255 * (in / 255) ** exponent`,
|
|
10
|
+
* applied on top of that window rather than replacing it. Narrowing the window
|
|
11
|
+
* instead cannot work: any linear window bright enough to lift the tissue
|
|
12
|
+
* saturates the top of the range, and on a contrast-enhanced study the lesion
|
|
13
|
+
* IS the top of the range. Gamma is monotonic and fixes both ends, so nothing
|
|
14
|
+
* clips however far the middle is raised.
|
|
15
|
+
*/
|
|
16
|
+
/** The parts of a loaded volume this reads. Structural rather than the
|
|
17
|
+
* loader's own type, so it is testable without building one. */
|
|
18
|
+
export interface ExposureVolume {
|
|
19
|
+
data: ArrayLike<number>;
|
|
20
|
+
min: number;
|
|
21
|
+
max: number;
|
|
22
|
+
}
|
|
23
|
+
/** Where the median tissue voxel should land on the 0-255 greyscale. */
|
|
24
|
+
export declare const DEFAULT_TARGET_GREY = 75;
|
|
25
|
+
/**
|
|
26
|
+
* The gamma exponent that puts `volume`'s median tissue voxel on
|
|
27
|
+
* `targetGrey`, or 1 when it needs no lift or cannot be measured.
|
|
28
|
+
*
|
|
29
|
+
* Solving per volume is what makes volumes with wildly different maxima come
|
|
30
|
+
* out looking alike.
|
|
31
|
+
*/
|
|
32
|
+
export declare function exposureExponent(volume: ExposureVolume, targetGrey?: number): number;
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* How much to lift a volume's mid-tones so its tissue is actually readable.
|
|
3
|
+
*
|
|
4
|
+
* A slice is painted with `(raw - windowLow) * 255 / (windowHigh - windowLow)`
|
|
5
|
+
* and the NRRD loader sets that window to the volume's own min/max. When the
|
|
6
|
+
* max is a handful of bright outliers -- normal on a contrast-enhanced MRI --
|
|
7
|
+
* the tissue ends up in the bottom of the range and the image reads as dark.
|
|
8
|
+
*
|
|
9
|
+
* The answer here is a gamma curve, `out = 255 * (in / 255) ** exponent`,
|
|
10
|
+
* applied on top of that window rather than replacing it. Narrowing the window
|
|
11
|
+
* instead cannot work: any linear window bright enough to lift the tissue
|
|
12
|
+
* saturates the top of the range, and on a contrast-enhanced study the lesion
|
|
13
|
+
* IS the top of the range. Gamma is monotonic and fixes both ends, so nothing
|
|
14
|
+
* clips however far the middle is raised.
|
|
15
|
+
*/
|
|
16
|
+
/** Where the median tissue voxel should land on the 0-255 greyscale. */
|
|
17
|
+
export const DEFAULT_TARGET_GREY = 75;
|
|
18
|
+
const BINS = 4096;
|
|
19
|
+
/** Volumes run to ~35M voxels and a full pass costs ~100ms. A uniform stride
|
|
20
|
+
* is unbiased for the intensity distribution. */
|
|
21
|
+
const MAX_SAMPLES = 4000000;
|
|
22
|
+
/**
|
|
23
|
+
* Otsu's threshold: the bin that best separates the histogram into two
|
|
24
|
+
* classes. Used to tell air from tissue.
|
|
25
|
+
*
|
|
26
|
+
* A fraction of the range would be simpler, but the range is exactly what
|
|
27
|
+
* volumes disagree on -- maxima two orders of magnitude apart are ordinary --
|
|
28
|
+
* so any threshold derived from `max` lands somewhere different in each.
|
|
29
|
+
*/
|
|
30
|
+
function otsuBin(hist, total) {
|
|
31
|
+
let sum = 0;
|
|
32
|
+
for (let b = 0; b < BINS; b++)
|
|
33
|
+
sum += b * hist[b];
|
|
34
|
+
let sumBelow = 0;
|
|
35
|
+
let countBelow = 0;
|
|
36
|
+
let bestVariance = 0;
|
|
37
|
+
let threshold = 0;
|
|
38
|
+
for (let b = 0; b < BINS; b++) {
|
|
39
|
+
countBelow += hist[b];
|
|
40
|
+
if (countBelow === 0)
|
|
41
|
+
continue;
|
|
42
|
+
const countAbove = total - countBelow;
|
|
43
|
+
if (countAbove === 0)
|
|
44
|
+
break;
|
|
45
|
+
sumBelow += b * hist[b];
|
|
46
|
+
const meanBelow = sumBelow / countBelow;
|
|
47
|
+
const meanAbove = (sum - sumBelow) / countAbove;
|
|
48
|
+
const spread = meanBelow - meanAbove;
|
|
49
|
+
const variance = countBelow * countAbove * spread * spread;
|
|
50
|
+
if (variance > bestVariance) {
|
|
51
|
+
bestVariance = variance;
|
|
52
|
+
threshold = b;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return threshold;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* The gamma exponent that puts `volume`'s median tissue voxel on
|
|
59
|
+
* `targetGrey`, or 1 when it needs no lift or cannot be measured.
|
|
60
|
+
*
|
|
61
|
+
* Solving per volume is what makes volumes with wildly different maxima come
|
|
62
|
+
* out looking alike.
|
|
63
|
+
*/
|
|
64
|
+
export function exposureExponent(volume, targetGrey = DEFAULT_TARGET_GREY) {
|
|
65
|
+
const { data, min, max } = volume;
|
|
66
|
+
if (!data || data.length === 0 || !(max > min))
|
|
67
|
+
return 1;
|
|
68
|
+
const scale = BINS / (max - min);
|
|
69
|
+
const stride = Math.max(1, Math.floor(data.length / MAX_SAMPLES));
|
|
70
|
+
const hist = new Uint32Array(BINS);
|
|
71
|
+
let sampled = 0;
|
|
72
|
+
for (let i = 0; i < data.length; i += stride) {
|
|
73
|
+
let bin = ((data[i] - min) * scale) | 0;
|
|
74
|
+
if (bin >= BINS)
|
|
75
|
+
bin = BINS - 1;
|
|
76
|
+
else if (bin < 0)
|
|
77
|
+
bin = 0;
|
|
78
|
+
hist[bin]++;
|
|
79
|
+
sampled++;
|
|
80
|
+
}
|
|
81
|
+
const air = otsuBin(hist, sampled);
|
|
82
|
+
let tissue = 0;
|
|
83
|
+
for (let b = air + 1; b < BINS; b++)
|
|
84
|
+
tissue += hist[b];
|
|
85
|
+
if (tissue === 0)
|
|
86
|
+
return 1; // nothing above the split: not an image we can read
|
|
87
|
+
let seen = 0;
|
|
88
|
+
let medianBin = air + 1;
|
|
89
|
+
for (let b = air + 1; b < BINS; b++) {
|
|
90
|
+
seen += hist[b];
|
|
91
|
+
if (seen * 2 >= tissue) {
|
|
92
|
+
medianBin = b;
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const median = min + (medianBin + 0.5) / scale;
|
|
97
|
+
const grey = (median - min) * 255 / (max - min);
|
|
98
|
+
// Only ever brighten: this exists to answer "too dark", and darkening a
|
|
99
|
+
// volume nobody complained about is not in its remit.
|
|
100
|
+
if (!(grey > 0) || grey >= targetGrey)
|
|
101
|
+
return 1;
|
|
102
|
+
return Math.log(targetGrey / 255) / Math.log(grey / 255);
|
|
103
|
+
}
|
|
104
|
+
//# sourceMappingURL=volumeExposure.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"volumeExposure.js","sourceRoot":"","sources":["../../src/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