BioGTK  6.5.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
SkiaStitch.cs
1using AForge;
2using BioLib;
3using BruTile;
4using CSScripting;
5using OpenSlideGTK;
6using org.apache.xmlgraphics.image.codec.tiff;
7using SkiaSharp;
8using System;
9using System.Collections.Concurrent;
10using System.Collections.Generic;
11using System.Runtime.InteropServices;
12using System.Threading;
13using System.Threading.Tasks;
14
15namespace BioGTK
16{
20 public class SkiaStitchingPipeline : IDisposable
21 {
22 #region Fields and Properties
23
24 private readonly SlideBase _slideSource;
25 private readonly OpenSlideGTK.OpenSlideBase _openSlideSource;
26 private readonly bool _isOpenSlide;
27
28 // Tile cache with thread-safe access
29 private ConcurrentDictionary<string, CachedTile> _tileCache;
30 private int _maxCacheSize;
31 private object _cacheLock = new object();
32
33 // Rendering state
34 private SKSurface _compositeSurface;
35 private SKCanvas _compositeCanvas;
36 private int _currentSurfaceWidth = 600;
37 private int _currentSurfaceHeight = 400;
38
39 // Async operation management
40 private ConcurrentDictionary<string, Task<SKImage>> _pendingTileFetches;
41
42 // Configuration
43 public int TileSize { get; set; } = 256;
44 public int PrefetchRadius { get; set; } = 1;
45 public SKSamplingOptions Sampling { get; set; } = SKSamplingOptions.Default;
46 public bool EnablePrefetch { get; set; } = false;
47
48 #endregion
49
50 #region Nested Types
51
55 private class CachedTile
56 {
57 public SKImage Image { get; set; }
58 public DateTime LastAccessed { get; set; }
59 public long SizeBytes { get; set; }
60 public string Key { get; set; }
61
62 public CachedTile(SKImage image, string key)
63 {
64 Image = image;
65 Key = key;
66 LastAccessed = DateTime.UtcNow;
67 SizeBytes = CalculateSize(image);
68 }
69
70 private long CalculateSize(SKImage image)
71 {
72 if (image == null) return 0;
73 return image.Width * image.Height * 4; // Assume RGBA
74 }
75 }
76
80 public class TileRequest
81 {
82 public int Level
83 {
84 get => Index.Level;
85 set => Index = new TileIndex(Index.Col, Index.Row, value);
86 }
87
88 // FIXED: Col is X, Row is Y
89 public int TileX
90 {
91 get => Index.Col * 256; // Col = X coordinate
92 set
93 {
94 int col = value / 256;
95 Index = new TileIndex(col, Index.Row, Level);
96 }
97 }
98
99 public int TileY
100 {
101 get => Index.Row * 256; // Row = Y coordinate
102 set
103 {
104 int row = value / 256;
105 Index = new TileIndex(Index.Col, row, Level);
106 }
107 }
108
109 public ZCT Coordinate { get; set; }
110 public Extent Extent { get; set; }
111 public TileIndex Index { get; set; }
112
113 public RectangleD Bounds
114 {
115 get
116 {
117 return new RectangleD(
118 Extent.MinX,
119 Extent.MinY,
120 Extent.MaxX - Extent.MinX,
121 Extent.MaxY - Extent.MinY
122 );
123 }
124 }
125
126 public TileInfo TileInfo
127 {
128 get => new TileInfo { Index = Index, Extent = Extent };
129 set
130 {
131 Index = value.Index;
132 Extent = value.Extent;
133 }
134 }
135
136 public TileRequest(TileInfo tf, ZCT coord)
137 {
138 Index = tf.Index;
139 Extent = tf.Extent;
140 Coordinate = coord;
141 }
142
143 public string GetCacheKey()
144 {
145 // Use Col/Row directly to avoid confusion
146 return $"{Index.Col}_{Index.Row}_{Level}_{Coordinate.Z}_{Coordinate.C}_{Coordinate.T}";
147 }
148 }
149 #endregion
150
151 #region Constructor and Initialization
152
156 public SkiaStitchingPipeline(OpenSlideGTK.OpenSlideBase openSlideSource, int maxCacheSizeMB = 512)
157 {
158 _openSlideSource = openSlideSource ?? throw new ArgumentNullException(nameof(openSlideSource));
159 _isOpenSlide = true;
160
161 InitializeCommon(maxCacheSizeMB);
162 }
163
167 public SkiaStitchingPipeline(SlideBase slideSource, int maxCacheSizeMB = 512)
168 {
169 _slideSource = slideSource ?? throw new ArgumentNullException(nameof(slideSource));
170 _isOpenSlide = false;
171
172 InitializeCommon(maxCacheSizeMB);
173 }
174
175 private void InitializeCommon(int maxCacheSizeMB)
176 {
177 _maxCacheSize = maxCacheSizeMB * 1024 * 1024;
178 _tileCache = new ConcurrentDictionary<string, CachedTile>();
179 _pendingTileFetches = new ConcurrentDictionary<string, Task<SKImage>>();
180 }
181
182 #endregion
183
184 #region Public Stitching Methods
185 private readonly SemaphoreSlim _renderSemaphore = new SemaphoreSlim(1, 1);
189 public async Task<SKImage> StitchViewportAsync(
190 PointD origin,
191 double resolution,
192 ZCT coordinate,
193 int viewportWidth = 600,
194 int viewportHeight = 400)
195 {
196 // Calculate viewport parameters
197 int level = CalculateLevelFromResolution(resolution);
198 var tileRequests = CalculateTileRequests(origin, viewportWidth, viewportHeight, level, coordinate, resolution);
199 if (viewportWidth <= 1 || viewportHeight <= 1)
200 {
201 viewportWidth = 600;
202 viewportHeight = 400;
203 }
204 // Create a NEW surface for this render - no shared state, fully parallel-safe
205 using (var surface = SKSurface.Create(new SKImageInfo(
206 viewportWidth,
207 viewportHeight,
208 SKColorType.Rgba8888,
209 SKAlphaType.Premul)))
210 {
211 if (surface == null)
212 {
213 Console.WriteLine("Failed to create SKSurface");
214 return null;
215 }
216
217 var canvas = surface.Canvas;
218 canvas.Clear(SKColors.Gray);
219
220 // Fetch and composite all tiles
221 await CompositeTilesAsync(canvas, tileRequests, origin, resolution);
222
223 // Create and return snapshot
224 return surface.Snapshot();
225 } // Surface automatically disposed here
226 }
227
231 public SKImage StitchRegion(
232 RectangleD region,
233 int level,
234 ZCT coordinate, double resolution, int pxwidth, int pxheight)
235 {
236 var tileRequests = CalculateTileRequests(
237 new PointD(region.X,region.Y),pxwidth, pxheight, level, coordinate, resolution);
238
239 int outputWidth = (int)Math.Ceiling(region.W);
240 int outputHeight = (int)Math.Ceiling(region.H);
241
242 using (var surface = SKSurface.Create(new SKImageInfo(outputWidth, outputHeight)))
243 {
244 var canvas = surface.Canvas;
245 canvas.Clear(SKColors.Transparent);
246
247 foreach (var request in tileRequests)
248 {
249 var tileImage = FetchTileSync(request);
250 if (tileImage != null)
251 {
252 DrawTileToCanvas(canvas,request, new AForge.PointD(region.X,region.Y), resolution, tileImage);
253 }
254 }
255
256 return surface.Snapshot();
257 }
258 }
259
260 #endregion
261
262 #region Tile Fetching and Caching
263
267 private async Task<SKImage> FetchTileAsync(TileRequest request, ZCT coordinate)
268 {
269 string key = request.GetCacheKey();
270
271 // 1. Check cache again to be sure
272 if (_tileCache.TryGetValue(key, out var cached)) return cached.Image;
273
274 // 2. Check if already being fetched
275 if (_pendingTileFetches.TryGetValue(key, out var pendingTask)) return await pendingTask;
276
277 // 3. Create the fetch task
278 var fetchTask = Task.Run(async () =>
279 {
280 try
281 {
282 byte[] data;
283 if (_isOpenSlide)
284 {
285 TileInfo tf = new TileInfo { Index = request.Index, Extent = request.Extent };
286 // OpenSlide usually takes Level, X-index, Y-index
287 data = _openSlideSource.GetTile(tf);
288 }
289 else
290 {
291 // SlideBase usually takes the Index object and ZCT
292 TileInfo tf = new TileInfo { Index = request.Index, Extent = request.Extent };
293 // OpenSlide usually takes Level, X-index, Y-index
294 data = _slideSource.GetTile(tf,request.Coordinate);
295 }
296
297 if (data == null || data.Length == 0) return null;
298
299 // Convert to Skia Image (using the shear-fix logic from before)
300 var levelResolution = CalculateResolutionFromLevel(request.Index.Level);
301 var image = ConvertTileDataToSKImage(data, request);
302
303 if (image != null)
304 {
305 var cachedTile = new CachedTile(image, DateTime.Now.ToString());
306 _tileCache.AddOrUpdate(key, cachedTile, (k, old) => cachedTile);
307 return image;
308 }
309 }
310 catch (Exception ex)
311 {
312 Console.WriteLine($"Fetch error for tile {key}: {ex.Message} + {ex.StackTrace}");
313 }
314 finally
315 {
316 _pendingTileFetches.TryRemove(key, out _);
317 }
318 return null;
319 });
320
321 _pendingTileFetches[key] = fetchTask;
322 return await fetchTask;
323 }
324
328 private SKImage FetchTileSync(TileRequest request)
329 {
330 string cacheKey = request.GetCacheKey();
331
332 // Check cache
333 if (_tileCache.TryGetValue(cacheKey, out var cachedTile))
334 {
335 cachedTile.LastAccessed = DateTime.UtcNow;
336 return cachedTile.Image;
337 }
338
339 // Fetch directly
340 try
341 {
342 byte[] tileData = FetchTileDataSync(request);
343 if (tileData == null || tileData.Length == 0)
344 return null;
345
346 SKImage skImage = ConvertTileDataToSKImage(tileData, request);
347
348 if (skImage != null)
349 {
350 AddToCache(cacheKey, skImage);
351 }
352
353 return skImage;
354 }
355 catch (Exception ex)
356 {
357 Console.WriteLine($"Error fetching tile sync {cacheKey}: {ex.Message}");
358 return null;
359 }
360 }
361
365 private async Task<byte[]> FetchTileDataAsync(TileRequest request)
366 {
367 Console.WriteLine($"\n--- Fetching tile data for Level {request.Level}, Index[{request.Index.Col},{request.Index.Row}] ---");
368 Console.WriteLine($" Extent: ({request.Extent.MinX:F0}, {request.Extent.MinY:F0}) to ({request.Extent.MaxX:F0}, {request.Extent.MaxY:F0})");
369
370 try
371 {
372 if (_isOpenSlide)
373 {
374 // Use BruTile's GetTile method directly
375 byte[] tileData = _openSlideSource.GetTile(request.TileInfo);
376
377 if (tileData == null || tileData.Length == 0)
378 {
379 Console.WriteLine($" ERROR: OpenSlide returned null/empty tile data");
380 return null;
381 }
382
383 Console.WriteLine($" OpenSlide returned {tileData.Length} bytes");
384 return tileData;
385 }
386 else
387 {
388 // For BioLib SlideBase
389 var sliceInfo = new BioLib.SliceInfo(
390 request.Extent.MinX,
391 request.Extent.MinY,
392 request.Bounds.W,
393 request.Bounds.H,
394 CalculateResolutionFromLevel(request.Level),
395 request.Coordinate
396 );
397
398 var origin = new PointD(request.Extent.MinX, request.Extent.MinY);
399 var size = new AForge.Size((int)request.Bounds.W, (int)request.Bounds.H);
400 TileInformation tf = new TileInformation(request.Index, request.Extent, request.Coordinate);
401 byte[] tileData = await _slideSource.GetTileAsync(tf, request.Coordinate);
402
403 if (tileData == null || tileData.Length == 0)
404 {
405 Console.WriteLine($" ERROR: SlideBase returned null/empty tile data");
406 return null;
407 }
408
409 Console.WriteLine($" SlideBase returned {tileData.Length} bytes");
410 return tileData;
411 }
412 }
413 catch (Exception ex)
414 {
415 Console.WriteLine($" EXCEPTION in FetchTileDataAsync: {ex.Message}");
416 Console.WriteLine($" {ex.StackTrace}");
417 return null;
418 }
419 }
423 private byte[] FetchTileDataSync(TileRequest request)
424 {
425 if (_isOpenSlide)
426 {
427 var sliceInfo = new OpenSlideGTK.SliceInfo(
428 request.Bounds.X,
429 request.Bounds.Y,
430 request.Bounds.W,
431 request.Bounds.H,
432 CalculateResolutionFromLevel(request.Level)
433 );
434
435 return _openSlideSource.GetSlice(sliceInfo, request.Coordinate, request.Level);
436 }
437 else
438 {
439 var sliceInfo = new BioLib.SliceInfo(
440 request.Bounds.X,
441 request.Bounds.Y,
442 request.Bounds.W,
443 request.Bounds.H,
444 CalculateResolutionFromLevel(request.Level),
445 request.Coordinate
446 );
447
448 var origin = new PointD(request.Bounds.X, request.Bounds.Y);
449 var size = new AForge.Size((int)request.Bounds.W, (int)request.Bounds.H);
450
451 return _slideSource.GetSlice(sliceInfo, origin, size).Result;
452 }
453 }
454
455 #endregion
456
457 #region Tile Compositing
458
462 private async Task CompositeTilesAsync(
463 SKCanvas canvas,
464 List<TileRequest> tileRequests,
465 PointD origin,
466 double resolution)
467 {
468 if (tileRequests.Count == 0) return;
469
470 // Fetch all tiles in parallel
471 var tileTasks = tileRequests.Select(async req =>
472 {
473 var image = await FetchTileAsync(req, req.Coordinate);
474 return new { Request = req, Image = image };
475 }).ToArray();
476
477 var results = await Task.WhenAll(tileTasks);
478
479 using (var paint = new SKPaint { IsAntialias = false })
480 {
481 foreach (var result in results)
482 {
483 if (result.Image == null) continue;
484 DrawTileToCanvas(canvas, result.Request, origin, resolution, result.Image, paint);
485 }
486 }
487 }
491 private void DrawTileToCanvas(
492 SKCanvas canvas,
493 TileRequest request,
494 PointD viewportOrigin,
495 double resolution,
496 SKImage tileImage,
497 SKPaint paint = null)
498 {
499 if (tileImage == null) return;
500
501 // X: (Tile Left - Viewport Left) / Resolution
502 float destX = (float)((request.Extent.MinX - viewportOrigin.X) / resolution);
503
504 // Y: (Viewport Top - Tile Top) / Resolution
505 // In BruTile, MaxY is the Top of the tile
506 float destY = (float)((viewportOrigin.Y - request.Extent.MaxY) / resolution);
507
508 // Calculate dimensions based on the Extent to ensure precision
509 float destW = (float)(request.Extent.Width / resolution);
510 float destH = (float)(request.Extent.Height / resolution);
511
512 var destRect = new SKRect(destX, destY, destX + destW, destY + destH);
513
514 // Use Sampling if provided in the class, otherwise use the paint object
515 if (paint != null)
516 canvas.DrawImage(tileImage, destRect, paint);
517 else
518 canvas.DrawImage(tileImage, destRect, Sampling);
519 }
523 private SKPaint CreateCompositePaint()
524 {
525 return new SKPaint
526 {
527 IsAntialias = true,
528 BlendMode = SKBlendMode.SrcOver
529 };
530 }
531
535 private SKPaint CreateTilePaint()
536 {
537 return new SKPaint
538 {
539 IsAntialias = false // Tiles should align perfectly
540 };
541 }
542
543 #endregion
544
545 #region Tile Calculation and Management
546
550 private List<TileRequest> CalculateTileRequests(PointD origin, int viewportWidth, int viewportHeight, int level, ZCT coordinate, double resolution)
551 {
552 // origin.X/Y is the Top-Left of the viewport in World Space.
553 // In BruTile (OSM/TMS style), Y increases UPWARDS.
554
555 double minX = origin.X;
556 double maxX = origin.X + (viewportWidth * resolution);
557
558 // Because Y increases UP, the Top of the screen (origin.Y)
559 // is the MAXIMUM Y value.
560 double maxY = origin.Y;
561
562 // The Bottom of the screen is the MINIMUM Y value.
563 double minY = origin.Y - (viewportHeight * resolution);
564
565 // Create the extent (MinX, MinY, MaxX, MaxY)
566 var viewportExtent = new Extent(minX, minY, maxX, maxY);
567
568 IEnumerable<TileInfo> tileInfos;
569 if (_isOpenSlide)
570 {
571 tileInfos = _openSlideSource.Schema.GetTileInfos(viewportExtent, level);
572 }
573 else
574 {
575 tileInfos = _slideSource.Schema.GetTileInfos(viewportExtent, level);
576 }
577
578 var requests = new List<TileRequest>();
579 foreach (var info in tileInfos)
580 {
581 requests.Add(new TileRequest(info, coordinate));
582 }
583 return requests;
584 }
585 #endregion
586
587 #region Cache Management
588
592 private void AddToCache(string key, SKImage image)
593 {
594 var cachedTile = new CachedTile(image, key);
595
596 lock (_cacheLock)
597 {
598 _tileCache[key] = cachedTile;
599
600 // Check if we need to evict
601 long currentCacheSize = CalculateTotalCacheSize();
602
603 if (currentCacheSize > _maxCacheSize)
604 {
605 EvictLeastRecentlyUsedTiles(currentCacheSize - _maxCacheSize);
606 }
607 }
608 }
609
613 private void EvictLeastRecentlyUsedTiles(long bytesToFree)
614 {
615 var sortedTiles = new List<CachedTile>(_tileCache.Values);
616 sortedTiles.Sort((a, b) => a.LastAccessed.CompareTo(b.LastAccessed));
617
618 long freedBytes = 0;
619 foreach (var tile in sortedTiles)
620 {
621 if (freedBytes >= bytesToFree)
622 break;
623
624 if (_tileCache.TryRemove(tile.Key, out var removed))
625 {
626 freedBytes += removed.SizeBytes;
627 removed.Image?.Dispose();
628 }
629 }
630 }
631
635 private long CalculateTotalCacheSize()
636 {
637 long total = 0;
638 foreach (var tile in _tileCache.Values)
639 {
640 total += tile.SizeBytes;
641 }
642 return total;
643 }
644
648 public void ClearCache()
649 {
650 lock (_cacheLock)
651 {
652 foreach (var tile in _tileCache.Values)
653 {
654 tile.Image?.Dispose();
655 }
656 _tileCache.Clear();
657 }
658 }
659
660 #endregion
661
662 #region Utility Methods
663
667 // SkiaStitch.cs - Update this method
668 private SKImage ConvertTileDataToSKImage(byte[] tileData, TileRequest request)
669 {
670 try
671 {
672 // 1. Try standard decoding (JPEG/PNG/etc)
673 using (var data = SKData.CreateCopy(tileData))
674 {
675 var image = SKImage.FromEncodedData(data);
676 if (image != null) return image;
677 }
678
679 // 2. Fallback for Raw Pixels (The "Shear" Fix)
680 // Use the Schema's defined TileWidth/Height, NOT the calculated resolution width
681 int width = _isOpenSlide ? _openSlideSource.Schema.GetTileWidth(request.Index.Level)
682 : _slideSource.Schema.GetTileWidth(request.Index.Level);
683 int height = _isOpenSlide ? _openSlideSource.Schema.GetTileHeight(request.Index.Level)
684 : _slideSource.Schema.GetTileHeight(request.Index.Level);
685
686 var info = new SKImageInfo(width, height, SKColorType.Bgra8888, SKAlphaType.Premul);
687 // If decode fails, create from raw RGBA data
688 // CRITICAL: Don't dispose SKData until image is disposed
689 var imageInfo = new SKImageInfo(width, height, SKColorType.Rgba8888, SKAlphaType.Premul);
690 var rawData = SKData.CreateCopy(tileData);
691 var pixmap = new SKPixmap(imageInfo, rawData.Data, imageInfo.RowBytes);
692
693 // Use release delegate to properly manage lifetime
694 return SKImage.FromPixels(pixmap, (address, context) =>
695 {
696 rawData?.Dispose();
697 }, null);
698 }
699 catch (Exception ex)
700 {
701 Console.WriteLine($"Error converting tile data: {ex.Message}");
702 return null;
703 }
704 }
708 private int CalculateLevelFromResolution(double resolution)
709 {
710 if (_isOpenSlide)
711 {
712 return OpenSlideGTK.TileUtil.GetLevel(_openSlideSource.Schema.Resolutions, resolution);
713 }
714 else
715 {
716 return OpenSlideGTK.TileUtil.GetLevel(_slideSource.Schema.Resolutions, resolution);
717 }
718 }
719
723 private double CalculateResolutionFromLevel(int level)
724 {
725 if (_isOpenSlide)
726 {
727 return _openSlideSource.Schema.Resolutions[level].UnitsPerPixel;
728 }
729 else
730 {
731 return _slideSource.Schema.Resolutions[level].UnitsPerPixel;
732 }
733 }
734
735 #endregion
736
737 #region Disposal
738 public void Dispose()
739 {
740 ClearCache();
741 _pendingTileFetches.Clear();;
742 }
743 #endregion
744 }
745}
SkiaSharp-based tile stitching pipeline for compositing large pyramidal images.
Definition SkiaStitch.cs:21
SkiaStitchingPipeline(SlideBase slideSource, int maxCacheSizeMB=512)
Initialize pipeline for BioLib slide source.
void ClearCache()
Clear entire cache.
SKImage StitchRegion(RectangleD region, int level, ZCT coordinate, double resolution, int pxwidth, int pxheight)
Stitch a specific region synchronously (for smaller areas)
async Task< SKImage > StitchViewportAsync(PointD origin, double resolution, ZCT coordinate, int viewportWidth=600, int viewportHeight=400)
Stitch tiles for a given viewport asynchronously.
SkiaStitchingPipeline(OpenSlideGTK.OpenSlideBase openSlideSource, int maxCacheSizeMB=512)
Initialize pipeline for OpenSlide source.
Definition ML.cs:17