BioLib  3.6.2
A GUI-less version of Bio .NET library for editing & annotating various microscopy image formats.
Loading...
Searching...
No Matches
BioLib.SlideSourceBase Class Referenceabstract
Inheritance diagram for BioLib.SlideSourceBase:
BioLib.ISlideSource BioLib.ISliceProvider BioLib.ISlideExternInfo BioLib.SlideBase

Public Member Functions

async Task< byte[]> GetSlice (SliceInfo sliceInfo)
 Get slice.
 
byte[] Get8BitBytes (Image< L8 > image)
 
byte[] GetRgb24Bytes (Image< Rgb24 > image)
 
byte[] Get16Bytes (Image< L16 > image)
 
IReadOnlyDictionary< string, byte[]> GetExternImages ()
 Extern image.
 
void Dispose ()
 
async Task< byte[]> GetTileAsync (TileInformation tileInfo)
 
async Task< byte[]> GetTileAsync (BruTile.TileInfo tileInfo)
 
- Public Member Functions inherited from BioLib.ISliceProvider
- Public Member Functions inherited from BioLib.ISlideExternInfo

Static Public Member Functions

static void Resister (string extensionUpper, Func< string, bool, ISlideSource > factory)
 resister decode for Specific format
 
static ISlideSource Create (BioImage source, SlideImage im, bool enableCache=true)
 
static byte[] ConvertRgbaToRgb (byte[] rgbaArray)
 

Public Attributes

TileCache cache = null
 

Static Public Attributes

static Extent destExtent
 
static Extent sourceExtent
 
static double curUnitsPerPixel = 1
 
static bool UseVips = true
 

Protected Member Functions

virtual void Dispose (bool disposing)
 

Properties

static bool UseRealResolution = true [get, set]
 
double MinUnitsPerPixel [get, protected set]
 um/pixel
 
SlideImage Image [get, set]
 
ITileSchema Schema [get, protected set]
 
string Name [get, protected set]
 
Attribution Attribution [get, protected set]
 
IReadOnlyDictionary< string, object > ExternInfo [get, protected set]
 Extern info.
 
string Source [get, protected set]
 File path.
 
- Properties inherited from BioLib.ISliceProvider
- Properties inherited from BioLib.ISlideExternInfo

Detailed Description

Definition at line 135 of file ISlideSource.cs.

Member Function Documentation

◆ ConvertRgbaToRgb()

static byte[] BioLib.SlideSourceBase.ConvertRgbaToRgb ( byte[] rgbaArray)
static

Definition at line 384 of file ISlideSource.cs.

385 {
386 // Initialize a new byte array for RGB24 format
387 byte[] rgbArray = new byte[(rgbaArray.Length / 4) * 3];
388
389 for (int i = 0, j = 0; i < rgbaArray.Length; i += 4, j += 3)
390 {
391 // Copy the R, G, B values, skip the A value
392 rgbArray[j] = rgbaArray[i + 2]; // B
393 rgbArray[j + 1] = rgbaArray[i + 1]; // G
394 rgbArray[j + 2] = rgbaArray[i]; // R
395 }
396
397 return rgbArray;
398 }

◆ Create()

static ISlideSource BioLib.SlideSourceBase.Create ( BioImage source,
SlideImage im,
bool enableCache = true )
static

Definition at line 153 of file ISlideSource.cs.

154 {
155
156 var ext = Path.GetExtension(source.file).ToUpper();
157 try
158 {
159 if (keyValuePairs.TryGetValue(ext, out var factory) && factory != null)
160 return factory.Invoke(source.file, enableCache);
161
162 if (!string.IsNullOrEmpty(SlideBase.DetectVendor(source.file)))
163 {
164 SlideBase b = new SlideBase(source, im, enableCache);
165
166 }
167 }
168 catch (Exception e)
169 {
170 Console.WriteLine(e.Message);
171 }
172 return null;
173 }

◆ Dispose() [1/2]

