BioLib  4.3.0
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
BioLib.ZarrConversion Class Reference

Static Public Member Functions

static async Task< BioImage > OpenAsBioImageAsync (string source, int multiscaleIndex=0, int datasetIndex=0, string? id=null, CancellationToken ct=default)
 
static async Task< BioImage > ToBioImageAsync (this MultiscaleNode multiscale, int multiscaleIndex=0, int datasetIndex=0, string? id=null, CancellationToken ct=default)
 
static async Task< BioImage > ToBioImageAsync (this ResolutionLevelNode level, string? id=null, CancellationToken ct=default)
 

Member Function Documentation

◆ OpenAsBioImageAsync()

static async Task< BioImage > BioLib.ZarrConversion.OpenAsBioImageAsync ( string source,
int multiscaleIndex = 0,
int datasetIndex = 0,
string? id = null,
CancellationToken ct = default )
static
18 {
19 await using var reader = await ZarrNET.Core.OmeZarrReader.OpenAsync(source, ct).ConfigureAwait(false);
20 var multiscale = await reader.AsMultiscaleImageAsync(ct).ConfigureAwait(false);
21 return await multiscale.ToBioImageAsync(multiscaleIndex, datasetIndex, id ?? source, ct).ConfigureAwait(false);
22 }
MultiscaleNode multiscale
Definition Bio.cs:5265

◆ ToBioImageAsync() [1/2]

static async Task< BioImage > BioLib.ZarrConversion.ToBioImageAsync ( this MultiscaleNode multiscale,
int multiscaleIndex = 0,
int datasetIndex = 0,
string? id = null,
CancellationToken ct = default )
static
30 {
31 var level = await multiscale.OpenResolutionLevelAsync(multiscaleIndex, datasetIndex, ct).ConfigureAwait(false);
32 return await level.ToBioImageAsync(id, ct).ConfigureAwait(false);
33 }

◆ ToBioImageAsync() [2/2]

static async Task< BioImage > BioLib.ZarrConversion.ToBioImageAsync ( this ResolutionLevelNode level,
string? id = null,
CancellationToken ct = default )
static
39 {
40 var axes = level.EffectiveAxes;
41 var shape = level.Shape;
42 string dataType = level.DataType.ToLowerInvariant();
43
44 int sizeZ = GetAxisSize(axes, shape, "z", 1);
45 int sizeT = GetAxisSize(axes, shape, "t", 1);
46 int channelCount = GetAxisSize(axes, shape, "c", 1);
47 bool isRgb = channelCount == 3 && (dataType == "uint8" || dataType == "uint16");
48 int logicalChannels = isRgb ? 1 : Math.Max(1, channelCount);
49 int bitsPerSample = GetBitsPerSample(dataType);
50 APixelFormat pixelFormat = GetPixelFormat(dataType, isRgb);
51
52 string imageId = string.IsNullOrWhiteSpace(id) ? "zarr.ome.zarr" : id;
53 var bio = new BioImage(imageId);
54 bio.ID = imageId;
55 bio.Filename = imageId;
56 bio.file = imageId;
57 bio.littleEndian = BitConverter.IsLittleEndian;
58 bio.Coordinate = new ZCT(0, 0, 0);
59
60 var samplePlane = await level.ReadPlaneAsync(t: 0, c: 0, z: 0, ct: ct).ConfigureAwait(false);
61 int sizeX = samplePlane.Width;
62 int sizeY = samplePlane.Height;
63
64 var pixelSizes = level.GetPixelSize();
65 var (physX, physY, physZ) = MapGeometry(axes, pixelSizes);
66
67 bio.Resolutions.Add(new Resolution(sizeX, sizeY, pixelFormat, physX, physY, physZ, 0, 0, 0));
68
69 if (isRgb)
70 {
71 bio.Channels.Add(new Channel(0, bitsPerSample, 3));
72 }
73 else
74 {
75 for (int c = 0; c < logicalChannels; c++)
76 {
77 bio.Channels.Add(new Channel(c, bitsPerSample, 1));
78 }
79 }
80
81 if (isRgb)
82 {
83 for (int t = 0; t < sizeT; t++)
84 {
85 for (int z = 0; z < sizeZ; z++)
86 {
87 PlaneResult red = (t == 0 && z == 0)
88 ? samplePlane
89 : await level.ReadPlaneAsync(t: t, c: 0, z: z, ct: ct).ConfigureAwait(false);
90 PlaneResult green = await level.ReadPlaneAsync(t: t, c: 1, z: z, ct: ct).ConfigureAwait(false);
91 PlaneResult blue = await level.ReadPlaneAsync(t: t, c: 2, z: z, ct: ct).ConfigureAwait(false);
92
93 byte[] bytes = InterleaveRgbPlanes(red, green, blue, dataType);
94 var bitmap = new Bitmap(
95 bio.ID,
96 red.Width,
97 red.Height,
98 pixelFormat,
99 bytes,
100 new ZCT(z, 0, t),
101 bio.Buffers.Count,
102 null,
103 bio.littleEndian,
104 true);
105
106 TrySeedStats(bitmap);
107 bio.Buffers.Add(bitmap);
108 }
109 }
110 }
111 else
112 {
113 for (int t = 0; t < sizeT; t++)
114 {
115 for (int z = 0; z < sizeZ; z++)
116 {
117 for (int c = 0; c < logicalChannels; c++)
118 {
119 int cIndex = channelCount > 1 ? c : 0;
120 PlaneResult plane = (t == 0 && z == 0 && cIndex == 0)
121 ? samplePlane
122 : await level.ReadPlaneAsync(t: t, c: cIndex, z: z, ct: ct).ConfigureAwait(false);
123
124 var bitmap = new Bitmap(
125 bio.ID,
126 plane.Width,
127 plane.Height,
128 pixelFormat,
129 plane.Data,
130 new ZCT(z, c, t),
131 bio.Buffers.Count,
132 null,
133 bio.littleEndian,
134 true);
135
136 TrySeedStats(bitmap);
137 bio.Buffers.Add(bitmap);
138 }
139 }
140 }
141 }
142
143 bio.UpdateCoords(sizeZ, logicalChannels, sizeT);
144 bio.StackOrder = BioImage.Order.ZCT;
145 bio.Volume = new VolumeD(
146 new Point3D(bio.StageSizeX, bio.StageSizeY, bio.StageSizeZ),
147 new Point3D(bio.PhysicalSizeX * bio.SizeX, bio.PhysicalSizeY * bio.SizeY, bio.PhysicalSizeZ * bio.SizeZ));
148
149 BioImage.AutoThreshold(bio, false);
150 if (bio.bitsPerPixel > 8)
151 bio.StackThreshold(true);
152 else
153 bio.StackThreshold(false);
154
155 Images.AddImage(bio);
156 return bio;
157 }
AForge.Channel Channel
Definition Bio.cs:54
AForge.ZCT ZCT
Definition Bio.cs:62
AForge.Bitmap Bitmap
Definition Bio.cs:53
AForge.PixelFormat APixelFormat
Definition ZarrConversion.cs:2

The documentation for this class was generated from the following file: