@fideus-labs/ngff-zarr 0.2.0 → 0.2.2
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/esm/browser-mod.d.ts +14 -0
- package/esm/browser-mod.d.ts.map +1 -0
- package/esm/browser-mod.js +23 -0
- package/esm/methods/itkwasm-browser.d.ts +6 -0
- package/esm/methods/itkwasm-browser.d.ts.map +1 -0
- package/esm/methods/itkwasm-browser.js +462 -0
- package/esm/methods/itkwasm-node.d.ts +6 -0
- package/esm/methods/itkwasm-node.d.ts.map +1 -0
- package/esm/methods/itkwasm-node.js +462 -0
- package/esm/methods/itkwasm-shared.d.ts +68 -0
- package/esm/methods/itkwasm-shared.d.ts.map +1 -0
- package/esm/methods/itkwasm-shared.js +489 -0
- package/esm/methods/itkwasm.d.ts +11 -3
- package/esm/methods/itkwasm.d.ts.map +1 -1
- package/esm/methods/itkwasm.js +11 -952
- package/package.json +28 -3
- package/script/browser-mod.d.ts +14 -0
- package/script/browser-mod.d.ts.map +1 -0
- package/script/browser-mod.js +48 -0
- package/script/methods/itkwasm-browser.d.ts +6 -0
- package/script/methods/itkwasm-browser.d.ts.map +1 -0
- package/script/methods/itkwasm-browser.js +488 -0
- package/script/methods/itkwasm-node.d.ts +6 -0
- package/script/methods/itkwasm-node.d.ts.map +1 -0
- package/script/methods/itkwasm-node.js +488 -0
- package/script/methods/itkwasm-shared.d.ts +68 -0
- package/script/methods/itkwasm-shared.d.ts.map +1 -0
- package/script/methods/itkwasm-shared.js +524 -0
- package/script/methods/itkwasm.d.ts +11 -3
- package/script/methods/itkwasm.d.ts.map +1 -1
- package/script/methods/itkwasm.js +14 -977
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Copyright (c) Fideus Labs LLC
|
|
2
|
+
// SPDX-License-Identifier: MIT
|
|
3
|
+
/**
|
|
4
|
+
* Node-compatible ITK-Wasm downsampling support
|
|
5
|
+
* Uses native WASM implementations from @itk-wasm/downsample
|
|
6
|
+
*/
|
|
7
|
+
import { downsampleBinShrinkNode as downsampleBinShrink, downsampleLabelImageNode as downsampleLabelImage, downsampleNode as downsample, } from "@itk-wasm/downsample";
|
|
8
|
+
import * as zarr from "zarrita";
|
|
9
|
+
import { NgffImage } from "../types/ngff_image.js";
|
|
10
|
+
import { dimScaleFactors, itkImageToZarr, nextScaleMetadata, SPATIAL_DIMS, updatePreviousDimFactors, zarrToItkImage, } from "./itkwasm-shared.js";
|
|
11
|
+
/**
|
|
12
|
+
* Perform Gaussian downsampling using ITK-Wasm (browser version)
|
|
13
|
+
*/
|
|
14
|
+
async function downsampleGaussian(image, dimFactors, spatialDims) {
|
|
15
|
+
// Handle time dimension by processing each time slice independently
|
|
16
|
+
if (image.dims.includes("t")) {
|
|
17
|
+
const tDimIndex = image.dims.indexOf("t");
|
|
18
|
+
const tSize = image.data.shape[tDimIndex];
|
|
19
|
+
const newDims = image.dims.filter((dim) => dim !== "t");
|
|
20
|
+
// Downsample each time slice
|
|
21
|
+
const downsampledSlices = [];
|
|
22
|
+
for (let t = 0; t < tSize; t++) {
|
|
23
|
+
// Extract time slice
|
|
24
|
+
const selection = new Array(image.data.shape.length).fill(null);
|
|
25
|
+
selection[tDimIndex] = t;
|
|
26
|
+
const sliceData = await zarr.get(image.data, selection);
|
|
27
|
+
// Create temporary zarr array for this slice
|
|
28
|
+
const sliceStore = new Map();
|
|
29
|
+
const sliceRoot = zarr.root(sliceStore);
|
|
30
|
+
const sliceShape = image.data.shape.filter((_, i) => i !== tDimIndex);
|
|
31
|
+
const sliceChunkShape = sliceShape.map((s) => Math.min(s, 256));
|
|
32
|
+
const sliceArray = await zarr.create(sliceRoot.resolve("slice"), {
|
|
33
|
+
shape: sliceShape,
|
|
34
|
+
chunk_shape: sliceChunkShape,
|
|
35
|
+
data_type: image.data.dtype,
|
|
36
|
+
fill_value: 0,
|
|
37
|
+
});
|
|
38
|
+
const fullSelection = new Array(sliceShape.length).fill(null);
|
|
39
|
+
await zarr.set(sliceArray, fullSelection, sliceData);
|
|
40
|
+
// Create NgffImage for this slice (without 't' dimension)
|
|
41
|
+
const sliceImage = new NgffImage({
|
|
42
|
+
data: sliceArray,
|
|
43
|
+
dims: newDims,
|
|
44
|
+
scale: Object.fromEntries(Object.entries(image.scale).filter(([dim]) => dim !== "t")),
|
|
45
|
+
translation: Object.fromEntries(Object.entries(image.translation).filter(([dim]) => dim !== "t")),
|
|
46
|
+
name: image.name,
|
|
47
|
+
axesUnits: image.axesUnits
|
|
48
|
+
? Object.fromEntries(Object.entries(image.axesUnits).filter(([dim]) => dim !== "t"))
|
|
49
|
+
: undefined,
|
|
50
|
+
computedCallbacks: image.computedCallbacks,
|
|
51
|
+
});
|
|
52
|
+
// Recursively downsample this slice (without 't', so no infinite loop)
|
|
53
|
+
const downsampledSlice = await downsampleGaussian(sliceImage, dimFactors, spatialDims);
|
|
54
|
+
downsampledSlices.push(downsampledSlice.data);
|
|
55
|
+
}
|
|
56
|
+
// Combine downsampled slices back into a single array with 't' dimension
|
|
57
|
+
const firstSlice = downsampledSlices[0];
|
|
58
|
+
const combinedShape = [...image.data.shape];
|
|
59
|
+
combinedShape[tDimIndex] = tSize;
|
|
60
|
+
// Update spatial dimensions based on downsampled size
|
|
61
|
+
for (let i = 0; i < image.dims.length; i++) {
|
|
62
|
+
if (i !== tDimIndex) {
|
|
63
|
+
const sliceIndex = i < tDimIndex ? i : i - 1;
|
|
64
|
+
combinedShape[i] = firstSlice.shape[sliceIndex];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
// Create combined array
|
|
68
|
+
const combinedStore = new Map();
|
|
69
|
+
const combinedRoot = zarr.root(combinedStore);
|
|
70
|
+
const combinedArray = await zarr.create(combinedRoot.resolve("combined"), {
|
|
71
|
+
shape: combinedShape,
|
|
72
|
+
chunk_shape: combinedShape.map((s) => Math.min(s, 256)),
|
|
73
|
+
data_type: image.data.dtype,
|
|
74
|
+
fill_value: 0,
|
|
75
|
+
});
|
|
76
|
+
// Copy each downsampled slice into the combined array
|
|
77
|
+
for (let t = 0; t < tSize; t++) {
|
|
78
|
+
const sliceData = await zarr.get(downsampledSlices[t]);
|
|
79
|
+
const targetSelection = new Array(combinedShape.length).fill(null);
|
|
80
|
+
targetSelection[tDimIndex] = t;
|
|
81
|
+
await zarr.set(combinedArray, targetSelection, sliceData);
|
|
82
|
+
}
|
|
83
|
+
// Compute new metadata (time dimension unchanged, spatial dimensions downsampled)
|
|
84
|
+
const [translation, scale] = nextScaleMetadata(image, dimFactors, spatialDims);
|
|
85
|
+
return new NgffImage({
|
|
86
|
+
data: combinedArray,
|
|
87
|
+
dims: image.dims,
|
|
88
|
+
scale: { ...image.scale, ...scale },
|
|
89
|
+
translation: { ...image.translation, ...translation },
|
|
90
|
+
name: image.name,
|
|
91
|
+
axesUnits: image.axesUnits,
|
|
92
|
+
computedCallbacks: image.computedCallbacks,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
const isVector = image.dims.includes("c");
|
|
96
|
+
// Convert to ITK-Wasm format
|
|
97
|
+
const itkImage = await zarrToItkImage(image.data, image.dims, isVector);
|
|
98
|
+
// Prepare shrink factors - need to be for ALL dimensions in ITK order (reversed)
|
|
99
|
+
const shrinkFactors = [];
|
|
100
|
+
for (let i = image.dims.length - 1; i >= 0; i--) {
|
|
101
|
+
const dim = image.dims[i];
|
|
102
|
+
if (SPATIAL_DIMS.includes(dim)) {
|
|
103
|
+
shrinkFactors.push(dimFactors[dim] || 1);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
// Use all zeros for cropRadius
|
|
107
|
+
const cropRadius = new Array(shrinkFactors.length).fill(0);
|
|
108
|
+
// Perform downsampling using browser-compatible function
|
|
109
|
+
const { downsampled } = await downsample(itkImage, {
|
|
110
|
+
shrinkFactors,
|
|
111
|
+
cropRadius: cropRadius,
|
|
112
|
+
});
|
|
113
|
+
// Compute new metadata
|
|
114
|
+
const [translation, scale] = nextScaleMetadata(image, dimFactors, spatialDims);
|
|
115
|
+
// Convert back to zarr array in a new in-memory store
|
|
116
|
+
const store = new Map();
|
|
117
|
+
const chunkShape = downsampled.size.map((s) => Math.min(s, 256)).reverse();
|
|
118
|
+
const array = await itkImageToZarr(downsampled, store, "image", chunkShape, image.dims);
|
|
119
|
+
return new NgffImage({
|
|
120
|
+
data: array,
|
|
121
|
+
dims: image.dims,
|
|
122
|
+
scale,
|
|
123
|
+
translation,
|
|
124
|
+
name: image.name,
|
|
125
|
+
axesUnits: image.axesUnits,
|
|
126
|
+
computedCallbacks: image.computedCallbacks,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Perform bin shrink downsampling using ITK-Wasm (browser version)
|
|
131
|
+
*/
|
|
132
|
+
async function downsampleBinShrinkImpl(image, dimFactors, spatialDims) {
|
|
133
|
+
// Handle time dimension by processing each time slice independently
|
|
134
|
+
if (image.dims.includes("t")) {
|
|
135
|
+
const tDimIndex = image.dims.indexOf("t");
|
|
136
|
+
const tSize = image.data.shape[tDimIndex];
|
|
137
|
+
const newDims = image.dims.filter((dim) => dim !== "t");
|
|
138
|
+
// Downsample each time slice
|
|
139
|
+
const downsampledSlices = [];
|
|
140
|
+
for (let t = 0; t < tSize; t++) {
|
|
141
|
+
// Extract time slice
|
|
142
|
+
const selection = new Array(image.data.shape.length).fill(null);
|
|
143
|
+
selection[tDimIndex] = t;
|
|
144
|
+
const sliceData = await zarr.get(image.data, selection);
|
|
145
|
+
// Create temporary zarr array for this slice
|
|
146
|
+
const sliceStore = new Map();
|
|
147
|
+
const sliceRoot = zarr.root(sliceStore);
|
|
148
|
+
const sliceShape = image.data.shape.filter((_, i) => i !== tDimIndex);
|
|
149
|
+
const sliceChunkShape = sliceShape.map((s) => Math.min(s, 256));
|
|
150
|
+
const sliceArray = await zarr.create(sliceRoot.resolve("slice"), {
|
|
151
|
+
shape: sliceShape,
|
|
152
|
+
chunk_shape: sliceChunkShape,
|
|
153
|
+
data_type: image.data.dtype,
|
|
154
|
+
fill_value: 0,
|
|
155
|
+
});
|
|
156
|
+
const fullSelection = new Array(sliceShape.length).fill(null);
|
|
157
|
+
await zarr.set(sliceArray, fullSelection, sliceData);
|
|
158
|
+
// Create NgffImage for this slice (without 't' dimension)
|
|
159
|
+
const sliceImage = new NgffImage({
|
|
160
|
+
data: sliceArray,
|
|
161
|
+
dims: newDims,
|
|
162
|
+
scale: Object.fromEntries(Object.entries(image.scale).filter(([dim]) => dim !== "t")),
|
|
163
|
+
translation: Object.fromEntries(Object.entries(image.translation).filter(([dim]) => dim !== "t")),
|
|
164
|
+
name: image.name,
|
|
165
|
+
axesUnits: image.axesUnits
|
|
166
|
+
? Object.fromEntries(Object.entries(image.axesUnits).filter(([dim]) => dim !== "t"))
|
|
167
|
+
: undefined,
|
|
168
|
+
computedCallbacks: image.computedCallbacks,
|
|
169
|
+
});
|
|
170
|
+
// Recursively downsample this slice
|
|
171
|
+
const downsampledSlice = await downsampleBinShrinkImpl(sliceImage, dimFactors, spatialDims);
|
|
172
|
+
downsampledSlices.push(downsampledSlice.data);
|
|
173
|
+
}
|
|
174
|
+
// Combine downsampled slices back into a single array with 't' dimension
|
|
175
|
+
const firstSlice = downsampledSlices[0];
|
|
176
|
+
const combinedShape = [...image.data.shape];
|
|
177
|
+
combinedShape[tDimIndex] = tSize;
|
|
178
|
+
// Update spatial dimensions based on downsampled size
|
|
179
|
+
for (let i = 0; i < image.dims.length; i++) {
|
|
180
|
+
if (i !== tDimIndex) {
|
|
181
|
+
const sliceIndex = i < tDimIndex ? i : i - 1;
|
|
182
|
+
combinedShape[i] = firstSlice.shape[sliceIndex];
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
// Create combined array
|
|
186
|
+
const combinedStore = new Map();
|
|
187
|
+
const combinedRoot = zarr.root(combinedStore);
|
|
188
|
+
const combinedArray = await zarr.create(combinedRoot.resolve("combined"), {
|
|
189
|
+
shape: combinedShape,
|
|
190
|
+
chunk_shape: combinedShape.map((s) => Math.min(s, 256)),
|
|
191
|
+
data_type: image.data.dtype,
|
|
192
|
+
fill_value: 0,
|
|
193
|
+
});
|
|
194
|
+
// Copy each downsampled slice into the combined array
|
|
195
|
+
for (let t = 0; t < tSize; t++) {
|
|
196
|
+
const sliceData = await zarr.get(downsampledSlices[t]);
|
|
197
|
+
const targetSelection = new Array(combinedShape.length).fill(null);
|
|
198
|
+
targetSelection[tDimIndex] = t;
|
|
199
|
+
await zarr.set(combinedArray, targetSelection, sliceData);
|
|
200
|
+
}
|
|
201
|
+
// Compute new metadata
|
|
202
|
+
const [translation, scale] = nextScaleMetadata(image, dimFactors, spatialDims);
|
|
203
|
+
return new NgffImage({
|
|
204
|
+
data: combinedArray,
|
|
205
|
+
dims: image.dims,
|
|
206
|
+
scale: { ...image.scale, ...scale },
|
|
207
|
+
translation: { ...image.translation, ...translation },
|
|
208
|
+
name: image.name,
|
|
209
|
+
axesUnits: image.axesUnits,
|
|
210
|
+
computedCallbacks: image.computedCallbacks,
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
const isVector = image.dims.includes("c");
|
|
214
|
+
// Convert to ITK-Wasm format
|
|
215
|
+
const itkImage = await zarrToItkImage(image.data, image.dims, isVector);
|
|
216
|
+
// Prepare shrink factors - only for spatial dimensions in ITK order (reversed)
|
|
217
|
+
const shrinkFactors = [];
|
|
218
|
+
for (let i = image.dims.length - 1; i >= 0; i--) {
|
|
219
|
+
const dim = image.dims[i];
|
|
220
|
+
if (SPATIAL_DIMS.includes(dim)) {
|
|
221
|
+
shrinkFactors.push(dimFactors[dim] || 1);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// Perform downsampling using browser-compatible function
|
|
225
|
+
const { downsampled } = await downsampleBinShrink(itkImage, {
|
|
226
|
+
shrinkFactors,
|
|
227
|
+
});
|
|
228
|
+
// Compute new metadata
|
|
229
|
+
const [translation, scale] = nextScaleMetadata(image, dimFactors, spatialDims);
|
|
230
|
+
// Convert back to zarr array in a new in-memory store
|
|
231
|
+
const store = new Map();
|
|
232
|
+
const chunkShape = downsampled.size.map((s) => Math.min(s, 256)).reverse();
|
|
233
|
+
const array = await itkImageToZarr(downsampled, store, "image", chunkShape, image.dims);
|
|
234
|
+
return new NgffImage({
|
|
235
|
+
data: array,
|
|
236
|
+
dims: image.dims,
|
|
237
|
+
scale,
|
|
238
|
+
translation,
|
|
239
|
+
name: image.name,
|
|
240
|
+
axesUnits: image.axesUnits,
|
|
241
|
+
computedCallbacks: image.computedCallbacks,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Perform label image downsampling using ITK-Wasm (browser version)
|
|
246
|
+
*/
|
|
247
|
+
async function downsampleLabelImageImpl(image, dimFactors, spatialDims) {
|
|
248
|
+
// Handle time dimension by processing each time slice independently
|
|
249
|
+
if (image.dims.includes("t")) {
|
|
250
|
+
const tDimIndex = image.dims.indexOf("t");
|
|
251
|
+
const tSize = image.data.shape[tDimIndex];
|
|
252
|
+
const newDims = image.dims.filter((dim) => dim !== "t");
|
|
253
|
+
// Downsample each time slice
|
|
254
|
+
const downsampledSlices = [];
|
|
255
|
+
for (let t = 0; t < tSize; t++) {
|
|
256
|
+
// Extract time slice
|
|
257
|
+
const selection = new Array(image.data.shape.length).fill(null);
|
|
258
|
+
selection[tDimIndex] = t;
|
|
259
|
+
const sliceData = await zarr.get(image.data, selection);
|
|
260
|
+
// Create temporary zarr array for this slice
|
|
261
|
+
const sliceStore = new Map();
|
|
262
|
+
const sliceRoot = zarr.root(sliceStore);
|
|
263
|
+
const sliceShape = image.data.shape.filter((_, i) => i !== tDimIndex);
|
|
264
|
+
const sliceChunkShape = sliceShape.map((s) => Math.min(s, 256));
|
|
265
|
+
const sliceArray = await zarr.create(sliceRoot.resolve("slice"), {
|
|
266
|
+
shape: sliceShape,
|
|
267
|
+
chunk_shape: sliceChunkShape,
|
|
268
|
+
data_type: image.data.dtype,
|
|
269
|
+
fill_value: 0,
|
|
270
|
+
});
|
|
271
|
+
const fullSelection = new Array(sliceShape.length).fill(null);
|
|
272
|
+
await zarr.set(sliceArray, fullSelection, sliceData);
|
|
273
|
+
// Create NgffImage for this slice (without 't' dimension)
|
|
274
|
+
const sliceImage = new NgffImage({
|
|
275
|
+
data: sliceArray,
|
|
276
|
+
dims: newDims,
|
|
277
|
+
scale: Object.fromEntries(Object.entries(image.scale).filter(([dim]) => dim !== "t")),
|
|
278
|
+
translation: Object.fromEntries(Object.entries(image.translation).filter(([dim]) => dim !== "t")),
|
|
279
|
+
name: image.name,
|
|
280
|
+
axesUnits: image.axesUnits
|
|
281
|
+
? Object.fromEntries(Object.entries(image.axesUnits).filter(([dim]) => dim !== "t"))
|
|
282
|
+
: undefined,
|
|
283
|
+
computedCallbacks: image.computedCallbacks,
|
|
284
|
+
});
|
|
285
|
+
// Recursively downsample this slice
|
|
286
|
+
const downsampledSlice = await downsampleLabelImageImpl(sliceImage, dimFactors, spatialDims);
|
|
287
|
+
downsampledSlices.push(downsampledSlice.data);
|
|
288
|
+
}
|
|
289
|
+
// Combine downsampled slices back into a single array with 't' dimension
|
|
290
|
+
const firstSlice = downsampledSlices[0];
|
|
291
|
+
const combinedShape = [...image.data.shape];
|
|
292
|
+
combinedShape[tDimIndex] = tSize;
|
|
293
|
+
// Update spatial dimensions based on downsampled size
|
|
294
|
+
for (let i = 0; i < image.dims.length; i++) {
|
|
295
|
+
if (i !== tDimIndex) {
|
|
296
|
+
const sliceIndex = i < tDimIndex ? i : i - 1;
|
|
297
|
+
combinedShape[i] = firstSlice.shape[sliceIndex];
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
// Create combined array
|
|
301
|
+
const combinedStore = new Map();
|
|
302
|
+
const combinedRoot = zarr.root(combinedStore);
|
|
303
|
+
const combinedArray = await zarr.create(combinedRoot.resolve("combined"), {
|
|
304
|
+
shape: combinedShape,
|
|
305
|
+
chunk_shape: combinedShape.map((s) => Math.min(s, 256)),
|
|
306
|
+
data_type: image.data.dtype,
|
|
307
|
+
fill_value: 0,
|
|
308
|
+
});
|
|
309
|
+
// Copy each downsampled slice into the combined array
|
|
310
|
+
for (let t = 0; t < tSize; t++) {
|
|
311
|
+
const sliceData = await zarr.get(downsampledSlices[t]);
|
|
312
|
+
const targetSelection = new Array(combinedShape.length).fill(null);
|
|
313
|
+
targetSelection[tDimIndex] = t;
|
|
314
|
+
await zarr.set(combinedArray, targetSelection, sliceData);
|
|
315
|
+
}
|
|
316
|
+
// Compute new metadata
|
|
317
|
+
const [translation, scale] = nextScaleMetadata(image, dimFactors, spatialDims);
|
|
318
|
+
return new NgffImage({
|
|
319
|
+
data: combinedArray,
|
|
320
|
+
dims: image.dims,
|
|
321
|
+
scale: { ...image.scale, ...scale },
|
|
322
|
+
translation: { ...image.translation, ...translation },
|
|
323
|
+
name: image.name,
|
|
324
|
+
axesUnits: image.axesUnits,
|
|
325
|
+
computedCallbacks: image.computedCallbacks,
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
const isVector = image.dims.includes("c");
|
|
329
|
+
// Convert to ITK-Wasm format
|
|
330
|
+
const itkImage = await zarrToItkImage(image.data, image.dims, isVector);
|
|
331
|
+
// Prepare shrink factors - need to be for ALL dimensions in ITK order (reversed)
|
|
332
|
+
const shrinkFactors = [];
|
|
333
|
+
for (let i = image.dims.length - 1; i >= 0; i--) {
|
|
334
|
+
const dim = image.dims[i];
|
|
335
|
+
if (SPATIAL_DIMS.includes(dim)) {
|
|
336
|
+
shrinkFactors.push(dimFactors[dim] || 1);
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
shrinkFactors.push(1); // Non-spatial dimensions don't shrink
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
// Use all zeros for cropRadius
|
|
343
|
+
const cropRadius = new Array(shrinkFactors.length).fill(0);
|
|
344
|
+
// Perform downsampling using browser-compatible function
|
|
345
|
+
const { downsampled } = await downsampleLabelImage(itkImage, {
|
|
346
|
+
shrinkFactors,
|
|
347
|
+
cropRadius: cropRadius,
|
|
348
|
+
});
|
|
349
|
+
// Compute new metadata
|
|
350
|
+
const [translation, scale] = nextScaleMetadata(image, dimFactors, spatialDims);
|
|
351
|
+
// Convert back to zarr array in a new in-memory store
|
|
352
|
+
const store = new Map();
|
|
353
|
+
const chunkShape = downsampled.size.map((s) => Math.min(s, 256)).reverse();
|
|
354
|
+
const array = await itkImageToZarr(downsampled, store, "image", chunkShape, image.dims);
|
|
355
|
+
return new NgffImage({
|
|
356
|
+
data: array,
|
|
357
|
+
dims: image.dims,
|
|
358
|
+
scale,
|
|
359
|
+
translation,
|
|
360
|
+
name: image.name,
|
|
361
|
+
axesUnits: image.axesUnits,
|
|
362
|
+
computedCallbacks: image.computedCallbacks,
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Main downsampling function for ITK-Wasm (browser version)
|
|
367
|
+
*/
|
|
368
|
+
export async function downsampleItkWasm(ngffImage, scaleFactors, smoothing) {
|
|
369
|
+
const multiscales = [ngffImage];
|
|
370
|
+
const dims = ngffImage.dims;
|
|
371
|
+
const spatialDims = dims.filter((dim) => SPATIAL_DIMS.includes(dim));
|
|
372
|
+
let previousImage = ngffImage;
|
|
373
|
+
let previousDimFactors = {};
|
|
374
|
+
for (const dim of dims)
|
|
375
|
+
previousDimFactors[dim] = 1;
|
|
376
|
+
for (let i = 0; i < scaleFactors.length; i++) {
|
|
377
|
+
const scaleFactor = scaleFactors[i];
|
|
378
|
+
let sourceImage;
|
|
379
|
+
let sourceDimFactors;
|
|
380
|
+
if (smoothing === "bin_shrink") {
|
|
381
|
+
// Purely incremental: scaleFactor is the shrink for this step
|
|
382
|
+
sourceImage = previousImage;
|
|
383
|
+
sourceDimFactors = {};
|
|
384
|
+
if (typeof scaleFactor === "number") {
|
|
385
|
+
for (const dim of spatialDims)
|
|
386
|
+
sourceDimFactors[dim] = scaleFactor;
|
|
387
|
+
}
|
|
388
|
+
else {
|
|
389
|
+
for (const dim of spatialDims) {
|
|
390
|
+
sourceDimFactors[dim] = scaleFactor[dim] || 1;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
for (const dim of dims) {
|
|
394
|
+
if (!(dim in sourceDimFactors))
|
|
395
|
+
sourceDimFactors[dim] = 1;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
else {
|
|
399
|
+
// Hybrid absolute strategy
|
|
400
|
+
const dimFactors = dimScaleFactors(dims, scaleFactor, previousDimFactors, ngffImage, previousImage);
|
|
401
|
+
let canDownsampleIncrementally = true;
|
|
402
|
+
for (const dim of Object.keys(dimFactors)) {
|
|
403
|
+
const dimIndex = ngffImage.dims.indexOf(dim);
|
|
404
|
+
if (dimIndex >= 0) {
|
|
405
|
+
const originalSize = ngffImage.data.shape[dimIndex];
|
|
406
|
+
const targetSize = Math.floor(originalSize /
|
|
407
|
+
(typeof scaleFactor === "number"
|
|
408
|
+
? scaleFactor
|
|
409
|
+
: scaleFactor[dim]));
|
|
410
|
+
const prevDimIndex = previousImage.dims.indexOf(dim);
|
|
411
|
+
const previousSize = previousImage.data.shape[prevDimIndex];
|
|
412
|
+
if (Math.floor(previousSize / dimFactors[dim]) !== targetSize) {
|
|
413
|
+
canDownsampleIncrementally = false;
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
if (canDownsampleIncrementally) {
|
|
419
|
+
sourceImage = previousImage;
|
|
420
|
+
sourceDimFactors = dimFactors;
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
sourceImage = ngffImage;
|
|
424
|
+
const originalDimFactors = {};
|
|
425
|
+
for (const dim of dims)
|
|
426
|
+
originalDimFactors[dim] = 1;
|
|
427
|
+
sourceDimFactors = dimScaleFactors(dims, scaleFactor, originalDimFactors);
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
let downsampled;
|
|
431
|
+
if (smoothing === "gaussian") {
|
|
432
|
+
downsampled = await downsampleGaussian(sourceImage, sourceDimFactors, spatialDims);
|
|
433
|
+
}
|
|
434
|
+
else if (smoothing === "bin_shrink") {
|
|
435
|
+
downsampled = await downsampleBinShrinkImpl(sourceImage, sourceDimFactors, spatialDims);
|
|
436
|
+
}
|
|
437
|
+
else if (smoothing === "label_image") {
|
|
438
|
+
downsampled = await downsampleLabelImageImpl(sourceImage, sourceDimFactors, spatialDims);
|
|
439
|
+
}
|
|
440
|
+
else {
|
|
441
|
+
throw new Error(`Unknown smoothing method: ${smoothing}`);
|
|
442
|
+
}
|
|
443
|
+
multiscales.push(downsampled);
|
|
444
|
+
previousImage = downsampled;
|
|
445
|
+
if (smoothing === "bin_shrink") {
|
|
446
|
+
if (typeof scaleFactor === "number") {
|
|
447
|
+
for (const dim of spatialDims) {
|
|
448
|
+
previousDimFactors[dim] *= scaleFactor;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
else {
|
|
452
|
+
for (const dim of spatialDims) {
|
|
453
|
+
previousDimFactors[dim] *= scaleFactor[dim] || 1;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
else {
|
|
458
|
+
previousDimFactors = updatePreviousDimFactors(scaleFactor, spatialDims, previousDimFactors);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
return multiscales;
|
|
462
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared helper functions for ITK-Wasm downsampling
|
|
3
|
+
* Used by both browser and Node implementations
|
|
4
|
+
*/
|
|
5
|
+
import type { Image } from "itk-wasm";
|
|
6
|
+
import * as zarr from "zarrita";
|
|
7
|
+
import { NgffImage } from "../types/ngff_image.js";
|
|
8
|
+
export declare const SPATIAL_DIMS: string[];
|
|
9
|
+
export interface DimFactors {
|
|
10
|
+
[key: string]: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Calculate the incremental factor needed to reach the target size from the previous size.
|
|
14
|
+
* This ensures exact target sizes when downsampling incrementally.
|
|
15
|
+
*/
|
|
16
|
+
export declare function calculateIncrementalFactor(previousSize: number, targetSize: number): number;
|
|
17
|
+
/**
|
|
18
|
+
* Convert dimension scale factors to ITK-Wasm format
|
|
19
|
+
* This computes the incremental scale factor relative to the previous scale,
|
|
20
|
+
* not the absolute scale factor from the original image.
|
|
21
|
+
*
|
|
22
|
+
* When originalImage and previousImage are provided, calculates the exact
|
|
23
|
+
* incremental factor needed to reach the target size from the previous size.
|
|
24
|
+
* This ensures we get exact 1x, 2x, 3x, 4x sizes even with incremental downsampling.
|
|
25
|
+
*/
|
|
26
|
+
export declare function dimScaleFactors(dims: string[], scaleFactor: Record<string, number> | number, previousDimFactors: DimFactors, originalImage?: NgffImage, previousImage?: NgffImage): DimFactors;
|
|
27
|
+
/**
|
|
28
|
+
* Update previous dimension factors
|
|
29
|
+
*/
|
|
30
|
+
export declare function updatePreviousDimFactors(scaleFactor: Record<string, number> | number, spatialDims: string[], previousDimFactors: DimFactors): DimFactors;
|
|
31
|
+
/**
|
|
32
|
+
* Compute next scale metadata
|
|
33
|
+
*/
|
|
34
|
+
export declare function nextScaleMetadata(image: NgffImage, dimFactors: DimFactors, spatialDims: string[]): [Record<string, number>, Record<string, number>];
|
|
35
|
+
/**
|
|
36
|
+
* Get ITK component type from typed array
|
|
37
|
+
*/
|
|
38
|
+
export declare function getItkComponentType(data: unknown): "uint8" | "int8" | "uint16" | "int16" | "uint32" | "int32" | "float32" | "float64";
|
|
39
|
+
/**
|
|
40
|
+
* Create identity matrix for ITK direction
|
|
41
|
+
*/
|
|
42
|
+
export declare function createIdentityMatrix(dimension: number): Float64Array;
|
|
43
|
+
/**
|
|
44
|
+
* Transpose array data according to permutation
|
|
45
|
+
*/
|
|
46
|
+
export declare function transposeArray(data: unknown, shape: number[], permutation: number[], componentType: "uint8" | "int8" | "uint16" | "int16" | "uint32" | "int32" | "float32" | "float64"): Float32Array | Float64Array | Uint8Array | Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array;
|
|
47
|
+
/**
|
|
48
|
+
* Convert zarr array to ITK-Wasm Image format
|
|
49
|
+
* If isVector is true, ensures "c" dimension is last by transposing if needed
|
|
50
|
+
*/
|
|
51
|
+
export declare function zarrToItkImage(array: zarr.Array<zarr.DataType, zarr.Readable>, dims: string[], isVector?: boolean): Promise<Image>;
|
|
52
|
+
/**
|
|
53
|
+
* Convert ITK-Wasm Image back to zarr array
|
|
54
|
+
* Uses the provided store instead of creating a new one
|
|
55
|
+
*
|
|
56
|
+
* Important: ITK-Wasm stores size in physical space order [x, y, z], but data in
|
|
57
|
+
* column-major order (x contiguous). This column-major layout with size [x, y, z]
|
|
58
|
+
* is equivalent to C-order (row-major) with shape [z, y, x]. We reverse the size
|
|
59
|
+
* to get the zarr shape and use C-order strides for that reversed shape.
|
|
60
|
+
*
|
|
61
|
+
* @param itkImage - The ITK-Wasm image to convert
|
|
62
|
+
* @param store - The zarr store to write to
|
|
63
|
+
* @param path - The path within the store
|
|
64
|
+
* @param chunkShape - The chunk shape (in spatial dimension order, will be adjusted for components)
|
|
65
|
+
* @param targetDims - The target dimension order (e.g., ["c", "z", "y", "x"])
|
|
66
|
+
*/
|
|
67
|
+
export declare function itkImageToZarr(itkImage: Image, store: Map<string, Uint8Array>, path: string, chunkShape: number[], targetDims?: string[]): Promise<zarr.Array<zarr.DataType, zarr.Readable>>;
|
|
68
|
+
//# sourceMappingURL=itkwasm-shared.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"itkwasm-shared.d.ts","sourceRoot":"","sources":["../../src/methods/itkwasm-shared.ts"],"names":[],"mappings":"AAGA;;;GAGG;AAEH,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AACtC,OAAO,KAAK,IAAI,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAEnD,eAAO,MAAM,YAAY,UAAkB,CAAC;AAE5C,MAAM,WAAW,UAAU;IACzB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC;CACvB;AAED;;;GAGG;AACH,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,MAAM,EACpB,UAAU,EAAE,MAAM,GACjB,MAAM,CAsBR;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAC7B,IAAI,EAAE,MAAM,EAAE,EACd,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,EAC5C,kBAAkB,EAAE,UAAU,EAC9B,aAAa,CAAC,EAAE,SAAS,EACzB,aAAa,CAAC,EAAE,SAAS,GACxB,UAAU,CAqEZ;AAED;;GAEG;AACH,wBAAgB,wBAAwB,CACtC,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,EAC5C,WAAW,EAAE,MAAM,EAAE,EACrB,kBAAkB,EAAE,UAAU,GAC7B,UAAU,CAcZ;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,SAAS,EAChB,UAAU,EAAE,UAAU,EACtB,WAAW,EAAE,MAAM,EAAE,GACpB,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAkBlD;AAsCD;;GAEG;AACH,wBAAgB,mBAAmB,CACjC,IAAI,EAAE,OAAO,GAEX,OAAO,GACP,MAAM,GACN,QAAQ,GACR,OAAO,GACP,QAAQ,GACR,OAAO,GACP,SAAS,GACT,SAAS,CASZ;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,SAAS,EAAE,MAAM,GAAG,YAAY,CAMpE;AAcD;;GAEG;AACH,wBAAgB,cAAc,CAC5B,IAAI,EAAE,OAAO,EACb,KAAK,EAAE,MAAM,EAAE,EACf,WAAW,EAAE,MAAM,EAAE,EACrB,aAAa,EACT,OAAO,GACP,MAAM,GACN,QAAQ,GACR,OAAO,GACP,QAAQ,GACR,OAAO,GACP,SAAS,GACT,SAAS,GAEX,YAAY,GACZ,YAAY,GACZ,UAAU,GACV,SAAS,GACT,WAAW,GACX,UAAU,GACV,WAAW,GACX,UAAU,CAqFb;AAED;;;GAGG;AACH,wBAAsB,cAAc,CAClC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,EAC/C,IAAI,EAAE,MAAM,EAAE,EACd,QAAQ,UAAQ,GACf,OAAO,CAAC,KAAK,CAAC,CAqFhB;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,cAAc,CAClC,QAAQ,EAAE,KAAK,EACf,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,EAC9B,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAAE,EACpB,UAAU,CAAC,EAAE,MAAM,EAAE,GACpB,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC,CAoJnD"}
|