void BioLib.SlideSourceBase.Dispose ( )

Definition at line 348 of file ISlideSource.cs.

349 {
350 Dispose(disposing: true);
351 GC.SuppressFinalize(this);
352 }

◆ Dispose() [2/2]

virtual void BioLib.SlideSourceBase.Dispose ( bool disposing)
protectedvirtual

Definition at line 331 of file ISlideSource.cs.

332 {
333 if (!disposedValue)
334 {
335 if (disposing)
336 {
337 //_bgraCache.Dispose();
338 }
339 disposedValue = true;
340 }
341 }

◆ Get16Bytes()

byte[] BioLib.SlideSourceBase.Get16Bytes ( Image< L16 > image)

Definition at line 293 of file ISlideSource.cs.

294 {
295 int width = image.Width;
296 int height = image.Height;
297 byte[] bytes = new byte[width * height * 2];
298
299 int byteIndex = 0;
300 for (int y = 0; y < height; y++)
301 {
302 for (int x = 0; x < width; x++)
303 {
304 L16 pixel = image[x, y];
305 byte[] bts = BitConverter.GetBytes(pixel.PackedValue);
306 bytes[byteIndex++] = bts[0];
307 bytes[byteIndex++] = bts[1];
308 }
309 }
310
311 return bytes;
312 }

◆ Get8BitBytes()

byte[] BioLib.SlideSourceBase.Get8BitBytes ( Image< L8 > image)

Definition at line 255 of file ISlideSource.cs.

256 {
257 int width = image.Width;
258 int height = image.Height;
259 byte[] rgbBytes = new byte[width * height]; // 3 bytes per pixel (RGB)
260
261 int byteIndex = 0;
262 for (int y = 0; y < height; y++)
263 {
264 for (int x = 0; x < width; x++)
265 {
266 L8 pixel = image[x, y];
267 rgbBytes[byteIndex++] = pixel.PackedValue;
268 }
269 }
270
271 return rgbBytes;
272 }

◆ GetExternImages()

IReadOnlyDictionary< string, byte[]> BioLib.SlideSourceBase.GetExternImages ( )
abstract

Extern image.

Returns

Implements BioLib.ISlideExternInfo.

◆ GetRgb24Bytes()

byte[] BioLib.SlideSourceBase.GetRgb24Bytes ( Image< Rgb24 > image)

Definition at line 273 of file ISlideSource.cs.

274 {
275 int width = image.Width;
276 int height = image.Height;
277 byte[] rgbBytes = new byte[width * height * 3]; // 3 bytes per pixel (RGB)
278
279 int byteIndex = 0;
280 for (int y = 0; y < height; y++)
281 {
282 for (int x = 0; x < width; x++)
283 {
284 Rgb24 pixel = image[x, y];
285 rgbBytes[byteIndex++] = pixel.B;
286 rgbBytes[byteIndex++] = pixel.G;
287 rgbBytes[byteIndex++] = pixel.R;
288 }
289 }
290
291 return rgbBytes;
292 }

◆ GetSlice()

async Task< byte[]> BioLib.SlideSourceBase.GetSlice ( SliceInfo sliceInfo)

Get slice.

Parameters
sliceInfoSlice info
Returns

Implements BioLib.ISliceProvider.

Definition at line 181 of file ISlideSource.cs.

