-
Notifications
You must be signed in to change notification settings - Fork 7
/
VolumeLoader.ts
455 lines (415 loc) · 16.1 KB
/
VolumeLoader.ts
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import "regenerator-runtime/runtime";
import Volume, { ImageInfo } from "./Volume";
import { openArray, openGroup, HTTPStore } from "zarr";
import { fromUrl } from "geotiff";
export type TypedArray =
| Uint8Array
| Int8Array
| Uint16Array
| Int16Array
| Uint32Array
| Int32Array
| Float32Array
| Float64Array;
/**
* @callback PerChannelCallback
* @param {string} imageurl
* @param {Volume} volume
* @param {number} channelindex
*/
type PerChannelCallback = (imageurl: string, volume: Volume, channelIndex: number) => void;
interface PackedChannelsImage {
name: string;
channels: number[];
}
type PackedChannelsImageRequests = Record<string, HTMLImageElement>;
// We want to find the most "square" packing of z tw by th tiles.
// Compute number of rows and columns.
function computePackedAtlasDims(z, tw, th): { nrows: number; ncols: number } {
let nextrows = 1;
let nextcols = z;
let ratio = (nextcols * tw) / (nextrows * th);
let nrows = nextrows;
let ncols = nextcols;
while (ratio > 1) {
nrows = nextrows;
ncols = nextcols;
nextcols -= 1;
nextrows = Math.ceil(z / nextcols);
ratio = (nextcols * tw) / (nextrows * th);
}
// const atlaswidth = ncols * tw;
// const atlasheight = nrows * th;
// console.log(atlaswidth, atlasheight);
return { nrows, ncols };
}
/**
* @class
*/
export default class VolumeLoader {
/**
* load per-channel volume data from a batch of image files containing the volume slices tiled across the images
* @param {Volume} volume
* @param {Array.<{name:string, channels:Array.<number>}>} imageArray
* @param {PerChannelCallback} onChannelLoaded Per-channel callback. Called when each channel's atlased volume data is loaded
* @returns {Object.<string, Image>} a map(imageurl : Image object) that should be used to cancel the download requests,
* for example if you need to destroy the image before all data has arrived.
* as requests arrive, the callback will be called per image, not per channel
* @example loadVolumeAtlasData([{
* "name": "AICS-10_5_5.ome.tif_atlas_0.png",
* "channels": [0, 1, 2]
* }, {
* "name": "AICS-10_5_5.ome.tif_atlas_1.png",
* "channels": [3, 4, 5]
* }, {
* "name": "AICS-10_5_5.ome.tif_atlas_2.png",
* "channels": [6, 7, 8]
* }], mycallback);
*/
static loadVolumeAtlasData(
volume: Volume,
imageArray: PackedChannelsImage[],
onChannelLoaded: PerChannelCallback
): PackedChannelsImageRequests {
const numImages = imageArray.length;
const requests = {};
//console.log("BEGIN DOWNLOAD DATA");
for (let i = 0; i < numImages; ++i) {
const url = imageArray[i].name;
const batch = imageArray[i].channels;
// using Image is just a trick to download the bits as a png.
// the Image will never be used again.
const img: HTMLImageElement = new Image();
img.onerror = function () {
console.log("ERROR LOADING " + url);
};
img.onload = (function (thisbatch) {
return function (event: Event) {
//console.log("GOT ch " + me.src);
// extract pixels by drawing to canvas
const canvas = document.createElement("canvas");
// nice thing about this is i could downsample here
const w = Math.floor((event?.target as HTMLImageElement).naturalWidth);
const h = Math.floor((event?.target as HTMLImageElement).naturalHeight);
canvas.setAttribute("width", "" + w);
canvas.setAttribute("height", "" + h);
const ctx = canvas.getContext("2d");
if (!ctx) {
console.log("Error creating canvas 2d context for " + url);
return;
}
ctx.globalCompositeOperation = "copy";
ctx.globalAlpha = 1.0;
ctx.drawImage(event?.target as CanvasImageSource, 0, 0, w, h);
// getImageData returns rgba.
// optimize: collapse rgba to single channel arrays
const iData = ctx.getImageData(0, 0, w, h);
const channelsBits: Uint8Array[] = [];
// allocate channels in batch
for (let ch = 0; ch < Math.min(thisbatch.length, 4); ++ch) {
channelsBits.push(new Uint8Array(w * h));
}
// extract the data
for (let j = 0; j < Math.min(thisbatch.length, 4); ++j) {
for (let px = 0; px < w * h; px++) {
channelsBits[j][px] = iData.data[px * 4 + j];
}
}
// done with img, iData, and canvas now.
for (let ch = 0; ch < Math.min(thisbatch.length, 4); ++ch) {
volume.setChannelDataFromAtlas(thisbatch[ch], channelsBits[ch], w, h);
onChannelLoaded(url, volume, thisbatch[ch]);
}
};
})(batch);
img.crossOrigin = "Anonymous";
img.src = url;
requests[url] = img;
}
return requests;
}
/**
* load 4d volume stored in json plus tiled png texture atlases
* @param {string} url
* @param {string} urlPrefix The part of the url to prepend for all the images specified in the json.
* @param {PerChannelCallback} onChannelLoaded Per-channel callback. Called when each channel's atlased volume data is loaded
* @returns {Promise<Volume>}
*/
static loadJson(url: string, urlPrefix: string, onChannelLoaded: PerChannelCallback): Promise<Volume> {
return fetch(url)
.then(function (response) {
return response.json();
})
.then(function (myJson) {
const vol = new Volume(myJson);
// if you need to adjust image paths prior to download,
// now is the time to do it:
myJson.images.forEach(function (element) {
element.name = urlPrefix + element.name;
});
VolumeLoader.loadVolumeAtlasData(vol, myJson.images, onChannelLoaded);
return vol;
});
}
/**
* load 5d ome-zarr into Volume object
* @param {string} url
* @param {PerChannelCallback} onChannelLoaded Per-channel callback. Called when each channel's atlased volume data is loaded
* @returns {Promise<Volume>}
*/
static async loadZarr(
urlStore: string,
imageName: string,
t: number,
onChannelLoaded: PerChannelCallback
): Promise<Volume> {
const store = new HTTPStore(urlStore);
const imagegroup = imageName;
const data = await openGroup(store, imagegroup, "r");
// get top-level metadata for this zarr image
const allmetadata = await data.attrs.asObject();
// take the first multiscales entry
const numlevels = allmetadata.multiscales[0].datasets.length;
// get raw scaling for level 0
// each entry of multiscales is a multiscale image.
const imageIndex = 0;
// there is one dataset for each multiscale level.
const dataset0 = allmetadata.multiscales[imageIndex].datasets[0];
// technically there can be any number of coordinateTransformations
// but there must be only one of type "scale".
// Here I assume that is the only one.
const scale5d = dataset0.coordinateTransformations[0].scale;
// TODO get metadata sizes for each level? how inefficient is that?
// update levelToLoad after we get size info about multiscales?
const metadata = allmetadata.omero;
const level0 = await openArray({ store: store, path: imagegroup + "/" + dataset0.path, mode: "r" });
// full res info
const w = level0.meta.shape[4];
const h = level0.meta.shape[3];
const z = level0.meta.shape[2];
const c = level0.meta.shape[1];
const sizeT = level0.meta.shape[0];
//console.log(`X=${w}, Y=${h}, Z=${z}, C=${c}, T=${sizeT}`);
// making a choice of a reduced level:
const downsampleZ = 2; // half the z
const levelToLoad = numlevels - 1; //1;
const dataset2 = allmetadata.multiscales[imageIndex].datasets[levelToLoad];
const level = await openArray({ store: store, path: imagegroup + "/" + dataset2.path, mode: "r" });
// reduced level info
const tw = level.meta.shape[4];
const th = level.meta.shape[3];
// compute rows and cols and atlas width and ht, given tw and th
const loadedZ = Math.ceil(z / downsampleZ);
const { nrows, ncols } = computePackedAtlasDims(loadedZ, tw, th);
const atlaswidth = ncols * tw;
const atlasheight = nrows * th;
const chnames: string[] = [];
for (let i = 0; i < metadata.channels.length; ++i) {
chnames.push(metadata.channels[i].label);
}
/* eslint-disable @typescript-eslint/naming-convention */
const imgdata: ImageInfo = {
width: w,
height: h,
channels: c,
channel_names: chnames,
rows: nrows,
cols: ncols,
tiles: loadedZ,
tile_width: tw,
tile_height: th,
// for webgl reasons, it is best for atlas_width and atlas_height to be <= 2048
// and ideally a power of 2. This generally implies downsampling the original volume data for display in this viewer.
atlas_width: atlaswidth,
atlas_height: atlasheight,
pixel_size_x: scale5d[4],
pixel_size_y: scale5d[3],
pixel_size_z: scale5d[2] * downsampleZ,
name: metadata.name,
version: metadata.version,
transform: {
translation: [0, 0, 0],
rotation: [0, 0, 0],
},
times: sizeT,
};
/* eslint-enable @typescript-eslint/naming-convention */
// got some data, now let's construct the volume.
const vol = new Volume(imgdata);
const storepath = imagegroup + "/" + dataset2.path;
// do each channel on a worker
for (let i = 0; i < c; ++i) {
const worker = new Worker(new URL("./workers/FetchZarrWorker", import.meta.url));
worker.onmessage = function (e) {
const u8 = e.data.data;
const channel = e.data.channel;
vol.setChannelDataFromVolume(channel, u8);
if (onChannelLoaded) {
// make up a unique name? or have caller pass this in?
onChannelLoaded(urlStore + "/" + imageName, vol, channel);
}
worker.terminate();
};
worker.onerror = function (e) {
alert("Error: Line " + e.lineno + " in " + e.filename + ": " + e.message);
};
worker.postMessage({
urlStore: urlStore,
time: Math.min(t, sizeT),
channel: i,
downsampleZ: downsampleZ,
path: storepath,
});
}
return vol;
}
static async loadOpenCell(onChannelLoaded: PerChannelCallback): Promise<Volume> {
const numChannels = 2;
// HQTILE or LQTILE
// make a json metadata dict for the two channels:
const urls = [
{
name: "https://opencell.czbiohub.org/data/opencell-microscopy/roi/czML0383-P0007/czML0383-P0007-A02-PML0308-S13_ROI-0424-0025-0600-0600-LQTILE-CH405.jpg",
channels: [0],
},
{
name: "https://opencell.czbiohub.org/data/opencell-microscopy/roi/czML0383-P0007/czML0383-P0007-A02-PML0308-S13_ROI-0424-0025-0600-0600-LQTILE-CH488.jpg",
channels: [1],
},
];
// we know these are standardized to 600x600, two channels, one channel per jpg.
const chnames: string[] = ["DNA", "Structure"];
/* eslint-disable @typescript-eslint/naming-convention */
const imgdata: ImageInfo = {
width: 600,
height: 600,
channels: numChannels,
channel_names: chnames,
rows: 27,
cols: 1,
tiles: 27,
tile_width: 600,
tile_height: 600,
// for webgl reasons, it is best for atlas_width and atlas_height to be <= 2048
// and ideally a power of 2. This generally implies downsampling the original volume data for display in this viewer.
atlas_width: 600,
atlas_height: 16200,
pixel_size_x: 1,
pixel_size_y: 1,
pixel_size_z: 2,
name: "TEST",
version: "1.0",
transform: {
translation: [0, 0, 0],
rotation: [0, 0, 0],
},
times: 1,
};
/* eslint-enable @typescript-eslint/naming-convention */
// got some data, now let's construct the volume.
const vol = new Volume(imgdata);
this.loadVolumeAtlasData(vol, urls, onChannelLoaded);
return vol;
}
static async loadTiff(url: string, onChannelLoaded: PerChannelCallback): Promise<Volume> {
const tiff = await fromUrl(url);
// DO NOT DO THIS, ITS SLOW
// const imagecount = await tiff.getImageCount();
// read the FIRST image
const image = await tiff.getImage();
const tiffimgdesc = image.getFileDirectory().ImageDescription;
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(tiffimgdesc, "text/xml");
const omeEl = xmlDoc.getElementsByTagName("OME")[0];
const image0El = omeEl.getElementsByTagName("Image")[0];
const pixelsEl = image0El.getElementsByTagName("Pixels")[0];
const sizex = Number(pixelsEl.getAttribute("SizeX"));
const sizey = Number(pixelsEl.getAttribute("SizeY"));
const sizez = Number(pixelsEl.getAttribute("SizeZ"));
const sizec = Number(pixelsEl.getAttribute("SizeC"));
const sizet = Number(pixelsEl.getAttribute("SizeT"));
const pixeltype = pixelsEl.getAttribute("Type");
const dimensionorder: string = pixelsEl.getAttribute("DimensionOrder") || "XYZCT";
// ignoring units for now
const pixelsizex = Number(pixelsEl.getAttribute("PhysicalSizeX"));
const pixelsizey = Number(pixelsEl.getAttribute("PhysicalSizeY"));
const pixelsizez = Number(pixelsEl.getAttribute("PhysicalSizeZ"));
const channelnames: string[] = [];
const channelsEls = pixelsEl.getElementsByTagName("Channel");
for (let i = 0; i < channelsEls.length; ++i) {
const name = channelsEls[i].getAttribute("Name");
const id = channelsEls[i].getAttribute("ID");
channelnames.push(name ? name : id ? id : "Channel" + i);
}
// compare with sizex, sizey
//const width = image.getWidth();
//const height = image.getHeight();
// TODO allow user setting of this downsampling info?
// TODO allow ROI selection: range of x,y,z,c for a given t
const { nrows, ncols } = computePackedAtlasDims(sizez, sizex, sizey);
// fit tiles to max of 2048x2048?
const targetSize = 2048;
const tilesizex = Math.floor(targetSize / ncols);
const tilesizey = Math.floor(targetSize / nrows);
// load tiff and check metadata
/* eslint-disable @typescript-eslint/naming-convention */
const imgdata: ImageInfo = {
width: sizex,
height: sizey,
channels: sizec,
channel_names: channelnames,
rows: nrows,
cols: ncols,
tiles: sizez,
tile_width: tilesizex,
tile_height: tilesizey,
// for webgl reasons, it is best for atlas_width and atlas_height to be <= 2048
// and ideally a power of 2. This generally implies downsampling the original volume data for display in this viewer.
atlas_width: tilesizex * ncols,
atlas_height: tilesizey * nrows,
pixel_size_x: pixelsizex,
pixel_size_y: pixelsizey,
pixel_size_z: pixelsizez,
name: "TEST",
version: "1.0",
transform: {
translation: [0, 0, 0],
rotation: [0, 0, 0],
},
times: sizet,
};
/* eslint-enable @typescript-eslint/naming-convention */
const vol = new Volume(imgdata);
// do each channel on a worker?
for (let channel = 0; channel < sizec; ++channel) {
const params = {
channel: channel,
// these are target xy sizes for the in-memory volume data
// they may or may not be the same size as original xy sizes
tilesizex: tilesizex,
tilesizey: tilesizey,
sizec: sizec,
sizez: sizez,
dimensionOrder: dimensionorder,
bytesPerSample: pixeltype === "uint8" ? 1 : pixeltype === "uint16" ? 2 : 4,
url: url,
};
const worker = new Worker(new URL("./workers/FetchTiffWorker", import.meta.url));
worker.onmessage = function (e) {
const u8 = e.data.data;
const channel = e.data.channel;
vol.setChannelDataFromVolume(channel, u8);
if (onChannelLoaded) {
// make up a unique name? or have caller pass this in?
onChannelLoaded(url, vol, channel);
}
worker.terminate();
};
worker.onerror = function (e) {
alert("Error: Line " + e.lineno + " in " + e.filename + ": " + e.message);
};
worker.postMessage(params);
}
return vol;
}
}