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