182 {
183 if (cache == null)
184 cache = new TileCache(this);
185 var curLevel = Image.BioImage.LevelFromResolution(sliceInfo.Resolution);
186 var curUnitsPerPixel = Schema.Resolutions[curLevel].UnitsPerPixel;
187 var tileInfos = Schema.GetTileInfos(sliceInfo.Extent, curLevel);
188 List<Tuple<Extent, byte[]>> tiles = new List<Tuple<Extent, byte[]>>();
189 foreach (BruTile.TileInfo t in tileInfos)
190 {
191 TileInformation tf = new TileInformation();
192 tf.Extent = t.Extent;
193 tf.Coordinate = sliceInfo.Coordinate;
194 tf.Index = t.Index;
195 byte[] c = await cache.GetTile(tf);
196 if(c!=null)
197 tiles.Add(Tuple.Create(t.Extent.WorldToPixelInvertedY(curUnitsPerPixel), c));
198 }
199 var srcPixelExtent = sliceInfo.Extent.WorldToPixelInvertedY(curUnitsPerPixel);
200 var dstPixelExtent = sliceInfo.Extent.WorldToPixelInvertedY(sliceInfo.Resolution);
201 var dstPixelHeight = sliceInfo.Parame.DstPixelHeight > 0 ? sliceInfo.Parame.DstPixelHeight : dstPixelExtent.Height;
202 var dstPixelWidth = sliceInfo.Parame.DstPixelWidth > 0 ? sliceInfo.Parame.DstPixelWidth : dstPixelExtent.Width;
203 destExtent = new Extent(0, 0, dstPixelWidth, dstPixelHeight);
204 sourceExtent = srcPixelExtent;
205 if (UseVips)
206 {
207 try
208 {
209 NetVips.Image im = null;
210 if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format16bppGrayScale)
211 im = ImageUtil.JoinVips16(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
212 else if(this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format24bppRgb)
213 im = ImageUtil.JoinVipsRGB24(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
214 return im.WriteToMemory();
215 }
216 catch (Exception e)
217 {
218 UseVips = false;
219 Console.WriteLine("Failed to use LibVips please install Libvips for your platform.");
220 Console.WriteLine(e.Message);
221 }
222 }
223 try
224 {
225 Image im = null;
226 if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format16bppGrayScale)
227 {
228 im = ImageUtil.Join16(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
229 byte[] bts = Get16Bytes((Image<L16>)im);
230 im.Dispose();
231 return bts;
232 }
233 else if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format24bppRgb)
234 {
235 im = ImageUtil.JoinRGB24(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
236 byte[] bts = GetRgb24Bytes((Image<Rgb24>)im);
237 im.Dispose();
238 return bts;
239 }
240 else if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format8bppIndexed)
241 {
242 im = ImageUtil.Join8Bit(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
243 byte[] bts = Get8BitBytes((Image<L8>)im);
244 im.Dispose();
245 return bts;
246 }
247 }
248 catch (Exception er)
249 {
250 Console.WriteLine(er.Message);
251 return null;
252 }
253 return null;
254 }

◆ GetTileAsync() [1/2]

async Task< byte[]> BioLib.SlideSourceBase.GetTileAsync ( BruTile.TileInfo tileInfo)

Definition at line 369 of file ISlideSource.cs.

370 {
371 if (tileInfo == null)
372 return null;
373 var r = Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
374 var tileWidth = Schema.Resolutions[tileInfo.Index.Level].TileWidth;
375 var tileHeight = Schema.Resolutions[tileInfo.Index.Level].TileHeight;
376 var curLevelOffsetXPixel = tileInfo.Extent.MinX / Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
377 var curLevelOffsetYPixel = -tileInfo.Extent.MaxY / Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
378 var curTileWidth = (int)(tileInfo.Extent.MaxX > Schema.Extent.Width ? tileWidth - (tileInfo.Extent.MaxX - Schema.Extent.Width) / r : tileWidth);
379 var curTileHeight = (int)(-tileInfo.Extent.MinY > Schema.Extent.Height ? tileHeight - (-tileInfo.Extent.MinY - Schema.Extent.Height) / r : tileHeight);
380
381 var bgraData = await Image.ReadRegionAsync(tileInfo.Index.Level, (long)curLevelOffsetXPixel, (long)curLevelOffsetYPixel, curTileWidth, curTileHeight, new ZCT());
382 return bgraData;
383 }

◆ GetTileAsync() [2/2]

async Task< byte[]> BioLib.SlideSourceBase.GetTileAsync ( TileInformation tileInfo)

Definition at line 354 of file ISlideSource.cs.

355 {
356 if (tileInfo == null)
357 return null;
358 var r = Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
359 var tileWidth = Schema.Resolutions[tileInfo.Index.Level].TileWidth;
360 var tileHeight = Schema.Resolutions[tileInfo.Index.Level].TileHeight;
361 var curLevelOffsetXPixel = tileInfo.Extent.MinX / Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
362 var curLevelOffsetYPixel = -tileInfo.Extent.MaxY / Schema.Resolutions[tileInfo.Index.Level].UnitsPerPixel;
363 var curTileWidth = (int)(tileInfo.Extent.MaxX > Schema.Extent.Width ? tileWidth - (tileInfo.Extent.MaxX - Schema.Extent.Width) / r : tileWidth);
364 var curTileHeight = (int)(-tileInfo.Extent.MinY > Schema.Extent.Height ? tileHeight - (-tileInfo.Extent.MinY - Schema.Extent.Height) / r : tileHeight);
365 var bgraData = await Image.ReadRegionAsync(tileInfo.Index.Level, (long)curLevelOffsetXPixel, (long)curLevelOffsetYPixel, curTileWidth, curTileHeight,tileInfo.Coordinate);
366 return bgraData;
367 }

◆ Resister()

static void BioLib.SlideSourceBase.Resister ( string extensionUpper,
Func< string, bool, ISlideSource > factory )
static

resister decode for Specific format

Parameters
extensionUpperdot and extension upper
factoryfile path,enable cache,decoder

Definition at line 147 of file ISlideSource.cs.

148 {
149 keyValuePairs.Add(extensionUpper, factory);
150 }

Member Data Documentation

◆ cache

TileCache BioLib.SlideSourceBase.cache = null

Definition at line 180 of file ISlideSource.cs.

◆ curUnitsPerPixel

double BioLib.SlideSourceBase.curUnitsPerPixel = 1
static

Definition at line 178 of file ISlideSource.cs.

◆ destExtent

Extent BioLib.SlideSourceBase.destExtent
static

Definition at line 176 of file ISlideSource.cs.

◆ sourceExtent

Extent BioLib.SlideSourceBase.sourceExtent
static

Definition at line 177 of file ISlideSource.cs.

◆ UseVips

bool BioLib.SlideSourceBase.UseVips = true
static

Definition at line 179 of file ISlideSource.cs.

Property Documentation

◆ Attribution

Attribution BioLib.SlideSourceBase.Attribution
getprotected set

Definition at line 320 of file ISlideSource.cs.

320{ get; protected set; }

◆ ExternInfo

IReadOnlyDictionary<string, object> BioLib.SlideSourceBase.ExternInfo
getprotected set

Extern info.

Implements BioLib.ISlideExternInfo.

Definition at line 322 of file ISlideSource.cs.

322{ get; protected set; }

◆ Image

SlideImage BioLib.SlideSourceBase.Image
getset

Definition at line 314 of file ISlideSource.cs.

314{ get; set; }

◆ MinUnitsPerPixel

double BioLib.SlideSourceBase.MinUnitsPerPixel
getprotected set

um/pixel

Implements BioLib.ISliceProvider.

Definition at line 175 of file ISlideSource.cs.

175{ get; protected set; }

◆ Name

string BioLib.SlideSourceBase.Name
getprotected set

Definition at line 318 of file ISlideSource.cs.

318{ get; protected set; }

◆ Schema

ITileSchema BioLib.SlideSourceBase.Schema
getprotected set

Definition at line 316 of file ISlideSource.cs.

316{ get; protected set; }

◆ Source

string BioLib.SlideSourceBase.Source
getprotected set

File path.

Implements BioLib.ISlideExternInfo.

Definition at line 324 of file ISlideSource.cs.

324{ get; protected set; }

◆ UseRealResolution

bool BioLib.SlideSourceBase.UseRealResolution = true
staticgetset

Definition at line 138 of file ISlideSource.cs.

138{ get; set; } = true;

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