-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathOpenSlide.cs
527 lines (482 loc) · 21.7 KB
/
OpenSlide.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
using System;
using System.Collections.Generic;
using System.Diagnostics;
using SkiaSharp;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace OpenSlideCs
{
public unsafe sealed class OpenSlide
{
private class Openslide : IDisposable
{
internal const int TILE_SIZE = 512;
/// <summary>
/// TILE_SIZE - overlaps (1px on each side)
/// </summary>
const int TILE_DOWNSAMPLE = TILE_SIZE - 2;
/*
* For DZ we need the levels to be always twice more zoomed in. The slide file can have levels (os_levels) with ratio 1000 between them. We need to recalculate our levels.
* based on https://github.com/openslide/openslide-python/blob/master/openslide/deepzoom.py
*/
public IntPtr handle;
public int max_os_level;
/// <summary>
/// dimensions per os_level
/// </summary>
List<SizeL> dimensions = new List<SizeL>();
/// <summary>
/// downsample per os_level (how many original pixels are represented by one pixel) usually first level is 1?
/// </summary>
List<double> downsamples = new List<double>();
/// <summary>
///dimensions per dz_level (in pixels)
/// </summary>
List<SizeL> z_dimensions = new List<SizeL>();
/// <summary>
///dimensions per dz_level (in tiles)
/// </summary>
SizeL[] t_dimensions;
/// <summary>
///Total downsamples for each dz_level (powers of 2)
/// </summary>
int[] l0_z_downsamples;
/// <summary>
/// Deep zoom levels
/// </summary>
int max_dz_level;
/// <summary>
/// Best os_level for dz_level based on downsample(slide_from_dz_level)
/// </summary>
int[] os_level_for_dz_level;
/// <summary>
///
/// </summary>
double[] l_z_downsamples;
public int[] EasyLevels;
private string origfile;
private void InitZDimensions()
{
///size on a current dz_level
var z_size = dimensions[0];
z_dimensions.Add(z_size);
while (z_size.Width > 1 || z_size.Height > 1)
{
z_size = new SizeL((long)Math.Max(1, Math.Ceiling(z_size.Width / 2.0)),
(long)Math.Max(1, Math.Ceiling(z_size.Height / 2.0)));
z_dimensions.Add(z_size);
}
z_dimensions.Reverse();
}
public string GetLastError()
{
var error = CheckForLastError();
if (error != null)
throw new ArgumentException("openslide error: " + error);
else
throw new ArgumentException("openslide error, but error is empty?");
}
public string CheckForLastError()
{
var lastError = Import.openslide_get_error(handle);
if (lastError == IntPtr.Zero)
return null;
else
return Marshal.PtrToStringAnsi(lastError);
}
public Openslide(string filename)
{
OpenSlide.TraceMsg("start openslide " + filename);
origfile = filename;
if (!File.Exists(filename))
throw new ArgumentException($"File '{filename}' can't be opened");
handle = Import.openslide_open(filename);
unsafe
{
if (handle == null || handle == IntPtr.Zero)
{
var vendor = Import.openslide_detect_vendor(filename);
//GetLastError();
if (vendor == IntPtr.Zero)
throw new ArgumentException("Vendor " + Marshal.PtrToStringAnsi(vendor) + " unsupported?");
else
throw new ArgumentException("File unrecognized");
}
}
OpenSlide.TraceMsg("opened openslide " + filename);
max_os_level = Import.openslide_get_level_count(handle);
if (max_os_level == -1)
GetLastError();
for (int level = 0; level < max_os_level; level++)
{
long w = 0, h = 0;
Import.openslide_get_level_dimensions(handle, level, out w, out h);
if (w == -1 || h == -1)
GetLastError();
dimensions.Add(new SizeL(w, h));
var downsample = Import.openslide_get_level_downsample(handle, level);
if (downsample == -1.0)
GetLastError();
downsamples.Add(downsample);
}
InitZDimensions();
t_dimensions = z_dimensions.Select(x => new SizeL((long)Math.Ceiling(x.Width / (double)TILE_DOWNSAMPLE),
(long)Math.Ceiling(x.Height / (double)TILE_DOWNSAMPLE))).ToArray();
max_dz_level = z_dimensions.Count;
l0_z_downsamples = Enumerable.Range(0, max_dz_level).Select(x => (int)Math.Pow(2, max_dz_level - x - 1)).ToArray();
os_level_for_dz_level = l0_z_downsamples.Select(x =>
{
var best_level = Import.openslide_get_best_level_for_downsample(handle, x * 1.01);
if (best_level == -1)
GetLastError();
return best_level;
}).ToArray();
l_z_downsamples = Enumerable.Range(0, max_dz_level).Select(l => l0_z_downsamples[l] / downsamples[os_level_for_dz_level[l]]).ToArray();
InitEasyLevels();
OpenSlide.TraceMsg("end openslide " + filename);
}
public MemoryStream GetTile(int level, long row, long col)
{
OpenSlide.TraceMsg("start gettile " + level + "/" + col + "_" + row);
if (level < 0 || level >= max_dz_level)
throw new ArgumentException($"wrong level level {level}, row {row}, col {col}");
if (t_dimensions[level].Width <= col || t_dimensions[level].Height <= row ||
0 > col || 0 > row)
throw new ArgumentException($"wrong address level {level}, row {row}, col {col}");
var os_level = os_level_for_dz_level[level];
//Calculate top/ left and bottom/ right overlap
var z_overlap_tl = new SizeL(col == 0 ? 0 : 1,
row == 0 ? 0 : 1);
var z_overlap_br = new SizeL(col == t_dimensions[level].Width ? 0 : 1,
row == t_dimensions[level].Height ? 0 : 1);
var z_size = new SizeL(Math.Min(TILE_DOWNSAMPLE, z_dimensions[level].Width - TILE_DOWNSAMPLE * col) + z_overlap_tl.Width + z_overlap_br.Width,
Math.Min(TILE_DOWNSAMPLE, z_dimensions[level].Height - TILE_DOWNSAMPLE * row) + z_overlap_tl.Height + z_overlap_br.Height);
if (z_size.Width < 0 || z_size.Height < 0)
throw new ArgumentException($"out of bounds level {level}, row {row}, col {col}");
var z_location = new SizeL(TILE_DOWNSAMPLE * col, TILE_DOWNSAMPLE * row);
var l_location = new System.Drawing.SizeF((float)l_z_downsamples[level] * (z_location.Width - z_overlap_tl.Width),
(float)l_z_downsamples[level] * (z_location.Height - z_overlap_tl.Height));
//Round location down and size up, and add offset of active area
var l0_location = new SizeL((long)(downsamples[os_level] * l_location.Width),
(long)(downsamples[os_level] * l_location.Height));
var l_size = new SizeL((long)Math.Min(Math.Ceiling(l_z_downsamples[level] * z_size.Width), dimensions[os_level].Width),
(long)Math.Min(Math.Ceiling(l_z_downsamples[level] * z_size.Height), dimensions[os_level].Height));
OpenSlide.TraceMsg("calcs done " + level + "/" + col + "_" + row);
var bmp = ReadRegion(l0_location, os_level, l_size);
if (l_size.Width != z_size.Width || l_size.Height != z_size.Height)
{ //only resize when necessary
OpenSlide.TraceMsg("resize " + level + "/" + col + "_" + row);
var oldbmp = bmp;
bmp = new SKBitmap((int)z_size.Width, (int)z_size.Height);
oldbmp.Resize(bmp, SKBitmapResizeMethod.Box);
}
OpenSlide.TraceMsg("new bmp " + level + "/" + col + "_" + row);
var stream = new MemoryStream();
//Prints tile coords for testing
//var g = Graphics.FromImage(resizedbmp);
//g.DrawString(level + "/" + col + "_" + row, new Font(FontFamily.GenericSansSerif, 18), new SolidBrush(Color.Black), resizedbmp.Width / 2, resizedbmp.Height / 2);
bmp.Encode(new SKManagedWStream(stream), SKEncodedImageFormat.Jpeg, 80);
OpenSlide.TraceMsg("end gettile " + level + "/" + col + "_" + row);
stream.Position = 0;
return stream;
}
public long Height
{
get
{
return dimensions[0].Height;
}
}
public long Width
{
get
{
return dimensions[0].Width;
}
}
private static string GetBytesReadable(long i)
{
// Get absolute value
long absolute_i = (i < 0 ? -i : i);
// Determine the suffix and readable value
string suffix;
double readable;
if (absolute_i >= 0x1000000000000000) // Exabyte
{
suffix = "EB";
readable = (i >> 50);
}
else if (absolute_i >= 0x4000000000000) // Petabyte
{
suffix = "PB";
readable = (i >> 40);
}
else if (absolute_i >= 0x10000000000) // Terabyte
{
suffix = "TB";
readable = (i >> 30);
}
else if (absolute_i >= 0x40000000) // Gigabyte
{
suffix = "GB";
readable = (i >> 20);
}
else if (absolute_i >= 0x100000) // Megabyte
{
suffix = "MB";
readable = (i >> 10);
}
else if (absolute_i >= 0x400) // Kilobyte
{
suffix = "KB";
readable = i;
}
else
{
return i.ToString("0 B"); // Byte
}
// Divide by 1024 to get fractional value
readable = (readable / 1024);
// Return formatted number with suffix
return readable.ToString("0.### ") + suffix;
}
private SKBitmap ReadRegion(SizeL location, int level, SizeL size)
{
Stopwatch sw = new Stopwatch();
sw.Start();
OpenSlide.TraceMsg("start ReadRegion " + level + "/" + location.Height + "_" + location.Width + ": " + GetBytesReadable(size.Width * size.Height * 3));
SKBitmap bmp = new SKBitmap((int)size.Width, (int)size.Height);
bmp.SetPixel(0, 0, SKColors.AliceBlue);
bmp.LockPixels();
var bmpdata = bmp.GetPixels();
OpenSlide.TraceMsg("bmp locked " + level + "/" + location.Height + "_" + location.Width);
unsafe
{
void* p = bmpdata.ToPointer();
Import.openslide_read_region(handle, p, location.Width, location.Height, level, size.Width, size.Height);
}
OpenSlide.TraceMsg("read finished " + level + "/" + location.Height + "_" + location.Width + ": " + GetBytesReadable(size.Width * size.Height * 3 / Math.Max(sw.ElapsedMilliseconds, 1)) + "/ms");
bmp.UnlockPixels();
OpenSlide.TraceMsg("unlock bits " + level + "/" + location.Height + "_" + location.Width);
if (bmp.GetPixel(0, 0) == SKColors.Black)
{
var error = CheckForLastError();
if (error != null)
throw new ArgumentException($"error reading region loc:{location}, level:{level}, size:{size}" + error);
//else just a black image?
}
OpenSlide.TraceMsg("end ReadRegion " + level + "/" + location.Height + "_" + location.Width);
return bmp;
}
public MemoryStream ReadThumbnail(int minsize)
{
for (int i = 0; i < z_dimensions.Count; i++)
{
var d = z_dimensions[i];
if (d.Width >= minsize || d.Height >= minsize)
return GetTile(i, 0, 0);
}
return null;
}
public double GetMPP()
{
double DEFAULT_MPP = 0.19872813990461;
var prop = Import.openslide_get_property_value(handle, OPENSLIDE_PROPERTY_NAME_MPP_X);
if (prop == IntPtr.Zero)
GetLastError();
var propstring = Marshal.PtrToStringAnsi(prop);
double ret = DEFAULT_MPP;
Double.TryParse(propstring.Replace(",", "."), out ret);
if (ret < 1e-10 || ret > 1000)
{
ret = DEFAULT_MPP;
Double.TryParse(propstring.Replace(".", ","), out ret);
}
return ret;
}
/// <summary>
/// Returns levels without scaling
/// </summary>
/// <returns></returns>
internal void InitEasyLevels()
{
var ret = new List<int>();
for (int i = 0; i < max_dz_level; i++)
{
if (Math.Abs(l0_z_downsamples[i] - 1) < 0.01)
ret.Add(i);
}
EasyLevels = ret.ToArray();
}
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
}
unsafe
{
if (handle != null && handle != IntPtr.Zero)
{
Import.openslide_close(handle);
}
}
disposedValue = true;
}
}
~Openslide()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
struct SizeL
{
public long Width;
public long Height;
public SizeL(long w, long h)
{
Width = w;
Height = h;
}
public override string ToString()
{
return "w:" + Width + " h:" + Height;
}
}
/// <summary>
/// microns per pixel
/// </summary>
const string OPENSLIDE_PROPERTY_NAME_MPP_X = "openslide.mpp-x";
const string OPENSLIDE_PROPERTY_NAME_MPP_Y = "openslide.mpp-y";
static Dictionary<string, Openslide> slides = new Dictionary<string, Openslide>();
static Regex rxDZ = new Regex("_files/([0-9]*)/([0-9]*)_([0-9]*).jpeg", RegexOptions.Compiled);
static object slides_lock = new Object();
private Openslide GetOpenSlide(string filename, bool canOpenFile = true)
{
if (!slides.ContainsKey(filename))
{
lock (slides_lock)
{
if (!slides.ContainsKey(filename))
{
if (!canOpenFile)
return null;
slides.Add(filename, new Openslide(filename));
//If we want to clean up the cache
//Task.Run(async delegate
//{
// await Task.Delay(1000 * 60 * 60 * 6);
// lock (slides_lock)
// {
// if (slides.ContainsKey(filename))
// {
// slides[filename].Dispose();
// slides.Remove(filename);
// }
// }
// return 42; //gotta return something
//});
}
}
}
return slides[filename];
}
public MemoryStream GetDZI(string filename, out long width, out long height)
{
var ost = GetOpenSlide(filename);
width = ost.Width;
height = ost.Height;
return GetDZI(width, height);
}
static string dzitemplate = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Image xmlns=\"http://schemas.microsoft.com/deepzoom/2008\" Format=\"jpeg\" Overlap=\"1\" TileSize=\"@TileSize\">" +
"<Size Height=\"@Height\" Width=\"@Width\"/></Image>";
public static MemoryStream GetDZI(long width, long height)
{
var s = dzitemplate.Replace("@Width", width.ToString()).Replace("@Height", height.ToString()).Replace("@TileSize", (Openslide.TILE_SIZE - 2).ToString());
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
public MemoryStream GetJpg(string filename, string path)
{
//url = '/<ID>_files/<int:level>/<int:col>_<int:row>.<format>'
OpenSlide.TraceMsg("start getjpg " + path);
var m = rxDZ.Match(path);
if (m.Success)
{
var ost = GetOpenSlide(filename);
OpenSlide.TraceMsg("open os " + path);
var level = Int32.Parse(m.Groups[1].Value);
var col = Int64.Parse(m.Groups[2].Value);
var row = Int64.Parse(m.Groups[3].Value);
var ret = ost.GetTile(level, row, col);
OpenSlide.TraceMsg("end getjpg " + path);
return ret;
}
return null;
}
public void EnsureOpen(string filename)
{
GetOpenSlide(filename);
}
public bool IsEasyLevel(string filename, int level)
{
var ost = GetOpenSlide(filename, false);
if (ost == null)
return true; //this can happen before the background thread opens all images. Default to showing all levels
return ost.EasyLevels.Contains(level);
}
public MemoryStream GetThumbnail(string filemame, int minsize)
{
var ost = GetOpenSlide(filemame);
return ost.ReadThumbnail(minsize);
}
public double GetMPP(string filemame)
{
var ost = GetOpenSlide(filemame);
return ost.GetMPP();
}
public static Action<String> OnTrace;
private static void TraceMsg(string m)
{
if (OnTrace != null)
OnTrace(m);
}
public static Tuple<long, long> TestPerf(string testpath)
{
var ret = new Tuple<long, long>(0, 0);
var buffer = new byte[4096];
var sw = new Stopwatch();
sw.Start();
using (var f = File.OpenRead(testpath))
{
f.Read(buffer, 0, 4096);
}
var TTfirstpage = sw.ElapsedMilliseconds;
var copyto = Path.GetTempFileName();
File.Delete(copyto);
sw.Restart();
File.Copy(testpath, copyto);
var TTcompletefile = sw.ElapsedMilliseconds;
File.Delete(copyto);
return new Tuple<long, long>(TTfirstpage, TTcompletefile);
}
}
}