@aics/vole-core 4.3.0 → 4.3.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/es/loaders/TiffLoader.js +30 -11
- package/package.json +1 -1
package/es/loaders/TiffLoader.js
CHANGED
|
@@ -4,20 +4,17 @@ import { ThreadableVolumeLoader, LoadSpec } from "./IVolumeLoader.js";
|
|
|
4
4
|
import { computePackedAtlasDims, MAX_ATLAS_EDGE } from "./VolumeLoaderUtils.js";
|
|
5
5
|
import { VolumeLoadError, VolumeLoadErrorType, wrapVolumeLoadError } from "./VolumeLoadError.js";
|
|
6
6
|
import { CImageInfo } from "../ImageInfo.js";
|
|
7
|
-
function
|
|
7
|
+
function trimNull(xml) {
|
|
8
8
|
// trim trailing unicode zeros?
|
|
9
|
-
|
|
10
|
-
const expr = /[\u0000]$/g;
|
|
11
|
-
return xml.trim().replace(expr, "").trim();
|
|
9
|
+
return xml && xml.trim().replace(/\0/g, "").trim();
|
|
12
10
|
}
|
|
13
11
|
function getOME(xml) {
|
|
14
|
-
if (xml
|
|
12
|
+
if (typeof xml !== "string") {
|
|
15
13
|
return undefined;
|
|
16
14
|
}
|
|
17
|
-
const prepared = prepareXML(xml);
|
|
18
15
|
const parser = new DOMParser();
|
|
19
16
|
try {
|
|
20
|
-
const xmlDoc = parser.parseFromString(
|
|
17
|
+
const xmlDoc = parser.parseFromString(xml, "text/xml");
|
|
21
18
|
return xmlDoc.getElementsByTagName("OME")[0];
|
|
22
19
|
} catch (e) {
|
|
23
20
|
return undefined;
|
|
@@ -104,20 +101,42 @@ class TiffLoader extends ThreadableVolumeLoader {
|
|
|
104
101
|
// const imagecount = await tiff.getImageCount();
|
|
105
102
|
// read the FIRST image
|
|
106
103
|
const image = await tiff.getImage().catch(wrapVolumeLoadError("Failed to open TIFF image", VolumeLoadErrorType.NOT_FOUND));
|
|
107
|
-
const
|
|
104
|
+
const image0DescriptionRaw = image.getFileDirectory().ImageDescription;
|
|
105
|
+
// Get rid of null terminator, if it's there (`JSON.parse` doesn't know what to do with it)
|
|
106
|
+
const image0Description = trimNull(image0DescriptionRaw);
|
|
107
|
+
const omeEl = getOME(image0Description);
|
|
108
108
|
if (omeEl !== undefined) {
|
|
109
109
|
const image0El = omeEl.getElementsByTagName("Image")[0];
|
|
110
110
|
this.dims = getOMEDims(image0El);
|
|
111
111
|
} else {
|
|
112
112
|
console.warn("Could not read OME-TIFF metadata from file. Doing our best with base TIFF metadata.");
|
|
113
113
|
this.dims = new OMEDims();
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
let shape = [];
|
|
115
|
+
if (typeof image0Description === "string") {
|
|
116
|
+
try {
|
|
117
|
+
const description = JSON.parse(image0Description);
|
|
118
|
+
if (Array.isArray(description.shape)) {
|
|
119
|
+
shape = description.shape;
|
|
120
|
+
}
|
|
121
|
+
// eslint-disable-next-line no-empty
|
|
122
|
+
} catch (_e) {}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// if `ImageDescription` is valid JSON with a `shape` field, we expect it to be an array of [t?, c?, z?, y, x].
|
|
126
|
+
this.dims.sizex = shape[shape.length - 1] ?? image.getWidth();
|
|
127
|
+
this.dims.sizey = shape[shape.length - 2] ?? image.getHeight();
|
|
128
|
+
this.dims.sizez = shape[shape.length - 3] ?? (await tiff.getImageCount());
|
|
129
|
+
|
|
116
130
|
// TODO this is a big hack/assumption about only loading multi-source tiffs that are not OMETIFF.
|
|
117
131
|
// We really have to check each url in the array for sizec to get the total number of channels
|
|
118
132
|
// See combinedNumChannels in ImageInfo below.
|
|
119
133
|
// Also compare with how OMEZarrLoader does this.
|
|
120
|
-
|
|
134
|
+
if (this.url.length > 1) {
|
|
135
|
+
// if multiple urls, assume one channel per url
|
|
136
|
+
this.dims.sizec = this.url.length;
|
|
137
|
+
} else {
|
|
138
|
+
this.dims.sizec = shape[shape.length - 4] ?? 1;
|
|
139
|
+
}
|
|
121
140
|
this.dims.pixeltype = getPixelType(image.getBytesPerPixel());
|
|
122
141
|
this.dims.channelnames = Array.from({
|
|
123
142
|
length: this.dims.sizec
|