Get slice.
345 {
346 if (cache == null)
347 cache = new TileCache(this);
348 var curLevel = Image.BioImage.LevelFromResolution(sliceInfo.Resolution);
349 var curUnitsPerPixel = Schema.Resolutions[curLevel].UnitsPerPixel;
350
351 var tileInfos = Schema.GetTileInfos(sliceInfo.Extent, curLevel);
352 if (tileInfos.Count() == 0)
353 {
354 tileInfos = Schema.GetTileInfos(sliceInfo.Extent.PixelToWorldInvertedY(curUnitsPerPixel), curLevel);
355 }
356 await
FetchTilesAsync(tileInfos.ToList(), curLevel, sliceInfo.Coordinate, PyramidalOrign, PyramidalSize);
357
358 var srcPixelExtent = sliceInfo.Extent.WorldToPixelInvertedY(curUnitsPerPixel);
359 var dstPixelExtent = sliceInfo.Extent.WorldToPixelInvertedY(sliceInfo.Resolution);
360 var dstPixelHeight = sliceInfo.Parame.DstPixelHeight > 0 ? sliceInfo.Parame.DstPixelHeight : dstPixelExtent.Height;
361 var dstPixelWidth = sliceInfo.Parame.DstPixelWidth > 0 ? sliceInfo.Parame.DstPixelWidth : dstPixelExtent.Width;
362 destExtent = new Extent(0, 0, dstPixelWidth, dstPixelHeight);
363 sourceExtent = srcPixelExtent;
364 if (UseGPU)
365 {
366 try
367 {
368 if (stitch == null)
369 {
370 Console.WriteLine("GPU stitching not initialized - falling back to CPU");
371 UseGPU = false;
372 }
373 else if (tileInfos.Count() > 0)
374 {
375
376 var schemaTileWidth = Schema.Resolutions[curLevel].TileWidth;
377 var schemaTileHeight = Schema.Resolutions[curLevel].TileHeight;
378
379
380 foreach (BruTile.TileInfo t in tileInfos)
381 {
382 if (!stitch.HasTile(t))
383 {
384 TileInformation tf = new TileInformation(t.Index, t.Extent, sliceInfo.Coordinate);
385 byte[] tileData = await cache.GetTile(tf);
386 if (tileData != null)
387 {
388
389 var curTileWidth = (int)(t.Extent.MaxX > Schema.Extent.Width
390 ? schemaTileWidth - (t.Extent.MaxX - Schema.Extent.Width) / curUnitsPerPixel
391 : schemaTileWidth);
392 var curTileHeight = (int)(-t.Extent.MinY > Schema.Extent.Height
393 ? schemaTileHeight - (-t.Extent.MinY - Schema.Extent.Height) / curUnitsPerPixel
394 : schemaTileHeight);
395
396
397 int expectedSize = curTileWidth * curTileHeight * 4;
398 if (tileData.Length != expectedSize)
399 {
400
401 int pixelCount = tileData.Length / 4;
402 if (pixelCount == schemaTileWidth * schemaTileHeight)
403 {
404 curTileWidth = schemaTileWidth;
405 curTileHeight = schemaTileHeight;
406 }
407 else
408 {
409
410 curTileWidth = schemaTileWidth;
411 curTileHeight = pixelCount / schemaTileWidth;
412 if (curTileWidth * curTileHeight != pixelCount)
413 {
414
415 curTileWidth = (int)Math.Sqrt(pixelCount);
416 curTileHeight = curTileWidth;
417 }
418 }
419 }
420
421 var gpuTile = new Stitch.GpuTile(t, tileData);
422 stitch.AddTile(gpuTile);
423 }
424 }
425 }
426 }
427 }
428 catch (Exception e)
429 {
430 Console.WriteLine(e.Message.ToString());
431 UseVips = true;
432 UseGPU = false;
433 }
434 }
435 else
436 if (UseVips)
437 {
438 try
439 {
440 List<Tuple<Extent, byte[]>> tiles = new List<Tuple<Extent, byte[]>>();
441 foreach (BruTile.TileInfo t in tileInfos)
442 {
443 TileInformation tf = new TileInformation(t.Index, t.Extent, sliceInfo.Coordinate);
444 byte[] c = await cache.GetTile(tf);
445 if (c != null)
446 tiles.Add(Tuple.Create(t.Extent.WorldToPixelInvertedY(curUnitsPerPixel), c));
447 }
448 NetVips.Image im = null;
449 if (Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format16bppGrayScale)
450 im = ImageUtil.JoinVips16(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
451 else if (Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format24bppRgb)
452 im = ImageUtil.JoinVipsRGB24(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
453 return im.WriteToMemory();
454 }
455 catch (Exception e)
456 {
457 UseVips = false;
458 Console.WriteLine("Failed to use LibVips please install Libvips for your platform.");
459 Console.WriteLine(e.Message);
460 }
461 }
462 try
463 {
464 Image im = null;
465 List<Tuple<Extent, byte[]>> tiles = new List<Tuple<Extent, byte[]>>();
466 foreach (BruTile.TileInfo t in tileInfos)
467 {
468 TileInformation tf = new TileInformation(t.Index, t.Extent, sliceInfo.Coordinate);
469 byte[] c = await cache.GetTile(tf);
470 if (c != null)
471 tiles.Add(Tuple.Create(t.Extent.WorldToPixelInvertedY(curUnitsPerPixel), c));
472 }
473 if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format16bppGrayScale)
474 {
475 im = ImageUtil.Join16(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
476 byte[] bts = Get16Bytes((Image<L16>)im);
477 im.Dispose();
478 return bts;
479 }
480 else if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format24bppRgb)
481 {
482 im = ImageUtil.JoinRGB24(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
483 byte[] bts = GetRgb24Bytes((Image<Rgb24>)im);
484 im.Dispose();
485 return bts;
486 }
487 else if (this.Image.BioImage.Resolutions[curLevel].PixelFormat == PixelFormat.Format8bppIndexed)
488 {
489 im = ImageUtil.Join8Bit(tiles, srcPixelExtent, new Extent(0, 0, dstPixelWidth, dstPixelHeight));
490 byte[] bts = Get8BitBytes((Image<L8>)im);
491 im.Dispose();
492 return bts;
493 }
494 }
495 catch (Exception er)
496 {
497 Console.WriteLine(er.Message);
498 return null;
499 }
500 return null;
501 }
async Task FetchTilesAsync(List< BruTile.TileInfo > tiles, int level, ZCT coordinate, PointD PyramidalOrigin, AForge.Size PyramidalSize)
Fetches tiles in priority order: center viewport first, then edges. Ensures visible content appears b...
Definition ISlideSource.cs:280