@vitessce/image-utils 4.1.3 → 4.2.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.
@@ -0,0 +1,74 @@
1
+ /* eslint-disable no-underscore-dangle */
2
+ // Reads of OME-TIFF metadata that viv does not expose: they go through the
3
+ // TIFF directory (IFD) of one plane, which viv's TiffPixelSource returns from
4
+ // its private _indexer.
5
+
6
+ type TiffImage = {
7
+ getTileHeight(): number;
8
+ getTileWidth(): number;
9
+ fileDirectory?: Record<string, unknown>;
10
+ };
11
+
12
+ export type TiffPixelSourceLike = {
13
+ labels: string[];
14
+ _indexer(selection: Record<string, number>): Promise<TiffImage>;
15
+ };
16
+
17
+ // Selection of the first plane, for reading a level's TIFF directory.
18
+ function getFirstPlaneSelection(labels: string[]): Record<string, number> {
19
+ return Object.fromEntries(labels
20
+ .filter(axis => axis !== 'y' && axis !== 'x')
21
+ .map(axis => [axis, 0]));
22
+ }
23
+
24
+ /**
25
+ * Tile shape of one OME-TIFF pyramid level, from its own TIFF directory.
26
+ * viv's tileSize is level 0's for every level, but pyramid writers often shrink
27
+ * tiles at coarse levels (e.g. 1024, 928, 464). Stripped (untiled) images give
28
+ * the image width and RowsPerStrip.
29
+ * @param pixelSource viv TiffPixelSource of the level.
30
+ * @returns [tileHeight, tileWidth]
31
+ */
32
+ export async function readTiffTileShape(
33
+ pixelSource: TiffPixelSourceLike,
34
+ ): Promise<[number, number]> {
35
+ const image = await pixelSource._indexer(getFirstPlaneSelection(pixelSource.labels));
36
+ return [image.getTileHeight(), image.getTileWidth()];
37
+ }
38
+
39
+ /**
40
+ * Sample value range of an OME-TIFF, if its writer recorded one: the TIFF
41
+ * SMinSampleValue / SMaxSampleValue tags, or the same keys in the OME-XML
42
+ * original metadata (kept by Bio-Formats conversions). Neither is part of the
43
+ * OME-XML model, and viv's parser drops annotations, so this reads the first IFD.
44
+ * Tags hold one value per sample; this returns the overall range.
45
+ * @param pixelSource viv TiffPixelSource of level 0.
46
+ * @returns [min, max], or null if not recorded.
47
+ */
48
+ export async function readTiffSampleRange(
49
+ pixelSource: TiffPixelSourceLike,
50
+ ): Promise<[number, number] | null> {
51
+ try {
52
+ const image = await pixelSource._indexer(getFirstPlaneSelection(pixelSource.labels));
53
+ const directory = image?.fileDirectory ?? {};
54
+ const fromTag = (value: unknown, reduce: (...values: number[]) => number) => (
55
+ value === undefined || typeof value === 'number'
56
+ ? value as number | undefined
57
+ : reduce(...Array.from(value as ArrayLike<number>))
58
+ );
59
+ let min = fromTag(directory.SMinSampleValue, Math.min);
60
+ let max = fromTag(directory.SMaxSampleValue, Math.max);
61
+ const description = directory.ImageDescription;
62
+ if (!(Number.isFinite(min) && Number.isFinite(max)) && typeof description === 'string') {
63
+ const fromXml = (key: string) => Number(description
64
+ .match(new RegExp(`<Key>${key}</Key>\\s*<Value>([^<]*)</Value>`))?.[1]);
65
+ min = fromXml('SMinSampleValue');
66
+ max = fromXml('SMaxSampleValue');
67
+ }
68
+ return Number.isFinite(min) && Number.isFinite(max) && (max as number) > (min as number)
69
+ ? [min as number, max as number]
70
+ : null;
71
+ } catch (error) {
72
+ return null; // The range is optional metadata.
73
+ }
74
+ }