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
SlideRenderer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using AForge;
6using BruTile;
7using OpenSlideGTK;
8
9namespace BioGTK
10{
11 public class SlideRenderer
12 {
13 private readonly SlideGLArea _glArea;
14 private OpenSlideBase _openSlideBase;
15 private SlideBase _slideBase;
16 private bool _useOpenSlide;
17 private BioLib.TileCache _tileCache;
18 private OpenSlideGTK.TileCache _openTileCache;
19 private HashSet<TileIndex> _uploadedTiles = new();
20 private int _currentLevel = -1;
21
22 public SlideRenderer(SlideGLArea glArea)
23 {
24 _glArea = glArea;
25 }
26
27 public void SetSource(OpenSlideBase source)
28 {
29 _openSlideBase = source;
30 _slideBase = null;
31 _useOpenSlide = true;
32 _openTileCache = new OpenSlideGTK.TileCache(source, 200);
33 ClearCache();
34 }
35
36 public void SetSource(SlideBase source)
37 {
38 _slideBase = source;
39 _openSlideBase = null;
40 _useOpenSlide = false;
41 _tileCache = new BioLib.TileCache(source, 200);
42 ClearCache();
43 }
44
45 public void ClearCache()
46 {
47 _glArea.ClearTextureCache();
48 _uploadedTiles.Clear();
49 _currentLevel = -1;
50 }
51
52 public async Task UpdateViewAsync(
53 PointD pyramidalOrigin,
54 int viewportWidth,
55 int viewportHeight,
56 double resolution,
57 ZCT coordinate)
58 {
59 if (_openSlideBase == null && _slideBase == null)
60 return;
61 if (viewportWidth <= 1 && viewportHeight <= 1)
62 return;
63 var schema = _useOpenSlide ? _openSlideBase.Schema : _slideBase.Schema;
64
65 int level = TileUtil.GetLevel(schema.Resolutions, resolution);
66 var levelRes = schema.Resolutions[level];
67 double levelUnitsPerPixel = levelRes.UnitsPerPixel;
68
69 if (level != _currentLevel)
70 {
71 _glArea.ReleaseLevelTextures(_currentLevel);
72 _currentLevel = level;
73 }
74
75 // Calculate world extent for the viewport
76 double minX = pyramidalOrigin.X * resolution;
77 double minY = -pyramidalOrigin.Y * resolution;
78 double width = viewportWidth * resolution;
79 double height = viewportHeight * resolution;
80 var worldExtent = new Extent(minX, minY - height, minX + width, minY);
81
82 // Get tiles that intersect the viewport
83 var tileInfos = schema.GetTileInfos(worldExtent, level).ToList();
84
85 if (tileInfos.Count == 0)
86 {
87 var pixelExtent = OpenSlideGTK.ExtentEx.WorldToPixelInvertedY(worldExtent, resolution);
88 tileInfos = schema.GetTileInfos(pixelExtent, level).ToList();
89 }
90
91 if(_openSlideBase != null)
92 await _openSlideBase.FetchTilesAsync(tileInfos.ToList(), level, coordinate);
93 else
94 await _slideBase.FetchTilesAsync(tileInfos.ToList(), level, coordinate, pyramidalOrigin, new Size(viewportWidth,viewportHeight));
95
96 var renderInfos = new List<TileRenderInfo>();
97 foreach (var tileInfo in tileInfos)
98 {
99 if (!_glArea.HasTileTexture(tileInfo.Index))
100 {
101 byte[] tileData;
102 if (_useOpenSlide)
103 tileData = await _openSlideBase.GetTileAsync(tileInfo);
104 else
105 tileData = await _slideBase.GetTileAsync(tileInfo,coordinate);
106 if (tileData != null)
107 {
108 // Fix: Calculate actual tile dimensions from extent, do not hardcode 256.
109 // This handles edge tiles that might be smaller.
110 int tW = (int)Math.Round(tileInfo.Extent.Width / levelUnitsPerPixel);
111 int tH = (int)Math.Round(tileInfo.Extent.Height / levelUnitsPerPixel);
112
113 _glArea.UploadTileTexture(tileInfo.Index, tileData, tW, tH);
114 _uploadedTiles.Add(tileInfo.Index);
115 }
116 }
117
118 if (_glArea.HasTileTexture(tileInfo.Index))
119 {
120 var renderInfo = CalculateScreenPosition(
121 tileInfo,
122 pyramidalOrigin,
123 resolution,
124 level,
125 schema);
126 renderInfos.Add(renderInfo);
127 }
128 }
129
130 _glArea.SetTilesToRender(renderInfos);
131 _glArea.RequestRedraw();
132 }
133
134 private async Task<byte[]> FetchTileAsync(TileInfo tileInfo, int level, ZCT coordinate)
135 {
136 try
137 {
138 if (_useOpenSlide)
139 {
140 var bt = await _openTileCache.GetTile(new OpenSlideGTK.Info(coordinate, tileInfo.Index, tileInfo.Extent, level));
141 return bt;
142 }
143 else
144 {
145 return await _tileCache.GetTile(new BioLib.TileInformation(tileInfo.Index, tileInfo.Extent, coordinate));
146 }
147 }
148 catch (Exception ex)
149 {
150 Console.WriteLine($"Error fetching tile {tileInfo.Index}: {ex.Message}");
151 return null;
152 }
153 }
154
155 private TileRenderInfo CalculateScreenPosition(
156 TileInfo tile,
157 PointD pyramidalOrigin,
158 double viewResolution,
159 int level,
160 ITileSchema schema)
161 {
162 // Get the actual tile size from schema (typically 256x256)
163 int tileWidth = schema.GetTileWidth(level);
164 int tileHeight = schema.GetTileHeight(level);
165
166 // Convert tile extent to pixel coordinates
167 var pixelExtent = OpenSlideGTK.ExtentEx.WorldToPixelInvertedY(tile.Extent, viewResolution);
168
169 // Use approach 1: MinY with positive origin
170 double tileLeftPx = pixelExtent.MinX;
171 double tileTopPx = pixelExtent.MinY;
172 double viewXPx = pyramidalOrigin.X;
173 double viewYPx = pyramidalOrigin.Y;
174
175 float screenX = (float)(tileLeftPx - viewXPx);
176 float screenY = (float)(tileTopPx - viewYPx);
177
178 // KEY FIX: Use the extent dimensions directly, not fixed tile size
179 // The extent already accounts for any edge tiles that might be smaller
180 float screenWidth = (float)pixelExtent.Width;
181 float screenHeight = (float)pixelExtent.Height;
182
183 // Comprehensive debug logging
184 // Console.WriteLine(...) // Reduced noise for production
185
186 return new TileRenderInfo(tile.Index, screenX, screenY, screenWidth, screenHeight);
187 }
188
189 public int CurrentLevel => _currentLevel;
190 public int CachedTextureCount => _uploadedTiles.Count;
191 }
192}
A GTK GLArea widget that renders pyramidal slide tiles directly on the GPU. Eliminates ReadPixels ove...
Definition ML.cs:17
Information needed to render a single tile.