-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathtoMultiscaleSpatialImage.js
93 lines (86 loc) · 2.43 KB
/
toMultiscaleSpatialImage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { readImageArrayBuffer } from 'itk-wasm'
import MultiscaleSpatialImage from './MultiscaleSpatialImage'
import InMemoryMultiscaleSpatialImage from './InMemoryMultiscaleSpatialImage'
import ZarrMultiscaleSpatialImage, {
isZarr,
} from './ZarrMultiscaleSpatialImage'
import ndarrayToItkImage from './ndarrayToItkImage'
import fetchBinaryContent from './fetchBinaryContent'
async function itkImageToInMemoryMultiscaleSpatialImage(image, isLabelImage) {
let chunkSize = [64, 64, 64]
if (image.data.length < 2e6) {
// Keep a single chunk
chunkSize = image.size
} else if (image.imageType.dimension === 2) {
chunkSize = [256, 256]
}
const {
scaleInfo,
imageType,
pyramid,
} = await InMemoryMultiscaleSpatialImage.buildPyramid(
image,
chunkSize,
isLabelImage
)
const multiscaleImage = new InMemoryMultiscaleSpatialImage(
pyramid,
scaleInfo,
imageType,
image.name
)
return multiscaleImage
}
async function toMultiscaleSpatialImage(
image,
isLabelImage = false,
maxConcurrency
) {
let multiscaleImage = null
if (image instanceof MultiscaleSpatialImage) {
// Already a multi-scale, chunked image
multiscaleImage = image
} else if (image.imageType !== undefined) {
// itk-wasm Image
multiscaleImage = await itkImageToInMemoryMultiscaleSpatialImage(
image,
isLabelImage
)
} else if (typeof image.getItem === 'function') {
// key value store
multiscaleImage = ZarrMultiscaleSpatialImage.fromStore(
image,
maxConcurrency
)
} else if (image._rtype !== undefined && image._rtype === 'ndarray') {
// ndarray
const itkImage = ndarrayToItkImage(image)
multiscaleImage = await itkImageToInMemoryMultiscaleSpatialImage(
itkImage,
isLabelImage
)
} else if (image.href !== undefined) {
if (isZarr(image.href)) {
multiscaleImage = ZarrMultiscaleSpatialImage.fromUrl(
image,
maxConcurrency
)
} else {
const dataBuffer = await fetchBinaryContent(image)
const { image: itkImage, webWorker } = await readImageArrayBuffer(
null,
dataBuffer,
image.pathname.split('/').pop()
)
webWorker.terminate()
multiscaleImage = await itkImageToInMemoryMultiscaleSpatialImage(
itkImage,
isLabelImage
)
}
} else {
throw new Error('Unexpected image')
}
return multiscaleImage
}
export default toMultiscaleSpatialImage